Skip to content

Commit

Permalink
refactor ShardingMetaData
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu committed Jul 15, 2018
1 parent 682afac commit bf08ccd
Showing 1 changed file with 9 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import io.shardingsphere.core.rule.TableRule;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

import java.sql.Connection;
Expand All @@ -51,21 +50,19 @@
*/
@RequiredArgsConstructor
@Getter
@Setter
@Slf4j
public abstract class ShardingMetaData {

private final ListeningExecutorService executorService;

private Map<String, TableMetaData> tableMetaDataMap;
private final Map<String, TableMetaData> tableMetaDataMap = new HashMap<>();

/**
* Initialize sharding metadata.
*
* @param shardingRule sharding rule
*/
public void init(final ShardingRule shardingRule) {
tableMetaDataMap = new HashMap<>();
try {
Collection<TableRule> tableRules = getTableRules(shardingRule);
for (TableRule each : tableRules) {
Expand Down Expand Up @@ -101,11 +98,11 @@ private Collection<TableRule> getTableRules(final ShardingRule shardingRule) thr
/**
* Refresh each tableMetaData by TableRule.
*
* @param each table rule
* @param tableRule table rule
* @param shardingRule sharding rule
*/
public void refresh(final TableRule each, final ShardingRule shardingRule) {
refresh(each, shardingRule, Collections.<String, Connection>emptyMap());
public void refresh(final TableRule tableRule, final ShardingRule shardingRule) {
refresh(tableRule, shardingRule, Collections.<String, Connection>emptyMap());
}

/**
Expand All @@ -119,8 +116,8 @@ public void refresh(final TableRule each, final ShardingRule shardingRule, final
tableMetaDataMap.put(each.getLogicTable(), getFinalTableMetaData(each.getLogicTable(), each.getActualDataNodes(), shardingRule.getShardingDataSourceNames(), connectionMap));
}

private TableMetaData getFinalTableMetaData(final String logicTableName, final List<DataNode> actualDataNodes,
final ShardingDataSourceNames shardingDataSourceNames, final Map<String, Connection> connectionMap) {
private TableMetaData getFinalTableMetaData(
final String logicTableName, final List<DataNode> actualDataNodes, final ShardingDataSourceNames shardingDataSourceNames, final Map<String, Connection> connectionMap) {
List<TableMetaData> actualTableMetaDataList = getAllActualTableMetaData(actualDataNodes, shardingDataSourceNames, connectionMap);
for (int i = 0; i < actualTableMetaDataList.size(); i++) {
if (actualTableMetaDataList.size() - 1 == i) {
Expand All @@ -133,17 +130,17 @@ private TableMetaData getFinalTableMetaData(final String logicTableName, final L
return new TableMetaData();
}

private List<TableMetaData> getAllActualTableMetaData(final List<DataNode> actualDataNodes, final ShardingDataSourceNames shardingDataSourceNames,
final Map<String, Connection> connectionMap) {
private List<TableMetaData> getAllActualTableMetaData(final List<DataNode> actualDataNodes, final ShardingDataSourceNames shardingDataSourceNames, final Map<String, Connection> connectionMap) {
List<ListenableFuture<TableMetaData>> result = new ArrayList<>();
for (final DataNode each : actualDataNodes) {
result.add(executorService.submit(new Callable<TableMetaData>() {

@Override
public TableMetaData call() throws Exception {
return getTableMetaData(each, shardingDataSourceNames, connectionMap);
}
}));
}

try {
return Futures.allAsList(result).get();
} catch (final InterruptedException | ExecutionException ex) {
Expand Down

0 comments on commit bf08ccd

Please sign in to comment.