Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor ShadowDMLStatementDataSourceMappingsRetriever #33568

Merged
merged 1 commit into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,8 @@
import org.apache.shardingsphere.shadow.route.retriever.dml.table.hint.ShadowTableHintDataSourceMappingsRetriever;
import org.apache.shardingsphere.shadow.rule.ShadowRule;
import org.apache.shardingsphere.shadow.spi.ShadowOperationType;
import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.SimpleTableSegment;

import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;

/**
Expand All @@ -45,47 +43,37 @@
@HighFrequencyInvocation
public final class ShadowDMLStatementDataSourceMappingsRetriever implements ShadowDataSourceMappingsRetriever {

private final Map<String, String> tableAliasAndNameMappings;
private final Map<String, String> tableAliasNameMap;

private final ShadowTableHintDataSourceMappingsRetriever tableHintDataSourceMappingsRetriever;

private final ShadowColumnDataSourceMappingsRetriever shadowColumnDataSourceMappingsRetriever;

public ShadowDMLStatementDataSourceMappingsRetriever(final QueryContext queryContext, final ShadowOperationType operationType) {
tableAliasAndNameMappings = getTableAliasAndNameMappings(((TableAvailable) queryContext.getSqlStatementContext()).getTablesContext().getSimpleTables());
tableAliasNameMap = ((TableAvailable) queryContext.getSqlStatementContext()).getTablesContext().getTableAliasNameMap();
tableHintDataSourceMappingsRetriever = new ShadowTableHintDataSourceMappingsRetriever(operationType, queryContext.getHintValueContext().isShadow());
shadowColumnDataSourceMappingsRetriever = createShadowDataSourceMappingsRetriever(queryContext, tableAliasAndNameMappings);
shadowColumnDataSourceMappingsRetriever = createShadowDataSourceMappingsRetriever(queryContext);
}

private Map<String, String> getTableAliasAndNameMappings(final Collection<SimpleTableSegment> tableSegments) {
Map<String, String> result = new LinkedHashMap<>(tableSegments.size(), 1F);
for (SimpleTableSegment each : tableSegments) {
String tableName = each.getTableName().getIdentifier().getValue();
String alias = each.getAliasName().isPresent() ? each.getAliasName().get() : tableName;
result.put(alias, tableName);
}
return result;
}

private ShadowColumnDataSourceMappingsRetriever createShadowDataSourceMappingsRetriever(final QueryContext queryContext, final Map<String, String> tableAliasAndNameMappings) {
private ShadowColumnDataSourceMappingsRetriever createShadowDataSourceMappingsRetriever(final QueryContext queryContext) {
if (queryContext.getSqlStatementContext() instanceof InsertStatementContext) {
return new ShadowInsertStatementDataSourceMappingsRetriever((InsertStatementContext) queryContext.getSqlStatementContext(), tableAliasAndNameMappings);
return new ShadowInsertStatementDataSourceMappingsRetriever((InsertStatementContext) queryContext.getSqlStatementContext());
}
if (queryContext.getSqlStatementContext() instanceof DeleteStatementContext) {
return new ShadowDeleteStatementDataSourceMappingsRetriever((DeleteStatementContext) queryContext.getSqlStatementContext(), queryContext.getParameters(), tableAliasAndNameMappings);
return new ShadowDeleteStatementDataSourceMappingsRetriever((DeleteStatementContext) queryContext.getSqlStatementContext(), queryContext.getParameters());
}
if (queryContext.getSqlStatementContext() instanceof UpdateStatementContext) {
return new ShadowUpdateStatementDataSourceMappingsRetriever((UpdateStatementContext) queryContext.getSqlStatementContext(), queryContext.getParameters(), tableAliasAndNameMappings);
return new ShadowUpdateStatementDataSourceMappingsRetriever((UpdateStatementContext) queryContext.getSqlStatementContext(), queryContext.getParameters());
}
if (queryContext.getSqlStatementContext() instanceof SelectStatementContext) {
return new ShadowSelectStatementDataSourceMappingsRetriever((SelectStatementContext) queryContext.getSqlStatementContext(), queryContext.getParameters(), tableAliasAndNameMappings);
return new ShadowSelectStatementDataSourceMappingsRetriever((SelectStatementContext) queryContext.getSqlStatementContext(), queryContext.getParameters());
}
return null;
}

@Override
public Map<String, String> retrieve(final ShadowRule rule) {
Collection<String> shadowTables = rule.filterShadowTables(tableAliasAndNameMappings.values());
Collection<String> shadowTables = rule.filterShadowTables(tableAliasNameMap.values());
Map<String, String> result = tableHintDataSourceMappingsRetriever.retrieve(rule, shadowTables);
return result.isEmpty() && null != shadowColumnDataSourceMappingsRetriever ? shadowColumnDataSourceMappingsRetriever.retrieve(rule, shadowTables) : result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

package org.apache.shardingsphere.shadow.route.retriever.dml.table.column;

import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.infra.annotation.HighFrequencyInvocation;
import org.apache.shardingsphere.shadow.condition.ShadowColumnCondition;
Expand All @@ -41,9 +40,6 @@ public abstract class ShadowColumnDataSourceMappingsRetriever implements ShadowT

private final ShadowOperationType operationType;

@Getter
private final Map<String, String> tableAliasAndNameMappings;

@Override
public Map<String, String> retrieve(final ShadowRule rule, final Collection<String> shadowTables) {
for (String each : shadowTables) {
Expand Down Expand Up @@ -87,8 +83,4 @@ private boolean isMatchColumnShadowAlgorithm(final String shadowTable, final Col
}

protected abstract Collection<ShadowColumnCondition> getShadowColumnConditions(String shadowColumnName);

protected final String getSingleTableName() {
return tableAliasAndNameMappings.values().iterator().next();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
* Shadow delete statement data source mappings retriever.
Expand All @@ -42,8 +41,8 @@ public final class ShadowDeleteStatementDataSourceMappingsRetriever extends Shad

private final List<Object> parameters;

public ShadowDeleteStatementDataSourceMappingsRetriever(final DeleteStatementContext sqlStatementContext, final List<Object> parameters, final Map<String, String> tableAliasAndNameMappings) {
super(ShadowOperationType.DELETE, tableAliasAndNameMappings);
public ShadowDeleteStatementDataSourceMappingsRetriever(final DeleteStatementContext sqlStatementContext, final List<Object> parameters) {
super(ShadowOperationType.DELETE);
this.sqlStatementContext = sqlStatementContext;
this.parameters = parameters;
}
Expand All @@ -52,7 +51,8 @@ public ShadowDeleteStatementDataSourceMappingsRetriever(final DeleteStatementCon
protected Collection<ShadowColumnCondition> getShadowColumnConditions(final String shadowColumnName) {
Collection<ShadowColumnCondition> result = new LinkedList<>();
for (ExpressionSegment each : getWhereSegment()) {
ShadowExtractor.extractValues(each, parameters).map(values -> new ShadowColumnCondition(getSingleTableName(), shadowColumnName, values)).ifPresent(result::add);
String tableName = sqlStatementContext.getTablesContext().getTableNames().iterator().next();
ShadowExtractor.extractValues(each, parameters).map(values -> new ShadowColumnCondition(tableName, shadowColumnName, values)).ifPresent(result::add);
}
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
* Shadow insert statement data source mappings retriever.
Expand All @@ -38,8 +37,8 @@ public final class ShadowInsertStatementDataSourceMappingsRetriever extends Shad

private final InsertStatementContext sqlStatementContext;

public ShadowInsertStatementDataSourceMappingsRetriever(final InsertStatementContext sqlStatementContext, final Map<String, String> tableAliasAndNameMappings) {
super(ShadowOperationType.INSERT, tableAliasAndNameMappings);
public ShadowInsertStatementDataSourceMappingsRetriever(final InsertStatementContext sqlStatementContext) {
super(ShadowOperationType.INSERT);
this.sqlStatementContext = sqlStatementContext;
}

Expand All @@ -54,7 +53,8 @@ protected Collection<ShadowColumnCondition> getShadowColumnConditions(final Stri
}
Collection<Comparable<?>> columnValues = getColumnValues(sqlStatementContext.getInsertValueContexts(), columnIndex);
columnIndex++;
result.add(new ShadowColumnCondition(getSingleTableName(), each, columnValues));
String tableName = sqlStatementContext.getTablesContext().getTableNames().iterator().next();
result.add(new ShadowColumnCondition(tableName, each, columnValues));
}
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
Expand All @@ -46,8 +45,8 @@ public final class ShadowSelectStatementDataSourceMappingsRetriever extends Shad

private final List<Object> parameters;

public ShadowSelectStatementDataSourceMappingsRetriever(final SelectStatementContext sqlStatementContext, final List<Object> parameters, final Map<String, String> tableAliasAndNameMappings) {
super(ShadowOperationType.SELECT, tableAliasAndNameMappings);
public ShadowSelectStatementDataSourceMappingsRetriever(final SelectStatementContext sqlStatementContext, final List<Object> parameters) {
super(ShadowOperationType.SELECT);
this.sqlStatementContext = sqlStatementContext;
this.parameters = parameters;
}
Expand Down Expand Up @@ -77,6 +76,8 @@ private Collection<ExpressionSegment> getWhereSegment() {

private String getOwnerTableName(final ColumnSegment columnSegment) {
Optional<OwnerSegment> owner = columnSegment.getOwner();
return owner.isPresent() ? getTableAliasAndNameMappings().get(owner.get().getIdentifier().getValue()) : getTableAliasAndNameMappings().values().iterator().next();
return owner.isPresent()
? sqlStatementContext.getTablesContext().getTableAliasNameMap().get(owner.get().getIdentifier().getValue())
: sqlStatementContext.getTablesContext().getTableNames().iterator().next();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
* Shadow update statement data source mappings retriever.
Expand All @@ -44,8 +43,8 @@ public final class ShadowUpdateStatementDataSourceMappingsRetriever extends Shad

private final List<Object> parameters;

public ShadowUpdateStatementDataSourceMappingsRetriever(final UpdateStatementContext sqlStatementContext, final List<Object> parameters, final Map<String, String> tableAliasAndNameMappings) {
super(ShadowOperationType.UPDATE, tableAliasAndNameMappings);
public ShadowUpdateStatementDataSourceMappingsRetriever(final UpdateStatementContext sqlStatementContext, final List<Object> parameters) {
super(ShadowOperationType.UPDATE);
this.sqlStatementContext = sqlStatementContext;
this.parameters = parameters;
}
Expand All @@ -58,7 +57,8 @@ protected Collection<ShadowColumnCondition> getShadowColumnConditions(final Stri
if (1 != columns.size()) {
continue;
}
ShadowExtractor.extractValues(each, parameters).map(values -> new ShadowColumnCondition(getSingleTableName(), shadowColumnName, values)).ifPresent(result::add);
String tableName = sqlStatementContext.getTablesContext().getTableNames().iterator().next();
ShadowExtractor.extractValues(each, parameters).map(values -> new ShadowColumnCondition(tableName, shadowColumnName, values)).ifPresent(result::add);
}
return result;
}
Expand Down