rdblue commented on code in PR #15241:
URL: https://github.com/apache/iceberg/pull/15241#discussion_r2801663932


##########
core/src/main/java/org/apache/iceberg/SnapshotChanges.java:
##########
@@ -0,0 +1,268 @@
+/*
+ * 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;
+
+import java.io.IOException;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.Queue;
+import java.util.concurrent.ExecutorService;
+import org.apache.iceberg.exceptions.RuntimeIOException;
+import org.apache.iceberg.io.FileIO;
+import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
+import org.apache.iceberg.relocated.com.google.common.collect.ImmutableList;
+import org.apache.iceberg.relocated.com.google.common.collect.Iterables;
+import org.apache.iceberg.relocated.com.google.common.collect.Lists;
+import org.apache.iceberg.relocated.com.google.common.collect.Queues;
+import org.apache.iceberg.util.Tasks;
+
+/**
+ * Helper class for retrieving file changes in a snapshot with caching.
+ *
+ * <p>This class caches the results of file change detection operations, 
making it efficient to
+ * query multiple file change types for the same snapshot. By default, 
manifests are read
+ * sequentially. Use {@link Builder#executeWith(ExecutorService)} to enable 
parallel reading.
+ */
+public class SnapshotChanges {
+  private final Snapshot snapshot;
+  private final FileIO io;
+  private final Map<Integer, PartitionSpec> specsById;
+  private final ExecutorService executorService;
+
+  private List<DataFile> addedDataFiles = null;
+  private List<DataFile> removedDataFiles = null;
+  private List<DeleteFile> addedDeleteFiles = null;
+  private List<DeleteFile> removedDeleteFiles = null;
+
+  private SnapshotChanges(
+      Snapshot snapshot,
+      FileIO io,
+      Map<Integer, PartitionSpec> specsById,
+      ExecutorService executorService) {
+    Preconditions.checkArgument(snapshot != null, "Snapshot cannot be null");
+    Preconditions.checkArgument(io != null, "FileIO cannot be null");
+    Preconditions.checkArgument(specsById != null, "Partition specs cannot be 
null");
+    this.snapshot = snapshot;
+    this.io = io;
+    this.specsById = specsById;
+    this.executorService = executorService;
+  }
+
+  /**
+   * Create a builder for SnapshotChanges.
+   *
+   * @param snapshot the snapshot to detect file changes for
+   * @param io a {@link FileIO} instance used for reading files from storage
+   * @param specsById a map of partition spec IDs to partition specs
+   * @return a new Builder
+   */
+  public static Builder builder(
+      Snapshot snapshot, FileIO io, Map<Integer, PartitionSpec> specsById) {
+    return new Builder(snapshot, io, specsById);
+  }
+
+  /** Returns all data files added to the table in this snapshot */
+  public Iterable<DataFile> addedDataFiles() {
+    if (addedDataFiles == null) {
+      cacheDataFileChanges();
+    }
+    return addedDataFiles;
+  }
+
+  /** Returns all data files removed from the table in this snapshot. */
+  public Iterable<DataFile> removedDataFiles() {
+    if (removedDataFiles == null) {
+      cacheDataFileChanges();
+    }
+    return removedDataFiles;
+  }
+
+  /** Returns all delete files added to the table in this snapshot. */
+  public Iterable<DeleteFile> addedDeleteFiles() {
+    if (addedDeleteFiles == null) {
+      cacheDeleteFileChanges();
+    }
+    return addedDeleteFiles;
+  }
+
+  /** Returns all delete files removed from the table in this snapshot. */
+  public Iterable<DeleteFile> removedDeleteFiles() {
+    if (removedDeleteFiles == null) {
+      cacheDeleteFileChanges();
+    }
+    return removedDeleteFiles;
+  }
+
+  private void cacheDataFileChanges() {
+    List<ManifestFile> changedManifests =
+        Lists.newArrayList(
+            Iterables.filter(
+                Iterables.filter(
+                    snapshot.allManifests(io),
+                    manifest -> manifest.content() == ManifestContent.DATA),
+                manifest -> Objects.equals(manifest.snapshotId(), 
snapshot.snapshotId())));
+
+    Queue<DataFileChanges> fileChangesByManifest = 
Queues.newConcurrentLinkedQueue();
+    Tasks.foreach(changedManifests)
+        .stopOnFailure()
+        .throwFailureWhenFinished()
+        .executeWith(executorService)
+        .run(manifest -> 
fileChangesByManifest.add(readDataFileChanges(manifest)));

Review Comment:
   You may want to use `ParallelIterable` instead of directly building on 
`Tasks`. The benefit of using `ParallelIterable` is that it manages the queue 
internally, doubles the number of running tasks (2x the size of the thread 
pool), handles closing for `CloseableIterable`, and allows you to consume the 
results while threads are running. The main benefit would be higher 
parallelism, but this would also be a little simpler because the original 
implementation that used `ManifestGroup` already supports passing a threadpool 
if you call `planWith`.
   
   Was the intent to avoid complication by replacing `ManifestGroup`?



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

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to