3434
3535namespace iceberg {
3636
37- Result<std::shared_ptr<SnapshotManager>> SnapshotManager::Make (
38- const std::string& table_name, std::shared_ptr<Table> table) {
39- if (table == nullptr ) {
40- return InvalidArgument (" Table cannot be null" );
41- }
42- if (table->metadata () == nullptr ) {
43- return InvalidArgument (" Cannot manage snapshots: table {} does not exist" ,
44- table_name);
45- }
46- // Create a transaction first
47- ICEBERG_ASSIGN_OR_RAISE (auto transaction,
48- Transaction::Make (table, Transaction::Kind::kUpdate ,
49- /* auto_commit=*/ false ));
50- auto manager = std::shared_ptr<SnapshotManager>(
51- new SnapshotManager (std::move (transaction), /* is_external=*/ false ));
52- return manager;
53- }
54-
5537Result<std::shared_ptr<SnapshotManager>> SnapshotManager::Make (
5638 std::shared_ptr<Transaction> transaction) {
57- if (transaction == nullptr ) {
58- return InvalidArgument (" Invalid input transaction: null" );
59- }
60- return std::shared_ptr<SnapshotManager>(
61- new SnapshotManager (std::move (transaction), /* is_external=*/ true ));
39+ ICEBERG_PRECHECK (transaction != nullptr , " Invalid input transaction: null" );
40+ return std::shared_ptr<SnapshotManager>(new SnapshotManager (std::move (transaction)));
6241}
6342
64- SnapshotManager::SnapshotManager (std::shared_ptr<Transaction> transaction,
65- bool is_external)
66- : PendingUpdate(transaction), is_external_transaction_(is_external) {}
43+ SnapshotManager::SnapshotManager (std::shared_ptr<Transaction> transaction)
44+ : PendingUpdate(transaction) {}
6745
6846SnapshotManager::~SnapshotManager () = default ;
6947
7048SnapshotManager& SnapshotManager::Cherrypick (int64_t snapshot_id) {
71- ICEBERG_BUILDER_RETURN_IF_ERROR (CommitIfRefUpdatesExist ());
7249 // TODO(anyone): Implement cherrypick operation
7350 ICEBERG_BUILDER_CHECK (false , " Cherrypick operation not yet implemented" );
7451 return *this ;
7552}
7653
7754SnapshotManager& SnapshotManager::SetCurrentSnapshot (int64_t snapshot_id) {
78- ICEBERG_BUILDER_RETURN_IF_ERROR (CommitIfRefUpdatesExist ());
7955 ICEBERG_BUILDER_ASSIGN_OR_RETURN (auto set_snapshot, transaction_->NewSetSnapshot ());
8056 set_snapshot->SetCurrentSnapshot (snapshot_id);
8157 ICEBERG_BUILDER_RETURN_IF_ERROR (set_snapshot->Commit ());
8258 return *this ;
8359}
8460
8561SnapshotManager& SnapshotManager::RollbackToTime (TimePointMs timestamp_ms) {
86- ICEBERG_BUILDER_RETURN_IF_ERROR (CommitIfRefUpdatesExist ());
8762 ICEBERG_BUILDER_ASSIGN_OR_RETURN (auto set_snapshot, transaction_->NewSetSnapshot ());
8863 set_snapshot->RollbackToTime (UnixMsFromTimePointMs (timestamp_ms));
8964 ICEBERG_BUILDER_RETURN_IF_ERROR (set_snapshot->Commit ());
9065 return *this ;
9166}
9267
9368SnapshotManager& SnapshotManager::RollbackTo (int64_t snapshot_id) {
94- ICEBERG_BUILDER_RETURN_IF_ERROR (CommitIfRefUpdatesExist ());
9569 ICEBERG_BUILDER_ASSIGN_OR_RETURN (auto set_snapshot, transaction_->NewSetSnapshot ());
9670 set_snapshot->RollbackTo (snapshot_id);
9771 ICEBERG_BUILDER_RETURN_IF_ERROR (set_snapshot->Commit ());
@@ -101,9 +75,8 @@ SnapshotManager& SnapshotManager::RollbackTo(int64_t snapshot_id) {
10175SnapshotManager& SnapshotManager::CreateBranch (const std::string& name) {
10276 if (base ().current_snapshot_id != kInvalidSnapshotId ) {
10377 ICEBERG_BUILDER_ASSIGN_OR_RETURN (auto current_snapshot, base ().Snapshot ());
104- if (current_snapshot != nullptr ) {
105- return CreateBranch (name, current_snapshot->snapshot_id );
106- }
78+ ICEBERG_DCHECK (current_snapshot != nullptr , " Current snapshot should not be null" );
79+ return CreateBranch (name, current_snapshot->snapshot_id );
10780 }
10881 const auto & current_refs = base ().refs ;
10982 ICEBERG_BUILDER_CHECK (!base ().refs .contains (name), " Ref {} already exists" , name);
@@ -197,11 +170,8 @@ SnapshotManager& SnapshotManager::SetMaxRefAgeMs(const std::string& name,
197170Result<std::shared_ptr<Snapshot>> SnapshotManager::Apply () { return base ().Snapshot (); }
198171
199172Status SnapshotManager::Commit () {
200- ICEBERG_RETURN_UNEXPECTED (CommitIfRefUpdatesExist ());
201- if (!is_external_transaction_) {
202- ICEBERG_RETURN_UNEXPECTED (transaction_->Commit ());
203- }
204- return {};
173+ ICEBERG_RETURN_UNEXPECTED (CheckErrors ());
174+ return CommitIfRefUpdatesExist ();
205175}
206176
207177Result<std::shared_ptr<UpdateSnapshotReference>>
0 commit comments