rdblue commented on code in PR #15241: URL: https://github.com/apache/iceberg/pull/15241#discussion_r2801624399
########## 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 SnapshotFileChanges { + 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 SnapshotFileChanges( + 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 SnapshotFileChanges. + * + * @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() { Review Comment: `Iterable` is the right class. `Iterable` provides something that can be iterated over multiple times, each time producing a new `Iterator`. That gives us the most flexibility, instead of forcing the caller to go back to this API to process removed data files another time -- the result of this method can be passed around and used without exposing this. `Iterable` also does not need to load anything into memory. A `List` is `Iterable`, but so is `ManifestReader` that loads chunks of data at a time. Java's enhanced `for` syntax is another reason to use `Iterable`: ```java // this works for (DataFile removed : changes.removedDataFiles()) { ... } ``` Otherwise you have to wrap to produce an `Iterable`: ```java // this has to create an Iterable using a lambda for (DataFile removed : (Iterable<DataFile>) () -> changes.removedDataFiles()) { ... } ``` -- 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]
