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


##########
hudi-io/src/main/java/org/apache/hudi/io/hfile/HFileWriterImpl.java:
##########
@@ -0,0 +1,233 @@
+/*
+ * 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.io.hfile.protobuf.generated.HFileProtos;
+
+import com.google.protobuf.ByteString;
+
+import java.io.IOException;
+import java.io.OutputStream;
+import java.nio.ByteBuffer;
+import java.nio.charset.StandardCharsets;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.Map;
+
+import static org.apache.hudi.io.hfile.DataSize.SIZEOF_INT16;
+import static 
org.apache.hudi.io.hfile.HFileBlock.getVariableLengthEncodedBytes;
+import static org.apache.hudi.io.hfile.HFileBlockType.TRAILER;
+import static org.apache.hudi.io.hfile.HFileInfo.LAST_KEY;
+import static org.apache.hudi.io.hfile.HFileInfo.MAX_MVCC_TS_KEY;
+import static org.apache.hudi.io.hfile.HFileTrailer.TRAILER_SIZE;
+
+/**
+ * Pure Java implementation of HFile writer (HFile v3 format) for Hudi.
+ */
+public class HFileWriterImpl implements HFileWriter {
+  private static final String COMPARATOR_CLASS_NAME
+      = "org.apache.hudi.io.storage.HoodieHBaseKVComparator";
+  private static final byte HFILE_VERSION = (byte) 3;
+  private final OutputStream outputStream;
+  private final HFileContext context;
+  // Meta Info map.
+  private final Map<String, byte[]> metaInfo = new HashMap<>();
+  // Data block under construction.
+  private HFileDataBlock currentDataBlock;
+  // Meta block under construction.
+  private final HFileRootIndexBlock rootIndexBlock;
+  private final HFileMetaIndexBlock metaIndexBlock;
+  private final HFileFileInfoBlock fileInfoBlock;
+  private long uncompressedDataBlockBytes;
+  private long totalUncompressedDataBlockBytes;
+  private long currentOffset;
+  private long loadOnOpenSectionOffset;
+  private final int blockSize;
+
+  // Variables used to record necessary information to reduce
+  // the memory usage.
+  private byte[] lastKey = new byte[0];
+  private long firstDataBlockOffset = -1;
+  private long lastDataBlockOffset;
+  private long totalNumberOfRecords = 0;
+
+  public HFileWriterImpl(HFileContext context, OutputStream outputStream) {
+    this.outputStream = outputStream;
+    this.context = context;
+    this.blockSize = this.context.getBlockSize();
+    this.uncompressedDataBlockBytes = 0L;
+    this.totalUncompressedDataBlockBytes = 0L;
+    this.currentOffset = 0L;
+    this.currentDataBlock = HFileDataBlock.createDataBlockToWrite(context, 
-1L);
+    this.rootIndexBlock = 
HFileRootIndexBlock.createRootIndexBlockToWrite(context);
+    this.metaIndexBlock = 
HFileMetaIndexBlock.createMetaIndexBlockToWrite(context);
+    this.fileInfoBlock = 
HFileFileInfoBlock.createFileInfoBlockToWrite(context);
+    initFileInfo();
+  }
+
+  // Append a data kv pair.
+  public void append(String key, byte[] value) throws IOException {
+    byte[] keyBytes = StringUtils.getUTF8Bytes(key);
+    lastKey = keyBytes;
+    // Records with the same key must be put into the same block.
+    // Here 9 = 4 bytes of key length + 4 bytes of value length + 1 byte MVCC.
+    if (!Arrays.equals(currentDataBlock.getLastKeyContent(), keyBytes)

Review Comment:
   Since this compares the key to add and last key, it can be extended to check 
ordering without performance penalty.  Let's do that in a follow-up PR.



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