zhjwpku commented on code in PR #542:
URL: https://github.com/apache/iceberg-cpp/pull/542#discussion_r2757904693


##########
src/iceberg/update/snapshot_manager.cc:
##########
@@ -0,0 +1,224 @@
+/*
+ * 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/snapshot_manager.h"
+
+#include <memory>
+#include <string>
+
+#include "iceberg/result.h"
+#include "iceberg/snapshot.h"
+#include "iceberg/table.h"
+#include "iceberg/table_metadata.h"
+#include "iceberg/transaction.h"
+#include "iceberg/update/fast_append.h"
+#include "iceberg/update/set_snapshot.h"
+#include "iceberg/update/update_snapshot_reference.h"
+#include "iceberg/util/macros.h"
+
+namespace iceberg {
+
+Result<std::shared_ptr<SnapshotManager>> SnapshotManager::Make(
+    const std::string& table_name, std::shared_ptr<Table> table) {
+  if (table == nullptr) {
+    return InvalidArgument("Table cannot be null");
+  }
+  if (table->metadata() == nullptr) {
+    return InvalidArgument("Cannot manage snapshots: table {} does not exist",
+                           table_name);
+  }
+  // Create a transaction first
+  ICEBERG_ASSIGN_OR_RAISE(auto transaction,
+                          Transaction::Make(table, Transaction::Kind::kUpdate,
+                                            /*auto_commit=*/false));
+  auto manager = std::shared_ptr<SnapshotManager>(
+      new SnapshotManager(std::move(transaction), /*is_external=*/false));
+  return manager;
+}
+
+Result<std::shared_ptr<SnapshotManager>> SnapshotManager::Make(
+    std::shared_ptr<Transaction> transaction) {
+  if (transaction == nullptr) {
+    return InvalidArgument("Invalid input transaction: null");
+  }
+  return std::shared_ptr<SnapshotManager>(
+      new SnapshotManager(std::move(transaction), /*is_external=*/true));
+}
+
+SnapshotManager::SnapshotManager(std::shared_ptr<Transaction> transaction,
+                                 bool is_external)
+    : PendingUpdate(transaction), is_external_transaction_(is_external) {}
+
+SnapshotManager::~SnapshotManager() = default;
+
+SnapshotManager& SnapshotManager::Cherrypick(int64_t snapshot_id) {
+  ICEBERG_BUILDER_RETURN_IF_ERROR(CommitIfRefUpdatesExist());
+  // TODO(anyone): Implement cherrypick operation
+  ICEBERG_BUILDER_CHECK(false, "Cherrypick operation not yet implemented");
+  return *this;
+}
+
+SnapshotManager& SnapshotManager::SetCurrentSnapshot(int64_t snapshot_id) {
+  ICEBERG_BUILDER_RETURN_IF_ERROR(CommitIfRefUpdatesExist());
+  ICEBERG_BUILDER_ASSIGN_OR_RETURN(auto set_snapshot, 
transaction_->NewSetSnapshot());
+  set_snapshot->SetCurrentSnapshot(snapshot_id);
+  ICEBERG_BUILDER_RETURN_IF_ERROR(set_snapshot->Commit());
+  return *this;
+}
+
+SnapshotManager& SnapshotManager::RollbackToTime(TimePointMs timestamp_ms) {
+  ICEBERG_BUILDER_RETURN_IF_ERROR(CommitIfRefUpdatesExist());
+  ICEBERG_BUILDER_ASSIGN_OR_RETURN(auto set_snapshot, 
transaction_->NewSetSnapshot());
+  set_snapshot->RollbackToTime(UnixMsFromTimePointMs(timestamp_ms));
+  ICEBERG_BUILDER_RETURN_IF_ERROR(set_snapshot->Commit());
+  return *this;
+}
+
+SnapshotManager& SnapshotManager::RollbackTo(int64_t snapshot_id) {
+  ICEBERG_BUILDER_RETURN_IF_ERROR(CommitIfRefUpdatesExist());
+  ICEBERG_BUILDER_ASSIGN_OR_RETURN(auto set_snapshot, 
transaction_->NewSetSnapshot());
+  set_snapshot->RollbackTo(snapshot_id);
+  ICEBERG_BUILDER_RETURN_IF_ERROR(set_snapshot->Commit());
+  return *this;
+}
+
+SnapshotManager& SnapshotManager::CreateBranch(const std::string& name) {
+  if (base().current_snapshot_id != kInvalidSnapshotId) {
+    ICEBERG_BUILDER_ASSIGN_OR_RETURN(auto current_snapshot, base().Snapshot());
+    if (current_snapshot != nullptr) {

Review Comment:
   Yes, I replaced the nullptr check to a ICEBERG_DCHECK.



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