use of com.dangdang.ddframe.rdb.sharding.api.rule.TableRule in project sharding-jdbc by dangdangdotcom.
the class ShardingRuleBuilderTest method assertBuildSuccess.
@Test
public void assertBuildSuccess() {
ShardingRuleConfig shardingRuleConfig = new ShardingRuleConfig();
shardingRuleConfig.setDataSource(createDataSourceMap());
shardingRuleConfig.setDefaultDataSourceName("ds_0");
shardingRuleConfig.setTables(createTableRuleConfigMap());
shardingRuleConfig.setIdGeneratorClass("com.dangdang.ddframe.rdb.sharding.config.common.fixture.IncrementIdGenerator");
shardingRuleConfig.setBindingTables(Collections.singletonList(createBindingTableRule("t_order", "t_order_item")));
shardingRuleConfig.setDefaultDatabaseStrategy(getDatabaseStrategyConfig(SingleAlgorithm.class.getName()));
shardingRuleConfig.setDefaultTableStrategy(getTableStrategyConfigForAlgorithmClass());
ShardingRule actual = new ShardingRuleBuilder(shardingRuleConfig).build();
assertThat(actual.getDataSourceRule().getDataSourceNames().size(), is(2));
assertThat(actual.getDataSourceRule().getDataSourceNames(), hasItem("ds_0"));
assertThat(actual.getDataSourceRule().getDataSourceNames(), hasItem("ds_1"));
assertThat(actual.getTableRules().size(), is(3));
for (TableRule each : actual.getTableRules()) {
String logicTable = each.getLogicTable();
switch(logicTable) {
case "t_order":
assertFalse(each.isDynamic());
assertThat(each.getActualTables().size(), is(4));
assertThat(each.getActualTables(), hasItem(new DataNode("ds_0", "t_order_0")));
assertThat(each.getActualTables(), hasItem(new DataNode("ds_0", "t_order_0")));
assertThat(each.getActualTables(), hasItem(new DataNode("ds_1", "t_order_0")));
assertThat(each.getActualTables(), hasItem(new DataNode("ds_1", "t_order_1")));
break;
case "t_order_item":
assertFalse(each.isDynamic());
assertThat(each.getActualTables().size(), is(4));
assertThat(each.getActualTables(), hasItem(new DataNode("ds_0", "t_order_item_0")));
assertThat(each.getActualTables(), hasItem(new DataNode("ds_0", "t_order_item_0")));
assertThat(each.getActualTables(), hasItem(new DataNode("ds_1", "t_order_item_0")));
assertThat(each.getActualTables(), hasItem(new DataNode("ds_1", "t_order_item_1")));
break;
case "t_log":
assertTrue(each.isDynamic());
assertThat(each.getActualTables().size(), is(2));
assertThat(each.getActualTables(), hasItem(new DataNode("ds_0", "SHARDING_JDBC DYNAMIC_TABLE_PLACEHOLDER")));
assertThat(each.getActualTables(), hasItem(new DataNode("ds_1", "SHARDING_JDBC DYNAMIC_TABLE_PLACEHOLDER")));
break;
default:
fail();
}
}
assertThat(actual.getBindingTableRules().size(), is(1));
BindingTableRule bindingTableRule = actual.getBindingTableRules().iterator().next();
assertTrue(bindingTableRule.hasLogicTable("t_order"));
assertTrue(bindingTableRule.hasLogicTable("t_order_item"));
assertFalse(bindingTableRule.hasLogicTable("t_log"));
assertThat(actual.getDatabaseShardingStrategy().getShardingColumns().size(), is(1));
assertThat(actual.getDatabaseShardingStrategy().getShardingColumns().iterator().next(), is("uid"));
assertThat(actual.getTableShardingStrategy().getShardingColumns().size(), is(1));
assertThat(actual.getTableShardingStrategy().getShardingColumns().iterator().next(), is("oid"));
}
use of com.dangdang.ddframe.rdb.sharding.api.rule.TableRule in project sharding-jdbc by dangdangdotcom.
the class ShardingRuleBuilder method buildTableRules.
private Collection<TableRule> buildTableRules(final DataSourceRule dataSourceRule) {
Collection<TableRule> result = new ArrayList<>(shardingRuleConfig.getTables().size());
for (Entry<String, TableRuleConfig> each : shardingRuleConfig.getTables().entrySet()) {
String logicTable = each.getKey();
TableRuleConfig tableRuleConfig = each.getValue();
TableRule.TableRuleBuilder tableRuleBuilder = TableRule.builder(logicTable).dataSourceRule(dataSourceRule).dynamic(tableRuleConfig.isDynamic()).databaseShardingStrategy(buildShardingStrategy(tableRuleConfig.getDatabaseStrategy(), DatabaseShardingStrategy.class)).tableShardingStrategy(buildShardingStrategy(tableRuleConfig.getTableStrategy(), TableShardingStrategy.class));
if (null != tableRuleConfig.getActualTables()) {
tableRuleBuilder.actualTables(new InlineParser(tableRuleConfig.getActualTables()).evaluate());
}
if (!Strings.isNullOrEmpty(tableRuleConfig.getDataSourceNames())) {
tableRuleBuilder.dataSourceNames(new InlineParser(tableRuleConfig.getDataSourceNames()).evaluate());
}
buildAutoIncrementColumn(tableRuleBuilder, tableRuleConfig);
result.add(tableRuleBuilder.build());
}
return result;
}
use of com.dangdang.ddframe.rdb.sharding.api.rule.TableRule in project sharding-jdbc by dangdangdotcom.
the class Main method getShardingDataSource.
private static ShardingDataSource getShardingDataSource() {
DataSourceRule dataSourceRule = new DataSourceRule(createDataSourceMap());
TableRule orderTableRule = TableRule.builder("t_order").actualTables(Arrays.asList("t_order_0", "t_order_1")).dataSourceRule(dataSourceRule).build();
TableRule orderItemTableRule = TableRule.builder("t_order_item").actualTables(Arrays.asList("t_order_item_0", "t_order_item_1")).dataSourceRule(dataSourceRule).build();
ShardingRule shardingRule = ShardingRule.builder().dataSourceRule(dataSourceRule).tableRules(Arrays.asList(orderTableRule, orderItemTableRule)).bindingTableRules(Collections.singletonList(new BindingTableRule(Arrays.asList(orderTableRule, orderItemTableRule)))).databaseShardingStrategy(new DatabaseShardingStrategy("user_id", new ModuloDatabaseShardingAlgorithm())).tableShardingStrategy(new TableShardingStrategy("order_id", new ModuloTableShardingAlgorithm())).build();
return new ShardingDataSource(shardingRule);
}
use of com.dangdang.ddframe.rdb.sharding.api.rule.TableRule in project sharding-jdbc by dangdangdotcom.
the class ShardingDataSourceTest method createShardingDataSource.
private ShardingDataSource createShardingDataSource(final DataSource dataSource) {
Map<String, DataSource> dataSourceMap = new HashMap<>(1);
dataSourceMap.put("ds", dataSource);
DataSourceRule dataSourceRule = new DataSourceRule(dataSourceMap);
TableRule tableRule = TableRule.builder("logicTable").actualTables(Arrays.asList("table_0", "table_1", "table_2")).dataSourceRule(dataSourceRule).build();
return new ShardingDataSource(ShardingRule.builder().dataSourceRule(dataSourceRule).tableRules(Collections.singletonList(tableRule)).build());
}
use of com.dangdang.ddframe.rdb.sharding.api.rule.TableRule in project sharding-jdbc by dangdangdotcom.
the class PreparedSQLRouter method generateId.
private void generateId(final List<Object> parameters) {
Optional<TableRule> tableRuleOptional = shardingRule.tryFindTableRule(sqlParsedResult.getRouteContext().getTables().iterator().next().getName());
if (!tableRuleOptional.isPresent()) {
return;
}
TableRule tableRule = tableRuleOptional.get();
GeneratedKeyContext generatedKeyContext = sqlParsedResult.getGeneratedKeyContext();
for (String each : generatedKeyContext.getColumns()) {
Object id = tableRule.generateId(each);
parameters.add(id);
generatedKeyContext.putValue(each, id);
}
}
Aggregations