You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@iotdb.apache.org by "Caideyipi (via GitHub)" <gi...@apache.org> on 2023/11/21 12:13:26 UTC

Re: [PR] [IOTDB-6249] Load: Streaming read timeseries metadata from tsfile when auto creating or verifying schema [iotdb]

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


##########
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();
+    }
+
+    final 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();
+    }
+
+    final 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

Review Comment:
   MetaDataCount



##########
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();
+    }
+
+    final 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();
+    }
+
+    final 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.

Review Comment:
   is still some data that remains



##########
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();
+    }
+
+    final 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();
+    }
+
+    final 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
+      final 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() {

Review Comment:
   Maybe deserialize without 's' is better



-- 
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: reviews-unsubscribe@iotdb.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org