Search in sources :

Example 21 with DatabaseShardingStrategy

use of com.dangdang.ddframe.rdb.sharding.api.strategy.database.DatabaseShardingStrategy 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.algorithm.ModuloDatabaseShardingAlgorithm) TableShardingStrategy(com.dangdang.ddframe.rdb.sharding.api.strategy.table.TableShardingStrategy) ModuloTableShardingAlgorithm(com.dangdang.ddframe.rdb.sharding.example.jdbc.algorithm.ModuloTableShardingAlgorithm)

Example 22 with DatabaseShardingStrategy

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

the class AbstractShardingDataBasesOnlyDBUnitTest method getShardingDataSource.

protected final ShardingDataSource getShardingDataSource() {
    if (null != shardingDataSource && !isShutdown) {
        return shardingDataSource;
    }
    isShutdown = false;
    DataSourceRule dataSourceRule = new DataSourceRule(createDataSourceMap("dataSource_%s"));
    TableRule orderTableRule = TableRule.builder("t_order").dataSourceRule(dataSourceRule).autoIncrementColumns("user_id").tableIdGenerator(IncrementIdGenerator.class).build();
    TableRule orderItemTableRule = TableRule.builder("t_order_item").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(Collections.singletonList("user_id"), new MultipleKeysModuloDatabaseShardingAlgorithm())).tableShardingStrategy(new TableShardingStrategy(Collections.singletonList("order_id"), new NoneTableShardingAlgorithm())).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) 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) TableShardingStrategy(com.dangdang.ddframe.rdb.sharding.api.strategy.table.TableShardingStrategy) IncrementIdGenerator(com.dangdang.ddframe.rdb.sharding.id.generator.fixture.IncrementIdGenerator) MultipleKeysModuloDatabaseShardingAlgorithm(com.dangdang.ddframe.rdb.integrate.fixture.MultipleKeysModuloDatabaseShardingAlgorithm) NoneTableShardingAlgorithm(com.dangdang.ddframe.rdb.sharding.api.strategy.table.NoneTableShardingAlgorithm)

Example 23 with DatabaseShardingStrategy

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

the class SingleTableRouter method createTableRuleWithDefaultDataSource.

private TableRule createTableRuleWithDefaultDataSource(final String logicTable, final DataSourceRule defaultDataSourceRule) {
    Map<String, DataSource> defaultDataSourceMap = new HashMap<>(1);
    defaultDataSourceMap.put(defaultDataSourceRule.getDefaultDataSourceName(), defaultDataSourceRule.getDefaultDataSource().get());
    return TableRule.builder(logicTable).dataSourceRule(new DataSourceRule(defaultDataSourceMap)).databaseShardingStrategy(new DatabaseShardingStrategy("", new NoneDatabaseShardingAlgorithm())).tableShardingStrategy(new TableShardingStrategy("", new NoneTableShardingAlgorithm())).build();
}
Also used : DatabaseShardingStrategy(com.dangdang.ddframe.rdb.sharding.api.strategy.database.DatabaseShardingStrategy) HashMap(java.util.HashMap) DataSourceRule(com.dangdang.ddframe.rdb.sharding.api.rule.DataSourceRule) NoneDatabaseShardingAlgorithm(com.dangdang.ddframe.rdb.sharding.api.strategy.database.NoneDatabaseShardingAlgorithm) TableShardingStrategy(com.dangdang.ddframe.rdb.sharding.api.strategy.table.TableShardingStrategy) NoneTableShardingAlgorithm(com.dangdang.ddframe.rdb.sharding.api.strategy.table.NoneTableShardingAlgorithm) DataSource(javax.sql.DataSource)

Example 24 with DatabaseShardingStrategy

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

the class SingleTableRouter method routeDataSources.

private Collection<String> routeDataSources() {
    DatabaseShardingStrategy strategy = shardingRule.getDatabaseShardingStrategy(tableRule);
    List<ShardingValue<?>> shardingValues;
    if (HintManagerHolder.isUseShardingHint()) {
        shardingValues = getDatabaseShardingValuesFromHint(strategy.getShardingColumns());
    } else {
        shardingValues = getShardingValues(strategy.getShardingColumns());
    }
    logBeforeRoute("database", logicTable, tableRule.getActualDatasourceNames(), strategy.getShardingColumns(), shardingValues);
    Collection<String> result = new HashSet<>(strategy.doStaticSharding(sqlStatementType, tableRule.getActualDatasourceNames(), shardingValues));
    logAfterRoute("database", logicTable, result);
    Preconditions.checkState(!result.isEmpty(), "no database route info");
    return result;
}
Also used : DatabaseShardingStrategy(com.dangdang.ddframe.rdb.sharding.api.strategy.database.DatabaseShardingStrategy) ShardingValue(com.dangdang.ddframe.rdb.sharding.api.ShardingValue) HashSet(java.util.HashSet)

Aggregations

DatabaseShardingStrategy (com.dangdang.ddframe.rdb.sharding.api.strategy.database.DatabaseShardingStrategy)24 DataSourceRule (com.dangdang.ddframe.rdb.sharding.api.rule.DataSourceRule)16 TableShardingStrategy (com.dangdang.ddframe.rdb.sharding.api.strategy.table.TableShardingStrategy)14 ShardingRule (com.dangdang.ddframe.rdb.sharding.api.rule.ShardingRule)13 TableRule (com.dangdang.ddframe.rdb.sharding.api.rule.TableRule)13 BindingTableRule (com.dangdang.ddframe.rdb.sharding.api.rule.BindingTableRule)12 ShardingDataSource (com.dangdang.ddframe.rdb.sharding.jdbc.ShardingDataSource)10 NoneDatabaseShardingAlgorithm (com.dangdang.ddframe.rdb.sharding.api.strategy.database.NoneDatabaseShardingAlgorithm)6 NoneTableShardingAlgorithm (com.dangdang.ddframe.rdb.sharding.api.strategy.table.NoneTableShardingAlgorithm)5 Test (org.junit.Test)5 HashMap (java.util.HashMap)4 DataSource (javax.sql.DataSource)4 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 IncrementIdGenerator (com.dangdang.ddframe.rdb.sharding.id.generator.fixture.IncrementIdGenerator)2 Before (org.junit.Before)2 NoneKeyModuloDatabaseShardingAlgorithm (com.dangdang.ddframe.rdb.integrate.fixture.NoneKeyModuloDatabaseShardingAlgorithm)1 SingleKeyDynamicModuloTableShardingAlgorithm (com.dangdang.ddframe.rdb.integrate.fixture.SingleKeyDynamicModuloTableShardingAlgorithm)1 ShardingValue (com.dangdang.ddframe.rdb.sharding.api.ShardingValue)1