提交 ea9d6aa2 编写于 作者: T terrymanu

rule => rule config 12th version, for yaml, but still break test cases

上级 0705f79e
......@@ -10,11 +10,6 @@
</parent>
<artifactId>sharding-jdbc-config-yaml</artifactId>
<dependencies>
<dependency>
<groupId>com.dangdang</groupId>
<artifactId>sharding-jdbc-config-common</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
......
......@@ -17,7 +17,7 @@
package com.dangdang.ddframe.rdb.sharding.config.yaml.api;
import com.dangdang.ddframe.rdb.sharding.config.common.api.ShardingRuleBuilder;
import com.dangdang.ddframe.rdb.sharding.config.yaml.internel.ShardingRuleBuilder;
import com.dangdang.ddframe.rdb.sharding.config.yaml.internel.YamlConfig;
import com.dangdang.ddframe.rdb.sharding.jdbc.core.datasource.ShardingDataSource;
import org.yaml.snakeyaml.Yaml;
......@@ -30,27 +30,33 @@ import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.SQLException;
import java.util.Collections;
import java.util.Map;
/**
* Sharding datasource for yaml.
*
* @author gaohongtao
* @author zhangliang
*/
public class YamlShardingDataSource extends ShardingDataSource {
public YamlShardingDataSource(final File yamlFile) throws IOException, SQLException {
super(new ShardingRuleBuilder(yamlFile.getName(), unmarshal(yamlFile)).build(), unmarshal(yamlFile).getProps());
super(new ShardingRuleBuilder(Collections.<String, DataSource>emptyMap(), unmarshal(yamlFile)).build(), unmarshal(yamlFile).getProps());
}
public YamlShardingDataSource(final Map<String, DataSource> dataSource, final File yamlFile) throws IOException, SQLException {
super(new ShardingRuleBuilder(yamlFile.getName(), dataSource, unmarshal(yamlFile)).build(), unmarshal(yamlFile).getProps());
public YamlShardingDataSource(final Map<String, DataSource> dataSourceMap, final File yamlFile) throws IOException, SQLException {
super(new ShardingRuleBuilder(dataSourceMap, unmarshal(yamlFile)).build(), unmarshal(yamlFile).getProps());
}
public YamlShardingDataSource(final String logRoot, final byte[] yamlByteArray) throws IOException, SQLException {
super(new ShardingRuleBuilder(logRoot, unmarshal(yamlByteArray)).build(), unmarshal(yamlByteArray).getProps());
public YamlShardingDataSource(final byte[] yamlByteArray) throws IOException, SQLException {
super(new ShardingRuleBuilder(Collections.<String, DataSource>emptyMap(), unmarshal(yamlByteArray)).build(), unmarshal(yamlByteArray).getProps());
}
public YamlShardingDataSource(final Map<String, DataSource> dataSourceMap, final byte[] yamlByteArray) throws IOException, SQLException {
super(new ShardingRuleBuilder(dataSourceMap, unmarshal(yamlByteArray)).build(), unmarshal(yamlByteArray).getProps());
}
private static YamlConfig unmarshal(final File yamlFile) throws IOException {
try (
FileInputStream fileInputStream = new FileInputStream(yamlFile);
......@@ -59,9 +65,8 @@ public class YamlShardingDataSource extends ShardingDataSource {
return new Yaml(new Constructor(YamlConfig.class)).loadAs(inputStreamReader, YamlConfig.class);
}
}
private static YamlConfig unmarshal(final byte[] yamlByteArray) throws IOException {
return new Yaml(new Constructor(YamlConfig.class)).loadAs(new ByteArrayInputStream(yamlByteArray), YamlConfig.class);
}
}
/*
* 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.yaml.internel;
import com.dangdang.ddframe.rdb.sharding.api.config.ShardingRuleConfig;
import com.dangdang.ddframe.rdb.sharding.api.config.TableRuleConfig;
import com.dangdang.ddframe.rdb.sharding.rule.ShardingRule;
import lombok.RequiredArgsConstructor;
import javax.sql.DataSource;
import java.util.Map;
import java.util.Map.Entry;
/**
* Sharding rule builder from yaml.
*
* @author zhangliang
*/
@RequiredArgsConstructor
public final class ShardingRuleBuilder {
private final Map<String, DataSource> dataSourceMap;
private final YamlConfig yamlConfig;
/**
* Build sharding rule from yaml.
*
* @return sharding rule from yaml
*/
public ShardingRule build() {
ShardingRuleConfig result = new ShardingRuleConfig();
if (dataSourceMap.isEmpty()) {
result.setDataSources(yamlConfig.getDataSources());
} else {
result.setDataSources(dataSourceMap);
}
result.setDefaultDataSourceName(yamlConfig.getDefaultDataSourceName());
for (Entry<String, TableRuleConfig> entry : yamlConfig.getTables().entrySet()) {
TableRuleConfig tableRuleConfig = entry.getValue();
tableRuleConfig.setLogicTable(entry.getKey());
result.getTableRuleConfigs().add(tableRuleConfig);
}
result.getBindingTableGroups().addAll(yamlConfig.getBindingTableGroups());
result.setDefaultDatabaseShardingStrategyConfig(yamlConfig.getDefaultDatabaseStrategy());
result.setDefaultTableShardingStrategyConfig(yamlConfig.getDefaultTableStrategy());
result.setDefaultKeyGeneratorClass(yamlConfig.getDefaultKeyGeneratorClass());
return result.build();
}
}
......@@ -17,10 +17,16 @@
package com.dangdang.ddframe.rdb.sharding.config.yaml.internel;
import com.dangdang.ddframe.rdb.sharding.config.common.api.config.ShardingRuleConfig;
import com.dangdang.ddframe.rdb.sharding.api.config.TableRuleConfig;
import com.dangdang.ddframe.rdb.sharding.api.config.strategy.ShardingStrategyConfig;
import lombok.Getter;
import lombok.Setter;
import javax.sql.DataSource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
/**
......@@ -30,7 +36,21 @@ import java.util.Properties;
*/
@Getter
@Setter
public class YamlConfig extends ShardingRuleConfig {
public class YamlConfig {
private Map<String, DataSource> dataSources = new HashMap<>();
private String defaultDataSourceName;
private Map<String, TableRuleConfig> tables = new HashMap<>();
private List<String> bindingTableGroups = new ArrayList<>();
private ShardingStrategyConfig defaultDatabaseStrategy;
private ShardingStrategyConfig defaultTableStrategy;
private String defaultKeyGeneratorClass;
private Properties props = new Properties();
}
......@@ -17,7 +17,7 @@
package com.dangdang.ddframe.rdb.sharding.config;
import com.dangdang.ddframe.rdb.sharding.config.yaml.YamlIntegratedTest;
import com.dangdang.ddframe.rdb.sharding.config.yaml.YamlIntegrateTest;
import com.dangdang.ddframe.rdb.sharding.config.yaml.YamlShardingDataSourceTest;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
......@@ -25,7 +25,7 @@ import org.junit.runners.Suite;
@RunWith(Suite.class)
@Suite.SuiteClasses({
YamlShardingDataSourceTest.class,
YamlIntegratedTest.class
YamlIntegrateTest.class
})
public class AllYamlTests {
}
......@@ -38,7 +38,7 @@ import java.util.Collection;
@RunWith(Parameterized.class)
@RequiredArgsConstructor
public class YamlIntegratedTest extends AbstractYamlShardingDataSourceTest {
public class YamlIntegrateTest extends AbstractYamlShardingDataSourceTest {
private final String filePath;
......@@ -56,7 +56,7 @@ public class YamlIntegratedTest extends AbstractYamlShardingDataSourceTest {
@Test
public void testWithDataSource() throws SQLException, URISyntaxException, IOException {
File yamlFile = new File(YamlIntegratedTest.class.getResource(filePath).toURI());
File yamlFile = new File(YamlIntegrateTest.class.getResource(filePath).toURI());
DataSource dataSource;
if (hasDataSource) {
dataSource = new YamlShardingDataSource(yamlFile);
......
dataSource:
dataSources:
db0: !!org.apache.commons.dbcp.BasicDataSource
driverClassName: org.h2.Driver
url: jdbc:h2:mem:%s;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MYSQL
......@@ -14,7 +14,7 @@ dataSource:
defaultDataSourceName: db0
keyGeneratorClass: com.dangdang.ddframe.rdb.sharding.config.yaml.fixture.IncrementKeyGenerator
defaultKeyGeneratorClass: com.dangdang.ddframe.rdb.sharding.config.yaml.fixture.IncrementKeyGenerator
tables:
config:
......@@ -23,26 +23,24 @@ tables:
t_order:
actualTables: t_order_${0..1}
generateKeyColumns:
- columnName: order_id
databaseStrategy: &db001
keyGeneratorColumnName: order_id
databaseShardingStrategyConfig: &db001
shardingColumns: order_id
algorithmClassName: com.dangdang.ddframe.rdb.sharding.config.yaml.algorithm.SingleAlgorithm
tableStrategy: &table001
tableShardingStrategyConfig: &table001
shardingColumns: id
algorithmExpression: t_order_${id % 2}
t_order_item:
actualTables: t_order_item_${0..1}
generateKeyColumns:
- columnName: order_item_id
columnKeyGeneratorClass: com.dangdang.ddframe.rdb.sharding.config.yaml.fixture.DecrementKeyGenerator
keyGeneratorColumnName: order_item_id
columnKeyGeneratorClass: com.dangdang.ddframe.rdb.sharding.config.yaml.fixture.DecrementKeyGenerator
#绑定表中其余的表的策略与第一张表的策略相同
databaseStrategy: *db001
tableStrategy: *table001
bindingTables:
- tableNames: t_order,t_order_item
bindingTableGroups: t_order,t_order_item
#默认数据库分片策略
defaultDatabaseStrategy:
shardingColumns: order_id, user_id
......
dataSource:
dataSources:
db0: !!org.apache.commons.dbcp.BasicDataSource
driverClassName: org.h2.Driver
url: jdbc:h2:mem:%s;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MYSQL
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册