Cpaulyz commented on code in PR #7270:
URL: https://github.com/apache/iotdb/pull/7270#discussion_r971609919


##########
node-commons/src/main/java/org/apache/iotdb/commons/path/PatternTreeMap.java:
##########
@@ -0,0 +1,156 @@
+/*
+ * 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.conf.IoTDBConstant;
+
+import javax.annotation.concurrent.NotThreadSafe;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Set;
+import java.util.function.BiConsumer;
+import java.util.function.Supplier;
+
+@NotThreadSafe
+public class PatternTreeMap<V> {
+  private final PathPatternNode<V> root;
+  private final Supplier<? extends Set<V>> supplier;
+  private BiConsumer<V, Set<V>> appendFunction;
+  private BiConsumer<V, Set<V>> deleteFunction;
+
+  /**
+   * Create PatternTreeMap.
+   *
+   * @param supplier provide type of set to store values on PathPatternNode
+   * @param appendFunction define the merge logic of append value
+   * @param deleteFunction define the split logic of delete value
+   */
+  public PatternTreeMap(
+      Supplier<? extends Set<V>> supplier,
+      BiConsumer<V, Set<V>> appendFunction,
+      BiConsumer<V, Set<V>> deleteFunction) {
+    this.root = new PathPatternNode<>(IoTDBConstant.PATH_ROOT, supplier);
+    this.supplier = supplier;
+    this.appendFunction = appendFunction;
+    this.deleteFunction = deleteFunction;
+  }
+
+  /**
+   * Append key and value to PatternTreeMap.
+   *
+   * @param key PartialPath that can contain '*' or '**'
+   * @param value The value to be appended
+   */
+  public void append(PartialPath key, V value) {
+    if (appendFunction == null) {
+      throw new UnsupportedOperationException();
+    }
+    String[] pathNodes = key.getNodes();
+    PathPatternNode<V> curNode = root;
+    for (int i = 1; i < pathNodes.length; i++) {
+      PathPatternNode<V> nextNode = curNode.getChildren(pathNodes[i]);
+      if (nextNode == null) {
+        nextNode = new PathPatternNode<>(pathNodes[i], supplier);
+        curNode.addChild(nextNode);
+      }
+      curNode = nextNode;
+    }
+    curNode.appendValue(value, appendFunction);
+  }
+
+  /**
+   * Delete key and value to PatternTreeMap.
+   *
+   * @param key PartialPath that can contain '*' or '**'
+   * @param value The value to be deleted
+   */
+  public void delete(PartialPath key, V value) {
+    if (deleteFunction == null) {
+      throw new UnsupportedOperationException();
+    }
+    deletePathNode(root, key.getNodes(), 0, value);
+  }
+
+  /**
+   * Recursive method for deleting value.
+   *
+   * @param node current PathPatternNode
+   * @param pathNodes pathNodes of key
+   * @param pos current index of pathNodes
+   * @param value the value to be deleted
+   * @return true if current PathPatternNode can be removed
+   */
+  private boolean deletePathNode(PathPatternNode<V> node, String[] pathNodes, 
int pos, V value) {
+    if (node == null) {
+      return false;
+    }
+    if (pos == pathNodes.length - 1) {
+      node.deleteValue(value, deleteFunction);
+    } else {
+      PathPatternNode<V> child = node.getChildren(pathNodes[pos + 1]);
+      if (deletePathNode(child, pathNodes, pos + 1, value)) {
+        node.deleteChild(child);
+      }
+    }
+    return node.isLeaf() && node.getValues().isEmpty();
+  }
+
+  /**
+   * Get value list related to PathPattern that overlapped with fullPath.
+   *
+   * @param fullPath full path without wildcard
+   * @return value list
+   */
+  public List<V> getOverlapped(PartialPath fullPath) {
+    List<V> res = new ArrayList<>();
+    searchOverlapped(root, fullPath.getNodes(), 0, res, false);
+    return res;
+  }
+
+  /**
+   * Recursive method for search overlapped pattern.
+   *
+   * @param node current PathPatternNode
+   * @param pathNodes pathNodes of key
+   * @param pos current index of pathNodes
+   * @param resultList result list
+   * @param fromMultiWildCard true if node in caller is '**', so there is no 
need to traverse the
+   *     remaining pathNodes
+   */
+  public void searchOverlapped(
+      PathPatternNode<V> node,
+      String[] pathNodes,
+      int pos,
+      List<V> resultList,
+      boolean fromMultiWildCard) {

Review Comment:
   `fromMultiWildCard` is unnecessary. I will remove it later.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to