choubenson commented on code in PR #12122:
URL: https://github.com/apache/iotdb/pull/12122#discussion_r1576040934


##########
iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/schema/ttl/TTLCache.java:
##########
@@ -0,0 +1,275 @@
+/*
+ * 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.schema.ttl;
+
+import org.apache.iotdb.commons.conf.CommonDescriptor;
+import org.apache.iotdb.commons.conf.IoTDBConstant;
+import org.apache.iotdb.commons.path.PartialPath;
+import org.apache.iotdb.commons.utils.CommonDateTimeUtils;
+import org.apache.iotdb.tsfile.utils.ReadWriteIOUtils;
+
+import javax.annotation.concurrent.NotThreadSafe;
+
+import java.io.BufferedInputStream;
+import java.io.BufferedOutputStream;
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.LinkedHashMap;
+import java.util.Map;
+import java.util.Objects;
+
+@NotThreadSafe
+public class TTLCache {
+
+  private final CacheNode ttlCacheTree;
+  public static final long NULL_TTL = -1;
+
+  private int ttlCount;
+
+  public TTLCache() {
+    ttlCacheTree = new CacheNode(IoTDBConstant.PATH_ROOT);
+    long defaultTTL =
+        CommonDateTimeUtils.convertMilliTimeWithPrecision(
+            CommonDescriptor.getInstance().getConfig().getDefaultTTLInMs(),
+            
CommonDescriptor.getInstance().getConfig().getTimestampPrecision());
+    defaultTTL = defaultTTL <= 0 ? Long.MAX_VALUE : defaultTTL;
+    ttlCacheTree.addChild(IoTDBConstant.MULTI_LEVEL_PATH_WILDCARD, defaultTTL);
+    ttlCount = 1;
+  }
+
+  /**
+   * Put ttl into cache tree.
+   *
+   * @param nodes should be prefix path or specific device path without 
wildcard
+   */
+  public void setTTL(String[] nodes, long ttl) {
+    if (nodes.length < 2 || ttl <= 0) {
+      return;
+    }
+    CacheNode parent = ttlCacheTree;
+    for (int i = 1; i < nodes.length; i++) {
+      CacheNode child = parent.getChild(nodes[i]);
+      if (child == null) {
+        parent.addChild(nodes[i], NULL_TTL);
+        child = parent.getChild(nodes[i]);
+      }
+      parent = child;
+    }
+    if (parent.ttl == NULL_TTL) {
+      ttlCount++;
+    }
+    parent.ttl = ttl;
+  }
+
+  /**
+   * Unset ttl and remove all useless nodes. If the path to be removed is 
internal node, then just
+   * reset its ttl. Else, find the sub path and remove it.
+   *
+   * @param nodes path to be removed
+   */
+  public void unsetTTL(String[] nodes) {
+    if (nodes.length < 2) {
+      return;
+    } else if (nodes.length == 2) {
+      // if path equals to root.**, then unset it to configured ttl
+      if (nodes[0].equals(IoTDBConstant.PATH_ROOT)
+          && nodes[1].equals(IoTDBConstant.MULTI_LEVEL_PATH_WILDCARD)) {
+        ttlCacheTree.getChild(IoTDBConstant.MULTI_LEVEL_PATH_WILDCARD).ttl =
+            CommonDescriptor.getInstance().getConfig().getDefaultTTLInMs();
+        return;
+      }
+    }
+    CacheNode parent = ttlCacheTree;

Review Comment:
   Resolved. Rename to `current`.



-- 
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: reviews-unsubscr...@iotdb.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to