Search in sources :

Example 11 with DataSourceRule

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

Example 12 with DataSourceRule

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

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

Example 14 with DataSourceRule

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

the class AbstractShardingDataBasesOnlyHintDBUnitTest method initDataSource.

ShardingDataSource initDataSource() {
    DataSourceRule dataSourceRule = new DataSourceRule(createDataSourceMap("dataSource_%s"));
    TableRule orderTableRule = TableRule.builder("t_order").dataSourceRule(dataSourceRule).build();
    TableRule orderItemTableRule = TableRule.builder("t_order_item").dataSourceRule(dataSourceRule).build();
    ShardingRule shardingRule = ShardingRule.builder().dataSourceRule(dataSourceRule).tableRules(Lists.newArrayList(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();
    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) TableShardingStrategy(com.dangdang.ddframe.rdb.sharding.api.strategy.table.TableShardingStrategy) MultipleKeysModuloDatabaseShardingAlgorithm(com.dangdang.ddframe.rdb.integrate.fixture.MultipleKeysModuloDatabaseShardingAlgorithm) NoneTableShardingAlgorithm(com.dangdang.ddframe.rdb.sharding.api.strategy.table.NoneTableShardingAlgorithm)

Example 15 with DataSourceRule

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

the class AbstractShardingTablesOnlyDBUnitTest 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").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();
    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 NoneDatabaseShardingAlgorithm())).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) 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) NoneDatabaseShardingAlgorithm(com.dangdang.ddframe.rdb.sharding.api.strategy.database.NoneDatabaseShardingAlgorithm) TableShardingStrategy(com.dangdang.ddframe.rdb.sharding.api.strategy.table.TableShardingStrategy) SingleKeyModuloTableShardingAlgorithm(com.dangdang.ddframe.rdb.integrate.fixture.SingleKeyModuloTableShardingAlgorithm)

Aggregations

DataSourceRule (com.dangdang.ddframe.rdb.sharding.api.rule.DataSourceRule)20 TableRule (com.dangdang.ddframe.rdb.sharding.api.rule.TableRule)17 DatabaseShardingStrategy (com.dangdang.ddframe.rdb.sharding.api.strategy.database.DatabaseShardingStrategy)16 ShardingRule (com.dangdang.ddframe.rdb.sharding.api.rule.ShardingRule)15 BindingTableRule (com.dangdang.ddframe.rdb.sharding.api.rule.BindingTableRule)12 TableShardingStrategy (com.dangdang.ddframe.rdb.sharding.api.strategy.table.TableShardingStrategy)12 ShardingDataSource (com.dangdang.ddframe.rdb.sharding.jdbc.ShardingDataSource)11 HashMap (java.util.HashMap)7 DataSource (javax.sql.DataSource)7 NoneTableShardingAlgorithm (com.dangdang.ddframe.rdb.sharding.api.strategy.table.NoneTableShardingAlgorithm)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 NoneDatabaseShardingAlgorithm (com.dangdang.ddframe.rdb.sharding.api.strategy.database.NoneDatabaseShardingAlgorithm)3 Before (org.junit.Before)3 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 ModuloDatabaseShardingAlgorithm (com.dangdang.ddframe.rdb.sharding.example.jdbc.algorithm.ModuloDatabaseShardingAlgorithm)1 ModuloTableShardingAlgorithm (com.dangdang.ddframe.rdb.sharding.example.jdbc.algorithm.ModuloTableShardingAlgorithm)1