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 fc8ec17ae08 Improve DistSQL DROP ENCRYPT RULE (#22378)
fc8ec17ae08 is described below
commit fc8ec17ae08104a58cae1a1259d924964f657597
Author: ChenJiaHao <[email protected]>
AuthorDate: Thu Nov 24 18:24:02 2022 +0800
Improve DistSQL DROP ENCRYPT RULE (#22378)
* Improve DROP ENCRYPT RULE, remove unused algorithms while dropping rule
* Supplementary test cases
* Fix style
* Fix variable name
---
.../update/DropEncryptRuleStatementUpdater.java | 28 ++++++++++------------
.../DropEncryptRuleStatementUpdaterTest.java | 11 +++++----
2 files changed, 20 insertions(+), 19 deletions(-)
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 5647fd1be4d..a05599f1e08 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
@@ -18,6 +18,7 @@
package org.apache.shardingsphere.encrypt.distsql.handler.update;
import org.apache.shardingsphere.encrypt.api.config.EncryptRuleConfiguration;
+import
org.apache.shardingsphere.encrypt.api.config.rule.EncryptColumnRuleConfiguration;
import
org.apache.shardingsphere.encrypt.api.config.rule.EncryptTableRuleConfiguration;
import
org.apache.shardingsphere.encrypt.distsql.parser.statement.DropEncryptRuleStatement;
import
org.apache.shardingsphere.infra.distsql.exception.rule.MissingRequiredRuleException;
@@ -57,28 +58,25 @@ public final class DropEncryptRuleStatementUpdater
implements RuleDefinitionDrop
@Override
public boolean updateCurrentRuleConfiguration(final
DropEncryptRuleStatement sqlStatement, final EncryptRuleConfiguration
currentRuleConfig) {
- for (String each : sqlStatement.getTables()) {
- dropRule(currentRuleConfig, each);
- }
+ sqlStatement.getTables().forEach(each -> dropRule(currentRuleConfig,
each));
+ dropUnusedEncryptor(currentRuleConfig);
return currentRuleConfig.getTables().isEmpty();
}
private void dropRule(final EncryptRuleConfiguration currentRuleConfig,
final String ruleName) {
Optional<EncryptTableRuleConfiguration> encryptTableRuleConfig =
currentRuleConfig.getTables().stream().filter(each ->
each.getName().equals(ruleName)).findAny();
- encryptTableRuleConfig.ifPresent(optional -> {
- currentRuleConfig.getTables().remove(encryptTableRuleConfig.get());
- encryptTableRuleConfig.get().getColumns().stream().filter(column
-> !isEncryptorInUse(currentRuleConfig, column.getEncryptorName()))
- .forEach(each ->
currentRuleConfig.getEncryptors().remove(each.getEncryptorName()));
- });
+ encryptTableRuleConfig.ifPresent(optional ->
currentRuleConfig.getTables().remove(encryptTableRuleConfig.get()));
}
- private boolean isEncryptorInUse(final EncryptRuleConfiguration
currentRuleConfig, final String toBeDroppedEncryptorName) {
- for (EncryptTableRuleConfiguration each :
currentRuleConfig.getTables()) {
- if (each.getColumns().stream().anyMatch(column ->
column.getEncryptorName().equals(toBeDroppedEncryptorName))) {
- return true;
- }
- }
- return false;
+ private void dropUnusedEncryptor(final EncryptRuleConfiguration
currentRuleConfig) {
+ Collection<String> inUsedEncryptors =
currentRuleConfig.getTables().stream().flatMap(each ->
each.getColumns().stream()).map(EncryptColumnRuleConfiguration::getEncryptorName)
+ .collect(Collectors.toSet());
+
inUsedEncryptors.addAll(currentRuleConfig.getTables().stream().flatMap(each ->
each.getColumns().stream()).map(EncryptColumnRuleConfiguration::getAssistedQueryEncryptorName)
+ .collect(Collectors.toSet()));
+
inUsedEncryptors.addAll(currentRuleConfig.getTables().stream().flatMap(each ->
each.getColumns().stream()).map(EncryptColumnRuleConfiguration::getLikeQueryEncryptorName)
+ .collect(Collectors.toSet()));
+ Collection<String> unusedEncryptors =
currentRuleConfig.getEncryptors().keySet().stream().filter(each ->
!inUsedEncryptors.contains(each)).collect(Collectors.toSet());
+ unusedEncryptors.forEach(each ->
currentRuleConfig.getEncryptors().remove(each));
}
@Override
diff --git
a/features/encrypt/distsql/handler/src/test/java/org/apache/shardingsphere/encrypt/distsql/handler/update/DropEncryptRuleStatementUpdaterTest.java
b/features/encrypt/distsql/handler/src/test/java/org/apache/shardingsphere/encrypt/distsql/handler/update/DropEncryptRuleStatementUpdaterTest.java
index 8fbce9d4700..02a8760a2cc 100644
---
a/features/encrypt/distsql/handler/src/test/java/org/apache/shardingsphere/encrypt/distsql/handler/update/DropEncryptRuleStatementUpdaterTest.java
+++
b/features/encrypt/distsql/handler/src/test/java/org/apache/shardingsphere/encrypt/distsql/handler/update/DropEncryptRuleStatementUpdaterTest.java
@@ -81,7 +81,7 @@ public final class DropEncryptRuleStatementUpdaterTest {
DropEncryptRuleStatement statement = createSQLStatement(true,
"t_encrypt_1");
updater.checkSQLStatement(database, statement,
mock(EncryptRuleConfiguration.class));
assertFalse(updater.updateCurrentRuleConfiguration(statement,
ruleConfig));
- assertThat(ruleConfig.getEncryptors().size(), is(1));
+ assertThat(ruleConfig.getEncryptors().size(), is(3));
}
private DropEncryptRuleStatement createSQLStatement(final String
tableName) {
@@ -93,10 +93,13 @@ public final class DropEncryptRuleStatementUpdaterTest {
}
private EncryptRuleConfiguration createCurrentRuleConfiguration() {
- EncryptColumnRuleConfiguration columnRuleConfig = new
EncryptColumnRuleConfiguration("user_id", "user_cipher", "", "", "user_plain",
"t_encrypt_user_id_MD5", null);
+ EncryptColumnRuleConfiguration columnRuleConfig = new
EncryptColumnRuleConfiguration("user_id", "user_cipher", "", "", "user_plain",
"t_encrypt_user_id_MD5", "t_encrypt_test_assisted",
+ "t_encrypt_test_like", null);
+ Map<String, AlgorithmConfiguration> encryptors = new HashMap<>(3, 1);
+ encryptors.put("t_encrypt_user_id_MD5", new
AlgorithmConfiguration("TEST", new Properties()));
+ encryptors.put("t_encrypt_test_assisted", new
AlgorithmConfiguration("TEST", new Properties()));
+ encryptors.put("t_encrypt_test_like", new
AlgorithmConfiguration("TEST", new Properties()));
EncryptTableRuleConfiguration tableRuleConfig = new
EncryptTableRuleConfiguration("t_encrypt",
Collections.singleton(columnRuleConfig), null);
- Map<String, AlgorithmConfiguration> encryptors = new HashMap<>(
- Collections.singletonMap("t_encrypt_user_id_MD5", new
AlgorithmConfiguration("TEST", new Properties())));
return new EncryptRuleConfiguration(new
LinkedList<>(Collections.singleton(tableRuleConfig)), encryptors, true);
}