use of io.shardingjdbc.core.api.config.strategy.InlineShardingStrategyConfiguration in project sharding-jdbc by shardingjdbc.
the class YamlShardingStrategyConfiguration method build.
/**
* Build sharding strategy configuration.
*
* @return sharding strategy configuration
*/
public ShardingStrategyConfiguration build() {
int shardingStrategyConfigCount = 0;
ShardingStrategyConfiguration result = null;
if (null != standard) {
shardingStrategyConfigCount++;
if (null == standard.getRangeAlgorithmClassName()) {
result = new StandardShardingStrategyConfiguration(standard.getShardingColumn(), ShardingAlgorithmFactory.newInstance(standard.getPreciseAlgorithmClassName(), PreciseShardingAlgorithm.class));
} else {
result = new StandardShardingStrategyConfiguration(standard.getShardingColumn(), ShardingAlgorithmFactory.newInstance(standard.getPreciseAlgorithmClassName(), PreciseShardingAlgorithm.class), ShardingAlgorithmFactory.newInstance(standard.getRangeAlgorithmClassName(), RangeShardingAlgorithm.class));
}
}
if (null != complex) {
shardingStrategyConfigCount++;
result = new ComplexShardingStrategyConfiguration(complex.getShardingColumns(), ShardingAlgorithmFactory.newInstance(complex.getAlgorithmClassName(), ComplexKeysShardingAlgorithm.class));
}
if (null != inline) {
shardingStrategyConfigCount++;
result = new InlineShardingStrategyConfiguration(inline.getShardingColumn(), inline.getAlgorithmExpression());
}
if (null != hint) {
shardingStrategyConfigCount++;
result = new HintShardingStrategyConfiguration(ShardingAlgorithmFactory.newInstance(hint.getAlgorithmClassName(), HintShardingAlgorithm.class));
}
if (null != none) {
shardingStrategyConfigCount++;
result = new NoneShardingStrategyConfiguration();
}
Preconditions.checkArgument(shardingStrategyConfigCount <= 1, "Only allowed 0 or 1 sharding strategy configuration.");
return result;
}
use of io.shardingjdbc.core.api.config.strategy.InlineShardingStrategyConfiguration in project sharding-jdbc by shardingjdbc.
the class ShardingRuleConfigurationConverterTest method assertToJsonForInlineStrategy.
@Test
public void assertToJsonForInlineStrategy() {
InlineShardingStrategyConfiguration actual = new InlineShardingStrategyConfiguration("order_id", "order_${user_id % 2}");
assertThat(ShardingRuleConfigurationConverter.toJson(getCommonShardingRuleConfig(actual)), is(getJsonForInlineStrategy()));
}
use of io.shardingjdbc.core.api.config.strategy.InlineShardingStrategyConfiguration in project sharding-jdbc by shardingjdbc.
the class ShardingStrategyConfigurationGsonTypeAdapter method write.
@Override
public void write(final JsonWriter out, final ShardingStrategyConfiguration value) throws IOException {
out.beginObject();
if (value instanceof StandardShardingStrategyConfiguration) {
out.name("type").value(ShardingStrategyType.STANDARD.name());
StandardShardingStrategyConfiguration shardingStrategyConfig = (StandardShardingStrategyConfiguration) value;
out.name("shardingColumn").value(shardingStrategyConfig.getShardingColumn());
out.name("preciseAlgorithmClassName").value(shardingStrategyConfig.getPreciseShardingAlgorithm().getClass().getName());
if (null != shardingStrategyConfig.getRangeShardingAlgorithm()) {
out.name("rangeAlgorithmClassName").value(shardingStrategyConfig.getRangeShardingAlgorithm().getClass().getName());
}
} else if (value instanceof ComplexShardingStrategyConfiguration) {
out.name("type").value(ShardingStrategyType.COMPLEX.name());
ComplexShardingStrategyConfiguration shardingStrategyConfig = (ComplexShardingStrategyConfiguration) value;
out.name("shardingColumns").value(shardingStrategyConfig.getShardingColumns());
out.name("algorithmClassName").value(shardingStrategyConfig.getShardingAlgorithm().getClass().getName());
} else if (value instanceof InlineShardingStrategyConfiguration) {
out.name("type").value(ShardingStrategyType.INLINE.name());
InlineShardingStrategyConfiguration shardingStrategyConfig = (InlineShardingStrategyConfiguration) value;
out.name("shardingColumn").value(shardingStrategyConfig.getShardingColumn());
out.name("algorithmExpression").value(shardingStrategyConfig.getAlgorithmExpression());
} else if (value instanceof HintShardingStrategyConfiguration) {
out.name("type").value(ShardingStrategyType.HINT.name());
HintShardingStrategyConfiguration shardingStrategyConfig = (HintShardingStrategyConfiguration) value;
out.name("algorithmClassName").value(shardingStrategyConfig.getShardingAlgorithm().getClass().getName());
} else if (value instanceof NoneShardingStrategyConfiguration) {
out.name("type").value(ShardingStrategyType.NONE.name());
}
out.endObject();
}
use of io.shardingjdbc.core.api.config.strategy.InlineShardingStrategyConfiguration in project sharding-jdbc by shardingjdbc.
the class OrchestrationShardingNamespaceTest method assertInlineStrategy.
@Test
public void assertInlineStrategy() {
InlineShardingStrategyConfiguration inlineStrategy = this.applicationContext.getBean("inlineStrategy", InlineShardingStrategyConfiguration.class);
assertThat(inlineStrategy.getShardingColumn(), is("order_id"));
assertThat(inlineStrategy.getAlgorithmExpression(), is("t_order_${order_id % 4}"));
}
use of io.shardingjdbc.core.api.config.strategy.InlineShardingStrategyConfiguration in project sharding-jdbc by shardingjdbc.
the class ShardingRuleConfigurationConverterTest method assertFromJsonForInlineStrategy.
@Test
public void assertFromJsonForInlineStrategy() {
ShardingRuleConfiguration actual = ShardingRuleConfigurationConverter.fromJson(getJsonForInlineStrategy());
assertCommon(actual);
InlineShardingStrategyConfiguration actualShardingStrategy = (InlineShardingStrategyConfiguration) actual.getDefaultTableShardingStrategyConfig();
assertThat(actualShardingStrategy.getShardingColumn(), is("order_id"));
assertThat(actualShardingStrategy.getAlgorithmExpression(), is("order_${user_id % 2}"));
}
Aggregations