From afced0b704d888cd45b5cd6da083acba4af951c9 Mon Sep 17 00:00:00 2001 From: zhangliang Date: Wed, 6 Nov 2024 19:41:14 +0800 Subject: [PATCH] Refactor ShadowDMLStatementDataSourceMappingsRetriever --- ...LStatementDataSourceMappingsRetriever.java | 30 ++++++------------- ...adowColumnDataSourceMappingsRetriever.java | 8 ----- ...eStatementDataSourceMappingsRetriever.java | 8 ++--- ...tStatementDataSourceMappingsRetriever.java | 8 ++--- ...tStatementDataSourceMappingsRetriever.java | 9 +++--- ...eStatementDataSourceMappingsRetriever.java | 8 ++--- 6 files changed, 26 insertions(+), 45 deletions(-) diff --git a/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/route/retriever/dml/ShadowDMLStatementDataSourceMappingsRetriever.java b/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/route/retriever/dml/ShadowDMLStatementDataSourceMappingsRetriever.java index b42cefe88151d..861ca0cde3cf2 100644 --- a/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/route/retriever/dml/ShadowDMLStatementDataSourceMappingsRetriever.java +++ b/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/route/retriever/dml/ShadowDMLStatementDataSourceMappingsRetriever.java @@ -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; /** @@ -45,47 +43,37 @@ @HighFrequencyInvocation public final class ShadowDMLStatementDataSourceMappingsRetriever implements ShadowDataSourceMappingsRetriever { - private final Map tableAliasAndNameMappings; + private final Map 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 getTableAliasAndNameMappings(final Collection tableSegments) { - Map 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 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 retrieve(final ShadowRule rule) { - Collection shadowTables = rule.filterShadowTables(tableAliasAndNameMappings.values()); + Collection shadowTables = rule.filterShadowTables(tableAliasNameMap.values()); Map result = tableHintDataSourceMappingsRetriever.retrieve(rule, shadowTables); return result.isEmpty() && null != shadowColumnDataSourceMappingsRetriever ? shadowColumnDataSourceMappingsRetriever.retrieve(rule, shadowTables) : result; } diff --git a/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/route/retriever/dml/table/column/ShadowColumnDataSourceMappingsRetriever.java b/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/route/retriever/dml/table/column/ShadowColumnDataSourceMappingsRetriever.java index 9b2cd17851168..1b3552d57fc3f 100644 --- a/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/route/retriever/dml/table/column/ShadowColumnDataSourceMappingsRetriever.java +++ b/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/route/retriever/dml/table/column/ShadowColumnDataSourceMappingsRetriever.java @@ -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; @@ -41,9 +40,6 @@ public abstract class ShadowColumnDataSourceMappingsRetriever implements ShadowT private final ShadowOperationType operationType; - @Getter - private final Map tableAliasAndNameMappings; - @Override public Map retrieve(final ShadowRule rule, final Collection shadowTables) { for (String each : shadowTables) { @@ -87,8 +83,4 @@ private boolean isMatchColumnShadowAlgorithm(final String shadowTable, final Col } protected abstract Collection getShadowColumnConditions(String shadowColumnName); - - protected final String getSingleTableName() { - return tableAliasAndNameMappings.values().iterator().next(); - } } diff --git a/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/route/retriever/dml/table/column/ShadowDeleteStatementDataSourceMappingsRetriever.java b/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/route/retriever/dml/table/column/ShadowDeleteStatementDataSourceMappingsRetriever.java index 432d75d2793fc..1064e7b9c7a15 100644 --- a/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/route/retriever/dml/table/column/ShadowDeleteStatementDataSourceMappingsRetriever.java +++ b/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/route/retriever/dml/table/column/ShadowDeleteStatementDataSourceMappingsRetriever.java @@ -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. @@ -42,8 +41,8 @@ public final class ShadowDeleteStatementDataSourceMappingsRetriever extends Shad private final List parameters; - public ShadowDeleteStatementDataSourceMappingsRetriever(final DeleteStatementContext sqlStatementContext, final List parameters, final Map tableAliasAndNameMappings) { - super(ShadowOperationType.DELETE, tableAliasAndNameMappings); + public ShadowDeleteStatementDataSourceMappingsRetriever(final DeleteStatementContext sqlStatementContext, final List parameters) { + super(ShadowOperationType.DELETE); this.sqlStatementContext = sqlStatementContext; this.parameters = parameters; } @@ -52,7 +51,8 @@ public ShadowDeleteStatementDataSourceMappingsRetriever(final DeleteStatementCon protected Collection getShadowColumnConditions(final String shadowColumnName) { Collection 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; } diff --git a/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/route/retriever/dml/table/column/ShadowInsertStatementDataSourceMappingsRetriever.java b/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/route/retriever/dml/table/column/ShadowInsertStatementDataSourceMappingsRetriever.java index 3a920f9ef8d28..5f3f6da36cb39 100644 --- a/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/route/retriever/dml/table/column/ShadowInsertStatementDataSourceMappingsRetriever.java +++ b/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/route/retriever/dml/table/column/ShadowInsertStatementDataSourceMappingsRetriever.java @@ -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. @@ -38,8 +37,8 @@ public final class ShadowInsertStatementDataSourceMappingsRetriever extends Shad private final InsertStatementContext sqlStatementContext; - public ShadowInsertStatementDataSourceMappingsRetriever(final InsertStatementContext sqlStatementContext, final Map tableAliasAndNameMappings) { - super(ShadowOperationType.INSERT, tableAliasAndNameMappings); + public ShadowInsertStatementDataSourceMappingsRetriever(final InsertStatementContext sqlStatementContext) { + super(ShadowOperationType.INSERT); this.sqlStatementContext = sqlStatementContext; } @@ -54,7 +53,8 @@ protected Collection getShadowColumnConditions(final Stri } Collection> 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; } diff --git a/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/route/retriever/dml/table/column/ShadowSelectStatementDataSourceMappingsRetriever.java b/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/route/retriever/dml/table/column/ShadowSelectStatementDataSourceMappingsRetriever.java index e0a2caa0ea4ab..b7e767e3cac4d 100644 --- a/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/route/retriever/dml/table/column/ShadowSelectStatementDataSourceMappingsRetriever.java +++ b/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/route/retriever/dml/table/column/ShadowSelectStatementDataSourceMappingsRetriever.java @@ -33,7 +33,6 @@ import java.util.Collection; import java.util.LinkedList; import java.util.List; -import java.util.Map; import java.util.Optional; /** @@ -46,8 +45,8 @@ public final class ShadowSelectStatementDataSourceMappingsRetriever extends Shad private final List parameters; - public ShadowSelectStatementDataSourceMappingsRetriever(final SelectStatementContext sqlStatementContext, final List parameters, final Map tableAliasAndNameMappings) { - super(ShadowOperationType.SELECT, tableAliasAndNameMappings); + public ShadowSelectStatementDataSourceMappingsRetriever(final SelectStatementContext sqlStatementContext, final List parameters) { + super(ShadowOperationType.SELECT); this.sqlStatementContext = sqlStatementContext; this.parameters = parameters; } @@ -77,6 +76,8 @@ private Collection getWhereSegment() { private String getOwnerTableName(final ColumnSegment columnSegment) { Optional 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(); } } diff --git a/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/route/retriever/dml/table/column/ShadowUpdateStatementDataSourceMappingsRetriever.java b/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/route/retriever/dml/table/column/ShadowUpdateStatementDataSourceMappingsRetriever.java index a9c8d31fdba20..ef143b67fe1dc 100644 --- a/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/route/retriever/dml/table/column/ShadowUpdateStatementDataSourceMappingsRetriever.java +++ b/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/route/retriever/dml/table/column/ShadowUpdateStatementDataSourceMappingsRetriever.java @@ -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. @@ -44,8 +43,8 @@ public final class ShadowUpdateStatementDataSourceMappingsRetriever extends Shad private final List parameters; - public ShadowUpdateStatementDataSourceMappingsRetriever(final UpdateStatementContext sqlStatementContext, final List parameters, final Map tableAliasAndNameMappings) { - super(ShadowOperationType.UPDATE, tableAliasAndNameMappings); + public ShadowUpdateStatementDataSourceMappingsRetriever(final UpdateStatementContext sqlStatementContext, final List parameters) { + super(ShadowOperationType.UPDATE); this.sqlStatementContext = sqlStatementContext; this.parameters = parameters; } @@ -58,7 +57,8 @@ protected Collection 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; }