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

menghaoran 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 982de42  Refactor PrivilegeMerge (#9968)
982de42 is described below

commit 982de42b585a05420ab2ccebc4fc9cf58f9830cb
Author: Juan Pan(Trista) <[email protected]>
AuthorDate: Wed Apr 7 17:04:48 2021 +0800

    Refactor PrivilegeMerge (#9968)
---
 .../storage/impl/StoragePrivilegeBuilder.java      | 15 +++--
 .../storage/impl/StoragePrivilegeMerger.java       | 65 ++++++++++------------
 .../authority/merge/PrivilegeMergeTest.java        |  2 +-
 3 files changed, 39 insertions(+), 43 deletions(-)

diff --git 
a/shardingsphere-features/shardingsphere-authority/shardingsphere-authority-common/src/main/java/org/apache/shardingsphere/authority/loader/storage/impl/StoragePrivilegeBuilder.java
 
b/shardingsphere-features/shardingsphere-authority/shardingsphere-authority-common/src/main/java/org/apache/shardingsphere/authority/loader/storage/impl/StoragePrivilegeBuilder.java
index b703acd..5c1bfee 100644
--- 
a/shardingsphere-features/shardingsphere-authority/shardingsphere-authority-common/src/main/java/org/apache/shardingsphere/authority/loader/storage/impl/StoragePrivilegeBuilder.java
+++ 
b/shardingsphere-features/shardingsphere-authority/shardingsphere-authority-common/src/main/java/org/apache/shardingsphere/authority/loader/storage/impl/StoragePrivilegeBuilder.java
@@ -31,6 +31,7 @@ import 
org.apache.shardingsphere.infra.spi.typed.TypedSPIRegistry;
 import javax.sql.DataSource;
 import java.util.Collection;
 import java.util.HashSet;
+import java.util.Iterator;
 import java.util.LinkedHashMap;
 import java.util.LinkedHashSet;
 import java.util.Map;
@@ -149,11 +150,15 @@ public final class StoragePrivilegeBuilder {
     }
     
     private static void checkPrivileges(final Map<ShardingSphereUser, 
Collection<ShardingSpherePrivileges>> userPrivilegeMap) {
-        for (Entry<ShardingSphereUser, Collection<ShardingSpherePrivileges>> 
entry : userPrivilegeMap.entrySet()) {
-            for (ShardingSpherePrivileges each : entry.getValue()) {
-                if (each.isEmpty()) {
-                    throw new ShardingSphereException(String.format("There is 
no enough privileges for %s on all database instances.", 
entry.getKey().getGrantee().toString().replaceAll("%", "%%")));
-                }
+        userPrivilegeMap.forEach(StoragePrivilegeBuilder::checkPrivileges);
+    }
+    
+    private static void checkPrivileges(final ShardingSphereUser user, final 
Collection<ShardingSpherePrivileges> privileges) {
+        Iterator<ShardingSpherePrivileges> iterator = privileges.iterator();
+        ShardingSpherePrivileges current = iterator.next();
+        while (iterator.hasNext()) {
+            if (!current.equals(iterator.next())) {
+                throw new ShardingSphereException("Different physical 
instances have different privileges for user %s", 
user.getGrantee().toString().replaceAll("%", "%%"));
             }
         }
     }
diff --git 
a/shardingsphere-features/shardingsphere-authority/shardingsphere-authority-common/src/main/java/org/apache/shardingsphere/authority/loader/storage/impl/StoragePrivilegeMerger.java
 
b/shardingsphere-features/shardingsphere-authority/shardingsphere-authority-common/src/main/java/org/apache/shardingsphere/authority/loader/storage/impl/StoragePrivilegeMerger.java
index 2d41d85..4f0a8c3 100644
--- 
a/shardingsphere-features/shardingsphere-authority/shardingsphere-authority-common/src/main/java/org/apache/shardingsphere/authority/loader/storage/impl/StoragePrivilegeMerger.java
+++ 
b/shardingsphere-features/shardingsphere-authority/shardingsphere-authority-common/src/main/java/org/apache/shardingsphere/authority/loader/storage/impl/StoragePrivilegeMerger.java
@@ -19,17 +19,15 @@ package 
org.apache.shardingsphere.authority.loader.storage.impl;
 
 import lombok.AccessLevel;
 import lombok.NoArgsConstructor;
+import org.apache.shardingsphere.authority.model.ShardingSpherePrivileges;
 import org.apache.shardingsphere.authority.model.database.SchemaPrivileges;
 import org.apache.shardingsphere.authority.model.database.TablePrivileges;
-import org.apache.shardingsphere.infra.exception.ShardingSphereException;
-import org.apache.shardingsphere.authority.model.ShardingSpherePrivileges;
 import org.apache.shardingsphere.infra.metadata.user.ShardingSphereUser;
 import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
 import org.apache.shardingsphere.infra.rule.type.DataNodeContainedRule;
 
 import java.util.Collection;
 import java.util.HashMap;
-import java.util.Iterator;
 import java.util.Map;
 import java.util.Map.Entry;
 import java.util.Optional;
@@ -43,62 +41,55 @@ public final class StoragePrivilegeMerger {
     /**
      * Merge privileges.
      * 
-     * @param authority authority
+     * @param privileges privileges
      * @param schemaName schema name
      * @param rules ShardingSphere rules
      * @return privileges
      */
-    public static Map<ShardingSphereUser, ShardingSpherePrivileges> 
merge(final Map<ShardingSphereUser, Collection<ShardingSpherePrivileges>> 
authority,
+    public static Map<ShardingSphereUser, ShardingSpherePrivileges> 
merge(final Map<ShardingSphereUser, Collection<ShardingSpherePrivileges>> 
privileges,
                                                                          final 
String schemaName, final Collection<ShardingSphereRule> rules) {
-        Map<ShardingSphereUser, ShardingSpherePrivileges> result = new 
HashMap<>(authority.size(), 1);
-        for (Entry<ShardingSphereUser, Collection<ShardingSpherePrivileges>> 
entry : authority.entrySet()) {
-            result.put(entry.getKey(), merge(entry.getKey(), entry.getValue(), 
schemaName, rules));
+        Map<ShardingSphereUser, ShardingSpherePrivileges> result = new 
HashMap<>(privileges.size(), 1);
+        for (Entry<ShardingSphereUser, Collection<ShardingSpherePrivileges>> 
entry : privileges.entrySet()) {
+            result.put(entry.getKey(), merge(entry.getValue(), schemaName, 
rules));
         }
         return result;
     }
     
-    private static ShardingSpherePrivileges merge(final ShardingSphereUser 
user, final Collection<ShardingSpherePrivileges> privileges, final String 
schemaName,
+    private static ShardingSpherePrivileges merge(final 
Collection<ShardingSpherePrivileges> privileges, final String schemaName,
                                                  final 
Collection<ShardingSphereRule> rules) {
-        if (privileges.isEmpty()) {
-            return new ShardingSpherePrivileges();
-        }
-        Iterator<ShardingSpherePrivileges> iterator = privileges.iterator();
-        ShardingSpherePrivileges result = iterator.next();
-        while (iterator.hasNext()) {
-            ShardingSpherePrivileges each = iterator.next();
-            if (!result.equals(each)) {
-                throw new ShardingSphereException("Different physical 
instances have different permissions for user %s@%s", 
user.getGrantee().getUsername(), user.getGrantee().getHostname());
-            }
-        }
-        merge(result, schemaName, rules);
+        return privileges.isEmpty() ? new ShardingSpherePrivileges() : 
getMergedPrivileges(privileges.iterator().next(), schemaName, rules);
+    }
+    
+    private static ShardingSpherePrivileges getMergedPrivileges(final 
ShardingSpherePrivileges privilege, final String schemaName, final 
Collection<ShardingSphereRule> rules) {
+        ShardingSpherePrivileges result = new ShardingSpherePrivileges();
+        
result.getAdministrativePrivileges().getPrivileges().addAll(privilege.getAdministrativePrivileges().getPrivileges());
+        
result.getDatabasePrivileges().getGlobalPrivileges().addAll(privilege.getDatabasePrivileges().getGlobalPrivileges());
+        
result.getDatabasePrivileges().getSpecificPrivileges().putAll(getMergedSchemaPrivileges(privilege,
 schemaName, rules));
         return result;
     }
     
-    private static void merge(final ShardingSpherePrivileges privilege, final 
String schemaName, final Collection<ShardingSphereRule> rules) {
-        Map<String, SchemaPrivileges> schemaPrivilegeMap = new HashMap<>();
+    private static Map<String, SchemaPrivileges> 
getMergedSchemaPrivileges(final ShardingSpherePrivileges privilege, final 
String schemaName, final Collection<ShardingSphereRule> rules) {
+        Map<String, SchemaPrivileges> result = new HashMap<>();
         for (Entry<String, SchemaPrivileges> entry : 
privilege.getDatabasePrivileges().getSpecificPrivileges().entrySet()) {
-            if (!schemaPrivilegeMap.containsKey(schemaName)) {
-                SchemaPrivileges newSchemaPrivilege = new 
SchemaPrivileges(schemaName);
-                
newSchemaPrivilege.getGlobalPrivileges().addAll(entry.getValue().getGlobalPrivileges());
-                
newSchemaPrivilege.getSpecificPrivileges().putAll(entry.getValue().getSpecificPrivileges());
-                merge(newSchemaPrivilege, rules);
-                schemaPrivilegeMap.put(schemaName, newSchemaPrivilege);
+            if (!result.containsKey(schemaName)) {
+                SchemaPrivileges schemaPrivileges = new 
SchemaPrivileges(schemaName);
+                
schemaPrivileges.getGlobalPrivileges().addAll(entry.getValue().getGlobalPrivileges());
+                
schemaPrivileges.getSpecificPrivileges().putAll(getMergedTablePrivileges(entry.getValue(),
 rules));
+                result.put(schemaName, schemaPrivileges);
             }
         }
-        privilege.getDatabasePrivileges().getSpecificPrivileges().clear();
-        
privilege.getDatabasePrivileges().getSpecificPrivileges().putAll(schemaPrivilegeMap);
+        return result;
     }
     
-    private static void merge(final SchemaPrivileges privilege, final 
Collection<ShardingSphereRule> rules) {
-        Map<String, TablePrivileges> tablePrivilegeMap = new HashMap<>();
+    private static Map<String, TablePrivileges> getMergedTablePrivileges(final 
SchemaPrivileges privilege, final Collection<ShardingSphereRule> rules) {
+        Map<String, TablePrivileges> result = new HashMap<>();
         for (Entry<String, TablePrivileges> entry : 
privilege.getSpecificPrivileges().entrySet()) {
             Optional<String> logicalTable = getLogicalTable(entry, rules);
-            if (logicalTable.isPresent() && 
!tablePrivilegeMap.containsKey(logicalTable.get())) {
-                tablePrivilegeMap.put(logicalTable.get(), new 
TablePrivileges(logicalTable.get(), entry.getValue().getPrivileges()));
+            if (logicalTable.isPresent() && 
!result.containsKey(logicalTable.get())) {
+                result.put(logicalTable.get(), new 
TablePrivileges(logicalTable.get(), entry.getValue().getPrivileges()));
             }
         }
-        privilege.getSpecificPrivileges().clear();
-        privilege.getSpecificPrivileges().putAll(tablePrivilegeMap);
+        return result;
     }
     
     private static Optional<String> getLogicalTable(final Entry<String, 
TablePrivileges> privilege, final Collection<ShardingSphereRule> rules) {
diff --git 
a/shardingsphere-features/shardingsphere-authority/shardingsphere-authority-common/src/test/java/org/apache/shardingsphere/authority/merge/PrivilegeMergeTest.java
 
b/shardingsphere-features/shardingsphere-authority/shardingsphere-authority-common/src/test/java/org/apache/shardingsphere/authority/merge/PrivilegeMergeTest.java
index 9cf3eb0..4260bad 100644
--- 
a/shardingsphere-features/shardingsphere-authority/shardingsphere-authority-common/src/test/java/org/apache/shardingsphere/authority/merge/PrivilegeMergeTest.java
+++ 
b/shardingsphere-features/shardingsphere-authority/shardingsphere-authority-common/src/test/java/org/apache/shardingsphere/authority/merge/PrivilegeMergeTest.java
@@ -38,7 +38,7 @@ import static org.junit.Assert.assertTrue;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
 
-public class PrivilegeMergeTest {
+public final class PrivilegeMergeTest {
     
     @Test
     public void assertPrivilegeMergeResult() {

Reply via email to