Search in sources :

Example 1 with ListShardingValue

use of io.shardingjdbc.core.api.algorithm.sharding.ListShardingValue in project sharding-jdbc by shardingjdbc.

the class InlineShardingStrategy method doSharding.

@Override
public Collection<String> doSharding(final Collection<String> availableTargetNames, final Collection<ShardingValue> shardingValues) {
    ShardingValue shardingValue = shardingValues.iterator().next();
    Preconditions.checkState(shardingValue instanceof ListShardingValue, "Inline strategy cannot support range sharding.");
    Collection<String> shardingResult = doSharding((ListShardingValue) shardingValue);
    Collection<String> result = new TreeSet<>(String.CASE_INSENSITIVE_ORDER);
    result.addAll(shardingResult);
    return result;
}
Also used : TreeSet(java.util.TreeSet) ListShardingValue(io.shardingjdbc.core.api.algorithm.sharding.ListShardingValue) PreciseShardingValue(io.shardingjdbc.core.api.algorithm.sharding.PreciseShardingValue) ShardingValue(io.shardingjdbc.core.api.algorithm.sharding.ShardingValue) ListShardingValue(io.shardingjdbc.core.api.algorithm.sharding.ListShardingValue)

Example 2 with ListShardingValue

use of io.shardingjdbc.core.api.algorithm.sharding.ListShardingValue in project sharding-jdbc by shardingjdbc.

the class StandardShardingStrategy method doSharding.

@Override
public Collection<String> doSharding(final Collection<String> availableTargetNames, final Collection<ShardingValue> shardingValues) {
    ShardingValue shardingValue = shardingValues.iterator().next();
    Collection<String> shardingResult = shardingValue instanceof ListShardingValue ? doSharding(availableTargetNames, (ListShardingValue) shardingValue) : doSharding(availableTargetNames, (RangeShardingValue) shardingValue);
    Collection<String> result = new TreeSet<>(String.CASE_INSENSITIVE_ORDER);
    result.addAll(shardingResult);
    return result;
}
Also used : RangeShardingValue(io.shardingjdbc.core.api.algorithm.sharding.RangeShardingValue) TreeSet(java.util.TreeSet) ListShardingValue(io.shardingjdbc.core.api.algorithm.sharding.ListShardingValue) PreciseShardingValue(io.shardingjdbc.core.api.algorithm.sharding.PreciseShardingValue) ShardingValue(io.shardingjdbc.core.api.algorithm.sharding.ShardingValue) RangeShardingValue(io.shardingjdbc.core.api.algorithm.sharding.RangeShardingValue) ListShardingValue(io.shardingjdbc.core.api.algorithm.sharding.ListShardingValue)

Example 3 with ListShardingValue

use of io.shardingjdbc.core.api.algorithm.sharding.ListShardingValue in project sharding-jdbc by shardingjdbc.

the class ConditionTest method assertGetShardingValue.

@Test
public void assertGetShardingValue() {
    Condition condition = new Condition(new Column("test", "test"), new SQLNumberExpression(1));
    ShardingValue shardingValue = condition.getShardingValue(Collections.emptyList());
    assertThat((Integer) ((ListShardingValue) shardingValue).getValues().iterator().next(), is(1));
    condition = new Condition(new Column("test", "test"), Arrays.<SQLExpression>asList(new SQLNumberExpression(1), new SQLNumberExpression(2)));
    shardingValue = condition.getShardingValue(Collections.emptyList());
    Iterator<?> iterator = ((ListShardingValue) shardingValue).getValues().iterator();
    assertThat((Integer) iterator.next(), is(1));
    assertThat((Integer) iterator.next(), is(2));
    condition = new Condition(new Column("test", "test"), new SQLNumberExpression(1), new SQLNumberExpression(2));
    shardingValue = condition.getShardingValue(Collections.emptyList());
    assertThat((Integer) ((RangeShardingValue) shardingValue).getValueRange().lowerEndpoint(), is(1));
    assertThat((Integer) ((RangeShardingValue) shardingValue).getValueRange().upperEndpoint(), is(2));
}
Also used : SQLNumberExpression(io.shardingjdbc.core.parsing.parser.expression.SQLNumberExpression) SQLExpression(io.shardingjdbc.core.parsing.parser.expression.SQLExpression) ListShardingValue(io.shardingjdbc.core.api.algorithm.sharding.ListShardingValue) RangeShardingValue(io.shardingjdbc.core.api.algorithm.sharding.RangeShardingValue) ShardingValue(io.shardingjdbc.core.api.algorithm.sharding.ShardingValue) ListShardingValue(io.shardingjdbc.core.api.algorithm.sharding.ListShardingValue) Test(org.junit.Test)

