Search in sources :

Example 1 with RangeShardingValue

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

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

the class UpdateStatementParserTest method assertUpdateStatementWithoutParameter.

private void assertUpdateStatementWithoutParameter(final DMLStatement updateStatement) {
    assertThat(updateStatement.getTables().find("TABLE_XXX").get().getName(), is("TABLE_XXX"));
    assertThat(updateStatement.getTables().find("TABLE_XXX").get().getAlias().get(), is("xxx"));
    Condition condition1 = updateStatement.getConditions().find(new Column("field1", "TABLE_XXX")).get();
    assertThat(condition1.getOperator(), is(ShardingOperator.EQUAL));
    assertThat(((ListShardingValue<? extends Comparable>) condition1.getShardingValue(Collections.emptyList())).getValues().iterator().next(), is((Comparable) 1));
    Condition condition2 = updateStatement.getConditions().find(new Column("field2", "TABLE_XXX")).get();
    assertThat(condition2.getOperator(), is(ShardingOperator.IN));
    Iterator<?> shardingValues2 = ((ListShardingValue) condition2.getShardingValue(Collections.emptyList())).getValues().iterator();
    assertThat(shardingValues2.next(), is((Object) 1));
    assertThat(shardingValues2.next(), is((Object) 3));
    assertFalse(shardingValues2.hasNext());
    Condition condition3 = updateStatement.getConditions().find(new Column("field3", "TABLE_XXX")).get();
    Range shardingValues3 = ((RangeShardingValue) condition3.getShardingValue(Collections.emptyList())).getValueRange();
    assertThat(condition3.getOperator(), is(ShardingOperator.BETWEEN));
    assertThat(shardingValues3.lowerEndpoint(), is((Comparable) 5));
    assertThat(shardingValues3.upperEndpoint(), is((Comparable) 20));
}
Also used : Condition(io.shardingjdbc.core.parsing.parser.context.condition.Condition) RangeShardingValue(io.shardingjdbc.core.api.algorithm.sharding.RangeShardingValue) Column(io.shardingjdbc.core.parsing.parser.context.condition.Column) Range(com.google.common.collect.Range)

Example 3 with RangeShardingValue

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

the class UpdateStatementParserTest method assertUpdateStatementWitParameter.

private void assertUpdateStatementWitParameter(final DMLStatement updateStatement) {
    assertThat(updateStatement.getTables().find("TABLE_XXX").get().getName(), is("TABLE_XXX"));
    assertThat(updateStatement.getTables().find("TABLE_XXX").get().getAlias().get(), is("xxx"));
    List<Object> actualParameters = Arrays.<Object>asList(0, 10, 20, 30, 40, 50, 60, 70, 80);
    Condition condition1 = updateStatement.getConditions().find(new Column("field1", "TABLE_XXX")).get();
    assertThat(condition1.getOperator(), is(ShardingOperator.EQUAL));
    assertThat(((ListShardingValue<? extends Comparable>) condition1.getShardingValue(actualParameters)).getValues().iterator().next(), is((Comparable) 20));
    Condition condition2 = updateStatement.getConditions().find(new Column("field2", "TABLE_XXX")).get();
    assertThat(condition2.getOperator(), is(ShardingOperator.IN));
    Iterator<?> shardingValue2 = ((ListShardingValue) condition2.getShardingValue(actualParameters)).getValues().iterator();
    assertThat(shardingValue2.next(), is((Object) 40));
    assertThat(shardingValue2.next(), is((Object) 50));
    assertFalse(shardingValue2.hasNext());
    Condition condition3 = updateStatement.getConditions().find(new Column("field3", "TABLE_XXX")).get();
    assertThat(condition3.getOperator(), is(ShardingOperator.BETWEEN));
    Range shardingValue3 = ((RangeShardingValue) condition3.getShardingValue(actualParameters)).getValueRange();
    assertThat(shardingValue3.lowerEndpoint(), is((Comparable) 70));
    assertThat(shardingValue3.upperEndpoint(), is((Comparable) 80));
}
Also used : Condition(io.shardingjdbc.core.parsing.parser.context.condition.Condition) RangeShardingValue(io.shardingjdbc.core.api.algorithm.sharding.RangeShardingValue) Column(io.shardingjdbc.core.parsing.parser.context.condition.Column) Range(com.google.common.collect.Range)

