iceberg-cpp
Loading...
Searching...
No Matches
snapshot_update.h
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20#pragma once
21
22#include <atomic>
23#include <functional>
24#include <memory>
25#include <optional>
26#include <span>
27#include <string>
28#include <unordered_map>
29#include <unordered_set>
30#include <vector>
31
32#include "iceberg/iceberg_export.h"
33#include "iceberg/result.h"
34#include "iceberg/snapshot.h"
35#include "iceberg/type_fwd.h"
37#include "iceberg/util/executor.h"
38
39namespace iceberg {
40
46class ICEBERG_EXPORT SnapshotUpdate : public PendingUpdate {
47 public:
49 struct ApplyResult {
50 std::shared_ptr<Snapshot> snapshot;
51 std::string target_branch;
52 bool stage_only = false;
53 };
54
55 ~SnapshotUpdate() override;
56
57 Kind kind() const override { return Kind::kUpdateSnapshot; }
58 bool IsRetryable() const override { return true; }
59
65 auto& DeleteWith(this auto& self,
66 std::function<Status(const std::string&)> delete_func) {
67 if (self.delete_func_) {
68 return self.AddError(ErrorKind::kInvalidArgument,
69 "Cannot set delete callback more than once");
70 }
71 self.delete_func_ = std::move(delete_func);
72 return self;
73 }
74
81 auto& StageOnly(this auto& self) {
82 self.stage_only_ = true;
83 return self;
84 }
85
90 auto& ScanManifestsWith(this auto& self, Executor& executor) {
91 self.plan_executor_ = std::ref(executor);
92 return self;
93 }
94
99 auto& ToBranch(this auto& self, const std::string& branch) {
100 if (branch.empty()) [[unlikely]] {
101 return self.AddError(ErrorKind::kInvalidArgument, "Branch name cannot be empty");
102 }
103
104 if (auto ref_it = self.base().refs.find(branch); ref_it != self.base().refs.end()) {
105 if (ref_it->second->type() != SnapshotRefType::kBranch) {
106 return self.AddError(ErrorKind::kInvalidArgument,
107 "{} is a tag, not a branch. Tags cannot be targets for "
108 "producing snapshots",
109 branch);
110 }
111 }
112
113 self.target_branch_ = branch;
114 return self;
115 }
116
122 auto& Set(this auto& self, const std::string& property, const std::string& value) {
123 static_cast<SnapshotUpdate&>(self).SetSummaryProperty(property, value);
124 return self;
125 }
126
134 auto& WriteManifestsWith(this auto& self, Executor& executor, int32_t parallelism) {
135 if (parallelism <= 0) [[unlikely]] {
136 return self.AddError(
137 ErrorKind::kInvalidArgument,
138 "Manifest write parallelism must be greater than 0, but was: {}", parallelism);
139 }
140
141 self.write_manifest_executor_ = std::ref(executor);
142 self.write_manifest_parallelism_ = parallelism;
143 return self;
144 }
145
154 Result<ApplyResult> Apply();
155
157 Status Finalize(Result<const TableMetadata*> commit_result) override;
158
159 protected:
161 std::shared_ptr<DataFile> file;
162 std::optional<int64_t> data_sequence_number;
163 };
164
165 explicit SnapshotUpdate(std::shared_ptr<TransactionContext> ctx);
166
173 Result<std::vector<ManifestFile>> WriteDataManifests(
174 std::span<const std::shared_ptr<DataFile>> files,
175 const std::shared_ptr<PartitionSpec>& spec,
176 std::optional<int64_t> data_sequence_number = std::nullopt);
177
178 Result<std::vector<ManifestFile>> WriteDeleteManifests(
179 std::span<const ContentFileWithSequenceNumber> files,
180 const std::shared_ptr<PartitionSpec>& spec);
181
182 const std::string& target_branch() const { return target_branch_; }
183 bool can_inherit_snapshot_id() const { return can_inherit_snapshot_id_; }
184 const std::string& commit_uuid() const { return commit_uuid_; }
185 int32_t manifest_count() const {
186 return manifest_count_.load(std::memory_order_relaxed);
187 }
188 int32_t attempt() const { return attempt_.load(std::memory_order_relaxed); }
189 int64_t target_manifest_size_bytes() const { return target_manifest_size_bytes_; }
190
201 virtual Status CleanUncommitted(const std::unordered_set<std::string>& committed) = 0;
202
206 virtual std::string operation() = 0;
207
214 virtual Status Validate(const TableMetadata& current_metadata,
215 const std::shared_ptr<Snapshot>& snapshot) {
216 return {};
217 };
218
224 virtual Result<std::vector<ManifestFile>> Apply(
225 const TableMetadata& metadata_to_update,
226 const std::shared_ptr<Snapshot>& snapshot) = 0;
227
231 virtual std::unordered_map<std::string, std::string> Summary() = 0;
232
237 virtual void SetSummaryProperty(const std::string& property, const std::string& value);
238
242 virtual bool CleanupAfterCommit() const { return true; }
243
245 int64_t SnapshotId();
246
251 Status DeleteFile(const std::string& path);
252
253 std::string ManifestPath();
254 std::string ManifestListPath();
255 SnapshotSummaryBuilder& summary_builder() { return summary_; }
256 SnapshotSummaryBuilder BuildManifestCountSummary(
257 std::span<const ManifestFile> manifests, int32_t replaced_manifests_count);
258
259 private:
261 Result<std::unordered_map<std::string, std::string>> ComputeSummary(
262 const TableMetadata& previous);
263
265 Status CleanAll();
266
267 protected:
268 SnapshotSummaryBuilder summary_;
269
270 private:
271 const bool can_inherit_snapshot_id_{true};
272 const std::string commit_uuid_;
273 OptionalExecutor write_manifest_executor_;
274 int32_t write_manifest_parallelism_{1};
275 std::atomic<int32_t> manifest_count_{0};
276 std::atomic<int32_t> attempt_{0};
277 std::vector<std::string> manifest_lists_;
278 const int64_t target_manifest_size_bytes_;
279 std::optional<int64_t> snapshot_id_;
280 OptionalExecutor plan_executor_;
281 bool stage_only_{false};
282 std::function<Status(const std::string&)> delete_func_;
283 std::string target_branch_{SnapshotRef::kMainBranch};
284 std::shared_ptr<Snapshot> staged_snapshot_;
285};
286
287} // namespace iceberg
Schedules iceberg-cpp internal planning tasks.
Definition executor.h:43
Base class for all kinds of table metadata updates.
Definition pending_update.h:41
Helper class for building snapshot summaries.
Definition snapshot.h:260
API for table changes that produce snapshots.
Definition snapshot_update.h:46
virtual Result< std::vector< ManifestFile > > Apply(const TableMetadata &metadata_to_update, const std::shared_ptr< Snapshot > &snapshot)=0
Apply the update's changes to the given metadata and snapshot.
virtual std::unordered_map< std::string, std::string > Summary()=0
Get the summary map for this operation.
auto & StageOnly(this auto &self)
Stage a snapshot in table metadata, but do not make it current.
Definition snapshot_update.h:81
virtual std::string operation()=0
A string that describes the action that produced the new snapshot.
virtual Status CleanUncommitted(const std::unordered_set< std::string > &committed)=0
Clean up any uncommitted manifests that were created.
bool IsRetryable() const override
Whether this update can be retried after a commit conflict.
Definition snapshot_update.h:58
auto & ToBranch(this auto &self, const std::string &branch)
Perform operations on a particular branch.
Definition snapshot_update.h:99
virtual Status Validate(const TableMetadata &current_metadata, const std::shared_ptr< Snapshot > &snapshot)
Validate the current metadata.
Definition snapshot_update.h:214
auto & ScanManifestsWith(this auto &self, Executor &executor)
Configure an executor for manifest planning work.
Definition snapshot_update.h:90
auto & WriteManifestsWith(this auto &self, Executor &executor, int32_t parallelism)
Configure an executor and max writer count for writing new manifests.
Definition snapshot_update.h:134
Kind kind() const override
Return the kind of this pending update.
Definition snapshot_update.h:57
auto & Set(this auto &self, const std::string &property, const std::string &value)
Set a summary property in the snapshot produced by this update.
Definition snapshot_update.h:122
virtual bool CleanupAfterCommit() const
Check if cleanup should happen after commit.
Definition snapshot_update.h:242
auto & DeleteWith(this auto &self, std::function< Status(const std::string &)> delete_func)
Set a callback to delete files instead of the table's default.
Definition snapshot_update.h:65
Result of applying a snapshot update.
Definition snapshot_update.h:49
Represents the metadata for an Iceberg table.
Definition table_metadata.h:73