This is an automated email from the ASF dual-hosted git repository.

szehon pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/iceberg.git


The following commit(s) were added to refs/heads/master by this push:
     new 1bdc699665 Core: Avoid extra getFileStatus call in HadoopInputFile 
(#5864)
1bdc699665 is described below

commit 1bdc699665a37071363d567c4a3299590e155579
Author: Prashant Singh <[email protected]>
AuthorDate: Wed Sep 28 23:01:42 2022 +0530

    Core: Avoid extra getFileStatus call in HadoopInputFile (#5864)
---
 .../java/org/apache/iceberg/hadoop/HadoopInputFile.java    |  8 +++++---
 .../java/org/apache/iceberg/hadoop/HadoopFileIOTest.java   | 14 ++++++++++++++
 2 files changed, 19 insertions(+), 3 deletions(-)

diff --git a/core/src/main/java/org/apache/iceberg/hadoop/HadoopInputFile.java 
b/core/src/main/java/org/apache/iceberg/hadoop/HadoopInputFile.java
index 6c74575f8c..558cac13a0 100644
--- a/core/src/main/java/org/apache/iceberg/hadoop/HadoopInputFile.java
+++ b/core/src/main/java/org/apache/iceberg/hadoop/HadoopInputFile.java
@@ -160,6 +160,8 @@ public class HadoopInputFile implements InputFile, 
NativelyEncryptedFile {
     if (stat == null) {
       try {
         this.stat = fs.getFileStatus(path);
+      } catch (FileNotFoundException e) {
+        throw new NotFoundException(e, "File does not exist: %s", path);
       } catch (IOException e) {
         throw new RuntimeIOException(e, "Failed to get status for file: %s", 
path);
       }
@@ -224,9 +226,9 @@ public class HadoopInputFile implements InputFile, 
NativelyEncryptedFile {
   @Override
   public boolean exists() {
     try {
-      return fs.exists(path);
-    } catch (IOException e) {
-      throw new RuntimeIOException(e, "Failed to check existence for file: 
%s", path);
+      return lazyStat() != null;
+    } catch (NotFoundException e) {
+      return false;
     }
   }
 
diff --git a/core/src/test/java/org/apache/iceberg/hadoop/HadoopFileIOTest.java 
b/core/src/test/java/org/apache/iceberg/hadoop/HadoopFileIOTest.java
index decbe3515f..2cf375592f 100644
--- a/core/src/test/java/org/apache/iceberg/hadoop/HadoopFileIOTest.java
+++ b/core/src/test/java/org/apache/iceberg/hadoop/HadoopFileIOTest.java
@@ -26,6 +26,7 @@ import java.io.IOException;
 import java.io.UncheckedIOException;
 import java.util.List;
 import java.util.Random;
+import java.util.UUID;
 import org.apache.hadoop.conf.Configuration;
 import org.apache.hadoop.fs.FileSystem;
 import org.apache.hadoop.fs.Path;
@@ -78,6 +79,19 @@ public class HadoopFileIOTest {
         totalFiles, 
Streams.stream(hadoopFileIO.listPrefix(parent.toUri().toString())).count());
   }
 
+  @Test
+  public void testFileExists() throws IOException {
+    Path parent = new Path(tempDir.toURI());
+    Path randomFilePath = new Path(parent, "random-file-" + 
UUID.randomUUID().toString());
+    fs.createNewFile(randomFilePath);
+
+    // check existence of the created file
+    
Assert.assertTrue(hadoopFileIO.newInputFile(randomFilePath.toUri().toString()).exists());
+
+    fs.delete(randomFilePath, false);
+    
Assert.assertFalse(hadoopFileIO.newInputFile(randomFilePath.toUri().toString()).exists());
+  }
+
   @Test
   public void testDeletePrefix() {
     Path parent = new Path(tempDir.toURI());

Reply via email to