Skip to content

Commit

Permalink
Rename StorageResource's variable names
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu committed Sep 28, 2023
1 parent 4add73f commit a048dd0
Show file tree
Hide file tree
Showing 11 changed files with 23 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public static ShardingSphereDatabase create(final String name, final DatabaseTyp
}

private static ResourceMetaData createResourceMetaData(final String databaseName, final StorageResource storageResource, final Map<String, DataSourcePoolProperties> propsMap) {
return new ResourceMetaData(databaseName, storageResource.getDataSourceMap(), storageResource.getStorageUnitNodeMap(), propsMap);
return new ResourceMetaData(databaseName, storageResource.getDataSources(), storageResource.getStorageUnitNodeMap(), propsMap);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@
@Getter
public final class StorageResource {

private final Map<StorageNodeName, DataSource> dataSourceMap;
private final Map<StorageNodeName, DataSource> dataSources;

private final Map<String, StorageNode> storageUnitNodeMap;

private final Map<String, DataSource> wrappedDataSources;

public StorageResource(final Map<StorageNodeName, DataSource> dataSourceMap, final Map<String, StorageNode> storageUnitNodeMap) {
this.dataSourceMap = dataSourceMap;
public StorageResource(final Map<StorageNodeName, DataSource> dataSources, final Map<String, StorageNode> storageUnitNodeMap) {
this.dataSources = dataSources;
this.storageUnitNodeMap = storageUnitNodeMap;
wrappedDataSources = createWrappedDataSources();
}
Expand All @@ -49,7 +49,7 @@ private Map<String, DataSource> createWrappedDataSources() {
Map<String, DataSource> result = new LinkedHashMap<>(storageUnitNodeMap.size(), 1F);
for (Entry<String, StorageNode> entry : storageUnitNodeMap.entrySet()) {
StorageNode storageNode = entry.getValue();
DataSource dataSource = dataSourceMap.get(storageNode.getName());
DataSource dataSource = dataSources.get(storageNode.getName());
if (null != dataSource) {
result.put(entry.getKey(), new CatalogSwitchableDataSource(dataSource, storageNode.getCatalog(), storageNode.getUrl()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ void assertGetDataSources() {
@Test
void assertGetStorageNodes() {
DataSourceGeneratedDatabaseConfiguration databaseConfig = createDataSourceGeneratedDatabaseConfiguration();
HikariDataSource hikariDataSource = (HikariDataSource) databaseConfig.getStorageResource().getDataSourceMap().get(new StorageNodeName("normal_db"));
HikariDataSource hikariDataSource = (HikariDataSource) databaseConfig.getStorageResource().getDataSources().get(new StorageNodeName("normal_db"));
assertThat(hikariDataSource.getJdbcUrl(), is("jdbc:mock://127.0.0.1/normal_db"));
assertThat(hikariDataSource.getUsername(), is("root"));
assertThat(hikariDataSource.getPassword(), is(""));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ void assertGetDataSources() {
@Test
void assertGetStorageNodes() {
DataSourceProvidedDatabaseConfiguration databaseConfig = createDataSourceProvidedDatabaseConfiguration();
MockedDataSource dataSource = (MockedDataSource) databaseConfig.getStorageResource().getDataSourceMap().get(new StorageNodeName("foo_ds"));
MockedDataSource dataSource = (MockedDataSource) databaseConfig.getStorageResource().getDataSources().get(new StorageNodeName("foo_ds"));
assertThat(dataSource.getUrl(), is("jdbc:mock://127.0.0.1/foo_ds"));
assertThat(dataSource.getUsername(), is("root"));
assertThat(dataSource.getPassword(), is("root"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ private Collection<RuleConfiguration> decorateRuleConfigs(final String databaseN

private Map<String, DataSourcePoolProperties> getDataSourcePoolPropertiesMap(final DatabaseConfiguration databaseConfigs) {
if (!databaseConfigs.getDataSources().isEmpty() && databaseConfigs.getDataSourcePoolPropertiesMap().isEmpty()) {
return getDataSourcePoolPropertiesMap(databaseConfigs.getStorageResource().getDataSourceMap());
return getDataSourcePoolPropertiesMap(databaseConfigs.getStorageResource().getDataSources());
}
return databaseConfigs.getDataSourcePoolPropertiesMap();
}
Expand All @@ -132,7 +132,7 @@ private Map<String, DataSourcePoolProperties> getDataSourcePoolPropertiesMap(fin
public Map<String, DataSourceConfiguration> getEffectiveDataSources(final String databaseName, final Map<String, ? extends DatabaseConfiguration> databaseConfigs) {
Map<String, DataSourcePoolProperties> propsMap = dataSourceUnitService.load(databaseName);
if (databaseConfigs.containsKey(databaseName) && !databaseConfigs.get(databaseName).getDataSources().isEmpty()) {
databaseConfigs.get(databaseName).getStorageResource().getDataSourceMap().values().forEach(each -> new DataSourcePoolDestroyer(each).asyncDestroy());
databaseConfigs.get(databaseName).getStorageResource().getDataSources().values().forEach(each -> new DataSourcePoolDestroyer(each).asyncDestroy());
}
return propsMap.entrySet().stream().collect(Collectors.toMap(Entry::getKey,
entry -> DataSourcePoolPropertiesCreator.createConfiguration(entry.getValue()), (oldValue, currentValue) -> oldValue, LinkedHashMap::new));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ private Collection<RuleConfiguration> decorateRuleConfigs(final String databaseN

private Map<String, DataSourcePoolProperties> getDataSourcePoolPropertiesMap(final DatabaseConfiguration databaseConfigs) {
if (!databaseConfigs.getDataSources().isEmpty() && databaseConfigs.getDataSourcePoolPropertiesMap().isEmpty()) {
return getDataSourcePoolPropertiesMap(databaseConfigs.getStorageResource().getDataSourceMap());
return getDataSourcePoolPropertiesMap(databaseConfigs.getStorageResource().getDataSources());
}
return databaseConfigs.getDataSourcePoolPropertiesMap();
}
Expand All @@ -145,7 +145,7 @@ private Map<String, DataSourcePoolProperties> getDataSourcePoolPropertiesMap(fin
public Map<String, DataSourceConfiguration> getEffectiveDataSources(final String databaseName, final Map<String, ? extends DatabaseConfiguration> databaseConfigs) {
Map<String, DataSourcePoolProperties> propsMap = dataSourceUnitService.load(databaseName);
if (databaseConfigs.containsKey(databaseName) && !databaseConfigs.get(databaseName).getDataSources().isEmpty()) {
databaseConfigs.get(databaseName).getStorageResource().getDataSourceMap().values().forEach(each -> new DataSourcePoolDestroyer(each).asyncDestroy());
databaseConfigs.get(databaseName).getStorageResource().getDataSources().values().forEach(each -> new DataSourcePoolDestroyer(each).asyncDestroy());
}
return propsMap.entrySet().stream().collect(Collectors.toMap(Entry::getKey,
entry -> DataSourcePoolPropertiesCreator.createConfiguration(entry.getValue()), (oldValue, currentValue) -> oldValue, LinkedHashMap::new));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ public Map<String, ShardingSphereDatabase> renewDatabase(final ShardingSphereDat
private Map<StorageNodeName, DataSource> getNewStorageNodes(final Map<StorageNodeName, DataSource> currentStorageNodes, final SwitchingResource resource) {
Map<StorageNodeName, DataSource> result = new LinkedHashMap<>();
for (Entry<StorageNodeName, DataSource> entry : currentStorageNodes.entrySet()) {
if (!resource.getStaleStorageResource().getDataSourceMap().containsKey(entry.getKey())) {
if (!resource.getStaleStorageResource().getDataSources().containsKey(entry.getKey())) {
result.put(entry.getKey(), entry.getValue());
}
}
Expand Down Expand Up @@ -366,8 +366,8 @@ private DatabaseConfiguration getDatabaseConfiguration(final ResourceMetaData re
private StorageResource getMergedStorageResource(final ResourceMetaData currentResourceMetaData, final SwitchingResource switchingResource) {
Map<StorageNodeName, DataSource> storageNodeDataSources = currentResourceMetaData.getDataSources();
Map<String, StorageNode> storageUnitNodeMap = currentResourceMetaData.getStorageUnitMetaData().getStorageUnitNodeMap();
if (null != switchingResource && null != switchingResource.getNewStorageResource() && !switchingResource.getNewStorageResource().getDataSourceMap().isEmpty()) {
storageNodeDataSources.putAll(switchingResource.getNewStorageResource().getDataSourceMap());
if (null != switchingResource && null != switchingResource.getNewStorageResource() && !switchingResource.getNewStorageResource().getDataSources().isEmpty()) {
storageNodeDataSources.putAll(switchingResource.getNewStorageResource().getDataSources());
}
if (null != switchingResource && null != switchingResource.getNewStorageResource() && !switchingResource.getNewStorageResource().getStorageUnitNodeMap().isEmpty()) {
storageUnitNodeMap.putAll(switchingResource.getNewStorageResource().getStorageUnitNodeMap());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public SwitchingResource createByAlterDataSourcePoolProperties(final ResourceMet
Map<String, StorageNode> toBeChangedStorageUnitNodeMap = StorageUnitNodeMapUtils.fromDataSourcePoolProperties(toBeChangedPropsMap);
StorageResource staleStorageResource = getStaleDataSources(resourceMetaData, toBeChangedStorageUnitNodeMap, toBeChangedPropsMap);
Collection<StorageNodeName> toBeChangedStorageNodeNames = toBeChangedStorageUnitNodeMap.values().stream().map(StorageNode::getName).collect(Collectors.toSet());
staleStorageResource.getDataSourceMap().putAll(getToBeDeletedDataSources(resourceMetaData.getDataSources(), toBeChangedStorageNodeNames));
staleStorageResource.getDataSources().putAll(getToBeDeletedDataSources(resourceMetaData.getDataSources(), toBeChangedStorageNodeNames));
staleStorageResource.getStorageUnitNodeMap().putAll(
getToBeDeletedStorageUnitNodeMap(resourceMetaData.getStorageUnitMetaData().getStorageUnits(), toBeChangedStorageUnitNodeMap.keySet()));
return new SwitchingResource(resourceMetaData,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,6 @@ public final class SwitchingResource {
* Close stale data sources.
*/
public void closeStaleDataSources() {
staleStorageResource.getDataSourceMap().values().stream().filter(Objects::nonNull).forEach(each -> new DataSourcePoolDestroyer(each).asyncDestroy());
staleStorageResource.getDataSources().values().stream().filter(Objects::nonNull).forEach(each -> new DataSourcePoolDestroyer(each).asyncDestroy());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ void assertCreateByAlterDataSourcePoolProperties() {
dataSourceMap.put("ds_0", new MockedDataSource());
dataSourceMap.put("ds_1", new MockedDataSource());
SwitchingResource actual = new ResourceSwitchManager().createByAlterDataSourcePoolProperties(new ResourceMetaData(dataSourceMap), Collections.emptyMap());
assertTrue(actual.getNewStorageResource().getDataSourceMap().isEmpty());
assertThat(actual.getStaleStorageResource().getDataSourceMap().size(), is(2));
assertTrue(actual.getNewStorageResource().getDataSources().isEmpty());
assertThat(actual.getStaleStorageResource().getDataSources().size(), is(2));
actual.closeStaleDataSources();
assertStaleDataSource((MockedDataSource) dataSourceMap.get("ds_0"));
assertStaleDataSource((MockedDataSource) dataSourceMap.get("ds_1"));
Expand Down Expand Up @@ -85,10 +85,10 @@ private Map<String, Object> getDataSourcePoolProperties(final int initialCapacit
}

private void assertNewDataSources(final SwitchingResource actual) {
assertThat(actual.getNewStorageResource().getDataSourceMap().size(), is(3));
assertTrue(actual.getNewStorageResource().getDataSourceMap().containsKey(new StorageNodeName("not_change")));
assertTrue(actual.getNewStorageResource().getDataSourceMap().containsKey(new StorageNodeName("new")));
assertTrue(actual.getNewStorageResource().getDataSourceMap().containsKey(new StorageNodeName("replace")));
assertThat(actual.getNewStorageResource().getDataSources().size(), is(3));
assertTrue(actual.getNewStorageResource().getDataSources().containsKey(new StorageNodeName("not_change")));
assertTrue(actual.getNewStorageResource().getDataSources().containsKey(new StorageNodeName("new")));
assertTrue(actual.getNewStorageResource().getDataSources().containsKey(new StorageNodeName("replace")));
}

private void assertStaleDataSources(final Map<String, DataSource> originalDataSourceMap) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ void assertSwap() throws IOException {
private void assertDataSources(final ProxyConfiguration proxyConfig) {
Map<String, DatabaseConfiguration> actual = proxyConfig.getDatabaseConfigurations();
assertThat(actual.size(), is(1));
HikariDataSource dataSource = (HikariDataSource) actual.get("swapper_test").getStorageResource().getDataSourceMap().get(new StorageNodeName("foo_db"));
HikariDataSource dataSource = (HikariDataSource) actual.get("swapper_test").getStorageResource().getDataSources().get(new StorageNodeName("foo_db"));
assertThat(dataSource.getJdbcUrl(), is("jdbc:h2:mem:foo_db;DB_CLOSE_DELAY=-1"));
assertThat(dataSource.getUsername(), is("sa"));
assertThat(dataSource.getPassword(), is(""));
Expand Down

0 comments on commit a048dd0

Please sign in to comment.