Search in sources :

Example 11 with TableShardingStrategy

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

the class ShardingRule method getTableShardingStrategy.

/**
     * 获取表分片策略.
     * 
     * <p>
     * 根据表规则配置对象获取分片策略, 如果获取不到则获取默认分片策略.
     * </p>
     * 
     * @param tableRule 表规则配置对象
     * @return 表分片策略
     */
public TableShardingStrategy getTableShardingStrategy(final TableRule tableRule) {
    TableShardingStrategy result = tableRule.getTableShardingStrategy();
    if (null == result) {
        result = tableShardingStrategy;
    }
    Preconditions.checkNotNull(result, "no table sharding strategy");
    return result;
}
Also used : TableShardingStrategy(com.dangdang.ddframe.rdb.sharding.api.strategy.table.TableShardingStrategy)

Example 12 with TableShardingStrategy

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

the class ShardingRuleBuilder method buildShardingAlgorithmClassName.

@SuppressWarnings("unchecked")
private <T extends ShardingStrategy> T buildShardingAlgorithmClassName(final List<String> shardingColumns, final String algorithmClassName, final Class<T> returnClass) {
    ShardingAlgorithm shardingAlgorithm;
    try {
        shardingAlgorithm = (ShardingAlgorithm) Class.forName(algorithmClassName).newInstance();
    } catch (final InstantiationException | IllegalAccessException | ClassNotFoundException ex) {
        throw new IllegalArgumentException(ex);
    }
    Preconditions.checkState(shardingAlgorithm instanceof SingleKeyShardingAlgorithm || shardingAlgorithm instanceof MultipleKeysShardingAlgorithm, "Sharding-JDBC: algorithmClassName is illegal");
    if (shardingAlgorithm instanceof SingleKeyShardingAlgorithm) {
        Preconditions.checkArgument(1 == shardingColumns.size(), "Sharding-JDBC: SingleKeyShardingAlgorithm must have only ONE sharding column");
        return returnClass.isAssignableFrom(DatabaseShardingStrategy.class) ? (T) new DatabaseShardingStrategy(shardingColumns.get(0), (SingleKeyDatabaseShardingAlgorithm<?>) shardingAlgorithm) : (T) new TableShardingStrategy(shardingColumns.get(0), (SingleKeyTableShardingAlgorithm<?>) shardingAlgorithm);
    }
    return returnClass.isAssignableFrom(DatabaseShardingStrategy.class) ? (T) new DatabaseShardingStrategy(shardingColumns, (MultipleKeysDatabaseShardingAlgorithm) shardingAlgorithm) : (T) new TableShardingStrategy(shardingColumns, (MultipleKeysTableShardingAlgorithm) shardingAlgorithm);
}
Also used : SingleKeyDatabaseShardingAlgorithm(com.dangdang.ddframe.rdb.sharding.api.strategy.database.SingleKeyDatabaseShardingAlgorithm) ShardingAlgorithm(com.dangdang.ddframe.rdb.sharding.router.strategy.ShardingAlgorithm) MultipleKeysDatabaseShardingAlgorithm(com.dangdang.ddframe.rdb.sharding.api.strategy.database.MultipleKeysDatabaseShardingAlgorithm) MultipleKeysTableShardingAlgorithm(com.dangdang.ddframe.rdb.sharding.api.strategy.table.MultipleKeysTableShardingAlgorithm) ClosureTableShardingAlgorithm(com.dangdang.ddframe.rdb.sharding.config.common.internal.algorithm.ClosureTableShardingAlgorithm) SingleKeyTableShardingAlgorithm(com.dangdang.ddframe.rdb.sharding.api.strategy.table.SingleKeyTableShardingAlgorithm) SingleKeyShardingAlgorithm(com.dangdang.ddframe.rdb.sharding.router.strategy.SingleKeyShardingAlgorithm) MultipleKeysShardingAlgorithm(com.dangdang.ddframe.rdb.sharding.router.strategy.MultipleKeysShardingAlgorithm) ClosureDatabaseShardingAlgorithm(com.dangdang.ddframe.rdb.sharding.config.common.internal.algorithm.ClosureDatabaseShardingAlgorithm) DatabaseShardingStrategy(com.dangdang.ddframe.rdb.sharding.api.strategy.database.DatabaseShardingStrategy) MultipleKeysShardingAlgorithm(com.dangdang.ddframe.rdb.sharding.router.strategy.MultipleKeysShardingAlgorithm) TableShardingStrategy(com.dangdang.ddframe.rdb.sharding.api.strategy.table.TableShardingStrategy) SingleKeyShardingAlgorithm(com.dangdang.ddframe.rdb.sharding.router.strategy.SingleKeyShardingAlgorithm)

Example 13 with TableShardingStrategy

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

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

Example 15 with TableShardingStrategy

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

the class TableRuleTest method assertTableRuleForDynamicWithTableStrategy.

@Test
public void assertTableRuleForDynamicWithTableStrategy() {
    TableRule actual = TableRule.builder("logicTable").dynamic(true).dataSourceRule(createDataSourceRule()).tableShardingStrategy(new TableShardingStrategy("", new NoneTableShardingAlgorithm())).build();
    assertDynamicTable(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)

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