Search in sources :

Example 11 with TableRule

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

the class AbstractShardingMasterSlaveDBUnitTest method getShardingDataSource.

protected final ShardingDataSource getShardingDataSource() {
    if (null != shardingDataSource && !isShutdown) {
        return shardingDataSource;
    }
    isShutdown = false;
    Map<String, DataSource> masterSlaveDataSourceMap = createDataSourceMap("dataSource_%s");
    MasterSlaveDataSource masterSlaveDs0 = new MasterSlaveDataSource("ms_0", masterSlaveDataSourceMap.get("dataSource_master_0"), Collections.singletonList(masterSlaveDataSourceMap.get("dataSource_slave_0")));
    MasterSlaveDataSource masterSlaveDs1 = new MasterSlaveDataSource("ms_1", masterSlaveDataSourceMap.get("dataSource_master_1"), Collections.singletonList(masterSlaveDataSourceMap.get("dataSource_slave_1")));
    MasterSlaveDataSource masterSlaveDs2 = new MasterSlaveDataSource("ms_2", masterSlaveDataSourceMap.get("dataSource_master_2"), Collections.singletonList(masterSlaveDataSourceMap.get("dataSource_slave_2")));
    MasterSlaveDataSource masterSlaveDs3 = new MasterSlaveDataSource("ms_3", masterSlaveDataSourceMap.get("dataSource_master_3"), Collections.singletonList(masterSlaveDataSourceMap.get("dataSource_slave_3")));
    MasterSlaveDataSource masterSlaveDs4 = new MasterSlaveDataSource("ms_4", masterSlaveDataSourceMap.get("dataSource_master_4"), Collections.singletonList(masterSlaveDataSourceMap.get("dataSource_slave_4")));
    MasterSlaveDataSource masterSlaveDs5 = new MasterSlaveDataSource("ms_5", masterSlaveDataSourceMap.get("dataSource_master_5"), Collections.singletonList(masterSlaveDataSourceMap.get("dataSource_slave_5")));
    MasterSlaveDataSource masterSlaveDs6 = new MasterSlaveDataSource("ms_6", masterSlaveDataSourceMap.get("dataSource_master_6"), Collections.singletonList(masterSlaveDataSourceMap.get("dataSource_slave_6")));
    MasterSlaveDataSource masterSlaveDs7 = new MasterSlaveDataSource("ms_7", masterSlaveDataSourceMap.get("dataSource_master_7"), Collections.singletonList(masterSlaveDataSourceMap.get("dataSource_slave_7")));
    MasterSlaveDataSource masterSlaveDs8 = new MasterSlaveDataSource("ms_8", masterSlaveDataSourceMap.get("dataSource_master_8"), Collections.singletonList(masterSlaveDataSourceMap.get("dataSource_slave_8")));
    MasterSlaveDataSource masterSlaveDs9 = new MasterSlaveDataSource("ms_9", masterSlaveDataSourceMap.get("dataSource_master_9"), Collections.singletonList(masterSlaveDataSourceMap.get("dataSource_slave_9")));
    Map<String, DataSource> dataSourceMap = new HashMap<>(10);
    dataSourceMap.put("ms_0", masterSlaveDs0);
    dataSourceMap.put("ms_1", masterSlaveDs1);
    dataSourceMap.put("ms_2", masterSlaveDs2);
    dataSourceMap.put("ms_3", masterSlaveDs3);
    dataSourceMap.put("ms_4", masterSlaveDs4);
    dataSourceMap.put("ms_5", masterSlaveDs5);
    dataSourceMap.put("ms_6", masterSlaveDs6);
    dataSourceMap.put("ms_7", masterSlaveDs7);
    dataSourceMap.put("ms_8", masterSlaveDs8);
    dataSourceMap.put("ms_9", masterSlaveDs9);
    DataSourceRule dataSourceRule = new DataSourceRule(dataSourceMap);
    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();
    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) HashMap(java.util.HashMap) ShardingDataSource(com.dangdang.ddframe.rdb.sharding.jdbc.ShardingDataSource) DataSourceRule(com.dangdang.ddframe.rdb.sharding.api.rule.DataSourceRule) MasterSlaveDataSource(com.dangdang.ddframe.rdb.sharding.jdbc.MasterSlaveDataSource) BindingTableRule(com.dangdang.ddframe.rdb.sharding.api.rule.BindingTableRule) TableShardingStrategy(com.dangdang.ddframe.rdb.sharding.api.strategy.table.TableShardingStrategy) SingleKeyModuloTableShardingAlgorithm(com.dangdang.ddframe.rdb.integrate.fixture.SingleKeyModuloTableShardingAlgorithm) MasterSlaveDataSource(com.dangdang.ddframe.rdb.sharding.jdbc.MasterSlaveDataSource) ShardingDataSource(com.dangdang.ddframe.rdb.sharding.jdbc.ShardingDataSource) DataSource(javax.sql.DataSource) ShardingRule(com.dangdang.ddframe.rdb.sharding.api.rule.ShardingRule) SingleKeyModuloDatabaseShardingAlgorithm(com.dangdang.ddframe.rdb.integrate.fixture.SingleKeyModuloDatabaseShardingAlgorithm)

