iceberg-cpp
Loading...
Searching...
No Matches
transaction.h
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
23#include <cstdint>
24#include <memory>
25#include <optional>
26#include <string>
27#include <string_view>
28#include <vector>
29
30#include "iceberg/iceberg_export.h"
31#include "iceberg/result.h"
32#include "iceberg/type_fwd.h"
33
34namespace iceberg {
35
37enum class TransactionKind : uint8_t { kCreate, kUpdate };
38
40class ICEBERG_EXPORT Transaction : public std::enable_shared_from_this<Transaction> {
41 public:
43
45 static Result<std::shared_ptr<Transaction>> Make(std::shared_ptr<Table> table,
46 TransactionKind kind);
47
49 static Result<std::shared_ptr<Transaction>> Make(
50 std::shared_ptr<TransactionContext> ctx);
51
53 const std::shared_ptr<Table>& table() const;
54
56 const TableMetadata* base() const;
57
59 const TableMetadata& current() const;
60
65 std::string MetadataFileLocation(std::string_view filename) const;
66
72 Result<std::shared_ptr<Table>> Commit();
73
76 Result<std::shared_ptr<UpdatePartitionSpec>> NewUpdatePartitionSpec();
77
80 Result<std::shared_ptr<UpdateProperties>> NewUpdateProperties();
81
84 Result<std::shared_ptr<UpdateSortOrder>> NewUpdateSortOrder();
85
88 Result<std::shared_ptr<UpdateSchema>> NewUpdateSchema();
89
92 Result<std::shared_ptr<ExpireSnapshots>> NewExpireSnapshots();
93
96 Result<std::shared_ptr<UpdateStatistics>> NewUpdateStatistics();
97
100 Result<std::shared_ptr<UpdatePartitionStatistics>> NewUpdatePartitionStatistics();
101
104 Result<std::shared_ptr<UpdateLocation>> NewUpdateLocation();
105
107 Result<std::shared_ptr<FastAppend>> NewFastAppend();
108
110 Result<std::shared_ptr<MergeAppend>> NewMergeAppend();
111
113 Result<std::shared_ptr<DeleteFiles>> NewDeleteFiles();
114
116 Result<std::shared_ptr<RowDelta>> NewRowDelta();
117
119 Result<std::shared_ptr<OverwriteFiles>> NewOverwrite();
120
123 Result<std::shared_ptr<RewriteFiles>> NewRewriteFiles();
124
126 Result<std::shared_ptr<SnapshotManager>> NewSnapshotManager();
127
130 Result<std::shared_ptr<SetSnapshot>> NewSetSnapshot();
131
134 Result<std::shared_ptr<UpdateSnapshotReference>> NewUpdateSnapshotReference();
135
136 private:
137 explicit Transaction(std::shared_ptr<TransactionContext> ctx);
138
139 Status AddUpdate(const std::shared_ptr<PendingUpdate>& update);
140
142 Status Apply(PendingUpdate& updates);
143
144 // Helper methods for applying different types of updates
145 Status ApplyExpireSnapshots(ExpireSnapshots& update);
146 Status ApplySetSnapshot(SetSnapshot& update);
147 Status ApplyUpdateLocation(UpdateLocation& update);
148 Status ApplyUpdatePartitionSpec(UpdatePartitionSpec& update);
149 Status ApplyUpdatePartitionStatistics(UpdatePartitionStatistics& update);
150 Status ApplyUpdateProperties(UpdateProperties& update);
151 Status ApplyUpdateSchema(UpdateSchema& update);
152 Status ApplyUpdateSnapshot(SnapshotUpdate& update);
153 Status ApplyUpdateSnapshotReference(UpdateSnapshotReference& update);
154 Status ApplyUpdateSortOrder(UpdateSortOrder& update);
155 Status ApplyUpdateStatistics(UpdateStatistics& update);
156
158 Result<std::shared_ptr<Table>> CommitOnce(bool is_first_attempt);
159
161 bool CanRetry() const;
162
163 private:
164 friend class PendingUpdate;
165
166 // Shared context owning the table, metadata builder, and kind.
167 std::shared_ptr<TransactionContext> ctx_;
168 // Keep track of all created pending updates.
169 std::vector<std::shared_ptr<PendingUpdate>> pending_updates_;
170 // To make the state simple, we require updates are added and committed in order.
171 bool last_update_committed_ = true;
172 // Tracks if transaction has been committed to prevent double-commit
173 bool committed_ = false;
174};
175
177class ICEBERG_EXPORT TransactionContext {
178 public:
181
182 static Result<std::shared_ptr<TransactionContext>> Make(std::shared_ptr<Table> table,
183 TransactionKind kind);
184
185 const TableMetadata* base() const;
186 const TableMetadata& current() const;
187 std::string MetadataFileLocation(std::string_view filename) const;
188
189 std::shared_ptr<Table> table;
190 std::unique_ptr<TableMetadataBuilder> metadata_builder;
191 TransactionKind kind;
192 // If PendingUpdate is created directly from Table, this is nullopt;
193 // otherwise, it holds a weak pointer to the Transaction that created it.
194 std::optional<std::weak_ptr<Transaction>> transaction;
195};
196
197} // 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:46
Shared context between Transaction and PendingUpdate instances.
Definition transaction.h:177
A transaction for performing multiple updates to a table.
Definition transaction.h:40
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:48
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
Represents the metadata for an Iceberg table.
Definition table_metadata.h:73