Example 4 with RangeShardingValue

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

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

the class DeleteStatementParserTest method assertDeleteStatementWithParameter.

private void assertDeleteStatementWithParameter(final DMLStatement deleteStatement) {
    assertThat(deleteStatement.getTables().find("TABLE_XXX").get().getName(), is("TABLE_XXX"));
    assertThat(deleteStatement.getTables().find("xxx").get().getAlias().get(), is("xxx"));
    List<Object> actualParameters = Arrays.<Object>asList(0, 10, 20, 30, 40, 50, 60, 70, 80);
    Condition condition1 = deleteStatement.getConditions().find(new Column("field1", "TABLE_XXX")).get();
    assertThat(condition1.getOperator(), is(ShardingOperator.EQUAL));
    assertThat(((ListShardingValue) condition1.getShardingValue(actualParameters)).getValues().iterator().next(), is((Object) 10));
    Condition condition2 = deleteStatement.getConditions().find(new Column("field2", "TABLE_XXX")).get();
    assertThat(condition2.getOperator(), is(ShardingOperator.IN));
    Iterator<?> shardingValues2 = ((ListShardingValue) condition2.getShardingValue(actualParameters)).getValues().iterator();
    assertThat(shardingValues2.next(), is((Object) 30));
    assertThat(shardingValues2.next(), is((Object) 40));
    Condition condition3 = deleteStatement.getConditions().find(new Column("field3", "TABLE_XXX")).get();
    assertThat(condition3.getOperator(), is(ShardingOperator.BETWEEN));
    Range shardingValues3 = ((RangeShardingValue) condition3.getShardingValue(actualParameters)).getValueRange();
    assertThat(shardingValues3.lowerEndpoint(), is((Comparable) 60));
    assertThat(shardingValues3.upperEndpoint(), is((Comparable) 70));
}
Also used : Condition(io.shardingjdbc.core.parsing.parser.context.condition.Condition) RangeShardingValue(io.shardingjdbc.core.api.algorithm.sharding.RangeShardingValue) Column(io.shardingjdbc.core.parsing.parser.context.condition.Column) Range(com.google.common.collect.Range)

Aggregations

RangeShardingValue (io.shardingjdbc.core.api.algorithm.sharding.RangeShardingValue)9 Range (com.google.common.collect.Range)4 Column (io.shardingjdbc.core.parsing.parser.context.condition.Column)4 Condition (io.shardingjdbc.core.parsing.parser.context.condition.Condition)4 Test (org.junit.Test)4 ShardingValue (io.shardingjdbc.core.api.algorithm.sharding.ShardingValue)3 ListShardingValue (io.shardingjdbc.core.api.algorithm.sharding.ListShardingValue)2 PreciseShardingValue (io.shardingjdbc.core.api.algorithm.sharding.PreciseShardingValue)2 ShardingKey (io.shardingjdbc.core.hint.ShardingKey)2 TestPreciseShardingAlgorithm (io.shardingjdbc.core.api.algorithm.fixture.TestPreciseShardingAlgorithm)1 TestRangeShardingAlgorithm (io.shardingjdbc.core.api.algorithm.fixture.TestRangeShardingAlgorithm)1 StandardShardingStrategyConfiguration (io.shardingjdbc.core.api.config.strategy.StandardShardingStrategyConfiguration)1 SQLExpression (io.shardingjdbc.core.parsing.parser.expression.SQLExpression)1 SQLNumberExpression (io.shardingjdbc.core.parsing.parser.expression.SQLNumberExpression)1 StandardShardingStrategy (io.shardingjdbc.core.routing.strategy.standard.StandardShardingStrategy)1 Collection (java.util.Collection)1 TreeSet (java.util.TreeSet)1