Skip to content

Commit

Permalink
Refactor StorageUnitMetaData (#28617)
Browse files Browse the repository at this point in the history
* Refactor StorageUnitMetaData

* Fix test case
  • Loading branch information
terrymanu authored Sep 28, 2023
1 parent b103ffa commit 0e9af37
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,16 @@ public final class ResourceMetaData {

public ResourceMetaData(final Map<String, DataSource> dataSources) {
this.dataSources = StorageNodeUtils.getStorageNodeDataSources(dataSources);
storageUnitMetaData = new StorageUnitMetaData(null, this.dataSources, dataSources.entrySet().stream()
.collect(Collectors.toMap(Entry::getKey, entry -> DataSourcePoolPropertiesCreator.create(entry.getValue()), (oldValue, currentValue) -> oldValue, LinkedHashMap::new)),
StorageUnitNodeMapUtils.fromDataSources(dataSources));
storageUnitMetaData = new StorageUnitMetaData(null, StorageUnitNodeMapUtils.fromDataSources(dataSources),
dataSources.entrySet().stream().collect(
Collectors.toMap(Entry::getKey, entry -> DataSourcePoolPropertiesCreator.create(entry.getValue()), (oldValue, currentValue) -> oldValue, LinkedHashMap::new)),
this.dataSources);
}

public ResourceMetaData(final String databaseName, final Map<StorageNodeName, DataSource> dataSources,
final Map<String, StorageNode> storageUnitNodeMap, final Map<String, DataSourcePoolProperties> propsMap) {
this.dataSources = dataSources;
storageUnitMetaData = new StorageUnitMetaData(databaseName, dataSources, propsMap, storageUnitNodeMap);
storageUnitMetaData = new StorageUnitMetaData(databaseName, storageUnitNodeMap, propsMap, dataSources);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,28 +37,21 @@ public final class StorageUnitMetaData {
// TODO zhangliang: should refactor
private final Map<String, StorageNode> storageNodes;

private final Map<String, DataSourcePoolProperties> dataSourcePoolPropertiesMap;

private final Map<String, StorageUnit> storageUnits;

// TODO zhangliang: should refactor
private final Map<String, DataSource> dataSources;

public StorageUnitMetaData(final String databaseName, final Map<StorageNodeName, DataSource> storageNodeDataSources,
final Map<String, DataSourcePoolProperties> dataSourcePoolPropertiesMap, final Map<String, StorageNode> storageNodes) {
public StorageUnitMetaData(final String databaseName, final Map<String, StorageNode> storageNodes, final Map<String, DataSourcePoolProperties> dataSourcePoolPropertiesMap,
final Map<StorageNodeName, DataSource> storageNodeDataSources) {
this.storageNodes = storageNodes;
this.dataSourcePoolPropertiesMap = dataSourcePoolPropertiesMap;
storageUnits = storageNodes.entrySet().stream().collect(
Collectors.toMap(Entry::getKey, entry -> new StorageUnit(databaseName, storageNodeDataSources, dataSourcePoolPropertiesMap.get(entry.getKey()), entry.getValue()),
(oldValue, currentValue) -> currentValue, () -> new LinkedHashMap<>(this.storageNodes.size(), 1F)));
dataSources = storageUnits.entrySet().stream().collect(
Collectors.toMap(Entry::getKey, entry -> entry.getValue().getDataSource(), (oldValue, currentValue) -> currentValue, () -> new LinkedHashMap<>(storageUnits.size(), 1F)));
}

/**
* Get data source pool properties map.
*
* @return data source pool properties map
*/
public Map<String, DataSourcePoolProperties> getDataSourcePoolPropertiesMap() {
return storageUnits.entrySet().stream()
.collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getDataSourcePoolProperties(), (oldValue, currentValue) -> oldValue, LinkedHashMap::new));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ private ContextManagerBuilderParameter createContextManagerBuilderParameter() {
private Map<String, ShardingSphereDatabase> createDatabases() {
when(database.getName()).thenReturn("db");
ResourceMetaData resourceMetaData = mock(ResourceMetaData.class, RETURNS_DEEP_STUBS);
when(resourceMetaData.getStorageUnitMetaData().getStorageNodes()).thenReturn(Collections.emptyMap());
when(database.getResourceMetaData()).thenReturn(resourceMetaData);
when(database.getSchemas()).thenReturn(Collections.singletonMap("foo_schema", new ShardingSphereSchema()));
when(database.getProtocolType()).thenReturn(TypedSPILoader.getService(DatabaseType.class, "FIXTURE"));
Expand Down

0 comments on commit 0e9af37

Please sign in to comment.