Search in sources :

Example 6 with ShardingRule

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"));
}
Also used : ShardingRule(com.dangdang.ddframe.rdb.sharding.api.rule.ShardingRule) Test(org.junit.Test)

Example 7 with ShardingRule

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);
}
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 8 with 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);
}
Also used : TableRule(com.dangdang.ddframe.rdb.sharding.api.rule.TableRule) HashMap(java.util.HashMap) DataSourceRule(com.dangdang.ddframe.rdb.sharding.api.rule.DataSourceRule) ShardingRule(com.dangdang.ddframe.rdb.sharding.api.rule.ShardingRule) TestDataSource(com.dangdang.ddframe.rdb.sharding.fixture.TestDataSource) DataSource(javax.sql.DataSource) Before(org.junit.Before)

Example 9 with ShardingRule

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"));
}
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 10 with ShardingRule

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

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