From 67e2f69bbf926144369954e916b08d69f88d90a2 Mon Sep 17 00:00:00 2001 From: zhangliang Date: Wed, 8 Jan 2025 01:40:50 +0800 Subject: [PATCH 1/2] Refactor DatabaseRuleMetaDataNode --- .../metadata/DatabaseRuleMetaDataNode.java | 74 +++++++++---------- .../database/DatabaseRulePersistService.java | 16 ++-- .../DatabaseRuleMetaDataNodeTest.java | 26 +++++-- 3 files changed, 61 insertions(+), 55 deletions(-) diff --git a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/node/metadata/DatabaseRuleMetaDataNode.java b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/node/metadata/DatabaseRuleMetaDataNode.java index 87912a04965b5..0bf712bbda23a 100644 --- a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/node/metadata/DatabaseRuleMetaDataNode.java +++ b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/node/metadata/DatabaseRuleMetaDataNode.java @@ -26,85 +26,81 @@ @NoArgsConstructor(access = AccessLevel.PRIVATE) public final class DatabaseRuleMetaDataNode { - private static final String ROOT_NODE = "metadata"; + private static final String ROOT_NODE = "/metadata"; private static final String RULE_NODE = "rules"; - private static final String ACTIVE_VERSION = "active_version"; + private static final String VERSIONS_NODE = "versions"; - private static final String VERSIONS = "versions"; + private static final String ACTIVE_VERSION_NODE = "active_version"; /** - * Get database rule active version node. + * Get database root path. * * @param databaseName database name - * @param ruleName rule name - * @param key key - * @return database rule active version node + * @return database root path */ - public static String getDatabaseRuleActiveVersionNode(final String databaseName, final String ruleName, final String key) { - return String.join("/", getDatabaseRuleNode(databaseName, ruleName), key, ACTIVE_VERSION); + public static String getRootPath(final String databaseName) { + return String.join("/", ROOT_NODE, databaseName, RULE_NODE); } /** - * Get database rule versions node. + * Get database rule path. * * @param databaseName database name - * @param ruleName rule name - * @param key key - * @return database rule versions node + * @param ruleTypeName rule type name + * @return database rule path */ - public static String getDatabaseRuleVersionsNode(final String databaseName, final String ruleName, final String key) { - return String.join("/", getDatabaseRuleNode(databaseName, ruleName), key, VERSIONS); + public static String getRulePath(final String databaseName, final String ruleTypeName) { + return String.join("/", getRootPath(databaseName), ruleTypeName); } /** - * Get database rule version node. + * Get database rule path. * * @param databaseName database name - * @param ruleName rule name + * @param ruleTypeName rule type name * @param key key - * @param version version - * @return database rule next version + * @return database rule path without version */ - public static String getDatabaseRuleVersionNode(final String databaseName, final String ruleName, final String key, final String version) { - return String.join("/", getDatabaseRuleNode(databaseName, ruleName), key, VERSIONS, version); + public static String getRulePath(final String databaseName, final String ruleTypeName, final String key) { + return String.join("/", getRulePath(databaseName, ruleTypeName), key); } /** - * Get database rule node. + * Get database rule versions path. * * @param databaseName database name - * @param ruleName rule name + * @param ruleTypeName rule type name * @param key key - * @return database rule node without version + * @return database rule versions path */ - public static String getDatabaseRuleNode(final String databaseName, final String ruleName, final String key) { - return String.join("/", getDatabaseRuleNode(databaseName, ruleName), key); + public static String getVersionsPath(final String databaseName, final String ruleTypeName, final String key) { + return String.join("/", getRulePath(databaseName, ruleTypeName, key), VERSIONS_NODE); } /** - * Get database rule root node. + * Get database rule version path. * * @param databaseName database name - * @param ruleName rule name - * @return database rule root node + * @param ruleTypeName rule type name + * @param key key + * @param version version + * @return database rule next version */ - public static String getDatabaseRuleNode(final String databaseName, final String ruleName) { - return String.join("/", getRulesNode(databaseName), ruleName); + public static String getVersionPath(final String databaseName, final String ruleTypeName, final String key, final String version) { + return String.join("/", getVersionsPath(databaseName, ruleTypeName, key), version); } /** - * Get database rules node. + * Get database rule active version path. * * @param databaseName database name - * @return database rules node + * @param ruleTypeName rule type name + * @param key key + * @return database rule active version path */ - public static String getRulesNode(final String databaseName) { - return String.join("/", getMetaDataNode(), databaseName, RULE_NODE); - } - - private static String getMetaDataNode() { - return String.join("/", "", ROOT_NODE); + public static String getActiveVersionPath(final String databaseName, final String ruleTypeName, final String key) { + return String.join("/", getRulePath(databaseName, ruleTypeName, key), ACTIVE_VERSION_NODE); } } diff --git a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/config/database/DatabaseRulePersistService.java b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/config/database/DatabaseRulePersistService.java index 11ab3859480e8..5c798e7fa70c0 100644 --- a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/config/database/DatabaseRulePersistService.java +++ b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/config/database/DatabaseRulePersistService.java @@ -60,7 +60,7 @@ public DatabaseRulePersistService(final PersistRepository repository) { * @return configurations */ public Collection load(final String databaseName) { - return new RepositoryTupleSwapperEngine().swapToRuleConfigurations(repositoryTuplePersistService.load(DatabaseRuleMetaDataNode.getRulesNode(databaseName))); + return new RepositoryTupleSwapperEngine().swapToRuleConfigurations(repositoryTuplePersistService.load(DatabaseRuleMetaDataNode.getRootPath(databaseName))); } /** @@ -85,19 +85,19 @@ public Collection persist(final String databaseName, final Coll private Collection persistDataNodes(final String databaseName, final String ruleName, final Collection repositoryTuples) { Collection result = new LinkedList<>(); for (RepositoryTuple each : repositoryTuples) { - List versions = metaDataVersionPersistService.getVersions(DatabaseRuleMetaDataNode.getDatabaseRuleVersionsNode(databaseName, ruleName, each.getKey())); + List versions = metaDataVersionPersistService.getVersions(DatabaseRuleMetaDataNode.getVersionsPath(databaseName, ruleName, each.getKey())); 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()); + repository.persist(DatabaseRuleMetaDataNode.getVersionPath(databaseName, ruleName, each.getKey(), nextVersion), each.getValue()); if (Strings.isNullOrEmpty(getActiveVersion(databaseName, ruleName, each.getKey()))) { - repository.persist(DatabaseRuleMetaDataNode.getDatabaseRuleActiveVersionNode(databaseName, ruleName, each.getKey()), MetaDataVersion.DEFAULT_VERSION); + repository.persist(DatabaseRuleMetaDataNode.getActiveVersionPath(databaseName, ruleName, each.getKey()), MetaDataVersion.DEFAULT_VERSION); } - result.add(new MetaDataVersion(DatabaseRuleMetaDataNode.getDatabaseRuleNode(databaseName, ruleName, each.getKey()), getActiveVersion(databaseName, ruleName, each.getKey()), nextVersion)); + result.add(new MetaDataVersion(DatabaseRuleMetaDataNode.getRulePath(databaseName, ruleName, each.getKey()), getActiveVersion(databaseName, ruleName, each.getKey()), nextVersion)); } return result; } private String getActiveVersion(final String databaseName, final String ruleName, final String key) { - return repository.query(DatabaseRuleMetaDataNode.getDatabaseRuleActiveVersionNode(databaseName, ruleName, key)); + return repository.query(DatabaseRuleMetaDataNode.getActiveVersionPath(databaseName, ruleName, key)); } /** @@ -107,7 +107,7 @@ private String getActiveVersion(final String databaseName, final String ruleName * @param ruleTypeName rule type name */ public void delete(final String databaseName, final String ruleTypeName) { - repository.delete(DatabaseRuleMetaDataNode.getDatabaseRuleNode(databaseName, ruleTypeName)); + repository.delete(DatabaseRuleMetaDataNode.getRulePath(databaseName, ruleTypeName)); } /** @@ -135,7 +135,7 @@ public Collection delete(final String databaseName, final Colle private Collection delete(final String databaseName, final String ruleName, final Collection repositoryTuples) { Collection result = new LinkedList<>(); for (RepositoryTuple each : repositoryTuples) { - String toBeDeletedKey = DatabaseRuleMetaDataNode.getDatabaseRuleNode(databaseName, ruleName, each.getKey()); + String toBeDeletedKey = DatabaseRuleMetaDataNode.getRulePath(databaseName, ruleName, each.getKey()); repository.delete(toBeDeletedKey); result.add(new MetaDataVersion(toBeDeletedKey)); } diff --git a/kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/node/metadata/DatabaseRuleMetaDataNodeTest.java b/kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/node/metadata/DatabaseRuleMetaDataNodeTest.java index 0b1dd0f401761..6ebac937ed684 100644 --- a/kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/node/metadata/DatabaseRuleMetaDataNodeTest.java +++ b/kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/node/metadata/DatabaseRuleMetaDataNodeTest.java @@ -24,22 +24,32 @@ class DatabaseRuleMetaDataNodeTest { @Test - void assertGetDatabaseRuleActiveVersionNode() { - assertThat(DatabaseRuleMetaDataNode.getDatabaseRuleActiveVersionNode("foo_db", "foo_rule", "foo_tbl"), is("/metadata/foo_db/rules/foo_rule/foo_tbl/active_version")); + void assertGetRootPath() { + assertThat(DatabaseRuleMetaDataNode.getRootPath("foo_db"), is("/metadata/foo_db/rules")); } @Test - void assertGetDatabaseRuleVersionsNode() { - assertThat(DatabaseRuleMetaDataNode.getDatabaseRuleVersionsNode("foo_db", "sharding", "foo_key"), is("/metadata/foo_db/rules/sharding/foo_key/versions")); + void assertGetRulePath() { + assertThat(DatabaseRuleMetaDataNode.getRulePath("foo_db", "foo_rule"), is("/metadata/foo_db/rules/foo_rule")); } @Test - void assertGetDatabaseRuleVersionNode() { - assertThat(DatabaseRuleMetaDataNode.getDatabaseRuleVersionNode("foo_db", "foo_rule", "foo_tbl", "1"), is("/metadata/foo_db/rules/foo_rule/foo_tbl/versions/1")); + void assertGetRulePathWithKey() { + assertThat(DatabaseRuleMetaDataNode.getRulePath("foo_db", "foo_rule", "sharding"), is("/metadata/foo_db/rules/foo_rule/sharding")); } @Test - void assertGetDatabaseRuleNode() { - assertThat(DatabaseRuleMetaDataNode.getDatabaseRuleNode("foo_db", "foo_rule", "sharding"), is("/metadata/foo_db/rules/foo_rule/sharding")); + void assertGetVersionsPath() { + assertThat(DatabaseRuleMetaDataNode.getVersionsPath("foo_db", "sharding", "foo_key"), is("/metadata/foo_db/rules/sharding/foo_key/versions")); + } + + @Test + void assertGetVersionPath() { + assertThat(DatabaseRuleMetaDataNode.getVersionPath("foo_db", "foo_rule", "foo_tbl", "1"), is("/metadata/foo_db/rules/foo_rule/foo_tbl/versions/1")); + } + + @Test + void assertGetActiveVersionPath() { + assertThat(DatabaseRuleMetaDataNode.getActiveVersionPath("foo_db", "foo_rule", "foo_tbl"), is("/metadata/foo_db/rules/foo_rule/foo_tbl/active_version")); } } From b5dc6405d32efc2447843c483ebf916818a4d24d Mon Sep 17 00:00:00 2001 From: zhangliang Date: Wed, 8 Jan 2025 01:42:23 +0800 Subject: [PATCH 2/2] Refactor DatabaseRuleMetaDataNode --- ....java => DatabaseRuleMetaDataNodePath.java} | 4 ++-- .../database/DatabaseRulePersistService.java | 18 +++++++++--------- ...a => DatabaseRuleMetaDataNodePathTest.java} | 14 +++++++------- 3 files changed, 18 insertions(+), 18 deletions(-) rename kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/node/metadata/{DatabaseRuleMetaDataNode.java => DatabaseRuleMetaDataNodePath.java} (97%) rename kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/node/metadata/{DatabaseRuleMetaDataNodeTest.java => DatabaseRuleMetaDataNodePathTest.java} (60%) diff --git a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/node/metadata/DatabaseRuleMetaDataNode.java b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/node/metadata/DatabaseRuleMetaDataNodePath.java similarity index 97% rename from kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/node/metadata/DatabaseRuleMetaDataNode.java rename to kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/node/metadata/DatabaseRuleMetaDataNodePath.java index 0bf712bbda23a..1daa0ea87d1df 100644 --- a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/node/metadata/DatabaseRuleMetaDataNode.java +++ b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/node/metadata/DatabaseRuleMetaDataNodePath.java @@ -21,10 +21,10 @@ import lombok.NoArgsConstructor; /** - * Database rule meta data node. + * Database rule meta data node path. */ @NoArgsConstructor(access = AccessLevel.PRIVATE) -public final class DatabaseRuleMetaDataNode { +public final class DatabaseRuleMetaDataNodePath { private static final String ROOT_NODE = "/metadata"; diff --git a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/config/database/DatabaseRulePersistService.java b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/config/database/DatabaseRulePersistService.java index 5c798e7fa70c0..464ad354eda52 100644 --- a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/config/database/DatabaseRulePersistService.java +++ b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/config/database/DatabaseRulePersistService.java @@ -22,7 +22,7 @@ import org.apache.shardingsphere.infra.metadata.version.MetaDataVersion; import org.apache.shardingsphere.infra.yaml.config.pojo.rule.YamlRuleConfiguration; import org.apache.shardingsphere.infra.yaml.config.swapper.rule.YamlRuleConfigurationSwapperEngine; -import org.apache.shardingsphere.metadata.persist.node.metadata.DatabaseRuleMetaDataNode; +import org.apache.shardingsphere.metadata.persist.node.metadata.DatabaseRuleMetaDataNodePath; import org.apache.shardingsphere.metadata.persist.service.config.RepositoryTuplePersistService; import org.apache.shardingsphere.metadata.persist.service.version.MetaDataVersionPersistService; import org.apache.shardingsphere.mode.spi.PersistRepository; @@ -60,7 +60,7 @@ public DatabaseRulePersistService(final PersistRepository repository) { * @return configurations */ public Collection load(final String databaseName) { - return new RepositoryTupleSwapperEngine().swapToRuleConfigurations(repositoryTuplePersistService.load(DatabaseRuleMetaDataNode.getRootPath(databaseName))); + return new RepositoryTupleSwapperEngine().swapToRuleConfigurations(repositoryTuplePersistService.load(DatabaseRuleMetaDataNodePath.getRootPath(databaseName))); } /** @@ -85,19 +85,19 @@ public Collection persist(final String databaseName, final Coll private Collection persistDataNodes(final String databaseName, final String ruleName, final Collection repositoryTuples) { Collection result = new LinkedList<>(); for (RepositoryTuple each : repositoryTuples) { - List versions = metaDataVersionPersistService.getVersions(DatabaseRuleMetaDataNode.getVersionsPath(databaseName, ruleName, each.getKey())); + List versions = metaDataVersionPersistService.getVersions(DatabaseRuleMetaDataNodePath.getVersionsPath(databaseName, ruleName, each.getKey())); String nextVersion = versions.isEmpty() ? MetaDataVersion.DEFAULT_VERSION : String.valueOf(Integer.parseInt(versions.get(0)) + 1); - repository.persist(DatabaseRuleMetaDataNode.getVersionPath(databaseName, ruleName, each.getKey(), nextVersion), each.getValue()); + repository.persist(DatabaseRuleMetaDataNodePath.getVersionPath(databaseName, ruleName, each.getKey(), nextVersion), each.getValue()); if (Strings.isNullOrEmpty(getActiveVersion(databaseName, ruleName, each.getKey()))) { - repository.persist(DatabaseRuleMetaDataNode.getActiveVersionPath(databaseName, ruleName, each.getKey()), MetaDataVersion.DEFAULT_VERSION); + repository.persist(DatabaseRuleMetaDataNodePath.getActiveVersionPath(databaseName, ruleName, each.getKey()), MetaDataVersion.DEFAULT_VERSION); } - result.add(new MetaDataVersion(DatabaseRuleMetaDataNode.getRulePath(databaseName, ruleName, each.getKey()), getActiveVersion(databaseName, ruleName, each.getKey()), nextVersion)); + result.add(new MetaDataVersion(DatabaseRuleMetaDataNodePath.getRulePath(databaseName, ruleName, each.getKey()), getActiveVersion(databaseName, ruleName, each.getKey()), nextVersion)); } return result; } private String getActiveVersion(final String databaseName, final String ruleName, final String key) { - return repository.query(DatabaseRuleMetaDataNode.getActiveVersionPath(databaseName, ruleName, key)); + return repository.query(DatabaseRuleMetaDataNodePath.getActiveVersionPath(databaseName, ruleName, key)); } /** @@ -107,7 +107,7 @@ private String getActiveVersion(final String databaseName, final String ruleName * @param ruleTypeName rule type name */ public void delete(final String databaseName, final String ruleTypeName) { - repository.delete(DatabaseRuleMetaDataNode.getRulePath(databaseName, ruleTypeName)); + repository.delete(DatabaseRuleMetaDataNodePath.getRulePath(databaseName, ruleTypeName)); } /** @@ -135,7 +135,7 @@ public Collection delete(final String databaseName, final Colle private Collection delete(final String databaseName, final String ruleName, final Collection repositoryTuples) { Collection result = new LinkedList<>(); for (RepositoryTuple each : repositoryTuples) { - String toBeDeletedKey = DatabaseRuleMetaDataNode.getRulePath(databaseName, ruleName, each.getKey()); + String toBeDeletedKey = DatabaseRuleMetaDataNodePath.getRulePath(databaseName, ruleName, each.getKey()); repository.delete(toBeDeletedKey); result.add(new MetaDataVersion(toBeDeletedKey)); } diff --git a/kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/node/metadata/DatabaseRuleMetaDataNodeTest.java b/kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/node/metadata/DatabaseRuleMetaDataNodePathTest.java similarity index 60% rename from kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/node/metadata/DatabaseRuleMetaDataNodeTest.java rename to kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/node/metadata/DatabaseRuleMetaDataNodePathTest.java index 6ebac937ed684..8afb5d95e8f80 100644 --- a/kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/node/metadata/DatabaseRuleMetaDataNodeTest.java +++ b/kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/node/metadata/DatabaseRuleMetaDataNodePathTest.java @@ -21,35 +21,35 @@ import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; -class DatabaseRuleMetaDataNodeTest { +class DatabaseRuleMetaDataNodePathTest { @Test void assertGetRootPath() { - assertThat(DatabaseRuleMetaDataNode.getRootPath("foo_db"), is("/metadata/foo_db/rules")); + assertThat(DatabaseRuleMetaDataNodePath.getRootPath("foo_db"), is("/metadata/foo_db/rules")); } @Test void assertGetRulePath() { - assertThat(DatabaseRuleMetaDataNode.getRulePath("foo_db", "foo_rule"), is("/metadata/foo_db/rules/foo_rule")); + assertThat(DatabaseRuleMetaDataNodePath.getRulePath("foo_db", "foo_rule"), is("/metadata/foo_db/rules/foo_rule")); } @Test void assertGetRulePathWithKey() { - assertThat(DatabaseRuleMetaDataNode.getRulePath("foo_db", "foo_rule", "sharding"), is("/metadata/foo_db/rules/foo_rule/sharding")); + assertThat(DatabaseRuleMetaDataNodePath.getRulePath("foo_db", "foo_rule", "sharding"), is("/metadata/foo_db/rules/foo_rule/sharding")); } @Test void assertGetVersionsPath() { - assertThat(DatabaseRuleMetaDataNode.getVersionsPath("foo_db", "sharding", "foo_key"), is("/metadata/foo_db/rules/sharding/foo_key/versions")); + assertThat(DatabaseRuleMetaDataNodePath.getVersionsPath("foo_db", "sharding", "foo_key"), is("/metadata/foo_db/rules/sharding/foo_key/versions")); } @Test void assertGetVersionPath() { - assertThat(DatabaseRuleMetaDataNode.getVersionPath("foo_db", "foo_rule", "foo_tbl", "1"), is("/metadata/foo_db/rules/foo_rule/foo_tbl/versions/1")); + assertThat(DatabaseRuleMetaDataNodePath.getVersionPath("foo_db", "foo_rule", "foo_tbl", "1"), is("/metadata/foo_db/rules/foo_rule/foo_tbl/versions/1")); } @Test void assertGetActiveVersionPath() { - assertThat(DatabaseRuleMetaDataNode.getActiveVersionPath("foo_db", "foo_rule", "foo_tbl"), is("/metadata/foo_db/rules/foo_rule/foo_tbl/active_version")); + assertThat(DatabaseRuleMetaDataNodePath.getActiveVersionPath("foo_db", "foo_rule", "foo_tbl"), is("/metadata/foo_db/rules/foo_rule/foo_tbl/active_version")); } }