Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move DEFAULT_VERSION to MetaDataVersion #31381

Merged
merged 1 commit into from
May 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
@Getter
public final class MetaDataVersion {

public static final String DEFAULT_VERSION = "0";

private static final String ACTIVE_VERSION = "active_version";

private static final String VERSIONS = "versions";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@
@RequiredArgsConstructor
public final class DataSourceNodePersistService implements DatabaseBasedPersistService<Map<String, DataSourcePoolProperties>> {

private static final String DEFAULT_VERSION = "0";

private final PersistRepository repository;

@Override
Expand All @@ -50,19 +48,20 @@ public void persist(final String databaseName, final Map<String, DataSourcePoolP
String activeVersion = getDataSourceActiveVersion(databaseName, entry.getKey());
List<String> versions = repository.getChildrenKeys(DataSourceMetaDataNode.getDataSourceNodeVersionsNode(databaseName, entry.getKey()));
repository.persist(DataSourceMetaDataNode.getDataSourceNodeVersionNode(databaseName, entry.getKey(), versions.isEmpty()
? DEFAULT_VERSION
? MetaDataVersion.DEFAULT_VERSION
: String.valueOf(Integer.parseInt(versions.get(0)) + 1)), YamlEngine.marshal(new YamlDataSourceConfigurationSwapper().swapToMap(entry.getValue())));
if (Strings.isNullOrEmpty(activeVersion)) {
repository.persist(DataSourceMetaDataNode.getDataSourceNodeActiveVersionNode(databaseName, entry.getKey()), DEFAULT_VERSION);
repository.persist(DataSourceMetaDataNode.getDataSourceNodeActiveVersionNode(databaseName, entry.getKey()), MetaDataVersion.DEFAULT_VERSION);
}
}
}

@SuppressWarnings("unchecked")
@Override
public Map<String, DataSourcePoolProperties> load(final String databaseName) {
Map<String, DataSourcePoolProperties> result = new LinkedHashMap<>();
for (String each : repository.getChildrenKeys(DataSourceMetaDataNode.getDataSourceNodesNode(databaseName))) {
Collection<String> childrenKeys = repository.getChildrenKeys(DataSourceMetaDataNode.getDataSourceNodesNode(databaseName));
Map<String, DataSourcePoolProperties> result = new LinkedHashMap<>(childrenKeys.size(), 1F);
for (String each : childrenKeys) {
String dataSourceValue = repository.query(DataSourceMetaDataNode.getDataSourceNodeVersionNode(databaseName, each, getDataSourceActiveVersion(databaseName, each)));
if (!Strings.isNullOrEmpty(dataSourceValue)) {
result.put(each, new YamlDataSourceConfigurationSwapper().swapToDataSourcePoolProperties(YamlEngine.unmarshal(dataSourceValue, Map.class)));
Expand Down Expand Up @@ -103,11 +102,11 @@ public Collection<MetaDataVersion> persistConfigurations(final String databaseNa
Collection<MetaDataVersion> result = new LinkedList<>();
for (Entry<String, DataSourcePoolProperties> entry : dataSourceConfigs.entrySet()) {
List<String> versions = repository.getChildrenKeys(DataSourceMetaDataNode.getDataSourceNodeVersionsNode(databaseName, entry.getKey()));
String nextActiveVersion = versions.isEmpty() ? DEFAULT_VERSION : String.valueOf(Integer.parseInt(versions.get(0)) + 1);
String nextActiveVersion = versions.isEmpty() ? MetaDataVersion.DEFAULT_VERSION : String.valueOf(Integer.parseInt(versions.get(0)) + 1);
repository.persist(DataSourceMetaDataNode.getDataSourceNodeVersionNode(databaseName, entry.getKey(), nextActiveVersion),
YamlEngine.marshal(new YamlDataSourceConfigurationSwapper().swapToMap(entry.getValue())));
if (Strings.isNullOrEmpty(getDataSourceActiveVersion(databaseName, entry.getKey()))) {
repository.persist(DataSourceMetaDataNode.getDataSourceNodeActiveVersionNode(databaseName, entry.getKey()), DEFAULT_VERSION);
repository.persist(DataSourceMetaDataNode.getDataSourceNodeActiveVersionNode(databaseName, entry.getKey()), MetaDataVersion.DEFAULT_VERSION);
}
result.add(new MetaDataVersion(DataSourceMetaDataNode.getDataSourceNodeNode(databaseName, entry.getKey()), getDataSourceActiveVersion(databaseName, entry.getKey()), nextActiveVersion));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@
@RequiredArgsConstructor
public final class DataSourceUnitPersistService implements DatabaseBasedPersistService<Map<String, DataSourcePoolProperties>> {

private static final String DEFAULT_VERSION = "0";

private final PersistRepository repository;

@Override
Expand All @@ -50,19 +48,20 @@ public void persist(final String databaseName, final Map<String, DataSourcePoolP
String activeVersion = getDataSourceActiveVersion(databaseName, entry.getKey());
List<String> versions = repository.getChildrenKeys(DataSourceMetaDataNode.getDataSourceUnitVersionsNode(databaseName, entry.getKey()));
repository.persist(DataSourceMetaDataNode.getDataSourceUnitVersionNode(databaseName, entry.getKey(), versions.isEmpty()
? DEFAULT_VERSION
? MetaDataVersion.DEFAULT_VERSION
: String.valueOf(Integer.parseInt(versions.get(0)) + 1)), YamlEngine.marshal(new YamlDataSourceConfigurationSwapper().swapToMap(entry.getValue())));
if (Strings.isNullOrEmpty(activeVersion)) {
repository.persist(DataSourceMetaDataNode.getDataSourceUnitActiveVersionNode(databaseName, entry.getKey()), DEFAULT_VERSION);
repository.persist(DataSourceMetaDataNode.getDataSourceUnitActiveVersionNode(databaseName, entry.getKey()), MetaDataVersion.DEFAULT_VERSION);
}
}
}

@SuppressWarnings("unchecked")
@Override
public Map<String, DataSourcePoolProperties> load(final String databaseName) {
Map<String, DataSourcePoolProperties> result = new LinkedHashMap<>();
for (String each : repository.getChildrenKeys(DataSourceMetaDataNode.getDataSourceUnitsNode(databaseName))) {
Collection<String> childrenKeys = repository.getChildrenKeys(DataSourceMetaDataNode.getDataSourceUnitsNode(databaseName));
Map<String, DataSourcePoolProperties> result = new LinkedHashMap<>(childrenKeys.size(), 1F);
for (String each : childrenKeys) {
String dataSourceValue = repository.query(DataSourceMetaDataNode.getDataSourceUnitVersionNode(databaseName, each, getDataSourceActiveVersion(databaseName, each)));
if (!Strings.isNullOrEmpty(dataSourceValue)) {
result.put(each, new YamlDataSourceConfigurationSwapper().swapToDataSourcePoolProperties(YamlEngine.unmarshal(dataSourceValue, Map.class)));
Expand Down Expand Up @@ -103,11 +102,11 @@ public Collection<MetaDataVersion> persistConfigurations(final String databaseNa
Collection<MetaDataVersion> result = new LinkedList<>();
for (Entry<String, DataSourcePoolProperties> entry : dataSourceConfigs.entrySet()) {
List<String> versions = repository.getChildrenKeys(DataSourceMetaDataNode.getDataSourceUnitVersionsNode(databaseName, entry.getKey()));
String nextActiveVersion = versions.isEmpty() ? DEFAULT_VERSION : String.valueOf(Integer.parseInt(versions.get(0)) + 1);
String nextActiveVersion = versions.isEmpty() ? MetaDataVersion.DEFAULT_VERSION : String.valueOf(Integer.parseInt(versions.get(0)) + 1);
repository.persist(DataSourceMetaDataNode.getDataSourceUnitVersionNode(databaseName, entry.getKey(), nextActiveVersion),
YamlEngine.marshal(new YamlDataSourceConfigurationSwapper().swapToMap(entry.getValue())));
if (Strings.isNullOrEmpty(getDataSourceActiveVersion(databaseName, entry.getKey()))) {
repository.persist(DataSourceMetaDataNode.getDataSourceUnitActiveVersionNode(databaseName, entry.getKey()), DEFAULT_VERSION);
repository.persist(DataSourceMetaDataNode.getDataSourceUnitActiveVersionNode(databaseName, entry.getKey()), MetaDataVersion.DEFAULT_VERSION);
}
result.add(new MetaDataVersion(DataSourceMetaDataNode.getDataSourceUnitNode(databaseName, entry.getKey()), getDataSourceActiveVersion(databaseName, entry.getKey()), nextActiveVersion));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@
*/
public final class DatabaseRulePersistService implements DatabaseBasedPersistService<Collection<RuleConfiguration>> {

private static final String DEFAULT_VERSION = "0";

private final PersistRepository repository;

private final RepositoryTuplePersistService repositoryTuplePersistService;
Expand Down Expand Up @@ -91,10 +89,10 @@ private Collection<MetaDataVersion> persistDataNodes(final String databaseName,
Collection<MetaDataVersion> result = new LinkedList<>();
for (RepositoryTuple each : repositoryTuples) {
List<String> versions = repository.getChildrenKeys(DatabaseRuleMetaDataNode.getDatabaseRuleVersionsNode(databaseName, ruleName, each.getKey()));
String nextVersion = versions.isEmpty() ? DEFAULT_VERSION : String.valueOf(Integer.parseInt(versions.get(0)) + 1);
String nextVersion = versions.isEmpty() ? MetaDataVersion.DEFAULT_VERSION : String.valueOf(Integer.parseInt(versions.get(0)) + 1);
repository.persist(DatabaseRuleMetaDataNode.getDatabaseRuleVersionNode(databaseName, ruleName, each.getKey(), nextVersion), each.getValue());
if (Strings.isNullOrEmpty(getActiveVersion(databaseName, ruleName, each.getKey()))) {
repository.persist(DatabaseRuleMetaDataNode.getDatabaseRuleActiveVersionNode(databaseName, ruleName, each.getKey()), DEFAULT_VERSION);
repository.persist(DatabaseRuleMetaDataNode.getDatabaseRuleActiveVersionNode(databaseName, ruleName, each.getKey()), MetaDataVersion.DEFAULT_VERSION);
}
result.add(new MetaDataVersion(DatabaseRuleMetaDataNode.getDatabaseRuleNode(databaseName, ruleName, each.getKey()), getActiveVersion(databaseName, ruleName, each.getKey()), nextVersion));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@
*/
public final class GlobalRulePersistService implements GlobalPersistService<Collection<RuleConfiguration>> {

private static final String DEFAULT_VERSION = "0";

private final PersistRepository repository;

private final MetaDataVersionPersistService metaDataVersionPersistService;
Expand Down Expand Up @@ -70,10 +68,10 @@ private Collection<MetaDataVersion> persistTuples(final Collection<RepositoryTup
Collection<MetaDataVersion> result = new LinkedList<>();
for (RepositoryTuple each : repositoryTuples) {
List<String> versions = repository.getChildrenKeys(GlobalNode.getGlobalRuleVersionsNode(each.getKey()));
String nextActiveVersion = versions.isEmpty() ? DEFAULT_VERSION : String.valueOf(Integer.parseInt(versions.get(0)) + 1);
String nextActiveVersion = versions.isEmpty() ? MetaDataVersion.DEFAULT_VERSION : String.valueOf(Integer.parseInt(versions.get(0)) + 1);
repository.persist(GlobalNode.getGlobalRuleVersionNode(each.getKey(), nextActiveVersion), each.getValue());
if (Strings.isNullOrEmpty(repository.query(GlobalNode.getGlobalRuleActiveVersionNode(each.getKey())))) {
repository.persist(GlobalNode.getGlobalRuleActiveVersionNode(each.getKey()), DEFAULT_VERSION);
repository.persist(GlobalNode.getGlobalRuleActiveVersionNode(each.getKey()), MetaDataVersion.DEFAULT_VERSION);
}
result.add(new MetaDataVersion(GlobalNode.getGlobalRuleNode(each.getKey()), repository.query(GlobalNode.getGlobalRuleActiveVersionNode(each.getKey())), nextActiveVersion));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,17 @@
@RequiredArgsConstructor
public final class PropertiesPersistService implements GlobalPersistService<Properties> {

private static final String DEFAULT_VERSION = "0";

private final PersistRepository repository;

private final MetaDataVersionPersistService metaDataVersionPersistService;

@Override
public void persist(final Properties props) {
List<String> versions = repository.getChildrenKeys(GlobalNode.getPropsVersionsNode());
String nextActiveVersion = versions.isEmpty() ? DEFAULT_VERSION : String.valueOf(Integer.parseInt(versions.get(0)) + 1);
String nextActiveVersion = versions.isEmpty() ? MetaDataVersion.DEFAULT_VERSION : String.valueOf(Integer.parseInt(versions.get(0)) + 1);
repository.persist(GlobalNode.getPropsVersionNode(nextActiveVersion), YamlEngine.marshal(props));
if (Strings.isNullOrEmpty(getActiveVersion())) {
repository.persist(GlobalNode.getPropsActiveVersionNode(), DEFAULT_VERSION);
repository.persist(GlobalNode.getPropsActiveVersionNode(), MetaDataVersion.DEFAULT_VERSION);
}
metaDataVersionPersistService.switchActiveVersion(Collections.singletonList(new MetaDataVersion(GlobalNode.getPropsRootNode(),
getActiveVersion(), nextActiveVersion)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@
@RequiredArgsConstructor
public final class TableMetaDataPersistService implements SchemaMetaDataPersistService<Map<String, ShardingSphereTable>> {

private static final String DEFAULT_VERSION = "0";

private final PersistRepository repository;

private final MetaDataVersionPersistService metaDataVersionPersistService;
Expand All @@ -54,13 +52,13 @@ public void persist(final String databaseName, final String schemaName, final Ma
for (Entry<String, ShardingSphereTable> entry : tables.entrySet()) {
String tableName = entry.getKey().toLowerCase();
List<String> versions = repository.getChildrenKeys(TableMetaDataNode.getTableVersionsNode(databaseName, schemaName, tableName));
String nextActiveVersion = versions.isEmpty() ? DEFAULT_VERSION : String.valueOf(Integer.parseInt(versions.get(0)) + 1);
String nextActiveVersion = versions.isEmpty() ? MetaDataVersion.DEFAULT_VERSION : String.valueOf(Integer.parseInt(versions.get(0)) + 1);
if (entry.getValue() != null) {
repository.persist(TableMetaDataNode.getTableVersionNode(databaseName, schemaName, tableName, nextActiveVersion),
YamlEngine.marshal(new YamlTableSwapper().swapToYamlConfiguration(entry.getValue())));
}
if (Strings.isNullOrEmpty(getActiveVersion(databaseName, schemaName, tableName))) {
repository.persist(TableMetaDataNode.getTableActiveVersionNode(databaseName, schemaName, tableName), DEFAULT_VERSION);
repository.persist(TableMetaDataNode.getTableActiveVersionNode(databaseName, schemaName, tableName), MetaDataVersion.DEFAULT_VERSION);
}
metaDataVersions.add(new MetaDataVersion(TableMetaDataNode.getTableNode(databaseName, schemaName, tableName), getActiveVersion(databaseName, schemaName, tableName), nextActiveVersion));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@
@RequiredArgsConstructor
public final class ViewMetaDataPersistService implements SchemaMetaDataPersistService<Map<String, ShardingSphereView>> {

private static final String DEFAULT_VERSION = "0";

private final PersistRepository repository;

private final MetaDataVersionPersistService metaDataVersionPersistService;
Expand All @@ -54,11 +52,11 @@ public void persist(final String databaseName, final String schemaName, final Ma
for (Entry<String, ShardingSphereView> entry : views.entrySet()) {
String viewName = entry.getKey().toLowerCase();
List<String> versions = repository.getChildrenKeys(ViewMetaDataNode.getViewVersionsNode(databaseName, schemaName, viewName));
String nextActiveVersion = versions.isEmpty() ? DEFAULT_VERSION : String.valueOf(Integer.parseInt(versions.get(0)) + 1);
String nextActiveVersion = versions.isEmpty() ? MetaDataVersion.DEFAULT_VERSION : String.valueOf(Integer.parseInt(versions.get(0)) + 1);
repository.persist(ViewMetaDataNode.getViewVersionNode(databaseName, schemaName, viewName, nextActiveVersion),
YamlEngine.marshal(new YamlViewSwapper().swapToYamlConfiguration(entry.getValue())));
if (Strings.isNullOrEmpty(getActiveVersion(databaseName, schemaName, viewName))) {
repository.persist(ViewMetaDataNode.getViewActiveVersionNode(databaseName, schemaName, viewName), DEFAULT_VERSION);
repository.persist(ViewMetaDataNode.getViewActiveVersionNode(databaseName, schemaName, viewName), MetaDataVersion.DEFAULT_VERSION);
}
metaDataVersions.add(new MetaDataVersion(ViewMetaDataNode.getViewNode(databaseName, schemaName, viewName), getActiveVersion(databaseName, schemaName, viewName), nextActiveVersion));
}
Expand Down