yihua commented on code in PR #12866: URL: https://github.com/apache/hudi/pull/12866#discussion_r1978126330
########## hudi-io/src/main/java/org/apache/hudi/io/hfile/HFileWriterImpl.java: ########## @@ -0,0 +1,259 @@ +/* + * 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.StringUtils; +import org.apache.hudi.common.util.io.ByteBufferBackedInputStream; +import org.apache.hudi.io.ByteArraySeekableDataInputStream; +import org.apache.hudi.io.hfile.protobuf.generated.HFileProtos; +import org.apache.hudi.io.hfile.writer.DataBlock; +import org.apache.hudi.io.hfile.writer.FileInfoBlock; +import org.apache.hudi.io.hfile.writer.IndexBlock; +import org.apache.hudi.io.hfile.writer.MetaBlock; +import org.apache.hudi.io.hfile.writer.MetaIndexBlock; +import org.apache.hudi.io.hfile.writer.RootIndexBlock; + +import com.google.protobuf.ByteString; +import com.google.protobuf.CodedOutputStream; + +import java.io.ByteArrayOutputStream; +import java.io.DataOutputStream; +import java.io.IOException; +import java.nio.ByteBuffer; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * Pure Java implementation of HFile writer (HFile v3 format) for Hudi. + * + * TODO: Known improvements to be done: + * 1. fully compatible with Hbase reader. + * 2. support given compression codec. + */ +public class HFileWriterImpl implements HFileWriter { + private final DataOutputStream outputStream; + private final HFileContext context; + // Flushed data blocks. + private final List<DataBlock> dataBlocks = new ArrayList<>(); + // Meta Info map. + private final Map<String, byte[]> metaInfo = new HashMap<>(); + // Data block under construction. + private DataBlock currentDataBlock; + // Meta block under construction. + private final IndexBlock rootIndexBlock; + private final IndexBlock metaIndexBlock; + private final FileInfoBlock fileInfoBlock; + private long uncompressedBytes; + private long totalUncompressedBytes; + private long currentOffset; + private long loadOnOpenSectionOffset; + private final int blockSize; + + public HFileWriterImpl(HFileContext context, DataOutputStream outputStream) { + this.outputStream = outputStream; + this.context = context; + this.blockSize = this.context.getBlockSize(); + this.uncompressedBytes = 0L; + this.totalUncompressedBytes = 0L; + this.currentOffset = 0L; + this.currentDataBlock = new DataBlock(blockSize); + this.rootIndexBlock = new RootIndexBlock(blockSize); + this.metaIndexBlock = new MetaIndexBlock(blockSize); + this.fileInfoBlock = new FileInfoBlock(blockSize); + initFileInfo(); + } + + // Append a data kv pair. + public void append(byte[] key, byte[] value) throws IOException { + if (uncompressedBytes + key.length + value.length + 9 > blockSize) { + flushCurrentDataBlock(); + uncompressedBytes = 0; + } + currentDataBlock.add(key, value); + int uncompressedKeyValueSize = key.length + value.length; + uncompressedBytes += uncompressedKeyValueSize + 9; + totalUncompressedBytes += uncompressedKeyValueSize + 9; + } + + // Append a metadata kv pair. + public void appendMetaInfo(String name, byte[] value) { + metaInfo.put(name, value); + } + + // Append a file info kv pair. + public void appendFileInfo(String name, byte[] value) { + fileInfoBlock.add(name, value); + } + + @Override + public void close() throws IOException { + flushCurrentDataBlock(); + flushMetaBlocks(); + writeLoadOnOpenSection(); + writeTrailer(); + outputStream.flush(); + outputStream.close(); + } + + private void flushCurrentDataBlock() throws IOException { + if (currentDataBlock.isEmpty()) { + return; + } + // Flush data block. + ByteBuffer blockBuffer = currentDataBlock.serialize(); + // Current block offset. + currentDataBlock.setBlockOffset(currentOffset); + long blockOffset = currentOffset; + writeBuffer(blockBuffer); + dataBlocks.add(currentDataBlock); Review Comment: Keeping all data blocks in memory can cause OOM. Could we incrementally update the metrics instead of keeping data blocks that have already been written? -- 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]
