Skip to content

Commit

Permalink
Refactor AbstractPersistService to RepositoryTuplePersistService (#31036
Browse files Browse the repository at this point in the history
)
  • Loading branch information
terrymanu authored Apr 27, 2024
1 parent 7d84d30 commit 12144de
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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$";

Expand All @@ -38,7 +41,13 @@ public abstract class AbstractPersistService {

private final PersistRepository repository;

protected final Collection<RepositoryTuple> getRepositoryTuples(final String rootNode) {
/**
* Load repository tuples.
*
* @param rootNode root node
* @return loaded repository tuples
*/
public Collection<RepositoryTuple> 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());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -37,15 +37,17 @@
/**
* Database rule persist service.
*/
public final class DatabaseRulePersistService extends AbstractPersistService implements DatabaseBasedPersistService<Collection<RuleConfiguration>> {
public final class DatabaseRulePersistService implements DatabaseBasedPersistService<Collection<RuleConfiguration>> {

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"})
Expand All @@ -61,7 +63,7 @@ public void persist(final String databaseName, final Collection<RuleConfiguratio

@Override
public Collection<RuleConfiguration> load(final String databaseName) {
Collection<RepositoryTuple> repositoryTuples = getRepositoryTuples(DatabaseRuleMetaDataNode.getRulesNode(databaseName));
Collection<RepositoryTuple> repositoryTuples = repositoryTuplePersistService.loadRepositoryTuples(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 @@ -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;
Expand All @@ -36,15 +36,17 @@
/**
* Global rule persist service.
*/
public final class GlobalRulePersistService extends AbstractPersistService implements GlobalPersistService<Collection<RuleConfiguration>> {
public final class GlobalRulePersistService implements GlobalPersistService<Collection<RuleConfiguration>> {

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"})
Expand All @@ -54,7 +56,7 @@ public void persist(final Collection<RuleConfiguration> globalRuleConfigs) {
.swapToYamlRuleConfigurations(globalRuleConfigs).entrySet()) {
Collection<RepositoryTuple> repositoryTuples = entry.getValue().swapToRepositoryTuples(entry.getKey());
if (!repositoryTuples.isEmpty()) {
persistDataNodes(repositoryTuples);
persistTuples(repositoryTuples);
}
}
}
Expand All @@ -67,13 +69,13 @@ public Collection<MetaDataVersion> persistConfigurations(final Collection<RuleCo
.swapToYamlRuleConfigurations(globalRuleConfigs).entrySet()) {
Collection<RepositoryTuple> repositoryTuples = entry.getValue().swapToRepositoryTuples(entry.getKey());
if (!repositoryTuples.isEmpty()) {
result.addAll(persistDataNodes(repositoryTuples));
result.addAll(persistTuples(repositoryTuples));
}
}
return result;
}

private Collection<MetaDataVersion> persistDataNodes(final Collection<RepositoryTuple> repositoryTuples) {
private Collection<MetaDataVersion> persistTuples(final Collection<RepositoryTuple> repositoryTuples) {
Collection<MetaDataVersion> result = new LinkedList<>();
for (RepositoryTuple each : repositoryTuples) {
List<String> versions = repository.getChildrenKeys(GlobalNode.getGlobalRuleVersionsNode(each.getKey()));
Expand All @@ -90,13 +92,13 @@ private Collection<MetaDataVersion> persistDataNodes(final Collection<Repository

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

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

0 comments on commit 12144de

Please sign in to comment.