This is an automated email from the ASF dual-hosted git repository.
wuweijie 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 53125f6 Fix bug of getAllDataNodes (#8958)
53125f6 is described below
commit 53125f68a1b04822c3ceaeeeac0b4e0d6eacde1a
Author: Juan Pan(Trista) <[email protected]>
AuthorDate: Sun Jan 10 12:56:13 2021 +0800
Fix bug of getAllDataNodes (#8958)
* Fix bug of getAllDataNodes
* add ut
---
.../org/apache/shardingsphere/sharding/rule/ShardingRule.java | 6 +++++-
.../org/apache/shardingsphere/sharding/rule/ShardingRuleTest.java | 8 ++++++++
2 files changed, 13 insertions(+), 1 deletion(-)
diff --git
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/main/java/org/apache/shardingsphere/sharding/rule/ShardingRule.java
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/main/java/org/apache/shardingsphere/sharding/rule/ShardingRule.java
index bacc9e6..302bb9e 100644
---
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/main/java/org/apache/shardingsphere/sharding/rule/ShardingRule.java
+++
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/main/java/org/apache/shardingsphere/sharding/rule/ShardingRule.java
@@ -52,6 +52,7 @@ import
org.apache.shardingsphere.sharding.spi.ShardingAlgorithm;
import javax.sql.DataSource;
import java.util.Collection;
+import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
@@ -447,7 +448,10 @@ public final class ShardingRule implements
DataNodeContainedRule, TableContained
@Override
public Map<String, Collection<DataNode>> getAllDataNodes() {
- return
tableRules.stream().collect(Collectors.toMap(TableRule::getLogicTable,
TableRule::getActualDataNodes, (oldValue, currentValue) -> oldValue,
LinkedHashMap::new));
+ Map<String, Collection<DataNode>> result = new LinkedHashMap<>();
+ singleTableRules.forEach((key, value) -> result.put(key,
Collections.singleton(new DataNode(value.getDataSourceName(),
value.getTableName()))));
+
result.putAll(tableRules.stream().collect(Collectors.toMap(TableRule::getLogicTable,
TableRule::getActualDataNodes, (oldValue, currentValue) -> oldValue,
LinkedHashMap::new)));
+ return result;
}
@Override
diff --git
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/test/java/org/apache/shardingsphere/sharding/rule/ShardingRuleTest.java
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/test/java/org/apache/shardingsphere/sharding/rule/ShardingRuleTest.java
index 545cff9..9574a98 100644
---
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/test/java/org/apache/shardingsphere/sharding/rule/ShardingRuleTest.java
+++
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/test/java/org/apache/shardingsphere/sharding/rule/ShardingRuleTest.java
@@ -29,6 +29,7 @@ import
org.apache.shardingsphere.sharding.api.config.rule.ShardingTableRuleConfi
import
org.apache.shardingsphere.sharding.api.config.strategy.keygen.KeyGenerateStrategyConfiguration;
import
org.apache.shardingsphere.sharding.api.config.strategy.sharding.NoneShardingStrategyConfiguration;
import
org.apache.shardingsphere.sharding.api.config.strategy.sharding.StandardShardingStrategyConfiguration;
+import org.apache.shardingsphere.sharding.rule.single.SingleTableRule;
import org.junit.Test;
import javax.sql.DataSource;
@@ -305,6 +306,13 @@ public final class ShardingRuleTest {
assertThat(createMaximumShardingRule().getTables(),
is(Arrays.asList("logic_table", "sub_logic_table")));
}
+ @Test
+ public void assertGetAllDataNodesWithSingleTables() {
+ ShardingRule shardingRule = createMaximumShardingRule();
+ shardingRule.getSingleTableRules().put("single", new
SingleTableRule("single", "ds_0"));
+ assertThat(shardingRule.getAllDataNodes().size(), is(3));
+ }
+
private ShardingRule createMaximumShardingRule() {
ShardingRuleConfiguration shardingRuleConfig = new
ShardingRuleConfiguration();
ShardingTableRuleConfiguration shardingTableRuleConfig =
createTableRuleConfiguration("LOGIC_TABLE", "ds_${0..1}.table_${0..2}");