Skip to content

Commit

Permalink
Refactor DatabaseBasedPersistService (#31034)
Browse files Browse the repository at this point in the history
* Refactor DatabaseBasedPersistService

* Refactor DatabaseBasedPersistService

* Refactor DatabaseBasedPersistService

* Refactor DatabaseBasedPersistService
  • Loading branch information
terrymanu authored Apr 27, 2024
1 parent f5b9f24 commit 37c9d9e
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,17 @@ default T load(String databaseName, String name) {
* Delete configurations.
*
* @param databaseName database name
* @param configs configurations
* @param configs to be deleted configurations
* @return meta data versions
*/
Collection<MetaDataVersion> deleteConfig(String databaseName, T configs);
Collection<MetaDataVersion> deleteConfigurations(String databaseName, T configs);

/**
* Persist configurations.
*
* @param databaseName database name
* @param configs configurations
* @param configs to be persisted configurations
* @return meta data versions
*/
Collection<MetaDataVersion> persistConfig(String databaseName, T configs);
Collection<MetaDataVersion> persistConfigurations(String databaseName, T configs);
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public void delete(final String databaseName, final String name) {
}

@Override
public Collection<MetaDataVersion> deleteConfig(final String databaseName, final Map<String, DataSourcePoolProperties> dataSourceConfigs) {
public Collection<MetaDataVersion> deleteConfigurations(final String databaseName, final Map<String, DataSourcePoolProperties> dataSourceConfigs) {
Collection<MetaDataVersion> result = new LinkedList<>();
for (Entry<String, DataSourcePoolProperties> entry : dataSourceConfigs.entrySet()) {
String delKey = DataSourceMetaDataNode.getDataSourceNodeNode(databaseName, entry.getKey());
Expand All @@ -99,7 +99,7 @@ public Collection<MetaDataVersion> deleteConfig(final String databaseName, final
}

@Override
public Collection<MetaDataVersion> persistConfig(final String databaseName, final Map<String, DataSourcePoolProperties> dataSourceConfigs) {
public Collection<MetaDataVersion> persistConfigurations(final String databaseName, final Map<String, DataSourcePoolProperties> dataSourceConfigs) {
Collection<MetaDataVersion> result = new LinkedList<>();
for (Entry<String, DataSourcePoolProperties> entry : dataSourceConfigs.entrySet()) {
List<String> versions = repository.getChildrenKeys(DataSourceMetaDataNode.getDataSourceNodeVersionsNode(databaseName, entry.getKey()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public void delete(final String databaseName, final String name) {
}

@Override
public Collection<MetaDataVersion> deleteConfig(final String databaseName, final Map<String, DataSourcePoolProperties> dataSourceConfigs) {
public Collection<MetaDataVersion> deleteConfigurations(final String databaseName, final Map<String, DataSourcePoolProperties> dataSourceConfigs) {
Collection<MetaDataVersion> result = new LinkedList<>();
for (Entry<String, DataSourcePoolProperties> entry : dataSourceConfigs.entrySet()) {
String delKey = DataSourceMetaDataNode.getDataSourceUnitNode(databaseName, entry.getKey());
Expand All @@ -99,7 +99,7 @@ public Collection<MetaDataVersion> deleteConfig(final String databaseName, final
}

@Override
public Collection<MetaDataVersion> persistConfig(final String databaseName, final Map<String, DataSourcePoolProperties> dataSourceConfigs) {
public Collection<MetaDataVersion> persistConfigurations(final String databaseName, final Map<String, DataSourcePoolProperties> dataSourceConfigs) {
Collection<MetaDataVersion> result = new LinkedList<>();
for (Entry<String, DataSourcePoolProperties> entry : dataSourceConfigs.entrySet()) {
List<String> versions = repository.getChildrenKeys(DataSourceMetaDataNode.getDataSourceUnitVersionsNode(databaseName, entry.getKey()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

/**
Expand All @@ -52,8 +51,7 @@ public DatabaseRulePersistService(final PersistRepository repository) {
@SuppressWarnings({"unchecked", "rawtypes"})
@Override
public void persist(final String databaseName, final Collection<RuleConfiguration> configs) {
Map<RuleConfiguration, YamlDataNodeRuleConfigurationSwapper> yamlConfigs = new YamlDataNodeRuleConfigurationSwapperEngine().swapToYamlRuleConfigurations(configs);
for (Entry<RuleConfiguration, YamlDataNodeRuleConfigurationSwapper> entry : yamlConfigs.entrySet()) {
for (Entry<RuleConfiguration, YamlDataNodeRuleConfigurationSwapper> entry : new YamlDataNodeRuleConfigurationSwapperEngine().swapToYamlRuleConfigurations(configs).entrySet()) {
Collection<RepositoryTuple> repositoryTuples = entry.getValue().swapToRepositoryTuples(entry.getKey());
if (!repositoryTuples.isEmpty()) {
persistDataNodes(databaseName, entry.getValue().getRuleTagName().toLowerCase(), repositoryTuples);
Expand All @@ -63,8 +61,8 @@ public void persist(final String databaseName, final Collection<RuleConfiguratio

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

@Override
Expand All @@ -74,10 +72,9 @@ public void delete(final String databaseName, final String name) {

@SuppressWarnings({"unchecked", "rawtypes"})
@Override
public Collection<MetaDataVersion> persistConfig(final String databaseName, final Collection<RuleConfiguration> configs) {
public Collection<MetaDataVersion> persistConfigurations(final String databaseName, final Collection<RuleConfiguration> configs) {
Collection<MetaDataVersion> result = new LinkedList<>();
Map<RuleConfiguration, YamlDataNodeRuleConfigurationSwapper> yamlConfigs = new YamlDataNodeRuleConfigurationSwapperEngine().swapToYamlRuleConfigurations(configs);
for (Entry<RuleConfiguration, YamlDataNodeRuleConfigurationSwapper> entry : yamlConfigs.entrySet()) {
for (Entry<RuleConfiguration, YamlDataNodeRuleConfigurationSwapper> entry : new YamlDataNodeRuleConfigurationSwapperEngine().swapToYamlRuleConfigurations(configs).entrySet()) {
Collection<RepositoryTuple> repositoryTuples = entry.getValue().swapToRepositoryTuples(entry.getKey());
if (!repositoryTuples.isEmpty()) {
result.addAll(persistDataNodes(databaseName, entry.getValue().getRuleTagName().toLowerCase(), repositoryTuples));
Expand All @@ -102,10 +99,9 @@ private Collection<MetaDataVersion> persistDataNodes(final String databaseName,

@SuppressWarnings({"unchecked", "rawtypes"})
@Override
public Collection<MetaDataVersion> deleteConfig(final String databaseName, final Collection<RuleConfiguration> configs) {
public Collection<MetaDataVersion> deleteConfigurations(final String databaseName, final Collection<RuleConfiguration> configs) {
Collection<MetaDataVersion> result = new LinkedList<>();
Map<RuleConfiguration, YamlDataNodeRuleConfigurationSwapper> yamlConfigs = new YamlDataNodeRuleConfigurationSwapperEngine().swapToYamlRuleConfigurations(configs);
for (Entry<RuleConfiguration, YamlDataNodeRuleConfigurationSwapper> entry : yamlConfigs.entrySet()) {
for (Entry<RuleConfiguration, YamlDataNodeRuleConfigurationSwapper> entry : new YamlDataNodeRuleConfigurationSwapperEngine().swapToYamlRuleConfigurations(configs).entrySet()) {
Collection<RepositoryTuple> repositoryTuples = entry.getValue().swapToRepositoryTuples(entry.getKey());
if (repositoryTuples.isEmpty()) {
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

/**
Expand All @@ -51,8 +50,8 @@ public GlobalRulePersistService(final PersistRepository repository) {
@SuppressWarnings({"unchecked", "rawtypes"})
@Override
public void persist(final Collection<RuleConfiguration> globalRuleConfigs) {
Map<RuleConfiguration, YamlDataNodeGlobalRuleConfigurationSwapper> yamlConfigs = new YamlDataNodeGlobalRuleConfigurationSwapperEngine().swapToYamlRuleConfigurations(globalRuleConfigs);
for (Entry<RuleConfiguration, YamlDataNodeGlobalRuleConfigurationSwapper> entry : yamlConfigs.entrySet()) {
for (Entry<RuleConfiguration, YamlDataNodeGlobalRuleConfigurationSwapper> entry : new YamlDataNodeGlobalRuleConfigurationSwapperEngine()
.swapToYamlRuleConfigurations(globalRuleConfigs).entrySet()) {
Collection<RepositoryTuple> repositoryTuples = entry.getValue().swapToRepositoryTuples(entry.getKey());
if (!repositoryTuples.isEmpty()) {
persistDataNodes(repositoryTuples);
Expand All @@ -64,8 +63,8 @@ public void persist(final Collection<RuleConfiguration> globalRuleConfigs) {
@Override
public Collection<MetaDataVersion> persistConfigurations(final Collection<RuleConfiguration> globalRuleConfigs) {
Collection<MetaDataVersion> result = new LinkedList<>();
Map<RuleConfiguration, YamlDataNodeGlobalRuleConfigurationSwapper> yamlConfigs = new YamlDataNodeGlobalRuleConfigurationSwapperEngine().swapToYamlRuleConfigurations(globalRuleConfigs);
for (Entry<RuleConfiguration, YamlDataNodeGlobalRuleConfigurationSwapper> entry : yamlConfigs.entrySet()) {
for (Entry<RuleConfiguration, YamlDataNodeGlobalRuleConfigurationSwapper> entry : new YamlDataNodeGlobalRuleConfigurationSwapperEngine()
.swapToYamlRuleConfigurations(globalRuleConfigs).entrySet()) {
Collection<RepositoryTuple> repositoryTuples = entry.getValue().swapToRepositoryTuples(entry.getKey());
if (!repositoryTuples.isEmpty()) {
result.addAll(persistDataNodes(repositoryTuples));
Expand All @@ -91,13 +90,13 @@ private Collection<MetaDataVersion> persistDataNodes(final Collection<Repository

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

@Override
public RuleConfiguration load(final String ruleName) {
Collection<RepositoryTuple> repositoryTuple = getRepositoryTuple(GlobalNode.getGlobalRuleNode(ruleName));
return new YamlDataNodeGlobalRuleConfigurationSwapperEngine().swapSingleRuleToRuleConfiguration(ruleName, repositoryTuple).orElse(null);
Collection<RepositoryTuple> repositoryTuples = getRepositoryTuple(GlobalNode.getGlobalRuleNode(ruleName));
return new YamlDataNodeGlobalRuleConfigurationSwapperEngine().swapSingleRuleToRuleConfiguration(ruleName, repositoryTuples).orElse(null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -376,9 +376,8 @@ public synchronized void alterGlobalRuleConfiguration(final RuleConfiguration ru
@SuppressWarnings("rawtypes")
@SneakyThrows(Exception.class)
private void closeStaleTransactionRule(final RuleConfiguration ruleConfig) {
Map<RuleConfiguration, YamlDataNodeGlobalRuleConfigurationSwapper> yamlConfigs =
new YamlDataNodeGlobalRuleConfigurationSwapperEngine().swapToYamlRuleConfigurations(Collections.singletonList(ruleConfig));
for (Entry<RuleConfiguration, YamlDataNodeGlobalRuleConfigurationSwapper> entry : yamlConfigs.entrySet()) {
for (Entry<RuleConfiguration, YamlDataNodeGlobalRuleConfigurationSwapper> entry : new YamlDataNodeGlobalRuleConfigurationSwapperEngine()
.swapToYamlRuleConfigurations(Collections.singleton(ruleConfig)).entrySet()) {
if ("transaction".equalsIgnoreCase(entry.getValue().getRuleTagName())) {
Optional<TransactionRule> transactionRule = metaDataContexts.get().getMetaData().getGlobalRuleMetaData().findSingleRule(TransactionRule.class);
if (!transactionRule.isPresent()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,18 +94,18 @@ public void alterSchemaMetaData(final AlterSchemaMetaDataPOJO alterSchemaMetaDat

@Override
public void registerStorageUnits(final String databaseName, final Map<String, DataSourcePoolProperties> toBeRegisteredProps) {
contextManager.getMetaDataContexts().getPersistService().getDataSourceUnitService().persistConfig(databaseName, toBeRegisteredProps);
contextManager.getMetaDataContexts().getPersistService().getDataSourceUnitService().persistConfigurations(databaseName, toBeRegisteredProps);
}

@Override
public void alterStorageUnits(final String databaseName, final Map<String, DataSourcePoolProperties> toBeUpdatedProps) {
DatabaseBasedPersistService<Map<String, DataSourcePoolProperties>> dataSourceService = contextManager.getMetaDataContexts().getPersistService().getDataSourceUnitService();
contextManager.getMetaDataContexts().getPersistService().getMetaDataVersionPersistService().switchActiveVersion(dataSourceService.persistConfig(databaseName, toBeUpdatedProps));
contextManager.getMetaDataContexts().getPersistService().getMetaDataVersionPersistService().switchActiveVersion(dataSourceService.persistConfigurations(databaseName, toBeUpdatedProps));
}

@Override
public void unregisterStorageUnits(final String databaseName, final Collection<String> toBeDroppedStorageUnitNames) {
contextManager.getMetaDataContexts().getPersistService().getDataSourceUnitService().deleteConfig(databaseName,
contextManager.getMetaDataContexts().getPersistService().getDataSourceUnitService().deleteConfigurations(databaseName,
getToBeDroppedDataSourcePoolProperties(contextManager.getMetaDataContexts().getPersistService().getDataSourceUnitService().load(databaseName), toBeDroppedStorageUnitNames));
}

Expand All @@ -123,21 +123,21 @@ private Map<String, DataSourcePoolProperties> getToBeDroppedDataSourcePoolProper
public void alterSingleRuleConfiguration(final String databaseName, final Collection<RuleConfiguration> ruleConfigs) {
ruleConfigs.removeIf(each -> !each.getClass().isAssignableFrom(SingleRuleConfiguration.class));
contextManager.getMetaDataContexts().getPersistService().getMetaDataVersionPersistService()
.switchActiveVersion(contextManager.getMetaDataContexts().getPersistService().getDatabaseRulePersistService().persistConfig(databaseName, ruleConfigs));
.switchActiveVersion(contextManager.getMetaDataContexts().getPersistService().getDatabaseRulePersistService().persistConfigurations(databaseName, ruleConfigs));
}

@Override
public Collection<MetaDataVersion> alterRuleConfiguration(final String databaseName, final RuleConfiguration toBeAlteredRuleConfig) {
if (null != toBeAlteredRuleConfig) {
return contextManager.getMetaDataContexts().getPersistService().getDatabaseRulePersistService().persistConfig(databaseName, Collections.singleton(toBeAlteredRuleConfig));
return contextManager.getMetaDataContexts().getPersistService().getDatabaseRulePersistService().persistConfigurations(databaseName, Collections.singleton(toBeAlteredRuleConfig));
}
return Collections.emptyList();
}

@Override
public void removeRuleConfigurationItem(final String databaseName, final RuleConfiguration toBeRemovedRuleConfig) {
if (null != toBeRemovedRuleConfig) {
contextManager.getMetaDataContexts().getPersistService().getDatabaseRulePersistService().deleteConfig(databaseName, Collections.singleton(toBeRemovedRuleConfig));
contextManager.getMetaDataContexts().getPersistService().getDatabaseRulePersistService().deleteConfigurations(databaseName, Collections.singleton(toBeRemovedRuleConfig));
}
}

Expand Down
Loading

0 comments on commit 37c9d9e

Please sign in to comment.