You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@iotdb.apache.org by ha...@apache.org on 2021/03/27 16:31:45 UTC

[iotdb] branch master updated: [ISSUE-2905] Fix Files.deleteIfExists() doesn't work for HDFS file (#2912)

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

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


The following commit(s) were added to refs/heads/master by this push:
     new b28fa65  [ISSUE-2905] Fix Files.deleteIfExists() doesn't work for HDFS file (#2912)
b28fa65 is described below

commit b28fa65cafc993b11611aec81fbea0e68b5e9951
Author: Zesong Sun <sz...@mails.tsinghua.edu.cn>
AuthorDate: Sun Mar 28 00:31:17 2021 +0800

    [ISSUE-2905] Fix Files.deleteIfExists() doesn't work for HDFS file (#2912)
    
    * Fix Files.deleteIfExists() doesn't work for HDFS file
    
    * Add FSFactory.deleteIfExists()
---
 .../org/apache/iotdb/db/engine/storagegroup/TsFileResource.java  | 9 ++++-----
 .../org/apache/iotdb/tsfile/fileSystem/fsFactory/FSFactory.java  | 8 ++++++++
 .../apache/iotdb/tsfile/fileSystem/fsFactory/HDFSFactory.java    | 5 +++++
 .../apache/iotdb/tsfile/fileSystem/fsFactory/LocalFSFactory.java | 6 ++++++
 4 files changed, 23 insertions(+), 5 deletions(-)

diff --git a/server/src/main/java/org/apache/iotdb/db/engine/storagegroup/TsFileResource.java b/server/src/main/java/org/apache/iotdb/db/engine/storagegroup/TsFileResource.java
index 8a9cd3a..407dc9c 100644
--- a/server/src/main/java/org/apache/iotdb/db/engine/storagegroup/TsFileResource.java
+++ b/server/src/main/java/org/apache/iotdb/db/engine/storagegroup/TsFileResource.java
@@ -254,7 +254,7 @@ public class TsFileResource {
     }
     File src = fsFactory.getFile(file + RESOURCE_SUFFIX + TEMP_SUFFIX);
     File dest = fsFactory.getFile(file + RESOURCE_SUFFIX);
-    Files.deleteIfExists(dest.toPath());
+    fsFactory.deleteIfExists(dest);
     fsFactory.moveFile(src, dest);
   }
 
@@ -459,14 +459,13 @@ public class TsFileResource {
   /** Remove the data file, its resource file, and its modification file physically. */
   public void remove() {
     try {
-      Files.deleteIfExists(file.toPath());
+      fsFactory.deleteIfExists(file);
     } catch (IOException e) {
       logger.error("TsFile {} cannot be deleted: {}", file, e.getMessage());
     }
     removeResourceFile();
     try {
-      Files.deleteIfExists(
-          fsFactory.getFile(file.getPath() + ModificationFile.FILE_SUFFIX).toPath());
+      fsFactory.deleteIfExists(fsFactory.getFile(file.getPath() + ModificationFile.FILE_SUFFIX));
     } catch (IOException e) {
       logger.error("ModificationFile {} cannot be deleted: {}", file, e.getMessage());
     }
@@ -474,7 +473,7 @@ public class TsFileResource {
 
   public void removeResourceFile() {
     try {
-      Files.deleteIfExists(fsFactory.getFile(file.getPath() + RESOURCE_SUFFIX).toPath());
+      fsFactory.deleteIfExists(fsFactory.getFile(file.getPath() + RESOURCE_SUFFIX));
     } catch (IOException e) {
       logger.error("TsFileResource {} cannot be deleted: {}", file, e.getMessage());
     }
diff --git a/tsfile/src/main/java/org/apache/iotdb/tsfile/fileSystem/fsFactory/FSFactory.java b/tsfile/src/main/java/org/apache/iotdb/tsfile/fileSystem/fsFactory/FSFactory.java
index fb9f2bb..804e880 100644
--- a/tsfile/src/main/java/org/apache/iotdb/tsfile/fileSystem/fsFactory/FSFactory.java
+++ b/tsfile/src/main/java/org/apache/iotdb/tsfile/fileSystem/fsFactory/FSFactory.java
@@ -24,6 +24,7 @@ import java.io.BufferedOutputStream;
 import java.io.BufferedReader;
 import java.io.BufferedWriter;
 import java.io.File;
+import java.io.IOException;
 import java.net.URI;
 
 public interface FSFactory {
@@ -128,4 +129,11 @@ public interface FSFactory {
    * @return list of files
    */
   File[] listFilesByPrefix(String fileFolder, String prefix);
+
+  /**
+   * delete the file if it exists
+   *
+   * @param file local file or HDFS file
+   */
+  boolean deleteIfExists(File file) throws IOException;
 }
diff --git a/tsfile/src/main/java/org/apache/iotdb/tsfile/fileSystem/fsFactory/HDFSFactory.java b/tsfile/src/main/java/org/apache/iotdb/tsfile/fileSystem/fsFactory/HDFSFactory.java
index d42277e..b1197bf 100644
--- a/tsfile/src/main/java/org/apache/iotdb/tsfile/fileSystem/fsFactory/HDFSFactory.java
+++ b/tsfile/src/main/java/org/apache/iotdb/tsfile/fileSystem/fsFactory/HDFSFactory.java
@@ -229,4 +229,9 @@ public class HDFSFactory implements FSFactory {
       return null;
     }
   }
+
+  @Override
+  public boolean deleteIfExists(File file) {
+    return file.delete();
+  }
 }
diff --git a/tsfile/src/main/java/org/apache/iotdb/tsfile/fileSystem/fsFactory/LocalFSFactory.java b/tsfile/src/main/java/org/apache/iotdb/tsfile/fileSystem/fsFactory/LocalFSFactory.java
index 779b66e..365ded1 100644
--- a/tsfile/src/main/java/org/apache/iotdb/tsfile/fileSystem/fsFactory/LocalFSFactory.java
+++ b/tsfile/src/main/java/org/apache/iotdb/tsfile/fileSystem/fsFactory/LocalFSFactory.java
@@ -34,6 +34,7 @@ import java.io.FileReader;
 import java.io.FileWriter;
 import java.io.IOException;
 import java.net.URI;
+import java.nio.file.Files;
 
 public class LocalFSFactory implements FSFactory {
 
@@ -131,4 +132,9 @@ public class LocalFSFactory implements FSFactory {
   public File[] listFilesByPrefix(String fileFolder, String prefix) {
     return new File(fileFolder).listFiles(file -> file.getName().startsWith(prefix));
   }
+
+  @Override
+  public boolean deleteIfExists(File file) throws IOException {
+    return Files.deleteIfExists(file.toPath());
+  }
 }