Skip to content

Commit

Permalink
Merge branch 'sharding-jdbc-transaction' to master branch.
Browse files Browse the repository at this point in the history
  • Loading branch information
haocao committed Mar 30, 2016
2 parents 15aea95 + 5cf4d8b commit 906202b
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,6 @@

package com.dangdang.ddframe.rdb.sharding.config.common.api;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.sql.DataSource;

import com.dangdang.ddframe.rdb.sharding.api.rule.BindingTableRule;
import com.dangdang.ddframe.rdb.sharding.api.rule.DataSourceRule;
import com.dangdang.ddframe.rdb.sharding.api.rule.ShardingRule;
Expand All @@ -49,6 +42,14 @@
import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import org.apache.commons.collections4.MapUtils;

import javax.sql.DataSource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* 分片规则构建器.
Expand All @@ -57,44 +58,28 @@
*/
public class ShardingRuleBuilder {

private Map<String, DataSource> externalDataSourceMap = new HashMap<>();
private final String logRoot;

private ShardingRuleConfig config;
private final Map<String, DataSource> externalDataSourceMap;

private String logRoot;
private final ShardingRuleConfig shardingRuleConfig;

/**
* 设置数据源映射.
*
* @param externalDataSourceMap 数据源映射
* @return 构建器对象
*/
public ShardingRuleBuilder setExternalDataSourceMap(final Map<String, DataSource> externalDataSourceMap) {
this.externalDataSourceMap.putAll(externalDataSourceMap);
return this;
public ShardingRuleBuilder(final ShardingRuleConfig shardingRuleConfig) {
logRoot = "default";
externalDataSourceMap = new HashMap<>();
this.shardingRuleConfig = shardingRuleConfig;
}

/**
* 解析规则配置对象中的规则配置.
*
* @param config 规则配置对象
* @return 构建器对象
*/
public ShardingRuleBuilder parse(final ShardingRuleConfig config) {
return parse("default", config);
public ShardingRuleBuilder(final String logRoot, final ShardingRuleConfig shardingRuleConfig) {
this.logRoot = logRoot;
externalDataSourceMap = new HashMap<>();
this.shardingRuleConfig = shardingRuleConfig;
}

/**
* 解析规则配置对象中的规则配置.
*
* @param logRoot 规则名称
* @param config 规则配置对象
* @return 构建器对象
*/
public ShardingRuleBuilder parse(final String logRoot, final ShardingRuleConfig config) {
this.config = config;
public ShardingRuleBuilder(final String logRoot, final Map<String, DataSource> externalDataSourceMap, final ShardingRuleConfig shardingRuleConfig) {
this.logRoot = logRoot;
return this;
this.externalDataSourceMap = externalDataSourceMap;
this.shardingRuleConfig = shardingRuleConfig;
}

/**
Expand All @@ -106,18 +91,18 @@ public ShardingRule build() {
DataSourceRule dataSourceRule = buildDataSourceRule();
Collection<TableRule> tableRules = buildTableRule(dataSourceRule);
return new ShardingRule(dataSourceRule, tableRules, buildBindingTableRule(tableRules),
buildShardingStrategy(config.getDefaultDatabaseStrategy(), DatabaseShardingStrategy.class),
buildShardingStrategy(config.getDefaultTableStrategy(), TableShardingStrategy.class));
buildShardingStrategy(shardingRuleConfig.getDefaultDatabaseStrategy(), DatabaseShardingStrategy.class),
buildShardingStrategy(shardingRuleConfig.getDefaultTableStrategy(), TableShardingStrategy.class));
}

private DataSourceRule buildDataSourceRule() {
Preconditions.checkArgument(config.getDataSource().size() > 0 || null != externalDataSourceMap && externalDataSourceMap.size() > 0, "Sharding JDBC: No data source config");
return config.getDataSource().size() > 0 ? new DataSourceRule(config.getDataSource()) : new DataSourceRule(externalDataSourceMap);
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);
}

private Collection<TableRule> buildTableRule(final DataSourceRule dataSourceRule) {
Collection<TableRule> result = new ArrayList<>(config.getTables().size());
for (Map.Entry<String, TableRuleConfig> each : config.getTables().entrySet()) {
Collection<TableRule> result = new ArrayList<>(shardingRuleConfig.getTables().size());
for (Map.Entry<String, TableRuleConfig> each : shardingRuleConfig.getTables().entrySet()) {
result.add(new TableRule(each.getKey(), ConfigUtil.transformCommaStringToList(each.getValue().getActualTables()), dataSourceRule,
buildShardingStrategy(each.getValue().getDatabaseStrategy(), DatabaseShardingStrategy.class),
buildShardingStrategy(each.getValue().getTableStrategy(), TableShardingStrategy.class)));
Expand All @@ -126,8 +111,8 @@ private Collection<TableRule> buildTableRule(final DataSourceRule dataSourceRule
}

private Collection<BindingTableRule> buildBindingTableRule(final Collection<TableRule> tableRules) {
Collection<BindingTableRule> result = new ArrayList<>(config.getBindingTables().size());
for (BindingTableRuleConfig each : config.getBindingTables()) {
Collection<BindingTableRule> result = new ArrayList<>(shardingRuleConfig.getBindingTables().size());
for (BindingTableRuleConfig each : shardingRuleConfig.getBindingTables()) {
result.add(new BindingTableRule(Lists.transform(ConfigUtil.transformCommaStringToList(each.getTableNames()), new Function<String, TableRule>() {
@Override
public TableRule apply(final String input) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public final class ConfigUtil {
public static List<String> transformCommaStringToList(final String strWithComma) {
final GroovyShell shell = new GroovyShell();
return flattenList(Lists.transform(splitComma(strWithComma), new Function<String, Object>() {

@Override
public Object apply(final String input) {
String compactInput = input.trim();
Expand All @@ -57,7 +58,7 @@ public Object apply(final String input) {
}));
}

private static List<String> flattenList(final List list) {
private static List<String> flattenList(final List<?> list) {
List<String> result = new ArrayList<>();
for (Object each : list) {
if (each instanceof GString) {
Expand All @@ -71,21 +72,12 @@ private static List<String> flattenList(final List list) {

private static List<String> splitComma(final String strWithComma) {
List<String> result = new ArrayList<>();
int offset = -1;
StringBuilder token = new StringBuilder();
int closureDepth = 0;
boolean isStartMatch = false;
while (++offset < strWithComma.length()) {
char c = strWithComma.charAt(offset);
for (int i = 0; i < strWithComma.length(); i++) {
char c = strWithComma.charAt(i);
switch (c) {
case ',':
if (closureDepth > 0) {
token.append(c);
} else {
result.add(token.toString());
token.setLength(0);
}
break;
case '$':
isStartMatch = true;
token.append(c);
Expand All @@ -100,6 +92,14 @@ private static List<String> splitComma(final String strWithComma) {
isStartMatch = false;
token.append(c);
break;
case ',':
if (closureDepth > 0) {
token.append(c);
} else {
result.add(token.toString());
token.setLength(0);
}
break;
default:
token.append(c);
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public class ShardingRuleBuilderTest {
public void testAll() {
Yaml yaml = new Yaml(new Constructor(ShardingRuleConfig.class));
ShardingRuleConfig config = (ShardingRuleConfig) yaml.load(ShardingRuleBuilderTest.class.getResourceAsStream("/config/config-all.yaml"));
ShardingRule shardingRule = new ShardingRuleBuilder().parse(config).build();
ShardingRule shardingRule = new ShardingRuleBuilder("config-all.yaml", config).build();
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()));
Expand All @@ -54,7 +54,7 @@ public void testMin() {
Map<String, DataSource> dsMap = new HashMap<>();
dsMap.put("ds", new BasicDataSource());
ShardingRuleConfig config = (ShardingRuleConfig) yaml.load(ShardingRuleBuilderTest.class.getResourceAsStream("/config/config-min.yaml"));
ShardingRule shardingRule = new ShardingRuleBuilder().setExternalDataSourceMap(dsMap).parse(config).build();
ShardingRule shardingRule = new ShardingRuleBuilder("config-min.yaml", dsMap, config).build();
assertThat(shardingRule.getTableRules().size(), is(1));
}

Expand All @@ -64,7 +64,7 @@ public void testClassNotFound() {
Map<String, DataSource> dsMap = new HashMap<>();
dsMap.put("ds", new BasicDataSource());
ShardingRuleConfig config = (ShardingRuleConfig) yaml.load(ShardingRuleBuilderTest.class.getResourceAsStream("/config/config-classNotFound.yaml"));
new ShardingRuleBuilder().setExternalDataSourceMap(dsMap).parse(config).build();
new ShardingRuleBuilder("config-classNotFound.yaml", dsMap, config).build();
}

@Test(expected = IllegalArgumentException.class)
Expand All @@ -73,7 +73,7 @@ public void testBindingError() {
Map<String, DataSource> dsMap = new HashMap<>();
dsMap.put("ds", new BasicDataSource());
ShardingRuleConfig config = (ShardingRuleConfig) yaml.load(ShardingRuleBuilderTest.class.getResourceAsStream("/config/config-bindingError.yaml"));
ShardingRule shardingRule = new ShardingRuleBuilder().setExternalDataSourceMap(dsMap).parse(config).build();
ShardingRule shardingRule = new ShardingRuleBuilder("config-bindingError.yaml", dsMap, config).build();
for (TableRule tableRule : shardingRule.getBindingTableRules().iterator().next().getTableRules()) {
log.info(tableRule.toString());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<pattern>[%-5level] %d{HH:mm:ss.SSS} [%thread] %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<logger name="com.dangdang" level="info" additivity="false">
<logger name="com.dangdang" level="warn" additivity="false">
<appender-ref ref="console"/>
</logger>
<root>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@
public class SpringShardingDataSource extends ShardingDataSource {

public SpringShardingDataSource(final ShardingRuleConfig shardingRuleConfig, final Properties props) {
super(new ShardingRuleBuilder().parse(shardingRuleConfig).build(), props);
super(new ShardingRuleBuilder(shardingRuleConfig).build(), props);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@
public class YamlShardingDataSource extends ShardingDataSource {

public YamlShardingDataSource(final File yamlFile) throws IOException {
super(new ShardingRuleBuilder().parse(yamlFile.getName(), parse(yamlFile)).build(), parse(yamlFile).getProps());
super(new ShardingRuleBuilder(yamlFile.getName(), unmarshal(yamlFile)).build(), unmarshal(yamlFile).getProps());
}

public YamlShardingDataSource(final Map<String, DataSource> dataSource, final File yamlFile) throws IOException {
super(new ShardingRuleBuilder().setExternalDataSourceMap(dataSource).parse(yamlFile.getName(), parse(yamlFile)).build(), parse(yamlFile).getProps());
super(new ShardingRuleBuilder(yamlFile.getName(), dataSource, unmarshal(yamlFile)).build(), unmarshal(yamlFile).getProps());
}

private static YamlConfig parse(final File yamlFile) throws IOException {
private static YamlConfig unmarshal(final File yamlFile) throws IOException {
return new Yaml(new Constructor(YamlConfig.class)).loadAs(new InputStreamReader(new FileInputStream(yamlFile), "UTF-8"), YamlConfig.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,4 @@
public class YamlConfig extends ShardingRuleConfig {

private Properties props = new Properties();

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<pattern>[%-5level] %d{HH:mm:ss.SSS} [%thread] %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<logger name="com.dangdang" level="info" additivity="false">
<logger name="com.dangdang" level="warn" additivity="false">
<appender-ref ref="console"/>
</logger>
<root>
Expand Down

0 comments on commit 906202b

Please sign in to comment.