Search in sources :

Example 6 with StandardShardingStrategyConfiguration

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

the class ShardingRuleConfigurationConverterTest method assertFromJsonForStandardStrategy.

@Test
public void assertFromJsonForStandardStrategy() {
    ShardingRuleConfiguration actual = ShardingRuleConfigurationConverter.fromJson(getJsonForStandardStrategy());
    assertCommon(actual);
    StandardShardingStrategyConfiguration actualShardingStrategy = (StandardShardingStrategyConfiguration) actual.getDefaultTableShardingStrategyConfig();
    assertThat(actualShardingStrategy.getShardingColumn(), is("order_id"));
    assertThat(actualShardingStrategy.getPreciseShardingAlgorithm(), instanceOf(TestPreciseShardingAlgorithm.class));
    assertThat(actualShardingStrategy.getRangeShardingAlgorithm(), instanceOf(TestRangeShardingAlgorithm.class));
}
Also used : StandardShardingStrategyConfiguration(io.shardingjdbc.core.api.config.strategy.StandardShardingStrategyConfiguration) TestPreciseShardingAlgorithm(io.shardingjdbc.orchestration.internal.json.fixture.TestPreciseShardingAlgorithm) ShardingRuleConfiguration(io.shardingjdbc.core.api.config.ShardingRuleConfiguration) TestRangeShardingAlgorithm(io.shardingjdbc.orchestration.internal.json.fixture.TestRangeShardingAlgorithm) Test(org.junit.Test)

Example 7 with StandardShardingStrategyConfiguration

use of io.shardingjdbc.core.api.config.strategy.StandardShardingStrategyConfiguration 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();
}
Also used : StandardShardingStrategyConfiguration(io.shardingjdbc.core.api.config.strategy.StandardShardingStrategyConfiguration) NoneShardingStrategyConfiguration(io.shardingjdbc.core.api.config.strategy.NoneShardingStrategyConfiguration) ComplexShardingStrategyConfiguration(io.shardingjdbc.core.api.config.strategy.ComplexShardingStrategyConfiguration) InlineShardingStrategyConfiguration(io.shardingjdbc.core.api.config.strategy.InlineShardingStrategyConfiguration) HintShardingStrategyConfiguration(io.shardingjdbc.core.api.config.strategy.HintShardingStrategyConfiguration)

Example 8 with StandardShardingStrategyConfiguration

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

the class OrchestrationShardingNamespaceTest method assertRangeStandardStrategy.

@Test
public void assertRangeStandardStrategy() {
    StandardShardingStrategyConfiguration rangeStandardStrategy = this.applicationContext.getBean("rangeStandardStrategy", StandardShardingStrategyConfiguration.class);
    assertThat(rangeStandardStrategy.getShardingColumn(), is("order_id"));
    assertThat(rangeStandardStrategy.getPreciseShardingAlgorithm(), instanceOf(PreciseModuloTableShardingAlgorithm.class));
    assertThat(rangeStandardStrategy.getRangeShardingAlgorithm(), instanceOf(RangeModuloTableShardingAlgorithm.class));
}
Also used : RangeModuloTableShardingAlgorithm(io.shardingjdbc.orchestration.spring.algorithm.RangeModuloTableShardingAlgorithm) StandardShardingStrategyConfiguration(io.shardingjdbc.core.api.config.strategy.StandardShardingStrategyConfiguration) PreciseModuloTableShardingAlgorithm(io.shardingjdbc.orchestration.spring.algorithm.PreciseModuloTableShardingAlgorithm) Test(org.junit.Test)

Example 9 with StandardShardingStrategyConfiguration

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

the class OrchestrationShardingNamespaceTest 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.orchestration.spring.algorithm.PreciseModuloDatabaseShardingAlgorithm) StandardShardingStrategyConfiguration(io.shardingjdbc.core.api.config.strategy.StandardShardingStrategyConfiguration) Test(org.junit.Test)

Example 10 with StandardShardingStrategyConfiguration

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

the class AbstractShardingDatabaseAndTableTest method getDataSources.

