shizy818 commented on code in PR #14265:
URL: https://github.com/apache/iotdb/pull/14265#discussion_r1865353421


##########
iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/memtable/ReadOnlyMemChunk.java:
##########
@@ -92,77 +115,223 @@ public ReadOnlyMemChunk(
         floatPrecision = 
TSFileDescriptor.getInstance().getConfig().getFloatPrecision();
       }
     }
-    this.tsBlock = tvList.buildTsBlock(floatPrecision, encoding, deletionList);
-    initChunkMetaFromTsBlock();
+    this.floatPrecision = floatPrecision;
+    this.encoding = encoding;
+    this.deletionList = deletionList;
+    this.tvListQueryMap = tvListQueryMap;
+    this.pageStatisticsList = new ArrayList<>();
+    this.pageOffsetsList = new ArrayList<>();
+    this.context.addTVListToSet(tvListQueryMap);
+
+    initChunkAndPageStatistics();
   }
 
-  private void initChunkMetaFromTsBlock() throws QueryProcessException {
-    Statistics statsByType = Statistics.getStatsByType(dataType);
+  private void initChunkAndPageStatistics() {
+    // create chunk metadata
+    Statistics chunkStatistics = Statistics.getStatsByType(dataType);
     IChunkMetadata metaData =
-        new ChunkMetadata(measurementUid, dataType, null, null, 0, 
statsByType);
-    if (!isEmpty()) {
-      switch (dataType) {
-        case BOOLEAN:
-          for (int i = 0; i < tsBlock.getPositionCount(); i++) {
-            statsByType.update(tsBlock.getTimeByIndex(i), 
tsBlock.getColumn(0).getBoolean(i));
-          }
-          break;
-        case TEXT:
-        case BLOB:
-        case STRING:
-          for (int i = 0; i < tsBlock.getPositionCount(); i++) {
-            statsByType.update(tsBlock.getTimeByIndex(i), 
tsBlock.getColumn(0).getBinary(i));
-          }
-          break;
-        case FLOAT:
-          for (int i = 0; i < tsBlock.getPositionCount(); i++) {
-            statsByType.update(tsBlock.getTimeByIndex(i), 
tsBlock.getColumn(0).getFloat(i));
-          }
-          break;
-        case INT32:
-        case DATE:
-          for (int i = 0; i < tsBlock.getPositionCount(); i++) {
-            statsByType.update(tsBlock.getTimeByIndex(i), 
tsBlock.getColumn(0).getInt(i));
-          }
-          break;
-        case INT64:
-        case TIMESTAMP:
-          for (int i = 0; i < tsBlock.getPositionCount(); i++) {
-            statsByType.update(tsBlock.getTimeByIndex(i), 
tsBlock.getColumn(0).getLong(i));
-          }
-          break;
-        case DOUBLE:
-          for (int i = 0; i < tsBlock.getPositionCount(); i++) {
-            statsByType.update(tsBlock.getTimeByIndex(i), 
tsBlock.getColumn(0).getDouble(i));
-          }
-          break;
-        default:
-          throw new QueryProcessException("Unsupported data type:" + dataType);
-      }
-    }
-    statsByType.setEmpty(isEmpty());
+        new ChunkMetadata(measurementUid, dataType, null, null, 0, 
chunkStatistics);
     metaData.setChunkLoader(new MemChunkLoader(context, this));
     metaData.setVersion(Long.MAX_VALUE);
     cachedMetaData = metaData;
+
+    sortTvLists();
+    updateChunkAndPageStatisticsFromTvLists();
+  }
+
+  private void sortTvLists() {
+    for (Map.Entry<TVList, Integer> entry : getTvListQueryMap().entrySet()) {
+      TVList tvList = entry.getKey();
+      int queryRowCount = entry.getValue();
+      if (!tvList.isSorted() && queryRowCount > tvList.seqRowCount()) {
+        tvList.sort();
+      }
+    }
+  }
+
+  private void updateChunkAndPageStatisticsFromTvLists() {
+    Statistics chunkStatistics = cachedMetaData.getStatistics();
+
+    int cnt = 0;
+    int[] deleteCursor = {0};
+    List<TVList> tvLists = new ArrayList<>(tvListQueryMap.keySet());
+    MergeSortTvListIterator timeValuePairIterator =
+        new MergeSortTvListIterator(dataType, encoding, floatPrecision, 
tvLists);
+    int[] tvListOffsets = timeValuePairIterator.getTVListOffsets();
+    while (timeValuePairIterator.hasNextTimeValuePair()) {
+      TimeValuePair tvPair = timeValuePairIterator.nextTimeValuePair();
+      if (!isPointDeleted(tvPair.getTimestamp(), deletionList, deleteCursor)) {
+        if (cnt % MAX_NUMBER_OF_POINTS_IN_PAGE == 0) {
+          Statistics stats = Statistics.getStatsByType(dataType);
+          pageStatisticsList.add(stats);
+          pageOffsetsList.add(tvListOffsets);
+        }
+
+        Statistics pageStatistics = 
pageStatisticsList.get(pageStatisticsList.size() - 1);
+        switch (dataType) {
+          case BOOLEAN:
+            chunkStatistics.update(tvPair.getTimestamp(), 
tvPair.getValue().getBoolean());
+            pageStatistics.update(tvPair.getTimestamp(), 
tvPair.getValue().getBoolean());
+            break;
+          case INT32:
+          case DATE:
+            chunkStatistics.update(tvPair.getTimestamp(), 
tvPair.getValue().getInt());
+            pageStatistics.update(tvPair.getTimestamp(), 
tvPair.getValue().getInt());
+            break;
+          case INT64:
+          case TIMESTAMP:
+            chunkStatistics.update(tvPair.getTimestamp(), 
tvPair.getValue().getLong());
+            pageStatistics.update(tvPair.getTimestamp(), 
tvPair.getValue().getLong());
+            break;
+          case FLOAT:
+            chunkStatistics.update(tvPair.getTimestamp(), 
tvPair.getValue().getFloat());
+            pageStatistics.update(tvPair.getTimestamp(), 
tvPair.getValue().getFloat());
+            break;
+          case DOUBLE:
+            chunkStatistics.update(tvPair.getTimestamp(), 
tvPair.getValue().getDouble());
+            pageStatistics.update(tvPair.getTimestamp(), 
tvPair.getValue().getDouble());
+            break;
+          case TEXT:
+          case BLOB:
+          case STRING:
+            chunkStatistics.update(tvPair.getTimestamp(), 
tvPair.getValue().getBinary());
+            pageStatistics.update(tvPair.getTimestamp(), 
tvPair.getValue().getBinary());
+            break;
+          default:
+            // do nothing
+        }
+        pageStatistics.setEmpty(false);
+      }
+      tvListOffsets = timeValuePairIterator.getTVListOffsets();
+      cnt++;

Review Comment:
    cnt++好像应该放到前面未被删除的判断内



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to