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/01/11 17:30:49 UTC

[shardingsphere] branch master updated: Refactor ifNotExists of CREATE BROADCAST TABLE RULE. (#23513)

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 e5ad8ea84f9 Refactor ifNotExists of CREATE BROADCAST TABLE RULE. (#23513)
e5ad8ea84f9 is described below

commit e5ad8ea84f959cd6f936b8278f7656fdf55dff1d
Author: yx9o <ya...@163.com>
AuthorDate: Thu Jan 12 01:30:39 2023 +0800

    Refactor ifNotExists of CREATE BROADCAST TABLE RULE. (#23513)
---
 .../CreateBroadcastTableRuleStatementUpdater.java  | 49 ++++++++++------------
 ...eateBroadcastTableRuleStatementUpdaterTest.java |  3 +-
 2 files changed, 24 insertions(+), 28 deletions(-)

diff --git a/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/CreateBroadcastTableRuleStatementUpdater.java b/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/CreateBroadcastTableRuleStatementUpdater.java
index c0ae5512b83..3d145a5e8b9 100644
--- a/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/CreateBroadcastTableRuleStatementUpdater.java
+++ b/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/CreateBroadcastTableRuleStatementUpdater.java
@@ -18,8 +18,6 @@
 package org.apache.shardingsphere.sharding.distsql.handler.update;
 
 import org.apache.shardingsphere.distsql.handler.exception.rule.DuplicateRuleException;
-import org.apache.shardingsphere.distsql.handler.exception.rule.RuleDefinitionViolationException;
-import org.apache.shardingsphere.distsql.handler.exception.rule.RuleInUsedException;
 import org.apache.shardingsphere.distsql.handler.update.RuleDefinitionCreateUpdater;
 import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
 import org.apache.shardingsphere.infra.util.exception.ShardingSpherePreconditions;
@@ -27,52 +25,51 @@ import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration;
 import org.apache.shardingsphere.sharding.distsql.parser.statement.CreateBroadcastTableRuleStatement;
 
 import java.util.Collection;
-import java.util.LinkedList;
+import java.util.LinkedHashSet;
 
 /**
  * Create broadcast table rule statement updater.
  */
 public final class CreateBroadcastTableRuleStatementUpdater implements RuleDefinitionCreateUpdater<CreateBroadcastTableRuleStatement, ShardingRuleConfiguration> {
     
-    private boolean ifNotExists;
-    
     @Override
-    public void checkSQLStatement(final ShardingSphereDatabase database, final CreateBroadcastTableRuleStatement sqlStatement,
-                                  final ShardingRuleConfiguration currentRuleConfig) throws RuleDefinitionViolationException {
-        ifNotExists = sqlStatement.isIfNotExists();
-        if (!ifNotExists) {
+    public void checkSQLStatement(final ShardingSphereDatabase database, final CreateBroadcastTableRuleStatement sqlStatement, final ShardingRuleConfiguration currentRuleConfig) {
+        if (!sqlStatement.isIfNotExists()) {
             checkDuplicate(sqlStatement, currentRuleConfig);
         }
     }
     
-    private void checkDuplicate(final CreateBroadcastTableRuleStatement sqlStatement, final ShardingRuleConfiguration currentRuleConfig) throws RuleInUsedException {
-        if (null == currentRuleConfig || currentRuleConfig.getBroadcastTables().isEmpty()) {
-            return;
-        }
-        Collection<String> duplicateBroadcastTables = new LinkedList<>(currentRuleConfig.getBroadcastTables());
-        duplicateBroadcastTables.retainAll(sqlStatement.getTables());
-        ShardingSpherePreconditions.checkState(duplicateBroadcastTables.isEmpty(), () -> new DuplicateRuleException("Broadcast", sqlStatement.getTables()));
-    }
-    
     @Override
     public ShardingRuleConfiguration buildToBeCreatedRuleConfiguration(final ShardingRuleConfiguration currentRuleConfig, final CreateBroadcastTableRuleStatement sqlStatement) {
         ShardingRuleConfiguration result = new ShardingRuleConfiguration();
-        result.setBroadcastTables(sqlStatement.getTables());
+        Collection<String> tables = sqlStatement.getTables();
+        if (sqlStatement.isIfNotExists()) {
+            Collection<String> duplicatedRuleNames = getDuplicatedRuleNames(sqlStatement, currentRuleConfig);
+            tables.removeIf(each -> duplicatedRuleNames.contains(each));
+        }
+        result.setBroadcastTables(tables);
         return result;
     }
     
     @Override
     public void updateCurrentRuleConfiguration(final ShardingRuleConfiguration currentRuleConfig, final ShardingRuleConfiguration toBeCreatedRuleConfig) {
-        if (ifNotExists) {
-            Collection<String> currentBroadCastTables = currentRuleConfig.getBroadcastTables();
-            toBeCreatedRuleConfig.getBroadcastTables().removeIf(currentBroadCastTables::contains);
-        }
-        if (toBeCreatedRuleConfig.getBroadcastTables().isEmpty()) {
-            return;
-        }
         currentRuleConfig.getBroadcastTables().addAll(toBeCreatedRuleConfig.getBroadcastTables());
     }
     
+    private Collection<String> getDuplicatedRuleNames(final CreateBroadcastTableRuleStatement sqlStatement, final ShardingRuleConfiguration currentRuleConfig) {
+        Collection<String> result = new LinkedHashSet<>();
+        if (null != currentRuleConfig && !currentRuleConfig.getBroadcastTables().isEmpty()) {
+            result.addAll(currentRuleConfig.getBroadcastTables());
+        }
+        result.retainAll(sqlStatement.getTables());
+        return result;
+    }
+    
+    private void checkDuplicate(final CreateBroadcastTableRuleStatement sqlStatement, final ShardingRuleConfiguration currentRuleConfig) {
+        Collection<String> duplicatedRuleNames = getDuplicatedRuleNames(sqlStatement, currentRuleConfig);
+        ShardingSpherePreconditions.checkState(duplicatedRuleNames.isEmpty(), () -> new DuplicateRuleException("Broadcast", sqlStatement.getTables()));
+    }
+    
     @Override
     public Class<ShardingRuleConfiguration> getRuleConfigurationClass() {
         return ShardingRuleConfiguration.class;
diff --git a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/CreateBroadcastTableRuleStatementUpdaterTest.java b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/CreateBroadcastTableRuleStatementUpdaterTest.java
index b6a58064e34..acb2898aca6 100644
--- a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/CreateBroadcastTableRuleStatementUpdaterTest.java
+++ b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/CreateBroadcastTableRuleStatementUpdaterTest.java
@@ -98,8 +98,7 @@ public final class CreateBroadcastTableRuleStatementUpdaterTest {
     
     private ShardingRuleConfiguration createCurrentRuleConfiguration() {
         ShardingRuleConfiguration result = new ShardingRuleConfiguration();
-        result.getBroadcastTables().add("t_order");
-        result.getBroadcastTables().add("t_address");
+        result.getBroadcastTables().addAll(Arrays.asList("t_order", "t_address"));
         return result;
     }
 }