Skip to content

Commit

Permalink
fix #80
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu committed May 23, 2016
1 parent 4695545 commit 5f4b92d
Show file tree
Hide file tree
Showing 36 changed files with 196 additions and 85 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ public ShardingRule build() {

private DataSourceRule buildDataSourceRule() {
Preconditions.checkArgument(!shardingRuleConfig.getDataSource().isEmpty() || MapUtils.isNotEmpty(externalDataSourceMap), "Sharding JDBC: No data source config");
return !shardingRuleConfig.getDataSource().isEmpty() ? new DataSourceRule(shardingRuleConfig.getDataSource()) : new DataSourceRule(externalDataSourceMap);
return shardingRuleConfig.getDataSource().isEmpty() ? new DataSourceRule(externalDataSourceMap, shardingRuleConfig.getDefaultDataSourceName())
: new DataSourceRule(shardingRuleConfig.getDataSource(), shardingRuleConfig.getDefaultDataSourceName());
}

private Collection<TableRule> buildTableRules(final DataSourceRule dataSourceRule) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public class ShardingRuleConfig {

private Map<String, DataSource> dataSource = new HashMap<>();

private String defaultDataSourceName;

private Map<String, TableRuleConfig> tables = new HashMap<>();

private List<BindingTableRuleConfig> bindingTables = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public void testAll() {
assertThat(shardingRule.getTableRules().size(), is(3));
assertThat(shardingRule.getBindingTableRules().size(), is(1));
assertThat(Arrays.asList(shardingRule.getTableRules().toArray()), hasItems(shardingRule.getBindingTableRules().iterator().next().getTableRules().toArray()));
assertThat(shardingRule.getDataSourceRule().getDefaultDataSourceName(), is("db0"));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ dataSource:
password:
maxActive: 100

defaultDataSourceName: db0

tables:
config:
actualTables: config_${0..1}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ public final class ShardingJdbcDataSourceBeanDefinitionParserTag {

public static final String DATA_SOURCES_TAG = "data-sources";

public static final String DEFAULT_DATA_SOURCE_TAG = "default-data-source";

public static final String TABLE_RULES_TAG = "table-rules";

public static final String TABLE_RULE_TAG = "table-rule";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,6 @@

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

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import com.dangdang.ddframe.rdb.sharding.config.common.api.config.BindingTableRuleConfig;
import com.dangdang.ddframe.rdb.sharding.config.common.api.config.ShardingRuleConfig;
import com.dangdang.ddframe.rdb.sharding.config.common.api.config.TableRuleConfig;
Expand All @@ -39,6 +34,11 @@
import org.springframework.util.xml.DomUtils;
import org.w3c.dom.Element;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Properties;

/**
* 基于Spring命名空间的数据源解析器.
*
Expand All @@ -60,6 +60,7 @@ private BeanDefinition parseShardingRuleConfig(final Element element, final Pars
Element shardingRuleElement = DomUtils.getChildElementByTagName(element, ShardingJdbcDataSourceBeanDefinitionParserTag.SHARDING_RULE_CONFIG_TAG);
BeanDefinitionBuilder factory = BeanDefinitionBuilder.rootBeanDefinition(ShardingRuleConfig.class);
factory.addPropertyValue("dataSource", parseDataSources(shardingRuleElement, parserContext));
parseDefaultDataSource(factory, shardingRuleElement);
factory.addPropertyValue("tables", parseTableRulesConfig(shardingRuleElement));
factory.addPropertyValue("bindingTables", parseBindingTablesConfig(shardingRuleElement));
factory.addPropertyValue("defaultDatabaseStrategy", parseDefaultDatabaseStrategyConfig(shardingRuleElement));
Expand All @@ -76,6 +77,13 @@ private Map<String, BeanDefinition> parseDataSources(final Element element, fina
return result;
}

private void parseDefaultDataSource(final BeanDefinitionBuilder factory, final Element element) {
String defaultDataSource = element.getAttribute(ShardingJdbcDataSourceBeanDefinitionParserTag.DEFAULT_DATA_SOURCE_TAG);
if (!Strings.isNullOrEmpty(defaultDataSource)) {
factory.addPropertyValue("defaultDataSourceName", defaultDataSource);
}
}

private Map<String, BeanDefinition> parseTableRulesConfig(final Element element) {
Element tableRulesElement = DomUtils.getChildElementByTagName(element, ShardingJdbcDataSourceBeanDefinitionParserTag.TABLE_RULES_TAG);
List<Element> tableRuleElements = DomUtils.getChildElementsByTagName(tableRulesElement, ShardingJdbcDataSourceBeanDefinitionParserTag.TABLE_RULE_TAG);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
<xsd:element ref="default-table-strategy" minOccurs="0" maxOccurs="1"/>
</xsd:sequence>
<xsd:attribute name="data-sources" type="xsd:string" use="required" />
<xsd:attribute name="default-data-source" type="xsd:string" use="optional" />
</xsd:complexType>
</xsd:element>
<xsd:element name="table-rules">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public void testWithAllPlaceholders() throws SQLException {
private void insertData() throws SQLException {
String orderSql = "INSERT INTO `t_order` (`order_id`, `user_id`, `status`) VALUES (?, ?, ?)";
String orderItemSql = "INSERT INTO `t_order_item` (`order_item_id`, `order_id`, `user_id`, `status`) VALUES (?, ?, ?, ?)";
String configSql = "INSERT INTO `t_config` (`id`, `status`) VALUES (?, ?)";
for (int orderId = 1; orderId <= 4; orderId++) {
for (int userId = 1; userId <= 2; userId++) {
try (Connection connection = getShardingDataSource().getConnection()) {
Expand All @@ -63,6 +64,13 @@ private void insertData() throws SQLException {
preparedStatement.setString(4, "insert");
preparedStatement.execute();
preparedStatement.close();


preparedStatement = connection.prepareStatement(configSql);
preparedStatement.setInt(1, new Long(System.nanoTime()).intValue());
preparedStatement.setString(2, "insert");
preparedStatement.execute();
preparedStatement.close();
}
}
}
Expand Down Expand Up @@ -91,4 +99,27 @@ private void selectData() throws SQLException {
preparedStatement.close();
}
}

// private void selectDefaultData() throws SQLException {
// String sql = "SELECT * FROM `t_config`";
// try (Connection connection = getShardingDataSource().getConnection()) {
// PreparedStatement preparedStatement = connection.prepareStatement(sql);
// preparedStatement.setInt(1, 1);
// preparedStatement.setInt(2, 1);
// preparedStatement.setInt(3, 1);
// ResultSet resultSet = preparedStatement.executeQuery();
// int count = 0;
// while (resultSet.next()) {
// if (0 == count) {
// assertThat(resultSet.getInt(1), is(1));
// assertThat(resultSet.getInt(2), is(5));
// } else if (1 == count) {
// assertThat(resultSet.getInt(1), is(1));
// assertThat(resultSet.getInt(2), is(1));
// }
// count++;
// }
// preparedStatement.close();
// }
// }
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
<rdb:strategy id="tableStrategy" sharding-columns="order_id" algorithm-class="com.dangdang.ddframe.rdb.sharding.spring.algorithm.SingleKeyModuloTableShardingAlgorithm"/>

<rdb:data-source id="shardingDataSource">
<rdb:sharding-rule data-sources="dbtbl_0,dbtbl_1">
<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}" database-strategy="databaseStrategy" table-strategy="tableStrategy"/>
<rdb:table-rule logic-table="t_order_item" actual-tables="t_order_item_${0..3}" database-strategy="databaseStrategy" table-strategy="tableStrategy"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
<rdb:strategy id="tableStrategy" sharding-columns="order_id" algorithm-class="com.dangdang.ddframe.rdb.sharding.spring.algorithm.SingleKeyModuloTableShardingAlgorithm"/>

<rdb:data-source id="shardingDataSource">
<rdb:sharding-rule data-sources="dbtbl_0,dbtbl_1">
<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}" database-strategy="databaseStrategy" table-strategy="tableStrategy"/>
<rdb:table-rule logic-table="t_order_item" actual-tables="t_order_item_${0..3}" database-strategy="databaseStrategy" table-strategy="tableStrategy"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<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">
<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}" database-strategy="databaseStrategy" table-strategy="orderTableStrategy"/>
<rdb:table-rule logic-table="t_order_item" actual-tables="t_order_item_${0..3}" database-strategy="databaseStrategy" table-strategy="orderItemTableStrategy"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<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">
<rdb:sharding-rule data-sources="dbtbl_0,dbtbl_1" default-data-source="dbtbl_0">
<rdb:table-rules>
<rdb:table-rule logic-table="t_order" dynamic="true" database-strategy="databaseStrategy" table-strategy="orderTableStrategy"/>
<rdb:table-rule logic-table="t_order_item" dynamic="true" database-strategy="databaseStrategy" table-strategy="orderItemTableStrategy"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<rdb:strategy id="tableStrategy" sharding-columns="order_id" algorithm-class="com.dangdang.ddframe.rdb.sharding.spring.algorithm.SingleKeyModuloTableShardingAlgorithm"/>

<rdb:data-source id="shardingDatasource">
<rdb:sharding-rule data-sources="dbtbl_0,dbtbl_1">
<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,t_order_1,t_order_2,t_order_3" database-strategy="databaseStrategy" table-strategy="tableStrategy"/>
<rdb:table-rule logic-table="t_order_item" actual-tables="t_order_item_0,t_order_item_1,t_order_item_2,t_order_item_3" database-strategy="databaseStrategy" table-strategy="tableStrategy"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<import resource="../datasource/dataSource.xml" />

<rdb:data-source id="shardingDataSource">
<rdb:sharding-rule data-sources="dbtbl_0,dbtbl_1">
<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,t_order_1,t_order_2,t_order_3"/>
<rdb:table-rule logic-table="t_order_item" actual-tables="t_order_item_0,t_order_item_1,t_order_item_2,t_order_item_3"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<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">
<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="dbtbl_0.t_order_${0..3}, dbtbl_1.t_order_${0..3}" database-strategy="databaseStrategy" table-strategy="orderTableStrategy"/>
<rdb:table-rule logic-table="t_order_item" actual-tables="t_order_item_${0..3}" database-strategy="databaseStrategy" table-strategy="orderItemTableStrategy"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@
<import resource="datasource/dataSource.xml" />

<bean id="dataSourceRule" class="com.dangdang.ddframe.rdb.sharding.api.rule.DataSourceRule">
<constructor-arg>
<constructor-arg index="0">
<map>
<entry key="dbtbl_0" value-ref="dbtbl_0"/>
<entry key="dbtbl_1" value-ref="dbtbl_1"/>
</map>
</constructor-arg>
<constructor-arg index="1" value="dbtbl_0" />
</bean>

<bean id="orderTableRule" class="com.dangdang.ddframe.rdb.sharding.api.rule.TableRule">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<entry key="dbtbl_1" value-ref="dbtbl_1"/>
</map>
</constructor-arg>
<constructor-arg index="1" value="dbtbl_0" />
</bean>

<bean id="orderTableRule" class="com.dangdang.ddframe.rdb.sharding.api.rule.TableRule">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,3 @@ CREATE TABLE `t_order_item_0` (`order_item_id` INT NOT NULL, `order_id` INT NOT
CREATE TABLE `t_order_item_1` (`order_item_id` INT NOT NULL, `order_id` INT NOT NULL, `user_id` INT NOT NULL, `status` VARCHAR(45) NULL, PRIMARY KEY (`order_item_id`));
CREATE TABLE `t_order_item_2` (`order_item_id` INT NOT NULL, `order_id` INT NOT NULL, `user_id` INT NOT NULL, `status` VARCHAR(45) NULL, PRIMARY KEY (`order_item_id`));
CREATE TABLE `t_order_item_3` (`order_item_id` INT NOT NULL, `order_id` INT NOT NULL, `user_id` INT NOT NULL, `status` VARCHAR(45) NULL, PRIMARY KEY (`order_item_id`));
CREATE TABLE `t_config` (`id` INT NOT NULL, `status` VARCHAR(45) NULL, PRIMARY KEY (`id`));
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
* 已废弃, 请使用ShardingDataSourceFactory创建数据源. 未来版本中将删除此类.
* </p>
*
* @deprecated
* @deprecated 已废弃, 请使用ShardingDataSourceFactory创建数据源. 未来版本中将删除此类.
* @author zhangliang
*/
@Deprecated
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@

package com.dangdang.ddframe.rdb.sharding.api.rule;

import java.util.Collection;
import java.util.Map;
import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import lombok.Getter;

import javax.sql.DataSource;

import com.google.common.base.Preconditions;
import java.util.Collection;
import java.util.Map;

/**
* 数据源配置对象.
Expand All @@ -33,9 +35,26 @@ public final class DataSourceRule {

private final Map<String, DataSource> dataSourceMap;

@Getter
private final String defaultDataSourceName;

public DataSourceRule(final Map<String, DataSource> dataSourceMap) {
this(dataSourceMap, null);
}

public DataSourceRule(final Map<String, DataSource> dataSourceMap, final String defaultDataSourceName) {
Preconditions.checkState(!dataSourceMap.isEmpty(), "Must have one data source at least.");
this.dataSourceMap = dataSourceMap;
if (1 == dataSourceMap.size()) {
this.defaultDataSourceName = dataSourceMap.entrySet().iterator().next().getKey();
return;
}
if (Strings.isNullOrEmpty(defaultDataSourceName)) {
this.defaultDataSourceName = null;
return;
}
Preconditions.checkState(dataSourceMap.containsKey(defaultDataSourceName), "Data source rule must include default data source.");
this.defaultDataSourceName = defaultDataSourceName;
}

/**
Expand All @@ -48,6 +67,15 @@ public DataSource getDataSource(final String name) {
return dataSourceMap.get(name);
}

/**
* 获取默认数据源实例.
*
* @return 默认数据源实例
*/
public Optional<DataSource> getDefaultDataSource() {
return Optional.fromNullable(dataSourceMap.get(defaultDataSourceName));
}

/**
* 获取所有数据源名称.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public final class ShardingRule {
*
* <p>未来将改为private权限, 不在对外公开, 不建议使用非Spring命名空间的配置.</p>
*
* @deprecated
* @deprecated 未来将改为private权限, 不在对外公开, 不建议使用非Spring命名空间的配置.
*/
@Deprecated
public ShardingRule(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public final class TableRule {
*
* <p>未来将改为private权限, 不在对外公开, 不建议使用非Spring命名空间的配置.</p>
*
* @deprecated
* @deprecated 未来将改为private权限, 不在对外公开, 不建议使用非Spring命名空间的配置.
*/
@Deprecated
public TableRule(final String logicTable, final boolean dynamic, final List<String> actualTables, final DataSourceRule dataSourceRule,
Expand Down
Loading

0 comments on commit 5f4b92d

Please sign in to comment.