From 12144de570ab59622720cf9c20c2085391b011c2 Mon Sep 17 00:00:00 2001 From: Liang Zhang Date: Sat, 27 Apr 2024 20:58:38 +0800 Subject: [PATCH] Refactor AbstractPersistService to RepositoryTuplePersistService (#31036) --- ...java => RepositoryTuplePersistService.java} | 13 +++++++++++-- .../rule/DatabaseRulePersistService.java | 10 ++++++---- .../global/GlobalRulePersistService.java | 18 ++++++++++-------- 3 files changed, 27 insertions(+), 14 deletions(-) rename kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/config/{AbstractPersistService.java => RepositoryTuplePersistService.java} (89%) diff --git a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/config/AbstractPersistService.java b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/config/RepositoryTuplePersistService.java similarity index 89% rename from kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/config/AbstractPersistService.java rename to kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/config/RepositoryTuplePersistService.java index a9d3966302185..3d00832e45ddf 100644 --- a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/config/AbstractPersistService.java +++ b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/config/RepositoryTuplePersistService.java @@ -27,8 +27,11 @@ import java.util.regex.Pattern; import java.util.stream.Collectors; +/** + * Repository tuple persist service. + */ @RequiredArgsConstructor -public abstract class AbstractPersistService { +public final class RepositoryTuplePersistService { private static final String ACTIVE_VERSION_PATTERN = "/active_version$"; @@ -38,7 +41,13 @@ public abstract class AbstractPersistService { private final PersistRepository repository; - protected final Collection getRepositoryTuples(final String rootNode) { + /** + * Load repository tuples. + * + * @param rootNode root node + * @return loaded repository tuples + */ + public Collection loadRepositoryTuples(final String rootNode) { Pattern pattern = Pattern.compile(ACTIVE_VERSION_PATTERN, Pattern.CASE_INSENSITIVE); return loadNodes(rootNode).stream().filter(each -> pattern.matcher(each).find()).map(this::getRepositoryTuple).collect(Collectors.toList()); } diff --git a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/config/database/rule/DatabaseRulePersistService.java b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/config/database/rule/DatabaseRulePersistService.java index 99a8fce280fe8..6c978497661a5 100644 --- a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/config/database/rule/DatabaseRulePersistService.java +++ b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/config/database/rule/DatabaseRulePersistService.java @@ -24,7 +24,7 @@ import org.apache.shardingsphere.infra.yaml.config.swapper.rule.YamlDataNodeRuleConfigurationSwapper; import org.apache.shardingsphere.infra.yaml.config.swapper.rule.YamlDataNodeRuleConfigurationSwapperEngine; import org.apache.shardingsphere.metadata.persist.node.metadata.DatabaseRuleMetaDataNode; -import org.apache.shardingsphere.metadata.persist.service.config.AbstractPersistService; +import org.apache.shardingsphere.metadata.persist.service.config.RepositoryTuplePersistService; import org.apache.shardingsphere.metadata.persist.service.config.database.DatabaseBasedPersistService; import org.apache.shardingsphere.mode.spi.PersistRepository; @@ -37,15 +37,17 @@ /** * Database rule persist service. */ -public final class DatabaseRulePersistService extends AbstractPersistService implements DatabaseBasedPersistService> { +public final class DatabaseRulePersistService implements DatabaseBasedPersistService> { private static final String DEFAULT_VERSION = "0"; private final PersistRepository repository; + private final RepositoryTuplePersistService repositoryTuplePersistService; + public DatabaseRulePersistService(final PersistRepository repository) { - super(repository); this.repository = repository; + repositoryTuplePersistService = new RepositoryTuplePersistService(repository); } @SuppressWarnings({"unchecked", "rawtypes"}) @@ -61,7 +63,7 @@ public void persist(final String databaseName, final Collection load(final String databaseName) { - Collection repositoryTuples = getRepositoryTuples(DatabaseRuleMetaDataNode.getRulesNode(databaseName)); + Collection repositoryTuples = repositoryTuplePersistService.loadRepositoryTuples(DatabaseRuleMetaDataNode.getRulesNode(databaseName)); return repositoryTuples.isEmpty() ? Collections.emptyList() : new YamlDataNodeRuleConfigurationSwapperEngine().swapToRuleConfigurations(repositoryTuples); } diff --git a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/config/global/GlobalRulePersistService.java b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/config/global/GlobalRulePersistService.java index 5f40a0d303247..fcd6869985960 100644 --- a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/config/global/GlobalRulePersistService.java +++ b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/config/global/GlobalRulePersistService.java @@ -24,7 +24,7 @@ import org.apache.shardingsphere.infra.yaml.config.swapper.rule.YamlDataNodeGlobalRuleConfigurationSwapper; import org.apache.shardingsphere.infra.yaml.config.swapper.rule.YamlDataNodeGlobalRuleConfigurationSwapperEngine; import org.apache.shardingsphere.metadata.persist.node.GlobalNode; -import org.apache.shardingsphere.metadata.persist.service.config.AbstractPersistService; +import org.apache.shardingsphere.metadata.persist.service.config.RepositoryTuplePersistService; import org.apache.shardingsphere.mode.spi.PersistRepository; import java.util.Collection; @@ -36,15 +36,17 @@ /** * Global rule persist service. */ -public final class GlobalRulePersistService extends AbstractPersistService implements GlobalPersistService> { +public final class GlobalRulePersistService implements GlobalPersistService> { private static final String DEFAULT_VERSION = "0"; private final PersistRepository repository; + private final RepositoryTuplePersistService repositoryTuplePersistService; + public GlobalRulePersistService(final PersistRepository repository) { - super(repository); this.repository = repository; + repositoryTuplePersistService = new RepositoryTuplePersistService(repository); } @SuppressWarnings({"unchecked", "rawtypes"}) @@ -54,7 +56,7 @@ public void persist(final Collection globalRuleConfigs) { .swapToYamlRuleConfigurations(globalRuleConfigs).entrySet()) { Collection repositoryTuples = entry.getValue().swapToRepositoryTuples(entry.getKey()); if (!repositoryTuples.isEmpty()) { - persistDataNodes(repositoryTuples); + persistTuples(repositoryTuples); } } } @@ -67,13 +69,13 @@ public Collection persistConfigurations(final Collection repositoryTuples = entry.getValue().swapToRepositoryTuples(entry.getKey()); if (!repositoryTuples.isEmpty()) { - result.addAll(persistDataNodes(repositoryTuples)); + result.addAll(persistTuples(repositoryTuples)); } } return result; } - private Collection persistDataNodes(final Collection repositoryTuples) { + private Collection persistTuples(final Collection repositoryTuples) { Collection result = new LinkedList<>(); for (RepositoryTuple each : repositoryTuples) { List versions = repository.getChildrenKeys(GlobalNode.getGlobalRuleVersionsNode(each.getKey())); @@ -90,13 +92,13 @@ private Collection persistDataNodes(final Collection load() { - Collection repositoryTuples = getRepositoryTuples(GlobalNode.getGlobalRuleRootNode()); + Collection repositoryTuples = repositoryTuplePersistService.loadRepositoryTuples(GlobalNode.getGlobalRuleRootNode()); return repositoryTuples.isEmpty() ? Collections.emptyList() : new YamlDataNodeGlobalRuleConfigurationSwapperEngine().swapToRuleConfigurations(repositoryTuples); } @Override public RuleConfiguration load(final String ruleName) { - Collection repositoryTuples = getRepositoryTuples(GlobalNode.getGlobalRuleNode(ruleName)); + Collection repositoryTuples = repositoryTuplePersistService.loadRepositoryTuples(GlobalNode.getGlobalRuleNode(ruleName)); return new YamlDataNodeGlobalRuleConfigurationSwapperEngine().swapSingleRuleToRuleConfiguration(ruleName, repositoryTuples).orElse(null); } }