Search in sources :

Example 6 with TableShardingStrategy

use of com.dangdang.ddframe.rdb.sharding.api.strategy.table.TableShardingStrategy in project sharding-jdbc by dangdangdotcom.

the class TableRuleTest method assertTableRuleWithTableShardingStrategyWithoutDataNode.

@Test
public void assertTableRuleWithTableShardingStrategyWithoutDataNode() {
    TableRule actual = TableRule.builder("logicTable").actualTables(Arrays.asList("table_0", "table_1", "table_2")).dataSourceRule(createDataSourceRule()).tableShardingStrategy(new TableShardingStrategy("", new NoneTableShardingAlgorithm())).build();
    assertActualTable(actual);
    assertNull(actual.getDatabaseShardingStrategy());
    assertNotNull(actual.getTableShardingStrategy());
}
Also used : TableShardingStrategy(com.dangdang.ddframe.rdb.sharding.api.strategy.table.TableShardingStrategy) NoneTableShardingAlgorithm(com.dangdang.ddframe.rdb.sharding.api.strategy.table.NoneTableShardingAlgorithm) Test(org.junit.Test)

Example 7 with TableShardingStrategy

use of com.dangdang.ddframe.rdb.sharding.api.strategy.table.TableShardingStrategy 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 8 with TableShardingStrategy

use of com.dangdang.ddframe.rdb.sharding.api.strategy.table.TableShardingStrategy in project sharding-jdbc by dangdangdotcom.

the class SingleTableRouter method routeTables.

private Collection<String> routeTables(final Collection<String> routedDataSources) {
    TableShardingStrategy strategy = shardingRule.getTableShardingStrategy(tableRule);
    List<ShardingValue<?>> shardingValues;
    if (HintManagerHolder.isUseShardingHint()) {
        shardingValues = getTableShardingValuesFromHint(strategy.getShardingColumns());
    } else {
        shardingValues = getShardingValues(strategy.getShardingColumns());
    }
    logBeforeRoute("table", logicTable, tableRule.getActualTables(), strategy.getShardingColumns(), shardingValues);
    Collection<String> result;
    if (tableRule.isDynamic()) {
        result = new HashSet<>(strategy.doDynamicSharding(shardingValues));
    } else {
        result = new HashSet<>(strategy.doStaticSharding(sqlStatementType, tableRule.getActualTableNames(routedDataSources), shardingValues));
    }
    logAfterRoute("table", logicTable, result);
    Preconditions.checkState(!result.isEmpty(), "no table route info");
    return result;
}
Also used : TableShardingStrategy(com.dangdang.ddframe.rdb.sharding.api.strategy.table.TableShardingStrategy) ShardingValue(com.dangdang.ddframe.rdb.sharding.api.ShardingValue)

Example 9 with TableShardingStrategy

use of com.dangdang.ddframe.rdb.sharding.api.strategy.table.TableShardingStrategy 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 10 with TableShardingStrategy

use of com.dangdang.ddframe.rdb.sharding.api.strategy.table.TableShardingStrategy 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)

Aggregations

TableShardingStrategy (com.dangdang.ddframe.rdb.sharding.api.strategy.table.TableShardingStrategy)21 DatabaseShardingStrategy (com.dangdang.ddframe.rdb.sharding.api.strategy.database.DatabaseShardingStrategy)14 BindingTableRule (com.dangdang.ddframe.rdb.sharding.api.rule.BindingTableRule)12 DataSourceRule (com.dangdang.ddframe.rdb.sharding.api.rule.DataSourceRule)12 TableRule (com.dangdang.ddframe.rdb.sharding.api.rule.TableRule)12 ShardingRule (com.dangdang.ddframe.rdb.sharding.api.rule.ShardingRule)10 ShardingDataSource (com.dangdang.ddframe.rdb.sharding.jdbc.ShardingDataSource)9 NoneTableShardingAlgorithm (com.dangdang.ddframe.rdb.sharding.api.strategy.table.NoneTableShardingAlgorithm)7 Test (org.junit.Test)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 NoneDatabaseShardingAlgorithm (com.dangdang.ddframe.rdb.sharding.api.strategy.database.NoneDatabaseShardingAlgorithm)3 HashMap (java.util.HashMap)3 DataSource (javax.sql.DataSource)3 SingleKeyDynamicModuloTableShardingAlgorithm (com.dangdang.ddframe.rdb.integrate.fixture.SingleKeyDynamicModuloTableShardingAlgorithm)1 ShardingValue (com.dangdang.ddframe.rdb.sharding.api.ShardingValue)1 MultipleKeysDatabaseShardingAlgorithm (com.dangdang.ddframe.rdb.sharding.api.strategy.database.MultipleKeysDatabaseShardingAlgorithm)1 SingleKeyDatabaseShardingAlgorithm (com.dangdang.ddframe.rdb.sharding.api.strategy.database.SingleKeyDatabaseShardingAlgorithm)1 MultipleKeysTableShardingAlgorithm (com.dangdang.ddframe.rdb.sharding.api.strategy.table.MultipleKeysTableShardingAlgorithm)1