use of com.dangdang.ddframe.rdb.sharding.api.rule.ShardingRule in project sharding-jdbc by dangdangdotcom.
the class YamlShardingDataSourceTest method assertBindingError.
@Test(expected = IllegalArgumentException.class)
public void assertBindingError() throws IOException, NoSuchFieldException, IllegalAccessException, URISyntaxException {
Map<String, DataSource> dataSourceMap = new HashMap<>(1);
dataSourceMap.put("ds", createDataSource());
ShardingRule shardingRule = getShardingRule(dataSourceMap, "/config/config-bindingError.yaml");
for (TableRule tableRule : shardingRule.getBindingTableRules().iterator().next().getTableRules()) {
log.info(tableRule.toString());
}
}
use of com.dangdang.ddframe.rdb.sharding.api.rule.ShardingRule in project sharding-jdbc by dangdangdotcom.
the class YamlShardingDataSourceTest method assertDynamic.
@Test
public void assertDynamic() throws IOException, NoSuchFieldException, IllegalAccessException, URISyntaxException {
Map<String, DataSource> dataSourceMap = new HashMap<>(1);
dataSourceMap.put("ds", createDataSource());
ShardingRule shardingRule = getShardingRule(dataSourceMap, "/config/config-dynamic.yaml");
int i = 0;
for (TableRule each : shardingRule.getTableRules()) {
i++;
assertThat(each.getActualTables().size(), is(2));
assertThat(each.getActualTables(), hasItem(new DynamicDataNode("db0")));
assertThat(each.getActualTables(), hasItem(new DynamicDataNode("db1")));
switch(i) {
case 1:
assertThat(each.getLogicTable(), is("config"));
break;
case 2:
assertThat(each.getLogicTable(), is("t_order"));
break;
case 3:
assertThat(each.getLogicTable(), is("t_order_item"));
break;
default:
fail();
}
}
}
use of com.dangdang.ddframe.rdb.sharding.api.rule.ShardingRule in project sharding-jdbc by dangdangdotcom.
the class YamlShardingDataSourceTest method assertMin.
@Test
public void assertMin() throws IOException, NoSuchFieldException, IllegalAccessException, URISyntaxException {
Map<String, DataSource> dataSourceMap = new HashMap<>(1);
dataSourceMap.put("ds", createDataSource());
ShardingRule shardingRule = getShardingRule(dataSourceMap, "/config/config-min.yaml");
assertThat(shardingRule.getTableRules().size(), is(1));
}
use of com.dangdang.ddframe.rdb.sharding.api.rule.ShardingRule in project sharding-jdbc by dangdangdotcom.
the class ShardingRuleBuilder method build.
/**
* 构建分片规则.
*
* @return 分片规则对象
*/
public ShardingRule build() {
DataSourceRule dataSourceRule = buildDataSourceRule();
Collection<TableRule> tableRules = buildTableRules(dataSourceRule);
com.dangdang.ddframe.rdb.sharding.api.rule.ShardingRule.ShardingRuleBuilder shardingRuleBuilder = ShardingRule.builder().dataSourceRule(dataSourceRule);
if (!Strings.isNullOrEmpty(shardingRuleConfig.getIdGeneratorClass())) {
shardingRuleBuilder.idGenerator(loadClass(shardingRuleConfig.getIdGeneratorClass(), IdGenerator.class));
}
return shardingRuleBuilder.tableRules(tableRules).bindingTableRules(buildBindingTableRules(tableRules)).databaseShardingStrategy(buildShardingStrategy(shardingRuleConfig.getDefaultDatabaseStrategy(), DatabaseShardingStrategy.class)).tableShardingStrategy(buildShardingStrategy(shardingRuleConfig.getDefaultTableStrategy(), TableShardingStrategy.class)).build();
}
use of com.dangdang.ddframe.rdb.sharding.api.rule.ShardingRule in project sharding-jdbc by dangdangdotcom.
the class AbstractRoutingDatabaseOnlyTest method initDataSource.
protected ShardingDataSource initDataSource() {
DataSourceRule dataSourceRule = new DataSourceRule(createDataSourceMap("dataSource_%s"));
ShardingRule shardingRule = ShardingRule.builder().dataSourceRule(dataSourceRule).databaseShardingStrategy(new DatabaseShardingStrategy(new NoneKeyModuloDatabaseShardingAlgorithm())).build();
return new ShardingDataSource(shardingRule);
}
Aggregations