From 0314ec248d317035c505ed56aa0d246b08aca39d Mon Sep 17 00:00:00 2001 From: Liang Zhang Date: Sat, 25 May 2024 13:23:20 +0800 Subject: [PATCH] Move DEFAULT_VERSION to MetaDataVersion (#31381) --- .../infra/metadata/version/MetaDataVersion.java | 2 ++ .../datasource/DataSourceNodePersistService.java | 15 +++++++-------- .../datasource/DataSourceUnitPersistService.java | 15 +++++++-------- .../database/rule/DatabaseRulePersistService.java | 6 ++---- .../config/global/GlobalRulePersistService.java | 6 ++---- .../config/global/PropertiesPersistService.java | 6 ++---- .../schema/TableMetaDataPersistService.java | 6 ++---- .../schema/ViewMetaDataPersistService.java | 6 ++---- 8 files changed, 26 insertions(+), 36 deletions(-) diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/version/MetaDataVersion.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/version/MetaDataVersion.java index c2c3b254aac4f..f3e09eebc0b18 100644 --- a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/version/MetaDataVersion.java +++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/version/MetaDataVersion.java @@ -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"; diff --git a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/config/database/datasource/DataSourceNodePersistService.java b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/config/database/datasource/DataSourceNodePersistService.java index b20032be6b5f6..e532f5251ea90 100644 --- a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/config/database/datasource/DataSourceNodePersistService.java +++ b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/config/database/datasource/DataSourceNodePersistService.java @@ -40,8 +40,6 @@ @RequiredArgsConstructor public final class DataSourceNodePersistService implements DatabaseBasedPersistService> { - private static final String DEFAULT_VERSION = "0"; - private final PersistRepository repository; @Override @@ -50,10 +48,10 @@ public void persist(final String databaseName, final Map 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); } } } @@ -61,8 +59,9 @@ public void persist(final String databaseName, final Map load(final String databaseName) { - Map result = new LinkedHashMap<>(); - for (String each : repository.getChildrenKeys(DataSourceMetaDataNode.getDataSourceNodesNode(databaseName))) { + Collection childrenKeys = repository.getChildrenKeys(DataSourceMetaDataNode.getDataSourceNodesNode(databaseName)); + Map 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))); @@ -103,11 +102,11 @@ public Collection persistConfigurations(final String databaseNa Collection result = new LinkedList<>(); for (Entry entry : dataSourceConfigs.entrySet()) { List 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)); } diff --git a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/config/database/datasource/DataSourceUnitPersistService.java b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/config/database/datasource/DataSourceUnitPersistService.java index 4c5fb9a60e27f..82aa2cc896cb5 100644 --- a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/config/database/datasource/DataSourceUnitPersistService.java +++ b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/config/database/datasource/DataSourceUnitPersistService.java @@ -40,8 +40,6 @@ @RequiredArgsConstructor public final class DataSourceUnitPersistService implements DatabaseBasedPersistService> { - private static final String DEFAULT_VERSION = "0"; - private final PersistRepository repository; @Override @@ -50,10 +48,10 @@ public void persist(final String databaseName, final Map 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); } } } @@ -61,8 +59,9 @@ public void persist(final String databaseName, final Map load(final String databaseName) { - Map result = new LinkedHashMap<>(); - for (String each : repository.getChildrenKeys(DataSourceMetaDataNode.getDataSourceUnitsNode(databaseName))) { + Collection childrenKeys = repository.getChildrenKeys(DataSourceMetaDataNode.getDataSourceUnitsNode(databaseName)); + Map 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))); @@ -103,11 +102,11 @@ public Collection persistConfigurations(final String databaseNa Collection result = new LinkedList<>(); for (Entry entry : dataSourceConfigs.entrySet()) { List 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)); } diff --git a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/config/database/rule/DatabaseRulePersistService.java b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/config/database/rule/DatabaseRulePersistService.java index 602550d062c41..9f3cdad159287 100644 --- a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/config/database/rule/DatabaseRulePersistService.java +++ b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/config/database/rule/DatabaseRulePersistService.java @@ -41,8 +41,6 @@ */ public final class DatabaseRulePersistService implements DatabaseBasedPersistService> { - private static final String DEFAULT_VERSION = "0"; - private final PersistRepository repository; private final RepositoryTuplePersistService repositoryTuplePersistService; @@ -91,10 +89,10 @@ private Collection persistDataNodes(final String databaseName, Collection result = new LinkedList<>(); for (RepositoryTuple each : repositoryTuples) { List 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)); } diff --git a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/config/global/GlobalRulePersistService.java b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/config/global/GlobalRulePersistService.java index 3980aee1f150c..0943bccd44246 100644 --- a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/config/global/GlobalRulePersistService.java +++ b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/config/global/GlobalRulePersistService.java @@ -39,8 +39,6 @@ */ public final class GlobalRulePersistService implements GlobalPersistService> { - private static final String DEFAULT_VERSION = "0"; - private final PersistRepository repository; private final MetaDataVersionPersistService metaDataVersionPersistService; @@ -70,10 +68,10 @@ private Collection persistTuples(final Collection result = new LinkedList<>(); for (RepositoryTuple each : repositoryTuples) { List 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)); } diff --git a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/config/global/PropertiesPersistService.java b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/config/global/PropertiesPersistService.java index 6abb939ed97cd..99fcc58039a9c 100644 --- a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/config/global/PropertiesPersistService.java +++ b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/config/global/PropertiesPersistService.java @@ -35,8 +35,6 @@ @RequiredArgsConstructor public final class PropertiesPersistService implements GlobalPersistService { - private static final String DEFAULT_VERSION = "0"; - private final PersistRepository repository; private final MetaDataVersionPersistService metaDataVersionPersistService; @@ -44,10 +42,10 @@ public final class PropertiesPersistService implements GlobalPersistService 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))); diff --git a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/schema/TableMetaDataPersistService.java b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/schema/TableMetaDataPersistService.java index 6feca1c974227..4562f5ea9a590 100644 --- a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/schema/TableMetaDataPersistService.java +++ b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/schema/TableMetaDataPersistService.java @@ -42,8 +42,6 @@ @RequiredArgsConstructor public final class TableMetaDataPersistService implements SchemaMetaDataPersistService> { - private static final String DEFAULT_VERSION = "0"; - private final PersistRepository repository; private final MetaDataVersionPersistService metaDataVersionPersistService; @@ -54,13 +52,13 @@ public void persist(final String databaseName, final String schemaName, final Ma for (Entry entry : tables.entrySet()) { String tableName = entry.getKey().toLowerCase(); List 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)); } diff --git a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/schema/ViewMetaDataPersistService.java b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/schema/ViewMetaDataPersistService.java index ec497b773593e..ad92b615d4fdc 100644 --- a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/schema/ViewMetaDataPersistService.java +++ b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/schema/ViewMetaDataPersistService.java @@ -42,8 +42,6 @@ @RequiredArgsConstructor public final class ViewMetaDataPersistService implements SchemaMetaDataPersistService> { - private static final String DEFAULT_VERSION = "0"; - private final PersistRepository repository; private final MetaDataVersionPersistService metaDataVersionPersistService; @@ -54,11 +52,11 @@ public void persist(final String databaseName, final String schemaName, final Ma for (Entry entry : views.entrySet()) { String viewName = entry.getKey().toLowerCase(); List 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)); }