Search in sources :

Example 1 with BindingTableRule

use of com.dangdang.ddframe.rdb.sharding.api.rule.BindingTableRule 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"));
}
Also used : TableRule(com.dangdang.ddframe.rdb.sharding.api.rule.TableRule) BindingTableRule(com.dangdang.ddframe.rdb.sharding.api.rule.BindingTableRule) ShardingRuleConfig(com.dangdang.ddframe.rdb.sharding.config.common.api.config.ShardingRuleConfig) DataNode(com.dangdang.ddframe.rdb.sharding.api.rule.DataNode) BindingTableRule(com.dangdang.ddframe.rdb.sharding.api.rule.BindingTableRule) ShardingRule(com.dangdang.ddframe.rdb.sharding.api.rule.ShardingRule) Test(org.junit.Test)

Example 2 with BindingTableRule

use of com.dangdang.ddframe.rdb.sharding.api.rule.BindingTableRule 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);
}
Also used : TableRule(com.dangdang.ddframe.rdb.sharding.api.rule.TableRule) BindingTableRule(com.dangdang.ddframe.rdb.sharding.api.rule.BindingTableRule) DatabaseShardingStrategy(com.dangdang.ddframe.rdb.sharding.api.strategy.database.DatabaseShardingStrategy) ShardingDataSource(com.dangdang.ddframe.rdb.sharding.jdbc.ShardingDataSource) DataSourceRule(com.dangdang.ddframe.rdb.sharding.api.rule.DataSourceRule) BindingTableRule(com.dangdang.ddframe.rdb.sharding.api.rule.BindingTableRule) ShardingRule(com.dangdang.ddframe.rdb.sharding.api.rule.ShardingRule) ModuloDatabaseShardingAlgorithm(com.dangdang.ddframe.rdb.sharding.example.jdbc.masterslave.algorithm.ModuloDatabaseShardingAlgorithm) TableShardingStrategy(com.dangdang.ddframe.rdb.sharding.api.strategy.table.TableShardingStrategy) ModuloTableShardingAlgorithm(com.dangdang.ddframe.rdb.sharding.example.jdbc.masterslave.algorithm.ModuloTableShardingAlgorithm)

Example 3 with BindingTableRule

use of com.dangdang.ddframe.rdb.sharding.api.rule.BindingTableRule in project sharding-jdbc by dangdangdotcom.

the class DynamicShardingBothHelper method getShardingDataSource.

public static ShardingDataSource getShardingDataSource(final Map<String, DataSource> dataSourceMap) {
    DataSourceRule dataSourceRule = new DataSourceRule(dataSourceMap);
    TableRule orderTableRule = TableRule.builder("t_order").dynamic(true).dataSourceRule(dataSourceRule).build();
    TableRule orderItemTableRule = TableRule.builder("t_order_item").dynamic(true).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 SingleKeyModuloDatabaseShardingAlgorithm())).tableShardingStrategy(new TableShardingStrategy("order_id", new SingleKeyDynamicModuloTableShardingAlgorithm("t_order_"))).build();
    return new ShardingDataSource(shardingRule);
}
Also used : TableRule(com.dangdang.ddframe.rdb.sharding.api.rule.TableRule) BindingTableRule(com.dangdang.ddframe.rdb.sharding.api.rule.BindingTableRule) DatabaseShardingStrategy(com.dangdang.ddframe.rdb.sharding.api.strategy.database.DatabaseShardingStrategy) ShardingDataSource(com.dangdang.ddframe.rdb.sharding.jdbc.ShardingDataSource) DataSourceRule(com.dangdang.ddframe.rdb.sharding.api.rule.DataSourceRule) SingleKeyDynamicModuloTableShardingAlgorithm(com.dangdang.ddframe.rdb.integrate.fixture.SingleKeyDynamicModuloTableShardingAlgorithm) BindingTableRule(com.dangdang.ddframe.rdb.sharding.api.rule.BindingTableRule) ShardingRule(com.dangdang.ddframe.rdb.sharding.api.rule.ShardingRule) TableShardingStrategy(com.dangdang.ddframe.rdb.sharding.api.strategy.table.TableShardingStrategy) SingleKeyModuloDatabaseShardingAlgorithm(com.dangdang.ddframe.rdb.integrate.fixture.SingleKeyModuloDatabaseShardingAlgorithm)

