This is an automated email from the ASF dual-hosted git repository.

zhangliang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git


The following commit(s) were added to refs/heads/master by this push:
     new b9f7653f742 Add ShadowTableHintDataSourceMappingsRetriever (#33552)
b9f7653f742 is described below

commit b9f7653f742bbe29c32d92ace7cf2752b4dfb580
Author: Liang Zhang <[email protected]>
AuthorDate: Tue Nov 5 11:44:30 2024 +0800

    Add ShadowTableHintDataSourceMappingsRetriever (#33552)
---
 ...dowDMLStatementDataSourceMappingsRetriever.java | 48 ++-----------
 ...ShadowTableHintDataSourceMappingsRetriever.java | 80 ++++++++++++++++++++++
 2 files changed, 86 insertions(+), 42 deletions(-)

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 88e1430565b..f109c823db7 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.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.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<String, String> 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<String, String> getTableAliasAndNameMappings(final 
Collection<SimpleTableSegment> tableSegments) {
@@ -71,41 +68,8 @@ public abstract class 
AbstractShadowDMLStatementDataSourceMappingsRetriever impl
     
     @Override
     public Map<String, String> retrieve(final ShadowRule rule) {
-        Collection<String> shadowTables = 
rule.filterShadowTables(tableAliasAndNameMappings.values());
-        if (shadowTables.isEmpty() && isMatchDefaultAlgorithm(rule)) {
-            return rule.getAllShadowDataSourceMappings();
-        }
-        Map<String, String> result = findBySQLHints(rule, shadowTables);
-        return result.isEmpty() ? findByShadowColumn(rule, shadowTables) : 
result;
-    }
-    
-    @SuppressWarnings("unchecked")
-    private boolean isMatchDefaultAlgorithm(final ShadowRule rule) {
-        Optional<ShadowAlgorithm> defaultAlgorithm = 
rule.getDefaultShadowAlgorithm();
-        if (defaultAlgorithm.isPresent() && defaultAlgorithm.get() instanceof 
HintShadowAlgorithm<?>) {
-            return 
HintShadowAlgorithmDeterminer.isShadow((HintShadowAlgorithm<Comparable<?>>) 
defaultAlgorithm.get(), new ShadowCondition(), rule, isShadow);
-        }
-        return false;
-    }
-    
-    private Map<String, String> findBySQLHints(final ShadowRule rule, final 
Collection<String> shadowTables) {
-        Map<String, String> 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<Comparable<?>> each : 
rule.getHintShadowAlgorithms(tableName)) {
-            if (HintShadowAlgorithmDeterminer.isShadow(each, shadowCondition, 
rule, isShadow)) {
-                return true;
-            }
-        }
-        return false;
+        Map<String, String> result = 
tableHintDataSourceMappingsRetriever.retrieve(rule);
+        return result.isEmpty() ? findByShadowColumn(rule, 
rule.filterShadowTables(tableAliasAndNameMappings.values())) : result;
     }
     
     private Map<String, String> findByShadowColumn(final ShadowRule rule, 
final Collection<String> 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 00000000000..db919317241
--- /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<String, String> tableAliasAndNameMappings;
+    
+    @Override
+    public Map<String, String> retrieve(final ShadowRule rule) {
+        Collection<String> shadowTables = 
rule.filterShadowTables(tableAliasAndNameMappings.values());
+        return shadowTables.isEmpty() && isMatchDefaultAlgorithm(rule) ? 
rule.getAllShadowDataSourceMappings() : 
findShadowDataSourceMappingsBySQLHints(rule, shadowTables);
+    }
+    
+    @SuppressWarnings("unchecked")
+    private boolean isMatchDefaultAlgorithm(final ShadowRule rule) {
+        Optional<ShadowAlgorithm> defaultAlgorithm = 
rule.getDefaultShadowAlgorithm();
+        if (defaultAlgorithm.isPresent() && defaultAlgorithm.get() instanceof 
HintShadowAlgorithm<?>) {
+            return 
HintShadowAlgorithmDeterminer.isShadow((HintShadowAlgorithm<Comparable<?>>) 
defaultAlgorithm.get(), new ShadowCondition(), rule, isShadow);
+        }
+        return false;
+    }
+    
+    private Map<String, String> findShadowDataSourceMappingsBySQLHints(final 
ShadowRule rule, final Collection<String> 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<Comparable<?>> each : 
rule.getHintShadowAlgorithms(tableName)) {
+            if (HintShadowAlgorithmDeterminer.isShadow(each, shadowCondition, 
rule, isShadow)) {
+                return true;
+            }
+        }
+        return false;
+    }
+}

Reply via email to