Search in sources :

Example 1 with IncrementKeyGenerator

use of io.shardingjdbc.core.fixture.IncrementKeyGenerator 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 IncrementKeyGenerator

use of io.shardingjdbc.core.fixture.IncrementKeyGenerator 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

ShardingRuleConfiguration (io.shardingjdbc.core.api.config.ShardingRuleConfiguration)2 TableRuleConfiguration (io.shardingjdbc.core.api.config.TableRuleConfiguration)2 DatabaseType (io.shardingjdbc.core.constant.DatabaseType)2 IncrementKeyGenerator (io.shardingjdbc.core.fixture.IncrementKeyGenerator)2 ShardingDataSource (io.shardingjdbc.core.jdbc.core.datasource.ShardingDataSource)2 ShardingRule (io.shardingjdbc.core.rule.ShardingRule)2 Map (java.util.Map)2 ComplexShardingStrategyConfiguration (io.shardingjdbc.core.api.config.strategy.ComplexShardingStrategyConfiguration)1 NoneShardingStrategyConfiguration (io.shardingjdbc.core.api.config.strategy.NoneShardingStrategyConfiguration)1 StandardShardingStrategyConfiguration (io.shardingjdbc.core.api.config.strategy.StandardShardingStrategyConfiguration)1 PreciseOrderShardingAlgorithm (io.shardingjdbc.core.fixture.PreciseOrderShardingAlgorithm)1 RangeOrderShardingAlgorithm (io.shardingjdbc.core.fixture.RangeOrderShardingAlgorithm)1 ComplexKeysModuloDatabaseShardingAlgorithm (io.shardingjdbc.core.integrate.fixture.ComplexKeysModuloDatabaseShardingAlgorithm)1 SQLShardingRule (io.shardingjdbc.core.integrate.jaxb.SQLShardingRule)1 LinkedList (java.util.LinkedList)1 Before (org.junit.Before)1