Search in sources :

Example 1 with StandardShardingStrategyConfiguration

use of io.shardingjdbc.core.api.config.strategy.StandardShardingStrategyConfiguration in project sharding-jdbc by shardingjdbc.

the class AbstractShardingJDBCDatabaseAndTableTest method initShardingDataSources.

@Before
public void initShardingDataSources() throws SQLException {
    if (!getShardingDataSources().isEmpty()) {
        return;
    }
    Map<DatabaseType, Map<String, DataSource>> dataSourceMap = createDataSourceMap();
    for (Map.Entry<DatabaseType, Map<String, DataSource>> entry : dataSourceMap.entrySet()) {
        final ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();
        TableRuleConfiguration orderTableRuleConfig = new TableRuleConfiguration();
        orderTableRuleConfig.setLogicTable("t_order");
        List<String> orderActualDataNodes = new LinkedList<>();
        for (String dataSourceName : entry.getValue().keySet()) {
            orderActualDataNodes.add(dataSourceName + ".t_order_${0..1}");
        }
        orderTableRuleConfig.setActualDataNodes(Joiner.on(",").join(orderActualDataNodes));
        shardingRuleConfig.getTableRuleConfigs().add(orderTableRuleConfig);
        TableRuleConfiguration orderItemTableRuleConfig = new TableRuleConfiguration();
        orderItemTableRuleConfig.setLogicTable("t_order_item");
        List<String> orderItemActualDataNodes = new LinkedList<>();
        for (String dataSourceName : entry.getValue().keySet()) {
            orderItemActualDataNodes.add(dataSourceName + ".t_order_item_${0..1}");
        }
        orderItemTableRuleConfig.setActualDataNodes(Joiner.on(",").join(orderItemActualDataNodes));
        orderItemTableRuleConfig.setKeyGeneratorColumnName("item_id");
        orderItemTableRuleConfig.setKeyGenerator(new IncrementKeyGenerator());
        shardingRuleConfig.getTableRuleConfigs().add(orderItemTableRuleConfig);
        TableRuleConfiguration configTableRuleConfig = new TableRuleConfiguration();
        configTableRuleConfig.setLogicTable("t_config");
        shardingRuleConfig.getTableRuleConfigs().add(configTableRuleConfig);
        shardingRuleConfig.getBindingTableGroups().add("t_order, t_order_item");
        shardingRuleConfig.setDefaultTableShardingStrategyConfig(new StandardShardingStrategyConfiguration("order_id", new PreciseOrderShardingAlgorithm(), new RangeOrderShardingAlgorithm()));
        shardingRuleConfig.setDefaultDatabaseShardingStrategyConfig(new StandardShardingStrategyConfiguration("user_id", new PreciseOrderShardingAlgorithm(), new RangeOrderShardingAlgorithm()));
        ShardingRule shardingRule = new ShardingRule(shardingRuleConfig, entry.getValue().keySet());
        getShardingDataSources().put(entry.getKey(), new ShardingDataSource(entry.getValue(), shardingRule));
    }
}
Also used : DatabaseType(io.shardingjdbc.core.constant.DatabaseType) ShardingDataSource(io.shardingjdbc.core.jdbc.core.datasource.ShardingDataSource) IncrementKeyGenerator(io.shardingjdbc.core.fixture.IncrementKeyGenerator) LinkedList(java.util.LinkedList) StandardShardingStrategyConfiguration(io.shardingjdbc.core.api.config.strategy.StandardShardingStrategyConfiguration) PreciseOrderShardingAlgorithm(io.shardingjdbc.core.fixture.PreciseOrderShardingAlgorithm) ShardingRuleConfiguration(io.shardingjdbc.core.api.config.ShardingRuleConfiguration) ShardingRule(io.shardingjdbc.core.rule.ShardingRule) Map(java.util.Map) TableRuleConfiguration(io.shardingjdbc.core.api.config.TableRuleConfiguration) RangeOrderShardingAlgorithm(io.shardingjdbc.core.fixture.RangeOrderShardingAlgorithm) Before(org.junit.Before)

