THUMarkLau commented on code in PR #12476:
URL: https://github.com/apache/iotdb/pull/12476#discussion_r1630745431


##########
iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/wal/io/WALInputStream.java:
##########
@@ -0,0 +1,284 @@
+/*
+ * 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.db.storageengine.dataregion.wal.io;
+
+import org.apache.tsfile.compress.IUnCompressor;
+import org.apache.tsfile.file.metadata.enums.CompressionType;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.nio.ByteBuffer;
+import java.nio.channels.FileChannel;
+import java.util.Objects;
+
+public class WALInputStream extends InputStream implements AutoCloseable {
+
+  private static final Logger logger = 
LoggerFactory.getLogger(WALInputStream.class);
+  private final FileChannel channel;
+  private final ByteBuffer headerBuffer = ByteBuffer.allocate(Integer.BYTES + 
1);
+  private final ByteBuffer compressedHeader = 
ByteBuffer.allocate(Integer.BYTES);
+  private ByteBuffer dataBuffer = null;
+  private long fileSize;
+  File logFile;
+  private long endOffset = -1;
+
+  enum FileVersion {
+    V1,
+    V2,
+    UNKNOWN
+  };
+
+  FileVersion version;
+
+  public WALInputStream(File logFile) throws IOException {
+    channel = FileChannel.open(logFile.toPath());
+    fileSize = channel.size();
+    analyzeFileVersion();
+    getEndOffset();
+    this.logFile = logFile;
+  }
+
+  private void getEndOffset() throws IOException {
+    if (channel.size() < WALWriter.MAGIC_STRING_BYTES + Integer.BYTES) {
+      endOffset = channel.size();
+      return;
+    }
+    ByteBuffer metadataSizeBuf = ByteBuffer.allocate(Integer.BYTES);
+    long position;
+    try {
+      if (version == FileVersion.V2) {
+        ByteBuffer magicStringBuffer = 
ByteBuffer.allocate(WALWriter.MAGIC_STRING_BYTES);
+        channel.read(magicStringBuffer, channel.size() - 
WALWriter.MAGIC_STRING_BYTES);
+        magicStringBuffer.flip();
+        if (!new 
String(magicStringBuffer.array()).equals(WALWriter.MAGIC_STRING)) {
+          // this is a broken wal file
+          endOffset = channel.size();
+          return;
+        }
+        position = channel.size() - WALWriter.MAGIC_STRING_BYTES - 
Integer.BYTES;
+      } else {
+        ByteBuffer magicStringBuffer =
+            ByteBuffer.allocate(WALWriter.MAGIC_STRING_V1.getBytes().length);
+        channel.read(
+            magicStringBuffer, channel.size() - 
WALWriter.MAGIC_STRING_V1.getBytes().length);
+        magicStringBuffer.flip();
+        if (!new 
String(magicStringBuffer.array()).equals(WALWriter.MAGIC_STRING_V1)) {
+          // this is a broken wal file
+          endOffset = channel.size();
+          return;
+        }
+        position = channel.size() - 
WALWriter.MAGIC_STRING_V1.getBytes().length - Integer.BYTES;
+      }
+      channel.read(metadataSizeBuf, position);
+      metadataSizeBuf.flip();
+      int metadataSize = metadataSizeBuf.getInt();
+      endOffset = channel.size() - WALWriter.MAGIC_STRING_BYTES - 
Integer.BYTES - metadataSize - 1;
+    } finally {
+      channel.position(WALWriter.MAGIC_STRING_BYTES);
+    }
+  }
+
+  private void analyzeFileVersion() throws IOException {
+    if (channel.size() < WALWriter.MAGIC_STRING_BYTES) {
+      version = FileVersion.UNKNOWN;
+      return;
+    }
+    if (isCurrentVersion()) {
+      this.version = FileVersion.V2;
+      return;
+    }
+    this.version = FileVersion.V1;
+  }
+
+  private boolean isCurrentVersion() throws IOException {
+    channel.position(0);
+    ByteBuffer buffer = ByteBuffer.allocate(WALWriter.MAGIC_STRING_BYTES);
+    channel.read(buffer);
+    return new String(buffer.array()).equals(WALWriter.MAGIC_STRING);
+  }
+
+  @Override
+  public int read() throws IOException {
+    if (Objects.isNull(dataBuffer) || dataBuffer.position() >= 
dataBuffer.limit()) {
+      loadNextSegment();
+    }
+    return dataBuffer.get() & 0xFF;

Review Comment:
   <img width="671" alt="image" 
src="https://github.com/apache/iotdb/assets/37140360/3729413f-8d85-4d8f-824c-530add032829";>
   



-- 
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-unsubscr...@iotdb.apache.org

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

Reply via email to