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 2022/12/30 08:17:03 UTC
[shardingsphere] branch master updated: Add `drop mask rule` DistSQL updater (#23197)
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 77325f0bbfa Add `drop mask rule` DistSQL updater (#23197)
77325f0bbfa is described below
commit 77325f0bbfac48070efb668dec8250b314f79bec
Author: Zichao <57...@users.noreply.github.com>
AuthorDate: Fri Dec 30 21:16:56 2022 +1300
Add `drop mask rule` DistSQL updater (#23197)
* add `drop mask rule` DistSQL updater
* add `drop mask rule` DistSQL updater
---
.../update/DropMaskRuleStatementUpdater.java | 84 ++++++++++++++++++++++
...re.distsql.handler.update.RuleDefinitionUpdater | 2 +-
.../parser/core/MaskDistSQLStatementVisitor.java | 7 ++
.../parser/statement/DropMaskRuleStatement.java | 37 ++++++++++
4 files changed, 129 insertions(+), 1 deletion(-)
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
new file mode 100644
index 00000000000..1d7c3a37127
--- /dev/null
+++ b/features/mask/distsql/handler/src/main/java/org/apache/shardingsphere/mask/distsql/handler/update/DropMaskRuleStatementUpdater.java
@@ -0,0 +1,84 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+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.metadata.database.ShardingSphereDatabase;
+import org.apache.shardingsphere.infra.util.exception.ShardingSpherePreconditions;
+import org.apache.shardingsphere.mask.api.config.MaskRuleConfiguration;
+import org.apache.shardingsphere.mask.api.config.rule.MaskColumnRuleConfiguration;
+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.Optional;
+import java.util.stream.Collectors;
+
+/**
+ * Drop mask rule statement updater.
+ */
+public final class DropMaskRuleStatementUpdater implements RuleDefinitionDropUpdater<DropMaskRuleStatement, MaskRuleConfiguration> {
+
+ @Override
+ public void checkSQLStatement(final ShardingSphereDatabase database, final DropMaskRuleStatement sqlStatement, final MaskRuleConfiguration currentRuleConfig) {
+ checkToBeDroppedMaskTableNames(database.getName(), sqlStatement, currentRuleConfig);
+ }
+
+ private void checkToBeDroppedMaskTableNames(final String databaseName, final DropMaskRuleStatement sqlStatement, final MaskRuleConfiguration currentRuleConfig) {
+ ShardingSpherePreconditions.checkState(isExistRuleConfig(currentRuleConfig), () -> new MissingRequiredRuleException("Mask", databaseName));
+ Collection<String> currentMaskTableNames = currentRuleConfig.getTables().stream().map(MaskTableRuleConfiguration::getName).collect(Collectors.toList());
+ Collection<String> notExistedTableNames = sqlStatement.getTables().stream().filter(each -> !currentMaskTableNames.contains(each)).collect(Collectors.toList());
+ ShardingSpherePreconditions.checkState(notExistedTableNames.isEmpty(), () -> new MissingRequiredRuleException("Mask", databaseName, notExistedTableNames));
+ }
+
+ @Override
+ public boolean hasAnyOneToBeDropped(final DropMaskRuleStatement sqlStatement, final MaskRuleConfiguration currentRuleConfig) {
+ return null != currentRuleConfig
+ && !getIdenticalData(currentRuleConfig.getTables().stream().map(MaskTableRuleConfiguration::getName).collect(Collectors.toSet()), sqlStatement.getTables()).isEmpty();
+ }
+
+ @Override
+ public boolean updateCurrentRuleConfiguration(final DropMaskRuleStatement sqlStatement, final MaskRuleConfiguration currentRuleConfig) {
+ sqlStatement.getTables().forEach(each -> dropRule(currentRuleConfig, each));
+ dropUnusedAlgorithm(currentRuleConfig);
+ return currentRuleConfig.getTables().isEmpty();
+ }
+
+ private void dropRule(final MaskRuleConfiguration currentRuleConfig, final String ruleName) {
+ Optional<MaskTableRuleConfiguration> maskTableRuleConfig = currentRuleConfig.getTables().stream().filter(each -> each.getName().equals(ruleName)).findAny();
+ maskTableRuleConfig.ifPresent(optional -> currentRuleConfig.getTables().remove(maskTableRuleConfig.get()));
+ }
+
+ private void dropUnusedAlgorithm(final MaskRuleConfiguration currentRuleConfig) {
+ Collection<String> inUsedAlgorithms = currentRuleConfig.getTables().stream().flatMap(each -> each.getColumns().stream()).map(MaskColumnRuleConfiguration::getMaskAlgorithm)
+ .collect(Collectors.toSet());
+ Collection<String> unusedAlgorithms = currentRuleConfig.getMaskAlgorithms().keySet().stream().filter(each -> !inUsedAlgorithms.contains(each)).collect(Collectors.toSet());
+ unusedAlgorithms.forEach(each -> currentRuleConfig.getMaskAlgorithms().remove(each));
+ }
+
+ @Override
+ public Class<MaskRuleConfiguration> getRuleConfigurationClass() {
+ return MaskRuleConfiguration.class;
+ }
+
+ @Override
+ public String getType() {
+ return DropMaskRuleStatement.class.getName();
+ }
+}
diff --git a/features/mask/distsql/handler/src/main/resources/META-INF/services/org.apache.shardingsphere.distsql.handler.update.RuleDefinitionUpdater b/features/mask/distsql/handler/src/main/resources/META-INF/services/org.apache.shardingsphere.distsql.handler.update.RuleDefinitionUpdater
index 470110c1b8b..d736f522cef 100644
--- a/features/mask/distsql/handler/src/main/resources/META-INF/services/org.apache.shardingsphere.distsql.handler.update.RuleDefinitionUpdater
+++ b/features/mask/distsql/handler/src/main/resources/META-INF/services/org.apache.shardingsphere.distsql.handler.update.RuleDefinitionUpdater
@@ -17,4 +17,4 @@
org.apache.shardingsphere.mask.distsql.handler.update.CreateMaskRuleStatementUpdater
org.apache.shardingsphere.mask.distsql.handler.update.AlterMaskRuleStatementUpdater
-#org.apache.shardingsphere.mask.distsql.handler.update.DropMaskRuleStatementUpdater
+org.apache.shardingsphere.mask.distsql.handler.update.DropMaskRuleStatementUpdater
diff --git a/features/mask/distsql/parser/src/main/java/org/apache/shardingsphere/mask/distsql/parser/core/MaskDistSQLStatementVisitor.java b/features/mask/distsql/parser/src/main/java/org/apache/shardingsphere/mask/distsql/parser/core/MaskDistSQLStatementVisitor.java
index 68e8f63e6d1..a8fff137e8f 100644
--- a/features/mask/distsql/parser/src/main/java/org/apache/shardingsphere/mask/distsql/parser/core/MaskDistSQLStatementVisitor.java
+++ b/features/mask/distsql/parser/src/main/java/org/apache/shardingsphere/mask/distsql/parser/core/MaskDistSQLStatementVisitor.java
@@ -23,6 +23,7 @@ import org.apache.shardingsphere.distsql.parser.autogen.MaskDistSQLStatementPars
import org.apache.shardingsphere.distsql.parser.autogen.MaskDistSQLStatementParser.AlterMaskRuleContext;
import org.apache.shardingsphere.distsql.parser.autogen.MaskDistSQLStatementParser.ColumnDefinitionContext;
import org.apache.shardingsphere.distsql.parser.autogen.MaskDistSQLStatementParser.CreateMaskRuleContext;
+import org.apache.shardingsphere.distsql.parser.autogen.MaskDistSQLStatementParser.DropMaskRuleContext;
import org.apache.shardingsphere.distsql.parser.autogen.MaskDistSQLStatementParser.MaskRuleDefinitionContext;
import org.apache.shardingsphere.distsql.parser.autogen.MaskDistSQLStatementParser.PropertiesDefinitionContext;
import org.apache.shardingsphere.distsql.parser.autogen.MaskDistSQLStatementParser.PropertyContext;
@@ -31,6 +32,7 @@ import org.apache.shardingsphere.mask.distsql.parser.segment.MaskColumnSegment;
import org.apache.shardingsphere.mask.distsql.parser.segment.MaskRuleSegment;
import org.apache.shardingsphere.mask.distsql.parser.statement.AlterMaskRuleStatement;
import org.apache.shardingsphere.mask.distsql.parser.statement.CreateMaskRuleStatement;
+import org.apache.shardingsphere.mask.distsql.parser.statement.DropMaskRuleStatement;
import org.apache.shardingsphere.sql.parser.api.visitor.ASTNode;
import org.apache.shardingsphere.sql.parser.api.visitor.SQLVisitor;
import org.apache.shardingsphere.sql.parser.sql.common.value.identifier.IdentifierValue;
@@ -53,6 +55,11 @@ public final class MaskDistSQLStatementVisitor extends MaskDistSQLStatementBaseV
return new AlterMaskRuleStatement(ctx.maskRuleDefinition().stream().map(each -> (MaskRuleSegment) visit(each)).collect(Collectors.toList()));
}
+ @Override
+ public ASTNode visitDropMaskRule(final DropMaskRuleContext ctx) {
+ return new DropMaskRuleStatement(null != ctx.ifExists(), ctx.ruleName().stream().map(this::getIdentifierValue).collect(Collectors.toList()));
+ }
+
@Override
public ASTNode visitMaskRuleDefinition(final MaskRuleDefinitionContext ctx) {
return new MaskRuleSegment(getIdentifierValue(ctx.ruleName()), ctx.columnDefinition().stream().map(each -> (MaskColumnSegment) visit(each)).collect(Collectors.toList()));
diff --git a/features/mask/distsql/statement/src/main/java/org/apache/shardingsphere/mask/distsql/parser/statement/DropMaskRuleStatement.java b/features/mask/distsql/statement/src/main/java/org/apache/shardingsphere/mask/distsql/parser/statement/DropMaskRuleStatement.java
new file mode 100644
index 00000000000..b84c2701769
--- /dev/null
+++ b/features/mask/distsql/statement/src/main/java/org/apache/shardingsphere/mask/distsql/parser/statement/DropMaskRuleStatement.java
@@ -0,0 +1,37 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.mask.distsql.parser.statement;
+
+import lombok.Getter;
+import org.apache.shardingsphere.distsql.parser.statement.rdl.drop.DropRuleStatement;
+
+import java.util.Collection;
+
+/**
+ * Drop mask rule statement.
+ */
+@Getter
+public final class DropMaskRuleStatement extends DropRuleStatement {
+
+ private final Collection<String> tables;
+
+ public DropMaskRuleStatement(final boolean ifExists, final Collection<String> tables) {
+ super(ifExists);
+ this.tables = tables;
+ }
+}