Skip to content

Commit

Permalink
Refactor AbstractPersistService (#31035)
Browse files Browse the repository at this point in the history
* Refactor AbstractPersistService.getRepositoryTuple()

* Refactor AbstractPersistService.getRepositoryTuples()

* Refactor AbstractPersistService

* Refactor AbstractPersistService

* Refactor AbstractPersistService

* Refactor AbstractPersistService

* Refactor AbstractPersistService

* Refactor AbstractPersistService
  • Loading branch information
terrymanu authored Apr 27, 2024
1 parent 37c9d9e commit 7d84d30
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -39,49 +38,29 @@ public abstract class AbstractPersistService {

private final PersistRepository repository;

/**
* Get repository tuples.
*
* @param rootPath root path
* @return repository tuples
*/
public Collection<RepositoryTuple> getRepositoryTuple(final String rootPath) {
Collection<RepositoryTuple> result = new LinkedList<>();
protected final Collection<RepositoryTuple> 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<String> getNodes(final String rootPath) {
private Collection<String> loadNodes(final String rootNode) {
Collection<String> result = new LinkedHashSet<>();
getAllNodes(result, rootPath);
loadNodes(rootNode, result);
if (1 == result.size()) {
return Collections.emptyList();
}
return result;
}

private void getAllNodes(final Collection<String> keys, final String path) {
keys.add(path);
List<String> 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<String> 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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public void persist(final String databaseName, final Collection<RuleConfiguratio

@Override
public Collection<RuleConfiguration> load(final String databaseName) {
Collection<RepositoryTuple> repositoryTuples = getRepositoryTuple(DatabaseRuleMetaDataNode.getRulesNode(databaseName));
Collection<RepositoryTuple> repositoryTuples = getRepositoryTuples(DatabaseRuleMetaDataNode.getRulesNode(databaseName));
return repositoryTuples.isEmpty() ? Collections.emptyList() : new YamlDataNodeRuleConfigurationSwapperEngine().swapToRuleConfigurations(repositoryTuples);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,23 +80,23 @@ private Collection<MetaDataVersion> persistDataNodes(final Collection<Repository
String nextActiveVersion = versions.isEmpty() ? DEFAULT_VERSION : String.valueOf(Integer.parseInt(versions.get(0)) + 1);
String persistKey = GlobalNode.getGlobalRuleVersionNode(each.getKey(), nextActiveVersion);
repository.persist(persistKey, each.getValue());
if (Strings.isNullOrEmpty(getActiveVersion(GlobalNode.getGlobalRuleActiveVersionNode(each.getKey())))) {
if (Strings.isNullOrEmpty(repository.getDirectly(GlobalNode.getGlobalRuleActiveVersionNode(each.getKey())))) {
repository.persist(GlobalNode.getGlobalRuleActiveVersionNode(each.getKey()), DEFAULT_VERSION);
}
result.add(new MetaDataVersion(GlobalNode.getGlobalRuleNode(each.getKey()), getActiveVersion(GlobalNode.getGlobalRuleActiveVersionNode(each.getKey())), nextActiveVersion));
result.add(new MetaDataVersion(GlobalNode.getGlobalRuleNode(each.getKey()), repository.getDirectly(GlobalNode.getGlobalRuleActiveVersionNode(each.getKey())), nextActiveVersion));
}
return result;
}

@Override
public Collection<RuleConfiguration> load() {
Collection<RepositoryTuple> repositoryTuples = getRepositoryTuple(GlobalNode.getGlobalRuleRootNode());
Collection<RepositoryTuple> repositoryTuples = getRepositoryTuples(GlobalNode.getGlobalRuleRootNode());
return repositoryTuples.isEmpty() ? Collections.emptyList() : new YamlDataNodeGlobalRuleConfigurationSwapperEngine().swapToRuleConfigurations(repositoryTuples);
}

@Override
public RuleConfiguration load(final String ruleName) {
Collection<RepositoryTuple> repositoryTuples = getRepositoryTuple(GlobalNode.getGlobalRuleNode(ruleName));
Collection<RepositoryTuple> repositoryTuples = getRepositoryTuples(GlobalNode.getGlobalRuleNode(ruleName));
return new YamlDataNodeGlobalRuleConfigurationSwapperEngine().swapSingleRuleToRuleConfiguration(ruleName, repositoryTuples).orElse(null);
}
}

0 comments on commit 7d84d30

Please sign in to comment.