This is an automated email from the ASF dual-hosted git repository.
zhangliang 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 ddc06bf4702 Refactor GlobalRulesBuilder (#33932)
ddc06bf4702 is described below
commit ddc06bf47020262dadad57a544ae1560f8f33683
Author: Liang Zhang <[email protected]>
AuthorDate: Fri Dec 6 12:31:20 2024 +0800
Refactor GlobalRulesBuilder (#33932)
---
.../infra/rule/builder/global/GlobalRulesBuilder.java | 4 ++--
.../infra/rule/builder/global/GlobalRulesBuilderTest.java | 12 +++++-------
.../mode/metadata/manager/GlobalConfigurationManager.java | 2 +-
3 files changed, 8 insertions(+), 10 deletions(-)
diff --git
a/infra/common/src/main/java/org/apache/shardingsphere/infra/rule/builder/global/GlobalRulesBuilder.java
b/infra/common/src/main/java/org/apache/shardingsphere/infra/rule/builder/global/GlobalRulesBuilder.java
index e8d8a230027..b1529ce53f2 100644
---
a/infra/common/src/main/java/org/apache/shardingsphere/infra/rule/builder/global/GlobalRulesBuilder.java
+++
b/infra/common/src/main/java/org/apache/shardingsphere/infra/rule/builder/global/GlobalRulesBuilder.java
@@ -91,8 +91,8 @@ public final class GlobalRulesBuilder {
* @return built rule
*/
@SuppressWarnings("unchecked")
- public static Collection<ShardingSphereRule> buildSingleRules(final
RuleConfiguration globalRuleConfig, final Map<String, ShardingSphereDatabase>
databases, final ConfigurationProperties props) {
+ public static Collection<ShardingSphereRule> buildSingleRules(final
RuleConfiguration globalRuleConfig, final Collection<ShardingSphereDatabase>
databases, final ConfigurationProperties props) {
return OrderedSPILoader.getServices(GlobalRuleBuilder.class,
Collections.singleton(globalRuleConfig)).entrySet()
- .stream().map(each -> each.getValue().build(each.getKey(),
databases.values(), props)).collect(Collectors.toList());
+ .stream().map(each -> each.getValue().build(each.getKey(),
databases, props)).collect(Collectors.toList());
}
}
diff --git
a/infra/common/src/test/java/org/apache/shardingsphere/infra/rule/builder/global/GlobalRulesBuilderTest.java
b/infra/common/src/test/java/org/apache/shardingsphere/infra/rule/builder/global/GlobalRulesBuilderTest.java
index 1474b5356f0..6dbf6f6f747 100644
---
a/infra/common/src/test/java/org/apache/shardingsphere/infra/rule/builder/global/GlobalRulesBuilderTest.java
+++
b/infra/common/src/test/java/org/apache/shardingsphere/infra/rule/builder/global/GlobalRulesBuilderTest.java
@@ -39,17 +39,15 @@ class GlobalRulesBuilderTest {
@Test
void assertBuildRules() {
- Collection<ShardingSphereRule> shardingSphereRules =
GlobalRulesBuilder.buildRules(
- Collections.singletonList(new
FixtureGlobalRuleConfiguration()), Collections.singleton(buildDatabase()),
mock(ConfigurationProperties.class));
- assertThat(shardingSphereRules.size(), is(1));
- assertThat(shardingSphereRules.iterator().next(),
instanceOf(FixtureGlobalRule.class));
+ Collection<ShardingSphereRule> rules =
GlobalRulesBuilder.buildRules(Collections.singletonList(new
FixtureGlobalRuleConfiguration()), Collections.singleton(buildDatabase()),
mock());
+ assertThat(rules.size(), is(1));
+ assertThat(rules.iterator().next(),
instanceOf(FixtureGlobalRule.class));
}
@Test
void assertBuildSingleRules() {
- Collection<ShardingSphereRule> shardingSphereRules =
GlobalRulesBuilder.buildSingleRules(
- new FixtureGlobalRuleConfiguration(),
Collections.singletonMap("foo_db", buildDatabase()),
mock(ConfigurationProperties.class));
- assertThat(shardingSphereRules.size(), is(1));
+ Collection<ShardingSphereRule> rules =
GlobalRulesBuilder.buildSingleRules(new FixtureGlobalRuleConfiguration(),
Collections.singleton(buildDatabase()), mock());
+ assertThat(rules.size(), is(1));
}
private ShardingSphereDatabase buildDatabase() {
diff --git
a/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/manager/GlobalConfigurationManager.java
b/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/manager/GlobalConfigurationManager.java
index e60405879b7..5e14d0ec64b 100644
---
a/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/manager/GlobalConfigurationManager.java
+++
b/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/manager/GlobalConfigurationManager.java
@@ -66,7 +66,7 @@ public final class GlobalConfigurationManager {
closeStaleTransactionRule(ruleConfig);
Collection<ShardingSphereRule> rules = new
LinkedList<>(metaDataContexts.get().getMetaData().getGlobalRuleMetaData().getRules());
rules.removeIf(each ->
each.getConfiguration().getClass().isAssignableFrom(ruleConfig.getClass()));
- rules.addAll(GlobalRulesBuilder.buildSingleRules(ruleConfig,
metaDataContexts.get().getMetaData().getDatabases(),
metaDataContexts.get().getMetaData().getProps()));
+ rules.addAll(GlobalRulesBuilder.buildSingleRules(ruleConfig,
metaDataContexts.get().getMetaData().getAllDatabases(),
metaDataContexts.get().getMetaData().getProps()));
metaDataContexts.get().getMetaData().getGlobalRuleMetaData().getRules().clear();
metaDataContexts.get().getMetaData().getGlobalRuleMetaData().getRules().addAll(rules);
ShardingSphereMetaData toBeChangedMetaData = new
ShardingSphereMetaData(metaDataContexts.get().getMetaData().getAllDatabases(),