提交 b005981e 编写于 作者: G gaohongtao

1.fix test case error in jdk 8

2.Optimize table sharding rule
上级 7921481a
......@@ -24,6 +24,7 @@ import java.util.HashMap;
import java.util.Map;
import com.dangdang.ddframe.rdb.sharding.api.DatabaseType;
import com.dangdang.ddframe.rdb.sharding.api.rule.DataNode;
import com.dangdang.ddframe.rdb.sharding.api.rule.TableRule;
import com.dangdang.ddframe.rdb.sharding.exception.SQLParserException;
import com.dangdang.ddframe.rdb.sharding.router.SQLRouteEngine;
......@@ -31,6 +32,7 @@ import com.dangdang.ddframe.rdb.sharding.router.SQLRouteResult;
import com.google.common.collect.Lists;
import org.junit.Test;
import static org.hamcrest.Matchers.hasItems;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;
......@@ -45,12 +47,13 @@ public class TableRuleTest extends AbstractConfigTest {
@Test
public void testActualTableName() {
Map<String, TableRule> tableRuleMap = getTableRule("actual_table_name");
assertThat(tableRuleMap.get("order_1").getActualTables().toString(), is("[DataNode(dataSourceName=db0, tableName=t_order_1), DataNode(dataSourceName=db0, tableName=t_order_3), DataNode(dataSourceName=db1, tableName=t_order_2), DataNode(dataSourceName=db1, tableName=t_order_4)]"));
assertThat(tableRuleMap.get("order_2").getActualTables().toString(), is("[DataNode(dataSourceName=db0, tableName=t_order_1), DataNode(dataSourceName=db1, tableName=t_order_1), DataNode(dataSourceName=db0, tableName=t_order_2), DataNode(dataSourceName=db1, tableName=t_order_2), DataNode(dataSourceName=db0, tableName=t_order_3), DataNode(dataSourceName=db1, tableName=t_order_3), DataNode(dataSourceName=db0, tableName=t_order_bak), DataNode(dataSourceName=db1, tableName=t_order_bak)]"));
assertThat(tableRuleMap.get("order_3").getActualTables().toString(), is("[DataNode(dataSourceName=db0, tableName=table_1), DataNode(dataSourceName=db1, tableName=table_1), DataNode(dataSourceName=db0, tableName=table_1_bak), DataNode(dataSourceName=db1, tableName=table_1_bak), " +
"DataNode(dataSourceName=db0, tableName=table_2), DataNode(dataSourceName=db1, tableName=table_2), DataNode(dataSourceName=db0, tableName=table_2_bak), DataNode(dataSourceName=db1, tableName=table_2_bak), " +
"DataNode(dataSourceName=db0, tableName=table_3), DataNode(dataSourceName=db1, tableName=table_3), DataNode(dataSourceName=db0, tableName=table_3_bak), DataNode(dataSourceName=db1, tableName=table_3_bak)]"));
assertThat(tableRuleMap.get("order_4").getActualTables().toString(), is("[DataNode(dataSourceName=db0, tableName=table_1), DataNode(dataSourceName=db1, tableName=table_1)]"));
assertThat(tableRuleMap.get("order_1").getActualTables(), hasItems(new DataNode("db0", "t_order_1"), new DataNode("db0", "t_order_3"), new DataNode("db1", "t_order_2"), new DataNode("db1", "t_order_4")));
assertThat(tableRuleMap.get("order_2").getActualTables(), hasItems(new DataNode("db0", "t_order_1"), new DataNode("db1", "t_order_1"), new DataNode("db0", "t_order_2"), new DataNode("db1", "t_order_2"),
new DataNode("db0", "t_order_3"), new DataNode("db1", "t_order_3"), new DataNode("db0", "t_order_bak"), new DataNode("db1", "t_order_bak")));
assertThat(tableRuleMap.get("order_3").getActualTables(), hasItems(new DataNode("db0", "table_1"), new DataNode("db1", "table_1"), new DataNode("db0", "table_1_bak"), new DataNode("db1", "table_1_bak"),
new DataNode("db0", "table_2"), new DataNode("db1", "table_2"), new DataNode("db0", "table_2_bak"), new DataNode("db1", "table_2_bak"),
new DataNode("db0", "table_3"), new DataNode("db1", "table_3"), new DataNode("db0", "table_3_bak"), new DataNode("db1", "table_3_bak")));
assertThat(tableRuleMap.get("order_4").getActualTables(), hasItems(new DataNode("db0", "table_1"), new DataNode("db1", "table_1")));
}
@Test
......
......@@ -22,15 +22,14 @@ import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.List;
import com.dangdang.ddframe.rdb.sharding.api.strategy.database.DatabaseShardingStrategy;
import com.dangdang.ddframe.rdb.sharding.api.strategy.table.TableShardingStrategy;
import com.google.common.base.Splitter;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.ToString;
import com.dangdang.ddframe.rdb.sharding.api.strategy.database.DatabaseShardingStrategy;
import com.dangdang.ddframe.rdb.sharding.api.strategy.table.TableShardingStrategy;
import com.google.common.base.Splitter;
/**
* 表规则配置对象.
*
......@@ -98,6 +97,19 @@ public final class TableRule {
return result;
}
/**
* 获取真实数据源.
*
* @return 真实表名称
*/
public Collection<String> getActualDatasourceNames() {
Collection<String> result = new LinkedHashSet<>(actualTables.size());
for (DataNode each : actualTables) {
result.add(each.getDataSourceName());
}
return result;
}
/**
* 根据数据源名称过滤获取真实表名称.
*
......
......@@ -47,7 +47,7 @@ public class ShardingStrategy {
* 根据分片值计算数据源名称集合.
*
*
* @param sqlStatementType
* @param sqlStatementType SQL语句的类型
* @param availableTargetNames 所有的可用数据源名称集合
* @param shardingValues 分库片值集合
* @return 分库后指向的数据源名称集合
......@@ -56,7 +56,7 @@ public class ShardingStrategy {
public Collection<String> doSharding(final SQLStatementType sqlStatementType, final Collection<String> availableTargetNames,
final Collection<ShardingValue<? extends Comparable<?>>> shardingValues) {
if (shardingValues.isEmpty()) {
if (SQLStatementType.INSERT.equals(sqlStatementType)) {
if (SQLStatementType.INSERT.equals(sqlStatementType) && availableTargetNames.size() > 1) {
throw new ShardingJdbcException("INSERT statement must contains sharding value");
} else {
return availableTargetNames;
......
......@@ -86,8 +86,8 @@ public final class SingleTableRouter {
} else {
databaseShardingValues = getShardingValues(strategy.getShardingColumns());
}
logBeforeRoute("database", logicTable, shardingRule.getDataSourceRule().getDataSourceNames(), strategy.getShardingColumns(), databaseShardingValues);
Collection<String> result = new HashSet<>(strategy.doSharding(sqlStatementType, shardingRule.getDataSourceRule().getDataSourceNames(), databaseShardingValues));
logBeforeRoute("database", logicTable, tableRule.get().getActualDatasourceNames(), strategy.getShardingColumns(), databaseShardingValues);
Collection<String> result = new HashSet<>(strategy.doSharding(sqlStatementType, tableRule.get().getActualDatasourceNames(), databaseShardingValues));
logAfterRoute("database", logicTable, result);
Preconditions.checkState(!result.isEmpty(), "no database route info");
return result;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册