beam-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dhalp...@apache.org
Subject [2/4] beam git commit: FileSystems: make tolerant of and more efficient for empty lists
Date Wed, 26 Apr 2017 19:37:40 GMT
FileSystems: make tolerant of and more efficient for empty lists


Project: http://git-wip-us.apache.org/repos/asf/beam/repo
Commit: http://git-wip-us.apache.org/repos/asf/beam/commit/bdee44f1
Tree: http://git-wip-us.apache.org/repos/asf/beam/tree/bdee44f1
Diff: http://git-wip-us.apache.org/repos/asf/beam/diff/bdee44f1

Branch: refs/heads/master
Commit: bdee44f1a675591b6a67883217e6669c5bb7b3b7
Parents: e07ba68
Author: Dan Halperin <dhalperi@google.com>
Authored: Tue Apr 25 20:48:23 2017 -0700
Committer: Dan Halperin <dhalperi@google.com>
Committed: Wed Apr 26 12:27:42 2017 -0700

----------------------------------------------------------------------
 .../org/apache/beam/sdk/io/FileSystems.java     | 55 +++++++++++++-------
 1 file changed, 36 insertions(+), 19 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/beam/blob/bdee44f1/sdks/java/core/src/main/java/org/apache/beam/sdk/io/FileSystems.java
----------------------------------------------------------------------
diff --git a/sdks/java/core/src/main/java/org/apache/beam/sdk/io/FileSystems.java b/sdks/java/core/src/main/java/org/apache/beam/sdk/io/FileSystems.java
index aa247c3..0b50070 100644
--- a/sdks/java/core/src/main/java/org/apache/beam/sdk/io/FileSystems.java
+++ b/sdks/java/core/src/main/java/org/apache/beam/sdk/io/FileSystems.java
@@ -214,22 +214,22 @@ public class FileSystems {
    * @param destResourceIds the references of the destination resources
    */
   public static void copy(
-      List<ResourceId> srcResourceIds,
-      List<ResourceId> destResourceIds,
-      MoveOptions... moveOptions) throws IOException {
-    validateOnlyScheme(srcResourceIds, destResourceIds);
+      List<ResourceId> srcResourceIds, List<ResourceId> destResourceIds, MoveOptions...
moveOptions)
+      throws IOException {
+    validateSrcDestLists(srcResourceIds, destResourceIds);
+    if (srcResourceIds.isEmpty()) {
+      // Short-circuit.
+      return;
+    }
 
-    List<ResourceId> srcToCopy;
-    List<ResourceId> destToCopy;
+    List<ResourceId> srcToCopy = srcResourceIds;
+    List<ResourceId> destToCopy = destResourceIds;
     if (Sets.newHashSet(moveOptions).contains(
         MoveOptions.StandardMoveOptions.IGNORE_MISSING_FILES)) {
       KV<List<ResourceId>, List<ResourceId>> existings =
           filterMissingFiles(srcResourceIds, destResourceIds);
       srcToCopy = existings.getKey();
       destToCopy = existings.getValue();
-    } else {
-      srcToCopy = srcResourceIds;
-      destToCopy = destResourceIds;
     }
     if (srcToCopy.isEmpty()) {
       return;
@@ -252,22 +252,22 @@ public class FileSystems {
    * @param destResourceIds the references of the destination resources
    */
   public static void rename(
-      List<ResourceId> srcResourceIds,
-      List<ResourceId> destResourceIds,
-      MoveOptions... moveOptions) throws IOException {
-    validateOnlyScheme(srcResourceIds, destResourceIds);
-    List<ResourceId> srcToRename;
-    List<ResourceId> destToRename;
+      List<ResourceId> srcResourceIds, List<ResourceId> destResourceIds, MoveOptions...
moveOptions)
+      throws IOException {
+    validateSrcDestLists(srcResourceIds, destResourceIds);
+    if (srcResourceIds.isEmpty()) {
+      // Short-circuit.
+      return;
+    }
 
+    List<ResourceId> srcToRename = srcResourceIds;
+    List<ResourceId> destToRename = destResourceIds;
     if (Sets.newHashSet(moveOptions).contains(
         MoveOptions.StandardMoveOptions.IGNORE_MISSING_FILES)) {
       KV<List<ResourceId>, List<ResourceId>> existings =
           filterMissingFiles(srcResourceIds, destResourceIds);
       srcToRename = existings.getKey();
       destToRename = existings.getValue();
-    } else {
-      srcToRename = srcResourceIds;
-      destToRename = destResourceIds;
     }
     if (srcToRename.isEmpty()) {
       return;
@@ -288,6 +288,11 @@ public class FileSystems {
    */
   public static void delete(
       Collection<ResourceId> resourceIds, MoveOptions... moveOptions) throws IOException
{
+    if (resourceIds.isEmpty()) {
+      // Short-circuit.
+      return;
+    }
+
     Collection<ResourceId> resourceIdsToDelete;
     if (Sets.newHashSet(moveOptions).contains(
         MoveOptions.StandardMoveOptions.IGNORE_MISSING_FILES)) {
@@ -329,6 +334,12 @@ public class FileSystems {
 
   private static KV<List<ResourceId>, List<ResourceId>> filterMissingFiles(
       List<ResourceId> srcResourceIds, List<ResourceId> destResourceIds) throws
IOException {
+    validateSrcDestLists(srcResourceIds, destResourceIds);
+    if (srcResourceIds.isEmpty()) {
+      // Short-circuit.
+      return KV.of(Collections.<ResourceId>emptyList(), Collections.<ResourceId>emptyList());
+    }
+
     List<ResourceId> srcToHandle = new ArrayList<>();
     List<ResourceId> destToHandle = new ArrayList<>();
 
@@ -342,13 +353,19 @@ public class FileSystems {
     return KV.of(srcToHandle, destToHandle);
   }
 
-  private static void validateOnlyScheme(
+  private static void validateSrcDestLists(
       List<ResourceId> srcResourceIds, List<ResourceId> destResourceIds) {
     checkArgument(
         srcResourceIds.size() == destResourceIds.size(),
         "Number of source resource ids %s must equal number of destination resource ids %s",
         srcResourceIds.size(),
         destResourceIds.size());
+
+    if (srcResourceIds.isEmpty()) {
+      // nothing more to validate.
+      return;
+    }
+
     Set<String> schemes = FluentIterable.from(srcResourceIds)
         .append(destResourceIds)
         .transform(new Function<ResourceId, String>() {


Mime
View raw message