Search in sources :

Example 1 with ShardingValue

use of io.shardingjdbc.core.api.algorithm.sharding.ShardingValue 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 ShardingValue

use of io.shardingjdbc.core.api.algorithm.sharding.ShardingValue 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 ShardingValue

use of io.shardingjdbc.core.api.algorithm.sharding.ShardingValue 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 ShardingValue

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

the class ShardingStrategyTest method assertDoShardingForBetweenSingleKey.

@Test
public void assertDoShardingForBetweenSingleKey() {
    StandardShardingStrategy strategy = new StandardShardingStrategy(new StandardShardingStrategyConfiguration("column", new TestPreciseShardingAlgorithm(), new TestRangeShardingAlgorithm()));
    assertThat(strategy.doSharding(targets, Collections.<ShardingValue>singletonList(new RangeShardingValue<>("logicTable", "column", Range.open("1", "3")))), is((Collection<String>) Sets.newHashSet("1", "2", "3")));
}
Also used : StandardShardingStrategyConfiguration(io.shardingjdbc.core.api.config.strategy.StandardShardingStrategyConfiguration) TestPreciseShardingAlgorithm(io.shardingjdbc.core.api.algorithm.fixture.TestPreciseShardingAlgorithm) TestRangeShardingAlgorithm(io.shardingjdbc.core.api.algorithm.fixture.TestRangeShardingAlgorithm) Collection(java.util.Collection) PreciseShardingValue(io.shardingjdbc.core.api.algorithm.sharding.PreciseShardingValue) ShardingValue(io.shardingjdbc.core.api.algorithm.sharding.ShardingValue) RangeShardingValue(io.shardingjdbc.core.api.algorithm.sharding.RangeShardingValue) StandardShardingStrategy(io.shardingjdbc.core.routing.strategy.standard.StandardShardingStrategy) Test(org.junit.Test)

Example 5 with ShardingValue

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

the class ShardingStrategyTest method assertDoShardingForMultipleKeys.

@Test
public void assertDoShardingForMultipleKeys() {
    ComplexShardingStrategy strategy = new ComplexShardingStrategy(new ComplexShardingStrategyConfiguration("column", new TestComplexKeysShardingAlgorithm()));
    assertThat(strategy.doSharding(targets, Collections.<ShardingValue>singletonList(new PreciseShardingValue<>("logicTable", "column", "1"))), is((Collection<String>) Sets.newHashSet("1", "2", "3")));
}
Also used : ComplexShardingStrategyConfiguration(io.shardingjdbc.core.api.config.strategy.ComplexShardingStrategyConfiguration) Collection(java.util.Collection) TestComplexKeysShardingAlgorithm(io.shardingjdbc.core.api.algorithm.fixture.TestComplexKeysShardingAlgorithm) PreciseShardingValue(io.shardingjdbc.core.api.algorithm.sharding.PreciseShardingValue) ShardingValue(io.shardingjdbc.core.api.algorithm.sharding.ShardingValue) RangeShardingValue(io.shardingjdbc.core.api.algorithm.sharding.RangeShardingValue) ComplexShardingStrategy(io.shardingjdbc.core.routing.strategy.complex.ComplexShardingStrategy) Test(org.junit.Test)

Aggregations

ShardingValue (io.shardingjdbc.core.api.algorithm.sharding.ShardingValue)8 PreciseShardingValue (io.shardingjdbc.core.api.algorithm.sharding.PreciseShardingValue)5 RangeShardingValue (io.shardingjdbc.core.api.algorithm.sharding.RangeShardingValue)5 Test (org.junit.Test)4 ListShardingValue (io.shardingjdbc.core.api.algorithm.sharding.ListShardingValue)3 Collection (java.util.Collection)2 TreeSet (java.util.TreeSet)2 TestComplexKeysShardingAlgorithm (io.shardingjdbc.core.api.algorithm.fixture.TestComplexKeysShardingAlgorithm)1 TestPreciseShardingAlgorithm (io.shardingjdbc.core.api.algorithm.fixture.TestPreciseShardingAlgorithm)1 TestRangeShardingAlgorithm (io.shardingjdbc.core.api.algorithm.fixture.TestRangeShardingAlgorithm)1 ComplexShardingStrategyConfiguration (io.shardingjdbc.core.api.config.strategy.ComplexShardingStrategyConfiguration)1 StandardShardingStrategyConfiguration (io.shardingjdbc.core.api.config.strategy.StandardShardingStrategyConfiguration)1 ShardingKey (io.shardingjdbc.core.hint.ShardingKey)1 SQLExpression (io.shardingjdbc.core.parsing.parser.expression.SQLExpression)1 SQLNumberExpression (io.shardingjdbc.core.parsing.parser.expression.SQLNumberExpression)1 ComplexShardingStrategy (io.shardingjdbc.core.routing.strategy.complex.ComplexShardingStrategy)1 NoneShardingStrategy (io.shardingjdbc.core.routing.strategy.none.NoneShardingStrategy)1 StandardShardingStrategy (io.shardingjdbc.core.routing.strategy.standard.StandardShardingStrategy)1 RoutingResult (io.shardingjdbc.core.routing.type.RoutingResult)1 TableUnit (io.shardingjdbc.core.routing.type.TableUnit)1