Skip to content

Commit

Permalink
Rename VersionNodePathGenerator (#34598)
Browse files Browse the repository at this point in the history
* Add VersionNodePathParser

* Refactor usage of VersionNodePathParser

* Refactor usage of VersionNodePathParser

* Rename VersionNodePathGenerator

* Rename VersionNodePathGenerator

* Rename VersionNodePathGenerator
  • Loading branch information
terrymanu authored Feb 7, 2025
1 parent e02dd51 commit de71e64
Show file tree
Hide file tree
Showing 23 changed files with 192 additions and 111 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public Map<String, DataSourcePoolProperties> load(final String databaseName) {
@SuppressWarnings("unchecked")
public DataSourcePoolProperties load(final String databaseName, final String dataSourceName) {
Integer dataSourceActiveVersion = getDataSourceActiveVersion(databaseName, dataSourceName);
String dataSourceValue = repository.query(DataSourceMetaDataNodePath.getStorageUnitVersionNodePath(databaseName, dataSourceName)
String dataSourceValue = repository.query(DataSourceMetaDataNodePath.getStorageUnitVersionNodePathGenerator(databaseName, dataSourceName)
.getVersionPath(null == dataSourceActiveVersion ? MetaDataVersion.DEFAULT_VERSION : dataSourceActiveVersion));
return yamlDataSourceConfigurationSwapper.swapToDataSourcePoolProperties(YamlEngine.unmarshal(dataSourceValue, Map.class));
}
Expand All @@ -87,19 +87,20 @@ public Collection<MetaDataVersion> persist(final String databaseName, final Map<
Collection<MetaDataVersion> result = new LinkedList<>();
for (Entry<String, DataSourcePoolProperties> entry : dataSourcePropsMap.entrySet()) {
Integer activeVersion = getDataSourceActiveVersion(databaseName, entry.getKey());
int nextActiveVersion = metaDataVersionPersistService.getNextVersion(DataSourceMetaDataNodePath.getStorageUnitVersionNodePath(databaseName, entry.getKey()).getVersionsPath());
repository.persist(DataSourceMetaDataNodePath.getStorageUnitVersionNodePath(databaseName, entry.getKey()).getVersionPath(nextActiveVersion),
int nextActiveVersion = metaDataVersionPersistService.getNextVersion(DataSourceMetaDataNodePath.getStorageUnitVersionNodePathGenerator(databaseName, entry.getKey()).getVersionsPath());
repository.persist(DataSourceMetaDataNodePath.getStorageUnitVersionNodePathGenerator(databaseName, entry.getKey()).getVersionPath(nextActiveVersion),
YamlEngine.marshal(yamlDataSourceConfigurationSwapper.swapToMap(entry.getValue())));
if (null == activeVersion) {
repository.persist(DataSourceMetaDataNodePath.getStorageUnitVersionNodePath(databaseName, entry.getKey()).getActiveVersionPath(), String.valueOf(MetaDataVersion.DEFAULT_VERSION));
repository.persist(DataSourceMetaDataNodePath.getStorageUnitVersionNodePathGenerator(databaseName, entry.getKey()).getActiveVersionPath(),
String.valueOf(MetaDataVersion.DEFAULT_VERSION));
}
result.add(new MetaDataVersion(DataSourceMetaDataNodePath.getStorageUnitPath(databaseName, entry.getKey()), activeVersion, nextActiveVersion));
}
return result;
}

private Integer getDataSourceActiveVersion(final String databaseName, final String dataSourceName) {
String value = repository.query(DataSourceMetaDataNodePath.getStorageUnitVersionNodePath(databaseName, dataSourceName).getActiveVersionPath());
String value = repository.query(DataSourceMetaDataNodePath.getStorageUnitVersionNodePathGenerator(databaseName, dataSourceName).getActiveVersionPath());
return Strings.isNullOrEmpty(value) ? null : Integer.parseInt(value);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,18 +90,19 @@ public Collection<MetaDataVersion> persist(final String databaseName, final Coll
private Collection<MetaDataVersion> persistDataNodes(final String databaseName, final String ruleName, final Collection<RepositoryTuple> repositoryTuples) {
Collection<MetaDataVersion> result = new LinkedList<>();
for (RepositoryTuple each : repositoryTuples) {
int nextVersion = metaDataVersionPersistService.getNextVersion(DatabaseRuleMetaDataNodePath.getVersionNodePath(databaseName, ruleName, each.getKey()).getVersionsPath());
repository.persist(DatabaseRuleMetaDataNodePath.getVersionNodePath(databaseName, ruleName, each.getKey()).getVersionPath(nextVersion), each.getValue());
int nextVersion = metaDataVersionPersistService.getNextVersion(DatabaseRuleMetaDataNodePath.getVersionNodePathGenerator(databaseName, ruleName, each.getKey()).getVersionsPath());
repository.persist(DatabaseRuleMetaDataNodePath.getVersionNodePathGenerator(databaseName, ruleName, each.getKey()).getVersionPath(nextVersion), each.getValue());
if (null == getActiveVersion(databaseName, ruleName, each.getKey())) {
repository.persist(DatabaseRuleMetaDataNodePath.getVersionNodePath(databaseName, ruleName, each.getKey()).getActiveVersionPath(), String.valueOf(MetaDataVersion.DEFAULT_VERSION));
repository.persist(DatabaseRuleMetaDataNodePath.getVersionNodePathGenerator(databaseName, ruleName, each.getKey()).getActiveVersionPath(),
String.valueOf(MetaDataVersion.DEFAULT_VERSION));
}
result.add(new MetaDataVersion(DatabaseRuleMetaDataNodePath.getRulePath(databaseName, ruleName, each.getKey()), getActiveVersion(databaseName, ruleName, each.getKey()), nextVersion));
}
return result;
}

private Integer getActiveVersion(final String databaseName, final String ruleName, final String key) {
String value = repository.query(DatabaseRuleMetaDataNodePath.getVersionNodePath(databaseName, ruleName, key).getActiveVersionPath());
String value = repository.query(DatabaseRuleMetaDataNodePath.getVersionNodePathGenerator(databaseName, ruleName, key).getActiveVersionPath());
return Strings.isNullOrEmpty(value) ? null : Integer.parseInt(value);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,9 @@ public void persist(final Collection<RuleConfiguration> globalRuleConfigs) {
private Collection<MetaDataVersion> persistTuples(final Collection<RepositoryTuple> repositoryTuples) {
Collection<MetaDataVersion> result = new LinkedList<>();
for (RepositoryTuple each : repositoryTuples) {
int nextActiveVersion = metaDataVersionPersistService.getNextVersion(GlobalRuleNodePath.getVersionNodePath(each.getKey()).getVersionsPath());
repository.persist(GlobalRuleNodePath.getVersionNodePath(each.getKey()).getVersionPath(nextActiveVersion), each.getValue());
String ruleActiveVersionPath = GlobalRuleNodePath.getVersionNodePath(each.getKey()).getActiveVersionPath();
int nextActiveVersion = metaDataVersionPersistService.getNextVersion(GlobalRuleNodePath.getVersionNodePathGenerator(each.getKey()).getVersionsPath());
repository.persist(GlobalRuleNodePath.getVersionNodePathGenerator(each.getKey()).getVersionPath(nextActiveVersion), each.getValue());
String ruleActiveVersionPath = GlobalRuleNodePath.getVersionNodePathGenerator(each.getKey()).getActiveVersionPath();
if (null == getRuleActiveVersion(ruleActiveVersionPath)) {
repository.persist(ruleActiveVersionPath, String.valueOf(MetaDataVersion.DEFAULT_VERSION));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import org.apache.shardingsphere.infra.util.yaml.YamlEngine;
import org.apache.shardingsphere.mode.metadata.persist.version.MetaDataVersionPersistService;
import org.apache.shardingsphere.mode.node.path.config.GlobalPropertiesNodePath;
import org.apache.shardingsphere.mode.node.path.version.VersionNodePath;
import org.apache.shardingsphere.mode.node.path.version.VersionNodePathGenerator;
import org.apache.shardingsphere.mode.spi.repository.PersistRepository;

import java.util.Collections;
Expand All @@ -45,9 +45,9 @@ public final class PropertiesPersistService {
* @return properties
*/
public Properties load() {
VersionNodePath versionNodePath = GlobalPropertiesNodePath.getVersionNodePath();
Integer activeVersion = getActiveVersion(versionNodePath);
String yamlContent = repository.query(versionNodePath.getVersionPath(null == activeVersion ? 0 : activeVersion));
VersionNodePathGenerator versionNodePathGenerator = GlobalPropertiesNodePath.getVersionNodePathGenerator();
Integer activeVersion = getActiveVersion(versionNodePathGenerator);
String yamlContent = repository.query(versionNodePathGenerator.getVersionPath(null == activeVersion ? 0 : activeVersion));
return Strings.isNullOrEmpty(yamlContent) ? new Properties() : YamlEngine.unmarshal(yamlContent, Properties.class);
}

Expand All @@ -57,17 +57,18 @@ public Properties load() {
* @param props properties
*/
public void persist(final Properties props) {
VersionNodePath versionNodePath = GlobalPropertiesNodePath.getVersionNodePath();
int nextActiveVersion = metaDataVersionPersistService.getNextVersion(versionNodePath.getVersionsPath());
repository.persist(versionNodePath.getVersionPath(nextActiveVersion), YamlEngine.marshal(props));
if (null == getActiveVersion(versionNodePath)) {
repository.persist(versionNodePath.getActiveVersionPath(), String.valueOf(MetaDataVersion.DEFAULT_VERSION));
VersionNodePathGenerator versionNodePathGenerator = GlobalPropertiesNodePath.getVersionNodePathGenerator();
int nextActiveVersion = metaDataVersionPersistService.getNextVersion(versionNodePathGenerator.getVersionsPath());
repository.persist(versionNodePathGenerator.getVersionPath(nextActiveVersion), YamlEngine.marshal(props));
if (null == getActiveVersion(versionNodePathGenerator)) {
repository.persist(versionNodePathGenerator.getActiveVersionPath(), String.valueOf(MetaDataVersion.DEFAULT_VERSION));
}
metaDataVersionPersistService.switchActiveVersion(Collections.singleton(new MetaDataVersion(GlobalPropertiesNodePath.getRootPath(), getActiveVersion(versionNodePath), nextActiveVersion)));
metaDataVersionPersistService
.switchActiveVersion(Collections.singleton(new MetaDataVersion(GlobalPropertiesNodePath.getRootPath(), getActiveVersion(versionNodePathGenerator), nextActiveVersion)));
}

private Integer getActiveVersion(final VersionNodePath versionNodePath) {
String value = repository.query(versionNodePath.getActiveVersionPath());
private Integer getActiveVersion(final VersionNodePathGenerator versionNodePathGenerator) {
String value = repository.query(versionNodePathGenerator.getActiveVersionPath());
return Strings.isNullOrEmpty(value) ? null : Integer.parseInt(value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public Collection<ShardingSphereTable> load(final String databaseName, final Str
public ShardingSphereTable load(final String databaseName, final String schemaName, final String tableName) {
Integer activeVersion = getActiveVersion(databaseName, schemaName, tableName);
String tableContent = repository.query(
TableMetaDataNodePath.getVersionNodePath(databaseName, schemaName, tableName).getVersionPath(null == activeVersion ? MetaDataVersion.DEFAULT_VERSION : activeVersion));
TableMetaDataNodePath.getVersionNodePathGenerator(databaseName, schemaName, tableName).getVersionPath(null == activeVersion ? MetaDataVersion.DEFAULT_VERSION : activeVersion));
return swapper.swapToObject(YamlEngine.unmarshal(tableContent, YamlShardingSphereTable.class));
}

Expand All @@ -82,11 +82,12 @@ public void persist(final String databaseName, final String schemaName, final Co
Collection<MetaDataVersion> metaDataVersions = new LinkedList<>();
for (ShardingSphereTable each : tables) {
String tableName = each.getName().toLowerCase();
int nextActiveVersion = metaDataVersionPersistService.getNextVersion(TableMetaDataNodePath.getVersionNodePath(databaseName, schemaName, tableName).getVersionsPath());
int nextActiveVersion = metaDataVersionPersistService.getNextVersion(TableMetaDataNodePath.getVersionNodePathGenerator(databaseName, schemaName, tableName).getVersionsPath());
repository.persist(
TableMetaDataNodePath.getVersionNodePath(databaseName, schemaName, tableName).getVersionPath(nextActiveVersion), YamlEngine.marshal(swapper.swapToYamlConfiguration(each)));
TableMetaDataNodePath.getVersionNodePathGenerator(databaseName, schemaName, tableName).getVersionPath(nextActiveVersion),
YamlEngine.marshal(swapper.swapToYamlConfiguration(each)));
if (null == getActiveVersion(databaseName, schemaName, tableName)) {
repository.persist(TableMetaDataNodePath.getVersionNodePath(databaseName, schemaName, tableName).getActiveVersionPath(), String.valueOf(MetaDataVersion.DEFAULT_VERSION));
repository.persist(TableMetaDataNodePath.getVersionNodePathGenerator(databaseName, schemaName, tableName).getActiveVersionPath(), String.valueOf(MetaDataVersion.DEFAULT_VERSION));
}
metaDataVersions.add(
new MetaDataVersion(TableMetaDataNodePath.getTablePath(databaseName, schemaName, tableName), getActiveVersion(databaseName, schemaName, tableName), nextActiveVersion));
Expand All @@ -95,7 +96,7 @@ public void persist(final String databaseName, final String schemaName, final Co
}

private Integer getActiveVersion(final String databaseName, final String schemaName, final String tableName) {
String value = repository.query(TableMetaDataNodePath.getVersionNodePath(databaseName, schemaName, tableName).getActiveVersionPath());
String value = repository.query(TableMetaDataNodePath.getVersionNodePathGenerator(databaseName, schemaName, tableName).getActiveVersionPath());
return Strings.isNullOrEmpty(value) ? null : Integer.parseInt(value);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ public Collection<ShardingSphereView> load(final String databaseName, final Stri
public ShardingSphereView load(final String databaseName, final String schemaName, final String viewName) {
Integer activeVersion = getActiveVersion(databaseName, schemaName, viewName);
String view =
repository.query(ViewMetaDataNodePath.getVersionNodePath(databaseName, schemaName, viewName).getVersionPath(null == activeVersion ? MetaDataVersion.DEFAULT_VERSION : activeVersion));
repository.query(
ViewMetaDataNodePath.getVersionNodePathGenerator(databaseName, schemaName, viewName).getVersionPath(null == activeVersion ? MetaDataVersion.DEFAULT_VERSION : activeVersion));
return swapper.swapToObject(YamlEngine.unmarshal(view, YamlShardingSphereView.class));
}

Expand All @@ -81,19 +82,19 @@ public void persist(final String databaseName, final String schemaName, final Co
Collection<MetaDataVersion> metaDataVersions = new LinkedList<>();
for (ShardingSphereView each : views) {
String viewName = each.getName().toLowerCase();
int nextActiveVersion = metaDataVersionPersistService.getNextVersion(ViewMetaDataNodePath.getVersionNodePath(databaseName, schemaName, viewName).getVersionsPath());
repository.persist(ViewMetaDataNodePath.getVersionNodePath(databaseName, schemaName, viewName).getVersionPath(nextActiveVersion),
int nextActiveVersion = metaDataVersionPersistService.getNextVersion(ViewMetaDataNodePath.getVersionNodePathGenerator(databaseName, schemaName, viewName).getVersionsPath());
repository.persist(ViewMetaDataNodePath.getVersionNodePathGenerator(databaseName, schemaName, viewName).getVersionPath(nextActiveVersion),
YamlEngine.marshal(swapper.swapToYamlConfiguration(each)));
if (null == getActiveVersion(databaseName, schemaName, viewName)) {
repository.persist(ViewMetaDataNodePath.getVersionNodePath(databaseName, schemaName, viewName).getActiveVersionPath(), String.valueOf(MetaDataVersion.DEFAULT_VERSION));
repository.persist(ViewMetaDataNodePath.getVersionNodePathGenerator(databaseName, schemaName, viewName).getActiveVersionPath(), String.valueOf(MetaDataVersion.DEFAULT_VERSION));
}
metaDataVersions.add(new MetaDataVersion(ViewMetaDataNodePath.getViewPath(databaseName, schemaName, viewName), getActiveVersion(databaseName, schemaName, viewName), nextActiveVersion));
}
metaDataVersionPersistService.switchActiveVersion(metaDataVersions);
}

private Integer getActiveVersion(final String databaseName, final String schemaName, final String viewName) {
String value = repository.query(ViewMetaDataNodePath.getVersionNodePath(databaseName, schemaName, viewName).getActiveVersionPath());
String value = repository.query(ViewMetaDataNodePath.getVersionNodePathGenerator(databaseName, schemaName, viewName).getActiveVersionPath());
return Strings.isNullOrEmpty(value) ? null : Integer.parseInt(value);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import lombok.extern.slf4j.Slf4j;
import org.apache.shardingsphere.infra.metadata.version.MetaDataVersion;
import org.apache.shardingsphere.mode.node.path.metadata.DatabaseMetaDataNodePath;
import org.apache.shardingsphere.mode.node.path.version.VersionNodePath;
import org.apache.shardingsphere.mode.node.path.version.VersionNodePathGenerator;
import org.apache.shardingsphere.mode.spi.repository.PersistRepository;

import java.util.Collection;
Expand All @@ -48,10 +48,10 @@ public void switchActiveVersion(final Collection<MetaDataVersion> metaDataVersio
if (each.getNextActiveVersion().equals(each.getCurrentActiveVersion())) {
continue;
}
repository.persist(new VersionNodePath(each.getPath()).getActiveVersionPath(), String.valueOf(each.getNextActiveVersion()));
VersionNodePath versionNodePath = new VersionNodePath(each.getPath());
getVersions(versionNodePath.getVersionsPath()).stream()
.filter(version -> version < each.getNextActiveVersion()).forEach(version -> repository.delete(versionNodePath.getVersionPath(version)));
repository.persist(new VersionNodePathGenerator(each.getPath()).getActiveVersionPath(), String.valueOf(each.getNextActiveVersion()));
VersionNodePathGenerator versionNodePathGenerator = new VersionNodePathGenerator(each.getPath());
getVersions(versionNodePathGenerator.getVersionsPath()).stream()
.filter(version -> version < each.getNextActiveVersion()).forEach(version -> repository.delete(versionNodePathGenerator.getVersionPath(version)));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.shardingsphere.mode.node.path.version.VersionNodePath;
import org.apache.shardingsphere.mode.node.path.version.VersionNodePathGenerator;
import org.apache.shardingsphere.mode.node.path.version.VersionNodePathParser;

/**
Expand All @@ -42,12 +42,12 @@ public static String getRootPath() {
}

/**
* Get properties version node path.
* Get properties version node path generator.
*
* @return properties version node path
* @return properties version node path generator
*/
public static VersionNodePath getVersionNodePath() {
return new VersionNodePath(getRootPath());
public static VersionNodePathGenerator getVersionNodePathGenerator() {
return new VersionNodePathGenerator(getRootPath());
}

/**
Expand Down
Loading

0 comments on commit de71e64

Please sign in to comment.