Search in sources :

Example 6 with ShardingKey

use of io.shardingjdbc.core.hint.ShardingKey in project sharding-jdbc by shardingjdbc.

the class HintManager method addTableShardingValue.

/**
 * Add sharding value for table.
 *
 * @param logicTable logic table name
 * @param shardingColumn sharding column name
 * @param operator sharding operator
 * @param values sharding value
 */
public void addTableShardingValue(final String logicTable, final String shardingColumn, final ShardingOperator operator, final Comparable<?>... values) {
    shardingHint = true;
    tableShardingValues.put(new ShardingKey(logicTable, shardingColumn), getShardingValue(logicTable, shardingColumn, operator, values));
}
Also used : ShardingKey(io.shardingjdbc.core.hint.ShardingKey)

Example 7 with ShardingKey

use of io.shardingjdbc.core.hint.ShardingKey in project sharding-jdbc by shardingjdbc.

the class HintManager method addDatabaseShardingValue.

/**
 * Add sharding value for database.
 *
 * @param logicTable logic table name
 * @param shardingColumn sharding column name
 * @param operator sharding operator
 * @param values sharding value
 */
public void addDatabaseShardingValue(final String logicTable, final String shardingColumn, final ShardingOperator operator, final Comparable<?>... values) {
    shardingHint = true;
    databaseShardingValues.put(new ShardingKey(logicTable, shardingColumn), getShardingValue(logicTable, shardingColumn, operator, values));
}
Also used : ShardingKey(io.shardingjdbc.core.hint.ShardingKey)

Example 8 with ShardingKey

use of io.shardingjdbc.core.hint.ShardingKey in project sharding-jdbc by shardingjdbc.

the class DatabaseHintRoutingEngine method route.

@Override
public RoutingResult route() {
    Optional<ShardingValue> shardingValue = HintManagerHolder.getDatabaseShardingValue(new ShardingKey(HintManagerHolder.DB_TABLE_NAME, HintManagerHolder.DB_COLUMN_NAME));
    Preconditions.checkState(shardingValue.isPresent());
    log.debug("Before database sharding only db:{} sharding values: {}", dataSourceNames, shardingValue.get());
    Collection<String> routingDataSources;
    routingDataSources = databaseShardingStrategy.doSharding(dataSourceNames, Collections.singletonList(shardingValue.get()));
    Preconditions.checkState(!routingDataSources.isEmpty(), "no database route info");
    log.debug("After database sharding only result: {}", routingDataSources);
    RoutingResult result = new RoutingResult();
    for (String each : routingDataSources) {
        result.getTableUnits().getTableUnits().add(new TableUnit(each, "", ""));
    }
    return result;
}
Also used : RoutingResult(io.shardingjdbc.core.routing.type.RoutingResult) ShardingKey(io.shardingjdbc.core.hint.ShardingKey) ShardingValue(io.shardingjdbc.core.api.algorithm.sharding.ShardingValue) TableUnit(io.shardingjdbc.core.routing.type.TableUnit)

Example 9 with ShardingKey

use of io.shardingjdbc.core.hint.ShardingKey in project sharding-jdbc by shardingjdbc.

the class HintManagerTest method assertAddTableShardingValueForEquals.

@SuppressWarnings("unchecked")
@Test
public void assertAddTableShardingValueForEquals() {
    try (HintManager hintManager = HintManager.getInstance()) {
        hintManager.addTableShardingValue("logicTable", "shardingColumn", 1);
        ShardingKey shardingKey = new ShardingKey("logicTable", "shardingColumn");
        assertTrue(HintManagerHolder.getTableShardingValue(shardingKey).isPresent());
        assertThat(hintManager.getTableShardingValue(shardingKey).getColumnName(), is("shardingColumn"));
        assertThat(((ListShardingValue<? extends Comparable>) hintManager.getTableShardingValue(shardingKey)).getValues().iterator().next(), is((Comparable) 1));
    }
}
Also used : ShardingKey(io.shardingjdbc.core.hint.ShardingKey) Test(org.junit.Test)

Aggregations

ShardingKey (io.shardingjdbc.core.hint.ShardingKey)9 Test (org.junit.Test)6 ListShardingValue (io.shardingjdbc.core.api.algorithm.sharding.ListShardingValue)2 RangeShardingValue (io.shardingjdbc.core.api.algorithm.sharding.RangeShardingValue)2 ShardingValue (io.shardingjdbc.core.api.algorithm.sharding.ShardingValue)1 RoutingResult (io.shardingjdbc.core.routing.type.RoutingResult)1 TableUnit (io.shardingjdbc.core.routing.type.TableUnit)1