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/06/15 10:51:09 UTC
[shardingsphere] branch master updated: Refactor MaskRule RDL statement updater (#26370)
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 99b1cea2db5 Refactor MaskRule RDL statement updater (#26370)
99b1cea2db5 is described below
commit 99b1cea2db57c0d9e503fd072ba432f0174f2935
Author: ChenJiaHao <Pa...@163.com>
AuthorDate: Thu Jun 15 18:51:00 2023 +0800
Refactor MaskRule RDL statement updater (#26370)
* Refactor MaskRule RDL statement updater
* Fix ReadwriteSplittingRule RDL statement updater
* Fix ShadowRule RDL statement updater
* Fix code style
* Fix code style
---
.../update/AlterMaskRuleStatementUpdater.java | 20 +++++++++++++++++++
.../update/DropMaskRuleStatementUpdater.java | 23 ++++++++++++++++++++++
...lterReadwriteSplittingRuleStatementUpdater.java | 2 +-
.../DropShadowAlgorithmStatementUpdater.java | 2 +-
4 files changed, 45 insertions(+), 2 deletions(-)
diff --git a/features/mask/distsql/handler/src/main/java/org/apache/shardingsphere/mask/distsql/handler/update/AlterMaskRuleStatementUpdater.java b/features/mask/distsql/handler/src/main/java/org/apache/shardingsphere/mask/distsql/handler/update/AlterMaskRuleStatementUpdater.java
index 7ec34a2c215..5022fdd6da2 100644
--- a/features/mask/distsql/handler/src/main/java/org/apache/shardingsphere/mask/distsql/handler/update/AlterMaskRuleStatementUpdater.java
+++ b/features/mask/distsql/handler/src/main/java/org/apache/shardingsphere/mask/distsql/handler/update/AlterMaskRuleStatementUpdater.java
@@ -19,6 +19,7 @@ package org.apache.shardingsphere.mask.distsql.handler.update;
import org.apache.shardingsphere.distsql.handler.exception.rule.MissingRequiredRuleException;
import org.apache.shardingsphere.distsql.handler.update.RuleDefinitionAlterUpdater;
+import org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.util.exception.ShardingSpherePreconditions;
import org.apache.shardingsphere.mask.api.config.MaskRuleConfiguration;
@@ -29,6 +30,9 @@ import org.apache.shardingsphere.mask.distsql.parser.segment.MaskRuleSegment;
import org.apache.shardingsphere.mask.distsql.parser.statement.AlterMaskRuleStatement;
import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
import java.util.stream.Collectors;
/**
@@ -61,6 +65,22 @@ public final class AlterMaskRuleStatementUpdater implements RuleDefinitionAlterU
public MaskRuleConfiguration buildToBeAlteredRuleConfiguration(final AlterMaskRuleStatement sqlStatement) {
return MaskRuleStatementConverter.convert(sqlStatement.getRules());
}
+
+ @Override
+ public MaskRuleConfiguration buildToBeDroppedRuleConfiguration(final MaskRuleConfiguration currentRuleConfig, final MaskRuleConfiguration toBeAlteredRuleConfig) {
+ Collection<String> toBeAlteredTableNames = toBeAlteredRuleConfig.getTables().stream().map(MaskTableRuleConfiguration::getName).collect(Collectors.toList());
+ Collection<MaskColumnRuleConfiguration> columns = currentRuleConfig.getTables().stream().filter(each -> !toBeAlteredTableNames.contains(each.getName()))
+ .flatMap(each -> each.getColumns().stream()).collect(Collectors.toList());
+ columns.addAll(toBeAlteredRuleConfig.getTables().stream().flatMap(each -> each.getColumns().stream()).collect(Collectors.toList()));
+ Collection<String> inUsedAlgorithmNames = columns.stream().map(MaskColumnRuleConfiguration::getMaskAlgorithm).collect(Collectors.toSet());
+ Map<String, AlgorithmConfiguration> toBeDroppedAlgorithms = new HashMap<>();
+ for (String each : currentRuleConfig.getMaskAlgorithms().keySet()) {
+ if (!inUsedAlgorithmNames.contains(each)) {
+ toBeDroppedAlgorithms.put(each, currentRuleConfig.getMaskAlgorithms().get(each));
+ }
+ }
+ return new MaskRuleConfiguration(Collections.emptyList(), toBeDroppedAlgorithms);
+ }
@Override
public void updateCurrentRuleConfiguration(final MaskRuleConfiguration currentRuleConfig, final MaskRuleConfiguration toBeAlteredRuleConfig) {
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 726b1e96fae..851a581d495 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
@@ -19,6 +19,7 @@ package org.apache.shardingsphere.mask.distsql.handler.update;
import org.apache.shardingsphere.distsql.handler.exception.rule.MissingRequiredRuleException;
import org.apache.shardingsphere.distsql.handler.update.RuleDefinitionDropUpdater;
+import org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.util.exception.ShardingSpherePreconditions;
import org.apache.shardingsphere.mask.api.config.MaskRuleConfiguration;
@@ -27,6 +28,10 @@ import org.apache.shardingsphere.mask.api.config.rule.MaskTableRuleConfiguration
import org.apache.shardingsphere.mask.distsql.parser.statement.DropMaskRuleStatement;
import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.LinkedList;
+import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
@@ -55,6 +60,24 @@ public final class DropMaskRuleStatementUpdater implements RuleDefinitionDropUpd
return null != currentRuleConfig
&& !getIdenticalData(currentRuleConfig.getTables().stream().map(MaskTableRuleConfiguration::getName).collect(Collectors.toSet()), sqlStatement.getTables()).isEmpty();
}
+
+ @Override
+ public MaskRuleConfiguration buildToBeDroppedRuleConfiguration(final MaskRuleConfiguration currentRuleConfig, final DropMaskRuleStatement sqlStatement) {
+ Collection<MaskTableRuleConfiguration> toBeDroppedTables = new LinkedList<>();
+ for (String each : sqlStatement.getTables()) {
+ toBeDroppedTables.add(new MaskTableRuleConfiguration(each, Collections.emptyList()));
+ }
+ Collection<MaskColumnRuleConfiguration> columns = currentRuleConfig.getTables().stream().filter(each -> !sqlStatement.getTables().contains(each.getName()))
+ .flatMap(each -> each.getColumns().stream()).collect(Collectors.toList());
+ Collection<String> inUsedAlgorithmNames = columns.stream().map(MaskColumnRuleConfiguration::getMaskAlgorithm).collect(Collectors.toSet());
+ Map<String, AlgorithmConfiguration> toBeDroppedAlgorithms = new HashMap<>();
+ for (String each : currentRuleConfig.getMaskAlgorithms().keySet()) {
+ if (!inUsedAlgorithmNames.contains(each)) {
+ toBeDroppedAlgorithms.put(each, currentRuleConfig.getMaskAlgorithms().get(each));
+ }
+ }
+ return new MaskRuleConfiguration(toBeDroppedTables, toBeDroppedAlgorithms);
+ }
@Override
public boolean updateCurrentRuleConfiguration(final DropMaskRuleStatement sqlStatement, final MaskRuleConfiguration currentRuleConfig) {
diff --git a/features/readwrite-splitting/distsql/handler/src/main/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/update/AlterReadwriteSplittingRuleStatementUpdater.java b/features/readwrite-splitting/distsql/handler/src/main/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/update/AlterReadwriteSplittingRuleStatementUpdater.java
index 0b448300759..c6c80219376 100644
--- a/features/readwrite-splitting/distsql/handler/src/main/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/update/AlterReadwriteSplittingRuleStatementUpdater.java
+++ b/features/readwrite-splitting/distsql/handler/src/main/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/update/AlterReadwriteSplittingRuleStatementUpdater.java
@@ -60,7 +60,7 @@ public final class AlterReadwriteSplittingRuleStatementUpdater implements RuleDe
for (ReadwriteSplittingDataSourceRuleConfiguration each : currentRuleConfig.getDataSources()) {
if (toBeAlteredDataSourceNames.contains(each.getName())) {
dataSources.add(each);
- loadBalancers.put(each.getLoadBalancerName(), null);
+ loadBalancers.put(each.getLoadBalancerName(), currentRuleConfig.getLoadBalancers().get(each));
}
}
return new ReadwriteSplittingRuleConfiguration(dataSources, loadBalancers);
diff --git a/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/update/DropShadowAlgorithmStatementUpdater.java b/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/update/DropShadowAlgorithmStatementUpdater.java
index 32619964c9a..40736448726 100644
--- a/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/update/DropShadowAlgorithmStatementUpdater.java
+++ b/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/update/DropShadowAlgorithmStatementUpdater.java
@@ -81,7 +81,7 @@ public final class DropShadowAlgorithmStatementUpdater implements RuleDefinition
public ShadowRuleConfiguration buildToBeDroppedRuleConfiguration(final ShadowRuleConfiguration currentRuleConfig, final DropShadowAlgorithmStatement sqlStatement) {
ShadowRuleConfiguration result = new ShadowRuleConfiguration();
for (String each : sqlStatement.getNames()) {
- result.getShadowAlgorithms().put(each, null);
+ result.getShadowAlgorithms().put(each, currentRuleConfig.getShadowAlgorithms().get(each));
}
return result;
}