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 83527e1fa78 Refactor check DatabaseRule is empty (#27544)
83527e1fa78 is described below

commit 83527e1fa7880509fb246685a100f7e785a9b9f7
Author: ChenJiaHao <[email protected]>
AuthorDate: Mon Jul 31 18:38:57 2023 +0800

    Refactor check DatabaseRule is empty (#27544)
---
 .../DropBroadcastTableRuleStatementUpdater.java    |  2 +-
 .../update/DropEncryptRuleStatementUpdater.java    |  2 +-
 .../update/DropMaskRuleStatementUpdater.java       |  2 +-
 ...DropReadwriteSplittingRuleStatementUpdater.java |  2 +-
 ...DropDefaultShadowAlgorithmStatementUpdater.java |  2 +-
 .../update/DropShadowRuleStatementUpdater.java     |  2 +-
 ...ropDefaultShardingStrategyStatementUpdater.java | 10 +-------
 .../DropShardingTableRuleStatementUpdater.java     |  2 +-
 .../proxy/backend/util/ExportUtils.java            | 30 ++--------------------
 9 files changed, 10 insertions(+), 44 deletions(-)

diff --git 
a/features/broadcast/distsql/handler/src/main/java/org/apache/shardingsphere/broadcast/distsql/handler/update/DropBroadcastTableRuleStatementUpdater.java
 
b/features/broadcast/distsql/handler/src/main/java/org/apache/shardingsphere/broadcast/distsql/handler/update/DropBroadcastTableRuleStatementUpdater.java
index 5eadddbd2bc..1976d16f34c 100644
--- 
a/features/broadcast/distsql/handler/src/main/java/org/apache/shardingsphere/broadcast/distsql/handler/update/DropBroadcastTableRuleStatementUpdater.java
+++ 
b/features/broadcast/distsql/handler/src/main/java/org/apache/shardingsphere/broadcast/distsql/handler/update/DropBroadcastTableRuleStatementUpdater.java
@@ -76,7 +76,7 @@ public final class DropBroadcastTableRuleStatementUpdater 
implements RuleDefinit
     @Override
     public boolean updateCurrentRuleConfiguration(final 
DropBroadcastTableRuleStatement sqlStatement, final BroadcastRuleConfiguration 
currentRuleConfig) {
         currentRuleConfig.getTables().removeIf(each -> 
containsIgnoreCase(sqlStatement.getTables(), each));
-        return currentRuleConfig.getTables().isEmpty();
+        return currentRuleConfig.isEmpty();
     }
     
     @Override
diff --git 
a/features/encrypt/distsql/handler/src/main/java/org/apache/shardingsphere/encrypt/distsql/handler/update/DropEncryptRuleStatementUpdater.java
 
b/features/encrypt/distsql/handler/src/main/java/org/apache/shardingsphere/encrypt/distsql/handler/update/DropEncryptRuleStatementUpdater.java
index 8eb1234f812..59378f5724b 100644
--- 
a/features/encrypt/distsql/handler/src/main/java/org/apache/shardingsphere/encrypt/distsql/handler/update/DropEncryptRuleStatementUpdater.java
+++ 
b/features/encrypt/distsql/handler/src/main/java/org/apache/shardingsphere/encrypt/distsql/handler/update/DropEncryptRuleStatementUpdater.java
@@ -77,7 +77,7 @@ public final class DropEncryptRuleStatementUpdater implements 
RuleDefinitionDrop
     public boolean updateCurrentRuleConfiguration(final 
DropEncryptRuleStatement sqlStatement, final EncryptRuleConfiguration 
currentRuleConfig) {
         sqlStatement.getTables().forEach(each -> dropRule(currentRuleConfig, 
each));
         dropUnusedEncryptor(currentRuleConfig);
-        return currentRuleConfig.getTables().isEmpty();
+        return currentRuleConfig.isEmpty();
     }
     
     private void dropRule(final EncryptRuleConfiguration currentRuleConfig, 
final String ruleName) {
diff --git 
a/features/mask/distsql/handler/src/main/java/org/apache/shardingsphere/mask/distsql/handler/update/DropMaskRuleStatementUpdater.java
 
b/features/mask/distsql/handler/src/main/java/org/apache/shardingsphere/mask/distsql/handler/update/DropMaskRuleStatementUpdater.java
index ba8d9b1bfdd..267ba37a8cd 100644
--- 
a/features/mask/distsql/handler/src/main/java/org/apache/shardingsphere/mask/distsql/handler/update/DropMaskRuleStatementUpdater.java
+++ 
b/features/mask/distsql/handler/src/main/java/org/apache/shardingsphere/mask/distsql/handler/update/DropMaskRuleStatementUpdater.java
@@ -77,7 +77,7 @@ public final class DropMaskRuleStatementUpdater implements 
RuleDefinitionDropUpd
     public boolean updateCurrentRuleConfiguration(final DropMaskRuleStatement 
sqlStatement, final MaskRuleConfiguration currentRuleConfig) {
         sqlStatement.getTables().forEach(each -> dropRule(currentRuleConfig, 
each));
         dropUnusedAlgorithm(currentRuleConfig);
-        return currentRuleConfig.getTables().isEmpty();
+        return currentRuleConfig.isEmpty();
     }
     
     private void dropRule(final MaskRuleConfiguration currentRuleConfig, final 
String ruleName) {
diff --git 
a/features/readwrite-splitting/distsql/handler/src/main/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/update/DropReadwriteSplittingRuleStatementUpdater.java
 
b/features/readwrite-splitting/distsql/handler/src/main/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/update/DropReadwriteSplittingRuleStatementUpdater.java
index 3e142547f17..e5092a27b61 100644
--- 
a/features/readwrite-splitting/distsql/handler/src/main/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/update/DropReadwriteSplittingRuleStatementUpdater.java
+++ 
b/features/readwrite-splitting/distsql/handler/src/main/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/update/DropReadwriteSplittingRuleStatementUpdater.java
@@ -111,7 +111,7 @@ public final class 
DropReadwriteSplittingRuleStatementUpdater implements RuleDef
             dropRule(currentRuleConfig, each);
         }
         dropUnusedLoadBalancer(currentRuleConfig);
-        return currentRuleConfig.getDataSources().isEmpty();
+        return currentRuleConfig.isEmpty();
     }
     
     private void dropRule(final ReadwriteSplittingRuleConfiguration 
currentRuleConfig, final String ruleName) {
diff --git 
a/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/update/DropDefaultShadowAlgorithmStatementUpdater.java
 
b/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/update/DropDefaultShadowAlgorithmStatementUpdater.java
index 8449675534c..9cb712f080a 100644
--- 
a/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/update/DropDefaultShadowAlgorithmStatementUpdater.java
+++ 
b/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/update/DropDefaultShadowAlgorithmStatementUpdater.java
@@ -66,7 +66,7 @@ public final class DropDefaultShadowAlgorithmStatementUpdater 
implements RuleDef
     public boolean updateCurrentRuleConfiguration(final 
DropDefaultShadowAlgorithmStatement sqlStatement, final ShadowRuleConfiguration 
currentRuleConfig) {
         
currentRuleConfig.getShadowAlgorithms().remove(currentRuleConfig.getDefaultShadowAlgorithmName());
         currentRuleConfig.setDefaultShadowAlgorithmName(null);
-        return false;
+        return currentRuleConfig.isEmpty();
     }
     
     @Override
diff --git 
a/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/update/DropShadowRuleStatementUpdater.java
 
b/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/update/DropShadowRuleStatementUpdater.java
index 8e579a2187f..ccc64402abe 100644
--- 
a/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/update/DropShadowRuleStatementUpdater.java
+++ 
b/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/update/DropShadowRuleStatementUpdater.java
@@ -112,7 +112,7 @@ public final class DropShadowRuleStatementUpdater 
implements RuleDefinitionDropU
         currentRuleConfig.getTables().forEach((key, value) -> 
value.getDataSourceNames().removeIf(sqlStatement.getNames()::contains));
         currentRuleConfig.getTables().entrySet().removeIf(entry -> 
entry.getValue().getDataSourceNames().isEmpty());
         dropUnusedAlgorithm(currentRuleConfig);
-        return currentRuleConfig.getDataSources().isEmpty() || 
currentRuleConfig.getTables().isEmpty();
+        return currentRuleConfig.isEmpty();
     }
     
     private void dropRule(final ShadowRuleConfiguration currentRuleConfig, 
final String ruleName) {
diff --git 
a/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/DropDefaultShardingStrategyStatementUpdater.java
 
b/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/DropDefaultShardingStrategyStatementUpdater.java
index b8fd9337a92..02d14377dc9 100644
--- 
a/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/DropDefaultShardingStrategyStatementUpdater.java
+++ 
b/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/DropDefaultShardingStrategyStatementUpdater.java
@@ -94,15 +94,7 @@ public final class 
DropDefaultShardingStrategyStatementUpdater implements RuleDe
             currentRuleConfig.setDefaultDatabaseShardingStrategy(null);
         }
         UnusedAlgorithmFinder.find(currentRuleConfig).forEach(each -> 
currentRuleConfig.getShardingAlgorithms().remove(each));
-        return isEmptyShardingTables(currentRuleConfig) && 
isEmptyShardingStrategy(currentRuleConfig);
-    }
-    
-    private boolean isEmptyShardingTables(final ShardingRuleConfiguration 
currentRuleConfig) {
-        return currentRuleConfig.getTables().isEmpty() && 
currentRuleConfig.getAutoTables().isEmpty();
-    }
-    
-    private boolean isEmptyShardingStrategy(final ShardingRuleConfiguration 
currentRuleConfig) {
-        return null == currentRuleConfig.getDefaultDatabaseShardingStrategy() 
&& null == currentRuleConfig.getDefaultTableShardingStrategy();
+        return currentRuleConfig.isEmpty();
     }
     
     @Override
diff --git 
a/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/DropShardingTableRuleStatementUpdater.java
 
b/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/DropShardingTableRuleStatementUpdater.java
index 4d096a9907b..27f8d808e47 100644
--- 
a/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/DropShardingTableRuleStatementUpdater.java
+++ 
b/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/DropShardingTableRuleStatementUpdater.java
@@ -127,7 +127,7 @@ public final class DropShardingTableRuleStatementUpdater 
implements RuleDefiniti
         UnusedAlgorithmFinder.find(currentRuleConfig).forEach(each -> 
currentRuleConfig.getShardingAlgorithms().remove(each));
         dropUnusedKeyGenerator(currentRuleConfig);
         dropUnusedAuditor(currentRuleConfig);
-        return isEmptyShardingTables(currentRuleConfig) && 
isEmptyShardingStrategy(currentRuleConfig);
+        return currentRuleConfig.isEmpty();
     }
     
     private void dropShardingTable(final ShardingRuleConfiguration 
currentRuleConfig, final String tableName) {
diff --git 
a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/util/ExportUtils.java
 
b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/util/ExportUtils.java
index 311d8556039..f9dea0f6507 100644
--- 
a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/util/ExportUtils.java
+++ 
b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/util/ExportUtils.java
@@ -19,20 +19,14 @@ package org.apache.shardingsphere.proxy.backend.util;
 
 import lombok.AccessLevel;
 import lombok.NoArgsConstructor;
-import 
org.apache.shardingsphere.encrypt.api.config.CompatibleEncryptRuleConfiguration;
-import org.apache.shardingsphere.encrypt.api.config.EncryptRuleConfiguration;
 import org.apache.shardingsphere.infra.config.rule.RuleConfiguration;
+import 
org.apache.shardingsphere.infra.config.rule.scope.DatabaseRuleConfiguration;
 import org.apache.shardingsphere.infra.datasource.props.DataSourceProperties;
 import 
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
 import org.apache.shardingsphere.infra.util.spi.type.ordered.OrderedSPILoader;
 import org.apache.shardingsphere.infra.util.yaml.YamlEngine;
 import 
org.apache.shardingsphere.infra.yaml.config.swapper.rule.YamlRuleConfigurationSwapper;
-import org.apache.shardingsphere.mask.api.config.MaskRuleConfiguration;
 import org.apache.shardingsphere.proxy.backend.exception.FileIOException;
-import 
org.apache.shardingsphere.readwritesplitting.api.ReadwriteSplittingRuleConfiguration;
-import org.apache.shardingsphere.shadow.api.config.ShadowRuleConfiguration;
-import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration;
-import org.apache.shardingsphere.single.api.config.SingleRuleConfiguration;
 
 import java.io.File;
 import java.io.IOException;
@@ -116,30 +110,10 @@ public final class ExportUtils {
         }
         stringBuilder.append("rules:").append(System.lineSeparator());
         for (Entry<RuleConfiguration, YamlRuleConfigurationSwapper> entry : 
OrderedSPILoader.getServices(YamlRuleConfigurationSwapper.class, 
ruleConfigs).entrySet()) {
-            if (checkRuleConfigIsEmpty(entry.getKey())) {
+            if (((DatabaseRuleConfiguration) entry.getKey()).isEmpty()) {
                 continue;
             }
             
stringBuilder.append(YamlEngine.marshal(Collections.singletonList(entry.getValue().swapToYamlConfiguration(entry.getKey()))));
         }
     }
-    
-    private static boolean checkRuleConfigIsEmpty(final RuleConfiguration 
ruleConfig) {
-        if (ruleConfig instanceof ShardingRuleConfiguration) {
-            ShardingRuleConfiguration shardingRuleConfig = 
(ShardingRuleConfiguration) ruleConfig;
-            return shardingRuleConfig.getTables().isEmpty() && 
shardingRuleConfig.getAutoTables().isEmpty();
-        } else if (ruleConfig instanceof ReadwriteSplittingRuleConfiguration) {
-            return ((ReadwriteSplittingRuleConfiguration) 
ruleConfig).getDataSources().isEmpty();
-        } else if (ruleConfig instanceof EncryptRuleConfiguration) {
-            return ((EncryptRuleConfiguration) 
ruleConfig).getTables().isEmpty();
-        } else if (ruleConfig instanceof CompatibleEncryptRuleConfiguration) {
-            return ((CompatibleEncryptRuleConfiguration) 
ruleConfig).getTables().isEmpty();
-        } else if (ruleConfig instanceof ShadowRuleConfiguration) {
-            return ((ShadowRuleConfiguration) 
ruleConfig).getTables().isEmpty();
-        } else if (ruleConfig instanceof MaskRuleConfiguration) {
-            return ((MaskRuleConfiguration) ruleConfig).getTables().isEmpty();
-        } else if (ruleConfig instanceof SingleRuleConfiguration) {
-            return !((SingleRuleConfiguration) 
ruleConfig).getDefaultDataSource().isPresent();
-        }
-        return false;
-    }
 }

Reply via email to