Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ void assertCheckCreateSuccess() {
@Test
void assertCheckAlterSuccess() {
Collection<AbstractTableRuleSegment> rules = new LinkedList<>();
AutoTableRuleSegment autoTableRuleSegment = new AutoTableRuleSegment("t_order", Arrays.asList("ds_0", "ds_1"));
AutoTableRuleSegment autoTableRuleSegment = new AutoTableRuleSegment("t_order", Arrays.asList("ds_0", "ds_1"), null, null);
autoTableRuleSegment.setShardingColumn("order_id");
autoTableRuleSegment.setShardingAlgorithmSegment(new AlgorithmSegment("CORE.AUTO.FIXTURE", PropertiesBuilder.build(new Property("sharding-count", "2"))));
rules.add(autoTableRuleSegment);
Expand All @@ -107,11 +107,11 @@ void assertCheckerBindingTableSuccess() {
ShardingRuleConfiguration shardingRuleConfig = createShardingRuleConfiguration();
shardingRuleConfig.getBindingTableGroups().add(new ShardingTableReferenceRuleConfiguration("reference_0", "t_order,t_order_item"));
Collection<AbstractTableRuleSegment> rules = new LinkedList<>();
AutoTableRuleSegment autoTable1 = new AutoTableRuleSegment("t_order", Arrays.asList("ds_0", "ds_1"));
AutoTableRuleSegment autoTable1 = new AutoTableRuleSegment("t_order", Arrays.asList("ds_0", "ds_1"), null, null);
autoTable1.setShardingColumn("order_id");
autoTable1.setShardingAlgorithmSegment(new AlgorithmSegment("CORE.AUTO.FIXTURE", PropertiesBuilder.build(new Property("sharding-count", "2"))));
rules.add(autoTable1);
AutoTableRuleSegment autoTable2 = new AutoTableRuleSegment("t_order_item", Arrays.asList("ds_0", "ds_1"));
AutoTableRuleSegment autoTable2 = new AutoTableRuleSegment("t_order_item", Arrays.asList("ds_0", "ds_1"), null, null);
autoTable2.setShardingColumn("order_id");
autoTable2.setShardingAlgorithmSegment(new AlgorithmSegment("CORE.AUTO.FIXTURE", PropertiesBuilder.build(new Property("sharding-count", "2"))));
rules.add(autoTable2);
Expand All @@ -121,49 +121,49 @@ void assertCheckerBindingTableSuccess() {
@Test
void assertCheckCreationWithDuplicated() {
List<AbstractTableRuleSegment> rules = Arrays.asList(
new AutoTableRuleSegment("t_order_duplicated", Arrays.asList("ds_0", "ds_1")),
new AutoTableRuleSegment("t_order_duplicated", Arrays.asList("ds_0", "ds_1")));
new AutoTableRuleSegment("t_order_duplicated", Arrays.asList("ds_0", "ds_1"), null, null),
new AutoTableRuleSegment("t_order_duplicated", Arrays.asList("ds_0", "ds_1"), null, null));
assertThrows(DuplicateRuleException.class, () -> ShardingTableRuleStatementChecker.checkCreation(database, rules, false, shardingRuleConfig));
}

@Test
void assertCheckCreationWithIdentical() {
Collection<AbstractTableRuleSegment> rules = Collections.singleton(new AutoTableRuleSegment("t_order", Arrays.asList("ds_0", "ds_1")));
Collection<AbstractTableRuleSegment> rules = Collections.singleton(new AutoTableRuleSegment("t_order", Arrays.asList("ds_0", "ds_1"), null, null));
assertThrows(DuplicateRuleException.class, () -> ShardingTableRuleStatementChecker.checkCreation(database, rules, false, shardingRuleConfig));
}

@Test
void assertCheckAlterationWithRuleRequiredMissed() {
Collection<AbstractTableRuleSegment> rules = Collections.singleton(new AutoTableRuleSegment("t_order_required_missed", Arrays.asList("ds_0", "ds_1")));
Collection<AbstractTableRuleSegment> rules = Collections.singleton(new AutoTableRuleSegment("t_order_required_missed", Arrays.asList("ds_0", "ds_1"), null, null));
assertThrows(MissingRequiredRuleException.class, () -> ShardingTableRuleStatementChecker.checkAlteration(database, rules, shardingRuleConfig));
}

@Test
void assertCheckCreationWithResourceRequiredMissed() {
Collection<AbstractTableRuleSegment> rules = Collections.singleton(new AutoTableRuleSegment("t_product", Arrays.asList("ds_required_missed", "ds_1")));
Collection<AbstractTableRuleSegment> rules = Collections.singleton(new AutoTableRuleSegment("t_product", Arrays.asList("ds_required_missed", "ds_1"), null, null));
assertThrows(MissingRequiredStorageUnitsException.class, () -> ShardingTableRuleStatementChecker.checkCreation(database, rules, false, shardingRuleConfig));
}

@Test
void assertCheckCreationWithInvalidKeyGenerateAlgorithm() {
AutoTableRuleSegment autoTableRuleSegment = new AutoTableRuleSegment("t_product", Arrays.asList("ds_0", "ds_1"));
autoTableRuleSegment.setKeyGenerateStrategySegment(new KeyGenerateStrategySegment("product_id", new AlgorithmSegment("invalid", PropertiesBuilder.build(new Property("invalid", "invalid")))));
KeyGenerateStrategySegment keyGenerator = new KeyGenerateStrategySegment("product_id", new AlgorithmSegment("invalid", PropertiesBuilder.build(new Property("invalid", "invalid"))));
AutoTableRuleSegment autoTableRuleSegment = new AutoTableRuleSegment("t_product", Arrays.asList("ds_0", "ds_1"), keyGenerator, null);
assertThrows(ServiceProviderNotFoundException.class,
() -> ShardingTableRuleStatementChecker.checkCreation(database, Collections.singleton(autoTableRuleSegment), false, shardingRuleConfig));
}

@Test
void assertCheckCreationWithInvalidAuditAlgorithm() {
AutoTableRuleSegment autoTableRuleSegment = new AutoTableRuleSegment("t_product", Arrays.asList("ds_0", "ds_1"));
autoTableRuleSegment.setAuditStrategySegment(new AuditStrategySegment(Collections.singleton(new ShardingAuditorSegment("sharding_key_required_auditor",
new AlgorithmSegment("invalid", new Properties()))), true));
AuditStrategySegment auditStrategySegment = new AuditStrategySegment(Collections.singleton(new ShardingAuditorSegment("sharding_key_required_auditor",
new AlgorithmSegment("invalid", new Properties()))), true);
AutoTableRuleSegment autoTableRuleSegment = new AutoTableRuleSegment("t_product", Arrays.asList("ds_0", "ds_1"), null, auditStrategySegment);
assertThrows(ServiceProviderNotFoundException.class,
() -> ShardingTableRuleStatementChecker.checkCreation(database, Collections.singleton(autoTableRuleSegment), false, shardingRuleConfig));
}

@Test
void assertCheckAutoTableWithNotExistShardingAlgorithms() {
AutoTableRuleSegment autoTableRuleSegment = new AutoTableRuleSegment("t_product", Arrays.asList("ds_0", "ds_1"));
AutoTableRuleSegment autoTableRuleSegment = new AutoTableRuleSegment("t_product", Arrays.asList("ds_0", "ds_1"), null, null);
autoTableRuleSegment.setShardingColumn("product_id");
autoTableRuleSegment.setShardingAlgorithmSegment(new AlgorithmSegment("not_exist", PropertiesBuilder.build(new Property("", ""))));
Collection<AbstractTableRuleSegment> rules = Collections.singleton(autoTableRuleSegment);
Expand All @@ -172,7 +172,7 @@ void assertCheckAutoTableWithNotExistShardingAlgorithms() {

@Test
void assertCheckAutoTableWithComplexShardingAlgorithms() {
AutoTableRuleSegment autoTableRuleSegment = new AutoTableRuleSegment("t_product", Arrays.asList("ds_0", "ds_1"));
AutoTableRuleSegment autoTableRuleSegment = new AutoTableRuleSegment("t_product", Arrays.asList("ds_0", "ds_1"), null, null);
autoTableRuleSegment.setShardingColumn("product_id");
autoTableRuleSegment.setShardingAlgorithmSegment(new AlgorithmSegment("complex", PropertiesBuilder.build(new Property("", ""))));
Collection<AbstractTableRuleSegment> rules = Collections.singleton(autoTableRuleSegment);
Expand Down Expand Up @@ -255,15 +255,15 @@ void assertCheckNullAlgorithmNameAndNullAlgorithmSegment() {

@Test
void assertCheckAutoTableRuleWithStandardShardingAlgorithm() {
AutoTableRuleSegment autoTableRuleSegment = new AutoTableRuleSegment("t_product", Arrays.asList("ds_0", "ds_1"));
AutoTableRuleSegment autoTableRuleSegment = new AutoTableRuleSegment("t_product", Arrays.asList("ds_0", "ds_1"), null, null);
autoTableRuleSegment.setShardingAlgorithmSegment(new AlgorithmSegment("INLINE", PropertiesBuilder.build(new Property("algorithm-expression", "ds_${product_id % 2}"))));
Collection<AbstractTableRuleSegment> rules = Collections.singleton(autoTableRuleSegment);
assertThrows(AlgorithmInitializationException.class, () -> ShardingTableRuleStatementChecker.checkCreation(database, rules, false, shardingRuleConfig));
}

@Test
void assertCheckAutoTableRuleWithAutoShardingAlgorithm() {
AutoTableRuleSegment autoTableRuleSegment = new AutoTableRuleSegment("t_product", Arrays.asList("ds_0", "ds_1"));
AutoTableRuleSegment autoTableRuleSegment = new AutoTableRuleSegment("t_product", Arrays.asList("ds_0", "ds_1"), null, null);
autoTableRuleSegment.setShardingAlgorithmSegment(new AlgorithmSegment("CORE.AUTO.FIXTURE", PropertiesBuilder.build(new Property("sharding-count", "4"))));
Collection<AbstractTableRuleSegment> rules = Collections.singleton(autoTableRuleSegment);
ShardingTableRuleStatementChecker.checkCreation(database, rules, false, shardingRuleConfig);
Expand Down Expand Up @@ -314,8 +314,8 @@ private Map<String, DataSource> createDataSource() {
}

private AutoTableRuleSegment createCompleteAutoTableRule() {
AutoTableRuleSegment result = new AutoTableRuleSegment("t_product_0", Arrays.asList("ds_0", "ds_1"));
result.setKeyGenerateStrategySegment(new KeyGenerateStrategySegment("product_id", new AlgorithmSegment("DISTSQL.FIXTURE", new Properties())));
KeyGenerateStrategySegment keyGenerator = new KeyGenerateStrategySegment("product_id", new AlgorithmSegment("DISTSQL.FIXTURE", new Properties()));
AutoTableRuleSegment result = new AutoTableRuleSegment("t_product_0", Arrays.asList("ds_0", "ds_1"), keyGenerator, null);
result.setShardingColumn("product_id");
result.setShardingAlgorithmSegment(new AlgorithmSegment("FOO.DISTSQL.FIXTURE", PropertiesBuilder.build(new Property("", ""))));
return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,19 +96,17 @@ private Collection<AbstractTableRuleSegment> createNoneStrategyTypeTableRuleSegm
}

private Collection<AbstractTableRuleSegment> createTableRuleSegment1() {
AutoTableRuleSegment autoTableRuleSegment1 = new AutoTableRuleSegment("t_order", Arrays.asList("ds0", "ds1"));
AutoTableRuleSegment autoTableRuleSegment1 = new AutoTableRuleSegment("t_order", Arrays.asList("ds0", "ds1"), null, null);
autoTableRuleSegment1.setShardingColumn("order_id");
autoTableRuleSegment1.setShardingAlgorithmSegment(new AlgorithmSegment("MOD", PropertiesBuilder.build(new Property("sharding_count", "2"))));
AutoTableRuleSegment autoTableRuleSegment2 = new AutoTableRuleSegment("t_order_2", Arrays.asList("ds0", "ds1"));
KeyGenerateStrategySegment keyGenerator = new KeyGenerateStrategySegment("order_id", new AlgorithmSegment("snowflake", new Properties()));
AuditStrategySegment auditStrategySegment = new AuditStrategySegment(Collections.singleton(new ShardingAuditorSegment("sharding_key_required_auditor",
new AlgorithmSegment("DML_SHARDING_CONDITIONS", new Properties()))), true);
AutoTableRuleSegment autoTableRuleSegment2 = new AutoTableRuleSegment("t_order_2", Arrays.asList("ds0", "ds1"), keyGenerator, auditStrategySegment);
autoTableRuleSegment2.setShardingColumn("order_id");
autoTableRuleSegment2.setShardingAlgorithmSegment(new AlgorithmSegment("MOD", PropertiesBuilder.build(new Property("sharding_count", "2"))));
autoTableRuleSegment2.setKeyGenerateStrategySegment(new KeyGenerateStrategySegment("order_id", new AlgorithmSegment("snowflake", new Properties())));
autoTableRuleSegment2.setAuditStrategySegment(new AuditStrategySegment(Collections.singleton(new ShardingAuditorSegment("sharding_key_required_auditor",
new AlgorithmSegment("DML_SHARDING_CONDITIONS", new Properties()))), true));
TableRuleSegment tableRuleSegment = new TableRuleSegment("t_order", Arrays.asList("ds0", "ds1"),
new KeyGenerateStrategySegment("order_id", new AlgorithmSegment("snowflake", PropertiesBuilder.build(new Property("", "")))),
new AuditStrategySegment(Collections.singleton(new ShardingAuditorSegment("sharding_key_required_auditor",
new AlgorithmSegment("DML_SHARDING_CONDITIONS", new Properties()))), true));
KeyGenerateStrategySegment keyGenerator1 = new KeyGenerateStrategySegment("order_id", new AlgorithmSegment("snowflake", PropertiesBuilder.build(new Property("", ""))));
TableRuleSegment tableRuleSegment = new TableRuleSegment("t_order", Arrays.asList("ds0", "ds1"), keyGenerator1, auditStrategySegment);
AlgorithmSegment databaseAlgorithmSegment = new AlgorithmSegment("inline", PropertiesBuilder.build(new Property("algorithm-expression", "ds_${product_id % 2}")));
tableRuleSegment.setDatabaseStrategySegment(new ShardingStrategySegment("standard", "order_id", databaseAlgorithmSegment));
tableRuleSegment.setTableStrategySegment(new ShardingStrategySegment("standard", "order_id", new AlgorithmSegment("order_id_algorithm", new Properties())));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,8 @@ void assertUpdateTableType() {
}

private AutoTableRuleSegment createCompleteAutoTableRule(final String logicTableName) {
AutoTableRuleSegment result = new AutoTableRuleSegment(logicTableName, Arrays.asList("ds_0", "ds_1"));
result.setKeyGenerateStrategySegment(new KeyGenerateStrategySegment("product_id", new AlgorithmSegment("DISTSQL.FIXTURE", new Properties())));
AutoTableRuleSegment result =
new AutoTableRuleSegment(logicTableName, Arrays.asList("ds_0", "ds_1"), new KeyGenerateStrategySegment("product_id", new AlgorithmSegment("DISTSQL.FIXTURE", new Properties())), null);
result.setShardingColumn("order_id");
result.setShardingAlgorithmSegment(new AlgorithmSegment("FOO.DISTSQL.FIXTURE", PropertiesBuilder.build(new Property("", ""))));
return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,8 +258,8 @@ void assertUpdateWithIfNotExistsStatement() {
}

private AutoTableRuleSegment createCompleteAutoTableRule() {
AutoTableRuleSegment result = new AutoTableRuleSegment("t_order_item_input", Collections.singleton("logic_ds"));
result.setKeyGenerateStrategySegment(new KeyGenerateStrategySegment("product_id", new AlgorithmSegment("DISTSQL.FIXTURE", new Properties())));
AutoTableRuleSegment result = new AutoTableRuleSegment("t_order_item_input", Collections.singleton("logic_ds"),
new KeyGenerateStrategySegment("product_id", new AlgorithmSegment("DISTSQL.FIXTURE", new Properties())), null);
result.setShardingColumn("order_id");
result.setShardingAlgorithmSegment(new AlgorithmSegment("FOO.DISTSQL.FIXTURE", PropertiesBuilder.build(new Property("", ""))));
return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,10 +256,9 @@ public ASTNode visitShowShardingTableNodes(final ShowShardingTableNodesContext c

@Override
public ASTNode visitShardingAutoTableRule(final ShardingAutoTableRuleContext ctx) {
AutoTableRuleSegment result = new AutoTableRuleSegment(getIdentifierValue(ctx.tableName()), getResources(ctx.storageUnits()));
Optional.ofNullable(ctx.keyGenerateDefinition()).ifPresent(optional -> result.setKeyGenerateStrategySegment((KeyGenerateStrategySegment) visit(ctx.keyGenerateDefinition())));
Optional.ofNullable(ctx.auditDefinition()).ifPresent(optional -> result.setAuditStrategySegment((AuditStrategySegment) visitAuditDefinition(getIdentifierValue(ctx.tableName()),
ctx.auditDefinition())));
KeyGenerateStrategySegment keyGenerateSegment = null == ctx.keyGenerateDefinition() ? null : (KeyGenerateStrategySegment) visit(ctx.keyGenerateDefinition());
AuditStrategySegment auditStrategySegment = null == ctx.auditDefinition() ? null : (AuditStrategySegment) visitAuditDefinition(getIdentifierValue(ctx.tableName()), ctx.auditDefinition());
AutoTableRuleSegment result = new AutoTableRuleSegment(getIdentifierValue(ctx.tableName()), getResources(ctx.storageUnits()), keyGenerateSegment, auditStrategySegment);
Optional.ofNullable(ctx.autoShardingColumnDefinition()).ifPresent(optional -> result.setShardingColumn(buildShardingColumn(ctx.autoShardingColumnDefinition())));
Optional.ofNullable(ctx.algorithmDefinition()).ifPresent(optional -> result.setShardingAlgorithmSegment((AlgorithmSegment) visit(ctx.algorithmDefinition())));
return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import org.apache.shardingsphere.distsql.segment.AlgorithmSegment;

import java.util.Collection;
import org.apache.shardingsphere.sharding.distsql.segment.strategy.AuditStrategySegment;
import org.apache.shardingsphere.sharding.distsql.segment.strategy.KeyGenerateStrategySegment;

/**
* Auto table rule segment.
Expand All @@ -34,7 +36,8 @@ public final class AutoTableRuleSegment extends AbstractTableRuleSegment {

private AlgorithmSegment shardingAlgorithmSegment;

public AutoTableRuleSegment(final String logicTable, final Collection<String> dataSources) {
super(logicTable, dataSources);
public AutoTableRuleSegment(final String logicTable, final Collection<String> dataSources, final KeyGenerateStrategySegment keyGenerateStrategySegment,
final AuditStrategySegment auditStrategySegment) {
super(logicTable, dataSources, keyGenerateStrategySegment, auditStrategySegment);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ public final class TableRuleSegment extends AbstractTableRuleSegment {

private ShardingStrategySegment databaseStrategySegment;

public TableRuleSegment(final String logicTable, final Collection<String> dataSourcesNodes,
public TableRuleSegment(final String logicTable, final Collection<String> dataSourceNodes,
final KeyGenerateStrategySegment keyGenerateStrategySegment, final AuditStrategySegment auditStrategySegment) {
super(logicTable, dataSourcesNodes, keyGenerateStrategySegment, auditStrategySegment);
super(logicTable, dataSourceNodes, keyGenerateStrategySegment, auditStrategySegment);
}
}