diff --git a/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/route/retriever/ShadowDataSourceMappingsRetrieverFactory.java b/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/route/retriever/ShadowDataSourceMappingsRetrieverFactory.java index 44ca22b3b848b..a1bf5eddefade 100644 --- a/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/route/retriever/ShadowDataSourceMappingsRetrieverFactory.java +++ b/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/route/retriever/ShadowDataSourceMappingsRetrieverFactory.java @@ -19,16 +19,17 @@ import lombok.AccessLevel; import lombok.NoArgsConstructor; +import org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext; import org.apache.shardingsphere.infra.binder.context.statement.dml.DeleteStatementContext; import org.apache.shardingsphere.infra.binder.context.statement.dml.InsertStatementContext; import org.apache.shardingsphere.infra.binder.context.statement.dml.SelectStatementContext; import org.apache.shardingsphere.infra.binder.context.statement.dml.UpdateStatementContext; import org.apache.shardingsphere.infra.session.query.QueryContext; -import org.apache.shardingsphere.shadow.route.retriever.dml.ShadowDeleteStatementDataSourceMappingsRetriever; -import org.apache.shardingsphere.shadow.route.retriever.dml.ShadowInsertStatementDataSourceMappingsRetriever; -import org.apache.shardingsphere.shadow.route.retriever.dml.ShadowSelectStatementDataSourceMappingsRetriever; -import org.apache.shardingsphere.shadow.route.retriever.dml.ShadowUpdateStatementDataSourceMappingsRetriever; +import org.apache.shardingsphere.shadow.route.retriever.dml.ShadowDMLStatementDataSourceMappingsRetriever; import org.apache.shardingsphere.shadow.route.retriever.hint.ShadowHintDataSourceMappingsRetriever; +import org.apache.shardingsphere.shadow.spi.ShadowOperationType; + +import java.util.Optional; /** * Shadow data source mappings retriever factory. @@ -43,22 +44,25 @@ public final class ShadowDataSourceMappingsRetrieverFactory { * @return created instance */ public static ShadowDataSourceMappingsRetriever newInstance(final QueryContext queryContext) { - if (queryContext.getSqlStatementContext() instanceof InsertStatementContext) { - return new ShadowInsertStatementDataSourceMappingsRetriever( - (InsertStatementContext) queryContext.getSqlStatementContext(), queryContext.getHintValueContext()); + Optional operationType = getShadowOperationType(queryContext.getSqlStatementContext()); + return operationType.isPresent() + ? new ShadowDMLStatementDataSourceMappingsRetriever(queryContext, operationType.get()) + : new ShadowHintDataSourceMappingsRetriever(queryContext.getHintValueContext()); + } + + private static Optional getShadowOperationType(final SQLStatementContext sqlStatementContext) { + if (sqlStatementContext instanceof InsertStatementContext) { + return Optional.of(ShadowOperationType.INSERT); } - if (queryContext.getSqlStatementContext() instanceof DeleteStatementContext) { - return new ShadowDeleteStatementDataSourceMappingsRetriever( - (DeleteStatementContext) queryContext.getSqlStatementContext(), queryContext.getParameters(), queryContext.getHintValueContext()); + if (sqlStatementContext instanceof DeleteStatementContext) { + return Optional.of(ShadowOperationType.DELETE); } - if (queryContext.getSqlStatementContext() instanceof UpdateStatementContext) { - return new ShadowUpdateStatementDataSourceMappingsRetriever( - (UpdateStatementContext) queryContext.getSqlStatementContext(), queryContext.getParameters(), queryContext.getHintValueContext()); + if (sqlStatementContext instanceof UpdateStatementContext) { + return Optional.of(ShadowOperationType.UPDATE); } - if (queryContext.getSqlStatementContext() instanceof SelectStatementContext) { - return new ShadowSelectStatementDataSourceMappingsRetriever( - (SelectStatementContext) queryContext.getSqlStatementContext(), queryContext.getParameters(), queryContext.getHintValueContext()); + if (sqlStatementContext instanceof SelectStatementContext) { + return Optional.of(ShadowOperationType.SELECT); } - return new ShadowHintDataSourceMappingsRetriever(queryContext.getHintValueContext()); + return Optional.empty(); } } 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 new file mode 100644 index 0000000000000..2971b1e35c164 --- /dev/null +++ b/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/route/retriever/dml/ShadowDMLStatementDataSourceMappingsRetriever.java @@ -0,0 +1,92 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.shardingsphere.shadow.route.retriever.dml; + +import org.apache.shardingsphere.infra.annotation.HighFrequencyInvocation; +import org.apache.shardingsphere.infra.binder.context.statement.dml.DeleteStatementContext; +import org.apache.shardingsphere.infra.binder.context.statement.dml.InsertStatementContext; +import org.apache.shardingsphere.infra.binder.context.statement.dml.SelectStatementContext; +import org.apache.shardingsphere.infra.binder.context.statement.dml.UpdateStatementContext; +import org.apache.shardingsphere.infra.binder.context.type.TableAvailable; +import org.apache.shardingsphere.infra.session.query.QueryContext; +import org.apache.shardingsphere.shadow.route.retriever.ShadowDataSourceMappingsRetriever; +import org.apache.shardingsphere.shadow.route.retriever.dml.table.column.ShadowColumnDataSourceMappingsRetriever; +import org.apache.shardingsphere.shadow.route.retriever.dml.table.column.ShadowDeleteStatementDataSourceMappingsRetriever; +import org.apache.shardingsphere.shadow.route.retriever.dml.table.column.ShadowInsertStatementDataSourceMappingsRetriever; +import org.apache.shardingsphere.shadow.route.retriever.dml.table.column.ShadowSelectStatementDataSourceMappingsRetriever; +import org.apache.shardingsphere.shadow.route.retriever.dml.table.column.ShadowUpdateStatementDataSourceMappingsRetriever; +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; + +/** + * Shadow DML statement data source mappings retriever. + */ +@HighFrequencyInvocation +public final class ShadowDMLStatementDataSourceMappingsRetriever implements ShadowDataSourceMappingsRetriever { + + private final Map tableAliasAndNameMappings; + + private final ShadowTableHintDataSourceMappingsRetriever tableHintDataSourceMappingsRetriever; + + private final ShadowColumnDataSourceMappingsRetriever shadowColumnDataSourceMappingsRetriever; + + public ShadowDMLStatementDataSourceMappingsRetriever(final QueryContext queryContext, final ShadowOperationType operationType) { + tableAliasAndNameMappings = getTableAliasAndNameMappings(((TableAvailable) queryContext.getSqlStatementContext()).getTablesContext().getSimpleTables()); + tableHintDataSourceMappingsRetriever = new ShadowTableHintDataSourceMappingsRetriever(operationType, queryContext.getHintValueContext().isShadow()); + shadowColumnDataSourceMappingsRetriever = createShadowDataSourceMappingsRetriever(queryContext, tableAliasAndNameMappings); + } + + 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) { + if (queryContext.getSqlStatementContext() instanceof InsertStatementContext) { + return new ShadowInsertStatementDataSourceMappingsRetriever((InsertStatementContext) queryContext.getSqlStatementContext(), tableAliasAndNameMappings); + } + if (queryContext.getSqlStatementContext() instanceof DeleteStatementContext) { + return new ShadowDeleteStatementDataSourceMappingsRetriever((DeleteStatementContext) queryContext.getSqlStatementContext(), queryContext.getParameters(), tableAliasAndNameMappings); + } + if (queryContext.getSqlStatementContext() instanceof UpdateStatementContext) { + return new ShadowUpdateStatementDataSourceMappingsRetriever((UpdateStatementContext) queryContext.getSqlStatementContext(), queryContext.getParameters(), tableAliasAndNameMappings); + } + if (queryContext.getSqlStatementContext() instanceof SelectStatementContext) { + return new ShadowSelectStatementDataSourceMappingsRetriever((SelectStatementContext) queryContext.getSqlStatementContext(), queryContext.getParameters(), tableAliasAndNameMappings); + } + throw new UnsupportedOperationException(String.format("unsupported SQL statement context `%s`", queryContext.getSqlStatementContext().getClass().getName())); + } + + @Override + public Map retrieve(final ShadowRule rule) { + Collection shadowTables = rule.filterShadowTables(tableAliasAndNameMappings.values()); + Map result = tableHintDataSourceMappingsRetriever.retrieve(rule, shadowTables); + return result.isEmpty() ? shadowColumnDataSourceMappingsRetriever.retrieve(rule, shadowTables) : result; + } +} diff --git a/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/route/retriever/dml/table/ShadowTableDataSourceMappingsRetriever.java b/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/route/retriever/dml/table/ShadowTableDataSourceMappingsRetriever.java new file mode 100644 index 0000000000000..fdb2e8bf29bdf --- /dev/null +++ b/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/route/retriever/dml/table/ShadowTableDataSourceMappingsRetriever.java @@ -0,0 +1,38 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.shardingsphere.shadow.route.retriever.dml.table; + +import org.apache.shardingsphere.shadow.rule.ShadowRule; + +import java.util.Collection; +import java.util.Map; + +/** + * Shadow table data source mappings retriever. + */ +public interface ShadowTableDataSourceMappingsRetriever { + + /** + * Retrieve shadow data source mappings. + * + * @param rule shadow rule + * @param shadowTables shadow tables + * @return retrieved shadow data source mappings + */ + Map retrieve(ShadowRule rule, Collection shadowTables); +} diff --git a/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/route/retriever/dml/AbstractShadowDMLStatementDataSourceMappingsRetriever.java b/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/route/retriever/dml/table/column/ShadowColumnDataSourceMappingsRetriever.java similarity index 60% rename from features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/route/retriever/dml/AbstractShadowDMLStatementDataSourceMappingsRetriever.java rename to features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/route/retriever/dml/table/column/ShadowColumnDataSourceMappingsRetriever.java index f109c823db7ec..652012d446971 100644 --- a/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/route/retriever/dml/AbstractShadowDMLStatementDataSourceMappingsRetriever.java +++ b/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/route/retriever/dml/table/column/ShadowColumnDataSourceMappingsRetriever.java @@ -15,64 +15,37 @@ * limitations under the License. */ -package org.apache.shardingsphere.shadow.route.retriever.dml; +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.infra.binder.context.statement.SQLStatementContext; -import org.apache.shardingsphere.infra.binder.context.type.TableAvailable; -import org.apache.shardingsphere.infra.hint.HintValueContext; import org.apache.shardingsphere.shadow.condition.ShadowColumnCondition; import org.apache.shardingsphere.shadow.condition.ShadowCondition; import org.apache.shardingsphere.shadow.route.determiner.ColumnShadowAlgorithmDeterminer; -import org.apache.shardingsphere.shadow.route.retriever.ShadowDataSourceMappingsRetriever; -import org.apache.shardingsphere.shadow.route.retriever.hint.ShadowTableHintDataSourceMappingsRetriever; +import org.apache.shardingsphere.shadow.route.retriever.dml.table.ShadowTableDataSourceMappingsRetriever; import org.apache.shardingsphere.shadow.rule.ShadowRule; import org.apache.shardingsphere.shadow.spi.ShadowOperationType; import org.apache.shardingsphere.shadow.spi.column.ColumnShadowAlgorithm; -import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.SimpleTableSegment; import java.util.Collection; import java.util.Collections; -import java.util.LinkedHashMap; import java.util.Map; /** - * Abstract shadow DML statement data source mappings retriever. + * Shadow column data source mappings retriever. */ @HighFrequencyInvocation -public abstract class AbstractShadowDMLStatementDataSourceMappingsRetriever implements ShadowDataSourceMappingsRetriever { +@RequiredArgsConstructor +public abstract class ShadowColumnDataSourceMappingsRetriever implements ShadowTableDataSourceMappingsRetriever { private final ShadowOperationType operationType; @Getter private final Map tableAliasAndNameMappings; - private final ShadowTableHintDataSourceMappingsRetriever tableHintDataSourceMappingsRetriever; - - protected AbstractShadowDMLStatementDataSourceMappingsRetriever(final SQLStatementContext sqlStatementContext, final HintValueContext hintValueContext, final ShadowOperationType operationType) { - this.operationType = operationType; - tableAliasAndNameMappings = getTableAliasAndNameMappings(((TableAvailable) sqlStatementContext).getTablesContext().getSimpleTables()); - tableHintDataSourceMappingsRetriever = new ShadowTableHintDataSourceMappingsRetriever(operationType, hintValueContext.isShadow(), tableAliasAndNameMappings); - } - - 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; - } - @Override - public Map retrieve(final ShadowRule rule) { - Map result = tableHintDataSourceMappingsRetriever.retrieve(rule); - return result.isEmpty() ? findByShadowColumn(rule, rule.filterShadowTables(tableAliasAndNameMappings.values())) : result; - } - - private Map findByShadowColumn(final ShadowRule rule, final Collection shadowTables) { + public Map retrieve(final ShadowRule rule, final Collection shadowTables) { for (String each : shadowTables) { Collection shadowColumnNames = rule.getShadowColumnNames(operationType, each); if (!shadowColumnNames.isEmpty() && isMatchAnyColumnShadowAlgorithms(rule, each, shadowColumnNames)) { diff --git a/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/route/retriever/dml/ShadowDeleteStatementDataSourceMappingsRetriever.java b/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/route/retriever/dml/table/column/ShadowDeleteStatementDataSourceMappingsRetriever.java similarity index 88% rename from features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/route/retriever/dml/ShadowDeleteStatementDataSourceMappingsRetriever.java rename to features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/route/retriever/dml/table/column/ShadowDeleteStatementDataSourceMappingsRetriever.java index 08a31ba9a2e5c..432d75d2793fc 100644 --- a/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/route/retriever/dml/ShadowDeleteStatementDataSourceMappingsRetriever.java +++ b/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/route/retriever/dml/table/column/ShadowDeleteStatementDataSourceMappingsRetriever.java @@ -15,13 +15,13 @@ * limitations under the License. */ -package org.apache.shardingsphere.shadow.route.retriever.dml; +package org.apache.shardingsphere.shadow.route.retriever.dml.table.column; +import org.apache.shardingsphere.infra.annotation.HighFrequencyInvocation; import org.apache.shardingsphere.infra.binder.context.statement.dml.DeleteStatementContext; -import org.apache.shardingsphere.infra.hint.HintValueContext; -import org.apache.shardingsphere.shadow.spi.ShadowOperationType; import org.apache.shardingsphere.shadow.condition.ShadowColumnCondition; import org.apache.shardingsphere.shadow.route.util.ShadowExtractor; +import org.apache.shardingsphere.shadow.spi.ShadowOperationType; import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.expr.ExpressionSegment; import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.predicate.AndPredicate; import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.predicate.WhereSegment; @@ -30,18 +30,20 @@ import java.util.Collection; import java.util.LinkedList; import java.util.List; +import java.util.Map; /** * Shadow delete statement data source mappings retriever. */ -public final class ShadowDeleteStatementDataSourceMappingsRetriever extends AbstractShadowDMLStatementDataSourceMappingsRetriever { +@HighFrequencyInvocation +public final class ShadowDeleteStatementDataSourceMappingsRetriever extends ShadowColumnDataSourceMappingsRetriever { private final DeleteStatementContext sqlStatementContext; private final List parameters; - public ShadowDeleteStatementDataSourceMappingsRetriever(final DeleteStatementContext sqlStatementContext, final List parameters, final HintValueContext hintValueContext) { - super(sqlStatementContext, hintValueContext, ShadowOperationType.DELETE); + public ShadowDeleteStatementDataSourceMappingsRetriever(final DeleteStatementContext sqlStatementContext, final List parameters, final Map tableAliasAndNameMappings) { + super(ShadowOperationType.DELETE, tableAliasAndNameMappings); this.sqlStatementContext = sqlStatementContext; this.parameters = parameters; } diff --git a/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/route/retriever/dml/ShadowInsertStatementDataSourceMappingsRetriever.java b/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/route/retriever/dml/table/column/ShadowInsertStatementDataSourceMappingsRetriever.java similarity index 87% rename from features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/route/retriever/dml/ShadowInsertStatementDataSourceMappingsRetriever.java rename to features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/route/retriever/dml/table/column/ShadowInsertStatementDataSourceMappingsRetriever.java index 2650a3f6fd7a0..3a920f9ef8d28 100644 --- a/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/route/retriever/dml/ShadowInsertStatementDataSourceMappingsRetriever.java +++ b/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/route/retriever/dml/table/column/ShadowInsertStatementDataSourceMappingsRetriever.java @@ -15,29 +15,31 @@ * limitations under the License. */ -package org.apache.shardingsphere.shadow.route.retriever.dml; +package org.apache.shardingsphere.shadow.route.retriever.dml.table.column; +import org.apache.shardingsphere.infra.annotation.HighFrequencyInvocation; import org.apache.shardingsphere.infra.binder.context.segment.insert.values.InsertValueContext; import org.apache.shardingsphere.infra.binder.context.statement.dml.InsertStatementContext; import org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions; -import org.apache.shardingsphere.infra.hint.HintValueContext; -import org.apache.shardingsphere.shadow.spi.ShadowOperationType; import org.apache.shardingsphere.shadow.condition.ShadowColumnCondition; import org.apache.shardingsphere.shadow.exception.syntax.UnsupportedShadowInsertValueException; +import org.apache.shardingsphere.shadow.spi.ShadowOperationType; import java.util.Collection; import java.util.LinkedList; import java.util.List; +import java.util.Map; /** * Shadow insert statement data source mappings retriever. */ -public final class ShadowInsertStatementDataSourceMappingsRetriever extends AbstractShadowDMLStatementDataSourceMappingsRetriever { +@HighFrequencyInvocation +public final class ShadowInsertStatementDataSourceMappingsRetriever extends ShadowColumnDataSourceMappingsRetriever { private final InsertStatementContext sqlStatementContext; - public ShadowInsertStatementDataSourceMappingsRetriever(final InsertStatementContext sqlStatementContext, final HintValueContext hintValueContext) { - super(sqlStatementContext, hintValueContext, ShadowOperationType.INSERT); + public ShadowInsertStatementDataSourceMappingsRetriever(final InsertStatementContext sqlStatementContext, final Map tableAliasAndNameMappings) { + super(ShadowOperationType.INSERT, tableAliasAndNameMappings); this.sqlStatementContext = sqlStatementContext; } diff --git a/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/route/retriever/dml/ShadowSelectStatementDataSourceMappingsRetriever.java b/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/route/retriever/dml/table/column/ShadowSelectStatementDataSourceMappingsRetriever.java similarity index 90% rename from features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/route/retriever/dml/ShadowSelectStatementDataSourceMappingsRetriever.java rename to features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/route/retriever/dml/table/column/ShadowSelectStatementDataSourceMappingsRetriever.java index 2d9240b20dbbc..c1901bc6e9de9 100644 --- a/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/route/retriever/dml/ShadowSelectStatementDataSourceMappingsRetriever.java +++ b/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/route/retriever/dml/table/column/ShadowSelectStatementDataSourceMappingsRetriever.java @@ -15,13 +15,13 @@ * limitations under the License. */ -package org.apache.shardingsphere.shadow.route.retriever.dml; +package org.apache.shardingsphere.shadow.route.retriever.dml.table.column; +import org.apache.shardingsphere.infra.annotation.HighFrequencyInvocation; import org.apache.shardingsphere.infra.binder.context.statement.dml.SelectStatementContext; -import org.apache.shardingsphere.infra.hint.HintValueContext; +import org.apache.shardingsphere.shadow.condition.ShadowColumnCondition; import org.apache.shardingsphere.shadow.route.util.ShadowExtractor; import org.apache.shardingsphere.shadow.spi.ShadowOperationType; -import org.apache.shardingsphere.shadow.condition.ShadowColumnCondition; import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.column.ColumnSegment; import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.expr.ExpressionSegment; import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.predicate.AndPredicate; @@ -33,19 +33,21 @@ import java.util.Collection; import java.util.LinkedList; import java.util.List; +import java.util.Map; import java.util.Optional; /** * Shadow select statement data source mappings retriever. */ -public final class ShadowSelectStatementDataSourceMappingsRetriever extends AbstractShadowDMLStatementDataSourceMappingsRetriever { +@HighFrequencyInvocation +public final class ShadowSelectStatementDataSourceMappingsRetriever extends ShadowColumnDataSourceMappingsRetriever { private final SelectStatementContext sqlStatementContext; private final List parameters; - public ShadowSelectStatementDataSourceMappingsRetriever(final SelectStatementContext sqlStatementContext, final List parameters, final HintValueContext hintValueContext) { - super(sqlStatementContext, hintValueContext, ShadowOperationType.SELECT); + public ShadowSelectStatementDataSourceMappingsRetriever(final SelectStatementContext sqlStatementContext, final List parameters, final Map tableAliasAndNameMappings) { + super(ShadowOperationType.SELECT, tableAliasAndNameMappings); this.sqlStatementContext = sqlStatementContext; this.parameters = parameters; } diff --git a/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/route/retriever/dml/ShadowUpdateStatementDataSourceMappingsRetriever.java b/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/route/retriever/dml/table/column/ShadowUpdateStatementDataSourceMappingsRetriever.java similarity index 89% rename from features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/route/retriever/dml/ShadowUpdateStatementDataSourceMappingsRetriever.java rename to features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/route/retriever/dml/table/column/ShadowUpdateStatementDataSourceMappingsRetriever.java index 1f734fc6c0503..a9c8d31fdba20 100644 --- a/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/route/retriever/dml/ShadowUpdateStatementDataSourceMappingsRetriever.java +++ b/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/route/retriever/dml/table/column/ShadowUpdateStatementDataSourceMappingsRetriever.java @@ -15,13 +15,13 @@ * limitations under the License. */ -package org.apache.shardingsphere.shadow.route.retriever.dml; +package org.apache.shardingsphere.shadow.route.retriever.dml.table.column; +import org.apache.shardingsphere.infra.annotation.HighFrequencyInvocation; import org.apache.shardingsphere.infra.binder.context.statement.dml.UpdateStatementContext; -import org.apache.shardingsphere.infra.hint.HintValueContext; +import org.apache.shardingsphere.shadow.condition.ShadowColumnCondition; import org.apache.shardingsphere.shadow.route.util.ShadowExtractor; import org.apache.shardingsphere.shadow.spi.ShadowOperationType; -import org.apache.shardingsphere.shadow.condition.ShadowColumnCondition; import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.column.ColumnSegment; import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.expr.ExpressionSegment; import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.predicate.AndPredicate; @@ -32,18 +32,20 @@ import java.util.Collection; import java.util.LinkedList; import java.util.List; +import java.util.Map; /** * Shadow update statement data source mappings retriever. */ -public final class ShadowUpdateStatementDataSourceMappingsRetriever extends AbstractShadowDMLStatementDataSourceMappingsRetriever { +@HighFrequencyInvocation +public final class ShadowUpdateStatementDataSourceMappingsRetriever extends ShadowColumnDataSourceMappingsRetriever { private final UpdateStatementContext sqlStatementContext; private final List parameters; - public ShadowUpdateStatementDataSourceMappingsRetriever(final UpdateStatementContext sqlStatementContext, final List parameters, final HintValueContext hintValueContext) { - super(sqlStatementContext, hintValueContext, ShadowOperationType.UPDATE); + public ShadowUpdateStatementDataSourceMappingsRetriever(final UpdateStatementContext sqlStatementContext, final List parameters, final Map tableAliasAndNameMappings) { + super(ShadowOperationType.UPDATE, tableAliasAndNameMappings); this.sqlStatementContext = sqlStatementContext; this.parameters = parameters; } diff --git a/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/route/retriever/hint/ShadowTableHintDataSourceMappingsRetriever.java b/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/route/retriever/dml/table/hint/ShadowTableHintDataSourceMappingsRetriever.java similarity index 88% rename from features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/route/retriever/hint/ShadowTableHintDataSourceMappingsRetriever.java rename to features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/route/retriever/dml/table/hint/ShadowTableHintDataSourceMappingsRetriever.java index db9193172418f..f967d14a7d7ed 100644 --- a/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/route/retriever/hint/ShadowTableHintDataSourceMappingsRetriever.java +++ b/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/route/retriever/dml/table/hint/ShadowTableHintDataSourceMappingsRetriever.java @@ -15,13 +15,13 @@ * limitations under the License. */ -package org.apache.shardingsphere.shadow.route.retriever.hint; +package org.apache.shardingsphere.shadow.route.retriever.dml.table.hint; import lombok.RequiredArgsConstructor; import org.apache.shardingsphere.infra.annotation.HighFrequencyInvocation; import org.apache.shardingsphere.shadow.condition.ShadowCondition; import org.apache.shardingsphere.shadow.route.determiner.HintShadowAlgorithmDeterminer; -import org.apache.shardingsphere.shadow.route.retriever.ShadowDataSourceMappingsRetriever; +import org.apache.shardingsphere.shadow.route.retriever.dml.table.ShadowTableDataSourceMappingsRetriever; import org.apache.shardingsphere.shadow.rule.ShadowRule; import org.apache.shardingsphere.shadow.spi.ShadowAlgorithm; import org.apache.shardingsphere.shadow.spi.ShadowOperationType; @@ -37,17 +37,14 @@ */ @HighFrequencyInvocation @RequiredArgsConstructor -public final class ShadowTableHintDataSourceMappingsRetriever implements ShadowDataSourceMappingsRetriever { +public final class ShadowTableHintDataSourceMappingsRetriever implements ShadowTableDataSourceMappingsRetriever { private final ShadowOperationType operationType; private final boolean isShadow; - private final Map tableAliasAndNameMappings; - @Override - public Map retrieve(final ShadowRule rule) { - Collection shadowTables = rule.filterShadowTables(tableAliasAndNameMappings.values()); + public Map retrieve(final ShadowRule rule, final Collection shadowTables) { return shadowTables.isEmpty() && isMatchDefaultAlgorithm(rule) ? rule.getAllShadowDataSourceMappings() : findShadowDataSourceMappingsBySQLHints(rule, shadowTables); } diff --git a/features/shadow/core/src/test/java/org/apache/shardingsphere/shadow/route/retriever/ShadowDataSourceMappingsRetrieverFactoryTest.java b/features/shadow/core/src/test/java/org/apache/shardingsphere/shadow/route/retriever/ShadowDataSourceMappingsRetrieverFactoryTest.java index 154a9ee9d185f..afd51c36a8eb1 100644 --- a/features/shadow/core/src/test/java/org/apache/shardingsphere/shadow/route/retriever/ShadowDataSourceMappingsRetrieverFactoryTest.java +++ b/features/shadow/core/src/test/java/org/apache/shardingsphere/shadow/route/retriever/ShadowDataSourceMappingsRetrieverFactoryTest.java @@ -27,10 +27,7 @@ import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData; import org.apache.shardingsphere.infra.session.connection.ConnectionContext; import org.apache.shardingsphere.infra.session.query.QueryContext; -import org.apache.shardingsphere.shadow.route.retriever.dml.ShadowDeleteStatementDataSourceMappingsRetriever; -import org.apache.shardingsphere.shadow.route.retriever.dml.ShadowInsertStatementDataSourceMappingsRetriever; -import org.apache.shardingsphere.shadow.route.retriever.dml.ShadowSelectStatementDataSourceMappingsRetriever; -import org.apache.shardingsphere.shadow.route.retriever.dml.ShadowUpdateStatementDataSourceMappingsRetriever; +import org.apache.shardingsphere.shadow.route.retriever.dml.ShadowDMLStatementDataSourceMappingsRetriever; import org.apache.shardingsphere.sql.parser.statement.core.statement.dml.DeleteStatement; import org.apache.shardingsphere.sql.parser.statement.core.statement.dml.InsertStatement; import org.apache.shardingsphere.sql.parser.statement.core.statement.dml.SelectStatement; @@ -52,16 +49,16 @@ class ShadowDataSourceMappingsRetrieverFactoryTest { void assertNewInstance() { ShadowDataSourceMappingsRetriever retriever = ShadowDataSourceMappingsRetrieverFactory.newInstance( new QueryContext(createInsertSqlStatementContext(), "", Collections.emptyList(), new HintValueContext(), mockConnectionContext(), mock(ShardingSphereMetaData.class))); - assertThat(retriever, instanceOf(ShadowInsertStatementDataSourceMappingsRetriever.class)); + assertThat(retriever, instanceOf(ShadowDMLStatementDataSourceMappingsRetriever.class)); ShadowDataSourceMappingsRetriever shadowUpdateRouteEngine = ShadowDataSourceMappingsRetrieverFactory.newInstance( new QueryContext(createUpdateSqlStatementContext(), "", Collections.emptyList(), new HintValueContext(), mockConnectionContext(), mock(ShardingSphereMetaData.class))); - assertThat(shadowUpdateRouteEngine, instanceOf(ShadowUpdateStatementDataSourceMappingsRetriever.class)); + assertThat(shadowUpdateRouteEngine, instanceOf(ShadowDMLStatementDataSourceMappingsRetriever.class)); ShadowDataSourceMappingsRetriever shadowDeleteRouteEngine = ShadowDataSourceMappingsRetrieverFactory.newInstance( new QueryContext(createDeleteSqlStatementContext(), "", Collections.emptyList(), new HintValueContext(), mockConnectionContext(), mock(ShardingSphereMetaData.class))); - assertThat(shadowDeleteRouteEngine, instanceOf(ShadowDeleteStatementDataSourceMappingsRetriever.class)); + assertThat(shadowDeleteRouteEngine, instanceOf(ShadowDMLStatementDataSourceMappingsRetriever.class)); ShadowDataSourceMappingsRetriever shadowSelectRouteEngine = ShadowDataSourceMappingsRetrieverFactory.newInstance( new QueryContext(createSelectSqlStatementContext(), "", Collections.emptyList(), new HintValueContext(), mockConnectionContext(), mock(ShardingSphereMetaData.class))); - assertThat(shadowSelectRouteEngine, instanceOf(ShadowSelectStatementDataSourceMappingsRetriever.class)); + assertThat(shadowSelectRouteEngine, instanceOf(ShadowDMLStatementDataSourceMappingsRetriever.class)); } private ConnectionContext mockConnectionContext() {