3838import java .util .function .BiConsumer ;
3939import java .util .function .Consumer ;
4040import java .util .function .Supplier ;
41+ import java .util .regex .Pattern ;
4142
4243import static org .apache .iotdb .commons .conf .IoTDBConstant .MULTI_LEVEL_PATH_WILDCARD ;
4344import static org .apache .iotdb .commons .conf .IoTDBConstant .ONE_LEVEL_PATH_WILDCARD ;
@@ -58,9 +59,9 @@ public class PathPatternNode<V, VSerializer extends PathPatternNode.Serializer<V
5859
5960 private final VSerializer serializer ;
6061
61- // Children names with wildcard, for accelerating wildcard searching.
62- // Here we do not include "*" or "**" to ensure that the set is empty in most cases .
63- private final Set <String > childrenNamesWithNonTrivialWildcard = new HashSet <>();
62+ // Compiled patterns for child names with wildcard, for accelerating wildcard searching.
63+ // Here we do not include "*" or "**" to ensure that the map is empty most of the time .
64+ private final Map <String , Pattern > childrenPatternsWithNonTrivialWildcard = new HashMap <>();
6465
6566 public PathPatternNode (String name , VSerializer serializer ) {
6667 this .name = name ;
@@ -94,10 +95,12 @@ public List<PathPatternNode<V, VSerializer>> getMatchChildren(String nodeName) {
9495 if (children .containsKey (MULTI_LEVEL_PATH_WILDCARD )) {
9596 res .add (children .get (MULTI_LEVEL_PATH_WILDCARD ));
9697 }
97- childrenNamesWithNonTrivialWildcard .stream ()
98- .filter (path -> PathPatternUtil .isNodeMatch (path , nodeName ))
99- .map (children ::get )
100- .forEach (res ::add );
98+ for (final Map .Entry <String , Pattern > entry :
99+ childrenPatternsWithNonTrivialWildcard .entrySet ()) {
100+ if (entry .getValue ().matcher (nodeName ).matches ()) {
101+ res .add (children .get (entry .getKey ()));
102+ }
103+ }
101104 return res ;
102105 }
103106
@@ -110,13 +113,16 @@ public void addChild(PathPatternNode<V, VSerializer> tmpNode) {
110113 if (PathPatternUtil .hasWildcard (nodeName )
111114 && !PathPatternUtil .isMultiLevelMatchWildcard (nodeName )
112115 && !ONE_LEVEL_PATH_WILDCARD .equals (nodeName )) {
113- childrenNamesWithNonTrivialWildcard .add (nodeName );
116+ childrenPatternsWithNonTrivialWildcard .computeIfAbsent (
117+ nodeName , PathPatternUtil ::compileNodePattern );
114118 }
115119 children .put (nodeName , tmpNode );
116120 }
117121
118- public void deleteChild (PathPatternNode <V , VSerializer > tmpNode ) {
119- children .remove (tmpNode .getName ());
122+ public void deleteChild (final PathPatternNode <V , VSerializer > tmpNode ) {
123+ final String nodeName = tmpNode .getName ();
124+ children .remove (nodeName );
125+ childrenPatternsWithNonTrivialWildcard .remove (nodeName );
120126 }
121127
122128 public void appendValue (V value , BiConsumer <V , Set <V >> remappingFunction ) {
@@ -248,6 +254,14 @@ void serializeChildren(DataOutputStream outputStream) throws IOException {
248254 }
249255 }
250256
257+ void clear () {
258+ if (Objects .nonNull (valueSet )) {
259+ valueSet .clear ();
260+ }
261+ children .clear ();
262+ childrenPatternsWithNonTrivialWildcard .clear ();
263+ }
264+
251265 public static <V , T extends PathPatternNode .Serializer <V >> PathPatternNode <V , T > deserializeNode (
252266 ByteBuffer buffer , T serializer , Consumer <String > nodeNameProcessor ) {
253267 PathPatternNode <V , T > node =
@@ -279,7 +293,10 @@ public long ramBytesUsed() {
279293 return SHALLOW_SIZE
280294 + RamUsageEstimator .sizeOf (name )
281295 + RamUsageEstimator .sizeOfHashSet (valueSet )
282- + RamUsageEstimator .sizeOfHashSet (childrenNamesWithNonTrivialWildcard )
296+ + RamUsageEstimator .sizeOfMapWithKnownShallowSize (
297+ childrenPatternsWithNonTrivialWildcard ,
298+ RamUsageEstimator .SHALLOW_SIZE_OF_HASHMAP ,
299+ RamUsageEstimator .SHALLOW_SIZE_OF_HASHMAP_ENTRY )
283300 + RamUsageEstimator .sizeOfMapWithKnownShallowSize (
284301 children ,
285302 RamUsageEstimator .SHALLOW_SIZE_OF_HASHMAP ,
0 commit comments