This is an automated email from the ASF dual-hosted git repository.

zhangliang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git


The following commit(s) were added to refs/heads/master by this push:
     new b9806710d94 Refactor UniqueRuleItemNodePath and NamedRuleItemNodePath 
(#34596)
b9806710d94 is described below

commit b9806710d94044f55b167c48baa995ad54841666
Author: Liang Zhang <zhangli...@apache.org>
AuthorDate: Fri Feb 7 23:42:33 2025 +0800

    Refactor UniqueRuleItemNodePath and NamedRuleItemNodePath (#34596)
    
    * Refactor UniqueRuleItemNodePath and NamedRuleItemNodePath
    
    * Refactor UniqueRuleItemNodePath and NamedRuleItemNodePath
    
    * Refactor UniqueRuleItemNodePath and NamedRuleItemNodePath
---
 .../type/RuleItemAlteredBuildExecutor.java         |  9 +++--
 .../type/RuleItemDroppedBuildExecutor.java         |  2 +-
 .../config/rule/item/NamedRuleItemNodePath.java    | 22 ++-----------
 .../config/rule/item/UniqueRuleItemNodePath.java   | 38 +++-------------------
 .../tuple/YamlRepositoryTupleSwapperEngine.java    |  9 ++---
 .../node/path/config/rule/RuleNodePathTest.java    |  7 ----
 .../rule/item/NamedRuleItemNodePathTest.java       | 25 --------------
 .../rule/item/UniqueRuleItemNodePathTest.java      | 29 -----------------
 8 files changed, 17 insertions(+), 124 deletions(-)

diff --git 
a/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/changed/executor/type/RuleItemAlteredBuildExecutor.java
 
b/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/changed/executor/type/RuleItemAlteredBuildExecutor.java
index 171c93f22a6..f72cdbe4549 100644
--- 
a/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/changed/executor/type/RuleItemAlteredBuildExecutor.java
+++ 
b/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/changed/executor/type/RuleItemAlteredBuildExecutor.java
@@ -37,15 +37,14 @@ public final class RuleItemAlteredBuildExecutor implements 
RuleItemChangedBuildE
     @Override
     public Optional<AlterRuleItem> build(final RuleNodePath ruleNodePath, 
final String databaseName, final MetaDataVersion metaDataVersion) {
         for (Entry<String, NamedRuleItemNodePath> entry : 
ruleNodePath.getNamedItems().entrySet()) {
-            Optional<String> itemName = 
entry.getValue().findNameByActiveVersion(metaDataVersion.getPath());
+            Optional<String> itemName = 
entry.getValue().getVersionNodePath().findIdentifierByActiveVersionPath(metaDataVersion.getPath(),
 1);
             if (itemName.isPresent()) {
-                return Optional.of(
-                        new AlterNamedRuleItem(databaseName, itemName.get(), 
metaDataVersion.getPath(),
-                                metaDataVersion.getNextActiveVersion(), 
ruleNodePath.getRoot().getRuleType() + "." + entry.getKey()));
+                return Optional.of(new AlterNamedRuleItem(databaseName, 
itemName.get(), metaDataVersion.getPath(),
+                        metaDataVersion.getNextActiveVersion(), 
ruleNodePath.getRoot().getRuleType() + "." + entry.getKey()));
             }
         }
         for (Entry<String, UniqueRuleItemNodePath> entry : 
ruleNodePath.getUniqueItems().entrySet()) {
-            if 
(entry.getValue().isActiveVersionPath(metaDataVersion.getPath())) {
+            if 
(entry.getValue().getVersionNodePath().isActiveVersionPath(metaDataVersion.getPath()))
 {
                 return Optional.of(new AlterUniqueRuleItem(databaseName, 
metaDataVersion.getPath(),
                         metaDataVersion.getNextActiveVersion(), 
ruleNodePath.getRoot().getRuleType() + "." + entry.getKey()));
             }
diff --git 
a/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/changed/executor/type/RuleItemDroppedBuildExecutor.java
 
b/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/changed/executor/type/RuleItemDroppedBuildExecutor.java
index dc7b583fc03..fc84e18c362 100644
--- 
a/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/changed/executor/type/RuleItemDroppedBuildExecutor.java
+++ 
b/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/changed/executor/type/RuleItemDroppedBuildExecutor.java
@@ -43,7 +43,7 @@ public final class RuleItemDroppedBuildExecutor implements 
RuleItemChangedBuildE
             }
         }
         for (Entry<String, UniqueRuleItemNodePath> entry : 
ruleNodePath.getUniqueItems().entrySet()) {
-            if 
(entry.getValue().isActiveVersionPath(metaDataVersion.getPath())) {
+            if 
(entry.getValue().getVersionNodePath().isActiveVersionPath(metaDataVersion.getPath()))
 {
                 return Optional.of(new DropUniqueRuleItem(databaseName, 
ruleNodePath.getRoot().getRuleType() + "." + entry.getKey()));
             }
         }
diff --git 
a/mode/node/src/main/java/org/apache/shardingsphere/mode/node/path/config/rule/item/NamedRuleItemNodePath.java
 
b/mode/node/src/main/java/org/apache/shardingsphere/mode/node/path/config/rule/item/NamedRuleItemNodePath.java
index 7dd6162a3da..9d92e924657 100644
--- 
a/mode/node/src/main/java/org/apache/shardingsphere/mode/node/path/config/rule/item/NamedRuleItemNodePath.java
+++ 
b/mode/node/src/main/java/org/apache/shardingsphere/mode/node/path/config/rule/item/NamedRuleItemNodePath.java
@@ -17,6 +17,7 @@
 
 package org.apache.shardingsphere.mode.node.path.config.rule.item;
 
+import lombok.Getter;
 import org.apache.shardingsphere.mode.node.path.NodePathPattern;
 import 
org.apache.shardingsphere.mode.node.path.config.rule.root.RuleRootNodePath;
 import org.apache.shardingsphere.mode.node.path.version.VersionNodePath;
@@ -34,6 +35,7 @@ public final class NamedRuleItemNodePath {
     
     private final Pattern itemPathPattern;
     
+    @Getter
     private final VersionNodePath versionNodePath;
     
     public NamedRuleItemNodePath(final RuleRootNodePath rootNodePath, final 
String type) {
@@ -53,26 +55,6 @@ public final class NamedRuleItemNodePath {
         return String.join("/", type, itemName);
     }
     
-    /**
-     * Find rule item name by version.
-     *
-     * @param versionPath version path
-     * @return found item rule name
-     */
-    public Optional<String> findNameByVersion(final String versionPath) {
-        return versionNodePath.findIdentifierByVersionsPath(versionPath, 1);
-    }
-    
-    /**
-     * Find rule item name by active version.
-     *
-     * @param activeVersionPath active version path
-     * @return found rule item name
-     */
-    public Optional<String> findNameByActiveVersion(final String 
activeVersionPath) {
-        return 
versionNodePath.findIdentifierByActiveVersionPath(activeVersionPath, 1);
-    }
-    
     /**
      * Find rule item name by item path.
      *
diff --git 
a/mode/node/src/main/java/org/apache/shardingsphere/mode/node/path/config/rule/item/UniqueRuleItemNodePath.java
 
b/mode/node/src/main/java/org/apache/shardingsphere/mode/node/path/config/rule/item/UniqueRuleItemNodePath.java
index 9736356ea3e..fccb514d98d 100644
--- 
a/mode/node/src/main/java/org/apache/shardingsphere/mode/node/path/config/rule/item/UniqueRuleItemNodePath.java
+++ 
b/mode/node/src/main/java/org/apache/shardingsphere/mode/node/path/config/rule/item/UniqueRuleItemNodePath.java
@@ -17,12 +17,10 @@
 
 package org.apache.shardingsphere.mode.node.path.config.rule.item;
 
+import lombok.Getter;
 import 
org.apache.shardingsphere.mode.node.path.config.rule.root.RuleRootNodePath;
 import org.apache.shardingsphere.mode.node.path.version.VersionNodePath;
 
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
-
 /**
  * Unique rule item node path.
  */
@@ -32,24 +30,19 @@ public final class UniqueRuleItemNodePath {
     
     private final String type;
     
-    private final Pattern pathPattern;
-    
-    private final Pattern activeVersionPathPattern;
+    @Getter
+    private final VersionNodePath versionNodePath;
     
     public UniqueRuleItemNodePath(final RuleRootNodePath ruleRootNodePath, 
final String type) {
         parentNode = null;
         this.type = type;
-        VersionNodePath versionNodePath = new 
VersionNodePath(ruleRootNodePath.getNodePrefix() + type);
-        pathPattern = Pattern.compile(String.join("/", 
versionNodePath.getVersionsPath(), VersionNodePath.VERSION_PATTERN));
-        activeVersionPathPattern = 
Pattern.compile(versionNodePath.getActiveVersionPath() + "$");
+        versionNodePath = new VersionNodePath(ruleRootNodePath.getNodePrefix() 
+ type);
     }
     
     public UniqueRuleItemNodePath(final RuleRootNodePath ruleRootNodePath, 
final String parentNode, final String type) {
         this.parentNode = parentNode;
         this.type = type;
-        VersionNodePath versionNodePath = new 
VersionNodePath(ruleRootNodePath.getNodePrefix() + parentNode + "/" + type);
-        pathPattern = Pattern.compile(String.join("/", 
versionNodePath.getVersionsPath(), VersionNodePath.VERSION_PATTERN));
-        activeVersionPathPattern = 
Pattern.compile(versionNodePath.getActiveVersionPath() + "$");
+        versionNodePath = new VersionNodePath(ruleRootNodePath.getNodePrefix() 
+ parentNode + "/" + type);
     }
     
     /**
@@ -60,25 +53,4 @@ public final class UniqueRuleItemNodePath {
     public String getPath() {
         return null == parentNode ? String.join("/", type) : String.join("/", 
parentNode, type);
     }
-    
-    /**
-     * Judge whether is validated rule item path.
-     *
-     * @param path path to be judged
-     * @return is validated rule item path or not
-     */
-    public boolean isValidatedPath(final String path) {
-        return pathPattern.matcher(path).find();
-    }
-    
-    /**
-     * Judge whether active version path.
-     *
-     * @param path path to be judged
-     * @return is active version path or not
-     */
-    public boolean isActiveVersionPath(final String path) {
-        Matcher matcher = activeVersionPathPattern.matcher(path);
-        return matcher.find();
-    }
 }
diff --git 
a/mode/node/src/main/java/org/apache/shardingsphere/mode/node/tuple/YamlRepositoryTupleSwapperEngine.java
 
b/mode/node/src/main/java/org/apache/shardingsphere/mode/node/tuple/YamlRepositoryTupleSwapperEngine.java
index 878b73542f8..07a7a4eb319 100644
--- 
a/mode/node/src/main/java/org/apache/shardingsphere/mode/node/tuple/YamlRepositoryTupleSwapperEngine.java
+++ 
b/mode/node/src/main/java/org/apache/shardingsphere/mode/node/tuple/YamlRepositoryTupleSwapperEngine.java
@@ -162,7 +162,7 @@ public final class YamlRepositoryTupleSwapperEngine {
         YamlRuleConfiguration yamlRuleConfig = 
toBeSwappedType.getConstructor().newInstance();
         RuleNodePath ruleNodePath = 
TypedSPILoader.getService(RuleNodePathProvider.class, 
yamlRuleConfig.getRuleConfigurationType()).getRuleNodePath();
         for (RepositoryTuple each : repositoryTuples.stream().filter(each -> 
ruleNodePath.getRoot().isValidatedPath(each.getKey())).collect(Collectors.toList()))
 {
-            if 
(ruleNodePath.getUniqueItem(tupleEntity.value()).isValidatedPath(each.getKey()))
 {
+            if 
(ruleNodePath.getUniqueItem(tupleEntity.value()).getVersionNodePath().isVersionPath(each.getKey()))
 {
                 return Optional.of(YamlEngine.unmarshal(each.getValue(), 
toBeSwappedType));
             }
         }
@@ -202,16 +202,17 @@ public final class YamlRepositoryTupleSwapperEngine {
         String tupleName = getTupleName(field);
         RepositoryTupleKeyListNameGenerator tupleKeyListNameGenerator = 
field.getAnnotation(RepositoryTupleKeyListNameGenerator.class);
         if (null != tupleKeyListNameGenerator && fieldValue instanceof 
Collection) {
-            
ruleNodePath.getNamedItem(tupleName).findNameByVersion(repositoryTuple.getKey()).ifPresent(optional
 -> ((Collection) fieldValue).add(repositoryTuple.getValue()));
+            ruleNodePath.getNamedItem(tupleName).getVersionNodePath()
+                    .findIdentifierByVersionsPath(repositoryTuple.getKey(), 
1).ifPresent(optional -> ((Collection) 
fieldValue).add(repositoryTuple.getValue()));
             return;
         }
         if (fieldValue instanceof Map) {
             Class<?> valueClass = (Class) ((ParameterizedType) 
field.getGenericType()).getActualTypeArguments()[1];
-            
ruleNodePath.getNamedItem(tupleName).findNameByVersion(repositoryTuple.getKey())
+            
ruleNodePath.getNamedItem(tupleName).getVersionNodePath().findIdentifierByVersionsPath(repositoryTuple.getKey(),
 1)
                     .ifPresent(optional -> ((Map) fieldValue).put(optional, 
YamlEngine.unmarshal(repositoryTuple.getValue(), valueClass)));
             return;
         }
-        if 
(!ruleNodePath.getUniqueItem(tupleName).isValidatedPath(repositoryTuple.getKey()))
 {
+        if 
(!ruleNodePath.getUniqueItem(tupleName).getVersionNodePath().isVersionPath(repositoryTuple.getKey()))
 {
             return;
         }
         if (fieldValue instanceof Collection) {
diff --git 
a/mode/node/src/test/java/org/apache/shardingsphere/mode/node/path/config/rule/RuleNodePathTest.java
 
b/mode/node/src/test/java/org/apache/shardingsphere/mode/node/path/config/rule/RuleNodePathTest.java
index 8528985522c..f2214f10f85 100644
--- 
a/mode/node/src/test/java/org/apache/shardingsphere/mode/node/path/config/rule/RuleNodePathTest.java
+++ 
b/mode/node/src/test/java/org/apache/shardingsphere/mode/node/path/config/rule/RuleNodePathTest.java
@@ -25,11 +25,9 @@ import org.junit.jupiter.api.Test;
 import java.util.Arrays;
 import java.util.Collections;
 import java.util.List;
-import java.util.Optional;
 
 import static org.hamcrest.CoreMatchers.is;
 import static org.hamcrest.MatcherAssert.assertThat;
-import static org.junit.jupiter.api.Assertions.assertTrue;
 
 class RuleNodePathTest {
     
@@ -46,18 +44,13 @@ class RuleNodePathTest {
     void assertFindNameByVersion() {
         NamedRuleItemNodePath namedRulePath = 
ruleNodePath.getNamedItem("tables");
         assertThat(namedRulePath.getPath("foo_tbl"), is("tables/foo_tbl"));
-        Optional<String> path = 
namedRulePath.findNameByVersion("/metadata/foo_db/rules/foo/tables/foo_tbl/versions/0");
-        assertTrue(path.isPresent());
-        assertThat(path.get(), is("foo_tbl"));
     }
     
     @Test
     void assertGetUniqueItem() {
         UniqueRuleItemNodePath uniqueRulePath = 
ruleNodePath.getUniqueItem("tables");
         assertThat(uniqueRulePath.getPath(), is("tables"));
-        
assertTrue(uniqueRulePath.isValidatedPath("/metadata/db/rules/foo/tables/versions/1234"));
         UniqueRuleItemNodePath uniqueRulePathWithType = 
ruleNodePath.getUniqueItem("type");
         assertThat(uniqueRulePathWithType.getPath(), is("tables/type"));
-        
assertTrue(uniqueRulePathWithType.isActiveVersionPath("/metadata/db/rules/foo/tables/type/active_version"));
     }
 }
diff --git 
a/mode/node/src/test/java/org/apache/shardingsphere/mode/node/path/config/rule/item/NamedRuleItemNodePathTest.java
 
b/mode/node/src/test/java/org/apache/shardingsphere/mode/node/path/config/rule/item/NamedRuleItemNodePathTest.java
index eb348392a46..9e469c42da8 100644
--- 
a/mode/node/src/test/java/org/apache/shardingsphere/mode/node/path/config/rule/item/NamedRuleItemNodePathTest.java
+++ 
b/mode/node/src/test/java/org/apache/shardingsphere/mode/node/path/config/rule/item/NamedRuleItemNodePathTest.java
@@ -36,31 +36,6 @@ class NamedRuleItemNodePathTest {
         assertThat(nodePath.getPath("foo_tbl"), is("tables/foo_tbl"));
     }
     
-    @Test
-    void assertFindNameByVersion() {
-        Optional<String> actual = 
nodePath.findNameByVersion("/metadata/foo_db/rules/foo/tables/foo_tbl/versions/0");
-        assertTrue(actual.isPresent());
-        assertThat(actual.get(), is("foo_tbl"));
-    }
-    
-    @Test
-    void assertFindNameByVersionWithInvalidPath() {
-        
assertFalse(nodePath.findNameByVersion("/metadata/foo_db/rules/bar/tables/foo_tbl/versions/0").isPresent());
-    }
-    
-    @Test
-    void assertFindNameByActiveVersion() {
-        Optional<String> actual = 
nodePath.findNameByActiveVersion("/metadata/foo_db/rules/foo/tables/foo_tbl/active_version");
-        assertTrue(actual.isPresent());
-        assertThat(actual.get(), is("foo_tbl"));
-    }
-    
-    @Test
-    void assertFindNameByActiveVersionWhenNotFound() {
-        Optional<String> actual = nodePath.findNameByActiveVersion("/invalid");
-        assertFalse(actual.isPresent());
-    }
-    
     @Test
     void assertFindNameByItemPath() {
         Optional<String> actual = 
nodePath.findNameByItemPath("/metadata/foo_db/rules/foo/tables/foo_tbl");
diff --git 
a/mode/node/src/test/java/org/apache/shardingsphere/mode/node/path/config/rule/item/UniqueRuleItemNodePathTest.java
 
b/mode/node/src/test/java/org/apache/shardingsphere/mode/node/path/config/rule/item/UniqueRuleItemNodePathTest.java
index 5d1a61d0e8c..0921294a0ce 100644
--- 
a/mode/node/src/test/java/org/apache/shardingsphere/mode/node/path/config/rule/item/UniqueRuleItemNodePathTest.java
+++ 
b/mode/node/src/test/java/org/apache/shardingsphere/mode/node/path/config/rule/item/UniqueRuleItemNodePathTest.java
@@ -22,8 +22,6 @@ import org.junit.jupiter.api.Test;
 
 import static org.hamcrest.CoreMatchers.is;
 import static org.hamcrest.MatcherAssert.assertThat;
-import static org.junit.jupiter.api.Assertions.assertTrue;
-import static org.junit.jupiter.api.Assertions.assertFalse;
 
 class UniqueRuleItemNodePathTest {
     
@@ -38,31 +36,4 @@ class UniqueRuleItemNodePathTest {
         UniqueRuleItemNodePath uniqueRuleItemNodePath = new 
UniqueRuleItemNodePath(new RuleRootNodePath("foo"), "test_parent", "test_path");
         assertThat(uniqueRuleItemNodePath.getPath(), 
is("test_parent/test_path"));
     }
-    
-    @Test
-    void assertIsValidPathWithNullParentNode() {
-        UniqueRuleItemNodePath uniqueRuleItemNodePath = new 
UniqueRuleItemNodePath(new RuleRootNodePath("foo"), "test_path");
-        
assertTrue(uniqueRuleItemNodePath.isValidatedPath("/word1/word2-/rules/foo/test_path/versions/1234"));
-    }
-    
-    @Test
-    void assertIsNotValidPathWithNullParentNode() {
-        UniqueRuleItemNodePath uniqueRuleItemNodePath = new 
UniqueRuleItemNodePath(new RuleRootNodePath("foo"), "test_path");
-        
assertFalse(uniqueRuleItemNodePath.isValidatedPath("/word1/word2/rules/test_foo/test_path/versions/1234"));
-        
assertFalse(uniqueRuleItemNodePath.isValidatedPath("/rules/test_foo/test/versions/1234"));
-        
assertFalse(uniqueRuleItemNodePath.isValidatedPath("/word1/word2/rules/foo/test_path/versions/"));
-    }
-    
-    @Test
-    void assertIsActiveVersionPath() {
-        UniqueRuleItemNodePath uniqueRuleItemNodePath = new 
UniqueRuleItemNodePath(new RuleRootNodePath("foo"), "test_path");
-        
assertTrue(uniqueRuleItemNodePath.isActiveVersionPath("/word1-/word2/rules/foo/test_path/active_version"));
-    }
-    
-    @Test
-    void assertIsNotActiveVersionPath() {
-        UniqueRuleItemNodePath uniqueRuleItemNodePath = new 
UniqueRuleItemNodePath(new RuleRootNodePath("foo"), "test_path");
-        
assertFalse(uniqueRuleItemNodePath.isActiveVersionPath("/word1/word2/rules/foo/test_path/active_version1"));
-        
assertFalse(uniqueRuleItemNodePath.isActiveVersionPath("/rules/foo/test_path/active_version"));
-    }
 }

Reply via email to