Example 4 with BindingTableRule

use of com.dangdang.ddframe.rdb.sharding.api.rule.BindingTableRule in project sharding-jdbc by dangdangdotcom.

the class StaticShardingBothHelper method getShardingDataSource.

public static ShardingDataSource getShardingDataSource(final Map<String, DataSource> dataSourceMap) {
    DataSourceRule dataSourceRule = new DataSourceRule(dataSourceMap, "dataSource_dbtbl_0");
    TableRule orderTableRule = TableRule.builder("t_order").actualTables(Arrays.asList("t_order_0", "t_order_1", "t_order_2", "t_order_3", "t_order_4", "t_order_5", "t_order_6", "t_order_7", "t_order_8", "t_order_9")).dataSourceRule(dataSourceRule).build();
    TableRule orderItemTableRule = TableRule.builder("t_order_item").actualTables(Arrays.asList("t_order_item_0", "t_order_item_1", "t_order_item_2", "t_order_item_3", "t_order_item_4", "t_order_item_5", "t_order_item_6", "t_order_item_7", "t_order_item_8", "t_order_item_9")).dataSourceRule(dataSourceRule).build();
    TableRule configRule = TableRule.builder("t_config").dataSourceRule(dataSourceRule).build();
    ShardingRule shardingRule = ShardingRule.builder().dataSourceRule(dataSourceRule).tableRules(Arrays.asList(orderTableRule, orderItemTableRule, configRule)).bindingTableRules(Collections.singletonList(new BindingTableRule(Arrays.asList(orderTableRule, orderItemTableRule)))).databaseShardingStrategy(new DatabaseShardingStrategy("user_id", new SingleKeyModuloDatabaseShardingAlgorithm())).tableShardingStrategy(new TableShardingStrategy("order_id", new SingleKeyModuloTableShardingAlgorithm())).build();
    return new ShardingDataSource(shardingRule);
}
Also used : TableRule(com.dangdang.ddframe.rdb.sharding.api.rule.TableRule) BindingTableRule(com.dangdang.ddframe.rdb.sharding.api.rule.BindingTableRule) DatabaseShardingStrategy(com.dangdang.ddframe.rdb.sharding.api.strategy.database.DatabaseShardingStrategy) ShardingDataSource(com.dangdang.ddframe.rdb.sharding.jdbc.ShardingDataSource) DataSourceRule(com.dangdang.ddframe.rdb.sharding.api.rule.DataSourceRule) BindingTableRule(com.dangdang.ddframe.rdb.sharding.api.rule.BindingTableRule) ShardingRule(com.dangdang.ddframe.rdb.sharding.api.rule.ShardingRule) TableShardingStrategy(com.dangdang.ddframe.rdb.sharding.api.strategy.table.TableShardingStrategy) SingleKeyModuloTableShardingAlgorithm(com.dangdang.ddframe.rdb.integrate.fixture.SingleKeyModuloTableShardingAlgorithm) SingleKeyModuloDatabaseShardingAlgorithm(com.dangdang.ddframe.rdb.integrate.fixture.SingleKeyModuloDatabaseShardingAlgorithm)

Example 5 with BindingTableRule

use of com.dangdang.ddframe.rdb.sharding.api.rule.BindingTableRule in project sharding-jdbc by dangdangdotcom.

the class AbstractShardingNullableDBUnitTest method getShardingDataSource.

