Search in sources :

Example 6 with NoneShardingStrategyConfiguration

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

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

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

the class AbstractShardingTableOnlyTest 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()) {
        final ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();
        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();
        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));
        orderItemTableRuleConfig.setLogicTable("t_order_item");
        orderItemTableRuleConfig.setKeyGeneratorColumnName("item_id");
        shardingRuleConfig.getTableRuleConfigs().add(orderItemTableRuleConfig);
        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.getBindingTableGroups().add("t_order, t_order_item");
        shardingRuleConfig.setDefaultDatabaseShardingStrategyConfig(new NoneShardingStrategyConfiguration());
        shardingRuleConfig.setDefaultTableShardingStrategyConfig(new StandardShardingStrategyConfiguration("order_id", new PreciseModuloTableShardingAlgorithm(), new RangeModuloTableShardingAlgorithm()));
        getShardingDataSources().put(entry.getKey(), new ShardingDataSource(entry.getValue(), new ShardingRule(shardingRuleConfig, entry.getValue().keySet())));
    }
    return getShardingDataSources();
}
Also used : DatabaseType(io.shardingjdbc.core.constant.DatabaseType) NoneShardingStrategyConfiguration(io.shardingjdbc.core.api.config.strategy.NoneShardingStrategyConfiguration) ShardingDataSource(io.shardingjdbc.core.jdbc.core.datasource.ShardingDataSource) LinkedList(java.util.LinkedList) RangeModuloTableShardingAlgorithm(io.shardingjdbc.core.integrate.fixture.RangeModuloTableShardingAlgorithm) 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)

Example 9 with NoneShardingStrategyConfiguration

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

the class AbstractShardingDatabaseOnlyWithHintTest method getShardingRule.

@Override
protected ShardingRule getShardingRule(final Map.Entry<DatabaseType, Map<String, DataSource>> dataSourceEntry) {
    ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();
    TableRuleConfiguration orderTableRuleConfig = new TableRuleConfiguration();
    orderTableRuleConfig.setLogicTable("t_order");
    shardingRuleConfig.getTableRuleConfigs().add(orderTableRuleConfig);
    TableRuleConfiguration orderItemTableRuleConfig = new TableRuleConfiguration();
    orderItemTableRuleConfig.setLogicTable("t_order_item");
    shardingRuleConfig.getTableRuleConfigs().add(orderItemTableRuleConfig);
    shardingRuleConfig.getBindingTableGroups().add("t_order, t_order_item");
    shardingRuleConfig.setDefaultDatabaseShardingStrategyConfig(new ComplexShardingStrategyConfiguration("user_id", new ComplexKeysModuloDatabaseShardingAlgorithm()));
    shardingRuleConfig.setDefaultTableShardingStrategyConfig(new NoneShardingStrategyConfiguration());
    return new ShardingRule(shardingRuleConfig, dataSourceEntry.getValue().keySet());
}
Also used : NoneShardingStrategyConfiguration(io.shardingjdbc.core.api.config.strategy.NoneShardingStrategyConfiguration) ShardingRuleConfiguration(io.shardingjdbc.core.api.config.ShardingRuleConfiguration) ComplexShardingStrategyConfiguration(io.shardingjdbc.core.api.config.strategy.ComplexShardingStrategyConfiguration) ComplexKeysModuloDatabaseShardingAlgorithm(io.shardingjdbc.core.integrate.fixture.ComplexKeysModuloDatabaseShardingAlgorithm) ShardingRule(io.shardingjdbc.core.rule.ShardingRule) TableRuleConfiguration(io.shardingjdbc.core.api.config.TableRuleConfiguration)

Example 10 with NoneShardingStrategyConfiguration

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

the class AbstractShardingDatabaseOnlyTest method getDataSources.

