Skip to content

Commit

Permalink
Refactor StorageResourceCreator
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu committed Sep 27, 2023
1 parent 9c45d37 commit f0b9395
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,13 @@
import org.apache.shardingsphere.infra.config.database.DatabaseConfiguration;
import org.apache.shardingsphere.infra.config.rule.RuleConfiguration;
import org.apache.shardingsphere.infra.datasource.pool.config.DataSourceConfiguration;
import org.apache.shardingsphere.infra.datasource.pool.creator.DataSourcePoolCreator;
import org.apache.shardingsphere.infra.datasource.pool.props.creator.DataSourcePoolPropertiesCreator;
import org.apache.shardingsphere.infra.datasource.pool.props.domain.DataSourcePoolProperties;
import org.apache.shardingsphere.infra.metadata.database.resource.StorageResource;
import org.apache.shardingsphere.infra.metadata.database.resource.StorageResourceCreator;
import org.apache.shardingsphere.infra.metadata.database.resource.node.StorageNode;
import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnitNodeMapper;

import javax.sql.DataSource;
import java.util.Collection;
Expand All @@ -49,7 +52,16 @@ public DataSourceGeneratedDatabaseConfiguration(final Map<String, DataSourceConf
ruleConfigurations = ruleConfigs;
dataSourcePoolPropertiesMap = dataSourceConfigs.entrySet().stream()
.collect(Collectors.toMap(Entry::getKey, entry -> DataSourcePoolPropertiesCreator.create(entry.getValue()), (oldValue, currentValue) -> oldValue, LinkedHashMap::new));
storageResource = StorageResourceCreator.createStorageResource(dataSourcePoolPropertiesMap);
Map<String, StorageUnitNodeMapper> mappers = StorageResourceCreator.getStorageUnitNodeMappers(dataSourcePoolPropertiesMap);
storageResource = new StorageResource(getStorageNodeDataSourceMap(mappers), mappers);
}

private Map<StorageNode, DataSource> getStorageNodeDataSourceMap(final Map<String, StorageUnitNodeMapper> mappers) {
Map<StorageNode, DataSource> result = new LinkedHashMap<>(mappers.size(), 1F);
for (Entry<String, StorageUnitNodeMapper> entry : mappers.entrySet()) {
result.computeIfAbsent(entry.getValue().getStorageNode(), key -> DataSourcePoolCreator.create(entry.getKey(), dataSourcePoolPropertiesMap.get(entry.getKey()), true, result.values()));
}
return result;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,10 @@
import org.apache.shardingsphere.infra.database.core.connector.url.UnrecognizedDatabaseURLException;
import org.apache.shardingsphere.infra.database.core.type.DatabaseTypeFactory;
import org.apache.shardingsphere.infra.database.core.type.DatabaseTypeRegistry;
import org.apache.shardingsphere.infra.datasource.pool.creator.DataSourcePoolCreator;
import org.apache.shardingsphere.infra.datasource.pool.props.domain.DataSourcePoolProperties;
import org.apache.shardingsphere.infra.metadata.database.resource.node.StorageNode;
import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnitNodeMapper;

import javax.sql.DataSource;
import java.util.Collection;
import java.util.HashSet;
import java.util.LinkedHashMap;
Expand All @@ -42,24 +40,6 @@
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class StorageResourceCreator {

/**
* Create storage resource.
*
* @param propsMap data source pool properties map
* @return created storage resource
*/
public static StorageResource createStorageResource(final Map<String, DataSourcePoolProperties> propsMap) {
Map<StorageNode, DataSource> storageNodes = new LinkedHashMap<>();
Map<String, StorageUnitNodeMapper> mappers = new LinkedHashMap<>();
for (Entry<String, DataSourcePoolProperties> entry : propsMap.entrySet()) {
String storageUnitName = entry.getKey();
StorageUnitNodeMapper mapper = getStorageUnitNodeMapper(storageUnitName, entry.getValue());
mappers.put(storageUnitName, mapper);
storageNodes.computeIfAbsent(mapper.getStorageNode(), key -> DataSourcePoolCreator.create(storageUnitName, entry.getValue(), true, storageNodes.values()));
}
return new StorageResource(storageNodes, mappers);
}

/**
* Get storage unit node mappers.
*
Expand Down

0 comments on commit f0b9395

Please sign in to comment.