Example 12 with TableRule

use of com.dangdang.ddframe.rdb.sharding.api.rule.TableRule 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();
}
Also used : TableRule(com.dangdang.ddframe.rdb.sharding.api.rule.TableRule) ShardingDataSource(com.dangdang.ddframe.rdb.sharding.jdbc.ShardingDataSource) DataSourceRule(com.dangdang.ddframe.rdb.sharding.api.rule.DataSourceRule) ShardingRule(com.dangdang.ddframe.rdb.sharding.api.rule.ShardingRule)

Example 13 with TableRule

use of com.dangdang.ddframe.rdb.sharding.api.rule.TableRule 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());
    }
}
Also used : TableRule(com.dangdang.ddframe.rdb.sharding.api.rule.TableRule) HashMap(java.util.HashMap) ShardingRule(com.dangdang.ddframe.rdb.sharding.api.rule.ShardingRule) YamlShardingDataSource(com.dangdang.ddframe.rdb.sharding.config.yaml.api.YamlShardingDataSource) BasicDataSource(org.apache.commons.dbcp.BasicDataSource) ShardingDataSource(com.dangdang.ddframe.rdb.sharding.jdbc.ShardingDataSource) DataSource(javax.sql.DataSource) Test(org.junit.Test)

Example 14 with TableRule

use of com.dangdang.ddframe.rdb.sharding.api.rule.TableRule 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();
        }
    }
}
Also used : TableRule(com.dangdang.ddframe.rdb.sharding.api.rule.TableRule) DynamicDataNode(com.dangdang.ddframe.rdb.sharding.api.rule.DynamicDataNode) HashMap(java.util.HashMap) ShardingRule(com.dangdang.ddframe.rdb.sharding.api.rule.ShardingRule) YamlShardingDataSource(com.dangdang.ddframe.rdb.sharding.config.yaml.api.YamlShardingDataSource) BasicDataSource(org.apache.commons.dbcp.BasicDataSource) ShardingDataSource(com.dangdang.ddframe.rdb.sharding.jdbc.ShardingDataSource) DataSource(javax.sql.DataSource) Test(org.junit.Test)

Example 15 with TableRule

use of com.dangdang.ddframe.rdb.sharding.api.rule.TableRule 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();
}
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) DataSourceRule(com.dangdang.ddframe.rdb.sharding.api.rule.DataSourceRule) ShardingRule(com.dangdang.ddframe.rdb.sharding.api.rule.ShardingRule) IdGenerator(com.dangdang.ddframe.rdb.sharding.id.generator.IdGenerator)

Aggregations

TableRule (com.dangdang.ddframe.rdb.sharding.api.rule.TableRule)22 DataSourceRule (com.dangdang.ddframe.rdb.sharding.api.rule.DataSourceRule)16 ShardingRule (com.dangdang.ddframe.rdb.sharding.api.rule.ShardingRule)16 BindingTableRule (com.dangdang.ddframe.rdb.sharding.api.rule.BindingTableRule)14 DatabaseShardingStrategy (com.dangdang.ddframe.rdb.sharding.api.strategy.database.DatabaseShardingStrategy)13 TableShardingStrategy (com.dangdang.ddframe.rdb.sharding.api.strategy.table.TableShardingStrategy)12 ShardingDataSource (com.dangdang.ddframe.rdb.sharding.jdbc.ShardingDataSource)12 HashMap (java.util.HashMap)6 DataSource (javax.sql.DataSource)6 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 Test (org.junit.Test)3 NoneDatabaseShardingAlgorithm (com.dangdang.ddframe.rdb.sharding.api.strategy.database.NoneDatabaseShardingAlgorithm)2 YamlShardingDataSource (com.dangdang.ddframe.rdb.sharding.config.yaml.api.YamlShardingDataSource)2 IncrementIdGenerator (com.dangdang.ddframe.rdb.sharding.id.generator.fixture.IncrementIdGenerator)2 GeneratedKeyContext (com.dangdang.ddframe.rdb.sharding.parser.result.GeneratedKeyContext)2 BasicDataSource (org.apache.commons.dbcp.BasicDataSource)2 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)1