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


##########
core/src/main/java/org/apache/iceberg/ReachableFileCleanup.java:
##########
@@ -0,0 +1,152 @@
+/*
+ * 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.Set;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ExecutorService;
+import java.util.function.Consumer;
+import java.util.stream.Collectors;
+import org.apache.iceberg.exceptions.RuntimeIOException;
+import org.apache.iceberg.io.CloseableIterable;
+import org.apache.iceberg.io.FileIO;
+import org.apache.iceberg.relocated.com.google.common.collect.Sets;
+import org.apache.iceberg.util.Tasks;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * File cleanup strategy for snapshot expiration which determines, via an 
in-memory reference set,
+ * metadata and data files that are not reachable given the previous and 
current table states.
+ */
+class ReachableFileCleanup extends FileCleanupStrategy {
+
+  private static final Logger LOG = 
LoggerFactory.getLogger(ReachableFileCleanup.class);
+
+  ReachableFileCleanup(
+      FileIO fileIO,
+      ExecutorService deleteExecutorService,
+      ExecutorService planExecutorService,
+      Consumer<String> deleteFunc) {
+    super(fileIO, deleteExecutorService, planExecutorService, deleteFunc);
+  }
+
+  @Override
+  public void cleanFiles(TableMetadata beforeExpiration, TableMetadata 
afterExpiration) {
+    Set<String> manifestListsToDelete = Sets.newHashSet();
+
+    Set<Snapshot> snapshotsBeforeExpiration = 
Sets.newHashSet(beforeExpiration.snapshots());
+    Set<Snapshot> snapshotsAfterExpiration = 
Sets.newHashSet(afterExpiration.snapshots());
+    Set<Snapshot> expiredSnapshots = Sets.newHashSet();
+    for (Snapshot snapshot : snapshotsBeforeExpiration) {
+      if (!snapshotsAfterExpiration.contains(snapshot)) {
+        expiredSnapshots.add(snapshot);
+        if (snapshot.manifestListLocation() != null) {
+          manifestListsToDelete.add(snapshot.manifestListLocation());
+        }
+      }
+    }
+
+    Set<ManifestFile> candidateManifestFilesForDeletion = 
readManifests(expiredSnapshots);
+    Set<ManifestFile> manifestFilesAfterExpiration = 
readManifests(snapshotsAfterExpiration);
+
+    Set<ManifestFile> manifestsToDelete = 
Sets.newHashSet(candidateManifestFilesForDeletion);
+    for (ManifestFile candidateManifestFile : 
candidateManifestFilesForDeletion) {
+      if (manifestFilesAfterExpiration.contains(candidateManifestFile)) {
+        manifestsToDelete.remove(candidateManifestFile);

Review Comment:
   I know this does the same thing and I might just be paranoid, but I'd prefer 
to add to the `manifestsToDelete` set one by one rather than adding all 
candidates and then removing them. That seems slightly more efficient (don't 
iterate twice) and safer because as this grows, it is better to worry about 
correctly adding to the set rather than correctly removing from it.



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