Skip to content

Commit

Permalink
Refactor GlobalRulesBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu committed Dec 3, 2024
1 parent 3d0d54f commit 167f2fb
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ public final class GlobalRulesBuilder {
*/
@SuppressWarnings({"unchecked", "rawtypes"})
public static Collection<ShardingSphereRule> buildRules(final Collection<RuleConfiguration> globalRuleConfigs,
final Map<String, ShardingSphereDatabase> databases, final ConfigurationProperties props) {
final Collection<ShardingSphereDatabase> databases, final ConfigurationProperties props) {
Collection<ShardingSphereRule> result = new LinkedList<>();
for (Entry<RuleConfiguration, GlobalRuleBuilder> entry : getRuleBuilderMap(globalRuleConfigs).entrySet()) {
result.add(entry.getValue().build(entry.getKey(), databases.values(), props));
result.add(entry.getValue().build(entry.getKey(), databases, props));
}
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class GlobalRulesBuilderTest {
@Test
void assertBuildRules() {
Collection<ShardingSphereRule> shardingSphereRules = GlobalRulesBuilder.buildRules(
Collections.singletonList(new FixtureGlobalRuleConfiguration()), Collections.singletonMap("foo_db", buildDatabase()), mock(ConfigurationProperties.class));
Collections.singletonList(new FixtureGlobalRuleConfiguration()), Collections.singleton(buildDatabase()), mock(ConfigurationProperties.class));
assertThat(shardingSphereRules.size(), is(1));
assertThat(shardingSphereRules.iterator().next(), instanceOf(FixtureGlobalRule.class));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ private MetaDataContexts createMetaDataContexts(final ShardingSphereDatabase dat
.createChangedDatabases(database.getName(), false, switchingResource, ruleConfigs, metaDataContexts.get(), metaDataPersistService, computeNodeInstanceContext);
ConfigurationProperties props = new ConfigurationProperties(metaDataPersistService.getPropsService().load());
RuleMetaData changedGlobalMetaData = new RuleMetaData(
GlobalRulesBuilder.buildRules(ruleConfigPersistDecorateEngine.restore(metaDataPersistService.getGlobalRuleService().load()), changedDatabases, props));
GlobalRulesBuilder.buildRules(ruleConfigPersistDecorateEngine.restore(metaDataPersistService.getGlobalRuleService().load()), changedDatabases.values(), props));
MetaDataContexts result = MetaDataContextsFactory.create(metaDataPersistService,
new ShardingSphereMetaData(changedDatabases, metaDataContexts.get().getMetaData().getGlobalResourceMetaData(), changedGlobalMetaData, props));
switchingResource.closeStaleDataSources();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ private static MetaDataContexts newMetaDataContexts(final MetaDataPersistService
final ConfigurationProperties props) {
// TODO load global data sources from persist service
ResourceMetaData globalResourceMetaData = new ResourceMetaData(param.getGlobalDataSources());
RuleMetaData globalRuleMetaData = new RuleMetaData(GlobalRulesBuilder.buildRules(globalRuleConfigs, databases, props));
RuleMetaData globalRuleMetaData = new RuleMetaData(GlobalRulesBuilder.buildRules(globalRuleConfigs, databases.values(), props));
ShardingSphereMetaData shardingSphereMetaData = new ShardingSphereMetaData(databases, globalResourceMetaData, globalRuleMetaData, props);
ShardingSphereStatistics shardingSphereStatistics = initStatistics(persistService, shardingSphereMetaData);
return new MetaDataContexts(shardingSphereMetaData, shardingSphereStatistics);
Expand Down Expand Up @@ -219,7 +219,7 @@ private static void restoreRules(final MetaDataContexts metaDataContexts, final
metaDataContexts.getMetaData().getGlobalRuleMetaData().getRules().removeIf(eachRule -> each.getRuleType() == eachRule.getClass());
RuleConfiguration restoredRuleConfig = each.restore(rule.getConfiguration());
ShardingSphereRule rebuiltRule = GlobalRulesBuilder.buildRules(
Collections.singleton(restoredRuleConfig), metaDataContexts.getMetaData().getDatabases(), metaDataContexts.getMetaData().getProps()).iterator().next();
Collections.singleton(restoredRuleConfig), metaDataContexts.getMetaData().getAllDatabases(), metaDataContexts.getMetaData().getProps()).iterator().next();
metaDataContexts.getMetaData().getGlobalRuleMetaData().getRules().add(rebuiltRule);
}
}
Expand Down Expand Up @@ -271,7 +271,7 @@ public static MetaDataContexts createBySwitchResource(final String databaseName,
createChangedDatabases(databaseName, internalLoadMetaData, switchingResource, null, originalMetaDataContexts, metaDataPersistService, computeNodeInstanceContext);
ConfigurationProperties props = originalMetaDataContexts.getMetaData().getProps();
RuleMetaData changedGlobalMetaData = new RuleMetaData(
GlobalRulesBuilder.buildRules(originalMetaDataContexts.getMetaData().getGlobalRuleMetaData().getConfigurations(), changedDatabases, props));
GlobalRulesBuilder.buildRules(originalMetaDataContexts.getMetaData().getGlobalRuleMetaData().getConfigurations(), changedDatabases.values(), props));
return create(metaDataPersistService, new ShardingSphereMetaData(changedDatabases, originalMetaDataContexts.getMetaData().getGlobalResourceMetaData(), changedGlobalMetaData, props));
}

Expand All @@ -294,7 +294,7 @@ public static MetaDataContexts createByAlterRule(final String databaseName, fina
createChangedDatabases(databaseName, internalLoadMetaData, null, ruleConfigs, originalMetaDataContexts, metaDataPersistService, computeNodeInstanceContext);
ConfigurationProperties props = originalMetaDataContexts.getMetaData().getProps();
RuleMetaData changedGlobalMetaData = new RuleMetaData(
GlobalRulesBuilder.buildRules(originalMetaDataContexts.getMetaData().getGlobalRuleMetaData().getConfigurations(), changedDatabases, props));
GlobalRulesBuilder.buildRules(originalMetaDataContexts.getMetaData().getGlobalRuleMetaData().getConfigurations(), changedDatabases.values(), props));
return create(metaDataPersistService, new ShardingSphereMetaData(changedDatabases, originalMetaDataContexts.getMetaData().getGlobalResourceMetaData(), changedGlobalMetaData, props));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ void setUp() throws SQLException {
when(database.getProtocolType()).thenReturn(TypedSPILoader.getService(DatabaseType.class, "FIXTURE"));
when(database.getRuleMetaData().getRules()).thenReturn(Collections.emptyList());
when(ExternalMetaDataFactory.create(anyMap(), any(), any())).thenReturn(new HashMap<>(Collections.singletonMap("foo_db", database)));
when(GlobalRulesBuilder.buildRules(anyCollection(), anyMap(), any(ConfigurationProperties.class))).thenReturn(Collections.singleton(new MockedRule()));
when(GlobalRulesBuilder.buildRules(anyCollection(), anyCollection(), any(ConfigurationProperties.class))).thenReturn(Collections.singleton(new MockedRule()));
}

private DatabaseRulePersistService mockDatabaseRulePersistService() {
Expand Down

0 comments on commit 167f2fb

Please sign in to comment.