This is an automated email from the ASF dual-hosted git repository.
sunnianjun 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 da6ceca55d3 Refactor ShardingTableMapperRule (#30324)
da6ceca55d3 is described below
commit da6ceca55d360af5c1ff1589dc91967ec770df09
Author: Liang Zhang <[email protected]>
AuthorDate: Wed Feb 28 07:48:07 2024 +0800
Refactor ShardingTableMapperRule (#30324)
* Refactor ShardingTableMapperRule
* Refactor SingleTableMapperRuleTest
---
.../apache/shardingsphere/sharding/rule/ShardingRule.java | 2 +-
.../sharding/rule/ShardingTableMapperRule.java | 12 ++++++------
.../sharding/rule/ShardingTableMapperRuleTest.java | 2 +-
.../org/apache/shardingsphere/single/rule/SingleRule.java | 2 +-
.../shardingsphere/single/rule/SingleTableMapperRule.java | 5 ++---
.../single/rule/SingleTableMapperRuleTest.java | 2 +-
6 files changed, 12 insertions(+), 13 deletions(-)
diff --git
a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/rule/ShardingRule.java
b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/rule/ShardingRule.java
index a177dd2bc0d..2079c9de729 100644
---
a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/rule/ShardingRule.java
+++
b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/rule/ShardingRule.java
@@ -146,7 +146,7 @@ public final class ShardingRule implements DatabaseRule,
DataNodeContainedRule,
}
shardingCache = null == ruleConfig.getShardingCache() ? null : new
ShardingCache(ruleConfig.getShardingCache(), this);
dataNodeRule = new ShardingDataNodeRule(shardingTables);
- tableMapperRule = new ShardingTableMapperRule(shardingTables);
+ tableMapperRule = new ShardingTableMapperRule(shardingTables.values());
}
private void validateUniqueActualDataNodesInTableRules() {
diff --git
a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/rule/ShardingTableMapperRule.java
b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/rule/ShardingTableMapperRule.java
index dc967616858..63054b4d386 100644
---
a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/rule/ShardingTableMapperRule.java
+++
b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/rule/ShardingTableMapperRule.java
@@ -21,7 +21,7 @@ import org.apache.shardingsphere.infra.datanode.DataNode;
import
org.apache.shardingsphere.infra.rule.identifier.type.table.TableMapperRule;
import
org.apache.shardingsphere.infra.rule.identifier.type.table.TableNamesMapper;
-import java.util.Map;
+import java.util.Collection;
/**
* Sharding table mapper rule.
@@ -32,20 +32,20 @@ public final class ShardingTableMapperRule implements
TableMapperRule {
private final TableNamesMapper actualTableMapper;
- public ShardingTableMapperRule(final Map<String, ShardingTable>
shardingTables) {
+ public ShardingTableMapperRule(final Collection<ShardingTable>
shardingTables) {
logicalTableMapper = createLogicalTableMapper(shardingTables);
actualTableMapper = createActualTableMapper(shardingTables);
}
- private TableNamesMapper createLogicalTableMapper(final Map<String,
ShardingTable> shardingTables) {
+ private TableNamesMapper createLogicalTableMapper(final
Collection<ShardingTable> shardingTables) {
TableNamesMapper result = new TableNamesMapper();
- shardingTables.values().forEach(each ->
result.put(each.getLogicTable()));
+ shardingTables.forEach(each -> result.put(each.getLogicTable()));
return result;
}
- private TableNamesMapper createActualTableMapper(final Map<String,
ShardingTable> shardingTables) {
+ private TableNamesMapper createActualTableMapper(final
Collection<ShardingTable> shardingTables) {
TableNamesMapper result = new TableNamesMapper();
- shardingTables.values().stream().flatMap(each ->
each.getActualDataNodes().stream()).map(DataNode::getTableName).forEach(result::put);
+ shardingTables.stream().flatMap(each ->
each.getActualDataNodes().stream()).map(DataNode::getTableName).forEach(result::put);
return result;
}
diff --git
a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/rule/ShardingTableMapperRuleTest.java
b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/rule/ShardingTableMapperRuleTest.java
index 3aaf346e0f5..0303cf197c6 100644
---
a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/rule/ShardingTableMapperRuleTest.java
+++
b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/rule/ShardingTableMapperRuleTest.java
@@ -38,7 +38,7 @@ class ShardingTableMapperRuleTest {
ShardingTable shardingTable = mock(ShardingTable.class);
when(shardingTable.getLogicTable()).thenReturn("foo_tbl");
when(shardingTable.getActualDataNodes()).thenReturn(Collections.singletonList(new
DataNode("foo_ds.foo_tbl_0")));
- tableMapperRule = new
ShardingTableMapperRule(Collections.singletonMap("foo_tbl", shardingTable));
+ tableMapperRule = new
ShardingTableMapperRule(Collections.singleton(shardingTable));
}
@Test
diff --git
a/kernel/single/core/src/main/java/org/apache/shardingsphere/single/rule/SingleRule.java
b/kernel/single/core/src/main/java/org/apache/shardingsphere/single/rule/SingleRule.java
index 115146d8247..c61f9238597 100644
---
a/kernel/single/core/src/main/java/org/apache/shardingsphere/single/rule/SingleRule.java
+++
b/kernel/single/core/src/main/java/org/apache/shardingsphere/single/rule/SingleRule.java
@@ -85,7 +85,7 @@ public final class SingleRule implements DatabaseRule,
DataNodeContainedRule, Ta
this.protocolType = protocolType;
singleTableDataNodes = SingleTableDataNodeLoader.load(databaseName,
protocolType, aggregateDataSourceMap, builtRules, configuration.getTables());
dataNodeRule = new SingleDataNodeRule(singleTableDataNodes);
- tableMapperRule = new SingleTableMapperRule(singleTableDataNodes);
+ tableMapperRule = new
SingleTableMapperRule(singleTableDataNodes.values());
}
/**
diff --git
a/kernel/single/core/src/main/java/org/apache/shardingsphere/single/rule/SingleTableMapperRule.java
b/kernel/single/core/src/main/java/org/apache/shardingsphere/single/rule/SingleTableMapperRule.java
index a1f1ba390fb..103e66ad2f8 100644
---
a/kernel/single/core/src/main/java/org/apache/shardingsphere/single/rule/SingleTableMapperRule.java
+++
b/kernel/single/core/src/main/java/org/apache/shardingsphere/single/rule/SingleTableMapperRule.java
@@ -22,7 +22,6 @@ import
org.apache.shardingsphere.infra.rule.identifier.type.table.TableMapperRul
import
org.apache.shardingsphere.infra.rule.identifier.type.table.TableNamesMapper;
import java.util.Collection;
-import java.util.Map;
/**
* Single table mapper rule.
@@ -31,9 +30,9 @@ public final class SingleTableMapperRule implements
TableMapperRule {
private final TableNamesMapper logicTableMapper;
- public SingleTableMapperRule(final Map<String, Collection<DataNode>>
singleTableDataNodes) {
+ public SingleTableMapperRule(final Collection<Collection<DataNode>>
singleTableDataNodes) {
logicTableMapper = new TableNamesMapper();
- singleTableDataNodes.forEach((key, value) ->
logicTableMapper.put(value.iterator().next().getTableName()));
+ singleTableDataNodes.forEach(each ->
logicTableMapper.put(each.iterator().next().getTableName()));
}
@Override
diff --git
a/kernel/single/core/src/test/java/org/apache/shardingsphere/single/rule/SingleTableMapperRuleTest.java
b/kernel/single/core/src/test/java/org/apache/shardingsphere/single/rule/SingleTableMapperRuleTest.java
index cbd5fe2d642..662216387ae 100644
---
a/kernel/single/core/src/test/java/org/apache/shardingsphere/single/rule/SingleTableMapperRuleTest.java
+++
b/kernel/single/core/src/test/java/org/apache/shardingsphere/single/rule/SingleTableMapperRuleTest.java
@@ -28,7 +28,7 @@ import static org.hamcrest.MatcherAssert.assertThat;
class SingleTableMapperRuleTest {
- private final SingleTableMapperRule tableMapperRule = new
SingleTableMapperRule(Collections.singletonMap("foo_tbl",
Collections.singleton(new DataNode("foo_ds.foo_tbl"))));
+ private final SingleTableMapperRule tableMapperRule = new
SingleTableMapperRule(Collections.singleton(Collections.singleton(new
DataNode("foo_ds.foo_tbl"))));
@Test
void assertGetLogicTableMapper() {