use of com.dangdang.ddframe.rdb.sharding.api.rule.ShardingRule in project sharding-jdbc by dangdangdotcom.
the class YamlShardingDataSourceTest method assertAll.
@Test
public void assertAll() throws IOException, NoSuchFieldException, IllegalAccessException, URISyntaxException {
ShardingRule shardingRule = getShardingRule("/config/config-all.yaml");
assertThat(shardingRule.getTableRules().size(), is(3));
assertThat(shardingRule.getBindingTableRules().size(), is(1));
assertThat(Arrays.asList(shardingRule.getTableRules().toArray()), hasItems(shardingRule.getBindingTableRules().iterator().next().getTableRules().toArray()));
assertThat(shardingRule.getDataSourceRule().getDefaultDataSourceName(), is("db0"));
}
use of com.dangdang.ddframe.rdb.sharding.api.rule.ShardingRule 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.ShardingRule in project sharding-jdbc by dangdangdotcom.
the class ShardingConnectionTest method setUp.
@Before
public void setUp() {
Map<String, DataSource> dataSourceMap = new HashMap<>(1);
dataSourceMap.put(DS_NAME, MASTER_SLAVE_DATA_SOURCE);
DataSourceRule dataSourceRule = new DataSourceRule(dataSourceMap);
ShardingRule rule = new ShardingRule.ShardingRuleBuilder().dataSourceRule(dataSourceRule).tableRules(Collections.singleton(new TableRule.TableRuleBuilder("test").dataSourceRule(dataSourceRule).build())).build();
ShardingContext sc = new ShardingContext(rule, null, null);
connection = new ShardingConnection(sc);
}
use of com.dangdang.ddframe.rdb.sharding.api.rule.ShardingRule 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.ShardingRule in project sharding-jdbc by dangdangdotcom.
the class AbstractSoftTransactionIntegrationTest method prepareEnv.
private void prepareEnv() throws SQLException {
DataSourceRule dataSourceRule = new DataSourceRule(createDataSourceMap());
TableRule tableRule = TableRule.builder("transaction_test").dataSourceRule(dataSourceRule).build();
ShardingRule shardingRule = ShardingRule.builder().dataSourceRule(dataSourceRule).tableRules(Lists.newArrayList(tableRule)).build();
shardingDataSource = new ShardingDataSource(shardingRule);
createTable(shardingDataSource);
transactionDataSource = createTransactionLogDataSource();
}
Aggregations