This is an automated email from the ASF dual-hosted git repository.

panjuan 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 23824b3  fix create sharding table rule error (#10698)
23824b3 is described below

commit 23824b3339942afa1a1f22dede3ff09004c7aa07
Author: Zhengqiang Duan <[email protected]>
AuthorDate: Mon Jun 7 16:16:22 2021 +0800

    fix create sharding table rule error (#10698)
    
    * fix create sharding table rule error
    
    * fix test case
---
 .../shardingsphere/sharding/rule/ShardingRule.java | 14 ++++++++---
 .../sharding/rule/ShardingRuleTest.java            | 28 ++++++++++++++++++++++
 2 files changed, 39 insertions(+), 3 deletions(-)

diff --git 
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/rule/ShardingRule.java
 
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/rule/ShardingRule.java
index 905e3dc..fab1c73 100644
--- 
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/rule/ShardingRule.java
+++ 
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/rule/ShardingRule.java
@@ -20,6 +20,7 @@ package org.apache.shardingsphere.sharding.rule;
 import com.google.common.base.Function;
 import com.google.common.base.Preconditions;
 import com.google.common.base.Splitter;
+import com.google.common.base.Strings;
 import com.google.common.eventbus.Subscribe;
 import lombok.Getter;
 import 
org.apache.shardingsphere.infra.config.algorithm.ShardingSphereAlgorithmFactory;
@@ -98,7 +99,7 @@ public final class ShardingRule implements FeatureRule, 
SchemaRule, DataNodeCont
     public ShardingRule(final ShardingRuleConfiguration config, final 
DatabaseType databaseType, final Map<String, DataSource> dataSourceMap) {
         Preconditions.checkArgument(null != config, "Sharding rule 
configuration cannot be null.");
         Preconditions.checkArgument(null != dataSourceMap && 
!dataSourceMap.isEmpty(), "Data sources cannot be empty.");
-        dataSourceNames = getDataSourceNames(config.getTables(), 
dataSourceMap.keySet());
+        dataSourceNames = getDataSourceNames(config.getTables(), 
config.getAutoTables(), dataSourceMap.keySet());
         config.getShardingAlgorithms().forEach((key, value) -> 
shardingAlgorithms.put(key, 
ShardingSphereAlgorithmFactory.createAlgorithm(value, 
ShardingAlgorithm.class)));
         config.getKeyGenerators().forEach((key, value) -> 
keyGenerators.put(key, ShardingSphereAlgorithmFactory.createAlgorithm(value, 
KeyGenerateAlgorithm.class)));
         tableRules = new LinkedList<>(createTableRules(config.getTables(), 
config.getDefaultKeyGenerateStrategy()));
@@ -116,7 +117,7 @@ public final class ShardingRule implements FeatureRule, 
SchemaRule, DataNodeCont
     public ShardingRule(final AlgorithmProvidedShardingRuleConfiguration 
config, final DatabaseType databaseType, final Map<String, DataSource> 
dataSourceMap) {
         Preconditions.checkArgument(null != config, "Sharding rule 
configuration cannot be null.");
         Preconditions.checkArgument(null != dataSourceMap && 
!dataSourceMap.isEmpty(), "Data sources cannot be empty.");
-        dataSourceNames = getDataSourceNames(config.getTables(), 
dataSourceMap.keySet());
+        dataSourceNames = getDataSourceNames(config.getTables(), 
config.getAutoTables(), dataSourceMap.keySet());
         shardingAlgorithms.putAll(config.getShardingAlgorithms());
         keyGenerators.putAll(config.getKeyGenerators());
         tableRules = new LinkedList<>(createTableRules(config.getTables(), 
config.getDefaultKeyGenerateStrategy()));
@@ -130,7 +131,8 @@ public final class ShardingRule implements FeatureRule, 
SchemaRule, DataNodeCont
                 ? 
TypedSPIRegistry.getRegisteredService(KeyGenerateAlgorithm.class) : 
keyGenerators.get(config.getDefaultKeyGenerateStrategy().getKeyGeneratorName());
     }
     
-    private Collection<String> getDataSourceNames(final 
Collection<ShardingTableRuleConfiguration> tableRuleConfigs, final 
Collection<String> dataSourceNames) {
+    private Collection<String> getDataSourceNames(final 
Collection<ShardingTableRuleConfiguration> tableRuleConfigs, 
+                                                  final 
Collection<ShardingAutoTableRuleConfiguration> autoTableRuleConfigs, final 
Collection<String> dataSourceNames) {
         if (tableRuleConfigs.isEmpty()) {
             return dataSourceNames;
         }
@@ -139,9 +141,15 @@ public final class ShardingRule implements FeatureRule, 
SchemaRule, DataNodeCont
         }
         Collection<String> result = new LinkedHashSet<>();
         tableRuleConfigs.forEach(each -> 
result.addAll(getDataSourceNames(each)));
+        autoTableRuleConfigs.forEach(each -> 
result.addAll(getDataSourceNames(each)));
         return result;
     }
     
+    private Collection<String> getDataSourceNames(final 
ShardingAutoTableRuleConfiguration shardingAutoTableRuleConfig) {
+        return 
Strings.isNullOrEmpty(shardingAutoTableRuleConfig.getActualDataSources()) 
+                ? Collections.emptyList() : 
Splitter.on(",").trimResults().splitToList(shardingAutoTableRuleConfig.getActualDataSources());
+    }
+    
     private Collection<String> getDataSourceNames(final 
ShardingTableRuleConfiguration shardingTableRuleConfig) {
         List<String> actualDataNodes = new 
InlineExpressionParser(shardingTableRuleConfig.getActualDataNodes()).splitAndEvaluate();
         return actualDataNodes.stream().map(each -> new 
DataNode(each).getDataSourceName()).collect(Collectors.toList());
diff --git 
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/rule/ShardingRuleTest.java
 
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/rule/ShardingRuleTest.java
index 4c2b2c2..fa09aeb 100644
--- 
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/rule/ShardingRuleTest.java
+++ 
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/rule/ShardingRuleTest.java
@@ -25,6 +25,7 @@ import 
org.apache.shardingsphere.sharding.algorithm.keygen.SnowflakeKeyGenerateA
 import 
org.apache.shardingsphere.sharding.algorithm.keygen.fixture.IncrementKeyGenerateAlgorithm;
 import 
org.apache.shardingsphere.sharding.algorithm.sharding.inline.InlineShardingAlgorithm;
 import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration;
+import 
org.apache.shardingsphere.sharding.api.config.rule.ShardingAutoTableRuleConfiguration;
 import 
org.apache.shardingsphere.sharding.api.config.rule.ShardingTableRuleConfiguration;
 import 
org.apache.shardingsphere.sharding.api.config.strategy.keygen.KeyGenerateStrategyConfiguration;
 import 
org.apache.shardingsphere.sharding.api.config.strategy.sharding.NoneShardingStrategyConfiguration;
@@ -36,6 +37,7 @@ import javax.sql.DataSource;
 import java.util.Arrays;
 import java.util.Collections;
 import java.util.HashMap;
+import java.util.LinkedHashSet;
 import java.util.Map;
 import java.util.Properties;
 
@@ -323,6 +325,30 @@ public final class ShardingRuleTest {
         assertThat(shardingRule.getAllDataNodes().size(), is(3));
     }
     
+    @Test
+    public void assertGetDataSourceNamesWithShardingAutoTables() {
+        ShardingRuleConfiguration shardingRuleConfig = new 
ShardingRuleConfiguration();
+        ShardingTableRuleConfiguration tableRuleConfig = new 
ShardingTableRuleConfiguration("logic_table", "ds_${0..1}.table_${0..2}");
+        shardingRuleConfig.getTables().add(tableRuleConfig);
+        ShardingAutoTableRuleConfiguration autoTableRuleConfig = new 
ShardingAutoTableRuleConfiguration("auto_table", "resource0, resource1");
+        autoTableRuleConfig.setShardingStrategy(new 
StandardShardingStrategyConfiguration("order_id", "hash_mod"));
+        shardingRuleConfig.getAutoTables().add(autoTableRuleConfig);
+        Properties props = new Properties();
+        props.put("sharding-count", 4);
+        shardingRuleConfig.getShardingAlgorithms().put("hash_mod", new 
ShardingSphereAlgorithmConfiguration("hash_mod", props));
+        ShardingRule shardingRule = new ShardingRule(shardingRuleConfig, 
mock(DatabaseType.class), createDataSourceMap());
+        assertThat(shardingRule.getDataSourceNames(), is(new 
LinkedHashSet<>(Arrays.asList("ds_0", "ds_1", "resource0", "resource1"))));
+    }
+    
+    @Test
+    public void assertGetDataSourceNamesWithoutShardingAutoTables() {
+        ShardingRuleConfiguration shardingRuleConfig = new 
ShardingRuleConfiguration();
+        ShardingTableRuleConfiguration shardingTableRuleConfig = new 
ShardingTableRuleConfiguration("LOGIC_TABLE", "ds_${0..1}.table_${0..2}");
+        shardingRuleConfig.getTables().add(shardingTableRuleConfig);
+        ShardingRule shardingRule = new ShardingRule(shardingRuleConfig, 
mock(DatabaseType.class), createDataSourceMap());
+        assertThat(shardingRule.getDataSourceNames(), is(new 
LinkedHashSet<>(Arrays.asList("ds_0", "ds_1"))));
+    }
+    
     private ShardingRule createMaximumShardingRule() {
         ShardingRuleConfiguration shardingRuleConfig = new 
ShardingRuleConfiguration();
         ShardingTableRuleConfiguration shardingTableRuleConfig = 
createTableRuleConfiguration("LOGIC_TABLE", "ds_${0..1}.table_${0..2}");
@@ -364,6 +390,8 @@ public final class ShardingRuleTest {
         Map<String, DataSource> result = new HashMap<>(2, 1);
         result.put("ds_0", mock(DataSource.class, RETURNS_DEEP_STUBS));
         result.put("ds_1", mock(DataSource.class, RETURNS_DEEP_STUBS));
+        result.put("resource0", mock(DataSource.class, RETURNS_DEEP_STUBS));
+        result.put("resource1", mock(DataSource.class, RETURNS_DEEP_STUBS));
         return result;
     }
     

Reply via email to