Search in sources :

Example 11 with ShardingRule

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());
    }
}
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 12 with ShardingRule

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();
        }
    }
}
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 13 with ShardingRule

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));
}
Also used : 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 ShardingRule

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();
}
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)

Example 15 with ShardingRule

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);
}
Also used : 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) NoneKeyModuloDatabaseShardingAlgorithm(com.dangdang.ddframe.rdb.integrate.fixture.NoneKeyModuloDatabaseShardingAlgorithm) ShardingRule(com.dangdang.ddframe.rdb.sharding.api.rule.ShardingRule)

Aggregations

ShardingRule (com.dangdang.ddframe.rdb.sharding.api.rule.ShardingRule)22 TableRule (com.dangdang.ddframe.rdb.sharding.api.rule.TableRule)17 DataSourceRule (com.dangdang.ddframe.rdb.sharding.api.rule.DataSourceRule)15 ShardingDataSource (com.dangdang.ddframe.rdb.sharding.jdbc.ShardingDataSource)14 DatabaseShardingStrategy (com.dangdang.ddframe.rdb.sharding.api.strategy.database.DatabaseShardingStrategy)13 BindingTableRule (com.dangdang.ddframe.rdb.sharding.api.rule.BindingTableRule)12 TableShardingStrategy (com.dangdang.ddframe.rdb.sharding.api.strategy.table.TableShardingStrategy)10 Test (org.junit.Test)7 DataSource (javax.sql.DataSource)6 HashMap (java.util.HashMap)5 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 YamlShardingDataSource (com.dangdang.ddframe.rdb.sharding.config.yaml.api.YamlShardingDataSource)3 BasicDataSource (org.apache.commons.dbcp.BasicDataSource)3 NoneDatabaseShardingAlgorithm (com.dangdang.ddframe.rdb.sharding.api.strategy.database.NoneDatabaseShardingAlgorithm)2 IncrementIdGenerator (com.dangdang.ddframe.rdb.sharding.id.generator.fixture.IncrementIdGenerator)2 NoneKeyModuloDatabaseShardingAlgorithm (com.dangdang.ddframe.rdb.integrate.fixture.NoneKeyModuloDatabaseShardingAlgorithm)1 SingleKeyDynamicModuloTableShardingAlgorithm (com.dangdang.ddframe.rdb.integrate.fixture.SingleKeyDynamicModuloTableShardingAlgorithm)1