SzyWilliam commented on code in PR #11540:
URL: https://github.com/apache/iotdb/pull/11540#discussion_r1398106204


##########
iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/analyze/LoadTsfileAnalyzer.java:
##########
@@ -236,142 +241,76 @@ private long getWritePointCount(Map<String, 
List<TimeseriesMetadata>> device2Tim
         .sum();
   }
 
-  private static final class TimeSeriesIterator
-      implements Iterator<Pair<String, TimeseriesMetadata>> {
-
-    private static final Logger LOGGER = 
LoggerFactory.getLogger(TimeSeriesIterator.class);
-
-    private static final long LOG_PRINT_INTERVAL = 10000;
-    private long returnedTimeseriesCount = 0;
-    private boolean lastLogHasPrinted = false;
-
-    private final File tsFile;
-    private final Iterator<Map.Entry<String, List<TimeseriesMetadata>>>
-        device2TimeseriesMetadataIterator;
-
-    private String currentDevice;
-    private Iterator<TimeseriesMetadata> timeseriesMetadataIterator;
-
-    public TimeSeriesIterator(
-        File tsFile, Map<String, List<TimeseriesMetadata>> 
device2TimeseriesMetadata) {
-      this.tsFile = tsFile;
-      this.device2TimeseriesMetadataIterator = 
device2TimeseriesMetadata.entrySet().iterator();
-    }
-
-    @Override
-    public boolean hasNext() {
-      if (timeseriesMetadataIterator == null || 
!timeseriesMetadataIterator.hasNext()) {
-        if (device2TimeseriesMetadataIterator.hasNext()) {
-          final Map.Entry<String, List<TimeseriesMetadata>> next =
-              device2TimeseriesMetadataIterator.next();
-          currentDevice = next.getKey();
-          timeseriesMetadataIterator = next.getValue().iterator();
-        } else {
-          if (!lastLogHasPrinted) {
-            LOGGER.info(
-                "Analyzing TsFile {}, all {} timeseries has been returned to 
analyzer.",
-                tsFile.getAbsolutePath(),
-                returnedTimeseriesCount);
-            lastLogHasPrinted = true;
-          }
-          return false;
-        }
-      }
-      return timeseriesMetadataIterator.hasNext();
-    }
-
-    @Override
-    public Pair<String, TimeseriesMetadata> next() {
-      if (returnedTimeseriesCount == 0) {
-        LOGGER.info(
-            "Analyzing TsFile {}, start to return timeseries to analyzer.",
-            tsFile.getAbsolutePath());
-      } else if (returnedTimeseriesCount % LOG_PRINT_INTERVAL == 0) {
-        LOGGER.info(
-            "Analyzing TsFile {}, until now {} timeseries has been returned to 
analyzer.",
-            tsFile.getAbsolutePath(),
-            returnedTimeseriesCount);
-      }
-      returnedTimeseriesCount++;
-
-      return new Pair<>(currentDevice, timeseriesMetadataIterator.next());
-    }
-  }
-
   private final class SchemaAutoCreatorAndVerifier {
 
     private final Map<String, Boolean> tsfileDevice2IsAligned = new 
HashMap<>();
-
-    private final int maxTimeseriesNumberPerBatch = 
CONFIG.getMaxLoadingTimeseriesNumber();
     private final Map<String, Set<MeasurementSchema>> 
currentBatchDevice2TimeseriesSchemas =
         new HashMap<>();
-    private int currentBatchTimeseriesCount = 0;
 
     private final Set<PartialPath> alreadySetDatabases = new HashSet<>();
 
     private SchemaAutoCreatorAndVerifier() {}
 
     public void autoCreateAndVerify(
-        TsFileSequenceReader reader, TimeSeriesIterator timeSeriesIterator)
+        TsFileSequenceReader reader,
+        Map<String, List<TimeseriesMetadata>> device2TimeseriesMetadataList)
         throws IOException, AuthException {
-      while (currentBatchTimeseriesCount < maxTimeseriesNumberPerBatch
-          && timeSeriesIterator.hasNext()) {
-        final Pair<String, TimeseriesMetadata> pair = 
timeSeriesIterator.next();
-        final String device = pair.left;
-        final TimeseriesMetadata timeseriesMetadata = pair.right;
-        final TSDataType dataType = timeseriesMetadata.getTsDataType();
-        if (dataType.equals(TSDataType.VECTOR)) {
-          tsfileDevice2IsAligned.put(device, true);
-
-          // not a timeseries, skip ++currentBatchTimeseriesCount
-        } else {
-          // check WRITE_DATA permission of timeseries
-          long startTime = System.nanoTime();
-          try {
-            String userName = context.getSession().getUserName();
-            if (!AuthorityChecker.SUPER_USER.equals(userName)) {
-              TSStatus status;
-              try {
-                List<PartialPath> paths =
-                    Collections.singletonList(
-                        new PartialPath(device, 
timeseriesMetadata.getMeasurementId()));
-                status =
-                    AuthorityChecker.getTSStatus(
-                        AuthorityChecker.checkFullPathListPermission(
-                            userName, paths, 
PrivilegeType.WRITE_DATA.ordinal()),
-                        paths,
-                        PrivilegeType.WRITE_DATA);
-              } catch (IllegalPathException e) {
-                throw new RuntimeException(e);
-              }
-              if (status.getCode() != 
TSStatusCode.SUCCESS_STATUS.getStatusCode()) {
-                throw new AuthException(
-                    TSStatusCode.representOf(status.getCode()), 
status.getMessage());
+      for (final Map.Entry<String, List<TimeseriesMetadata>> entry :
+          device2TimeseriesMetadataList.entrySet()) {
+        final String device = entry.getKey();
+
+        for (final TimeseriesMetadata timeseriesMetadata : entry.getValue()) {
+          final TSDataType dataType = timeseriesMetadata.getTsDataType();
+          if (dataType.equals(TSDataType.VECTOR)) {

Review Comment:
   TSDataType.VECTOR.equals(dataType)



##########
iotdb-core/tsfile/src/main/java/org/apache/iotdb/tsfile/read/TsFileSequenceReaderTimeseriesMetadataIterator.java:
##########
@@ -0,0 +1,222 @@
+/*
+ * 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.iotdb.tsfile.read;
+
+import 
org.apache.iotdb.tsfile.exception.TsFileSequenceReaderTimeseriesMetadataIteratorException;
+import org.apache.iotdb.tsfile.file.metadata.MetadataIndexEntry;
+import org.apache.iotdb.tsfile.file.metadata.MetadataIndexNode;
+import org.apache.iotdb.tsfile.file.metadata.TimeseriesMetadata;
+import org.apache.iotdb.tsfile.file.metadata.enums.MetadataIndexNodeType;
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.util.ArrayDeque;
+import java.util.ArrayList;
+import java.util.Deque;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.NoSuchElementException;
+
+public class TsFileSequenceReaderTimeseriesMetadataIterator
+    implements Iterator<Map<String, List<TimeseriesMetadata>>> {
+
+  private static final int MAX_TIMESERIES_METADATA_COUNT = 2000;
+  private final TsFileSequenceReader reader;
+  private final boolean needChunkMetadata;
+  private ByteBuffer currentBuffer = null;
+  private final Deque<MetadataIndexEntryInfo> metadataIndexEntryStack = new 
ArrayDeque<>();
+  private String currentDeviceId;
+  private int currentTimeseriesMetadataCount = 0;
+
+  public TsFileSequenceReaderTimeseriesMetadataIterator(
+      TsFileSequenceReader reader, boolean needChunkMetadata) throws 
IOException {
+    this.reader = reader;
+
+    if (this.reader.tsFileMetaData == null) {
+      this.reader.readFileMetadata();
+    }
+
+    MetadataIndexNode metadataIndexNode = 
reader.tsFileMetaData.getMetadataIndex();
+    long curEntryEndOffset = metadataIndexNode.getEndOffset();
+    List<MetadataIndexEntry> metadataIndexEntryList = 
metadataIndexNode.getChildren();
+    this.needChunkMetadata = needChunkMetadata;
+
+    for (int i = metadataIndexEntryList.size() - 1; i >= 0; i--) {
+      metadataIndexEntryStack.push(
+          new MetadataIndexEntryInfo(
+              metadataIndexEntryList.get(i), metadataIndexNode.getNodeType(), 
curEntryEndOffset));
+      curEntryEndOffset = metadataIndexEntryList.get(i).getOffset();
+    }
+  }
+
+  @Override
+  public boolean hasNext() {
+    return !metadataIndexEntryStack.isEmpty()
+        || (currentBuffer != null && currentBuffer.hasRemaining());
+  }
+
+  @Override
+  public Map<String, List<TimeseriesMetadata>> next() {
+    if (!hasNext()) {
+      throw new NoSuchElementException();
+    }
+
+    Map<String, List<TimeseriesMetadata>> timeseriesMetadataMap = new 
HashMap<>();
+
+    while (currentTimeseriesMetadataCount < MAX_TIMESERIES_METADATA_COUNT) {
+
+      /* 1. Check Buffer
+       *  TimeseriesMetataCount has reached the limit in the previous loop and
+       *  maybe there are still some data remained in the buffer.
+       */
+      if (currentBuffer != null && currentBuffer.hasRemaining()) {
+        timeseriesMetadataMap
+            .computeIfAbsent(currentDeviceId, k -> new ArrayList<>())
+            .addAll(deserializesTimeseriesMetadata());
+      }
+
+      if (currentTimeseriesMetadataCount >= MAX_TIMESERIES_METADATA_COUNT
+          || metadataIndexEntryStack.isEmpty()) {
+        break;
+      }
+
+      // 2. Deserialize MetadataIndexEntry
+      MetadataIndexEntryInfo indexEntryInfo = metadataIndexEntryStack.pop();
+
+      try {
+        deserializeMetadataIndexEntry(indexEntryInfo, timeseriesMetadataMap);
+      } catch (IOException e) {
+        throw new TsFileSequenceReaderTimeseriesMetadataIteratorException(
+            String.format(
+                "TsFileSequenceReaderTimeseriesMetadataIterator: 
deserializeMetadataIndexEntry failed, "
+                    + "MetadataIndexEntryInfo: %s, "
+                    + e.getMessage(),
+                indexEntryInfo));
+      }
+    }
+
+    // 3. Reset currentTimeseriesMetadataCount
+    if (currentTimeseriesMetadataCount >= MAX_TIMESERIES_METADATA_COUNT) {
+      currentTimeseriesMetadataCount = 0;
+    }
+
+    return timeseriesMetadataMap;
+  }
+
+  private void deserializeMetadataIndexEntry(
+      MetadataIndexEntryInfo metadataIndexEntryInfo,
+      Map<String, List<TimeseriesMetadata>> timeseriesMetadataMap)
+      throws IOException {
+    if (metadataIndexEntryInfo
+        .getMetadataIndexNodeType()
+        .equals(MetadataIndexNodeType.LEAF_MEASUREMENT)) {
+      deserializeLeafMeasurement(
+          metadataIndexEntryInfo.getMetadataIndexEntry(),
+          metadataIndexEntryInfo.getEndOffset(),
+          timeseriesMetadataMap);
+
+    } else {
+      deserializeInternalNode(
+          metadataIndexEntryInfo.getMetadataIndexEntry(),
+          metadataIndexEntryInfo.getEndOffset(),
+          metadataIndexEntryInfo
+              .getMetadataIndexNodeType()
+              .equals(MetadataIndexNodeType.LEAF_DEVICE));
+    }
+  }
+
+  private void deserializeLeafMeasurement(
+      MetadataIndexEntry metadataIndexEntry,
+      long endOffset,
+      Map<String, List<TimeseriesMetadata>> timeseriesMetadataMap)
+      throws IOException {
+    if (currentBuffer != null && currentBuffer.hasRemaining()) {
+      throw new TsFileSequenceReaderTimeseriesMetadataIteratorException(
+          "currentBuffer still has some data left before 
deserializeLeafMeasurement");
+    }
+
+    currentBuffer = reader.readData(metadataIndexEntry.getOffset(), endOffset);
+
+    timeseriesMetadataMap
+        .computeIfAbsent(currentDeviceId, k -> new ArrayList<>())
+        .addAll(deserializesTimeseriesMetadata());
+  }
+
+  private List<TimeseriesMetadata> deserializesTimeseriesMetadata() {
+    List<TimeseriesMetadata> timeseriesMetadataList = new ArrayList<>();

Review Comment:
   you can still use final here



-- 
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