You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by ji...@apache.org on 2023/02/01 16:28:11 UTC

[shardingsphere] branch master updated: Refactor ColumnShadowAlgorithm (#23912)

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

jianglongtao 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 228a60b79a1 Refactor ColumnShadowAlgorithm (#23912)
228a60b79a1 is described below

commit 228a60b79a14599afa72177495323ac1cbe4f8d3
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Thu Feb 2 00:28:04 2023 +0800

    Refactor ColumnShadowAlgorithm (#23912)
    
    * Refactor WeightReadQueryLoadBalanceAlgorithm
    
    * Refactor WeightReadQueryLoadBalanceAlgorithm
    
    * Refactor ColumnShadowAlgorithm
---
 .../shadow/api/shadow/column/ColumnShadowAlgorithm.java  | 15 +++++++++++++++
 .../column/AbstractColumnMatchedShadowAlgorithm.java     |  8 ++++----
 .../shardingsphere/shadow/rule/ShadowTableRule.java      | 16 +++++++---------
 3 files changed, 26 insertions(+), 13 deletions(-)

diff --git a/features/shadow/api/src/main/java/org/apache/shardingsphere/shadow/api/shadow/column/ColumnShadowAlgorithm.java b/features/shadow/api/src/main/java/org/apache/shardingsphere/shadow/api/shadow/column/ColumnShadowAlgorithm.java
index fb07734d697..8e7858c5cff 100644
--- a/features/shadow/api/src/main/java/org/apache/shardingsphere/shadow/api/shadow/column/ColumnShadowAlgorithm.java
+++ b/features/shadow/api/src/main/java/org/apache/shardingsphere/shadow/api/shadow/column/ColumnShadowAlgorithm.java
@@ -17,6 +17,7 @@
 
 package org.apache.shardingsphere.shadow.api.shadow.column;
 
+import org.apache.shardingsphere.shadow.api.shadow.ShadowOperationType;
 import org.apache.shardingsphere.shadow.spi.ShadowAlgorithm;
 
 /**
@@ -31,4 +32,18 @@ public interface ColumnShadowAlgorithm<T extends Comparable<?>> extends ShadowAl
      * @return is need shadow or not
      */
     boolean isShadow(PreciseColumnShadowValue<T> columnShadowValue);
+    
+    /**
+     * Get shadow column.
+     * 
+     * @return shadow column
+     */
+    String getShadowColumn();
+    
+    /**
+     * Get shadow operation type.
+     *
+     * @return shadow operation type
+     */
+    ShadowOperationType getShadowOperationType();
 }
diff --git a/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/algorithm/shadow/column/AbstractColumnMatchedShadowAlgorithm.java b/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/algorithm/shadow/column/AbstractColumnMatchedShadowAlgorithm.java
index 72e21487d3d..a5042d514d5 100644
--- a/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/algorithm/shadow/column/AbstractColumnMatchedShadowAlgorithm.java
+++ b/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/algorithm/shadow/column/AbstractColumnMatchedShadowAlgorithm.java
@@ -31,13 +31,13 @@ import java.util.Properties;
 /**
  * Abstract column matched shadow algorithm.
  */
+@Getter
 public abstract class AbstractColumnMatchedShadowAlgorithm implements ColumnShadowAlgorithm<Comparable<?>> {
     
     private static final String COLUMN_PROPS_KEY = "column";
     
     private static final String OPERATION_PROPS_KEY = "operation";
     
-    @Getter
     private Properties props;
     
     private String shadowColumn;
@@ -53,16 +53,16 @@ public abstract class AbstractColumnMatchedShadowAlgorithm implements ColumnShad
     
     private String getShadowColumn(final Properties props) {
         String result = props.getProperty(COLUMN_PROPS_KEY);
-        ShardingSpherePreconditions.checkNotNull(result, () -> new ShadowAlgorithmInitializationException(getType(), "Column shadow algorithm column cannot be null."));
+        ShardingSpherePreconditions.checkNotNull(result, () -> new ShadowAlgorithmInitializationException(getType(), "Column shadow algorithm column cannot be null"));
         return result;
     }
     
     private ShadowOperationType getShadowOperationType(final Properties props) {
         String operationType = props.getProperty(OPERATION_PROPS_KEY);
-        ShardingSpherePreconditions.checkNotNull(operationType, () -> new ShadowAlgorithmInitializationException(getType(), "Column shadow algorithm operation cannot be null."));
+        ShardingSpherePreconditions.checkNotNull(operationType, () -> new ShadowAlgorithmInitializationException(getType(), "Column shadow algorithm operation cannot be null"));
         Optional<ShadowOperationType> result = ShadowOperationType.contains(operationType);
         ShardingSpherePreconditions.checkState(result.isPresent(),
-                () -> new ShadowAlgorithmInitializationException(getType(), "Column shadow algorithm operation must be one of [select, insert, update, delete]."));
+                () -> new ShadowAlgorithmInitializationException(getType(), "Column shadow algorithm operation must be one of [select, insert, update, delete]"));
         return result.get();
     }
     
diff --git a/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/rule/ShadowTableRule.java b/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/rule/ShadowTableRule.java
index 427aa18f3cd..ce59b1bfcfa 100644
--- a/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/rule/ShadowTableRule.java
+++ b/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/rule/ShadowTableRule.java
@@ -59,21 +59,19 @@ public final class ShadowTableRule {
     private Map<ShadowOperationType, Collection<ShadowAlgorithmNameRule>> initColumnShadowAlgorithmNames(final Collection<String> shadowAlgorithmNames,
                                                                                                          final Map<String, ShadowAlgorithm> shadowAlgorithms) {
         Map<ShadowOperationType, Collection<ShadowAlgorithmNameRule>> result = new EnumMap<>(ShadowOperationType.class);
-        shadowAlgorithmNames.forEach(each -> {
+        for (String each : shadowAlgorithmNames) {
             ShadowAlgorithm shadowAlgorithm = shadowAlgorithms.get(each);
             if (shadowAlgorithm instanceof ColumnShadowAlgorithm) {
-                String operation = shadowAlgorithm.getProps().getProperty("operation");
-                String shadowColumnName = shadowAlgorithm.getProps().getProperty("column");
-                ShadowOperationType.contains(operation).ifPresent(optional -> initShadowAlgorithmNames(optional, each, shadowColumnName, result));
+                initShadowAlgorithmNames(each, (ColumnShadowAlgorithm<?>) shadowAlgorithm, result);
             }
-        });
+        }
         return result;
     }
     
-    private void initShadowAlgorithmNames(final ShadowOperationType operationType, final String algorithmName, final String shadowColumnName,
+    private void initShadowAlgorithmNames(final String name, final ColumnShadowAlgorithm<?> algorithm,
                                           final Map<ShadowOperationType, Collection<ShadowAlgorithmNameRule>> columnShadowAlgorithmNames) {
-        Collection<ShadowAlgorithmNameRule> shadowAlgorithmNameRules = columnShadowAlgorithmNames.get(operationType);
-        ShardingSpherePreconditions.checkState(null == shadowAlgorithmNameRules, () -> new InvalidShadowAlgorithmOperationException(operationType.name(), tableName));
-        columnShadowAlgorithmNames.put(operationType, Collections.singletonList(new ShadowAlgorithmNameRule(shadowColumnName, algorithmName)));
+        ShadowOperationType operationType = algorithm.getShadowOperationType();
+        ShardingSpherePreconditions.checkState(!columnShadowAlgorithmNames.containsKey(operationType), () -> new InvalidShadowAlgorithmOperationException(operationType.name(), tableName));
+        columnShadowAlgorithmNames.put(operationType, Collections.singleton(new ShadowAlgorithmNameRule(algorithm.getShadowColumn(), name)));
     }
 }