vinothchandar commented on a change in pull request #1176: [HUDI-430] Adding 
InlineFileSystem to support embedding any file format as an InlineFile
URL: https://github.com/apache/incubator-hudi/pull/1176#discussion_r393336968
 
 

 ##########
 File path: 
hudi-utilities/src/test/java/org/apache/hudi/utilities/inline/fs/TestHFileReadWriteFlow.java
 ##########
 @@ -0,0 +1,243 @@
+/*
+ * 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.utilities.inline.fs;
+
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.fs.FSDataInputStream;
+import org.apache.hadoop.fs.FSDataOutputStream;
+import org.apache.hadoop.fs.Path;
+import org.apache.hadoop.hbase.HConstants;
+import org.apache.hadoop.hbase.KeyValue;
+import org.apache.hadoop.hbase.io.hfile.CacheConfig;
+import org.apache.hadoop.hbase.io.hfile.HFile;
+import org.apache.hadoop.hbase.io.hfile.HFileContext;
+import org.apache.hadoop.hbase.io.hfile.HFileContextBuilder;
+import org.apache.hadoop.hbase.io.hfile.HFileScanner;
+import org.apache.hadoop.hbase.util.Bytes;
+import org.junit.After;
+import org.junit.Assert;
+import org.junit.Test;
+
+import java.io.File;
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.Set;
+import java.util.UUID;
+
+import static 
org.apache.hudi.utilities.inline.fs.FileSystemTestUtils.FILE_SCHEME;
+import static org.apache.hudi.utilities.inline.fs.FileSystemTestUtils.RANDOM;
+import static 
org.apache.hudi.utilities.inline.fs.FileSystemTestUtils.getPhantomFile;
+import static 
org.apache.hudi.utilities.inline.fs.FileSystemTestUtils.getRandomOuterInMemPath;
+
+/**
+ * Tests {@link InlineFileSystem} using HFile writer and reader.
+ */
+public class TestHFileReadWriteFlow {
+
+  private final Configuration inMemoryConf;
+  private final Configuration inlineConf;
+  private final int minBlockSize = 1024;
+  private static final String LOCAL_FORMATTER = "%010d";
+  private int maxRows = 100 + RANDOM.nextInt(1000);
+  private Path generatedPath;
+
+  public TestHFileReadWriteFlow() {
+    inMemoryConf = new Configuration();
+    inMemoryConf.set("fs." + InMemoryFileSystem.SCHEME + ".impl", 
InMemoryFileSystem.class.getName());
+    inlineConf = new Configuration();
+    inlineConf.set("fs." + InlineFileSystem.SCHEME + ".impl", 
InlineFileSystem.class.getName());
+  }
+
+  @After
+  public void teardown() throws IOException {
+    if (generatedPath != null) {
+      File filePath = new 
File(generatedPath.toString().substring(generatedPath.toString().indexOf(':') + 
1));
+      if (filePath.exists()) {
+        FileSystemTestUtils.deleteFile(filePath);
+      }
+    }
+  }
+
+  @Test
+  public void testSimpleInlineFileSystem() throws IOException {
+    Path outerInMemFSPath = getRandomOuterInMemPath();
+    Path outerPath = new Path(FILE_SCHEME + 
outerInMemFSPath.toString().substring(outerInMemFSPath.toString().indexOf(':')));
+    generatedPath = outerPath;
+    CacheConfig cacheConf = new CacheConfig(inMemoryConf);
+    FSDataOutputStream fout = createFSOutput(outerInMemFSPath, inMemoryConf);
+    HFileContext meta = new HFileContextBuilder()
+        .withBlockSize(minBlockSize)
+        .build();
+    HFile.Writer writer = HFile.getWriterFactory(inMemoryConf, cacheConf)
+        .withOutputStream(fout)
+        .withFileContext(meta)
+        .withComparator(new KeyValue.KVComparator())
+        .create();
+
+    writeRecords(writer);
+    fout.close();
+
+    byte[] inlineBytes = getBytesToInline(outerInMemFSPath);
+    long startOffset = generateOuterFile(outerPath, inlineBytes);
+
+    long inlineLength = inlineBytes.length;
+
+    // Generate phantom inline file
+    Path inlinePath = getPhantomFile(outerPath, startOffset, inlineLength);
+
+    InlineFileSystem inlineFileSystem = (InlineFileSystem) 
inlinePath.getFileSystem(inlineConf);
+    FSDataInputStream fin = inlineFileSystem.open(inlinePath);
+
+    HFile.Reader reader = HFile.createReader(inlineFileSystem, inlinePath, 
cacheConf, inlineConf);
+    // Load up the index.
+    reader.loadFileInfo();
+    // Get a scanner that caches and that does not use pread.
+    HFileScanner scanner = reader.getScanner(true, false);
+    // Align scanner at start of the file.
+    scanner.seekTo();
+    readAllRecords(scanner);
+
+    Set<Integer> rowIdsToSearch = getRandomValidRowIds(10);
+    for (int rowId : rowIdsToSearch) {
+      Assert.assertTrue("location lookup failed",
+          scanner.seekTo(KeyValue.createKeyValueFromKey(getSomeKey(rowId))) == 
0);
+      // read the key and see if it matches
+      ByteBuffer readKey = scanner.getKey();
+      Assert.assertTrue("seeked key does not match", 
Arrays.equals(getSomeKey(rowId),
+          Bytes.toBytes(readKey)));
+      scanner.seekTo(KeyValue.createKeyValueFromKey(getSomeKey(rowId)));
+      ByteBuffer val1 = scanner.getValue();
+      scanner.seekTo(KeyValue.createKeyValueFromKey(getSomeKey(rowId)));
+      ByteBuffer val2 = scanner.getValue();
+      Assert.assertTrue(Arrays.equals(Bytes.toBytes(val1), 
Bytes.toBytes(val2)));
+    }
+
+    int[] invalidRowIds = {-4, maxRows, maxRows + 1, maxRows + 120, maxRows + 
160, maxRows + 1000};
+    for (int rowId : invalidRowIds) {
+      Assert.assertFalse("location lookup should have failed",
+          scanner.seekTo(KeyValue.createKeyValueFromKey(getSomeKey(rowId))) == 
0);
+    }
+    reader.close();
+    fin.close();
+    outerPath.getFileSystem(inMemoryConf).delete(outerPath, true);
+  }
+
+  private Set<Integer> getRandomValidRowIds(int count) {
+    Set<Integer> rowIds = new HashSet<>();
+    while (rowIds.size() < count) {
+      int index = RANDOM.nextInt(maxRows);
+      if (!rowIds.contains(index)) {
+        rowIds.add(index);
+      }
+    }
+    return rowIds;
+  }
+
+  private byte[] getSomeKey(int rowId) {
+    KeyValue kv = new KeyValue(String.format(LOCAL_FORMATTER, 
Integer.valueOf(rowId)).getBytes(),
+        Bytes.toBytes("family"), Bytes.toBytes("qual"), 
HConstants.LATEST_TIMESTAMP, KeyValue.Type.Put);
+    return kv.getKey();
+  }
+
+  private FSDataOutputStream createFSOutput(Path name, Configuration conf) 
throws IOException {
+    //if (fs.exists(name)) fs.delete(name, true);
 
 Review comment:
   remove?

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to