manuzhang commented on code in PR #1104:
URL: https://github.com/apache/iceberg-cpp/pull/1104#discussion_r4613615130


##########
src/iceberg/transaction.cc:
##########
@@ -534,7 +621,40 @@ void Transaction::CleanupUpdates() noexcept {
 }
 
 void Transaction::FinalizeUpdates(const TableMetadata& committed) noexcept {
+  std::optional<std::unordered_set<std::string>> committed_files;
+  internal::LogAndIgnoreFailure("Transaction file discovery", [&]() -> Status {
+    std::unordered_set<int64_t> snapshot_ids;
+    for (const auto& change : ctx_->metadata_builder->changes()) {
+      if (change->kind() == TableUpdate::Kind::kAddSnapshot) {
+        const auto& snapshot =
+            internal::checked_cast<const 
table::AddSnapshot&>(*change).snapshot();
+        snapshot_ids.insert(snapshot->snapshot_id);
+      }
+    }
+    std::unordered_set<std::string> content_paths;
+    for (const auto& update : pending_updates_) {
+      if (update->kind() != PendingUpdate::Kind::kUpdateSnapshot) {
+        continue;
+      }
+      auto& snapshot_update = internal::checked_cast<SnapshotUpdate&>(*update);
+      std::lock_guard lock(snapshot_update.staging_mutex_);
+      content_paths.insert(snapshot_update.staged_content_files_.begin(),
+                           snapshot_update.staged_content_files_.end());
+    }
+    ICEBERG_ASSIGN_OR_RAISE(
+        committed_files,
+        CommittedTransactionFiles(committed, ctx_->table->io(), 
std::move(snapshot_ids),
+                                  std::move(content_paths)));
+    return {};
+  });
   for (const auto& update : pending_updates_) {
+    if (update->kind() == PendingUpdate::Kind::kUpdateSnapshot && 
committed_files) {
+      // Failed discovery must never turn into an empty keep-set.
+      internal::LogAndIgnoreFailure("Snapshot cleanup", [&] {
+        return 
internal::checked_cast<SnapshotUpdate&>(*update).CleanUncommittedFiles(

Review Comment:
   We cannot clean uncommitted files when committed_files is empty if 
`FindAddedSnapshots` throws in `CommittedTransactionFiles` above.



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