linliu-code commented on code in PR #12866:
URL: https://github.com/apache/hudi/pull/12866#discussion_r1992466754


##########
hudi-io/src/test/java/org/apache/hudi/io/hfile/TestHFileWriter.java:
##########
@@ -0,0 +1,275 @@
+/*
+ * 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.io.ByteBufferBackedInputStream;
+import org.apache.hudi.io.ByteArraySeekableDataInputStream;
+
+import org.junit.jupiter.api.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.DataOutputStream;
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.nio.channels.FileChannel;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.nio.file.StandardOpenOption;
+import java.util.Arrays;
+
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+public class TestHFileWriter {
+  private static final Logger LOG = 
LoggerFactory.getLogger(TestHFileWriter.class);
+  private static final String TEST_FILE = "test.hfile";
+  private static final HFileContext CONTEXT = HFileContext.builder().build();
+
+  @Test
+  void testOverflow() throws Exception {
+    try {
+      // 1. Write data.
+      writeTestFile();
+
+      // 2. Validate file size.
+      validateHFileSize();
+
+      // 3. Validate file structure.
+      validateHFileStructure();
+
+      // 4. Validate consistency with HFileReader.
+      validateConsistencyWithHFileReader();
+
+      LOG.info("All validations passed!");
+    } finally {
+      Files.deleteIfExists(Paths.get(TEST_FILE));
+    }
+  }
+
+  @Test
+  void testSameKeyLocation() throws IOException {
+    // 50 bytes for data part limit.
+    HFileContext context = new HFileContext.Builder().blockSize(50).build();
+    String testFile = "test_data.hfile";
+    try (DataOutputStream outputStream =
+             new DataOutputStream(Files.newOutputStream(Paths.get(testFile)));
+        HFileWriter writer = new HFileWriterImpl(context, outputStream)) {
+      writer.append("key1", "value1".getBytes());
+      writer.append("key1", "value2".getBytes());
+      writer.append("key1", "value3".getBytes());
+      writer.append("key2", "value4".getBytes());
+    } catch (IOException e) {
+      throw new RuntimeException(e);
+    }
+
+    // Validate.
+    try (FileChannel channel = FileChannel.open(Paths.get(testFile), 
StandardOpenOption.READ)) {
+      ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, 
channel.size());
+      // Point to the first data block.
+      buf.position(0);
+
+      // Validate magic.
+      byte[] dataBlockMagic = new byte[8];
+      buf.get(dataBlockMagic);
+      assertArrayEquals("DATABLK*".getBytes(), dataBlockMagic);
+
+      // Skip header.
+      buf.position(buf.position() + 25);
+      // Validate data.
+      // Each record is about 20 bytes.
+      // The first block should contain 3 pairs, and the second one contains 1.
+      validateKeyValue(buf, "key1", "value1");
+      validateKeyValue(buf, "key1", "value2");
+      validateKeyValue(buf, "key1", "value3");
+
+      // Validate magic.
+      buf.get(dataBlockMagic);
+      assertArrayEquals("DATABLK*".getBytes(), dataBlockMagic);
+
+      // Skip header.
+      buf.position(buf.position() + 25);
+      // Validate data.
+      validateKeyValue(buf, "key2", "value4");
+    } finally {
+      Files.deleteIfExists(Paths.get(testFile));
+    }
+  }
+
+  @Test
+  void testUniqueKeyLocation() throws IOException {
+    // 50 bytes for data part limit.
+    HFileContext context = new HFileContext.Builder().blockSize(50).build();
+    String testFile = "test_data.hfile";
+    try (DataOutputStream outputStream =
+             new DataOutputStream(Files.newOutputStream(Paths.get(testFile)));
+         HFileWriter writer = new HFileWriterImpl(context, outputStream)) {
+      writer.append("key1", "value1".getBytes());

Review Comment:
   Add more keys.



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