You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by ji...@apache.org on 2023/01/01 09:13:59 UTC
[shardingsphere] branch master updated: Refactor ifNotExists of CREATE SHADOW RULE. (#23243)
This is an automated email from the ASF dual-hosted git repository.
jianglongtao pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new 31bc3e2b63b Refactor ifNotExists of CREATE SHADOW RULE. (#23243)
31bc3e2b63b is described below
commit 31bc3e2b63b59a49efec93cb5b11a20190b8f355
Author: yx9o <ya...@163.com>
AuthorDate: Sun Jan 1 17:13:51 2023 +0800
Refactor ifNotExists of CREATE SHADOW RULE. (#23243)
---
...eateDefaultShadowAlgorithmStatementUpdater.java | 11 +++---
.../update/CreateShadowRuleStatementUpdater.java | 41 ++++++++++------------
2 files changed, 24 insertions(+), 28 deletions(-)
diff --git a/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/update/CreateDefaultShadowAlgorithmStatementUpdater.java b/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/update/CreateDefaultShadowAlgorithmStatementUpdater.java
index e39d40f3b60..8e1f9dfa891 100644
--- a/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/update/CreateDefaultShadowAlgorithmStatementUpdater.java
+++ b/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/update/CreateDefaultShadowAlgorithmStatementUpdater.java
@@ -17,6 +17,7 @@
package org.apache.shardingsphere.shadow.distsql.handler.update;
+import com.google.common.base.Strings;
import org.apache.shardingsphere.distsql.handler.exception.algorithm.DuplicateAlgorithmException;
import org.apache.shardingsphere.distsql.handler.exception.algorithm.InvalidAlgorithmConfigurationException;
import org.apache.shardingsphere.distsql.handler.update.RuleDefinitionCreateUpdater;
@@ -52,7 +53,7 @@ public final class CreateDefaultShadowAlgorithmStatementUpdater implements RuleD
@Override
public RuleConfiguration buildToBeCreatedRuleConfiguration(final ShadowRuleConfiguration currentRuleConfig, final CreateDefaultShadowAlgorithmStatement sqlStatement) {
- ShadowRuleConfiguration result = null;
+ ShadowRuleConfiguration result = new ShadowRuleConfiguration();
if (getDuplicatedRuleNames(currentRuleConfig).isEmpty()) {
result = new ShadowRuleConfiguration();
result.setShadowAlgorithms(buildAlgorithmMap(sqlStatement));
@@ -63,8 +64,8 @@ public final class CreateDefaultShadowAlgorithmStatementUpdater implements RuleD
@Override
public void updateCurrentRuleConfiguration(final ShadowRuleConfiguration currentRuleConfig, final ShadowRuleConfiguration toBeCreatedRuleConfig) {
- if (null != currentRuleConfig && null != toBeCreatedRuleConfig) {
- currentRuleConfig.getShadowAlgorithms().putAll(toBeCreatedRuleConfig.getShadowAlgorithms());
+ currentRuleConfig.getShadowAlgorithms().putAll(toBeCreatedRuleConfig.getShadowAlgorithms());
+ if (!Strings.isNullOrEmpty(toBeCreatedRuleConfig.getDefaultShadowAlgorithmName())) {
currentRuleConfig.setDefaultShadowAlgorithmName(toBeCreatedRuleConfig.getDefaultShadowAlgorithmName());
}
}
@@ -74,7 +75,7 @@ public final class CreateDefaultShadowAlgorithmStatementUpdater implements RuleD
sqlStatement.getShadowAlgorithmSegment().getAlgorithmSegment().getProps()));
}
- private static Collection<String> getDuplicatedRuleNames(final ShadowRuleConfiguration currentRuleConfig) {
+ private Collection<String> getDuplicatedRuleNames(final ShadowRuleConfiguration currentRuleConfig) {
Collection<String> currentAlgorithmNames = null == currentRuleConfig ? Collections.emptyList() : currentRuleConfig.getShadowAlgorithms().keySet();
return Stream.of("default_shadow_algorithm").filter(currentAlgorithmNames::contains).collect(Collectors.toSet());
}
@@ -90,7 +91,7 @@ public final class CreateDefaultShadowAlgorithmStatementUpdater implements RuleD
TypedSPIRegistry.findRegisteredService(ShadowAlgorithm.class, shadowAlgorithmType).isPresent(), () -> new InvalidAlgorithmConfigurationException("shadow", shadowAlgorithmType));
}
- private static void checkAlgorithmCompleteness(final Collection<AlgorithmSegment> algorithmSegments) {
+ private void checkAlgorithmCompleteness(final Collection<AlgorithmSegment> algorithmSegments) {
Collection<AlgorithmSegment> incompleteAlgorithms = algorithmSegments.stream().filter(each -> each.getName().isEmpty() || each.getProps().isEmpty()).collect(Collectors.toSet());
ShardingSpherePreconditions.checkState(incompleteAlgorithms.isEmpty(), () -> new InvalidAlgorithmConfigurationException("shadow"));
}
diff --git a/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/update/CreateShadowRuleStatementUpdater.java b/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/update/CreateShadowRuleStatementUpdater.java
index 514e43b6940..84dab6f8ac4 100644
--- a/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/update/CreateShadowRuleStatementUpdater.java
+++ b/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/update/CreateShadowRuleStatementUpdater.java
@@ -34,7 +34,6 @@ import org.apache.shardingsphere.shadow.distsql.parser.statement.CreateShadowRul
import org.apache.shardingsphere.shadow.spi.ShadowAlgorithm;
import java.util.Collection;
-import java.util.LinkedList;
import java.util.Map;
import java.util.stream.Collectors;
@@ -43,48 +42,44 @@ import java.util.stream.Collectors;
*/
public final class CreateShadowRuleStatementUpdater implements RuleDefinitionCreateUpdater<CreateShadowRuleStatement, ShadowRuleConfiguration> {
- private Collection<String> duplicatedRuleNames = new LinkedList<>();
+ @Override
+ public void checkSQLStatement(final ShardingSphereDatabase database, final CreateShadowRuleStatement sqlStatement, final ShadowRuleConfiguration currentRuleConfig) {
+ String databaseName = database.getName();
+ checkDuplicatedRules(databaseName, sqlStatement, currentRuleConfig);
+ checkStorageUnits(database, sqlStatement.getRules());
+ checkAlgorithms(databaseName, sqlStatement.getRules());
+ checkAlgorithmType(sqlStatement.getRules());
+ }
@Override
public RuleConfiguration buildToBeCreatedRuleConfiguration(final ShadowRuleConfiguration currentRuleConfig, final CreateShadowRuleStatement sqlStatement) {
Collection<ShadowRuleSegment> segments = sqlStatement.getRules();
- if (!duplicatedRuleNames.isEmpty()) {
- segments.removeIf(each -> duplicatedRuleNames.contains(each.getRuleName()));
+ if (sqlStatement.isIfNotExists()) {
+ Collection<String> toBeCreatedRuleNames = ShadowRuleStatementSupporter.getRuleNames(sqlStatement.getRules());
+ toBeCreatedRuleNames.retainAll(ShadowRuleStatementSupporter.getRuleNames(currentRuleConfig));
+ segments.removeIf(each -> toBeCreatedRuleNames.contains(each.getRuleName()));
}
return ShadowRuleStatementConverter.convert(segments);
}
@Override
public void updateCurrentRuleConfiguration(final ShadowRuleConfiguration currentRuleConfig, final ShadowRuleConfiguration toBeCreatedRuleConfig) {
- if (null != currentRuleConfig) {
- currentRuleConfig.getDataSources().addAll(toBeCreatedRuleConfig.getDataSources());
- currentRuleConfig.getShadowAlgorithms().putAll(toBeCreatedRuleConfig.getShadowAlgorithms());
- updateTables(currentRuleConfig.getTables(), toBeCreatedRuleConfig.getTables());
- }
+ currentRuleConfig.getDataSources().addAll(toBeCreatedRuleConfig.getDataSources());
+ currentRuleConfig.getShadowAlgorithms().putAll(toBeCreatedRuleConfig.getShadowAlgorithms());
+ updateTables(currentRuleConfig.getTables(), toBeCreatedRuleConfig.getTables());
}
private void updateTables(final Map<String, ShadowTableConfiguration> currentTables, final Map<String, ShadowTableConfiguration> toBeCreateTables) {
toBeCreateTables.forEach((key, value) -> currentTables.merge(key, value, ShadowRuleStatementSupporter::mergeConfiguration));
}
- @Override
- public void checkSQLStatement(final ShardingSphereDatabase database, final CreateShadowRuleStatement sqlStatement, final ShadowRuleConfiguration currentRuleConfig) {
- String databaseName = database.getName();
- checkDuplicatedRules(databaseName, sqlStatement, currentRuleConfig);
- checkStorageUnits(database, sqlStatement.getRules());
- checkAlgorithms(databaseName, sqlStatement.getRules());
- checkAlgorithmType(sqlStatement.getRules());
- }
-
private void checkDuplicatedRules(final String databaseName, final CreateShadowRuleStatement sqlStatement, final ShadowRuleConfiguration currentRuleConfig) {
Collection<String> toBeCreatedRuleNames = ShadowRuleStatementSupporter.getRuleNames(sqlStatement.getRules());
ShadowRuleStatementChecker.checkDuplicated(toBeCreatedRuleNames, duplicated -> new DuplicateRuleException("shadow", databaseName, duplicated));
- toBeCreatedRuleNames.retainAll(ShadowRuleStatementSupporter.getRuleNames(currentRuleConfig));
- if (sqlStatement.isIfNotExists()) {
- duplicatedRuleNames = toBeCreatedRuleNames;
- return;
+ if (!sqlStatement.isIfNotExists()) {
+ toBeCreatedRuleNames.retainAll(ShadowRuleStatementSupporter.getRuleNames(currentRuleConfig));
+ ShardingSpherePreconditions.checkState(toBeCreatedRuleNames.isEmpty(), () -> new DuplicateRuleException("shadow", databaseName, toBeCreatedRuleNames));
}
- ShardingSpherePreconditions.checkState(toBeCreatedRuleNames.isEmpty(), () -> new DuplicateRuleException("shadow", databaseName, toBeCreatedRuleNames));
}
private void checkStorageUnits(final ShardingSphereDatabase database, final Collection<ShadowRuleSegment> segments) {