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.rule.changed;
19  
20  import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
21  import org.apache.shardingsphere.infra.rule.event.rule.alter.AlterRuleItemEvent;
22  import org.apache.shardingsphere.infra.rule.event.rule.drop.DropRuleItemEvent;
23  import org.apache.shardingsphere.infra.util.yaml.YamlEngine;
24  import org.apache.shardingsphere.mode.spi.RuleItemConfigurationChangedProcessor;
25  import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration;
26  import org.apache.shardingsphere.sharding.api.config.strategy.sharding.ShardingStrategyConfiguration;
27  import org.apache.shardingsphere.sharding.metadata.nodepath.ShardingRuleNodePathProvider;
28  import org.apache.shardingsphere.sharding.rule.ShardingRule;
29  import org.apache.shardingsphere.sharding.yaml.config.strategy.sharding.YamlShardingStrategyConfiguration;
30  import org.apache.shardingsphere.sharding.yaml.swapper.strategy.YamlShardingStrategyConfigurationSwapper;
31  
32  /**
33   * Default table sharding strategy changed processor.
34   */
35  public final class DefaultTableShardingStrategyChangedProcessor implements RuleItemConfigurationChangedProcessor<ShardingRuleConfiguration, ShardingStrategyConfiguration> {
36      
37      @Override
38      public ShardingStrategyConfiguration swapRuleItemConfiguration(final AlterRuleItemEvent event, final String yamlContent) {
39          return new YamlShardingStrategyConfigurationSwapper().swapToObject(YamlEngine.unmarshal(yamlContent, YamlShardingStrategyConfiguration.class));
40      }
41      
42      @Override
43      public ShardingRuleConfiguration findRuleConfiguration(final ShardingSphereDatabase database) {
44          return database.getRuleMetaData().findSingleRule(ShardingRule.class).map(ShardingRule::getConfiguration).orElseGet(ShardingRuleConfiguration::new);
45      }
46      
47      @Override
48      public void changeRuleItemConfiguration(final AlterRuleItemEvent event, final ShardingRuleConfiguration currentRuleConfig, final ShardingStrategyConfiguration toBeChangedItemConfig) {
49          currentRuleConfig.setDefaultTableShardingStrategy(toBeChangedItemConfig);
50      }
51      
52      @Override
53      public void dropRuleItemConfiguration(final DropRuleItemEvent event, final ShardingRuleConfiguration currentRuleConfig) {
54          currentRuleConfig.setDefaultTableShardingStrategy(null);
55      }
56      
57      @Override
58      public String getType() {
59          return ShardingRuleNodePathProvider.RULE_TYPE + "." + ShardingRuleNodePathProvider.DEFAULT_TABLE_STRATEGY;
60      }
61  }