Skip to content

Commit

Permalink
Refactor DatabaseRuleDefinitionExecutor (#29891)
Browse files Browse the repository at this point in the history
* Revise javadoc

* Refactor DatabaseRuleDefinitionExecutor

* Refactor DatabaseRuleDefinitionExecutor

* Refactor DatabaseRuleDefinitionExecutor
  • Loading branch information
terrymanu authored Jan 28, 2024
1 parent 678dac7 commit f32b968
Show file tree
Hide file tree
Showing 70 changed files with 807 additions and 681 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package org.apache.shardingsphere.broadcast.distsql.handler.update;

import lombok.Setter;
import org.apache.shardingsphere.broadcast.api.config.BroadcastRuleConfiguration;
import org.apache.shardingsphere.broadcast.distsql.statement.CreateBroadcastTableRuleStatement;
import org.apache.shardingsphere.distsql.handler.exception.rule.DuplicateRuleException;
Expand All @@ -31,10 +32,13 @@
/**
* Create broadcast table rule executor.
*/
@Setter
public final class CreateBroadcastTableRuleExecutor implements DatabaseRuleCreateExecutor<CreateBroadcastTableRuleStatement, BroadcastRuleConfiguration> {

private ShardingSphereDatabase database;

@Override
public void checkBeforeUpdate(final ShardingSphereDatabase database, final CreateBroadcastTableRuleStatement sqlStatement, final BroadcastRuleConfiguration currentRuleConfig) {
public void checkBeforeUpdate(final CreateBroadcastTableRuleStatement sqlStatement, final BroadcastRuleConfiguration currentRuleConfig) {
ShardingSpherePreconditions.checkState(!database.getResourceMetaData().getStorageUnits().isEmpty(), () -> new EmptyStorageUnitException(database.getName()));
if (!sqlStatement.isIfNotExists()) {
checkDuplicate(sqlStatement, currentRuleConfig);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package org.apache.shardingsphere.broadcast.distsql.handler.update;

import lombok.Setter;
import org.apache.shardingsphere.broadcast.api.config.BroadcastRuleConfiguration;
import org.apache.shardingsphere.broadcast.distsql.statement.DropBroadcastTableRuleStatement;
import org.apache.shardingsphere.distsql.handler.exception.rule.MissingRequiredRuleException;
Expand All @@ -31,29 +32,31 @@
/**
* Drop broadcast table rule executor.
*/
@Setter
public final class DropBroadcastTableRuleExecutor implements DatabaseRuleDropExecutor<DropBroadcastTableRuleStatement, BroadcastRuleConfiguration> {

private ShardingSphereDatabase database;

@Override
public void checkBeforeUpdate(final ShardingSphereDatabase database, final DropBroadcastTableRuleStatement sqlStatement, final BroadcastRuleConfiguration currentRuleConfig) {
public void checkBeforeUpdate(final DropBroadcastTableRuleStatement sqlStatement, final BroadcastRuleConfiguration currentRuleConfig) {
if (!isExistRuleConfig(currentRuleConfig) && sqlStatement.isIfExists()) {
return;
}
String databaseName = database.getName();
checkCurrentRuleConfiguration(databaseName, currentRuleConfig);
checkBroadcastTableRuleExist(databaseName, sqlStatement, currentRuleConfig);
checkCurrentRuleConfiguration(currentRuleConfig);
checkBroadcastTableRuleExist(sqlStatement, currentRuleConfig);
}

private void checkCurrentRuleConfiguration(final String databaseName, final BroadcastRuleConfiguration currentRuleConfig) {
ShardingSpherePreconditions.checkNotNull(currentRuleConfig, () -> new MissingRequiredRuleException("Broadcast", databaseName));
private void checkCurrentRuleConfiguration(final BroadcastRuleConfiguration currentRuleConfig) {
ShardingSpherePreconditions.checkNotNull(currentRuleConfig, () -> new MissingRequiredRuleException("Broadcast", database.getName()));
}

private void checkBroadcastTableRuleExist(final String databaseName, final DropBroadcastTableRuleStatement sqlStatement, final BroadcastRuleConfiguration currentRuleConfig) {
private void checkBroadcastTableRuleExist(final DropBroadcastTableRuleStatement sqlStatement, final BroadcastRuleConfiguration currentRuleConfig) {
if (sqlStatement.isIfExists()) {
return;
}
Collection<String> currentRules = currentRuleConfig.getTables();
Collection<String> notExistRules = sqlStatement.getTables().stream().filter(each -> !containsIgnoreCase(currentRules, each)).collect(Collectors.toList());
ShardingSpherePreconditions.checkState(notExistRules.isEmpty(), () -> new MissingRequiredRuleException("Broadcast", databaseName, notExistRules));
ShardingSpherePreconditions.checkState(notExistRules.isEmpty(), () -> new MissingRequiredRuleException("Broadcast", database.getName(), notExistRules));
}

private boolean containsIgnoreCase(final Collection<String> currentRules, final String ruleName) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,26 +43,29 @@ class CreateBroadcastTableRuleStatementUpdaterTest {
void assertCheckSQLStatementWithEmptyStorageUnit() {
BroadcastRuleConfiguration currentConfig = mock(BroadcastRuleConfiguration.class);
when(currentConfig.getTables()).thenReturn(Collections.singleton("t_address"));
CreateBroadcastTableRuleStatement statement = new CreateBroadcastTableRuleStatement(false, Collections.singleton("t_address"));
CreateBroadcastTableRuleStatement sqlStatement = new CreateBroadcastTableRuleStatement(false, Collections.singleton("t_address"));
ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS);
when(database.getResourceMetaData().getStorageUnits()).thenReturn(Collections.emptyMap());
assertThrows(EmptyStorageUnitException.class, () -> executor.checkBeforeUpdate(database, statement, currentConfig));
executor.setDatabase(database);
assertThrows(EmptyStorageUnitException.class, () -> executor.checkBeforeUpdate(sqlStatement, currentConfig));
}

@Test
void assertCheckSQLStatementWithDuplicateBroadcastRule() {
BroadcastRuleConfiguration currentConfig = mock(BroadcastRuleConfiguration.class);
when(currentConfig.getTables()).thenReturn(Collections.singleton("t_address"));
CreateBroadcastTableRuleStatement statement = new CreateBroadcastTableRuleStatement(false, Collections.singleton("t_address"));
assertThrows(DuplicateRuleException.class, () -> executor.checkBeforeUpdate(mockShardingSphereDatabase(), statement, currentConfig));
CreateBroadcastTableRuleStatement sqlStatement = new CreateBroadcastTableRuleStatement(false, Collections.singleton("t_address"));
executor.setDatabase(mockShardingSphereDatabase());
assertThrows(DuplicateRuleException.class, () -> executor.checkBeforeUpdate(sqlStatement, currentConfig));
}

@Test
void assertBuildToBeCreatedRuleConfiguration() {
BroadcastRuleConfiguration currentConfig = new BroadcastRuleConfiguration(new LinkedList<>());
CreateBroadcastTableRuleStatement statement = new CreateBroadcastTableRuleStatement(false, Collections.singleton("t_address"));
executor.checkBeforeUpdate(mockShardingSphereDatabase(), statement, currentConfig);
BroadcastRuleConfiguration toBeCreatedRuleConfig = executor.buildToBeCreatedRuleConfiguration(currentConfig, statement);
CreateBroadcastTableRuleStatement sqlStatement = new CreateBroadcastTableRuleStatement(false, Collections.singleton("t_address"));
executor.setDatabase(mockShardingSphereDatabase());
executor.checkBeforeUpdate(sqlStatement, currentConfig);
BroadcastRuleConfiguration toBeCreatedRuleConfig = executor.buildToBeCreatedRuleConfiguration(currentConfig, sqlStatement);
executor.updateCurrentRuleConfiguration(currentConfig, toBeCreatedRuleConfig);
assertThat(currentConfig.getTables().size(), is(1));
assertThat(currentConfig.getTables().iterator().next(), is("t_address"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,34 +35,33 @@

class DropBroadcastTableRuleStatementUpdaterTest {

private ShardingSphereDatabase database;

private final DropBroadcastTableRuleExecutor executor = new DropBroadcastTableRuleExecutor();

@BeforeEach
void setUp() {
database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS);
ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS);
when(database.getName()).thenReturn("sharding_db");
executor.setDatabase(database);
}

@Test
void assertCheckSQLStatementWithoutCurrentRule() {
DropBroadcastTableRuleStatement statement = new DropBroadcastTableRuleStatement(false, Collections.singleton("t_address"));
assertThrows(MissingRequiredRuleException.class, () -> executor.checkBeforeUpdate(database, statement, null));
DropBroadcastTableRuleStatement sqlStatement = new DropBroadcastTableRuleStatement(false, Collections.singleton("t_address"));
assertThrows(MissingRequiredRuleException.class, () -> executor.checkBeforeUpdate(sqlStatement, null));
}

@Test
void assertCheckSQLStatementWithoutToBeDroppedRule() {
DropBroadcastTableRuleStatement statement = new DropBroadcastTableRuleStatement(false, Collections.singleton("t_address"));
assertThrows(MissingRequiredRuleException.class, () -> executor.checkBeforeUpdate(database, statement, new BroadcastRuleConfiguration(Collections.emptyList())));
DropBroadcastTableRuleStatement sqlStatement = new DropBroadcastTableRuleStatement(false, Collections.singleton("t_address"));
assertThrows(MissingRequiredRuleException.class, () -> executor.checkBeforeUpdate(sqlStatement, new BroadcastRuleConfiguration(Collections.emptyList())));
}

@Test
void assertUpdateCurrentRuleConfiguration() {
BroadcastRuleConfiguration config = new BroadcastRuleConfiguration(new LinkedList<>());
config.getTables().add("t_address");
DropBroadcastTableRuleStatement statement = new DropBroadcastTableRuleStatement(false, Collections.singleton("t_address"));
assertTrue(executor.updateCurrentRuleConfiguration(statement, config));
DropBroadcastTableRuleStatement sqlStatement = new DropBroadcastTableRuleStatement(false, Collections.singleton("t_address"));
assertTrue(executor.updateCurrentRuleConfiguration(sqlStatement, config));
assertTrue(config.getTables().isEmpty());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.apache.shardingsphere.encrypt.distsql.handler.update;

import com.google.common.base.Preconditions;
import lombok.Setter;
import org.apache.shardingsphere.distsql.handler.exception.rule.InvalidRuleConfigurationException;
import org.apache.shardingsphere.distsql.handler.exception.rule.MissingRequiredRuleException;
import org.apache.shardingsphere.distsql.handler.type.rdl.rule.spi.database.DatabaseRuleAlterExecutor;
Expand All @@ -42,26 +43,28 @@
/**
* Alter encrypt rule executor.
*/
@Setter
public final class AlterEncryptRuleExecutor implements DatabaseRuleAlterExecutor<AlterEncryptRuleStatement, EncryptRuleConfiguration> {

private ShardingSphereDatabase database;

@Override
public void checkBeforeUpdate(final ShardingSphereDatabase database, final AlterEncryptRuleStatement sqlStatement, final EncryptRuleConfiguration currentRuleConfig) {
String databaseName = database.getName();
checkCurrentRuleConfiguration(databaseName, currentRuleConfig);
checkToBeAlteredRules(databaseName, sqlStatement, currentRuleConfig);
public void checkBeforeUpdate(final AlterEncryptRuleStatement sqlStatement, final EncryptRuleConfiguration currentRuleConfig) {
checkCurrentRuleConfiguration(currentRuleConfig);
checkToBeAlteredRules(sqlStatement, currentRuleConfig);
checkColumnNames(sqlStatement);
checkToBeAlteredEncryptors(sqlStatement);
}

private void checkCurrentRuleConfiguration(final String databaseName, final EncryptRuleConfiguration currentRuleConfig) {
ShardingSpherePreconditions.checkNotNull(currentRuleConfig, () -> new MissingRequiredRuleException("Encrypt", databaseName));
private void checkCurrentRuleConfiguration(final EncryptRuleConfiguration currentRuleConfig) {
ShardingSpherePreconditions.checkNotNull(currentRuleConfig, () -> new MissingRequiredRuleException("Encrypt", database.getName()));
}

private void checkToBeAlteredRules(final String databaseName, final AlterEncryptRuleStatement sqlStatement, final EncryptRuleConfiguration currentRuleConfig) {
private void checkToBeAlteredRules(final AlterEncryptRuleStatement sqlStatement, final EncryptRuleConfiguration currentRuleConfig) {
Collection<String> currentEncryptTableNames = currentRuleConfig.getTables().stream().map(EncryptTableRuleConfiguration::getName).collect(Collectors.toList());
Collection<String> notExistEncryptTableNames = getToBeAlteredEncryptTableNames(sqlStatement).stream().filter(each -> !currentEncryptTableNames.contains(each)).collect(Collectors.toList());
if (!notExistEncryptTableNames.isEmpty()) {
throw new MissingRequiredRuleException("Encrypt", databaseName, notExistEncryptTableNames);
throw new MissingRequiredRuleException("Encrypt", database.getName(), notExistEncryptTableNames);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package org.apache.shardingsphere.encrypt.distsql.handler.update;

import lombok.Setter;
import org.apache.shardingsphere.distsql.handler.exception.algorithm.InvalidAlgorithmConfigurationException;
import org.apache.shardingsphere.distsql.handler.exception.rule.DuplicateRuleException;
import org.apache.shardingsphere.distsql.handler.exception.rule.InvalidRuleConfigurationException;
Expand All @@ -42,17 +43,32 @@
/**
* Create encrypt rule executor.
*/
@Setter
public final class CreateEncryptRuleExecutor implements DatabaseRuleCreateExecutor<CreateEncryptRuleStatement, EncryptRuleConfiguration> {

private ShardingSphereDatabase database;

@Override
public void checkBeforeUpdate(final ShardingSphereDatabase database, final CreateEncryptRuleStatement sqlStatement, final EncryptRuleConfiguration currentRuleConfig) {
public void checkBeforeUpdate(final CreateEncryptRuleStatement sqlStatement, final EncryptRuleConfiguration currentRuleConfig) {
if (!sqlStatement.isIfNotExists()) {
checkDuplicateRuleNames(database.getName(), sqlStatement, currentRuleConfig);
checkDuplicateRuleNames(sqlStatement, currentRuleConfig);
}
checkColumnNames(sqlStatement);
checkAlgorithmTypes(sqlStatement);
checkToBeCreatedEncryptors(sqlStatement);
checkDataSources(database);
checkDataSources();
}

private void checkColumnNames(final CreateEncryptRuleStatement sqlStatement) {
for (EncryptRuleSegment each : sqlStatement.getRules()) {
ShardingSpherePreconditions.checkState(isColumnNameNotConflicts(each),
() -> new InvalidRuleConfigurationException("encrypt", "assisted query column or like query column conflicts with logic column"));
}
}

private boolean isColumnNameNotConflicts(final EncryptRuleSegment rule) {
return rule.getColumns().stream().noneMatch(each -> null != each.getLikeQuery() && each.getName().equals(each.getLikeQuery().getName())
|| null != each.getAssistedQuery() && each.getName().equals(each.getAssistedQuery().getName()));
}

private void checkAlgorithmTypes(final CreateEncryptRuleStatement sqlStatement) {
Expand Down Expand Up @@ -88,9 +104,9 @@ private void checkAssistedAlgorithmType(final EncryptColumnItemSegment itemSegme
() -> new InvalidAlgorithmConfigurationException("assisted encrypt", encryptAlgorithm.getType()));
}

private void checkDuplicateRuleNames(final String databaseName, final CreateEncryptRuleStatement sqlStatement, final EncryptRuleConfiguration currentRuleConfig) {
private void checkDuplicateRuleNames(final CreateEncryptRuleStatement sqlStatement, final EncryptRuleConfiguration currentRuleConfig) {
Collection<String> duplicatedRuleNames = getDuplicatedRuleNames(sqlStatement, currentRuleConfig);
ShardingSpherePreconditions.checkState(duplicatedRuleNames.isEmpty(), () -> new DuplicateRuleException("encrypt", databaseName, duplicatedRuleNames));
ShardingSpherePreconditions.checkState(duplicatedRuleNames.isEmpty(), () -> new DuplicateRuleException("encrypt", database.getName(), duplicatedRuleNames));
}

private Collection<String> getDuplicatedRuleNames(final CreateEncryptRuleStatement sqlStatement, final EncryptRuleConfiguration currentRuleConfig) {
Expand All @@ -101,18 +117,6 @@ private Collection<String> getDuplicatedRuleNames(final CreateEncryptRuleStateme
return sqlStatement.getRules().stream().map(EncryptRuleSegment::getTableName).filter(currentRuleNames::contains).collect(Collectors.toSet());
}

private void checkColumnNames(final CreateEncryptRuleStatement sqlStatement) {
for (EncryptRuleSegment each : sqlStatement.getRules()) {
ShardingSpherePreconditions.checkState(isColumnNameNotConflicts(each),
() -> new InvalidRuleConfigurationException("encrypt", "assisted query column or like query column conflicts with logic column"));
}
}

private boolean isColumnNameNotConflicts(final EncryptRuleSegment rule) {
return rule.getColumns().stream().noneMatch(each -> null != each.getLikeQuery() && each.getName().equals(each.getLikeQuery().getName())
|| null != each.getAssistedQuery() && each.getName().equals(each.getAssistedQuery().getName()));
}

private void checkToBeCreatedEncryptors(final CreateEncryptRuleStatement sqlStatement) {
Collection<AlgorithmSegment> encryptors = new LinkedHashSet<>();
sqlStatement.getRules().forEach(each -> each.getColumns().forEach(column -> {
Expand All @@ -127,7 +131,7 @@ private void checkToBeCreatedEncryptors(final CreateEncryptRuleStatement sqlStat
encryptors.stream().filter(Objects::nonNull).forEach(each -> TypedSPILoader.checkService(EncryptAlgorithm.class, each.getName(), each.getProps()));
}

private void checkDataSources(final ShardingSphereDatabase database) {
private void checkDataSources() {
ShardingSpherePreconditions.checkState(!database.getResourceMetaData().getStorageUnits().isEmpty(), () -> new EmptyStorageUnitException(database.getName()));
}

Expand Down
Loading

0 comments on commit f32b968

Please sign in to comment.