From 090fd8634348806aab85bb5e976f932cd02d472a Mon Sep 17 00:00:00 2001 From: zhangliang Date: Tue, 5 Nov 2024 11:42:02 +0800 Subject: [PATCH] Add ShadowTableHintDataSourceMappingsRetriever --- ...LStatementDataSourceMappingsRetriever.java | 48 ++--------- ...wTableHintDataSourceMappingsRetriever.java | 80 +++++++++++++++++++ 2 files changed, 86 insertions(+), 42 deletions(-) create mode 100644 features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/route/retriever/hint/ShadowTableHintDataSourceMappingsRetriever.java 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/AbstractShadowDMLStatementDataSourceMappingsRetriever.java index 88e1430565bf8..f109c823db7ec 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/AbstractShadowDMLStatementDataSourceMappingsRetriever.java @@ -25,20 +25,17 @@ 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.determiner.HintShadowAlgorithmDeterminer; import org.apache.shardingsphere.shadow.route.retriever.ShadowDataSourceMappingsRetriever; +import org.apache.shardingsphere.shadow.route.retriever.hint.ShadowTableHintDataSourceMappingsRetriever; import org.apache.shardingsphere.shadow.rule.ShadowRule; -import org.apache.shardingsphere.shadow.spi.ShadowAlgorithm; import org.apache.shardingsphere.shadow.spi.ShadowOperationType; import org.apache.shardingsphere.shadow.spi.column.ColumnShadowAlgorithm; -import org.apache.shardingsphere.shadow.spi.hint.HintShadowAlgorithm; 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; -import java.util.Optional; /** * Abstract shadow DML statement data source mappings retriever. @@ -48,15 +45,15 @@ public abstract class AbstractShadowDMLStatementDataSourceMappingsRetriever impl private final ShadowOperationType operationType; - private final boolean isShadow; - @Getter private final Map tableAliasAndNameMappings; + private final ShadowTableHintDataSourceMappingsRetriever tableHintDataSourceMappingsRetriever; + protected AbstractShadowDMLStatementDataSourceMappingsRetriever(final SQLStatementContext sqlStatementContext, final HintValueContext hintValueContext, final ShadowOperationType operationType) { this.operationType = operationType; - isShadow = hintValueContext.isShadow(); tableAliasAndNameMappings = getTableAliasAndNameMappings(((TableAvailable) sqlStatementContext).getTablesContext().getSimpleTables()); + tableHintDataSourceMappingsRetriever = new ShadowTableHintDataSourceMappingsRetriever(operationType, hintValueContext.isShadow(), tableAliasAndNameMappings); } private Map getTableAliasAndNameMappings(final Collection tableSegments) { @@ -71,41 +68,8 @@ private Map getTableAliasAndNameMappings(final Collection retrieve(final ShadowRule rule) { - Collection shadowTables = rule.filterShadowTables(tableAliasAndNameMappings.values()); - if (shadowTables.isEmpty() && isMatchDefaultAlgorithm(rule)) { - return rule.getAllShadowDataSourceMappings(); - } - Map result = findBySQLHints(rule, shadowTables); - return result.isEmpty() ? findByShadowColumn(rule, shadowTables) : result; - } - - @SuppressWarnings("unchecked") - private boolean isMatchDefaultAlgorithm(final ShadowRule rule) { - Optional defaultAlgorithm = rule.getDefaultShadowAlgorithm(); - if (defaultAlgorithm.isPresent() && defaultAlgorithm.get() instanceof HintShadowAlgorithm) { - return HintShadowAlgorithmDeterminer.isShadow((HintShadowAlgorithm>) defaultAlgorithm.get(), new ShadowCondition(), rule, isShadow); - } - return false; - } - - private Map findBySQLHints(final ShadowRule rule, final Collection shadowTables) { - Map result = new LinkedHashMap<>(); - for (String each : shadowTables) { - if (isContainsShadowInSQLHints(rule, each, new ShadowCondition(each, operationType))) { - result.putAll(rule.getShadowDataSourceMappings(each)); - return result; - } - } - return result; - } - - private boolean isContainsShadowInSQLHints(final ShadowRule rule, final String tableName, final ShadowCondition shadowCondition) { - for (HintShadowAlgorithm> each : rule.getHintShadowAlgorithms(tableName)) { - if (HintShadowAlgorithmDeterminer.isShadow(each, shadowCondition, rule, isShadow)) { - return true; - } - } - return false; + Map result = tableHintDataSourceMappingsRetriever.retrieve(rule); + return result.isEmpty() ? findByShadowColumn(rule, rule.filterShadowTables(tableAliasAndNameMappings.values())) : result; } private Map findByShadowColumn(final ShadowRule rule, final Collection shadowTables) { 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/hint/ShadowTableHintDataSourceMappingsRetriever.java new file mode 100644 index 0000000000000..db9193172418f --- /dev/null +++ b/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/route/retriever/hint/ShadowTableHintDataSourceMappingsRetriever.java @@ -0,0 +1,80 @@ +/* + * 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.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.rule.ShadowRule; +import org.apache.shardingsphere.shadow.spi.ShadowAlgorithm; +import org.apache.shardingsphere.shadow.spi.ShadowOperationType; +import org.apache.shardingsphere.shadow.spi.hint.HintShadowAlgorithm; + +import java.util.Collection; +import java.util.Collections; +import java.util.Map; +import java.util.Optional; + +/** + * Shadow table hint data source mappings retriever. + */ +@HighFrequencyInvocation +@RequiredArgsConstructor +public final class ShadowTableHintDataSourceMappingsRetriever implements ShadowDataSourceMappingsRetriever { + + 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()); + return shadowTables.isEmpty() && isMatchDefaultAlgorithm(rule) ? rule.getAllShadowDataSourceMappings() : findShadowDataSourceMappingsBySQLHints(rule, shadowTables); + } + + @SuppressWarnings("unchecked") + private boolean isMatchDefaultAlgorithm(final ShadowRule rule) { + Optional defaultAlgorithm = rule.getDefaultShadowAlgorithm(); + if (defaultAlgorithm.isPresent() && defaultAlgorithm.get() instanceof HintShadowAlgorithm) { + return HintShadowAlgorithmDeterminer.isShadow((HintShadowAlgorithm>) defaultAlgorithm.get(), new ShadowCondition(), rule, isShadow); + } + return false; + } + + private Map findShadowDataSourceMappingsBySQLHints(final ShadowRule rule, final Collection shadowTables) { + for (String each : shadowTables) { + if (containsShadowInSQLHints(rule, each, new ShadowCondition(each, operationType))) { + return rule.getShadowDataSourceMappings(each); + } + } + return Collections.emptyMap(); + } + + private boolean containsShadowInSQLHints(final ShadowRule rule, final String tableName, final ShadowCondition shadowCondition) { + for (HintShadowAlgorithm> each : rule.getHintShadowAlgorithms(tableName)) { + if (HintShadowAlgorithmDeterminer.isShadow(each, shadowCondition, rule, isShadow)) { + return true; + } + } + return false; + } +}