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

tanxinyu pushed a commit to branch enhance_ideviceid_partition_cache
in repository https://gitbox.apache.org/repos/asf/iotdb.git

commit fc334138b42e47079bd27f9bf302fcf42fe30bca
Author: OneSizeFitQuorum <[email protected]>
AuthorDate: Tue Jul 2 15:28:21 2024 +0800

    enhance partition calculation for null segment
    
    Signed-off-by: OneSizeFitQuorum <[email protected]>
---
 .../partition/executor/SeriesPartitionExecutor.java    |  2 ++
 .../partition/executor/hash/APHashExecutor.java        | 18 ++++++++++++++----
 .../partition/executor/hash/BKDRHashExecutor.java      | 11 ++++++++---
 .../partition/executor/hash/JSHashExecutor.java        | 12 +++++++++---
 .../partition/executor/hash/SDBMHashExecutor.java      | 12 +++++++++---
 5 files changed, 42 insertions(+), 13 deletions(-)

diff --git 
a/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/partition/executor/SeriesPartitionExecutor.java
 
b/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/partition/executor/SeriesPartitionExecutor.java
index a07cd600476..d2666446e8e 100644
--- 
a/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/partition/executor/SeriesPartitionExecutor.java
+++ 
b/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/partition/executor/SeriesPartitionExecutor.java
@@ -36,6 +36,8 @@ public abstract class SeriesPartitionExecutor {
 
   protected final int seriesPartitionSlotNum;
 
+  protected final int NULL_SEGMENT_HASH_NUM = (int) Character.MAX_VALUE + 1;
+
   public SeriesPartitionExecutor(int seriesPartitionSlotNum) {
     this.seriesPartitionSlotNum = seriesPartitionSlotNum;
   }
diff --git 
a/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/partition/executor/hash/APHashExecutor.java
 
b/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/partition/executor/hash/APHashExecutor.java
index 1573502fb65..9390111d247 100644
--- 
a/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/partition/executor/hash/APHashExecutor.java
+++ 
b/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/partition/executor/hash/APHashExecutor.java
@@ -55,14 +55,24 @@ public class APHashExecutor extends SeriesPartitionExecutor 
{
     int index = 0;
 
     for (int segmentID = 0; segmentID < segmentNum; segmentID++) {
-      String segment = (String) deviceID.segment(segmentID);
-      for (int i = 0; i < segment.length(); i++) {
+      Object segment = deviceID.segment(segmentID);
+      if (segment instanceof String) {
+        String segmentStr = (String) segment;
+        for (int i = 0; i < segmentStr.length(); i++) {
+          if ((index++ & 1) == 0) {
+            hash ^= ((hash << 7) ^ (int) segmentStr.charAt(i) ^ (hash >> 3));
+          } else {
+            hash ^= (~((hash << 11) ^ (int) segmentStr.charAt(i) ^ (hash >> 
5)));
+          }
+        }
+      } else {
         if ((index++ & 1) == 0) {
-          hash ^= ((hash << 7) ^ (int) segment.charAt(i) ^ (hash >> 3));
+          hash ^= ((hash << 7) ^ NULL_SEGMENT_HASH_NUM ^ (hash >> 3));
         } else {
-          hash ^= (~((hash << 11) ^ (int) segment.charAt(i) ^ (hash >> 5)));
+          hash ^= (~((hash << 11) ^ NULL_SEGMENT_HASH_NUM ^ (hash >> 5)));
         }
       }
+
       if (segmentID < segmentNum - 1) {
         if ((index++ & 1) == 0) {
           hash ^= ((hash << 7) ^ (int) PATH_SEPARATOR ^ (hash >> 3));
diff --git 
a/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/partition/executor/hash/BKDRHashExecutor.java
 
b/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/partition/executor/hash/BKDRHashExecutor.java
index 25aaa055bdc..c039e8ddd83 100644
--- 
a/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/partition/executor/hash/BKDRHashExecutor.java
+++ 
b/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/partition/executor/hash/BKDRHashExecutor.java
@@ -52,9 +52,14 @@ public class BKDRHashExecutor extends 
SeriesPartitionExecutor {
     int segmentNum = deviceID.segmentNum();
 
     for (int segmentID = 0; segmentID < segmentNum; segmentID++) {
-      String segment = (String) deviceID.segment(segmentID);
-      for (int i = 0; i < segment.length(); i++) {
-        hash = hash * SEED + (int) segment.charAt(i);
+      Object segment = deviceID.segment(segmentID);
+      if (segment instanceof String) {
+        String segmentStr = (String) segment;
+        for (int i = 0; i < segmentStr.length(); i++) {
+          hash = hash * SEED + (int) segmentStr.charAt(i);
+        }
+      } else {
+        hash = hash * SEED + NULL_SEGMENT_HASH_NUM;
       }
       if (segmentID < segmentNum - 1) {
         hash = hash * SEED + (int) PATH_SEPARATOR;
diff --git 
a/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/partition/executor/hash/JSHashExecutor.java
 
b/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/partition/executor/hash/JSHashExecutor.java
index c3e088e6e8f..1e8c2031583 100644
--- 
a/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/partition/executor/hash/JSHashExecutor.java
+++ 
b/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/partition/executor/hash/JSHashExecutor.java
@@ -51,10 +51,16 @@ public class JSHashExecutor extends SeriesPartitionExecutor 
{
     int segmentNum = deviceID.segmentNum();
 
     for (int segmentID = 0; segmentID < segmentNum; segmentID++) {
-      String segment = (String) deviceID.segment(segmentID);
-      for (int i = 0; i < segment.length(); i++) {
-        hash ^= ((hash << 5) + (int) segment.charAt(i) + (hash >> 2));
+      Object segment = deviceID.segment(segmentID);
+      if (segment instanceof String) {
+        String segmentStr = (String) segment;
+        for (int i = 0; i < segmentStr.length(); i++) {
+          hash ^= ((hash << 5) + (int) segmentStr.charAt(i) + (hash >> 2));
+        }
+      } else {
+        hash ^= ((hash << 5) + NULL_SEGMENT_HASH_NUM + (hash >> 2));
       }
+
       if (segmentID < segmentNum - 1) {
         hash ^= ((hash << 5) + (int) PATH_SEPARATOR + (hash >> 2));
       }
diff --git 
a/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/partition/executor/hash/SDBMHashExecutor.java
 
b/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/partition/executor/hash/SDBMHashExecutor.java
index 48cff807bab..e2143c00c0c 100644
--- 
a/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/partition/executor/hash/SDBMHashExecutor.java
+++ 
b/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/partition/executor/hash/SDBMHashExecutor.java
@@ -49,10 +49,16 @@ public class SDBMHashExecutor extends 
SeriesPartitionExecutor {
     int segmentNum = deviceID.segmentNum();
 
     for (int segmentID = 0; segmentID < segmentNum; segmentID++) {
-      String segment = (String) deviceID.segment(segmentID);
-      for (int i = 0; i < segment.length(); i++) {
-        hash = ((int) segment.charAt(i) + (hash << 6) + (hash << 16) - hash);
+      Object segment = deviceID.segment(segmentID);
+      if (segment instanceof String) {
+        String segmentStr = (String) segment;
+        for (int i = 0; i < segmentStr.length(); i++) {
+          hash = ((int) segmentStr.charAt(i) + (hash << 6) + (hash << 16) - 
hash);
+        }
+      } else {
+        hash = (NULL_SEGMENT_HASH_NUM + (hash << 6) + (hash << 16) - hash);
       }
+
       if (segmentID < segmentNum - 1) {
         hash = ((int) PATH_SEPARATOR + (hash << 6) + (hash << 16) - hash);
       }

Reply via email to