@Override
protected 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>> each : dataSourceMap.entrySet()) {
        final ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();
        TableRuleConfiguration orderTableRuleConfig = new TableRuleConfiguration();
        orderTableRuleConfig.setLogicTable("t_order");
        orderTableRuleConfig.setLogicIndex("t_order_index");
        orderTableRuleConfig.setKeyGeneratorColumnName("order_id");
        orderTableRuleConfig.setKeyGenerator(new IncrementKeyGenerator());
        shardingRuleConfig.getTableRuleConfigs().add(orderTableRuleConfig);
        TableRuleConfiguration orderItemTableRuleConfig = new TableRuleConfiguration();
        orderItemTableRuleConfig.setLogicTable("t_order_item");
        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(orderItemTableRuleConfig);
        shardingRuleConfig.getBindingTableGroups().add("t_order, t_order_item");
        shardingRuleConfig.setDefaultDatabaseShardingStrategyConfig(new ComplexShardingStrategyConfiguration("user_id", new ComplexKeysModuloDatabaseShardingAlgorithm()));
        shardingRuleConfig.setDefaultTableShardingStrategyConfig(new NoneShardingStrategyConfiguration());
        getShardingDataSources().put(each.getKey(), new ShardingDataSource(each.getValue(), new ShardingRule(shardingRuleConfig, each.getValue().keySet())));
    }
    return getShardingDataSources();
}
Also used : DatabaseType(io.shardingjdbc.core.constant.DatabaseType) NoneShardingStrategyConfiguration(io.shardingjdbc.core.api.config.strategy.NoneShardingStrategyConfiguration) ShardingRuleConfiguration(io.shardingjdbc.core.api.config.ShardingRuleConfiguration) ShardingDataSource(io.shardingjdbc.core.jdbc.core.datasource.ShardingDataSource) ComplexShardingStrategyConfiguration(io.shardingjdbc.core.api.config.strategy.ComplexShardingStrategyConfiguration) ComplexKeysModuloDatabaseShardingAlgorithm(io.shardingjdbc.core.integrate.fixture.ComplexKeysModuloDatabaseShardingAlgorithm) SQLShardingRule(io.shardingjdbc.core.integrate.jaxb.SQLShardingRule) ShardingRule(io.shardingjdbc.core.rule.ShardingRule) IncrementKeyGenerator(io.shardingjdbc.core.fixture.IncrementKeyGenerator) Map(java.util.Map) TableRuleConfiguration(io.shardingjdbc.core.api.config.TableRuleConfiguration)

Aggregations

NoneShardingStrategyConfiguration (io.shardingjdbc.core.api.config.strategy.NoneShardingStrategyConfiguration)19 TableRuleConfiguration (io.shardingjdbc.core.api.config.TableRuleConfiguration)16 ShardingRuleConfiguration (io.shardingjdbc.core.api.config.ShardingRuleConfiguration)14 Test (org.junit.Test)13 NoneShardingStrategy (io.shardingjdbc.core.routing.strategy.none.NoneShardingStrategy)5 ComplexShardingStrategyConfiguration (io.shardingjdbc.core.api.config.strategy.ComplexShardingStrategyConfiguration)4 StandardShardingStrategyConfiguration (io.shardingjdbc.core.api.config.strategy.StandardShardingStrategyConfiguration)4 ShardingRule (io.shardingjdbc.core.rule.ShardingRule)3 HintShardingStrategyConfiguration (io.shardingjdbc.core.api.config.strategy.HintShardingStrategyConfiguration)2 InlineShardingStrategyConfiguration (io.shardingjdbc.core.api.config.strategy.InlineShardingStrategyConfiguration)2 DatabaseType (io.shardingjdbc.core.constant.DatabaseType)2 ComplexKeysModuloDatabaseShardingAlgorithm (io.shardingjdbc.core.integrate.fixture.ComplexKeysModuloDatabaseShardingAlgorithm)2 SQLShardingRule (io.shardingjdbc.core.integrate.jaxb.SQLShardingRule)2 ShardingDataSource (io.shardingjdbc.core.jdbc.core.datasource.ShardingDataSource)2 Map (java.util.Map)2 TestPreciseShardingAlgorithm (io.shardingjdbc.core.api.algorithm.fixture.TestPreciseShardingAlgorithm)1 ShardingStrategyConfiguration (io.shardingjdbc.core.api.config.strategy.ShardingStrategyConfiguration)1 IncrementKeyGenerator (io.shardingjdbc.core.fixture.IncrementKeyGenerator)1 PreciseModuloTableShardingAlgorithm (io.shardingjdbc.core.integrate.fixture.PreciseModuloTableShardingAlgorithm)1 RangeModuloTableShardingAlgorithm (io.shardingjdbc.core.integrate.fixture.RangeModuloTableShardingAlgorithm)1