Skip to content

Commit

Permalink
fix #82
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu committed May 25, 2016
1 parent 502a0f0 commit e1bbc5c
Show file tree
Hide file tree
Showing 20 changed files with 284 additions and 93 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,17 @@ private DataSourceRule buildDataSourceRule() {
private Collection<TableRule> buildTableRules(final DataSourceRule dataSourceRule) {
Collection<TableRule> result = new ArrayList<>(shardingRuleConfig.getTables().size());
for (Entry<String, TableRuleConfig> each : shardingRuleConfig.getTables().entrySet()) {
TableRule.TableRuleBuilder tableRuleBuilder = TableRule.builder(each.getKey()).dataSourceRule(dataSourceRule).dynamic(each.getValue().isDynamic())
.databaseShardingStrategy(buildShardingStrategy(each.getValue().getDatabaseStrategy(), DatabaseShardingStrategy.class))
.tableShardingStrategy(buildShardingStrategy(each.getValue().getTableStrategy(), TableShardingStrategy.class));
if (null != each.getValue().getActualTables()) {
tableRuleBuilder.actualTables(new InlineParser(each.getValue().getActualTables()).evaluate());
String logicTable = each.getKey();
TableRuleConfig tableRuleConfig = each.getValue();
TableRule.TableRuleBuilder tableRuleBuilder = TableRule.builder(logicTable).dataSourceRule(dataSourceRule)
.dynamic(tableRuleConfig.isDynamic())
.databaseShardingStrategy(buildShardingStrategy(tableRuleConfig.getDatabaseStrategy(), DatabaseShardingStrategy.class))
.tableShardingStrategy(buildShardingStrategy(tableRuleConfig.getTableStrategy(), TableShardingStrategy.class));
if (null != tableRuleConfig.getActualTables()) {
tableRuleBuilder.actualTables(new InlineParser(tableRuleConfig.getActualTables()).evaluate());
}
if (!Strings.isNullOrEmpty(tableRuleConfig.getDataSourceNames())) {
tableRuleBuilder.dataSourceNames(new InlineParser(tableRuleConfig.getDataSourceNames()).evaluate());
}
result.add(tableRuleBuilder.build());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public class TableRuleConfig {

private String actualTables;

private String dataSourceNames;

private StrategyConfig databaseStrategy;

private StrategyConfig tableStrategy;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,19 @@
package com.dangdang.ddframe.rdb.sharding.config.common;

import com.dangdang.ddframe.rdb.sharding.config.common.api.ShardingRuleBuilderTest;
import com.dangdang.ddframe.rdb.sharding.config.common.internal.parser.InlineParserTest;
import com.dangdang.ddframe.rdb.sharding.config.common.internal.algorithm.ClosureShardingAlgorithmTest;
import com.dangdang.ddframe.rdb.sharding.config.common.internal.algorithm.ClosureDatabaseShardingAlgorithmTest;
import com.dangdang.ddframe.rdb.sharding.config.common.internal.algorithm.ClosureTableShardingAlgorithmTest;
import com.dangdang.ddframe.rdb.sharding.config.common.internal.algorithm.ShardingValueWrapperTest;
import com.dangdang.ddframe.rdb.sharding.config.common.internal.parser.InlineParserTest;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;

@RunWith(Suite.class)
@Suite.SuiteClasses({
ShardingRuleBuilderTest.class,
ClosureShardingAlgorithmTest.class,
ShardingValueWrapperTest.class,
ShardingRuleBuilderTest.class,
ClosureDatabaseShardingAlgorithmTest.class,
ClosureTableShardingAlgorithmTest.class,
ShardingValueWrapperTest.class,
InlineParserTest.class
})
public class AllTests {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Copyright 1999-2015 dangdang.com.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* </p>
*/

package com.dangdang.ddframe.rdb.sharding.config.common.internal.algorithm;

import com.dangdang.ddframe.rdb.sharding.api.ShardingValue;
import com.google.common.collect.BoundType;
import com.google.common.collect.Range;
import groovy.lang.MissingMethodException;
import org.junit.Test;

import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;

import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.IsCollectionContaining.hasItem;
import static org.junit.Assert.assertThat;

public abstract class AbstractClosureShardingAlgorithmTest {

protected static final String EXPRESSION = "target_${log.info(id.toString()); id.longValue() % 2}";

protected static final String WRONG_EXPRESSION = "target_${log.info(id.error());}";

protected static final String LOG_ROOT = "default";

protected abstract ClosureShardingAlgorithm createClosureShardingAlgorithm();

protected abstract ClosureShardingAlgorithm createErrorClosureShardingAlgorithm();

@Test
public void assertEqual() {
Collection<String> result = createClosureShardingAlgorithm().doSharding(Collections.singletonList("target_1"), Collections.<ShardingValue<?>>singletonList(new ShardingValue<>("target", "id", 1L)));
assertThat(result.size(), is(1));
assertThat(result, hasItem("target_1"));
}

@Test
public void assertIn() {
Collection<String> result = createClosureShardingAlgorithm().doSharding(Arrays.asList("target_0", "target_1"),
Collections.<ShardingValue<?>>singletonList(new ShardingValue<>("target", "id", Arrays.asList(1, 2))));
assertThat(result.size(), is(2));
assertThat(result, hasItem("target_0"));
assertThat(result, hasItem("target_1"));
}

@Test(expected = UnsupportedOperationException.class)
public void assertBetween() {
createClosureShardingAlgorithm().doSharding(Arrays.asList("target_0", "target_1"),
Collections.<ShardingValue<?>>singletonList(new ShardingValue<>("target", "id", Range.range(1, BoundType.CLOSED, 2, BoundType.OPEN))));
}

@Test(expected = MissingMethodException.class)
public void assertEvaluateInlineExpressionFailure() {
createErrorClosureShardingAlgorithm().doSharding(Collections.singletonList("target_1"), Collections.<ShardingValue<?>>singletonList(new ShardingValue<>("target", "id", 1L)));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright 1999-2015 dangdang.com.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* </p>
*/

package com.dangdang.ddframe.rdb.sharding.config.common.internal.algorithm;

public final class ClosureDatabaseShardingAlgorithmTest extends AbstractClosureShardingAlgorithmTest {

@Override
protected ClosureShardingAlgorithm createClosureShardingAlgorithm() {
return new ClosureDatabaseShardingAlgorithm(EXPRESSION, LOG_ROOT);
}

@Override
protected ClosureShardingAlgorithm createErrorClosureShardingAlgorithm() {
return new ClosureDatabaseShardingAlgorithm(WRONG_EXPRESSION, LOG_ROOT);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright 1999-2015 dangdang.com.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* </p>
*/

package com.dangdang.ddframe.rdb.sharding.config.common.internal.algorithm;

public final class ClosureTableShardingAlgorithmTest extends AbstractClosureShardingAlgorithmTest {

@Override
protected ClosureShardingAlgorithm createClosureShardingAlgorithm() {
return new ClosureTableShardingAlgorithm(EXPRESSION, LOG_ROOT);
}

@Override
protected ClosureShardingAlgorithm createErrorClosureShardingAlgorithm() {
return new ClosureTableShardingAlgorithm(WRONG_EXPRESSION, LOG_ROOT);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ defaultDataSourceName: db0
tables:
config:
actualTables: config_${0..1}
dataSourceNames: config

t_order:
actualTables: t_order_${0..1}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ public final class ShardingJdbcDataSourceBeanDefinitionParserTag {

public static final String ACTUAL_TABLES_ATTR = "actual-tables";

public static final String DATA_SOURCE_NAMES_ATTR = "data-source-names";

public static final String DATABASE_STRATEGY_ATTR = "database-strategy";

public static final String TABLE_STRATEGY_ATTR = "table-strategy";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@ private BeanDefinition parseTableRuleConfig(final Element tableElement) {
if (!Strings.isNullOrEmpty(actualTables)) {
factory.addPropertyValue("actualTables", actualTables);
}
String dataSourceNames = tableElement.getAttribute(ShardingJdbcDataSourceBeanDefinitionParserTag.DATA_SOURCE_NAMES_ATTR);
if (!Strings.isNullOrEmpty(dataSourceNames)) {
factory.addPropertyValue("dataSourceNames", dataSourceNames);
}
String databaseStrategy = tableElement.getAttribute(ShardingJdbcDataSourceBeanDefinitionParserTag.DATABASE_STRATEGY_ATTR);
if (!Strings.isNullOrEmpty(databaseStrategy)) {
factory.addPropertyReference("databaseStrategy", databaseStrategy);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
<xsd:attribute name="logic-table" type="xsd:string" use="required" />
<xsd:attribute name="dynamic" type="xsd:string" use="optional" />
<xsd:attribute name="actual-tables" type="xsd:string" use="optional" />
<xsd:attribute name="data-source-names" type="xsd:string" use="optional" />
<xsd:attribute name="database-strategy" type="xsd:string" use="optional" />
<xsd:attribute name="table-strategy" type="xsd:string" use="optional" />
</xsd:complexType>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.dangdang.ddframe.rdb.sharding.spring.cases.namespace.WithNamespaceBindingTablesTest;
import com.dangdang.ddframe.rdb.sharding.spring.cases.namespace.WithNamespaceDefaultStrategyTest;
import com.dangdang.ddframe.rdb.sharding.spring.cases.namespace.WithNamespaceDifferentTablesTest;
import com.dangdang.ddframe.rdb.sharding.spring.cases.namespace.WithNamespaceForIndicatedDataSourceNamesTest;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
Expand All @@ -41,7 +42,8 @@
WithNamespaceBindingTablesTest.class,
WithoutNamespaceTest.class,
WithoutNamespaceDefaultStrategyTest.class,
WithNamespaceDifferentTablesTest.class
WithNamespaceDifferentTablesTest.class,
WithNamespaceForIndicatedDataSourceNamesTest.class
})
public class AllSpringTests {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright 1999-2015 dangdang.com.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* </p>
*/

package com.dangdang.ddframe.rdb.sharding.spring.cases.namespace;

import com.dangdang.ddframe.rdb.sharding.spring.AbstractShardingBothDataBasesAndTablesSpringDBUnitTest;
import org.springframework.test.context.ContextConfiguration;

@ContextConfiguration(locations = "classpath:META-INF/rdb/namespace/withNamespaceForIndicatedDataSourceNames.xml")
public final class WithNamespaceForIndicatedDataSourceNamesTest extends AbstractShardingBothDataBasesAndTablesSpringDBUnitTest {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:rdb="http://www.dangdang.com/schema/ddframe/rdb"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.dangdang.com/schema/ddframe/rdb
http://www.dangdang.com/schema/ddframe/rdb/rdb.xsd
">
<import resource="../datasource/dataSource.xml" />

<rdb:strategy id="databaseStrategy" sharding-columns="user_id" algorithm-expression="dbtbl_${user_id.longValue() % 2}"/>

<rdb:strategy id="orderTableStrategy" sharding-columns="order_id" algorithm-expression="t_order_${order_id.longValue() % 4}"/>

<rdb:strategy id="orderItemTableStrategy" sharding-columns="order_id" algorithm-expression="t_order_item_${order_id.longValue() % 4}"/>

<rdb:data-source id="shardingDataSource">
<rdb:sharding-rule data-sources="dbtbl_0,dbtbl_1" default-data-source="dbtbl_0">
<rdb:table-rules>
<rdb:table-rule logic-table="t_order" actual-tables="t_order_${0..3}, t_order_${0..3}" data-source-names="dbtbl_0, dbtbl_1" database-strategy="databaseStrategy" table-strategy="orderTableStrategy"/>
<rdb:table-rule logic-table="t_order_item" actual-tables="t_order_item_${0..3}" data-source-names="dbtbl_${0..1}" database-strategy="databaseStrategy" table-strategy="orderItemTableStrategy"/>
</rdb:table-rules>
</rdb:sharding-rule>
</rdb:data-source>
</beans>
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,11 @@
</list>
</constructor-arg>
<constructor-arg index="3" ref="dataSourceRule"/>
<constructor-arg index="4" ref="databaseShardingStrategy"/>
<constructor-arg index="5" ref="tableShardingStrategy"/>
<constructor-arg index="4">
<null />
</constructor-arg>
<constructor-arg index="5" ref="databaseShardingStrategy"/>
<constructor-arg index="6" ref="tableShardingStrategy"/>
</bean>

<bean id="orderItemTableRule" class="com.dangdang.ddframe.rdb.sharding.api.rule.TableRule">
Expand All @@ -45,8 +48,11 @@
</list>
</constructor-arg>
<constructor-arg index="3" ref="dataSourceRule"/>
<constructor-arg index="4" ref="databaseShardingStrategy"/>
<constructor-arg index="5" ref="tableShardingStrategy"/>
<constructor-arg index="4">
<null />
</constructor-arg>
<constructor-arg index="5" ref="databaseShardingStrategy"/>
<constructor-arg index="6" ref="tableShardingStrategy"/>
</bean>

<bean id="databaseShardingStrategy" class="com.dangdang.ddframe.rdb.sharding.api.strategy.database.DatabaseShardingStrategy">
Expand Down
Loading

0 comments on commit e1bbc5c

Please sign in to comment.