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

Caideyipi pushed a commit to branch dev/1.3
in repository https://gitbox.apache.org/repos/asf/iotdb.git


The following commit(s) were added to refs/heads/dev/1.3 by this push:
     new 6266332ad6e [To dev/1.3] Fix PathPatternNode wildcard cache lifecycle 
(#18672) (#18688)
6266332ad6e is described below

commit 6266332ad6ec8d36583d6401680f054e8cf5c5a5
Author: Caideyipi <[email protected]>
AuthorDate: Fri Nov 6 09:36:02 2026 +0800

    [To dev/1.3] Fix PathPatternNode wildcard cache lifecycle (#18672) (#18688)
    
    * Fix PathPatternNode wildcard cache lifecycle (#18672)
    
    * Remove unused PathPatternNode.clear
    
    * Update PathPatternNode.java
---
 .../apache/iotdb/commons/path/PathPatternNode.java | 32 +++++++----
 .../apache/iotdb/commons/path/PathPatternUtil.java |  6 +-
 .../iotdb/commons/path/PathPatternNodeTest.java    | 67 ++++++++++++++++++++++
 3 files changed, 92 insertions(+), 13 deletions(-)

diff --git 
a/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/path/PathPatternNode.java
 
b/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/path/PathPatternNode.java
index c7f778e1896..887a570e304 100644
--- 
a/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/path/PathPatternNode.java
+++ 
b/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/path/PathPatternNode.java
@@ -31,7 +31,6 @@ import java.nio.ByteBuffer;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.HashMap;
-import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
 import java.util.Objects;
@@ -39,6 +38,7 @@ import java.util.Set;
 import java.util.function.BiConsumer;
 import java.util.function.Consumer;
 import java.util.function.Supplier;
+import java.util.regex.Pattern;
 
 import static 
org.apache.iotdb.commons.conf.IoTDBConstant.MULTI_LEVEL_PATH_WILDCARD;
 import static 
org.apache.iotdb.commons.conf.IoTDBConstant.ONE_LEVEL_PATH_WILDCARD;
@@ -59,9 +59,9 @@ public class PathPatternNode<V, VSerializer extends 
PathPatternNode.Serializer<V
 
   private final VSerializer serializer;
 
-  // Children names with wildcard, for accelerating wildcard searching.
-  // Here we do not include "*" or "**" to ensure that the set is empty in 
most cases.
-  private final Set<String> childrenNamesWithNonTrivialWildcard = new 
HashSet<>();
+  // Compiled patterns for child names with wildcard, for accelerating 
wildcard searching.
+  // Here we do not include "*" or "**" to ensure that the map is empty most 
of the time.
+  private final Map<String, Pattern> childrenPatternsWithNonTrivialWildcard = 
new HashMap<>();
 
   public PathPatternNode(String name, VSerializer serializer) {
     this.name = name;
@@ -95,10 +95,12 @@ public class PathPatternNode<V, VSerializer extends 
PathPatternNode.Serializer<V
     if (children.containsKey(MULTI_LEVEL_PATH_WILDCARD)) {
       res.add(children.get(MULTI_LEVEL_PATH_WILDCARD));
     }
-    childrenNamesWithNonTrivialWildcard.stream()
-        .filter(path -> PathPatternUtil.isNodeMatch(path, nodeName))
-        .map(children::get)
-        .forEach(res::add);
+    for (final Map.Entry<String, Pattern> entry :
+        childrenPatternsWithNonTrivialWildcard.entrySet()) {
+      if (entry.getValue().matcher(nodeName).matches()) {
+        res.add(children.get(entry.getKey()));
+      }
+    }
     return res;
   }
 
@@ -111,13 +113,16 @@ public class PathPatternNode<V, VSerializer extends 
PathPatternNode.Serializer<V
     if (PathPatternUtil.hasWildcard(nodeName)
         && !PathPatternUtil.isMultiLevelMatchWildcard(nodeName)
         && !ONE_LEVEL_PATH_WILDCARD.equals(nodeName)) {
-      childrenNamesWithNonTrivialWildcard.add(nodeName);
+      childrenPatternsWithNonTrivialWildcard.computeIfAbsent(
+          nodeName, PathPatternUtil::compileNodePattern);
     }
     children.put(nodeName, tmpNode);
   }
 
-  public void deleteChild(PathPatternNode<V, VSerializer> tmpNode) {
-    children.remove(tmpNode.getName());
+  public void deleteChild(final PathPatternNode<V, VSerializer> tmpNode) {
+    final String nodeName = tmpNode.getName();
+    children.remove(nodeName);
+    childrenPatternsWithNonTrivialWildcard.remove(nodeName);
   }
 
   public void appendValue(V value, BiConsumer<V, Set<V>> remappingFunction) {
@@ -280,7 +285,10 @@ public class PathPatternNode<V, VSerializer extends 
PathPatternNode.Serializer<V
     return SHALLOW_SIZE
         + RamUsageEstimator.sizeOf(name)
         + RamUsageEstimator.sizeOfHashSet(valueSet)
-        + RamUsageEstimator.sizeOfHashSet(childrenNamesWithNonTrivialWildcard)
+        + RamUsageEstimator.sizeOfMapWithKnownShallowSize(
+            childrenPatternsWithNonTrivialWildcard,
+            RamUsageEstimator.SHALLOW_SIZE_OF_HASHMAP,
+            RamUsageEstimator.SHALLOW_SIZE_OF_HASHMAP_ENTRY)
         + RamUsageEstimator.sizeOfMapWithKnownShallowSize(
             children,
             RamUsageEstimator.SHALLOW_SIZE_OF_HASHMAP,
diff --git 
a/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/path/PathPatternUtil.java
 
b/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/path/PathPatternUtil.java
index 6ee73645350..23e8d506a12 100644
--- 
a/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/path/PathPatternUtil.java
+++ 
b/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/path/PathPatternUtil.java
@@ -52,6 +52,10 @@ public class PathPatternUtil {
         || patternNode.equals(MULTI_LEVEL_PATH_WILDCARD)) {
       return true;
     }
-    return Pattern.matches(patternNode.replace("*", ".*"), nodeName);
+    return compileNodePattern(patternNode).matcher(nodeName).matches();
+  }
+
+  static Pattern compileNodePattern(final String patternNode) {
+    return Pattern.compile(patternNode.replace("*", ".*"));
   }
 }
diff --git 
a/iotdb-core/node-commons/src/test/java/org/apache/iotdb/commons/path/PathPatternNodeTest.java
 
b/iotdb-core/node-commons/src/test/java/org/apache/iotdb/commons/path/PathPatternNodeTest.java
new file mode 100644
index 00000000000..c0d972dd8d9
--- /dev/null
+++ 
b/iotdb-core/node-commons/src/test/java/org/apache/iotdb/commons/path/PathPatternNodeTest.java
@@ -0,0 +1,67 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.iotdb.commons.path;
+
+import org.apache.iotdb.commons.path.PathPatternNode.VoidSerializer;
+
+import org.junit.Test;
+
+import java.util.List;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertSame;
+import static org.junit.Assert.assertTrue;
+
+public class PathPatternNodeTest {
+
+  @Test
+  public void testNonTrivialWildcardChildCacheOnAddAndDelete() {
+    final PathPatternNode<Void, VoidSerializer> parent = newNode("parent");
+    final PathPatternNode<Void, VoidSerializer> wildcardChild = 
newNode("device*");
+
+    parent.addChild(wildcardChild);
+    final List<PathPatternNode<Void, VoidSerializer>> matchedChildren =
+        parent.getMatchChildren("device1");
+    assertEquals(1, matchedChildren.size());
+    assertSame(wildcardChild, matchedChildren.get(0));
+
+    parent.deleteChild(wildcardChild);
+    assertTrue(parent.getMatchChildren("device1").isEmpty());
+  }
+
+  @Test
+  public void testReplacingNonTrivialWildcardChildKeepsCache() {
+    final PathPatternNode<Void, VoidSerializer> parent = newNode("parent");
+    final PathPatternNode<Void, VoidSerializer> originalChild = 
newNode("device*");
+    final PathPatternNode<Void, VoidSerializer> replacementChild = 
newNode("device*");
+
+    parent.addChild(originalChild);
+    parent.addChild(replacementChild);
+
+    final List<PathPatternNode<Void, VoidSerializer>> matchedChildren =
+        parent.getMatchChildren("device1");
+    assertEquals(1, matchedChildren.size());
+    assertSame(replacementChild, matchedChildren.get(0));
+  }
+
+  private PathPatternNode<Void, VoidSerializer> newNode(final String name) {
+    return new PathPatternNode<>(name, VoidSerializer.getInstance());
+  }
+}

Reply via email to