taojintianxia commented on a change in pull request #12382:
URL: https://github.com/apache/shardingsphere/pull/12382#discussion_r707853288



##########
File path: 
shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-distsql/shardingsphere-shadow-distsql-handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/update/AlterShadowRuleStatementUpdater.java
##########
@@ -0,0 +1,180 @@
+/*
+ * 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.shadow.distsql.handler.update;
+
+import org.apache.shardingsphere.infra.config.RuleConfiguration;
+import org.apache.shardingsphere.infra.distsql.exception.DistSQLException;
+import 
org.apache.shardingsphere.infra.distsql.exception.resource.RequiredResourceMissedException;
+import 
org.apache.shardingsphere.infra.distsql.exception.rule.AlgorithmInUsedException;
+import 
org.apache.shardingsphere.infra.distsql.exception.rule.DuplicateRuleException;
+import 
org.apache.shardingsphere.infra.distsql.exception.rule.InvalidAlgorithmConfigurationException;
+import 
org.apache.shardingsphere.infra.distsql.exception.rule.RequiredRuleMissedException;
+import 
org.apache.shardingsphere.infra.distsql.update.RuleDefinitionAlterUpdater;
+import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
+import org.apache.shardingsphere.shadow.api.config.ShadowRuleConfiguration;
+import 
org.apache.shardingsphere.shadow.distsql.handler.converter.ShadowRuleStatementConverter;
+import 
org.apache.shardingsphere.shadow.distsql.parser.segment.ShadowAlgorithmSegment;
+import 
org.apache.shardingsphere.shadow.distsql.parser.segment.ShadowRuleSegment;
+import 
org.apache.shardingsphere.shadow.distsql.parser.statement.AlterShadowRuleStatement;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.function.Function;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+
+/**
+ * Alter shadow rule statement updater.
+ */
+public final class AlterShadowRuleStatementUpdater implements 
RuleDefinitionAlterUpdater<AlterShadowRuleStatement, ShadowRuleConfiguration> {
+    
+    private static final String SHADOW = "shadow";
+    
+    private static final String RULE_NAME = "ruleName";
+    
+    private static final String TABLE = "table";
+    
+    private static final String RESOURCE = "resource";
+    
+    private static final String ALGORITHM = "algorithm";
+    
+    @Override
+    public RuleConfiguration buildToBeAlteredRuleConfiguration(final 
AlterShadowRuleStatement sqlStatement) {
+        return ShadowRuleStatementConverter.convert(sqlStatement.getRules());
+    }
+    
+    @Override
+    public void updateCurrentRuleConfiguration(final ShadowRuleConfiguration 
currentRuleConfig, final ShadowRuleConfiguration toBeAlteredRuleConfig) {
+        
currentRuleConfig.getDataSources().putAll(toBeAlteredRuleConfig.getDataSources());
+        
currentRuleConfig.getShadowAlgorithms().putAll(toBeAlteredRuleConfig.getShadowAlgorithms());
+        
currentRuleConfig.getTables().putAll(toBeAlteredRuleConfig.getTables());
+    }
+    
+    @Override
+    public void checkSQLStatement(final ShardingSphereMetaData metaData, final 
AlterShadowRuleStatement sqlStatement, final ShadowRuleConfiguration 
currentRuleConfig) throws DistSQLException {
+        String schemaName = metaData.getName();
+        checkConfigurationExist(schemaName, currentRuleConfig);
+        checkRuleNames(schemaName, sqlStatement, currentRuleConfig);
+        checkResources(schemaName, sqlStatement, metaData);
+        checkTables(schemaName, sqlStatement);
+        checkAlgorithms(schemaName, sqlStatement);
+    }
+    
+    private void checkConfigurationExist(final String schemaName, final 
ShadowRuleConfiguration currentRuleConfig) throws DistSQLException {
+        DistSQLException.predictionThrow(null != currentRuleConfig, new 
RequiredRuleMissedException(SHADOW, schemaName));
+    }
+    
+    private void checkRuleNames(final String schemaName, final 
AlterShadowRuleStatement sqlStatement, final ShadowRuleConfiguration 
currentRuleConfig) throws DistSQLException {
+        List<String> currentRuleNames = getPropertiesFrom(currentRuleConfig, 
RULE_NAME);
+        List<String> requireRuleNames = getPropertiesFrom(sqlStatement, 
RULE_NAME);
+        checkDuplicate(requireRuleNames, duplicate -> new 
DuplicateRuleException(SHADOW, schemaName, duplicate));
+        checkDifferent(requireRuleNames, currentRuleNames, different -> new 
InvalidAlgorithmConfigurationException("shadow rule name ", different));
+    }
+    
+    private void checkTables(final String schemaName, final 
AlterShadowRuleStatement sqlStatement) throws DistSQLException {
+        List<String> requireTables = getPropertiesFrom(sqlStatement, TABLE);
+        checkDuplicate(requireTables, duplicate -> new 
DuplicateRuleException(SHADOW, schemaName, duplicate));
+    }
+    
+    private void checkResources(final String schemaName, final 
AlterShadowRuleStatement sqlStatement, final ShardingSphereMetaData metaData) 
throws DistSQLException {
+        List<String> requireSourceResources = getPropertiesFrom(sqlStatement, 
RESOURCE);
+        checkDuplicate(requireSourceResources, duplicate -> new 
DuplicateRuleException(SHADOW, schemaName, duplicate));
+        checkResourceExist(sqlStatement.getRules(), metaData, schemaName);
+    }
+    
+    private void checkAlgorithms(final String schemaName, final 
AlterShadowRuleStatement sqlStatement) throws DistSQLException {
+        checkAlgorithmCompleteness(sqlStatement);
+        List<String> requireAlgorithmNames = getPropertiesFrom(sqlStatement, 
ALGORITHM);
+        checkDuplicate(requireAlgorithmNames, duplicate -> new 
AlgorithmInUsedException(schemaName, duplicate));
+    }
+    
+    private void checkAlgorithmCompleteness(final AlterShadowRuleStatement 
sqlStatement) throws DistSQLException {
+        List<ShadowAlgorithmSegment> incompleteAlgorithms = 
getShadowAlgorithmSegment(sqlStatement).flatMap(Collection::stream).filter(each 
-> !each.isComplete()).collect(Collectors.toList());
+        DistSQLException.predictionThrow(incompleteAlgorithms.isEmpty(), new 
InvalidAlgorithmConfigurationException(SHADOW));
+    }
+    
+    private List<String> getPropertiesFrom(final ShadowRuleConfiguration 
currentRuleConfig, final String propName) {

Review comment:
       the method name seems not that good.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to