You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@iceberg.apache.org by GitBox <gi...@apache.org> on 2022/08/02 17:33:42 UTC

[GitHub] [iceberg] kbendick commented on a diff in pull request #4918: Core: Add InMemoryFileIO implementation

kbendick commented on code in PR #4918:
URL: https://github.com/apache/iceberg/pull/4918#discussion_r912162163


##########
core/src/test/java/org/apache/iceberg/io/inmemory/InMemoryFileIOTest.java:
##########
@@ -0,0 +1,256 @@
+/*
+ * 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.iceberg.io.inmemory;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.nio.ByteBuffer;
+import java.nio.charset.StandardCharsets;
+import java.util.stream.IntStream;
+import org.apache.iceberg.io.InputFile;
+import org.apache.iceberg.io.OutputFile;
+import org.apache.iceberg.io.PositionOutputStream;
+import org.apache.iceberg.io.SeekableInputStream;
+import org.assertj.core.api.Assertions;
+import org.assertj.core.data.Offset;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import static java.nio.charset.StandardCharsets.UTF_8;
+
+public class InMemoryFileIOTest {
+
+  private InMemoryFileIO fileIO;
+  private InMemoryFileStore store;
+
+  @BeforeEach
+  public void before() {
+    fileIO = new InMemoryFileIO();
+    store = fileIO.getStore();
+  }
+
+  @Test
+  public void testGetStore() {
+    Assertions.assertThat(store).isNotNull();
+  }
+
+  @Test
+  public void testDeleteFile() {
+    Assertions.assertThat(store.exists("file1")).isFalse();
+
+    // Create the file
+    store.put("file1", ByteBuffer.wrap(new byte[0]));
+    Assertions.assertThat(store.exists("file1")).isTrue();
+
+    // Delete the file
+    fileIO.deleteFile("file1");
+    // Verify that the file has been deleted
+    Assertions.assertThat(store.exists("file1")).isFalse();
+
+    Assertions.assertThatThrownBy(() -> fileIO.deleteFile("nonExisting"))
+        .isInstanceOf(IllegalArgumentException.class)
+        .hasMessage("File at location 'nonExisting' does not exist");
+  }
+
+  @Test
+  public void testNewInputFile() throws IOException {
+    String fileName = "file1";
+
+    store.put(fileName, ByteBuffer.wrap("data1".getBytes(UTF_8)));
+
+    InputFile inputFile = fileIO.newInputFile(fileName);
+    Assertions.assertThat(inputFile).isNotNull();
+
+    Assertions.assertThat(inputFile.exists()).isTrue();
+    Assertions.assertThat(inputFile.location()).isEqualTo(fileName);
+    Assertions.assertThat(inputFile.getLength()).isEqualTo(5);
+
+    try (SeekableInputStream inputStream = inputFile.newStream()) {
+      Assertions.assertThat(inputStream).isNotNull();
+
+      // Test read()
+      Assertions.assertThat(inputStream.getPos()).isEqualTo(0);
+      Assertions.assertThat(inputStream.read()).isEqualTo('d');
+
+      // Test read(byte[], index, len)
+      byte[] dataRead = new byte[3];
+      Assertions.assertThat(inputStream.read(dataRead, 0, 2)).isEqualTo(2);
+      Assertions.assertThat(new String(dataRead, 0, 2, UTF_8)).isEqualTo("at");
+    }
+  }
+
+  @Test
+  public void testSeek() throws IOException {
+    String fileName = "file1";
+
+    // Number of rows
+    int numRowsInChunk = 10;
+
+    // Emulate parquet file structure
+    ByteBuffer buffer = ByteBuffer.allocate(1024);
+    // Add magic number
+    buffer.put("par1".getBytes(UTF_8));
+    // Add column chunk 1 for int32
+    IntStream.range(0, numRowsInChunk).forEach(buffer::putInt);
+    // Add column chunk 2 for float64
+    IntStream.range(0, numRowsInChunk).forEach(buffer::putDouble);
+    // Add metadata footer (index to the start of chunks)
+    buffer.putInt(4);
+    buffer.putInt(4 + numRowsInChunk * 4);
+    // Add footer size
+    buffer.putInt(8);
+    // Add magic number
+    buffer.put("par1".getBytes(UTF_8));
+
+    // Put the data in the store
+    buffer.flip();
+    store.put(fileName, buffer);
+
+    // magic number, chunk1, chunk2, footer, footer size, magic number
+    int expectedFileSize = 4 + numRowsInChunk * 4 + numRowsInChunk * 8 + 8 + 4 + 4;

Review Comment:
   Nit: Some of these might be better off as constants. But since it's all in the same method and well defined above I'm ok either way.



-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org