@Override
protected final Map<DatabaseType, ShardingDataSource> getDataSources() throws SQLException {
    if (!getShardingDataSources().isEmpty()) {
        return getShardingDataSources();
    }
    Map<DatabaseType, Map<String, DataSource>> dataSourceMap = createDataSourceMap();
    for (Map.Entry<DatabaseType, Map<String, DataSource>> entry : dataSourceMap.entrySet()) {
        ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();
        shardingRuleConfig.setDefaultDataSourceName("dataSource_dbtbl_0");
        TableRuleConfiguration orderTableRuleConfig = new TableRuleConfiguration();
        orderTableRuleConfig.setLogicTable("t_order");
        orderTableRuleConfig.setLogicIndex("t_order_index");
        List<String> orderActualDataNodes = new LinkedList<>();
        for (String dataSourceName : entry.getValue().keySet()) {
            orderActualDataNodes.add(dataSourceName + ".t_order_${0..9}");
        }
        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..9}");
        }
        orderItemTableRuleConfig.setActualDataNodes(Joiner.on(",").join(orderItemActualDataNodes));
        shardingRuleConfig.getTableRuleConfigs().add(orderItemTableRuleConfig);
        TableRuleConfiguration configTableRuleConfig = new TableRuleConfiguration();
        configTableRuleConfig.setLogicTable("t_config");
        TableRuleConfiguration logTableRuleConfig = new TableRuleConfiguration();
        logTableRuleConfig.setLogicIndex("t_log_index");
        logTableRuleConfig.setLogicTable("t_log");
        TableRuleConfiguration tempLogTableRuleConfig = new TableRuleConfiguration();
        tempLogTableRuleConfig.setLogicTable("t_temp_log");
        shardingRuleConfig.getTableRuleConfigs().add(logTableRuleConfig);
        shardingRuleConfig.getTableRuleConfigs().add(tempLogTableRuleConfig);
        shardingRuleConfig.getTableRuleConfigs().add(configTableRuleConfig);
        shardingRuleConfig.getBindingTableGroups().add("t_order, t_order_item");
        shardingRuleConfig.setDefaultTableShardingStrategyConfig(new StandardShardingStrategyConfiguration("order_id", new PreciseModuloTableShardingAlgorithm(), new RangeModuloTableShardingAlgorithm()));
        shardingRuleConfig.setDefaultDatabaseShardingStrategyConfig(new StandardShardingStrategyConfiguration("user_id", new PreciseModuloDatabaseShardingAlgorithm(), new RangeModuloDatabaseShardingAlgorithm()));
        getShardingDataSources().put(entry.getKey(), new ShardingDataSource(entry.getValue(), new ShardingRule(shardingRuleConfig, entry.getValue().keySet())));
    }
    return getShardingDataSources();
}
Also used : PreciseModuloDatabaseShardingAlgorithm(io.shardingjdbc.core.integrate.fixture.PreciseModuloDatabaseShardingAlgorithm) DatabaseType(io.shardingjdbc.core.constant.DatabaseType) ShardingDataSource(io.shardingjdbc.core.jdbc.core.datasource.ShardingDataSource) LinkedList(java.util.LinkedList) RangeModuloTableShardingAlgorithm(io.shardingjdbc.core.integrate.fixture.RangeModuloTableShardingAlgorithm) RangeModuloDatabaseShardingAlgorithm(io.shardingjdbc.core.integrate.fixture.RangeModuloDatabaseShardingAlgorithm) StandardShardingStrategyConfiguration(io.shardingjdbc.core.api.config.strategy.StandardShardingStrategyConfiguration) ShardingRuleConfiguration(io.shardingjdbc.core.api.config.ShardingRuleConfiguration) SQLShardingRule(io.shardingjdbc.core.integrate.jaxb.SQLShardingRule) ShardingRule(io.shardingjdbc.core.rule.ShardingRule) Map(java.util.Map) TableRuleConfiguration(io.shardingjdbc.core.api.config.TableRuleConfiguration) PreciseModuloTableShardingAlgorithm(io.shardingjdbc.core.integrate.fixture.PreciseModuloTableShardingAlgorithm)

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