iceberg-cpp
Loading...
Searching...
No Matches
transaction.h
Go to the documentation of this file.
1
2/*
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 */
20
21#pragma once
22
25
26#include <cstdint>
27#include <memory>
28#include <optional>
29#include <string>
30#include <string_view>
31#include <vector>
32
34#include "iceberg/result.h"
35#include "iceberg/type_fwd.h"
36
37namespace iceberg {
38
40enum class TransactionKind : uint8_t { kCreate, kUpdate };
41
43enum class TransactionState : uint8_t {
44 kReady,
45 kUpdatePending,
46 kCommitted,
47 kFailed,
48 kAborted,
49 kCommitStateUnknown,
50};
51
53class ICEBERG_EXPORT Transaction : public std::enable_shared_from_this<Transaction> {
54 public:
56
58 static Result<std::shared_ptr<Transaction>> Make(std::shared_ptr<Table> table,
59 TransactionKind kind);
60
66 std::shared_ptr<TransactionContext> ctx);
67
69 const std::shared_ptr<Table>& table() const;
70
72 const TableMetadata* base() const;
73
75 const TableMetadata& current() const;
76
81 std::string MetadataFileLocation(std::string_view filename) const;
82
89
94
96 TransactionState state() const { return state_; }
97
101
105
109
113
117
121
125
129
132
135
138
141
144
148
152
155
158
162
166
170
171 private:
172 explicit Transaction(std::shared_ptr<TransactionContext> ctx);
173
174 Status CheckReady() const;
175 Status AddUpdate(const std::shared_ptr<PendingUpdate>& update);
176
177 Status CommitUpdate(PendingUpdate& update);
178 Status ApplyUpdate(PendingUpdate& update);
179 Status ReplayUpdates();
180
181 // Helper methods for applying different types of updates
182 Status ApplyExpireSnapshots(ExpireSnapshots& update);
183 Status ApplySetSnapshot(SetSnapshot& update);
184 Status ApplyUpdateLocation(UpdateLocation& update);
185 Status ApplyUpdatePartitionSpec(UpdatePartitionSpec& update);
186 Status ApplyUpdatePartitionStatistics(UpdatePartitionStatistics& update);
187 Status ApplyUpdateProperties(UpdateProperties& update);
188 Status ApplyUpdateSchema(UpdateSchema& update);
189 Status ApplyUpdateSnapshot(SnapshotUpdate& update);
190 Status ApplyUpdateSnapshotReference(UpdateSnapshotReference& update);
191 Status ApplyUpdateSortOrder(UpdateSortOrder& update);
192 Status ApplyUpdateStatistics(UpdateStatistics& update);
193
194 Result<std::shared_ptr<Table>> CommitOnce(bool is_first_attempt);
195
197 bool CanRetry() const;
198
199 void CleanupUpdates() noexcept;
200 void FinalizeUpdates(const TableMetadata& committed) noexcept;
201
202 private:
203 friend class PendingUpdate;
204
205 // Shared context owning the table, metadata builder, and kind.
206 std::shared_ptr<TransactionContext> ctx_;
207 // Keep track of all created pending updates.
208 std::vector<std::shared_ptr<PendingUpdate>> pending_updates_;
209
210 TransactionState state_ = TransactionState::kReady;
211};
212
214class ICEBERG_EXPORT TransactionContext {
215 public:
218
219 static Result<std::shared_ptr<TransactionContext>> Make(std::shared_ptr<Table> table,
220 TransactionKind kind);
221
222 const TableMetadata* base() const;
223 const TableMetadata& current() const;
224 std::string MetadataFileLocation(std::string_view filename) const;
225 Result<std::unique_ptr<LocationProvider>> NewLocationProvider() const;
226
227 std::shared_ptr<Table> table;
228 std::unique_ptr<TableMetadataBuilder> metadata_builder;
229 TransactionKind kind;
230 // If PendingUpdate is created directly from Table, this is nullopt;
231 // otherwise, it holds a weak pointer to the Transaction that created it.
232 std::optional<std::weak_ptr<Transaction>> transaction;
233};
234
235} // namespace iceberg
API for removing old snapshots from a table.
Definition expire_snapshots.h:68
Base class for all kinds of table metadata updates.
Definition pending_update.h:48
Sets the current snapshot directly or by rolling back.
Definition set_snapshot.h:37
API for table changes that produce snapshots.
Definition snapshot_update.h:61
Shared context between Transaction and PendingUpdate instances.
Definition transaction.h:214
A transaction for performing multiple updates to a table.
Definition transaction.h:53
Result< std::shared_ptr< UpdateSnapshotReference > > NewUpdateSnapshotReference()
Create a new UpdateSnapshotReference to update snapshot references (branches and tags) and commit the...
const TableMetadata * base() const
Returns the base metadata without any changes.
Result< std::shared_ptr< UpdateSortOrder > > NewUpdateSortOrder()
Create a new UpdateSortOrder to update the table sort order and commit the changes.
Result< std::shared_ptr< RewriteManifests > > NewRewriteManifests()
Create a new RewriteManifests to rewrite manifest layout.
Status Abort()
Discard staged changes and clean owned files best effort. Repeated Abort succeeds without repeating c...
const TableMetadata & current() const
Return the current metadata with staged changes applied.
Result< std::shared_ptr< CherryPickOperation > > NewCherryPickOperation()
Create a new CherryPickOperation to apply the changes of an existing snapshot onto the current state.
Result< std::shared_ptr< UpdatePartitionStatistics > > NewUpdatePartitionStatistics()
Create a new UpdatePartitionStatistics to update partition statistics and commit the changes.
static Result< std::shared_ptr< Transaction > > Make(std::shared_ptr< Table > table, TransactionKind kind)
Create a new transaction.
const std::shared_ptr< Table > & table() const
Return the Table that this transaction will update.
Result< std::shared_ptr< DeleteFiles > > NewDeleteFiles()
Create a new DeleteFiles to delete data files and commit the changes.
Result< std::shared_ptr< FastAppend > > NewFastAppend()
Create a new FastAppend to append data files and commit the changes.
Result< std::shared_ptr< SetSnapshot > > NewSetSnapshot()
Create a new SetSnapshot to set the current snapshot or rollback to a previous snapshot and commit th...
Result< std::shared_ptr< UpdateLocation > > NewUpdateLocation()
Create a new UpdateLocation to update the table location and commit the changes.
Result< std::shared_ptr< SnapshotManager > > NewSnapshotManager()
Create a new SnapshotManager to manage snapshots.
Result< std::shared_ptr< ExpireSnapshots > > NewExpireSnapshots()
Create a new ExpireSnapshots to remove expired snapshots and commit the changes.
TransactionState state() const
Return the current state of the transaction.
Definition transaction.h:96
std::string MetadataFileLocation(std::string_view filename) const
Return the location of the metadata file with the given filename.
Result< std::shared_ptr< Table > > Commit()
Apply the pending changes from all actions and commit.
Result< std::shared_ptr< UpdatePartitionSpec > > NewUpdatePartitionSpec()
Create a new UpdatePartitionSpec to update the partition spec of this table and commit the changes.
Result< std::shared_ptr< RowDelta > > NewRowDelta()
Create a new RowDelta to add rows and row-level deletes.
Result< std::shared_ptr< UpdateStatistics > > NewUpdateStatistics()
Create a new UpdateStatistics to update table statistics and commit the changes.
Result< std::shared_ptr< ReplacePartitions > > NewReplacePartitions()
Create a new ReplacePartitions to dynamically overwrite partitions and commit the changes.
static Result< std::shared_ptr< Transaction > > Make(std::shared_ptr< TransactionContext > ctx)
Create a detached transaction from an existing context.
Result< std::shared_ptr< UpdateSchema > > NewUpdateSchema()
Create a new UpdateSchema to alter the columns of this table and commit the changes.
Result< std::shared_ptr< MergeAppend > > NewMergeAppend()
Create a new MergeAppend to append data files and merge manifests.
Result< std::shared_ptr< UpdateProperties > > NewUpdateProperties()
Create a new UpdateProperties to update table properties and commit the changes.
Result< std::shared_ptr< RewriteFiles > > NewRewriteFiles()
Create a new RewriteFiles to replace files in this table and commit the changes.
Result< std::shared_ptr< OverwriteFiles > > NewOverwrite()
Create a new OverwriteFiles to overwrite data files and commit the changes.
Updating table location with a new base location.
Definition update_location.h:34
API for partition spec evolution.
Definition update_partition_spec.h:44
Updates table partition statistics.
Definition update_partition_statistics.h:39
Updates table properties.
Definition update_properties.h:39
API for schema evolution.
Definition update_schema.h:45
Updates snapshot references.
Definition update_snapshot_reference.h:39
Updating table sort order with a newly created order.
Definition update_sort_order.h:37
Updates table statistics.
Definition update_statistics.h:39
Definition expected.h:1199
Define symbol visibility macros for core Iceberg APIs.
Core Apache Iceberg C++ APIs.
Definition arrow_io_util.h:34
TransactionKind
Whether a transaction creates a new table or updates an existing one.
Definition transaction.h:40
TransactionState
Lifecycle outcomes of a transaction. Failed transactions may only be aborted.
Definition transaction.h:43
STL namespace.
Define Result, Status, and error helpers.
Represents the metadata for an Iceberg table.
Definition table_metadata.h:73