未验证 提交 cfbd2acb 编写于 作者: J Juan Pan(Trista) 提交者: GitHub

CreateAutoTableRule (#5951)

上级 d8893752
......@@ -104,11 +104,11 @@ public final class TableRule {
}
public TableRule(final ShardingAutoTableRuleConfiguration tableRuleConfig, final Collection<String> dataSourceNames, final String defaultGenerateKeyColumn) {
Preconditions.checkArgument(null == tableRuleConfig.getShardingStrategy() || tableRuleConfig.getShardingStrategy() instanceof ShardingAutoTableAlgorithm,
"ShardingAutoTableAlgorithm is required.");
logicTable = tableRuleConfig.getLogicTable().toLowerCase();
databaseShardingStrategy = null;
tableShardingStrategy = null == tableRuleConfig.getShardingStrategy() ? null : ShardingStrategyFactory.newInstance(tableRuleConfig.getShardingStrategy());
Preconditions.checkArgument(null == tableRuleConfig.getShardingStrategy() || tableShardingStrategy.getShardingAlgorithm() instanceof ShardingAutoTableAlgorithm,
"ShardingAutoTableAlgorithm is required.");
List<String> dataNodes = getDataNodes(tableRuleConfig, dataSourceNames);
dataNodeIndexMap = new HashMap<>(dataNodes.size(), 1);
actualDataNodes = isEmptyDataNodes(dataNodes) ? generateDataNodes(tableRuleConfig.getLogicTable(), dataSourceNames) : generateDataNodes(dataNodes, dataSourceNames);
......
......@@ -18,17 +18,18 @@
package org.apache.shardingsphere.sharding.rule;
import com.google.common.collect.Sets;
import org.apache.shardingsphere.infra.config.exception.ShardingSphereConfigurationException;
import org.apache.shardingsphere.infra.datanode.DataNode;
import org.apache.shardingsphere.infra.spi.ShardingSphereServiceLoader;
import org.apache.shardingsphere.infra.spi.type.TypedSPIRegistry;
import org.apache.shardingsphere.sharding.api.config.KeyGeneratorConfiguration;
import org.apache.shardingsphere.sharding.api.config.ShardingAutoTableRuleConfiguration;
import org.apache.shardingsphere.sharding.api.config.ShardingTableRuleConfiguration;
import org.apache.shardingsphere.sharding.api.config.strategy.NoneShardingStrategyConfiguration;
import org.apache.shardingsphere.sharding.api.config.strategy.StandardShardingStrategyConfiguration;
import org.apache.shardingsphere.sharding.spi.keygen.KeyGenerateAlgorithm;
import org.apache.shardingsphere.sharding.strategy.algorithm.keygen.fixture.IncrementKeyGenerateAlgorithm;
import org.apache.shardingsphere.sharding.strategy.algorithm.sharding.inline.InlineShardingAlgorithm;
import org.apache.shardingsphere.infra.spi.ShardingSphereServiceLoader;
import org.apache.shardingsphere.sharding.spi.keygen.KeyGenerateAlgorithm;
import org.apache.shardingsphere.infra.spi.type.TypedSPIRegistry;
import org.apache.shardingsphere.infra.config.exception.ShardingSphereConfigurationException;
import org.apache.shardingsphere.infra.datanode.DataNode;
import org.junit.BeforeClass;
import org.junit.Test;
......@@ -86,6 +87,46 @@ public final class TableRuleTest {
assertThat(actual.getGenerateKeyColumn().get(), is("col_1"));
assertThat(actual.getKeyGenerateAlgorithm(), instanceOf(IncrementKeyGenerateAlgorithm.class));
}
@Test
public void assertCreateAutoTableRuleWithActualDataSources() {
ShardingSphereServiceLoader.register(KeyGenerateAlgorithm.class);
ShardingAutoTableRuleConfiguration tableRuleConfig = new ShardingAutoTableRuleConfiguration("LOGIC_TABLE", "ds0,ds1");
InlineShardingAlgorithm shardingAlgorithm = new InlineShardingAlgorithm();
Properties properties = new Properties();
properties.setProperty("algorithm.expression", "ds${0..1}.logic_table_${0..1}");
shardingAlgorithm.setProperties(properties);
tableRuleConfig.setShardingStrategy(new StandardShardingStrategyConfiguration("col_1", shardingAlgorithm));
TableRule actual = new TableRule(tableRuleConfig, Arrays.asList("ds0", "ds1", "ds2"), null);
assertThat(actual.getLogicTable(), is("logic_table"));
assertThat(actual.getActualDataNodes().size(), is(4));
assertTrue(actual.getActualDataNodes().contains(new DataNode("ds0", "logic_table_0")));
assertTrue(actual.getActualDataNodes().contains(new DataNode("ds1", "logic_table_1")));
assertTrue(actual.getActualDataNodes().contains(new DataNode("ds0", "logic_table_2")));
assertTrue(actual.getActualDataNodes().contains(new DataNode("ds1", "logic_table_3")));
assertNull(actual.getDatabaseShardingStrategy());
assertNotNull(actual.getTableShardingStrategy());
}
@Test
public void assertCreateAutoTableRuleWithoutActualDataSources() {
ShardingSphereServiceLoader.register(KeyGenerateAlgorithm.class);
ShardingAutoTableRuleConfiguration tableRuleConfig = new ShardingAutoTableRuleConfiguration("LOGIC_TABLE", null);
InlineShardingAlgorithm shardingAlgorithm = new InlineShardingAlgorithm();
Properties properties = new Properties();
properties.setProperty("algorithm.expression", "ds${0..1}.logic_table_${0..1}");
shardingAlgorithm.setProperties(properties);
tableRuleConfig.setShardingStrategy(new StandardShardingStrategyConfiguration("col_1", shardingAlgorithm));
TableRule actual = new TableRule(tableRuleConfig, Arrays.asList("ds0", "ds1", "ds2"), null);
assertThat(actual.getLogicTable(), is("logic_table"));
assertThat(actual.getActualDataNodes().size(), is(4));
assertTrue(actual.getActualDataNodes().contains(new DataNode("ds0", "logic_table_0")));
assertTrue(actual.getActualDataNodes().contains(new DataNode("ds1", "logic_table_1")));
assertTrue(actual.getActualDataNodes().contains(new DataNode("ds2", "logic_table_2")));
assertTrue(actual.getActualDataNodes().contains(new DataNode("ds0", "logic_table_3")));
assertNull(actual.getDatabaseShardingStrategy());
assertNotNull(actual.getTableShardingStrategy());
}
@Test
public void assertGetActualDatasourceNames() {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册