You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by pa...@apache.org on 2022/11/20 13:44:41 UTC

[shardingsphere] branch master updated: Refactor ShardingTableRuleStatementChecker (#22297)

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

panjuan 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 b352e2a1e6c Refactor ShardingTableRuleStatementChecker (#22297)
b352e2a1e6c is described below

commit b352e2a1e6c0c63bbbdd014fbd9714a0f34cd068
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Sun Nov 20 21:44:34 2022 +0800

    Refactor ShardingTableRuleStatementChecker (#22297)
---
 .../checker/ShardingTableRuleStatementChecker.java | 36 ++++++++--------------
 .../parser/segment/AutoTableRuleSegment.java       |  9 +++---
 .../distsql/parser/segment/TableRuleSegment.java   |  3 +-
 3 files changed, 18 insertions(+), 30 deletions(-)

diff --git a/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/checker/ShardingTableRuleStatementChecker.java b/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/checker/ShardingTableRuleStatementChecker.java
index fb38b0ed693..b55b0fd0e6f 100644
--- a/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/checker/ShardingTableRuleStatementChecker.java
+++ b/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/checker/ShardingTableRuleStatementChecker.java
@@ -91,8 +91,7 @@ public final class ShardingTableRuleStatementChecker {
      * @param rules rules
      * @param currentRuleConfig current rule configuration
      */
-    public static void checkCreation(final ShardingSphereDatabase database,
-                                     final Collection<AbstractTableRuleSegment> rules, final ShardingRuleConfiguration currentRuleConfig) {
+    public static void checkCreation(final ShardingSphereDatabase database, final Collection<AbstractTableRuleSegment> rules, final ShardingRuleConfiguration currentRuleConfig) {
         check(database, rules, currentRuleConfig, true);
     }
     
@@ -119,20 +118,19 @@ public final class ShardingTableRuleStatementChecker {
         Collection<String> dataSourceNames = getRequiredResource(toBeCheckedRuleConfig);
         dataSourceNames.addAll(getRequiredResource(currentRuleConfig));
         ShardingSpherePreconditions.checkState(check(toBeCheckedRuleConfig, dataSourceNames),
-                () -> new InvalidRuleConfigurationException("sharding table", bindingTableGroups, Collections.singleton("invalid binding table configuration")));
+                () -> new InvalidRuleConfigurationException("sharding table", bindingTableGroups, Collections.singleton("invalid binding table configuration.")));
     }
     
-    private static void check(final ShardingSphereDatabase database,
-                              final Collection<AbstractTableRuleSegment> rules, final ShardingRuleConfiguration currentRuleConfig, final boolean isCreate) {
+    private static void check(final ShardingSphereDatabase database, final Collection<AbstractTableRuleSegment> rules, final ShardingRuleConfiguration currentRuleConfig, final boolean isCreated) {
         String databaseName = database.getName();
-        checkTables(databaseName, rules, currentRuleConfig, isCreate);
+        checkTables(databaseName, rules, currentRuleConfig, isCreated);
         checkResources(databaseName, rules, database);
         checkKeyGenerators(rules, currentRuleConfig);
         checkAuditors(rules, currentRuleConfig);
         Map<String, List<AbstractTableRuleSegment>> groupedTableRule = groupingByClassType(rules);
         checkAutoTableRule(groupedTableRule.getOrDefault(AutoTableRuleSegment.class.getSimpleName(), Collections.emptyList()));
         checkTableRule(databaseName, groupedTableRule.getOrDefault(TableRuleSegment.class.getSimpleName(), Collections.emptyList()));
-        if (!isCreate) {
+        if (!isCreated) {
             checkBindingTableRules(rules, currentRuleConfig);
         }
     }
@@ -195,18 +193,12 @@ public final class ShardingTableRuleStatementChecker {
         }
         Collection<String> result = new LinkedHashSet<>();
         tableRuleConfigs.forEach(each -> result.addAll(getDataSourceNames(each)));
-        autoTableRuleConfigs.forEach(each -> result.addAll(getDataSourceNames(each)));
+        autoTableRuleConfigs.forEach(each -> result.addAll(new InlineExpressionParser(each.getActualDataSources()).splitAndEvaluate()));
         return result;
     }
     
-    private static Collection<String> getDataSourceNames(final ShardingAutoTableRuleConfiguration shardingAutoTableRuleConfig) {
-        Collection<String> actualDataSources = new InlineExpressionParser(shardingAutoTableRuleConfig.getActualDataSources()).splitAndEvaluate();
-        return new HashSet<>(actualDataSources);
-    }
-    
     private static Collection<String> getDataSourceNames(final ShardingTableRuleConfiguration shardingTableRuleConfig) {
-        Collection<String> actualDataNodes = new InlineExpressionParser(shardingTableRuleConfig.getActualDataNodes()).splitAndEvaluate();
-        return actualDataNodes.stream().map(each -> new DataNode(each).getDataSourceName()).collect(Collectors.toList());
+        return new InlineExpressionParser(shardingTableRuleConfig.getActualDataNodes()).splitAndEvaluate().stream().map(each -> new DataNode(each).getDataSourceName()).collect(Collectors.toList());
     }
     
     private static boolean isValidDataNode(final String dataNodeStr) {
@@ -265,18 +257,17 @@ public final class ShardingTableRuleStatementChecker {
     }
     
     private static void checkAuditors(final Collection<AbstractTableRuleSegment> rules, final ShardingRuleConfiguration currentRuleConfig) {
-        Set<String> notExistAuditors = new LinkedHashSet<>();
-        Set<String> requiredAuditors = new LinkedHashSet<>();
         Collection<AuditStrategySegment> auditStrategySegments = rules.stream().map(AbstractTableRuleSegment::getAuditStrategySegment).filter(Objects::nonNull).collect(Collectors.toList());
         Collection<String> auditorNames = new LinkedList<>();
         for (AuditStrategySegment each : auditStrategySegments) {
             auditorNames.addAll(each.getAuditorNames());
         }
-        auditorNames.stream().filter(each -> null == currentRuleConfig || !currentRuleConfig.getAuditors().containsKey(each)).collect(Collectors.toList()).forEach(notExistAuditors::add);
+        Collection<String> notExistAuditors = auditorNames.stream().filter(each -> null == currentRuleConfig || !currentRuleConfig.getAuditors().containsKey(each)).collect(Collectors.toSet());
         ShardingSpherePreconditions.checkState(notExistAuditors.isEmpty(), () -> new MissingRequiredAlgorithmException("auditor", notExistAuditors));
+        Set<String> requiredAuditors = new LinkedHashSet<>();
         for (AuditStrategySegment each : auditStrategySegments) {
-            requiredAuditors.addAll(each.getShardingAuditorSegments().stream().map(ShardingAuditorSegment::getAlgorithmSegment).collect(Collectors.toList())
-                    .stream().map(AlgorithmSegment::getName).collect(Collectors.toList()));
+            requiredAuditors.addAll(each.getShardingAuditorSegments().stream()
+                    .map(ShardingAuditorSegment::getAlgorithmSegment).collect(Collectors.toList()).stream().map(AlgorithmSegment::getName).collect(Collectors.toList()));
         }
         Collection<String> invalidAuditors = requiredAuditors.stream().distinct().filter(each -> !ShardingAuditAlgorithmFactory.contains(each)).collect(Collectors.toList());
         ShardingSpherePreconditions.checkState(invalidAuditors.isEmpty(), () -> new InvalidAlgorithmConfigurationException("auditor", invalidAuditors));
@@ -291,8 +282,7 @@ public final class ShardingTableRuleStatementChecker {
     }
     
     private static void checkAutoTableShardingAlgorithms(final Collection<AutoTableRuleSegment> rules) {
-        Collection<AutoTableRuleSegment> incompleteShardingRules = rules.stream().filter(each -> !each.isCompleteShardingAlgorithm()).collect(Collectors.toList());
-        ShardingSpherePreconditions.checkState(incompleteShardingRules.isEmpty(), () -> new InvalidAlgorithmConfigurationException("sharding"));
+        ShardingSpherePreconditions.checkState(rules.stream().allMatch(AutoTableRuleSegment::isShardingAlgorithmCompleted), () -> new InvalidAlgorithmConfigurationException("sharding"));
         Collection<String> invalidShardingAlgorithms = rules.stream().map(each -> each.getShardingAlgorithmSegment().getName()).distinct()
                 .filter(each -> !ShardingAlgorithmFactory.contains(each)).collect(Collectors.toList());
         ShardingSpherePreconditions.checkState(invalidShardingAlgorithms.isEmpty(), () -> new InvalidAlgorithmConfigurationException("sharding", invalidShardingAlgorithms));
@@ -352,7 +342,7 @@ public final class ShardingTableRuleStatementChecker {
         Collection<String> dataSourceNames = getRequiredResource(toBeCheckedRuleConfig);
         dataSourceNames.addAll(getRequiredResource(toBeAlteredRuleConfig));
         ShardingSpherePreconditions.checkState(check(toBeCheckedRuleConfig, dataSourceNames),
-                () -> new InvalidRuleConfigurationException("sharding table", toBeAlteredLogicTableNames, Collections.singleton("invalid binding table configuration")));
+                () -> new InvalidRuleConfigurationException("sharding table", toBeAlteredLogicTableNames, Collections.singleton("invalid binding table configuration.")));
     }
     
     private static ShardingRuleConfiguration createToBeCheckedShardingRuleConfiguration(final ShardingRuleConfiguration currentRuleConfig) {
diff --git a/features/sharding/distsql/statement/src/main/java/org/apache/shardingsphere/sharding/distsql/parser/segment/AutoTableRuleSegment.java b/features/sharding/distsql/statement/src/main/java/org/apache/shardingsphere/sharding/distsql/parser/segment/AutoTableRuleSegment.java
index 88228ee2c6e..b46fa1c91c1 100644
--- a/features/sharding/distsql/statement/src/main/java/org/apache/shardingsphere/sharding/distsql/parser/segment/AutoTableRuleSegment.java
+++ b/features/sharding/distsql/statement/src/main/java/org/apache/shardingsphere/sharding/distsql/parser/segment/AutoTableRuleSegment.java
@@ -27,12 +27,11 @@ import java.util.Collection;
  * Auto table rule segment.
  */
 @Getter
+@Setter
 public final class AutoTableRuleSegment extends AbstractTableRuleSegment {
     
-    @Setter
     private String shardingColumn;
     
-    @Setter
     private AlgorithmSegment shardingAlgorithmSegment;
     
     public AutoTableRuleSegment(final String logicTable, final Collection<String> dataSources) {
@@ -48,11 +47,11 @@ public final class AutoTableRuleSegment extends AbstractTableRuleSegment {
     }
     
     /**
-     * Determine whether there is a complete sharding algorithm.
+     * Determine whether sharding algorithm completed.
      *
-     * @return has datasource or not
+     * @return completed sharding algorithm or not
      */
-    public boolean isCompleteShardingAlgorithm() {
+    public boolean isShardingAlgorithmCompleted() {
         return null != shardingColumn && null != shardingAlgorithmSegment;
     }
 }
diff --git a/features/sharding/distsql/statement/src/main/java/org/apache/shardingsphere/sharding/distsql/parser/segment/TableRuleSegment.java b/features/sharding/distsql/statement/src/main/java/org/apache/shardingsphere/sharding/distsql/parser/segment/TableRuleSegment.java
index 877fa621ce5..6f22a3897e0 100644
--- a/features/sharding/distsql/statement/src/main/java/org/apache/shardingsphere/sharding/distsql/parser/segment/TableRuleSegment.java
+++ b/features/sharding/distsql/statement/src/main/java/org/apache/shardingsphere/sharding/distsql/parser/segment/TableRuleSegment.java
@@ -26,12 +26,11 @@ import java.util.Collection;
  * Table rule segment.
  */
 @Getter
+@Setter
 public final class TableRuleSegment extends AbstractTableRuleSegment {
     
-    @Setter
     private ShardingStrategySegment tableStrategySegment;
     
-    @Setter
     private ShardingStrategySegment databaseStrategySegment;
     
     public TableRuleSegment(final String logicTable, final Collection<String> dataSourcesNote) {