yihua commented on code in PR #10241: URL: https://github.com/apache/hudi/pull/10241#discussion_r1456562550
########## 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 = Review Comment: This is already happening - each `HFileBlockReader` instance reads the whole byte range fully before processing. In this case, all three blocks are in the range of `[trailer.getLoadOnOpenDataOffset(), fileSize - HFileTrailer.getTrailerSize())`, so bytes are read in one RPC call, and then the parsing of each block happens. ``` public HFileBlock nextBlock(HFileBlockType expectedBlockType) throws IOException { if (offset >= byteBuff.length) { throw new EOFException("No more data to read"); } if (!isReadFully) { // Full range of bytes are read fully into a byte array stream.seek(streamStartOffset); stream.readFully(byteBuff); isReadFully = true; } ... ``` -- 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]
