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


##########
iotdb-core/tsfile/src/main/java/org/apache/iotdb/tsfile/read/TsFileSequenceReaderTimeseriesMetadataIterator.java:
##########
@@ -0,0 +1,213 @@
+/*
+ * 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.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 RuntimeException(e);

Review Comment:
   Throw some meaningful exception, with some message.



##########
iotdb-core/tsfile/src/main/java/org/apache/iotdb/tsfile/read/TsFileSequenceReaderTimeseriesMetadataIterator.java:
##########
@@ -0,0 +1,213 @@
+/*
+ * 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.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 RuntimeException(e);
+      }
+    }
+
+    // 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()) {
+      currentBuffer = reader.readData(metadataIndexEntry.getOffset(), 
endOffset);

Review Comment:
   This should not happen? throw a exception can be a better choice.



##########
iotdb-core/tsfile/src/main/java/org/apache/iotdb/tsfile/file/metadata/enums/MetadataIndexNodeType.java:
##########
@@ -41,7 +41,7 @@ public enum MetadataIndexNodeType {
   /** INTERNAL_MEASUREMENT. */
   INTERNAL_MEASUREMENT((byte) 2),
 
-  /** INTERNAL_MEASUREMENT. */
+  /** LEAF_MEASUREMENT. */

Review Comment:
   xswl



##########
iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/analyze/LoadTsfileAnalyzer.java:
##########
@@ -190,42 +187,50 @@ public Analysis analyzeFileByFile() {
   private void analyzeSingleTsFile(File tsFile) throws IOException, 
AuthException {
     try (final TsFileSequenceReader reader = new 
TsFileSequenceReader(tsFile.getAbsolutePath())) {
       // can be reused when constructing tsfile resource
-      final Map<String, List<TimeseriesMetadata>> device2TimeseriesMetadata =
-          reader.getAllTimeseriesMetadata(true);
-      if (device2TimeseriesMetadata.isEmpty()) {
-        LOGGER.warn("device2TimeseriesMetadata is empty, because maybe the 
tsfile is empty");
-        return;
-      }
-
-      // auto create or verify schema
-      if (IoTDBDescriptor.getInstance().getConfig().isAutoCreateSchemaEnabled()
-          || loadTsFileStatement.isVerifySchema()) {
-
-        final TimeSeriesIterator timeSeriesIterator =
-            new TimeSeriesIterator(tsFile, device2TimeseriesMetadata);
-        while (timeSeriesIterator.hasNext()) {
-          schemaAutoCreatorAndVerifier.autoCreateAndVerify(reader, 
timeSeriesIterator);
-        }
+      final TsFileSequenceReaderTimeseriesMetadataIterator 
timeseriesMetadataIterator =
+          new TsFileSequenceReaderTimeseriesMetadataIterator(reader, true);
 
-        
schemaAutoCreatorAndVerifier.flushAndClearDeviceIsAlignedCacheIfNecessary();
-      }
+      long writePointCount = 0;
 
       // construct tsfile resource
       final TsFileResource tsFileResource = new TsFileResource(tsFile);
       if (!tsFileResource.resourceFileExists()) {
         // it will be serialized in LoadSingleTsFileNode
-        FileLoaderUtils.updateTsFileResource(device2TimeseriesMetadata, 
tsFileResource);
         tsFileResource.updatePlanIndexes(reader.getMinPlanIndex());
         tsFileResource.updatePlanIndexes(reader.getMaxPlanIndex());
       } else {
         tsFileResource.deserialize();
       }
 
+      // auto create or verify schema
+      if (IoTDBDescriptor.getInstance().getConfig().isAutoCreateSchemaEnabled()
+          || loadTsFileStatement.isVerifySchema()) {
+        while (timeseriesMetadataIterator.hasNext()) {
+          Map<String, List<TimeseriesMetadata>> device2TimeseriesMetadata =
+              timeseriesMetadataIterator.next();
+
+          // check if the tsfile is empty
+          if (device2TimeseriesMetadata.isEmpty()) {
+            LOGGER.warn("device2TimeseriesMetadata is empty, because maybe the 
tsfile is empty");

Review Comment:
   move this logger before while.
   if (!timeseriesMetadataIterator.hasNext())
   LOGGER.warn("device2TimeseriesMetadata is empty, because maybe the tsfile is 
empty");



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