protected final ShardingDataSource getShardingDataSource() {
    if (null != shardingDataSource && !isShutdown) {
        return shardingDataSource;
    }
    isShutdown = false;
    DataSourceRule dataSourceRule = new DataSourceRule(createDataSourceMap("dataSource_%s"));
    TableRule orderTableRule = TableRule.builder("t_order").dataSourceRule(dataSourceRule).build();
    ShardingRule shardingRule = ShardingRule.builder().dataSourceRule(dataSourceRule).tableRules(Collections.singletonList(orderTableRule)).bindingTableRules(Collections.singletonList(new BindingTableRule(Collections.singletonList(orderTableRule)))).databaseShardingStrategy(new DatabaseShardingStrategy(Collections.singletonList("user_id"), new MultipleKeysModuloDatabaseShardingAlgorithm())).tableShardingStrategy(new TableShardingStrategy(Collections.singletonList("order_id"), new NoneTableShardingAlgorithm())).build();
    shardingDataSource = new ShardingDataSource(shardingRule);
    return shardingDataSource;
}
Also used : TableRule(com.dangdang.ddframe.rdb.sharding.api.rule.TableRule) BindingTableRule(com.dangdang.ddframe.rdb.sharding.api.rule.BindingTableRule) DatabaseShardingStrategy(com.dangdang.ddframe.rdb.sharding.api.strategy.database.DatabaseShardingStrategy) ShardingDataSource(com.dangdang.ddframe.rdb.sharding.jdbc.ShardingDataSource) DataSourceRule(com.dangdang.ddframe.rdb.sharding.api.rule.DataSourceRule) BindingTableRule(com.dangdang.ddframe.rdb.sharding.api.rule.BindingTableRule) ShardingRule(com.dangdang.ddframe.rdb.sharding.api.rule.ShardingRule) TableShardingStrategy(com.dangdang.ddframe.rdb.sharding.api.strategy.table.TableShardingStrategy) MultipleKeysModuloDatabaseShardingAlgorithm(com.dangdang.ddframe.rdb.integrate.fixture.MultipleKeysModuloDatabaseShardingAlgorithm) NoneTableShardingAlgorithm(com.dangdang.ddframe.rdb.sharding.api.strategy.table.NoneTableShardingAlgorithm)

Aggregations

BindingTableRule (com.dangdang.ddframe.rdb.sharding.api.rule.BindingTableRule)12 TableRule (com.dangdang.ddframe.rdb.sharding.api.rule.TableRule)12 DataSourceRule (com.dangdang.ddframe.rdb.sharding.api.rule.DataSourceRule)11 ShardingRule (com.dangdang.ddframe.rdb.sharding.api.rule.ShardingRule)11 DatabaseShardingStrategy (com.dangdang.ddframe.rdb.sharding.api.strategy.database.DatabaseShardingStrategy)11 TableShardingStrategy (com.dangdang.ddframe.rdb.sharding.api.strategy.table.TableShardingStrategy)11 ShardingDataSource (com.dangdang.ddframe.rdb.sharding.jdbc.ShardingDataSource)9 MultipleKeysModuloDatabaseShardingAlgorithm (com.dangdang.ddframe.rdb.integrate.fixture.MultipleKeysModuloDatabaseShardingAlgorithm)3 SingleKeyModuloDatabaseShardingAlgorithm (com.dangdang.ddframe.rdb.integrate.fixture.SingleKeyModuloDatabaseShardingAlgorithm)3 SingleKeyModuloTableShardingAlgorithm (com.dangdang.ddframe.rdb.integrate.fixture.SingleKeyModuloTableShardingAlgorithm)3 NoneTableShardingAlgorithm (com.dangdang.ddframe.rdb.sharding.api.strategy.table.NoneTableShardingAlgorithm)3 HashMap (java.util.HashMap)2 DataSource (javax.sql.DataSource)2 SingleKeyDynamicModuloTableShardingAlgorithm (com.dangdang.ddframe.rdb.integrate.fixture.SingleKeyDynamicModuloTableShardingAlgorithm)1 DataNode (com.dangdang.ddframe.rdb.sharding.api.rule.DataNode)1 NoneDatabaseShardingAlgorithm (com.dangdang.ddframe.rdb.sharding.api.strategy.database.NoneDatabaseShardingAlgorithm)1 ShardingRuleConfig (com.dangdang.ddframe.rdb.sharding.config.common.api.config.ShardingRuleConfig)1 ModuloDatabaseShardingAlgorithm (com.dangdang.ddframe.rdb.sharding.example.jdbc.algorithm.ModuloDatabaseShardingAlgorithm)1 ModuloTableShardingAlgorithm (com.dangdang.ddframe.rdb.sharding.example.jdbc.algorithm.ModuloTableShardingAlgorithm)1 ModuloDatabaseShardingAlgorithm (com.dangdang.ddframe.rdb.sharding.example.jdbc.masterslave.algorithm.ModuloDatabaseShardingAlgorithm)1