Search in sources :

Example 1 with TableRule

use of com.dangdang.ddframe.rdb.sharding.api.rule.TableRule 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 TableRule

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

the class ShardingRuleBuilder method buildTableRules.

private Collection<TableRule> buildTableRules(final DataSourceRule dataSourceRule) {
    Collection<TableRule> result = new ArrayList<>(shardingRuleConfig.getTables().size());
    for (Entry<String, TableRuleConfig> each : shardingRuleConfig.getTables().entrySet()) {
        String logicTable = each.getKey();
        TableRuleConfig tableRuleConfig = each.getValue();
        TableRule.TableRuleBuilder tableRuleBuilder = TableRule.builder(logicTable).dataSourceRule(dataSourceRule).dynamic(tableRuleConfig.isDynamic()).databaseShardingStrategy(buildShardingStrategy(tableRuleConfig.getDatabaseStrategy(), DatabaseShardingStrategy.class)).tableShardingStrategy(buildShardingStrategy(tableRuleConfig.getTableStrategy(), TableShardingStrategy.class));
        if (null != tableRuleConfig.getActualTables()) {
            tableRuleBuilder.actualTables(new InlineParser(tableRuleConfig.getActualTables()).evaluate());
        }
        if (!Strings.isNullOrEmpty(tableRuleConfig.getDataSourceNames())) {
            tableRuleBuilder.dataSourceNames(new InlineParser(tableRuleConfig.getDataSourceNames()).evaluate());
        }
        buildAutoIncrementColumn(tableRuleBuilder, tableRuleConfig);
        result.add(tableRuleBuilder.build());
    }
    return result;
}
Also used : TableRule(com.dangdang.ddframe.rdb.sharding.api.rule.TableRule) BindingTableRule(com.dangdang.ddframe.rdb.sharding.api.rule.BindingTableRule) ArrayList(java.util.ArrayList) InlineParser(com.dangdang.ddframe.rdb.sharding.config.common.internal.parser.InlineParser) TableShardingStrategy(com.dangdang.ddframe.rdb.sharding.api.strategy.table.TableShardingStrategy) BindingTableRuleConfig(com.dangdang.ddframe.rdb.sharding.config.common.api.config.BindingTableRuleConfig) TableRuleConfig(com.dangdang.ddframe.rdb.sharding.config.common.api.config.TableRuleConfig)

Example 3 with TableRule

use of com.dangdang.ddframe.rdb.sharding.api.rule.TableRule 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 4 with TableRule

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

the class ShardingDataSourceTest method createShardingDataSource.

private ShardingDataSource createShardingDataSource(final DataSource dataSource) {
    Map<String, DataSource> dataSourceMap = new HashMap<>(1);
    dataSourceMap.put("ds", dataSource);
    DataSourceRule dataSourceRule = new DataSourceRule(dataSourceMap);
    TableRule tableRule = TableRule.builder("logicTable").actualTables(Arrays.asList("table_0", "table_1", "table_2")).dataSourceRule(dataSourceRule).build();
    return new ShardingDataSource(ShardingRule.builder().dataSourceRule(dataSourceRule).tableRules(Collections.singletonList(tableRule)).build());
}
Also used : TableRule(com.dangdang.ddframe.rdb.sharding.api.rule.TableRule) HashMap(java.util.HashMap) DataSourceRule(com.dangdang.ddframe.rdb.sharding.api.rule.DataSourceRule) DataSource(javax.sql.DataSource)

Example 5 with TableRule

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

the class PreparedSQLRouter method generateId.

private void generateId(final List<Object> parameters) {
    Optional<TableRule> tableRuleOptional = shardingRule.tryFindTableRule(sqlParsedResult.getRouteContext().getTables().iterator().next().getName());
    if (!tableRuleOptional.isPresent()) {
        return;
    }
    TableRule tableRule = tableRuleOptional.get();
    GeneratedKeyContext generatedKeyContext = sqlParsedResult.getGeneratedKeyContext();
    for (String each : generatedKeyContext.getColumns()) {
        Object id = tableRule.generateId(each);
        parameters.add(id);
        generatedKeyContext.putValue(each, id);
    }
}
Also used : TableRule(com.dangdang.ddframe.rdb.sharding.api.rule.TableRule) GeneratedKeyContext(com.dangdang.ddframe.rdb.sharding.parser.result.GeneratedKeyContext)

Aggregations

TableRule (com.dangdang.ddframe.rdb.sharding.api.rule.TableRule)23 DataSourceRule (com.dangdang.ddframe.rdb.sharding.api.rule.DataSourceRule)17 ShardingRule (com.dangdang.ddframe.rdb.sharding.api.rule.ShardingRule)17 BindingTableRule (com.dangdang.ddframe.rdb.sharding.api.rule.BindingTableRule)14 DatabaseShardingStrategy (com.dangdang.ddframe.rdb.sharding.api.strategy.database.DatabaseShardingStrategy)14 TableShardingStrategy (com.dangdang.ddframe.rdb.sharding.api.strategy.table.TableShardingStrategy)13 ShardingDataSource (com.dangdang.ddframe.rdb.sharding.jdbc.ShardingDataSource)12 HashMap (java.util.HashMap)7 DataSource (javax.sql.DataSource)7 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 DruidDataSource (com.alibaba.druid.pool.DruidDataSource)1