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

zhangliang 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 437bc8426c0 Fix case-insensitive issue when partial update encrypt and 
mask rule. (#31330)
437bc8426c0 is described below

commit 437bc8426c0fb57ed87bf8f6b7851cce3dee1b3c
Author: Cong Hu <[email protected]>
AuthorDate: Wed May 22 10:35:59 2024 +0800

    Fix case-insensitive issue when partial update encrypt and mask rule. 
(#31330)
---
 .../shardingsphere/encrypt/rule/EncryptRule.java   | 30 ++++++++++++----------
 .../apache/shardingsphere/mask/rule/MaskRule.java  | 27 ++++++++++---------
 2 files changed, 29 insertions(+), 28 deletions(-)

diff --git 
a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rule/EncryptRule.java
 
b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rule/EncryptRule.java
index 19992b809fe..50a32bb9a51 100644
--- 
a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rule/EncryptRule.java
+++ 
b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rule/EncryptRule.java
@@ -17,6 +17,8 @@
 
 package org.apache.shardingsphere.encrypt.rule;
 
+import com.cedarsoftware.util.CaseInsensitiveMap;
+import com.cedarsoftware.util.CaseInsensitiveSet;
 import com.google.common.base.Preconditions;
 import org.apache.shardingsphere.encrypt.api.config.EncryptRuleConfiguration;
 import 
org.apache.shardingsphere.encrypt.api.config.rule.EncryptColumnRuleConfiguration;
@@ -33,7 +35,8 @@ import 
org.apache.shardingsphere.infra.rule.scope.DatabaseRule;
 import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
 
 import java.util.Collection;
-import java.util.HashSet;
+import java.util.Collections;
+import java.util.Map;
 import java.util.Map.Entry;
 import java.util.Optional;
 import java.util.concurrent.ConcurrentHashMap;
@@ -49,9 +52,9 @@ public final class EncryptRule implements DatabaseRule, 
PartialRuleUpdateSupport
     
     private final AtomicReference<EncryptRuleConfiguration> ruleConfig = new 
AtomicReference<>();
     
-    private final ConcurrentHashMap<String, EncryptAlgorithm> encryptors;
+    private final Map<String, EncryptAlgorithm> encryptors;
     
-    private final ConcurrentHashMap<String, EncryptTable> tables = new 
ConcurrentHashMap<>();
+    private final Map<String, EncryptTable> tables = new 
CaseInsensitiveMap<>(Collections.emptyMap(), new ConcurrentHashMap<>());
     
     private final AtomicReference<RuleAttributes> attributes = new 
AtomicReference<>();
     
@@ -61,13 +64,13 @@ public final class EncryptRule implements DatabaseRule, 
PartialRuleUpdateSupport
         encryptors = createEncryptors(ruleConfig);
         for (EncryptTableRuleConfiguration each : ruleConfig.getTables()) {
             each.getColumns().forEach(this::checkEncryptorType);
-            tables.put(each.getName().toLowerCase(), new EncryptTable(each, 
encryptors));
+            tables.put(each.getName(), new EncryptTable(each, encryptors));
         }
         attributes.set(new RuleAttributes(new 
EncryptTableMapperRuleAttribute(tables.keySet())));
     }
     
-    private ConcurrentHashMap<String, EncryptAlgorithm> createEncryptors(final 
EncryptRuleConfiguration ruleConfig) {
-        ConcurrentHashMap<String, EncryptAlgorithm> result = new 
ConcurrentHashMap<>(ruleConfig.getEncryptors().size(), 1F);
+    private Map<String, EncryptAlgorithm> createEncryptors(final 
EncryptRuleConfiguration ruleConfig) {
+        Map<String, EncryptAlgorithm> result = new 
CaseInsensitiveMap<>(Collections.emptyMap(), new 
ConcurrentHashMap<>(ruleConfig.getEncryptors().size(), 1F));
         for (Entry<String, AlgorithmConfiguration> entry : 
ruleConfig.getEncryptors().entrySet()) {
             result.put(entry.getKey(), 
TypedSPILoader.getService(EncryptAlgorithm.class, entry.getValue().getType(), 
entry.getValue().getProps()));
         }
@@ -98,12 +101,12 @@ public final class EncryptRule implements DatabaseRule, 
PartialRuleUpdateSupport
     
     /**
      * Find encrypt table.
-     * 
+     *
      * @param tableName table name
      * @return encrypt table
      */
     public Optional<EncryptTable> findEncryptTable(final String tableName) {
-        return Optional.ofNullable(tables.get(tableName.toLowerCase()));
+        return Optional.ofNullable(tables.get(tableName));
     }
     
     /**
@@ -135,17 +138,16 @@ public final class EncryptRule implements DatabaseRule, 
PartialRuleUpdateSupport
     
     @Override
     public boolean partialUpdate(final EncryptRuleConfiguration 
toBeUpdatedRuleConfig) {
-        Collection<String> toBeAddedTableNames = 
toBeUpdatedRuleConfig.getTables().stream().map(EncryptTableRuleConfiguration::getName).filter(each
 -> !tables.containsKey(each.toLowerCase()))
-                .collect(Collectors.toList());
+        Collection<String> toBeUpdatedTablesNames = 
toBeUpdatedRuleConfig.getTables().stream().map(EncryptTableRuleConfiguration::getName).collect(Collectors.toCollection(CaseInsensitiveSet::new));
+        Collection<String> toBeAddedTableNames = 
toBeUpdatedTablesNames.stream().filter(each -> 
!tables.containsKey(each)).collect(Collectors.toList());
         if (!toBeAddedTableNames.isEmpty()) {
             toBeAddedTableNames.forEach(each -> addTableRule(each, 
toBeUpdatedRuleConfig));
             attributes.set(new RuleAttributes(new 
EncryptTableMapperRuleAttribute(tables.keySet())));
             return true;
         }
-        Collection<String> toBeRemovedTableNames = new 
HashSet<>(tables.keySet());
-        
toBeRemovedTableNames.removeAll(toBeUpdatedRuleConfig.getTables().stream().map(EncryptTableRuleConfiguration::getName).collect(Collectors.toList()));
+        Collection<String> toBeRemovedTableNames = 
tables.keySet().stream().filter(each -> 
!toBeUpdatedTablesNames.contains(each)).collect(Collectors.toList());
         if (!toBeRemovedTableNames.isEmpty()) {
-            
toBeRemovedTableNames.stream().map(String::toLowerCase).forEach(tables::remove);
+            toBeRemovedTableNames.forEach(tables::remove);
             attributes.set(new RuleAttributes(new 
EncryptTableMapperRuleAttribute(tables.keySet())));
             // TODO check and remove unused INLINE encryptors
             return true;
@@ -161,7 +163,7 @@ public final class EncryptRule implements DatabaseRule, 
PartialRuleUpdateSupport
             encryptors.computeIfAbsent(entry.getKey(), key -> 
TypedSPILoader.getService(EncryptAlgorithm.class, entry.getValue().getType(), 
entry.getValue().getProps()));
         }
         tableRuleConfig.getColumns().forEach(this::checkEncryptorType);
-        tables.put(tableName.toLowerCase(), new EncryptTable(tableRuleConfig, 
encryptors));
+        tables.put(tableName, new EncryptTable(tableRuleConfig, encryptors));
     }
     
     private EncryptTableRuleConfiguration getTableRuleConfiguration(final 
String tableName, final EncryptRuleConfiguration toBeUpdatedRuleConfig) {
diff --git 
a/features/mask/core/src/main/java/org/apache/shardingsphere/mask/rule/MaskRule.java
 
b/features/mask/core/src/main/java/org/apache/shardingsphere/mask/rule/MaskRule.java
index 560eeb4002f..deed8174bf1 100644
--- 
a/features/mask/core/src/main/java/org/apache/shardingsphere/mask/rule/MaskRule.java
+++ 
b/features/mask/core/src/main/java/org/apache/shardingsphere/mask/rule/MaskRule.java
@@ -17,6 +17,8 @@
 
 package org.apache.shardingsphere.mask.rule;
 
+import com.cedarsoftware.util.CaseInsensitiveMap;
+import com.cedarsoftware.util.CaseInsensitiveSet;
 import com.google.common.base.Preconditions;
 import 
org.apache.shardingsphere.infra.algorithm.core.config.AlgorithmConfiguration;
 import org.apache.shardingsphere.infra.rule.PartialRuleUpdateSupported;
@@ -29,7 +31,8 @@ import 
org.apache.shardingsphere.mask.rule.attribute.MaskTableMapperRuleAttribut
 import org.apache.shardingsphere.mask.spi.MaskAlgorithm;
 
 import java.util.Collection;
-import java.util.HashSet;
+import java.util.Collections;
+import java.util.Map;
 import java.util.Map.Entry;
 import java.util.Optional;
 import java.util.concurrent.ConcurrentHashMap;
@@ -43,20 +46,17 @@ public final class MaskRule implements DatabaseRule, 
PartialRuleUpdateSupported<
     
     private final AtomicReference<MaskRuleConfiguration> configuration = new 
AtomicReference<>();
     
-    private final ConcurrentHashMap<String, MaskAlgorithm<?, ?>> 
maskAlgorithms;
+    private final Map<String, MaskAlgorithm<?, ?>> maskAlgorithms = new 
CaseInsensitiveMap<>(Collections.emptyMap(), new ConcurrentHashMap<>());
     
-    private final ConcurrentHashMap<String, MaskTable> tables;
+    private final Map<String, MaskTable> tables = new 
CaseInsensitiveMap<>(Collections.emptyMap(), new ConcurrentHashMap<>());
     
     private final AtomicReference<RuleAttributes> attributes = new 
AtomicReference<>();
     
     @SuppressWarnings("unchecked")
     public MaskRule(final MaskRuleConfiguration ruleConfig) {
         configuration.set(ruleConfig);
-        maskAlgorithms = ruleConfig.getMaskAlgorithms().entrySet().stream()
-                .collect(Collectors.toMap(Entry::getKey, entry -> 
TypedSPILoader.getService(MaskAlgorithm.class, entry.getValue().getType(), 
entry.getValue().getProps()),
-                        (oldValue, currentValue) -> oldValue, 
ConcurrentHashMap::new));
-        tables = ruleConfig.getTables().stream()
-                .collect(Collectors.toMap(each -> 
each.getName().toLowerCase(), each -> new MaskTable(each, maskAlgorithms), 
(oldValue, currentValue) -> oldValue, ConcurrentHashMap::new));
+        ruleConfig.getMaskAlgorithms().forEach((key, value) -> 
maskAlgorithms.put(key, TypedSPILoader.getService(MaskAlgorithm.class, 
value.getType(), value.getProps())));
+        ruleConfig.getTables().forEach(each -> tables.put(each.getName(), new 
MaskTable(each, maskAlgorithms)));
         attributes.set(new RuleAttributes(new 
MaskTableMapperRuleAttribute(tables.keySet())));
     }
     
@@ -87,17 +87,16 @@ public final class MaskRule implements DatabaseRule, 
PartialRuleUpdateSupported<
     
     @Override
     public boolean partialUpdate(final MaskRuleConfiguration 
toBeUpdatedRuleConfig) {
-        Collection<String> toBeAddedTableNames = 
toBeUpdatedRuleConfig.getTables().stream().map(MaskTableRuleConfiguration::getName).collect(Collectors.toList());
-        toBeAddedTableNames.removeAll(tables.keySet());
+        Collection<String> toBeUpdatedTablesNames = 
toBeUpdatedRuleConfig.getTables().stream().map(MaskTableRuleConfiguration::getName).collect(Collectors.toCollection(CaseInsensitiveSet::new));
+        Collection<String> toBeAddedTableNames = 
toBeUpdatedTablesNames.stream().filter(each -> 
!tables.containsKey(each)).collect(Collectors.toList());
         if (!toBeAddedTableNames.isEmpty()) {
             toBeAddedTableNames.forEach(each -> addTableRule(each, 
toBeUpdatedRuleConfig));
             attributes.set(new RuleAttributes(new 
MaskTableMapperRuleAttribute(tables.keySet())));
             return true;
         }
-        Collection<String> toBeRemovedTableNames = new 
HashSet<>(tables.keySet());
-        
toBeRemovedTableNames.removeAll(toBeUpdatedRuleConfig.getTables().stream().map(MaskTableRuleConfiguration::getName).collect(Collectors.toList()));
+        Collection<String> toBeRemovedTableNames = 
tables.keySet().stream().filter(each -> 
!toBeUpdatedTablesNames.contains(each)).collect(Collectors.toList());
         if (!toBeRemovedTableNames.isEmpty()) {
-            
toBeRemovedTableNames.stream().map(String::toLowerCase).forEach(tables::remove);
+            toBeRemovedTableNames.forEach(tables::remove);
             attributes.set(new RuleAttributes(new 
MaskTableMapperRuleAttribute(tables.keySet())));
             // TODO check and remove unused INLINE mask algorithms
             return true;
@@ -112,7 +111,7 @@ public final class MaskRule implements DatabaseRule, 
PartialRuleUpdateSupported<
         for (Entry<String, AlgorithmConfiguration> entry : 
toBeUpdatedRuleConfig.getMaskAlgorithms().entrySet()) {
             maskAlgorithms.computeIfAbsent(entry.getKey(), key -> 
TypedSPILoader.getService(MaskAlgorithm.class, entry.getValue().getType(), 
entry.getValue().getProps()));
         }
-        tables.put(tableName.toLowerCase(), new MaskTable(tableRuleConfig, 
maskAlgorithms));
+        tables.put(tableName, new MaskTable(tableRuleConfig, maskAlgorithms));
     }
     
     private MaskTableRuleConfiguration getTableRuleConfiguration(final String 
tableName, final MaskRuleConfiguration toBeUpdatedRuleConfig) {

Reply via email to