Search in sources :

Example 1 with ShardingValue

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));
}
Also used : ShardingStrategy(com.dangdang.ddframe.rdb.sharding.router.strategy.ShardingStrategy) ShardingValue(com.dangdang.ddframe.rdb.sharding.api.ShardingValue) Test(org.junit.Test)

Example 2 with ShardingValue

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);
}
Also used : ShardingKey(com.dangdang.ddframe.rdb.sharding.hint.ShardingKey) ShardingValue(com.dangdang.ddframe.rdb.sharding.api.ShardingValue)

Example 3 with ShardingValue

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;
}
Also used : TableShardingStrategy(com.dangdang.ddframe.rdb.sharding.api.strategy.table.TableShardingStrategy) ShardingValue(com.dangdang.ddframe.rdb.sharding.api.ShardingValue)

Example 4 with ShardingValue

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;
}
Also used : DatabaseShardingStrategy(com.dangdang.ddframe.rdb.sharding.api.strategy.database.DatabaseShardingStrategy) ShardingValue(com.dangdang.ddframe.rdb.sharding.api.ShardingValue) HashSet(java.util.HashSet)

Example 5 with ShardingValue

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());
}
Also used : ShardingValue(com.dangdang.ddframe.rdb.sharding.api.ShardingValue)

Aggregations

ShardingValue (com.dangdang.ddframe.rdb.sharding.api.ShardingValue)5 DatabaseShardingStrategy (com.dangdang.ddframe.rdb.sharding.api.strategy.database.DatabaseShardingStrategy)1 TableShardingStrategy (com.dangdang.ddframe.rdb.sharding.api.strategy.table.TableShardingStrategy)1 ShardingKey (com.dangdang.ddframe.rdb.sharding.hint.ShardingKey)1 ShardingStrategy (com.dangdang.ddframe.rdb.sharding.router.strategy.ShardingStrategy)1 HashSet (java.util.HashSet)1 Test (org.junit.Test)1