View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.shardingsphere.sharding.distsql.handler.update;
19  
20  import lombok.Setter;
21  import org.apache.shardingsphere.distsql.handler.engine.update.rdl.rule.spi.database.DatabaseRuleAlterExecutor;
22  import org.apache.shardingsphere.distsql.handler.required.DistSQLExecutorCurrentRuleRequired;
23  import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
24  import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration;
25  import org.apache.shardingsphere.sharding.api.config.rule.ShardingAutoTableRuleConfiguration;
26  import org.apache.shardingsphere.sharding.api.config.rule.ShardingTableRuleConfiguration;
27  import org.apache.shardingsphere.sharding.distsql.handler.checker.ShardingTableRuleStatementChecker;
28  import org.apache.shardingsphere.sharding.distsql.handler.converter.ShardingTableRuleStatementConverter;
29  import org.apache.shardingsphere.sharding.distsql.statement.AlterShardingTableRuleStatement;
30  import org.apache.shardingsphere.sharding.rule.ShardingRule;
31  
32  import java.util.Collection;
33  import java.util.stream.Collectors;
34  
35  /**
36   * Alter sharding table rule executor.
37   */
38  @DistSQLExecutorCurrentRuleRequired(ShardingRule.class)
39  @Setter
40  public final class AlterShardingTableRuleExecutor implements DatabaseRuleAlterExecutor<AlterShardingTableRuleStatement, ShardingRule, ShardingRuleConfiguration> {
41      
42      private ShardingSphereDatabase database;
43      
44      private ShardingRule rule;
45      
46      @Override
47      public void checkBeforeUpdate(final AlterShardingTableRuleStatement sqlStatement) {
48          ShardingTableRuleStatementChecker.checkAlteration(database, sqlStatement.getRules(), rule.getConfiguration());
49          checkUniqueActualDataNodes(sqlStatement);
50      }
51      
52      private void checkUniqueActualDataNodes(final AlterShardingTableRuleStatement sqlStatement) {
53          rule.getShardingRuleChecker().checkToBeAddedDataNodes(ShardingTableRuleStatementConverter.convertDataNodes(sqlStatement.getRules()), true);
54      }
55      
56      @Override
57      public ShardingRuleConfiguration buildToBeAlteredRuleConfiguration(final AlterShardingTableRuleStatement sqlStatement) {
58          return ShardingTableRuleStatementConverter.convert(sqlStatement.getRules());
59      }
60      
61      @Override
62      public ShardingRuleConfiguration buildToBeDroppedRuleConfiguration(final ShardingRuleConfiguration toBeAlteredRuleConfig) {
63          ShardingRuleConfiguration result = new ShardingRuleConfiguration();
64          Collection<String> toBeAlteredShardingTableNames = toBeAlteredRuleConfig.getTables().stream().map(ShardingTableRuleConfiguration::getLogicTable).collect(Collectors.toSet());
65          for (ShardingAutoTableRuleConfiguration each : rule.getConfiguration().getAutoTables()) {
66              if (toBeAlteredShardingTableNames.contains(each.getLogicTable())) {
67                  result.getAutoTables().add(each);
68              }
69          }
70          Collection<String> toBeAlteredAutoTableNames = toBeAlteredRuleConfig.getAutoTables().stream().map(ShardingAutoTableRuleConfiguration::getLogicTable).collect(Collectors.toSet());
71          for (ShardingTableRuleConfiguration each : rule.getConfiguration().getTables()) {
72              if (toBeAlteredAutoTableNames.contains(each.getLogicTable())) {
73                  result.getTables().add(each);
74              }
75          }
76          UnusedAlgorithmFinder.findUnusedShardingAlgorithm(rule.getConfiguration()).forEach(each -> result.getShardingAlgorithms().put(each, rule.getConfiguration().getShardingAlgorithms().get(each)));
77          UnusedAlgorithmFinder.findUnusedKeyGenerator(rule.getConfiguration()).forEach(each -> result.getKeyGenerators().put(each, rule.getConfiguration().getKeyGenerators().get(each)));
78          UnusedAlgorithmFinder.findUnusedAuditor(rule.getConfiguration()).forEach(each -> result.getAuditors().put(each, rule.getConfiguration().getAuditors().get(each)));
79          return result;
80      }
81      
82      @Override
83      public Class<ShardingRule> getRuleClass() {
84          return ShardingRule.class;
85      }
86      
87      @Override
88      public Class<AlterShardingTableRuleStatement> getType() {
89          return AlterShardingTableRuleStatement.class;
90      }
91  }