Browse Source

HADOOP-19196. Allow base path to be deleted as well using Bulk Delete. (#6872)

Contributed by: Mukund Thakur
Mukund Thakur 10 months ago
parent
commit
06dd3bfee8

+ 4 - 4
hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/BulkDeleteUtils.java

@@ -48,14 +48,14 @@ public final class BulkDeleteUtils {
   }
 
   /**
-   * Check if a path is under a base path.
+   * Check if a given path is the base path or under the base path.
    * @param p path to check.
    * @param basePath base path.
-   * @return true if the path is under the base path.
+   * @return true if the given path is the base path or under the base path.
    */
   public static boolean validatePathIsUnderParent(Path p, Path basePath) {
-    while (p.getParent() != null) {
-      if (p.getParent().equals(basePath)) {
+    while (p != null) {
+      if (p.equals(basePath)) {
         return true;
       }
       p = p.getParent();

+ 1 - 0
hadoop-common-project/hadoop-common/src/site/markdown/filesystem/bulkdelete.md

@@ -23,6 +23,7 @@ in an object store or filesystem.
 
 * An API for submitting a list of paths to delete.
 * This list must be no larger than the "page size" supported by the client; This size is also exposed as a method.
+* This list must not have any path outside the base path.
 * Triggers a request to delete files at the specific paths.
 * Returns a list of which paths were reported as delete failures by the store.
 * Does not consider a nonexistent file to be a failure.

+ 12 - 0
hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/contract/AbstractContractBulkDeleteTest.java

@@ -20,6 +20,7 @@ package org.apache.hadoop.fs.contract;
 
 import java.io.IOException;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.List;
 import java.util.Map;
 
@@ -167,6 +168,17 @@ public abstract class AbstractContractBulkDeleteTest extends AbstractFSContractT
             () -> bulkDelete_delete(getFileSystem(), basePath, paths));
   }
 
+  /**
+   * We should be able to delete the base path itself
+   * using bulk delete operation.
+   */
+  @Test
+  public void testDeletePathSameAsBasePath() throws Exception {
+    assertSuccessfulBulkDelete(bulkDelete_delete(getFileSystem(),
+            basePath,
+            Arrays.asList(basePath)));
+  }
+
   /**
    * This test should fail as path is not absolute.
    */