28#include <unordered_map>
29#include <unordered_set>
32#include "iceberg/iceberg_export.h"
33#include "iceberg/result.h"
34#include "iceberg/snapshot.h"
37#include "iceberg/util/executor.h"
50 std::shared_ptr<Snapshot> snapshot;
51 std::string target_branch;
52 bool stage_only =
false;
57 Kind
kind()
const override {
return Kind::kUpdateSnapshot; }
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");
71 self.delete_func_ = std::move(delete_func);
82 self.stage_only_ =
true;
91 self.plan_executor_ = std::ref(executor);
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");
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",
113 self.target_branch_ = branch;
122 auto&
Set(
this auto& self,
const std::string& property,
const std::string& value) {
123 static_cast<SnapshotUpdate&
>(self).SetSummaryProperty(property, value);
135 if (parallelism <= 0) [[unlikely]] {
136 return self.AddError(
137 ErrorKind::kInvalidArgument,
138 "Manifest write parallelism must be greater than 0, but was: {}", parallelism);
141 self.write_manifest_executor_ = std::ref(executor);
142 self.write_manifest_parallelism_ = parallelism;
154 Result<ApplyResult> Apply();
157 Status Finalize(Result<const TableMetadata*> commit_result)
override;
161 std::shared_ptr<DataFile> file;
162 std::optional<int64_t> data_sequence_number;
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);
178 Result<std::vector<ManifestFile>> WriteDeleteManifests(
179 std::span<const ContentFileWithSequenceNumber> files,
180 const std::shared_ptr<PartitionSpec>& spec);
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);
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_; }
215 const std::shared_ptr<Snapshot>& snapshot) {
224 virtual Result<std::vector<ManifestFile>>
Apply(
226 const std::shared_ptr<Snapshot>& snapshot) = 0;
231 virtual std::unordered_map<std::string, std::string>
Summary() = 0;
237 virtual void SetSummaryProperty(
const std::string& property,
const std::string& value);
245 int64_t SnapshotId();
251 Status DeleteFile(
const std::string& path);
253 std::string ManifestPath();
254 std::string ManifestListPath();
256 SnapshotSummaryBuilder BuildManifestCountSummary(
257 std::span<const ManifestFile> manifests, int32_t replaced_manifests_count);
261 Result<std::unordered_map<std::string, std::string>> ComputeSummary(
262 const TableMetadata& previous);
268 SnapshotSummaryBuilder summary_;
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_;
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 ¤t_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
Definition snapshot_update.h:160