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

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


The following commit(s) were added to refs/heads/cem by this push:
     new d61ab6fcdf0 fix
d61ab6fcdf0 is described below

commit d61ab6fcdf0b3b28293e5880acd99efaab2aabe1
Author: Caideyipi <[email protected]>
AuthorDate: Fri Nov 28 16:58:30 2025 +0800

    fix
---
 .../fetcher/cache/TableDeviceLastCache.java        | 49 +++++++++++++---------
 .../fetcher/cache/TreeDeviceNormalSchema.java      |  4 +-
 2 files changed, 31 insertions(+), 22 deletions(-)

diff --git 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/metadata/fetcher/cache/TableDeviceLastCache.java
 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/metadata/fetcher/cache/TableDeviceLastCache.java
index f814139e880..0ad384c8bd9 100644
--- 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/metadata/fetcher/cache/TableDeviceLastCache.java
+++ 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/metadata/fetcher/cache/TableDeviceLastCache.java
@@ -120,14 +120,12 @@ public class TableDeviceLastCache {
           (measurementKey, tvPair) -> {
             if (Objects.isNull(newPair)) {
               diff.addAndGet(
-                  -((isTableModel ? 0 : (int) 
RamUsageEstimator.sizeOf(finalMeasurement))
-                      + getTVPairEntrySize(tvPair)));
+                  -((isTableModel ? 0 : sizeOf(finalMeasurement)) + 
getTVPairEntrySize(tvPair)));
               return null;
             }
             if (Objects.isNull(tvPair)) {
               diff.addAndGet(
-                  (isTableModel ? 0 : (int) 
RamUsageEstimator.sizeOf(finalMeasurement))
-                      + getTVPairEntrySize(newPair));
+                  (isTableModel ? 0 : sizeOf(finalMeasurement)) + 
getTVPairEntrySize(newPair));
               return newPair;
             }
             return tvPair;
@@ -136,6 +134,10 @@ public class TableDeviceLastCache {
     return diff.get();
   }
 
+  private int sizeOf(String s) {
+    return s == "" ? 0 : (int) RamUsageEstimator.sizeOf(s);
+  }
+
   int tryUpdate(
       final @Nonnull String[] measurements, final @Nonnull TimeValuePair[] 
timeValuePairs) {
     return tryUpdate(measurements, timeValuePairs, false);
@@ -151,7 +153,9 @@ public class TableDeviceLastCache {
     for (int i = 0; i < measurements.length; ++i) {
       if (Objects.isNull(timeValuePairs[i])) {
         if (invalidateNull) {
-          measurement2CachedLastMap.remove(measurements[i]);
+          diff.addAndGet(
+              sizeOf(measurements[i])
+                  + 
getTVPairEntrySize(measurement2CachedLastMap.remove(measurements[i])));
         }
         continue;
       }
@@ -187,9 +191,7 @@ public class TableDeviceLastCache {
     measurement2CachedLastMap.computeIfPresent(
         measurement,
         (s, timeValuePair) -> {
-          diff.set(
-              (isTableModel ? 0 : (int) RamUsageEstimator.sizeOf(s))
-                  + getTVPairEntrySize(timeValuePair));
+          diff.set((isTableModel ? 0 : sizeOf(s)) + 
getTVPairEntrySize(timeValuePair));
           time.set(timeValuePair.getTimestamp());
           return null;
         });
@@ -200,7 +202,7 @@ public class TableDeviceLastCache {
         "",
         (s, timeValuePair) -> {
           if (timeValuePair.getTimestamp() <= time.get()) {
-            diff.addAndGet(getTVPairEntrySize(timeValuePair));
+            diff.addAndGet(sizeOf(s) + getTVPairEntrySize(timeValuePair));
             return null;
           }
           return timeValuePair;
@@ -209,13 +211,18 @@ public class TableDeviceLastCache {
     return diff.get();
   }
 
-  private int getTVPairEntrySize(final TimeValuePair tvPair) {
-    return (int) RamUsageEstimator.HASHTABLE_RAM_BYTES_PER_ENTRY
-        + ((Objects.isNull(tvPair)
-                || tvPair == PLACEHOLDER_TIME_VALUE_PAIR
-                || tvPair == EMPTY_TIME_VALUE_PAIR)
-            ? 0
-            : tvPair.getSize());
+  private static int getTVPairEntrySize(final TimeValuePair tvPair) {
+    return (int) RamUsageEstimator.HASHTABLE_RAM_BYTES_PER_ENTRY + 
getTVPairSize(tvPair);
+  }
+
+  private static int getTVPairSize(final TimeValuePair tvPair) {
+    return isEmptyTVPair(tvPair) ? 0 : tvPair.getSize();
+  }
+
+  private static boolean isEmptyTVPair(final TimeValuePair tvPair) {
+    return Objects.isNull(tvPair)
+        || tvPair == PLACEHOLDER_TIME_VALUE_PAIR
+        || tvPair == EMPTY_TIME_VALUE_PAIR;
   }
 
   @Nullable
@@ -263,15 +270,17 @@ public class TableDeviceLastCache {
     return INSTANCE_SIZE
         + (int) RamUsageEstimator.HASHTABLE_RAM_BYTES_PER_ENTRY * 
measurement2CachedLastMap.size()
         + measurement2CachedLastMap.values().stream()
-            .mapToInt(TimeValuePair::getSize)
+            .mapToInt(TableDeviceLastCache::getTVPairSize)
             .reduce(0, Integer::sum);
   }
 
   private static int getDiffSize(
       final TimeValuePair oldTimeValuePair, final TimeValuePair 
newTimeValuePair) {
-    if (oldTimeValuePair == EMPTY_TIME_VALUE_PAIR
-        || oldTimeValuePair == PLACEHOLDER_TIME_VALUE_PAIR) {
-      return newTimeValuePair.getSize();
+    if (isEmptyTVPair(oldTimeValuePair)) {
+      return getTVPairSize(newTimeValuePair);
+    }
+    if (isEmptyTVPair(newTimeValuePair)) {
+      return -getTVPairSize(oldTimeValuePair);
     }
     final TsPrimitiveType oldValue = oldTimeValuePair.getValue();
     final TsPrimitiveType newValue = newTimeValuePair.getValue();
diff --git 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/metadata/fetcher/cache/TreeDeviceNormalSchema.java
 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/metadata/fetcher/cache/TreeDeviceNormalSchema.java
index 4e9c2fe9fde..da37202c470 100644
--- 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/metadata/fetcher/cache/TreeDeviceNormalSchema.java
+++ 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/metadata/fetcher/cache/TreeDeviceNormalSchema.java
@@ -99,13 +99,13 @@ public class TreeDeviceNormalSchema implements 
IDeviceSchema {
   public int estimateSize() {
     // Do not need to calculate database because it is interned
     return INSTANCE_SIZE
+        + measurementMap.size() * (int) 
RamUsageEstimator.HASHTABLE_RAM_BYTES_PER_ENTRY
         + measurementMap.entrySet().stream()
             .mapToInt(
                 entry ->
                     Math.toIntExact(
                         RamUsageEstimator.sizeOf(entry.getKey())
-                            + SchemaCacheEntry.estimateSize(entry.getValue())
-                            + RamUsageEstimator.HASHTABLE_RAM_BYTES_PER_ENTRY))
+                            + SchemaCacheEntry.estimateSize(entry.getValue())))
             .reduce(0, Integer::sum);
   }
 }

Reply via email to