use of com.dangdang.ddframe.rdb.sharding.api.strategy.database.DatabaseShardingStrategy 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();
}
use of com.dangdang.ddframe.rdb.sharding.api.strategy.database.DatabaseShardingStrategy 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);
}
use of com.dangdang.ddframe.rdb.sharding.api.strategy.database.DatabaseShardingStrategy 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);
}
use of com.dangdang.ddframe.rdb.sharding.api.strategy.database.DatabaseShardingStrategy 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);
}
use of com.dangdang.ddframe.rdb.sharding.api.strategy.database.DatabaseShardingStrategy 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;
}
Aggregations