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
43class ICEBERG_EXPORT Transaction : public std::enable_shared_from_this<Transaction> {
44 public:
46
48 static Result<std::shared_ptr<Transaction>> Make(std::shared_ptr<Table> table,
49 TransactionKind kind);
50
56 std::shared_ptr<TransactionContext> ctx);
57
59 const std::shared_ptr<Table>& table() const;
60
62 const TableMetadata* base() const;
63
65 const TableMetadata& current() const;
66
71 std::string MetadataFileLocation(std::string_view filename) const;
72
79
83
87
91
95
99
103
107
111
114
117
120
123
126
130
133
137
141
142 private:
143 explicit Transaction(std::shared_ptr<TransactionContext> ctx);
144
145 Status AddUpdate(const std::shared_ptr<PendingUpdate>& update);
146
148 Status Apply(PendingUpdate& updates);
149
150 // Helper methods for applying different types of updates
151 Status ApplyExpireSnapshots(ExpireSnapshots& update);
152 Status ApplySetSnapshot(SetSnapshot& update);
153 Status ApplyUpdateLocation(UpdateLocation& update);
154 Status ApplyUpdatePartitionSpec(UpdatePartitionSpec& update);
155 Status ApplyUpdatePartitionStatistics(UpdatePartitionStatistics& update);
156 Status ApplyUpdateProperties(UpdateProperties& update);
157 Status ApplyUpdateSchema(UpdateSchema& update);
158 Status ApplyUpdateSnapshot(SnapshotUpdate& update);
159 Status ApplyUpdateSnapshotReference(UpdateSnapshotReference& update);
160 Status ApplyUpdateSortOrder(UpdateSortOrder& update);
161 Status ApplyUpdateStatistics(UpdateStatistics& update);
162
164 Result<std::shared_ptr<Table>> CommitOnce(bool is_first_attempt);
165
167 bool CanRetry() const;
168
169 private:
170 friend class PendingUpdate;
171
172 // Shared context owning the table, metadata builder, and kind.
173 std::shared_ptr<TransactionContext> ctx_;
174 // Keep track of all created pending updates.
175 std::vector<std::shared_ptr<PendingUpdate>> pending_updates_;
176
177 // To make the state simple, we require updates are added and committed in order.
178 bool last_update_committed_ = true;
179 // Tracks if transaction has been committed to prevent double-commit
180 bool committed_ = false;
181};
182
184class ICEBERG_EXPORT TransactionContext {
185 public:
188
189 static Result<std::shared_ptr<TransactionContext>> Make(std::shared_ptr<Table> table,
190 TransactionKind kind);
191
192 const TableMetadata* base() const;
193 const TableMetadata& current() const;
194 std::string MetadataFileLocation(std::string_view filename) const;
195 Result<std::unique_ptr<LocationProvider>> NewLocationProvider() const;
196
197 std::shared_ptr<Table> table;
198 std::unique_ptr<TableMetadataBuilder> metadata_builder;
199 TransactionKind kind;
200 // If PendingUpdate is created directly from Table, this is nullopt;
201 // otherwise, it holds a weak pointer to the Transaction that created it.
202 std::optional<std::weak_ptr<Transaction>> transaction;
203};
204
205} // 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:41
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:49
Shared context between Transaction and PendingUpdate instances.
Definition transaction.h:184
A transaction for performing multiple updates to a table.
Definition transaction.h:43
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.
const TableMetadata & current() const
Return the current metadata with staged changes applied.
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.
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.
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
Define symbol visibility macros for core Iceberg APIs.
Core Apache Iceberg C++ APIs.
Definition arrow_io_util.h:33
TransactionKind
Whether a transaction creates a new table or updates an existing one.
Definition transaction.h:40
std::expected< T, E > Result
Result alias.
Definition result.h:88
Define Result, Status, and error helpers.
Represents the metadata for an Iceberg table.
Definition table_metadata.h:73