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


##########
hudi-io/src/main/java/org/apache/hudi/io/hfile/HFileReaderImpl.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.hudi.io.hfile;
+
+import org.apache.hudi.common.util.Option;
+
+import org.apache.hadoop.fs.FSDataInputStream;
+
+import java.io.ByteArrayInputStream;
+import java.io.DataInputStream;
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.util.Map;
+import java.util.TreeMap;
+
+import static org.apache.hudi.io.hfile.HFileBlock.HFILEBLOCK_HEADER_SIZE;
+import static org.apache.hudi.io.hfile.HFileUtils.readMajorVersion;
+
+/**
+ * An implementation a {@link HFileReader}.
+ */
+public class HFileReaderImpl implements HFileReader {
+  private final FSDataInputStream stream;
+  private final long fileSize;
+
+  private final HFilePosition currentPos;
+  private boolean isMetadataInitialized = false;
+  private HFileTrailer trailer;
+  private HFileContext context;
+  private TreeMap<Key, BlockIndexEntry> dataBlockIndexEntryMap;
+  private TreeMap<Key, BlockIndexEntry> metaBlockIndexEntryMap;
+  private HFileInfo fileInfo;
+  private Option<BlockIndexEntry> currentDataBlockEntry;
+  private Option<HFileDataBlock> currentDataBlock;
+
+  public HFileReaderImpl(FSDataInputStream stream, long fileSize) {
+    this.stream = stream;
+    this.fileSize = fileSize;
+    this.currentPos = new HFilePosition();
+    this.currentDataBlockEntry = Option.empty();
+    this.currentDataBlock = Option.empty();
+  }
+
+  @Override
+  public synchronized void initializeMetadata() throws IOException {
+    if (this.isMetadataInitialized) {
+      return;
+    }
+
+    // Read Trailer (serialized in Proto)
+    this.trailer = readTrailer(stream, fileSize);
+    this.context = HFileContext.builder()
+        .compressionCodec(trailer.getCompressionCodec())
+        .build();
+    HFileBlockReader blockReader = new HFileBlockReader(
+        context, stream, trailer.getLoadOnOpenDataOffset(),
+        fileSize - HFileTrailer.getTrailerSize());
+    HFileRootIndexBlock dataIndexBlock =
+        (HFileRootIndexBlock) blockReader.nextBlock(HFileBlockType.ROOT_INDEX);
+    this.dataBlockIndexEntryMap = 
dataIndexBlock.readBlockIndex(trailer.getDataIndexCount(), false);
+    HFileRootIndexBlock metaIndexBlock =
+        (HFileRootIndexBlock) blockReader.nextBlock(HFileBlockType.ROOT_INDEX);
+    this.metaBlockIndexEntryMap = 
metaIndexBlock.readBlockIndex(trailer.getMetaIndexCount(), true);
+    HFileFileInfoBlock fileInfoBlock =
+        (HFileFileInfoBlock) blockReader.nextBlock(HFileBlockType.FILE_INFO);
+    this.fileInfo = fileInfoBlock.readFileInfo();
+    this.isMetadataInitialized = true;
+  }
+
+  @Override
+  public Option<byte[]> getMetaInfo(UTF8StringKey key) throws IOException {
+    initializeMetadata();
+    byte[] bytes = fileInfo.get(key);
+    return bytes != null ? Option.of(bytes) : Option.empty();
+  }
+
+  @Override
+  public Option<ByteBuffer> getMetaBlock(String metaBlockName) throws 
IOException {
+    initializeMetadata();
+    BlockIndexEntry blockIndexEntry = metaBlockIndexEntryMap.get(new 
UTF8StringKey(metaBlockName));
+    if (blockIndexEntry == null) {
+      return Option.empty();
+    }
+    HFileBlockReader blockReader = new HFileBlockReader(
+        context, stream, blockIndexEntry.getOffset(),
+        blockIndexEntry.getOffset() + blockIndexEntry.getSize());
+    HFileMetaBlock block = (HFileMetaBlock) 
blockReader.nextBlock(HFileBlockType.META);
+    return Option.of(block.readContent());
+  }
+
+  @Override
+  public long getNumKeyValueEntries() {
+    try {
+      initializeMetadata();
+      return trailer.getNumKeyValueEntries();
+    } catch (IOException e) {
+      throw new RuntimeException("Cannot read HFile", e);
+    }
+  }
+
+  @Override
+  public int seekTo(Key key) throws IOException {
+    Option<KeyValue> currentKeyValue = getKeyValue();
+    if (!currentKeyValue.isPresent()) {
+      return SEEK_TO_EOF;
+    }
+    int compareCurrent = key.compareTo(currentKeyValue.get().getKey());
+    if (compareCurrent > 0) {
+      if (currentDataBlockEntry.get().getNextBlockKey().isPresent()) {
+        int comparedNextBlockFirstKey =
+            key.compareTo(currentDataBlockEntry.get().getNextBlockKey().get());
+        if (comparedNextBlockFirstKey >= 0) {
+          // Searches the block that may contain the lookup key based the 
starting keys of
+          // all blocks (sorted in the TreeMap of block index entries), using 
binary search.
+          // The result contains the greatest key less than or equal to the 
given key,
+          // or null if there is no such key.
+
+          Map.Entry<Key, BlockIndexEntry> floorEntry = 
dataBlockIndexEntryMap.floorEntry(key);
+          if (floorEntry == null) {
+            // Key smaller than the start key of the first block

Review Comment:
   Actually, the `floorEntry` should never be null.  Throwing an 
`IllegalStateException` here.



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