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 0e4074a1 add ArrayDeviceID
0e4074a1 is described below

commit 0e4074a1c76fbb66cf3537cb00d54fa18821a1a1
Author: jt2594838 <[email protected]>
AuthorDate: Sun Apr 7 17:47:01 2024 +0800

    add ArrayDeviceID
---
 .../tsfile/exception/TsFileRuntimeException.java   |   4 +
 .../apache/tsfile/file/metadata/ArrayDeviceID.java | 124 +++++++++++++++++++++
 .../apache/tsfile/file/metadata/PlainDeviceID.java |  11 +-
 3 files changed, 138 insertions(+), 1 deletion(-)

diff --git 
a/tsfile/src/main/java/org/apache/tsfile/exception/TsFileRuntimeException.java 
b/tsfile/src/main/java/org/apache/tsfile/exception/TsFileRuntimeException.java
index 9bbd16e8..fe9fa934 100644
--- 
a/tsfile/src/main/java/org/apache/tsfile/exception/TsFileRuntimeException.java
+++ 
b/tsfile/src/main/java/org/apache/tsfile/exception/TsFileRuntimeException.java
@@ -30,4 +30,8 @@ public class TsFileRuntimeException extends RuntimeException {
   public TsFileRuntimeException(String message) {
     super(message);
   }
+
+  public TsFileRuntimeException(Throwable cause) {
+    super(cause);
+  }
 }
diff --git 
a/tsfile/src/main/java/org/apache/tsfile/file/metadata/ArrayDeviceID.java 
b/tsfile/src/main/java/org/apache/tsfile/file/metadata/ArrayDeviceID.java
new file mode 100644
index 00000000..4685df9c
--- /dev/null
+++ b/tsfile/src/main/java/org/apache/tsfile/file/metadata/ArrayDeviceID.java
@@ -0,0 +1,124 @@
+/*
+ * 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.tsfile.file.metadata;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.nio.ByteBuffer;
+import java.nio.charset.StandardCharsets;
+import org.apache.tsfile.exception.TsFileRuntimeException;
+import org.apache.tsfile.utils.RamUsageEstimator;
+import org.apache.tsfile.utils.ReadWriteIOUtils;
+
+public class ArrayDeviceID implements IDeviceID {
+
+  private static final long INSTANCE_SIZE =
+      RamUsageEstimator.shallowSizeOfInstance(ArrayDeviceID.class);
+
+  private String[] segments;
+
+  public ArrayDeviceID(String[] segments) {
+    this.segments = segments;
+  }
+
+  @Override
+  public int serialize(ByteBuffer byteBuffer) {
+    int cnt = 0;
+    cnt += ReadWriteIOUtils.write(segments.length, byteBuffer);
+    for (String segment : segments) {
+      cnt += ReadWriteIOUtils.write(segment, byteBuffer);
+    }
+    return cnt;
+  }
+
+  @Override
+  public int serialize(OutputStream outputStream) throws IOException {
+    int cnt = 0;
+    cnt += ReadWriteIOUtils.write(segments.length, outputStream);
+    for (String segment : segments) {
+      cnt += ReadWriteIOUtils.write(segment, outputStream);
+    }
+    return cnt;
+  }
+
+  @Override
+  public byte[] getBytes() {
+    ByteArrayOutputStream publicBAOS = new ByteArrayOutputStream(256);
+    for (String segment : segments) {
+      try {
+        publicBAOS.write(segment.getBytes(StandardCharsets.UTF_8));
+      } catch (IOException e) {
+        throw new TsFileRuntimeException(e);
+      }
+    }
+    return publicBAOS.toByteArray();
+  }
+
+  @Override
+  public boolean isEmpty() {
+    return segments == null || segments.length == 0;
+  }
+
+  @Override
+  public String getTableName() {
+    return segments[0];
+  }
+
+  @Override
+  public int segmentNum() {
+    return segments.length;
+  }
+
+  @Override
+  public String segment(int i) {
+    return segments[i];
+  }
+
+  @Override
+  public int compareTo(IDeviceID o) {
+    int thisSegmentNum = segmentNum();
+    int otherSegmentNum = o.segmentNum();
+    for (int i = 0; i < thisSegmentNum; i++) {
+      if (i >= otherSegmentNum) {
+        // the other ID is a prefix of this one
+        return 1;
+      }
+      final int comp = this.segment(i).compareTo(o.segment(i));
+      if (comp != 0) {
+        // the partial comparison has a result
+        return comp;
+      }
+    }
+
+    if (thisSegmentNum < otherSegmentNum) {
+      // this ID is a prefix of the other one
+      return -1;
+    }
+
+    // two ID equal
+    return 0;
+  }
+
+  @Override
+  public long ramBytesUsed() {
+    return INSTANCE_SIZE + RamUsageEstimator.sizeOf(segments);
+  }
+}
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 b28f1fff..f7bb0b84 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
@@ -38,6 +38,7 @@ public class PlainDeviceID implements IDeviceID {
   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)
           + RamUsageEstimator.shallowSizeOfInstance(String.class);
   private final String deviceID;
   private String tableName;
@@ -94,7 +95,15 @@ public class PlainDeviceID implements IDeviceID {
 
   @Override
   public long ramBytesUsed() {
-    return INSTANCE_SIZE + sizeOfCharArray(deviceID.length());
+    long size = INSTANCE_SIZE;
+    size += sizeOfCharArray(deviceID.length());
+    if (tableName != null) {
+      size += sizeOfCharArray(tableName.length());
+    }
+    if (segments != null) {
+      size += RamUsageEstimator.sizeOf(segments);
+    }
+    return size;
   }
 
   public static PlainDeviceID deserialize(ByteBuffer byteBuffer) {

Reply via email to