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/AbstractPersistService.java index 2c34ae46d2e0e..a9d3966302185 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/AbstractPersistService.java @@ -24,9 +24,8 @@ import java.util.Collection; import java.util.Collections; import java.util.LinkedHashSet; -import java.util.LinkedList; -import java.util.List; import java.util.regex.Pattern; +import java.util.stream.Collectors; @RequiredArgsConstructor public abstract class AbstractPersistService { @@ -39,49 +38,29 @@ public abstract class AbstractPersistService { private final PersistRepository repository; - /** - * Get repository tuples. - * - * @param rootPath root path - * @return repository tuples - */ - public Collection getRepositoryTuple(final String rootPath) { - Collection result = new LinkedList<>(); + protected final Collection getRepositoryTuples(final String rootNode) { Pattern pattern = Pattern.compile(ACTIVE_VERSION_PATTERN, Pattern.CASE_INSENSITIVE); - for (String each : getNodes(rootPath)) { - if (pattern.matcher(each).find()) { - String activeRuleKey = each.replace(ACTIVE_VERSION_PATH, VERSIONS_PATH) + "/" + getActiveVersion(each); - result.add(new RepositoryTuple(activeRuleKey, repository.getDirectly(activeRuleKey))); - } - } - return result; + return loadNodes(rootNode).stream().filter(each -> pattern.matcher(each).find()).map(this::getRepositoryTuple).collect(Collectors.toList()); } - private Collection getNodes(final String rootPath) { + private Collection loadNodes(final String rootNode) { Collection result = new LinkedHashSet<>(); - getAllNodes(result, rootPath); + loadNodes(rootNode, result); if (1 == result.size()) { return Collections.emptyList(); } return result; } - private void getAllNodes(final Collection keys, final String path) { - keys.add(path); - List childKeys = repository.getChildrenKeys(path); - if (childKeys.isEmpty()) { - return; - } - for (String each : childKeys) { - getAllNodes(keys, getPath(path, each)); + private void loadNodes(final String toBeLoadedNode, final Collection loadedNodes) { + loadedNodes.add(toBeLoadedNode); + for (String each : repository.getChildrenKeys(toBeLoadedNode)) { + loadNodes(String.join("/", toBeLoadedNode, each), loadedNodes); } } - private String getPath(final String path, final String childKey) { - return String.join("/", path, childKey); - } - - protected String getActiveVersion(final String key) { - return repository.getDirectly(key); + private RepositoryTuple getRepositoryTuple(final String node) { + String activeRuleKey = node.replace(ACTIVE_VERSION_PATH, VERSIONS_PATH) + "/" + repository.getDirectly(node); + return new RepositoryTuple(activeRuleKey, repository.getDirectly(activeRuleKey)); } } 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 3316d2e60ca40..99a8fce280fe8 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 @@ -61,7 +61,7 @@ public void persist(final String databaseName, final Collection load(final String databaseName) { - Collection repositoryTuples = getRepositoryTuple(DatabaseRuleMetaDataNode.getRulesNode(databaseName)); + Collection repositoryTuples = getRepositoryTuples(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 514670167d1ec..5f40a0d303247 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 @@ -80,23 +80,23 @@ private Collection persistDataNodes(final Collection load() { - Collection repositoryTuples = getRepositoryTuple(GlobalNode.getGlobalRuleRootNode()); + Collection repositoryTuples = getRepositoryTuples(GlobalNode.getGlobalRuleRootNode()); return repositoryTuples.isEmpty() ? Collections.emptyList() : new YamlDataNodeGlobalRuleConfigurationSwapperEngine().swapToRuleConfigurations(repositoryTuples); } @Override public RuleConfiguration load(final String ruleName) { - Collection repositoryTuples = getRepositoryTuple(GlobalNode.getGlobalRuleNode(ruleName)); + Collection repositoryTuples = getRepositoryTuples(GlobalNode.getGlobalRuleNode(ruleName)); return new YamlDataNodeGlobalRuleConfigurationSwapperEngine().swapSingleRuleToRuleConfiguration(ruleName, repositoryTuples).orElse(null); } }