Skip to content

Commit

Permalink
Refactor ResourceMetaData
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu committed Sep 27, 2023
1 parent 8fdf257 commit 5087e61
Show file tree
Hide file tree
Showing 7 changed files with 12 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,8 @@ public final class ResourceMetaData {
private final StorageUnitMetaData storageUnitMetaData;

public ResourceMetaData(final Map<String, DataSource> dataSources) {
this(null, dataSources);
}

public ResourceMetaData(final String databaseName, final Map<String, DataSource> dataSources) {
dataSourceMap = StorageNodeUtils.getStorageNodeDataSources(dataSources);
storageUnitMetaData = new StorageUnitMetaData(databaseName, dataSourceMap, dataSources.entrySet().stream()
storageUnitMetaData = new StorageUnitMetaData(null, dataSourceMap, dataSources.entrySet().stream()
.collect(Collectors.toMap(Entry::getKey, entry -> DataSourcePoolPropertiesCreator.create(entry.getValue()), (oldValue, currentValue) -> oldValue, LinkedHashMap::new)),
StorageUnitNodeMapperUtils.fromDataSources(dataSources));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,28 +57,28 @@ class ShardingSphereDatabaseTest {

@Test
void assertIsComplete() {
ResourceMetaData resourceMetaData = new ResourceMetaData("sharding_db", Collections.singletonMap("ds", new MockedDataSource()));
ResourceMetaData resourceMetaData = new ResourceMetaData(Collections.singletonMap("ds", new MockedDataSource()));
RuleMetaData ruleMetaData = new RuleMetaData(Collections.singleton(mock(ShardingSphereRule.class)));
assertTrue(new ShardingSphereDatabase("foo_db", mock(DatabaseType.class), resourceMetaData, ruleMetaData, Collections.emptyMap()).isComplete());
}

@Test
void assertIsNotCompleteWithoutRule() {
ResourceMetaData resourceMetaData = new ResourceMetaData("sharding_db", Collections.singletonMap("ds", new MockedDataSource()));
ResourceMetaData resourceMetaData = new ResourceMetaData(Collections.singletonMap("ds", new MockedDataSource()));
RuleMetaData ruleMetaData = new RuleMetaData(Collections.emptyList());
assertFalse(new ShardingSphereDatabase("foo_db", mock(DatabaseType.class), resourceMetaData, ruleMetaData, Collections.emptyMap()).isComplete());
}

@Test
void assertIsNotCompleteWithoutDataSource() {
ResourceMetaData resourceMetaData = new ResourceMetaData("sharding_db", Collections.emptyMap());
ResourceMetaData resourceMetaData = new ResourceMetaData(Collections.emptyMap());
RuleMetaData ruleMetaData = new RuleMetaData(Collections.singleton(mock(ShardingSphereRule.class)));
assertFalse(new ShardingSphereDatabase("foo_db", mock(DatabaseType.class), resourceMetaData, ruleMetaData, Collections.emptyMap()).isComplete());
}

@Test
void assertReloadRules() {
ResourceMetaData resourceMetaData = new ResourceMetaData("sharding_db", Collections.singletonMap("ds", new MockedDataSource()));
ResourceMetaData resourceMetaData = new ResourceMetaData(Collections.singletonMap("ds", new MockedDataSource()));
Collection<ShardingSphereRule> rules = new LinkedList<>();
rules.add(mock(MutableDataNodeRule.class, RETURNS_DEEP_STUBS));
rules.add(mock(DataSourceContainedRule.class, RETURNS_DEEP_STUBS));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ void assertBuildRouteSQLRewriteResult() {
Map<RouteUnit, SQLRewriteUnit> sqlRewriteUnits = new HashMap<>(2, 1F);
sqlRewriteUnits.put(routeUnit1, sqlRewriteUnit1);
sqlRewriteUnits.put(routeUnit2, sqlRewriteUnit2);
ResourceMetaData resourceMetaData = new ResourceMetaData("sharding_db", Collections.emptyMap());
ResourceMetaData resourceMetaData = new ResourceMetaData(Collections.emptyMap());
RuleMetaData ruleMetaData = new RuleMetaData(Collections.emptyList());
ShardingSphereDatabase database = new ShardingSphereDatabase(DefaultDatabase.LOGIC_NAME, mock(DatabaseType.class), resourceMetaData, ruleMetaData, buildDatabase());
Collection<ExecutionUnit> actual = ExecutionContextBuilder.build(database, new RouteSQLRewriteResult(sqlRewriteUnits), mock(SQLStatementContext.class));
Expand All @@ -89,7 +89,7 @@ void assertBuildRouteSQLRewriteResultWithEmptyPrimaryKeyMeta() {
SQLRewriteUnit sqlRewriteUnit2 = new SQLRewriteUnit("sql2", Collections.singletonList("parameter2"));
Map<RouteUnit, SQLRewriteUnit> sqlRewriteUnits = new HashMap<>(2, 1F);
sqlRewriteUnits.put(routeUnit2, sqlRewriteUnit2);
ResourceMetaData resourceMetaData = new ResourceMetaData("sharding_db", Collections.emptyMap());
ResourceMetaData resourceMetaData = new ResourceMetaData(Collections.emptyMap());
RuleMetaData ruleMetaData = new RuleMetaData(Collections.emptyList());
ShardingSphereDatabase database = new ShardingSphereDatabase(DefaultDatabase.LOGIC_NAME, mock(DatabaseType.class), resourceMetaData, ruleMetaData, buildDatabaseWithoutPrimaryKey());
Collection<ExecutionUnit> actual = ExecutionContextBuilder.build(database, new RouteSQLRewriteResult(sqlRewriteUnits), mock(SQLStatementContext.class));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class TransactionRuleBuilderTest {
@Test
void assertBuild() {
TransactionRuleConfiguration ruleConfig = new TransactionRuleConfiguration("LOCAL", "provider", new Properties());
ShardingSphereDatabase database = new ShardingSphereDatabase("logic_db", null, new ResourceMetaData("db", createDataSourceMap()),
ShardingSphereDatabase database = new ShardingSphereDatabase("logic_db", null, new ResourceMetaData(createDataSourceMap()),
new RuleMetaData(Collections.singletonList(mock(ShardingSphereRule.class))), Collections.singletonMap("test", mock(ShardingSphereSchema.class)));
TransactionRule rule = new TransactionRuleBuilder().build(ruleConfig, Collections.singletonMap(DefaultDatabase.LOGIC_NAME, database), mock(ConfigurationProperties.class));
assertNotNull(rule.getConfiguration());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ private ShardingSphereDatabase mockDatabase() {

@Test
void assertGetDataSourceMap() {
ResourceMetaData resourceMetaData = new ResourceMetaData("sharding_db", Collections.singletonMap("foo_ds", new MockedDataSource()));
ResourceMetaData resourceMetaData = new ResourceMetaData(Collections.singletonMap("foo_ds", new MockedDataSource()));
ShardingSphereDatabase database =
new ShardingSphereDatabase(DefaultDatabase.LOGIC_NAME, mock(DatabaseType.class), resourceMetaData, mock(RuleMetaData.class), Collections.emptyMap());
when(metaDataContexts.getMetaData().getDatabase(DefaultDatabase.LOGIC_NAME)).thenReturn(database);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class ResourceSwitchManagerTest {
@Test
void assertCreate() {
Map<String, DataSource> dataSourceMap = createDataSourceMap();
SwitchingResource actual = new ResourceSwitchManager().create(new ResourceMetaData("sharding_db", dataSourceMap), createToBeChangedDataSourcePoolPropertiesMap());
SwitchingResource actual = new ResourceSwitchManager().create(new ResourceMetaData(dataSourceMap), createToBeChangedDataSourcePoolPropertiesMap());
assertNewDataSources(actual);
actual.closeStaleDataSources();
assertStaleDataSources(dataSourceMap);
Expand All @@ -52,7 +52,7 @@ void assertCreateByAlterDataSourcePoolProperties() {
Map<String, DataSource> dataSourceMap = new HashMap<>(3, 1F);
dataSourceMap.put("ds_0", new MockedDataSource());
dataSourceMap.put("ds_1", new MockedDataSource());
SwitchingResource actual = new ResourceSwitchManager().createByAlterDataSourcePoolProperties(new ResourceMetaData("sharding_db", dataSourceMap), Collections.emptyMap());
SwitchingResource actual = new ResourceSwitchManager().createByAlterDataSourcePoolProperties(new ResourceMetaData(dataSourceMap), Collections.emptyMap());
assertTrue(actual.getNewStorageResource().getDataSourceMap().isEmpty());
assertThat(actual.getStaleStorageResource().getDataSourceMap().size(), is(2));
actual.closeStaleDataSources();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ void assertAlterRuleConfiguration() {

private ShardingSphereMetaData createShardingSphereMetaData() {
return new ShardingSphereMetaData(Collections.singletonMap("db", new ShardingSphereDatabase("db", TypedSPILoader.getService(DatabaseType.class, "FIXTURE"),
new ResourceMetaData("db", Collections.emptyMap()), new RuleMetaData(Collections.emptyList()), Collections.emptyMap())),
new ResourceMetaData(Collections.emptyMap()), new RuleMetaData(Collections.emptyList()), Collections.emptyMap())),
mock(ResourceMetaData.class), new RuleMetaData(Collections.emptyList()), new ConfigurationProperties(new Properties()));
}

Expand Down

0 comments on commit 5087e61

Please sign in to comment.