Skip to content

Commit a75a02a

Browse files
authored
Fix InUsedShardingStorageUnitRetriever (#35197)
1 parent c741759 commit a75a02a

File tree

2 files changed

+16
-17
lines changed

2 files changed

+16
-17
lines changed

Diff for: features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/query/InUsedShardingStorageUnitRetriever.java

+8-9
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@
1717

1818
package org.apache.shardingsphere.sharding.distsql.handler.query;
1919

20+
import com.cedarsoftware.util.CaseInsensitiveSet;
2021
import org.apache.shardingsphere.distsql.handler.executor.rql.resource.InUsedStorageUnitRetriever;
2122
import org.apache.shardingsphere.distsql.statement.rql.rule.database.ShowRulesUsedStorageUnitStatement;
22-
import org.apache.shardingsphere.sharding.api.config.rule.ShardingAutoTableRuleConfiguration;
23-
import org.apache.shardingsphere.sharding.api.config.rule.ShardingTableRuleConfiguration;
2423
import org.apache.shardingsphere.sharding.rule.ShardingRule;
24+
import org.apache.shardingsphere.sharding.rule.ShardingTable;
2525

2626
import java.util.Collection;
27-
import java.util.LinkedList;
27+
import java.util.HashSet;
2828

2929
/**
3030
* In used sharding storage unit retriever.
@@ -33,12 +33,11 @@ public final class InUsedShardingStorageUnitRetriever implements InUsedStorageUn
3333

3434
@Override
3535
public Collection<String> getInUsedResources(final ShowRulesUsedStorageUnitStatement sqlStatement, final ShardingRule rule) {
36-
Collection<String> result = new LinkedList<>();
37-
for (ShardingAutoTableRuleConfiguration each : rule.getConfiguration().getAutoTables()) {
38-
result.add(each.getLogicTable());
39-
}
40-
for (ShardingTableRuleConfiguration each : rule.getConfiguration().getTables()) {
41-
result.add(each.getLogicTable());
36+
Collection<String> result = new HashSet<>(rule.getShardingTables().size(), 1F);
37+
for (ShardingTable each : rule.getShardingTables().values()) {
38+
if (new CaseInsensitiveSet<>(each.getActualDataSourceNames()).contains(sqlStatement.getStorageUnitName())) {
39+
result.add(each.getLogicTable());
40+
}
4241
}
4342
return result;
4443
}

Diff for: features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/handler/query/InUsedShardingStorageUnitRetrieverTest.java

+8-8
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,15 @@
2020
import org.apache.shardingsphere.distsql.handler.executor.rql.resource.InUsedStorageUnitRetriever;
2121
import org.apache.shardingsphere.distsql.statement.rql.rule.database.ShowRulesUsedStorageUnitStatement;
2222
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
23-
import org.apache.shardingsphere.sharding.api.config.rule.ShardingAutoTableRuleConfiguration;
24-
import org.apache.shardingsphere.sharding.api.config.rule.ShardingTableRuleConfiguration;
2523
import org.apache.shardingsphere.sharding.rule.ShardingRule;
24+
import org.apache.shardingsphere.sharding.rule.ShardingTable;
2625
import org.junit.jupiter.api.Test;
2726

2827
import java.util.Arrays;
2928
import java.util.Collections;
3029

3130
import static org.hamcrest.CoreMatchers.is;
3231
import static org.hamcrest.MatcherAssert.assertThat;
33-
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
3432
import static org.mockito.Mockito.mock;
3533
import static org.mockito.Mockito.when;
3634

@@ -41,14 +39,16 @@ class InUsedShardingStorageUnitRetrieverTest {
4139

4240
@Test
4341
void assertGetInUsedResources() {
44-
ShowRulesUsedStorageUnitStatement sqlStatement = new ShowRulesUsedStorageUnitStatement("prod_ds", null);
45-
assertThat(retriever.getInUsedResources(sqlStatement, mockRule()), is(Arrays.asList("foo_auto_tbl", "foo_tbl")));
42+
ShowRulesUsedStorageUnitStatement sqlStatement = new ShowRulesUsedStorageUnitStatement("foo_ds", null);
43+
assertThat(retriever.getInUsedResources(sqlStatement, mockRule()), is(Collections.singleton("foo_tbl")));
4644
}
4745

4846
private ShardingRule mockRule() {
49-
ShardingRule result = mock(ShardingRule.class, RETURNS_DEEP_STUBS);
50-
when(result.getConfiguration().getAutoTables()).thenReturn(Collections.singleton(new ShardingAutoTableRuleConfiguration("foo_auto_tbl", "")));
51-
when(result.getConfiguration().getTables()).thenReturn(Collections.singleton(new ShardingTableRuleConfiguration("foo_tbl", "")));
47+
ShardingRule result = mock(ShardingRule.class);
48+
ShardingTable fooTbl = mock(ShardingTable.class);
49+
when(fooTbl.getLogicTable()).thenReturn("foo_tbl");
50+
when(fooTbl.getActualDataSourceNames()).thenReturn(Arrays.asList("foo_ds", "bar_ds"));
51+
when(result.getShardingTables()).thenReturn(Collections.singletonMap("foo_tbl", fooTbl));
5252
return result;
5353
}
5454
}

0 commit comments

Comments
 (0)