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


##########
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;
+import java.util.Optional;
+
+/**
+ * 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();
+    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 empty {@link Optional} if the lookup key does not exist.
+   * @throws IOException upon error.
+   */
+  public Optional<KeyValue> seekTo(Key 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
+      return Optional.empty();
+    }
+    BlockIndexEntry blockToRead = blockIndexEntryList.get(rootLevelBlockIndex);
+    HFileBlockReader blockReader = new HFileBlockReader(
+        context, stream, blockToRead.getOffset(), blockToRead.getOffset() + 
(long) blockToRead.getSize());
+    HFileDataBlock dataBlock = (HFileDataBlock) 
blockReader.nextBlock(HFileBlockType.DATA);

Review Comment:
   Disregard the last comment.  I've supported multiple `seekTo` calls from the 
same reader, supporting prefix search too.



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