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

zhaojinchao 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 7362338b777 Refactor ShardingAuditChecker (#18712)
7362338b777 is described below

commit 7362338b777008ef735ae3b091fc33e5c67e986c
Author: Liang Zhang <zhangli...@apache.org>
AuthorDate: Thu Jun 30 10:13:50 2022 +0800

    Refactor ShardingAuditChecker (#18712)
    
    * Rename ShardingRuleConfiguration.shardingAudits to auditStrategies
    
    * Refactor ShardingAuditChecker
    
    * Refactor ShardingAuditChecker
---
 .../sharding/spi/ShardingAuditAlgorithm.java        |  8 ++------
 .../checker/audit/ShardingAuditChecker.java         |  2 +-
 .../shardingsphere/sharding/rule/ShardingRule.java  | 21 +++++++--------------
 3 files changed, 10 insertions(+), 21 deletions(-)

diff --git 
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-api/src/main/java/org/apache/shardingsphere/sharding/spi/ShardingAuditAlgorithm.java
 
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-api/src/main/java/org/apache/shardingsphere/sharding/spi/ShardingAuditAlgorithm.java
index 4afed78c067..058cb46a4c2 100644
--- 
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-api/src/main/java/org/apache/shardingsphere/sharding/spi/ShardingAuditAlgorithm.java
+++ 
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-api/src/main/java/org/apache/shardingsphere/sharding/spi/ShardingAuditAlgorithm.java
@@ -21,11 +21,9 @@ import org.apache.shardingsphere.infra.check.SQLCheckResult;
 import 
org.apache.shardingsphere.infra.config.algorithm.ShardingSphereAlgorithm;
 import 
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
 import org.apache.shardingsphere.infra.metadata.user.Grantee;
-import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
 import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
 
 import java.util.List;
-import java.util.Map;
 
 /**
  * Sharding audit algorithm.
@@ -37,11 +35,9 @@ public interface ShardingAuditAlgorithm extends 
ShardingSphereAlgorithm {
      *
      * @param sqlStatement SQL statement
      * @param parameters SQL parameters
-     * @param currentDatabase current database
-     * @param databases databases
      * @param grantee grantee
-     * @param rule shardingSphereRule
+     * @param database database
      * @return SQL check result
      */
-    SQLCheckResult check(SQLStatement sqlStatement, List<Object> parameters, 
Grantee grantee, String currentDatabase, Map<String, ShardingSphereDatabase> 
databases, ShardingSphereRule rule);
+    SQLCheckResult check(SQLStatement sqlStatement, List<Object> parameters, 
Grantee grantee, ShardingSphereDatabase database);
 }
diff --git 
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/checker/audit/ShardingAuditChecker.java
 
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/checker/audit/ShardingAuditChecker.java
index 465e60bd1d7..d58ae646054 100644
--- 
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/checker/audit/ShardingAuditChecker.java
+++ 
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/checker/audit/ShardingAuditChecker.java
@@ -45,7 +45,7 @@ public final class ShardingAuditChecker implements 
SQLChecker<ShardingRule> {
     public SQLCheckResult check(final SQLStatement sqlStatement, final 
List<Object> parameters, final Grantee grantee,
                                 final String currentDatabase, final 
Map<String, ShardingSphereDatabase> databases, final ShardingRule rule) {
         for (Entry<String, ShardingAuditStrategyConfiguration> entry : 
rule.getAuditStrategies().entrySet()) {
-            SQLCheckResult result = 
rule.getShardingAuditAlgorithms().get(entry.getValue().getAuditAlgorithmName()).check(sqlStatement,
 parameters, grantee, currentDatabase, databases, rule);
+            SQLCheckResult result = 
rule.getAuditAlgorithms().get(entry.getValue().getAuditAlgorithmName()).check(sqlStatement,
 parameters, grantee, databases.get(currentDatabase));
             if (!result.isPassed()) {
                 return result;
             }
diff --git 
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/rule/ShardingRule.java
 
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/rule/ShardingRule.java
index c87cc2ff8c2..ad65c20e89a 100644
--- 
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/rule/ShardingRule.java
+++ 
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/rule/ShardingRule.java
@@ -95,7 +95,7 @@ public final class ShardingRule implements DatabaseRule, 
DataNodeContainedRule,
     
     private final Map<String, KeyGenerateAlgorithm> keyGenerators = new 
LinkedHashMap<>();
     
-    private final Map<String, ShardingAuditAlgorithm> shardingAuditAlgorithms 
= new LinkedHashMap<>();
+    private final Map<String, ShardingAuditAlgorithm> auditAlgorithms = new 
LinkedHashMap<>();
     
     private final Map<String, TableRule> tableRules = new LinkedHashMap<>();
     
@@ -120,7 +120,7 @@ public final class ShardingRule implements DatabaseRule, 
DataNodeContainedRule,
         this.dataSourceNames = getDataSourceNames(config.getTables(), 
config.getAutoTables(), dataSourceNames);
         config.getShardingAlgorithms().forEach((key, value) -> 
shardingAlgorithms.put(key, createShardingAlgorithm(key, value, 
config.getTables(), config.getAutoTables())));
         config.getKeyGenerators().forEach((key, value) -> 
keyGenerators.put(key, KeyGenerateAlgorithmFactory.newInstance(value)));
-        config.getAuditAlgorithms().forEach((key, value) -> 
shardingAuditAlgorithms.put(key, 
ShardingAuditAlgorithmFactory.newInstance(value)));
+        config.getAuditAlgorithms().forEach((key, value) -> 
auditAlgorithms.put(key, ShardingAuditAlgorithmFactory.newInstance(value)));
         tableRules.putAll(createTableRules(config.getTables(), 
config.getDefaultKeyGenerateStrategy()));
         tableRules.putAll(createAutoTableRules(config.getAutoTables(), 
config.getDefaultKeyGenerateStrategy()));
         broadcastTables = createBroadcastTables(config.getBroadcastTables());
@@ -132,10 +132,11 @@ public final class ShardingRule implements DatabaseRule, 
DataNodeContainedRule,
                 : 
keyGenerators.get(config.getDefaultKeyGenerateStrategy().getKeyGeneratorName());
         defaultShardingColumn = config.getDefaultShardingColumn();
         shardingTableDataNodes = createShardingTableDataNodes(tableRules);
-        auditStrategies = createShardingAudits(config.getAuditStrategies());
         
Preconditions.checkArgument(isValidBindingTableConfiguration(tableRules, new 
BindingTableCheckedConfiguration(this.dataSourceNames, shardingAlgorithms, 
config.getBindingTableGroups(),
                 broadcastTables, defaultDatabaseShardingStrategyConfig, 
defaultTableShardingStrategyConfig, defaultShardingColumn)),
                 "Invalid binding table configuration in 
ShardingRuleConfiguration.");
+        auditStrategies = null == config.getAuditStrategies() ? 
Collections.emptyMap() : config.getAuditStrategies();
+        
Preconditions.checkArgument(auditStrategies.values().stream().allMatch(each -> 
auditAlgorithms.containsKey(each.getAuditAlgorithmName())), "Cannot find 
sharding audit algorithm");
     }
     
     public ShardingRule(final AlgorithmProvidedShardingRuleConfiguration 
config, final Collection<String> dataSourceNames) {
@@ -143,7 +144,7 @@ public final class ShardingRule implements DatabaseRule, 
DataNodeContainedRule,
         this.dataSourceNames = getDataSourceNames(config.getTables(), 
config.getAutoTables(), dataSourceNames);
         shardingAlgorithms.putAll(config.getShardingAlgorithms());
         keyGenerators.putAll(config.getKeyGenerators());
-        shardingAuditAlgorithms.putAll(config.getAuditAlgorithms());
+        auditAlgorithms.putAll(config.getAuditAlgorithms());
         tableRules.putAll(createTableRules(config.getTables(), 
config.getDefaultKeyGenerateStrategy()));
         tableRules.putAll(createAutoTableRules(config.getAutoTables(), 
config.getDefaultKeyGenerateStrategy()));
         broadcastTables = createBroadcastTables(config.getBroadcastTables());
@@ -155,10 +156,11 @@ public final class ShardingRule implements DatabaseRule, 
DataNodeContainedRule,
                 : 
keyGenerators.get(config.getDefaultKeyGenerateStrategy().getKeyGeneratorName());
         defaultShardingColumn = config.getDefaultShardingColumn();
         shardingTableDataNodes = createShardingTableDataNodes(tableRules);
-        auditStrategies = createShardingAudits(config.getAuditStrategies());
         
Preconditions.checkArgument(isValidBindingTableConfiguration(tableRules, new 
BindingTableCheckedConfiguration(this.dataSourceNames, shardingAlgorithms, 
config.getBindingTableGroups(),
                 broadcastTables, defaultDatabaseShardingStrategyConfig, 
defaultTableShardingStrategyConfig, defaultShardingColumn)),
                 "Invalid binding table configuration in 
ShardingRuleConfiguration.");
+        auditStrategies = null == config.getAuditStrategies() ? 
Collections.emptyMap() : config.getAuditStrategies();
+        
Preconditions.checkArgument(auditStrategies.values().stream().allMatch(each -> 
auditAlgorithms.containsKey(each.getAuditAlgorithmName())), "Cannot find 
sharding audit algorithm");
     }
     
     private Map<String, Collection<DataNode>> 
createShardingTableDataNodes(final Map<String, TableRule> tableRules) {
@@ -169,15 +171,6 @@ public final class ShardingRule implements DatabaseRule, 
DataNodeContainedRule,
         return result;
     }
     
-    private Map<String, ShardingAuditStrategyConfiguration> 
createShardingAudits(final Map<String, ShardingAuditStrategyConfiguration> 
shardingAudits) {
-        if (null == shardingAudits) {
-            return new LinkedHashMap<>(0, 1);
-        }
-        
Preconditions.checkArgument(shardingAudits.values().stream().allMatch(each -> 
shardingAuditAlgorithms.containsKey(each.getAuditAlgorithmName())),
-                "Cannot find sharding audit algorithm");
-        return shardingAudits;
-    }
-    
     private Collection<String> getDataSourceNames(final 
Collection<ShardingTableRuleConfiguration> tableRuleConfigs,
                                                   final 
Collection<ShardingAutoTableRuleConfiguration> autoTableRuleConfigs, final 
Collection<String> dataSourceNames) {
         if (tableRuleConfigs.isEmpty() && autoTableRuleConfigs.isEmpty()) {

Reply via email to