Example 4 with ListShardingValue

use of io.shardingjdbc.core.api.algorithm.sharding.ListShardingValue in project sharding-jdbc by shardingjdbc.

the class HintManagerTest method assertAddTableShardingValueForIn.

@Test
public void assertAddTableShardingValueForIn() {
    try (HintManager hintManager = HintManager.getInstance()) {
        hintManager.addTableShardingValue("logicTable", "shardingColumn", ShardingOperator.IN, 1, 3, 5);
        ShardingKey shardingKey = new ShardingKey("logicTable", "shardingColumn");
        assertTrue(HintManagerHolder.getTableShardingValue(shardingKey).isPresent());
        assertThat(hintManager.getTableShardingValue(shardingKey).getColumnName(), is("shardingColumn"));
        assertThat(((ListShardingValue) hintManager.getTableShardingValue(shardingKey)).getValues().size(), is(3));
    }
}
Also used : ShardingKey(io.shardingjdbc.core.hint.ShardingKey) ListShardingValue(io.shardingjdbc.core.api.algorithm.sharding.ListShardingValue) Test(org.junit.Test)

Example 5 with ListShardingValue

use of io.shardingjdbc.core.api.algorithm.sharding.ListShardingValue in project sharding-jdbc by shardingjdbc.

the class HintManagerTest method assertAddDatabaseShardingValueForIn.

@Test
public void assertAddDatabaseShardingValueForIn() {
    try (HintManager hintManager = HintManager.getInstance()) {
        hintManager.addDatabaseShardingValue("logicTable", "shardingColumn", ShardingOperator.IN, 1, 3, 5);
        ShardingKey shardingKey = new ShardingKey("logicTable", "shardingColumn");
        assertTrue(HintManagerHolder.getDatabaseShardingValue(shardingKey).isPresent());
        assertThat(hintManager.getDatabaseShardingValue(shardingKey).getColumnName(), is("shardingColumn"));
        assertThat(((ListShardingValue) hintManager.getDatabaseShardingValue(shardingKey)).getValues().size(), is(3));
    }
}
Also used : ShardingKey(io.shardingjdbc.core.hint.ShardingKey) ListShardingValue(io.shardingjdbc.core.api.algorithm.sharding.ListShardingValue) Test(org.junit.Test)

Aggregations

ListShardingValue (io.shardingjdbc.core.api.algorithm.sharding.ListShardingValue)5 ShardingValue (io.shardingjdbc.core.api.algorithm.sharding.ShardingValue)3 Test (org.junit.Test)3 PreciseShardingValue (io.shardingjdbc.core.api.algorithm.sharding.PreciseShardingValue)2 RangeShardingValue (io.shardingjdbc.core.api.algorithm.sharding.RangeShardingValue)2 ShardingKey (io.shardingjdbc.core.hint.ShardingKey)2 TreeSet (java.util.TreeSet)2 SQLExpression (io.shardingjdbc.core.parsing.parser.expression.SQLExpression)1 SQLNumberExpression (io.shardingjdbc.core.parsing.parser.expression.SQLNumberExpression)1