Search in sources :

Example 1 with ComplexShardingStrategyConfiguration

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

the class InsertStatementParserTest method createShardingRuleWithGenerateKeyColumns.

private ShardingRule createShardingRuleWithGenerateKeyColumns() {
    DataSource dataSource = mock(DataSource.class);
    Connection connection = mock(Connection.class);
    DatabaseMetaData databaseMetaData = mock(DatabaseMetaData.class);
    try {
        when(dataSource.getConnection()).thenReturn(connection);
        when(connection.getMetaData()).thenReturn(databaseMetaData);
        when(databaseMetaData.getDatabaseProductName()).thenReturn("H2");
    } catch (final SQLException ex) {
        throw new RuntimeException(ex);
    }
    final ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();
    TableRuleConfiguration tableRuleConfig = new TableRuleConfiguration();
    tableRuleConfig.setLogicTable("TABLE_XXX");
    tableRuleConfig.setActualDataNodes("ds.table_${0..2}");
    tableRuleConfig.setTableShardingStrategyConfig(new ComplexShardingStrategyConfiguration("field1", new TestComplexKeysShardingAlgorithm()));
    tableRuleConfig.setKeyGeneratorColumnName("field2");
    shardingRuleConfig.getTableRuleConfigs().add(tableRuleConfig);
    shardingRuleConfig.setDefaultKeyGenerator(new IncrementKeyGenerator());
    return new ShardingRule(shardingRuleConfig, Lists.newArrayList("ds"));
}
Also used : SQLException(java.sql.SQLException) ShardingRuleConfiguration(io.shardingjdbc.core.api.config.ShardingRuleConfiguration) ComplexShardingStrategyConfiguration(io.shardingjdbc.core.api.config.strategy.ComplexShardingStrategyConfiguration) Connection(java.sql.Connection) ShardingRule(io.shardingjdbc.core.rule.ShardingRule) IncrementKeyGenerator(io.shardingjdbc.core.keygen.fixture.IncrementKeyGenerator) DatabaseMetaData(java.sql.DatabaseMetaData) TestComplexKeysShardingAlgorithm(io.shardingjdbc.core.api.algorithm.fixture.TestComplexKeysShardingAlgorithm) TableRuleConfiguration(io.shardingjdbc.core.api.config.TableRuleConfiguration) DataSource(javax.sql.DataSource)

Example 2 with ComplexShardingStrategyConfiguration

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

the class ShardingStrategyTest method assertDoShardingForMultipleKeys.

@Test
public void assertDoShardingForMultipleKeys() {
    ComplexShardingStrategy strategy = new ComplexShardingStrategy(new ComplexShardingStrategyConfiguration("column", new TestComplexKeysShardingAlgorithm()));
    assertThat(strategy.doSharding(targets, Collections.<ShardingValue>singletonList(new PreciseShardingValue<>("logicTable", "column", "1"))), is((Collection<String>) Sets.newHashSet("1", "2", "3")));
}
Also used : ComplexShardingStrategyConfiguration(io.shardingjdbc.core.api.config.strategy.ComplexShardingStrategyConfiguration) Collection(java.util.Collection) TestComplexKeysShardingAlgorithm(io.shardingjdbc.core.api.algorithm.fixture.TestComplexKeysShardingAlgorithm) PreciseShardingValue(io.shardingjdbc.core.api.algorithm.sharding.PreciseShardingValue) ShardingValue(io.shardingjdbc.core.api.algorithm.sharding.ShardingValue) RangeShardingValue(io.shardingjdbc.core.api.algorithm.sharding.RangeShardingValue) ComplexShardingStrategy(io.shardingjdbc.core.routing.strategy.complex.ComplexShardingStrategy) Test(org.junit.Test)

Example 3 with ComplexShardingStrategyConfiguration

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

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

the class ShardingRuleConfigurationConverterTest method assertToJsonForComplexStrategy.

@Test
public void assertToJsonForComplexStrategy() {
    ComplexShardingStrategyConfiguration actual = new ComplexShardingStrategyConfiguration("order_id,item_id", new TestComplexKeysShardingAlgorithm());
    assertThat(ShardingRuleConfigurationConverter.toJson(getCommonShardingRuleConfig(actual)), is(getJsonForComplexStrategy()));
}
Also used : ComplexShardingStrategyConfiguration(io.shardingjdbc.core.api.config.strategy.ComplexShardingStrategyConfiguration) TestComplexKeysShardingAlgorithm(io.shardingjdbc.orchestration.internal.json.fixture.TestComplexKeysShardingAlgorithm) Test(org.junit.Test)

Example 5 with ComplexShardingStrategyConfiguration

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

Aggregations

ComplexShardingStrategyConfiguration (io.shardingjdbc.core.api.config.strategy.ComplexShardingStrategyConfiguration)14 ShardingRuleConfiguration (io.shardingjdbc.core.api.config.ShardingRuleConfiguration)8 TableRuleConfiguration (io.shardingjdbc.core.api.config.TableRuleConfiguration)7 ShardingRule (io.shardingjdbc.core.rule.ShardingRule)7 TestComplexKeysShardingAlgorithm (io.shardingjdbc.core.api.algorithm.fixture.TestComplexKeysShardingAlgorithm)5 Test (org.junit.Test)5 NoneShardingStrategyConfiguration (io.shardingjdbc.core.api.config.strategy.NoneShardingStrategyConfiguration)4 ComplexKeysModuloDatabaseShardingAlgorithm (io.shardingjdbc.core.integrate.fixture.ComplexKeysModuloDatabaseShardingAlgorithm)3 HintShardingStrategyConfiguration (io.shardingjdbc.core.api.config.strategy.HintShardingStrategyConfiguration)2 InlineShardingStrategyConfiguration (io.shardingjdbc.core.api.config.strategy.InlineShardingStrategyConfiguration)2 StandardShardingStrategyConfiguration (io.shardingjdbc.core.api.config.strategy.StandardShardingStrategyConfiguration)2 DatabaseType (io.shardingjdbc.core.constant.DatabaseType)2 SQLShardingRule (io.shardingjdbc.core.integrate.jaxb.SQLShardingRule)2 ShardingDataSource (io.shardingjdbc.core.jdbc.core.datasource.ShardingDataSource)2 TestQueryResult (io.shardingjdbc.core.merger.fixture.TestQueryResult)2 TestComplexKeysShardingAlgorithm (io.shardingjdbc.orchestration.internal.json.fixture.TestComplexKeysShardingAlgorithm)2 Connection (java.sql.Connection)2 DatabaseMetaData (java.sql.DatabaseMetaData)2 ResultSet (java.sql.ResultSet)2 ResultSetMetaData (java.sql.ResultSetMetaData)2