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.yaml.swapper;
19  
20  import org.apache.shardingsphere.infra.algorithm.core.yaml.YamlAlgorithmConfigurationSwapper;
21  import org.apache.shardingsphere.infra.yaml.config.swapper.rule.YamlRuleConfigurationSwapper;
22  import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration;
23  import org.apache.shardingsphere.sharding.constant.ShardingOrder;
24  import org.apache.shardingsphere.sharding.yaml.config.YamlShardingRuleConfiguration;
25  import org.apache.shardingsphere.sharding.yaml.config.rule.YamlShardingAutoTableRuleConfiguration;
26  import org.apache.shardingsphere.sharding.yaml.config.rule.YamlTableRuleConfiguration;
27  import org.apache.shardingsphere.sharding.yaml.swapper.cache.YamlShardingCacheConfigurationSwapper;
28  import org.apache.shardingsphere.sharding.yaml.swapper.rule.YamlShardingAutoTableRuleConfigurationSwapper;
29  import org.apache.shardingsphere.sharding.yaml.swapper.rule.YamlShardingTableReferenceRuleConfigurationConverter;
30  import org.apache.shardingsphere.sharding.yaml.swapper.rule.YamlShardingTableRuleConfigurationSwapper;
31  import org.apache.shardingsphere.sharding.yaml.swapper.strategy.YamlKeyGenerateStrategyConfigurationSwapper;
32  import org.apache.shardingsphere.sharding.yaml.swapper.strategy.YamlShardingAuditStrategyConfigurationSwapper;
33  import org.apache.shardingsphere.sharding.yaml.swapper.strategy.YamlShardingStrategyConfigurationSwapper;
34  
35  import java.util.Map.Entry;
36  import java.util.stream.Collectors;
37  
38  /**
39   * YAML sharding rule configuration swapper.
40   */
41  public final class YamlShardingRuleConfigurationSwapper implements YamlRuleConfigurationSwapper<YamlShardingRuleConfiguration, ShardingRuleConfiguration> {
42      
43      private final YamlShardingTableRuleConfigurationSwapper tableSwapper = new YamlShardingTableRuleConfigurationSwapper();
44      
45      private final YamlShardingStrategyConfigurationSwapper shardingStrategySwapper = new YamlShardingStrategyConfigurationSwapper();
46      
47      private final YamlKeyGenerateStrategyConfigurationSwapper keyGenerateStrategySwapper = new YamlKeyGenerateStrategyConfigurationSwapper();
48      
49      private final YamlAlgorithmConfigurationSwapper algorithmSwapper = new YamlAlgorithmConfigurationSwapper();
50      
51      private final YamlShardingAuditStrategyConfigurationSwapper auditStrategySwapper = new YamlShardingAuditStrategyConfigurationSwapper();
52      
53      private final YamlShardingAutoTableRuleConfigurationSwapper autoTableSwapper = new YamlShardingAutoTableRuleConfigurationSwapper();
54      
55      private final YamlShardingCacheConfigurationSwapper shardingCacheSwapper = new YamlShardingCacheConfigurationSwapper();
56      
57      @Override
58      public YamlShardingRuleConfiguration swapToYamlConfiguration(final ShardingRuleConfiguration data) {
59          YamlShardingRuleConfiguration result = new YamlShardingRuleConfiguration();
60          data.getTables().forEach(each -> result.getTables().put(each.getLogicTable(), tableSwapper.swapToYamlConfiguration(each)));
61          data.getAutoTables().forEach(each -> result.getAutoTables().put(each.getLogicTable(), autoTableSwapper.swapToYamlConfiguration(each)));
62          result.getBindingTables().addAll(data.getBindingTableGroups().stream().map(YamlShardingTableReferenceRuleConfigurationConverter::convertToYamlString).collect(Collectors.toList()));
63          setYamlStrategies(data, result);
64          setYamlAlgorithms(data, result);
65          result.setDefaultShardingColumn(data.getDefaultShardingColumn());
66          if (null != data.getShardingCache()) {
67              result.setShardingCache(shardingCacheSwapper.swapToYamlConfiguration(data.getShardingCache()));
68          }
69          return result;
70      }
71      
72      private void setYamlStrategies(final ShardingRuleConfiguration data, final YamlShardingRuleConfiguration yamlConfig) {
73          if (null != data.getDefaultDatabaseShardingStrategy()) {
74              yamlConfig.setDefaultDatabaseStrategy(shardingStrategySwapper.swapToYamlConfiguration(data.getDefaultDatabaseShardingStrategy()));
75          }
76          if (null != data.getDefaultTableShardingStrategy()) {
77              yamlConfig.setDefaultTableStrategy(shardingStrategySwapper.swapToYamlConfiguration(data.getDefaultTableShardingStrategy()));
78          }
79          if (null != data.getDefaultKeyGenerateStrategy()) {
80              yamlConfig.setDefaultKeyGenerateStrategy(keyGenerateStrategySwapper.swapToYamlConfiguration(data.getDefaultKeyGenerateStrategy()));
81          }
82          if (null != data.getDefaultAuditStrategy()) {
83              yamlConfig.setDefaultAuditStrategy(auditStrategySwapper.swapToYamlConfiguration(data.getDefaultAuditStrategy()));
84          }
85      }
86      
87      private void setYamlAlgorithms(final ShardingRuleConfiguration data, final YamlShardingRuleConfiguration yamlConfig) {
88          if (null != data.getShardingAlgorithms()) {
89              data.getShardingAlgorithms().forEach((key, value) -> yamlConfig.getShardingAlgorithms().put(key, algorithmSwapper.swapToYamlConfiguration(value)));
90          }
91          if (null != data.getKeyGenerators()) {
92              data.getKeyGenerators().forEach((key, value) -> yamlConfig.getKeyGenerators().put(key, algorithmSwapper.swapToYamlConfiguration(value)));
93          }
94          if (null != data.getAuditors()) {
95              data.getAuditors().forEach((key, value) -> yamlConfig.getAuditors().put(key, algorithmSwapper.swapToYamlConfiguration(value)));
96          }
97      }
98      
99      @Override
100     public ShardingRuleConfiguration swapToObject(final YamlShardingRuleConfiguration yamlConfig) {
101         ShardingRuleConfiguration result = new ShardingRuleConfiguration();
102         for (Entry<String, YamlTableRuleConfiguration> entry : yamlConfig.getTables().entrySet()) {
103             YamlTableRuleConfiguration tableRuleConfig = entry.getValue();
104             tableRuleConfig.setLogicTable(entry.getKey());
105             result.getTables().add(tableSwapper.swapToObject(tableRuleConfig));
106         }
107         for (Entry<String, YamlShardingAutoTableRuleConfiguration> entry : yamlConfig.getAutoTables().entrySet()) {
108             YamlShardingAutoTableRuleConfiguration tableRuleConfig = entry.getValue();
109             tableRuleConfig.setLogicTable(entry.getKey());
110             result.getAutoTables().add(autoTableSwapper.swapToObject(tableRuleConfig));
111         }
112         result.getBindingTableGroups().addAll(yamlConfig.getBindingTables().stream().map(YamlShardingTableReferenceRuleConfigurationConverter::convertToObject).collect(Collectors.toList()));
113         setStrategies(yamlConfig, result);
114         setAlgorithms(yamlConfig, result);
115         result.setDefaultShardingColumn(yamlConfig.getDefaultShardingColumn());
116         if (null != yamlConfig.getShardingCache()) {
117             result.setShardingCache(shardingCacheSwapper.swapToObject(yamlConfig.getShardingCache()));
118         }
119         return result;
120     }
121     
122     private void setStrategies(final YamlShardingRuleConfiguration yamlConfig, final ShardingRuleConfiguration ruleConfig) {
123         if (null != yamlConfig.getDefaultDatabaseStrategy()) {
124             ruleConfig.setDefaultDatabaseShardingStrategy(shardingStrategySwapper.swapToObject(yamlConfig.getDefaultDatabaseStrategy()));
125         }
126         if (null != yamlConfig.getDefaultTableStrategy()) {
127             ruleConfig.setDefaultTableShardingStrategy(shardingStrategySwapper.swapToObject(yamlConfig.getDefaultTableStrategy()));
128         }
129         if (null != yamlConfig.getDefaultKeyGenerateStrategy()) {
130             ruleConfig.setDefaultKeyGenerateStrategy(keyGenerateStrategySwapper.swapToObject(yamlConfig.getDefaultKeyGenerateStrategy()));
131         }
132         if (null != yamlConfig.getDefaultAuditStrategy()) {
133             ruleConfig.setDefaultAuditStrategy(auditStrategySwapper.swapToObject(yamlConfig.getDefaultAuditStrategy()));
134         }
135     }
136     
137     private void setAlgorithms(final YamlShardingRuleConfiguration yamlConfig, final ShardingRuleConfiguration ruleConfig) {
138         if (null != yamlConfig.getShardingAlgorithms()) {
139             yamlConfig.getShardingAlgorithms().forEach((key, value) -> ruleConfig.getShardingAlgorithms().put(key, algorithmSwapper.swapToObject(value)));
140         }
141         if (null != yamlConfig.getKeyGenerators()) {
142             yamlConfig.getKeyGenerators().forEach((key, value) -> ruleConfig.getKeyGenerators().put(key, algorithmSwapper.swapToObject(value)));
143         }
144         if (null != yamlConfig.getAuditors()) {
145             yamlConfig.getAuditors().forEach((key, value) -> ruleConfig.getAuditors().put(key, algorithmSwapper.swapToObject(value)));
146         }
147     }
148     
149     @Override
150     public Class<ShardingRuleConfiguration> getTypeClass() {
151         return ShardingRuleConfiguration.class;
152     }
153     
154     @Override
155     public String getRuleTagName() {
156         return "SHARDING";
157     }
158     
159     @Override
160     public int getOrder() {
161         return ShardingOrder.ORDER;
162     }
163 }