wgtmac commented on code in PR #509:
URL: https://github.com/apache/iceberg-cpp/pull/509#discussion_r2688991708


##########
src/iceberg/table_metadata.cc:
##########
@@ -1087,11 +1090,8 @@ Status 
TableMetadataBuilder::Impl::SetBranchSnapshot(int64_t snapshot_id,
     // change is a noop
     return {};
   }
-
-  auto snapshot_it = snapshots_by_id_.find(snapshot_id);
-  ICEBERG_CHECK(snapshot_it != snapshots_by_id_.end(),
-                "Cannot set {} to unknown snapshot: {}", branch, snapshot_id);
-  return SetBranchSnapshotInternal(*snapshot_it->second, branch);
+  ICEBERG_ASSIGN_OR_RAISE(auto snapshot, metadata_.SnapshotById(snapshot_id));

Review Comment:
   Please revert this change. `metadata_.SnapshotById(snapshot_id)` is slower 
than `snapshots_by_id_.find(snapshot_id)`.



##########
src/iceberg/update/set_snapshot.cc:
##########
@@ -0,0 +1,146 @@
+/*
+ * 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.
+ */
+
+#include "iceberg/update/set_snapshot.h"
+
+#include <cstdint>
+#include <memory>
+#include <optional>
+
+#include "iceberg/snapshot.h"
+#include "iceberg/table_metadata.h"
+#include "iceberg/transaction.h"
+#include "iceberg/util/error_collector.h"
+#include "iceberg/util/macros.h"
+#include "iceberg/util/snapshot_util_internal.h"
+#include "iceberg/util/timepoint.h"
+
+namespace iceberg {
+
+Result<std::shared_ptr<SetSnapshot>> SetSnapshot::Make(
+    std::shared_ptr<Transaction> transaction) {
+  ICEBERG_PRECHECK(transaction != nullptr,
+                   "Cannot create SetSnapshot without a transaction");
+  return std::shared_ptr<SetSnapshot>(new SetSnapshot(std::move(transaction)));
+}
+
+SetSnapshot::SetSnapshot(std::shared_ptr<Transaction> transaction)
+    : PendingUpdate(std::move(transaction)) {}
+
+SetSnapshot::~SetSnapshot() = default;
+
+SetSnapshot& SetSnapshot::SetCurrentSnapshot(int64_t snapshot_id) {
+  const TableMetadata& base_metadata = transaction_->current();
+
+  // Validate that the snapshot exists
+  auto snapshot_result = base_metadata.SnapshotById(snapshot_id);

Review Comment:
   ```suggestion
     // Validate that the snapshot exists
     auto snapshot_result = base().SnapshotById(snapshot_id);
   ```



##########
src/iceberg/update/set_snapshot.h:
##########
@@ -0,0 +1,97 @@
+/*
+ * 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.
+ */
+
+#pragma once
+
+#include <cstdint>
+#include <memory>
+#include <optional>
+
+#include "iceberg/iceberg_export.h"
+#include "iceberg/result.h"
+#include "iceberg/type_fwd.h"
+#include "iceberg/update/pending_update.h"
+
+/// \file iceberg/update/set_snapshot.h
+/// \brief Sets the current snapshot directly or by rolling back.
+
+namespace iceberg {
+
+/// \brief Sets the current snapshot directly or by rolling back.
+///
+/// This update is not exposed through the Table API. Instead, it is a 
package-private
+/// part of the Transaction API intended for use in ManageSnapshots.
+///
+/// When committing, these changes will be applied to the current table 
metadata.
+/// Commit conflicts will not be resolved and will result in a CommitFailed 
error.

Review Comment:
   ```suggestion
   ```
   
   These comments are Java specific.



##########
src/iceberg/update/pending_update.h:
##########
@@ -47,6 +47,7 @@ class ICEBERG_EXPORT PendingUpdate : public ErrorCollector {
     kUpdateSchema,
     kUpdateSnapshot,
     kUpdateSortOrder,
+    kSetSnapshot,

Review Comment:
   Sort it alphabetically.



##########
src/iceberg/update/set_snapshot.cc:
##########
@@ -0,0 +1,146 @@
+/*
+ * 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.
+ */
+
+#include "iceberg/update/set_snapshot.h"
+
+#include <cstdint>
+#include <memory>
+#include <optional>
+
+#include "iceberg/snapshot.h"
+#include "iceberg/table_metadata.h"
+#include "iceberg/transaction.h"
+#include "iceberg/util/error_collector.h"
+#include "iceberg/util/macros.h"
+#include "iceberg/util/snapshot_util_internal.h"
+#include "iceberg/util/timepoint.h"
+
+namespace iceberg {
+
+Result<std::shared_ptr<SetSnapshot>> SetSnapshot::Make(
+    std::shared_ptr<Transaction> transaction) {
+  ICEBERG_PRECHECK(transaction != nullptr,
+                   "Cannot create SetSnapshot without a transaction");
+  return std::shared_ptr<SetSnapshot>(new SetSnapshot(std::move(transaction)));
+}
+
+SetSnapshot::SetSnapshot(std::shared_ptr<Transaction> transaction)
+    : PendingUpdate(std::move(transaction)) {}
+
+SetSnapshot::~SetSnapshot() = default;
+
+SetSnapshot& SetSnapshot::SetCurrentSnapshot(int64_t snapshot_id) {
+  const TableMetadata& base_metadata = transaction_->current();
+
+  // Validate that the snapshot exists
+  auto snapshot_result = base_metadata.SnapshotById(snapshot_id);
+  ICEBERG_BUILDER_CHECK(snapshot_result.has_value(),
+                        "Cannot roll back to unknown snapshot id: {}", 
snapshot_id);
+
+  target_snapshot_id_ = snapshot_id;
+
+  return *this;
+}
+
+SetSnapshot& SetSnapshot::RollbackToTime(int64_t timestamp_ms) {
+  // Find the latest snapshot by timestamp older than timestamp_ms
+  ICEBERG_BUILDER_ASSIGN_OR_RETURN(auto snapshot_opt,
+                                   FindLatestAncestorOlderThan(timestamp_ms));
+
+  ICEBERG_BUILDER_CHECK(snapshot_opt.has_value(),
+                        "Cannot roll back, no valid snapshot older than: {}",
+                        timestamp_ms);
+
+  target_snapshot_id_ = snapshot_opt.value()->snapshot_id;
+  is_rollback_ = true;
+
+  return *this;
+}
+
+SetSnapshot& SetSnapshot::RollbackTo(int64_t snapshot_id) {
+  const TableMetadata& current = transaction_->current();

Review Comment:
   ditto



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