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<SnapshotManager>> NewSnapshotManager();
111
114 Result<std::shared_ptr<SetSnapshot>> NewSetSnapshot();
115
118 Result<std::shared_ptr<UpdateSnapshotReference>> NewUpdateSnapshotReference();
119
120 private:
121 explicit Transaction(std::shared_ptr<TransactionContext> ctx);
122
123 Status AddUpdate(const std::shared_ptr<PendingUpdate>& update);
124
126 Status Apply(PendingUpdate& updates);
127
128 // Helper methods for applying different types of updates
129 Status ApplyExpireSnapshots(ExpireSnapshots& update);
130 Status ApplySetSnapshot(SetSnapshot& update);
131 Status ApplyUpdateLocation(UpdateLocation& update);
132 Status ApplyUpdatePartitionSpec(UpdatePartitionSpec& update);
133 Status ApplyUpdatePartitionStatistics(UpdatePartitionStatistics& update);
134 Status ApplyUpdateProperties(UpdateProperties& update);
135 Status ApplyUpdateSchema(UpdateSchema& update);
136 Status ApplyUpdateSnapshot(SnapshotUpdate& update);
137 Status ApplyUpdateSnapshotReference(UpdateSnapshotReference& update);
138 Status ApplyUpdateSortOrder(UpdateSortOrder& update);
139 Status ApplyUpdateStatistics(UpdateStatistics& update);
140
142 Result<std::shared_ptr<Table>> CommitOnce(bool is_first_attempt);
143
145 bool CanRetry() const;
146
147 private:
148 friend class PendingUpdate;
149
150 // Shared context owning the table, metadata builder, and kind.
151 std::shared_ptr<TransactionContext> ctx_;
152 // Keep track of all created pending updates.
153 std::vector<std::shared_ptr<PendingUpdate>> pending_updates_;
154 // To make the state simple, we require updates are added and committed in order.
155 bool last_update_committed_ = true;
156 // Tracks if transaction has been committed to prevent double-commit
157 bool committed_ = false;
158};
159
161class ICEBERG_EXPORT TransactionContext {
162 public:
165
166 static Result<std::shared_ptr<TransactionContext>> Make(std::shared_ptr<Table> table,
167 TransactionKind kind);
168
169 const TableMetadata* base() const;
170 const TableMetadata& current() const;
171 std::string MetadataFileLocation(std::string_view filename) const;
172
173 std::shared_ptr<Table> table;
174 std::unique_ptr<TableMetadataBuilder> metadata_builder;
175 TransactionKind kind;
176 // If PendingUpdate is created directly from Table, this is nullopt;
177 // otherwise, it holds a weak pointer to the Transaction that created it.
178 std::optional<std::weak_ptr<Transaction>> transaction;
179};
180
181} // namespace iceberg
API for removing old snapshots from a table.
Definition expire_snapshots.h:66
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
Base class for operations that produce snapshots.
Definition snapshot_update.h:43
Shared context between Transaction and PendingUpdate instances.
Definition transaction.h:161
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:72