yihua commented on code in PR #10241:
URL: https://github.com/apache/hudi/pull/10241#discussion_r1447808569


##########
hudi-io/src/main/java/org/apache/hudi/io/hfile/HFileReader.java:
##########
@@ -0,0 +1,178 @@
+/*
+ * 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.hudi.io.hfile;
+
+import org.apache.hadoop.fs.FSDataInputStream;
+
+import java.io.ByteArrayInputStream;
+import java.io.DataInputStream;
+import java.io.IOException;
+import java.util.Collections;
+import java.util.List;
+
+/**
+ * A reader reading a HFile.
+ */
+public class HFileReader {
+  private final FSDataInputStream stream;
+  private final long fileSize;
+  private boolean isMetadataInitialized = false;
+  private HFileContext context;
+  private List<BlockIndexEntry> blockIndexEntryList;
+  private HFileBlock metaIndexBlock;
+  private HFileBlock fileInfoBlock;
+
+  public HFileReader(FSDataInputStream stream, long fileSize) {
+    this.stream = stream;
+    this.fileSize = fileSize;
+  }
+
+  /**
+   * Initializes the metadata by reading the "Load-on-open" section.
+   *
+   * @throws IOException upon error.
+   */
+  public void initializeMetadata() throws IOException {
+    assert !this.isMetadataInitialized;
+
+    // Read Trailer (serialized in Proto)
+    HFileTrailer trailer = readTrailer(stream, fileSize);
+    this.context = HFileContext.builder()
+        .compressAlgo(trailer.getCompressionCodec())
+        .build();
+    // At this point, HFileContext is not known yet until we parse the trailer
+    HFileBlockReader blockReader = new HFileBlockReader(
+        context, stream, trailer.getLoadOnOpenDataOffset(), fileSize - 
HFileTrailer.getTrailerSize());
+    HFileRootIndexBlock dataIndexBlock =
+        (HFileRootIndexBlock) blockReader.nextBlock(HFileBlockType.ROOT_INDEX);
+    this.blockIndexEntryList = 
dataIndexBlock.readDataIndex(trailer.getDataIndexCount());
+    this.metaIndexBlock = blockReader.nextBlock(HFileBlockType.ROOT_INDEX);
+    this.fileInfoBlock = blockReader.nextBlock(HFileBlockType.FILE_INFO);
+
+    this.isMetadataInitialized = true;
+  }
+
+  /**
+   * Seeks to the key to look up.
+   *
+   * @param key Key to look up.
+   * @return The {@link KeyValue} instance in the block that contains the 
exact same key as the
+   * lookup key; or {@link null} if the lookup key does not exist.
+   * @throws IOException upon error.
+   */
+  public KeyValue seekTo(KeyValue key) throws IOException {
+    BlockIndexEntry lookUpKey = new BlockIndexEntry(key, -1, -1);
+    int rootLevelBlockIndex = searchBlockByKey(lookUpKey);
+    if (rootLevelBlockIndex < 0) {
+      // Key smaller than the start key of the first block

Review Comment:
   Last key is checked now.



-- 
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: commits-unsubscr...@hudi.apache.org

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

Reply via email to