Example 2 with StandardShardingStrategyConfiguration

use of io.shardingjdbc.core.api.config.strategy.StandardShardingStrategyConfiguration in project sharding-jdbc by shardingjdbc.

the class ShardingNamespaceTest method assertStandardStrategy.

@Test
public void assertStandardStrategy() {
    StandardShardingStrategyConfiguration standardStrategy = this.applicationContext.getBean("standardStrategy", StandardShardingStrategyConfiguration.class);
    assertThat(standardStrategy.getShardingColumn(), is("user_id"));
    assertThat(standardStrategy.getPreciseShardingAlgorithm(), instanceOf(PreciseModuloDatabaseShardingAlgorithm.class));
}
Also used : PreciseModuloDatabaseShardingAlgorithm(io.shardingjdbc.spring.algorithm.PreciseModuloDatabaseShardingAlgorithm) StandardShardingStrategyConfiguration(io.shardingjdbc.core.api.config.strategy.StandardShardingStrategyConfiguration) Test(org.junit.Test)

Example 3 with StandardShardingStrategyConfiguration

use of io.shardingjdbc.core.api.config.strategy.StandardShardingStrategyConfiguration 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 4 with StandardShardingStrategyConfiguration

use of io.shardingjdbc.core.api.config.strategy.StandardShardingStrategyConfiguration 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;
}
Also used : YamlStandardShardingStrategyConfiguration(io.shardingjdbc.core.yaml.sharding.strategy.YamlStandardShardingStrategyConfiguration) StandardShardingStrategyConfiguration(io.shardingjdbc.core.api.config.strategy.StandardShardingStrategyConfiguration) YamlNoneShardingStrategyConfiguration(io.shardingjdbc.core.yaml.sharding.strategy.YamlNoneShardingStrategyConfiguration) NoneShardingStrategyConfiguration(io.shardingjdbc.core.api.config.strategy.NoneShardingStrategyConfiguration) ComplexShardingStrategyConfiguration(io.shardingjdbc.core.api.config.strategy.ComplexShardingStrategyConfiguration) YamlComplexShardingStrategyConfiguration(io.shardingjdbc.core.yaml.sharding.strategy.YamlComplexShardingStrategyConfiguration) YamlHintShardingStrategyConfiguration(io.shardingjdbc.core.yaml.sharding.strategy.YamlHintShardingStrategyConfiguration) ComplexShardingStrategyConfiguration(io.shardingjdbc.core.api.config.strategy.ComplexShardingStrategyConfiguration) YamlNoneShardingStrategyConfiguration(io.shardingjdbc.core.yaml.sharding.strategy.YamlNoneShardingStrategyConfiguration) YamlInlineShardingStrategyConfiguration(io.shardingjdbc.core.yaml.sharding.strategy.YamlInlineShardingStrategyConfiguration) YamlStandardShardingStrategyConfiguration(io.shardingjdbc.core.yaml.sharding.strategy.YamlStandardShardingStrategyConfiguration) YamlComplexShardingStrategyConfiguration(io.shardingjdbc.core.yaml.sharding.strategy.YamlComplexShardingStrategyConfiguration) StandardShardingStrategyConfiguration(io.shardingjdbc.core.api.config.strategy.StandardShardingStrategyConfiguration) ShardingStrategyConfiguration(io.shardingjdbc.core.api.config.strategy.ShardingStrategyConfiguration) InlineShardingStrategyConfiguration(io.shardingjdbc.core.api.config.strategy.InlineShardingStrategyConfiguration) NoneShardingStrategyConfiguration(io.shardingjdbc.core.api.config.strategy.NoneShardingStrategyConfiguration) HintShardingStrategyConfiguration(io.shardingjdbc.core.api.config.strategy.HintShardingStrategyConfiguration) YamlInlineShardingStrategyConfiguration(io.shardingjdbc.core.yaml.sharding.strategy.YamlInlineShardingStrategyConfiguration) InlineShardingStrategyConfiguration(io.shardingjdbc.core.api.config.strategy.InlineShardingStrategyConfiguration) YamlHintShardingStrategyConfiguration(io.shardingjdbc.core.yaml.sharding.strategy.YamlHintShardingStrategyConfiguration) HintShardingStrategyConfiguration(io.shardingjdbc.core.api.config.strategy.HintShardingStrategyConfiguration)

