RussellSpitzer commented on a change in pull request #2591:
URL: https://github.com/apache/iceberg/pull/2591#discussion_r632237292



##########
File path: 
spark/src/main/java/org/apache/iceberg/spark/actions/BaseRewriteDataFilesSparkAction.java
##########
@@ -0,0 +1,321 @@
+/*
+ * 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.iceberg.spark.actions;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.UUID;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentLinkedQueue;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+import org.apache.iceberg.DataFile;
+import org.apache.iceberg.FileScanTask;
+import org.apache.iceberg.StructLike;
+import org.apache.iceberg.Table;
+import org.apache.iceberg.actions.RewriteDataFiles;
+import org.apache.iceberg.actions.RewriteStrategy;
+import org.apache.iceberg.expressions.Expression;
+import org.apache.iceberg.expressions.Expressions;
+import org.apache.iceberg.io.CloseableIterable;
+import org.apache.iceberg.relocated.com.google.common.collect.ImmutableList;
+import org.apache.iceberg.relocated.com.google.common.collect.Sets;
+import org.apache.iceberg.relocated.com.google.common.collect.Streams;
+import 
org.apache.iceberg.relocated.com.google.common.util.concurrent.ThreadFactoryBuilder;
+import org.apache.iceberg.util.Pair;
+import org.apache.iceberg.util.PropertyUtil;
+import org.apache.iceberg.util.Tasks;
+import org.apache.spark.sql.SparkSession;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+abstract class BaseRewriteDataFilesSparkAction
+    extends BaseSnapshotUpdateSparkAction<RewriteDataFiles, 
RewriteDataFiles.Result> implements RewriteDataFiles {
+
+  private static final Logger LOG = 
LoggerFactory.getLogger(BaseRewriteDataFilesSparkAction.class);
+
+  private RewriteDataFiles.Strategy strategyType = Strategy.BINPACK;
+  private Table table;
+  private Expression filter = Expressions.alwaysTrue();
+
+  /**
+   * returns the Spark version specific strategy
+   */
+  protected abstract RewriteStrategy rewriteStrategy(Strategy type);
+
+  protected BaseRewriteDataFilesSparkAction(SparkSession spark, Table table) {
+    super(spark);
+    this.table = table;
+  }
+
+  @Override
+  public RewriteDataFiles strategy(Strategy type) {
+    strategyType = type;
+    return this;
+  }
+
+  @Override
+  public RewriteDataFiles filter(Expression expression) {
+    filter = Expressions.and(filter, expression);
+    return this;
+  }
+
+  @Override
+  public Result execute() {
+    RewriteStrategy strategy =  
rewriteStrategy(strategyType).options(this.options());
+
+    CloseableIterable<FileScanTask> files = table.newScan()
+        .filter(filter)
+        .ignoreResiduals()
+        .planFiles();
+
+    Map<StructLike, List<FileScanTask>> filesByPartition =
+        Streams.stream(files).collect(Collectors.groupingBy(task -> 
task.file().partition()));
+
+    Map<StructLike, List<List<FileScanTask>>> fileGroupsByPartition =
+        filesByPartition.entrySet().stream().collect(Collectors.toMap(e -> 
e.getKey(), e -> {
+          Iterable<FileScanTask> filtered = 
strategy.selectFilesToRewrite(e.getValue());
+          Iterable<List<FileScanTask>> groupedTasks = 
strategy.planFileGroups(filtered);

Review comment:
       I pulled out this and the group into a single function, I think that 
helps, but take a look in the new version




-- 
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



---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org
For additional commands, e-mail: issues-h...@iceberg.apache.org

Reply via email to