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

jiangtian pushed a commit to branch tsFile_v4
in repository https://gitbox.apache.org/repos/asf/tsfile.git


The following commit(s) were added to refs/heads/tsFile_v4 by this push:
     new 1866b69a Supplement interfaces in IDeviceID
1866b69a is described below

commit 1866b69a9e5cfb306baf85babbb80977122f6323
Author: jt2594838 <[email protected]>
AuthorDate: Sun Apr 7 12:23:50 2024 +0800

    Supplement interfaces in IDeviceID
---
 .../org/apache/tsfile/file/metadata/IDeviceID.java | 19 ++++++++
 .../apache/tsfile/file/metadata/PlainDeviceID.java | 53 ++++++++++++++++++++++
 2 files changed, 72 insertions(+)

diff --git 
a/tsfile/src/main/java/org/apache/tsfile/file/metadata/IDeviceID.java 
b/tsfile/src/main/java/org/apache/tsfile/file/metadata/IDeviceID.java
index 9008cd8c..5b3e7255 100644
--- a/tsfile/src/main/java/org/apache/tsfile/file/metadata/IDeviceID.java
+++ b/tsfile/src/main/java/org/apache/tsfile/file/metadata/IDeviceID.java
@@ -38,8 +38,27 @@ public interface IDeviceID extends Comparable<IDeviceID>, 
Accountable {
 
   boolean isEmpty();
 
+  /**
+   * @return the table name associated with the device. For a path-DeviceId, 
like "root.a.b.c.d", it
+   *     is converted according to a fixed rule, like assuming the first three 
levels ("root.a.b")
+   *     as the table name; for a tuple-deviceId, like "(table1, beijing, 
turbine)", it is the first
+   *     element in the deviceId, namely "table1".
+   */
   String getTableName();
 
+  /**
+   * @return how many segments this DeviceId consists of. For a path-DeviceId, 
like "root.a.b.c.d",
+   *     it is 5; fot a tuple-DeviceId, like "(table1, beijing, turbine)", it 
is 3.
+   */
+  int segmentNum();
+
+  /**
+   * @param i the sequence number of the segment that should be returned.
+   * @return i-th segment in this DeviceId.
+   * @throws ArrayIndexOutOfBoundsException if i >= segmentNum().
+   */
+  String segment(int i);
+
   static IDeviceID deserializeFrom(ByteBuffer byteBuffer) {
     return new PlainDeviceID(ReadWriteIOUtils.readVarIntString(byteBuffer));
   }
diff --git 
a/tsfile/src/main/java/org/apache/tsfile/file/metadata/PlainDeviceID.java 
b/tsfile/src/main/java/org/apache/tsfile/file/metadata/PlainDeviceID.java
index eb922521..b28f1fff 100644
--- a/tsfile/src/main/java/org/apache/tsfile/file/metadata/PlainDeviceID.java
+++ b/tsfile/src/main/java/org/apache/tsfile/file/metadata/PlainDeviceID.java
@@ -19,6 +19,7 @@
 
 package org.apache.tsfile.file.metadata;
 
+import org.apache.tsfile.common.constant.TsFileConstant;
 import org.apache.tsfile.utils.RamUsageEstimator;
 import org.apache.tsfile.utils.ReadWriteIOUtils;
 
@@ -29,13 +30,18 @@ import java.util.Objects;
 
 import static org.apache.tsfile.utils.RamUsageEstimator.sizeOfCharArray;
 
+// TODO: rename to PathDeviceID (countering TupleDeviceID or ArrayDeviceID)
 /** Using device id path as id. */
 public class PlainDeviceID implements IDeviceID {
 
+  // TODO: configurable but unchangeable
+  private static final int DEFAULT_SEGMENT_NUM_FOR_TABLE_NAME = 3;
   private static final long INSTANCE_SIZE =
       RamUsageEstimator.shallowSizeOfInstance(PlainDeviceID.class)
           + RamUsageEstimator.shallowSizeOfInstance(String.class);
   private final String deviceID;
+  private String tableName;
+  private String[] segments;
 
   public PlainDeviceID(String deviceID) {
     this.deviceID = deviceID;
@@ -102,4 +108,51 @@ public class PlainDeviceID implements IDeviceID {
     }
     return deviceID.compareTo(((PlainDeviceID) other).deviceID);
   }
+
+  @Override
+  public String getTableName() {
+    if (tableName != null) {
+      return tableName;
+    }
+
+    int lastSeparatorPos = -1;
+    int separatorNum = 0;
+
+    for (int i = 0; i < deviceID.length(); i++) {
+      if (deviceID.charAt(i) == TsFileConstant.PATH_SEPARATOR_CHAR) {
+        lastSeparatorPos = i;
+        separatorNum++;
+        if (separatorNum == DEFAULT_SEGMENT_NUM_FOR_TABLE_NAME) {
+          break;
+        }
+      }
+    }
+    if (lastSeparatorPos == -1) {
+      // not find even one separator, probably during a test, use the deviceId 
as the tableName
+      tableName = deviceID;
+    } else {
+      // use the first DEFAULT_SEGMENT_NUM_FOR_TABLE_NAME segments or all 
segments but the last
+      // one as the table name
+      tableName = deviceID.substring(0, lastSeparatorPos);
+    }
+
+    return tableName;
+  }
+
+  @Override
+  public int segmentNum() {
+    if (segments != null) {
+      return segments.length;
+    }
+    segments = deviceID.split(TsFileConstant.PATH_SEPARATER_NO_REGEX);
+    return segments.length;
+  }
+
+  @Override
+  public String segment(int i) {
+    if (i >= segmentNum()) {
+      throw new ArrayIndexOutOfBoundsException(i);
+    }
+    return segments[i];
+  }
 }

Reply via email to