Example 5 with StandardShardingStrategyConfiguration

use of io.shardingjdbc.core.api.config.strategy.StandardShardingStrategyConfiguration in project sharding-jdbc by shardingjdbc.

the class ShardingRuleConfigurationConverterTest method assertToJsonForStandardStrategy.

@Test
public void assertToJsonForStandardStrategy() {
    StandardShardingStrategyConfiguration actual = new StandardShardingStrategyConfiguration("order_id", new TestPreciseShardingAlgorithm(), new TestRangeShardingAlgorithm());
    assertThat(ShardingRuleConfigurationConverter.toJson(getCommonShardingRuleConfig(actual)), is(getJsonForStandardStrategy()));
}
Also used : StandardShardingStrategyConfiguration(io.shardingjdbc.core.api.config.strategy.StandardShardingStrategyConfiguration) TestPreciseShardingAlgorithm(io.shardingjdbc.orchestration.internal.json.fixture.TestPreciseShardingAlgorithm) TestRangeShardingAlgorithm(io.shardingjdbc.orchestration.internal.json.fixture.TestRangeShardingAlgorithm) Test(org.junit.Test)

Aggregations

StandardShardingStrategyConfiguration (io.shardingjdbc.core.api.config.strategy.StandardShardingStrategyConfiguration)17 Test (org.junit.Test)9 ShardingRuleConfiguration (io.shardingjdbc.core.api.config.ShardingRuleConfiguration)7 TableRuleConfiguration (io.shardingjdbc.core.api.config.TableRuleConfiguration)6 TestPreciseShardingAlgorithm (io.shardingjdbc.core.api.algorithm.fixture.TestPreciseShardingAlgorithm)5 NoneShardingStrategyConfiguration (io.shardingjdbc.core.api.config.strategy.NoneShardingStrategyConfiguration)4 DatabaseType (io.shardingjdbc.core.constant.DatabaseType)4 ShardingDataSource (io.shardingjdbc.core.jdbc.core.datasource.ShardingDataSource)4 ShardingRule (io.shardingjdbc.core.rule.ShardingRule)4 LinkedList (java.util.LinkedList)4 Map (java.util.Map)4 SQLShardingRule (io.shardingjdbc.core.integrate.jaxb.SQLShardingRule)3 ComplexShardingStrategyConfiguration (io.shardingjdbc.core.api.config.strategy.ComplexShardingStrategyConfiguration)2 HintShardingStrategyConfiguration (io.shardingjdbc.core.api.config.strategy.HintShardingStrategyConfiguration)2 InlineShardingStrategyConfiguration (io.shardingjdbc.core.api.config.strategy.InlineShardingStrategyConfiguration)2 PreciseModuloDatabaseShardingAlgorithm (io.shardingjdbc.core.integrate.fixture.PreciseModuloDatabaseShardingAlgorithm)2 PreciseModuloTableShardingAlgorithm (io.shardingjdbc.core.integrate.fixture.PreciseModuloTableShardingAlgorithm)2 RangeModuloDatabaseShardingAlgorithm (io.shardingjdbc.core.integrate.fixture.RangeModuloDatabaseShardingAlgorithm)2 RangeModuloTableShardingAlgorithm (io.shardingjdbc.core.integrate.fixture.RangeModuloTableShardingAlgorithm)2 Column (io.shardingjdbc.core.parsing.parser.context.condition.Column)2