use of com.dangdang.ddframe.rdb.sharding.api.ShardingValue in project sharding-jdbc by dangdangdotcom.
the class ShardingStrategyTest method assertDoStaticShardingWithoutShardingColumns.
@Test
public void assertDoStaticShardingWithoutShardingColumns() {
ShardingStrategy strategy = new ShardingStrategy(Collections.singletonList("column"), null);
assertThat(strategy.doStaticSharding(SQLStatementType.SELECT, targets, Collections.<ShardingValue<?>>emptyList()), is(targets));
}
use of com.dangdang.ddframe.rdb.sharding.api.ShardingValue in project sharding-jdbc by dangdangdotcom.
the class DatabaseRouter method route.
/**
* 根据Hint路由到库.
*
* @return 库路由结果
*/
public DatabaseRoutingResult route() {
Optional<ShardingValue<?>> shardingValueOptional = HintManagerHolder.getDatabaseShardingValue(new ShardingKey(HintManagerHolder.DB_TABLE_NAME, HintManagerHolder.DB_COLUMN_NAME));
Preconditions.checkState(shardingValueOptional.isPresent());
log.debug("Before database sharding only db:{} sharding values: {}", dataSourceRule.getDataSourceNames(), shardingValueOptional.get());
Collection<String> routedResult = databaseShardingStrategy.doStaticSharding(sqlStatementType, dataSourceRule.getDataSourceNames(), Collections.<ShardingValue<?>>singleton(shardingValueOptional.get()));
Preconditions.checkState(!routedResult.isEmpty(), "no database route info");
log.debug("After database sharding only result: {}", routedResult);
return new DatabaseRoutingResult(routedResult);
}
use of com.dangdang.ddframe.rdb.sharding.api.ShardingValue 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;
}
use of com.dangdang.ddframe.rdb.sharding.api.ShardingValue 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;
}
use of com.dangdang.ddframe.rdb.sharding.api.ShardingValue in project sharding-jdbc by dangdangdotcom.
the class ShardingStrategy method doSharding.
@SuppressWarnings({ "unchecked", "rawtypes" })
private Collection<String> doSharding(final Collection<ShardingValue<?>> shardingValues, final Collection<String> availableTargetNames) {
if (shardingAlgorithm instanceof NoneKeyShardingAlgorithm) {
return Collections.singletonList(((NoneKeyShardingAlgorithm) shardingAlgorithm).doSharding(availableTargetNames, shardingValues.iterator().next()));
}
if (shardingAlgorithm instanceof SingleKeyShardingAlgorithm) {
SingleKeyShardingAlgorithm<?> singleKeyShardingAlgorithm = (SingleKeyShardingAlgorithm<?>) shardingAlgorithm;
ShardingValue shardingValue = shardingValues.iterator().next();
switch(shardingValue.getType()) {
case SINGLE:
return Collections.singletonList(singleKeyShardingAlgorithm.doEqualSharding(availableTargetNames, shardingValue));
case LIST:
return singleKeyShardingAlgorithm.doInSharding(availableTargetNames, shardingValue);
case RANGE:
return singleKeyShardingAlgorithm.doBetweenSharding(availableTargetNames, shardingValue);
default:
throw new UnsupportedOperationException(shardingValue.getType().getClass().getName());
}
}
if (shardingAlgorithm instanceof MultipleKeysShardingAlgorithm) {
return ((MultipleKeysShardingAlgorithm) shardingAlgorithm).doSharding(availableTargetNames, shardingValues);
}
throw new UnsupportedOperationException(shardingAlgorithm.getClass().getName());
}
Aggregations