Skip to content

Commit

Permalink
fix #2153
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu committed Apr 2, 2019
1 parent 6d90ffa commit 8b99619
Showing 1 changed file with 17 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,13 @@
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
* Table rule.
Expand All @@ -53,6 +55,9 @@ public final class TableRule {

private final List<DataNode> actualDataNodes;

@Getter(AccessLevel.NONE)
private final Set<String> actualTables;

@Getter(AccessLevel.NONE)
private final Map<DataNode, Integer> dataNodeIndexMap;

Expand All @@ -71,6 +76,7 @@ public final class TableRule {
public TableRule(final String defaultDataSourceName, final String logicTableName) {
logicTable = logicTableName.toLowerCase();
actualDataNodes = Collections.singletonList(new DataNode(defaultDataSourceName, logicTableName));
actualTables = getActualTables();
dataNodeIndexMap = Collections.emptyMap();
databaseShardingStrategy = null;
tableShardingStrategy = null;
Expand All @@ -84,6 +90,7 @@ public TableRule(final Collection<String> dataSourceNames, final String logicTab
logicTable = logicTableName.toLowerCase();
dataNodeIndexMap = new HashMap<>(dataSourceNames.size(), 1);
actualDataNodes = generateDataNodes(logicTableName, dataSourceNames);
actualTables = getActualTables();
databaseShardingStrategy = null;
tableShardingStrategy = null;
generateKeyColumn = null;
Expand All @@ -98,6 +105,7 @@ public TableRule(final TableRuleConfiguration tableRuleConfig, final ShardingDat
dataNodeIndexMap = new HashMap<>(dataNodes.size(), 1);
actualDataNodes = isEmptyDataNodes(dataNodes)
? generateDataNodes(tableRuleConfig.getLogicTable(), shardingDataSourceNames.getDataSourceNames()) : generateDataNodes(dataNodes, shardingDataSourceNames.getDataSourceNames());
actualTables = getActualTables();
databaseShardingStrategy = null == tableRuleConfig.getDatabaseShardingStrategyConfig() ? null : ShardingStrategyFactory.newInstance(tableRuleConfig.getDatabaseShardingStrategyConfig());
tableShardingStrategy = null == tableRuleConfig.getTableShardingStrategyConfig() ? null : ShardingStrategyFactory.newInstance(tableRuleConfig.getTableShardingStrategyConfig());
generateKeyColumn = getGenerateKeyColumn(tableRuleConfig.getKeyGeneratorConfig(), defaultGenerateKeyColumn);
Expand All @@ -107,6 +115,14 @@ public TableRule(final TableRuleConfiguration tableRuleConfig, final ShardingDat
logicIndex = null == tableRuleConfig.getLogicIndex() ? null : tableRuleConfig.getLogicIndex().toLowerCase();
}

private Set<String> getActualTables() {
Set<String> result = new HashSet<>(actualDataNodes.size(), 1);
for (DataNode each : actualDataNodes) {
result.add(each.getTableName());
}
return result;
}

private boolean containsKeyGeneratorConfiguration(final TableRuleConfiguration tableRuleConfiguration) {
return null != tableRuleConfiguration.getKeyGeneratorConfig() && !Strings.isNullOrEmpty(tableRuleConfiguration.getKeyGeneratorConfig().getType());
}
Expand Down Expand Up @@ -201,11 +217,6 @@ int findActualTableIndex(final String dataSourceName, final String actualTableNa
}

boolean isExisted(final String actualTableName) {
for (DataNode each : actualDataNodes) {
if (each.getTableName().equalsIgnoreCase(actualTableName)) {
return true;
}
}
return false;
return actualTables.contains(actualTableName);
}
}

0 comments on commit 8b99619

Please sign in to comment.