You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by GitBox <gi...@apache.org> on 2022/09/05 15:38:50 UTC

[GitHub] [shardingsphere] sunkai-cai commented on a diff in pull request #20777: Enhanced algorithm checking for CREATE SHARDING TABLE RULE

sunkai-cai commented on code in PR #20777:
URL: https://github.com/apache/shardingsphere/pull/20777#discussion_r963015708


##########
shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/checker/ShardingTableRuleStatementChecker.java:
##########
@@ -84,8 +84,8 @@ public final class ShardingTableRuleStatementChecker {
     /**
      * Check create sharing table rule statement.
      *
-     * @param database database

Review Comment:
   done it.



##########
shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/checker/ShardingTableRuleStatementChecker.java:
##########
@@ -278,24 +278,35 @@ private static void checkTableRule(final String databaseName, final ShardingRule
     }
     
     private static void checkStrategy(final String databaseName, final ShardingRuleConfiguration currentRuleConfig, final Collection<TableRuleSegment> rules) throws DistSQLException {
-        Collection<String> currentAlgorithms = null == currentRuleConfig ? Collections.emptySet() : currentRuleConfig.getShardingAlgorithms().keySet();
+        Map<String, AlgorithmConfiguration> currentAlgorithms = null == currentRuleConfig ? new HashMap<>() : currentRuleConfig.getShardingAlgorithms();
         Collection<String> invalidAlgorithms = rules.stream().map(each -> Arrays.asList(each.getDatabaseStrategySegment(), each.getTableStrategySegment()))
                 .flatMap(Collection::stream).filter(Objects::nonNull).filter(each -> isInvalidStrategy(currentAlgorithms, each))
-                .map(ShardingStrategySegment::getShardingAlgorithmName).collect(Collectors.toList());
+                .map(each -> null != each.getShardingAlgorithmName() ? each.getShardingAlgorithmName() : (null != each.getAlgorithmSegment() ? each.getAlgorithmSegment().getName() : ""))
+                .collect(Collectors.toList());
         DistSQLException.predictionThrow(invalidAlgorithms.isEmpty(), () -> new InvalidAlgorithmConfigurationException(databaseName, invalidAlgorithms));
     }
     
-    private static boolean isInvalidStrategy(final Collection<String> currentAlgorithms, final ShardingStrategySegment shardingStrategySegment) {
+    private static boolean isInvalidStrategy(final Map<String, AlgorithmConfiguration> currentAlgorithms, final ShardingStrategySegment shardingStrategySegment) {
         return !ShardingStrategyType.contain(shardingStrategySegment.getType())
                 || !ShardingStrategyType.getValueOf(shardingStrategySegment.getType()).isValid(shardingStrategySegment.getShardingColumn())
-                || !isAlgorithmExists(currentAlgorithms, shardingStrategySegment);
+                || !isStandardAlgorithmExists(currentAlgorithms, shardingStrategySegment);
     }
     
-    private static boolean isAlgorithmExists(final Collection<String> currentAlgorithms, final ShardingStrategySegment shardingStrategySegment) {
+    private static boolean isStandardAlgorithmExists(final Map<String, AlgorithmConfiguration> currentAlgorithms, final ShardingStrategySegment shardingStrategySegment) {
+        ShardingAlgorithm shardingAlgorithm;
         if (null == shardingStrategySegment.getShardingAlgorithmName() && null != shardingStrategySegment.getAlgorithmSegment()) {
-            return true;
+            shardingAlgorithm = ShardingAlgorithmFactory.newInstance(new AlgorithmConfiguration(
+                    shardingStrategySegment.getAlgorithmSegment().getName(),
+                    shardingStrategySegment.getAlgorithmSegment().getProps()));
+        } else {
+            AlgorithmConfiguration algorithmConfiguration = currentAlgorithms.get(shardingStrategySegment.getShardingAlgorithmName());
+            if (null == algorithmConfiguration) {
+                return false;
+            }
+            shardingAlgorithm = ShardingAlgorithmFactory.newInstance(algorithmConfiguration);
         }
-        return currentAlgorithms.contains(shardingStrategySegment.getShardingAlgorithmName());
+        

Review Comment:
   done it.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@shardingsphere.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org