iceberg-cpp
Loading...
Searching...
No Matches
snapshot.h
Go to the documentation of this file.
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
24
25#include <memory>
26#include <optional>
27#include <span>
28#include <string>
29#include <string_view>
30#include <unordered_map>
31#include <variant>
32
36#include "iceberg/result.h"
37#include "iceberg/type_fwd.h"
38#include "iceberg/util/lazy.h"
40
41namespace iceberg {
42
44enum class SnapshotRefType {
48 kBranch,
50 kTag,
51};
52
54ICEBERG_EXPORT constexpr std::string_view ToString(SnapshotRefType type) noexcept {
55 switch (type) {
57 return "branch";
59 return "tag";
60 }
62}
65 std::string_view str) noexcept {
66 if (str == "branch") return SnapshotRefType::kBranch;
67 if (str == "tag") return SnapshotRefType::kTag;
68 return InvalidArgument("Invalid snapshot reference type: {}", str);
69}
70
72struct ICEBERG_EXPORT SnapshotRef {
73 static constexpr std::string_view kMainBranch = "main";
74
75 struct ICEBERG_EXPORT Branch {
79 std::optional<int32_t> min_snapshots_to_keep;
83 std::optional<int64_t> max_snapshot_age_ms;
87 std::optional<int64_t> max_ref_age_ms;
88
90 friend bool operator==(const Branch& lhs, const Branch& rhs) {
91 return lhs.Equals(rhs);
92 }
93
94 private:
96 bool Equals(const Branch& other) const;
97 };
98
99 struct ICEBERG_EXPORT Tag {
103 std::optional<int64_t> max_ref_age_ms;
104
106 friend bool operator==(const Tag& lhs, const Tag& rhs) { return lhs.Equals(rhs); }
107
108 private:
110 bool Equals(const Tag& other) const;
111 };
112
114 int64_t snapshot_id;
116 std::variant<Branch, Tag> retention;
117
118 SnapshotRefType type() const noexcept;
119
120 std::optional<int64_t> max_ref_age_ms() const noexcept;
121
130 static Result<std::unique_ptr<SnapshotRef>> MakeBranch(
131 int64_t snapshot_id, std::optional<int32_t> min_snapshots_to_keep = std::nullopt,
132 std::optional<int64_t> max_snapshot_age_ms = std::nullopt,
133 std::optional<int64_t> max_ref_age_ms = std::nullopt);
134
141 static Result<std::unique_ptr<SnapshotRef>> MakeTag(
142 int64_t snapshot_id, std::optional<int64_t> max_ref_age_ms = std::nullopt);
143
149 std::unique_ptr<SnapshotRef> Clone(
150 std::optional<int64_t> new_snapshot_id = std::nullopt) const;
151
153 Status Validate() const;
154
156 friend bool operator==(const SnapshotRef& lhs, const SnapshotRef& rhs) {
157 return lhs.Equals(rhs);
158 }
159
160 private:
162 bool Equals(const SnapshotRef& other) const;
163};
164
166struct ICEBERG_EXPORT SnapshotSummaryFields {
168 inline static const std::string kOperation = "operation";
170 inline static const std::string kFirstRowId = "first-row-id";
172 inline static const std::string kAddedRows = "added-rows";
173
175
177 inline static const std::string kAddedDataFiles = "added-data-files";
179 inline static const std::string kDeletedDataFiles = "deleted-data-files";
181 inline static const std::string kTotalDataFiles = "total-data-files";
184 inline static const std::string kAddedDeleteFiles = "added-delete-files";
186 inline static const std::string kAddedEqDeleteFiles = "added-equality-delete-files";
188 inline static const std::string kRemovedEqDeleteFiles = "removed-equality-delete-files";
190 inline static const std::string kAddedPosDeleteFiles = "added-position-delete-files";
192 inline static const std::string kRemovedPosDeleteFiles =
193 "removed-position-delete-files";
195 inline static const std::string kAddedDVs = "added-dvs";
197 inline static const std::string kRemovedDVs = "removed-dvs";
200 inline static const std::string kRemovedDeleteFiles = "removed-delete-files";
203 inline static const std::string kTotalDeleteFiles = "total-delete-files";
205 inline static const std::string kAddedRecords = "added-records";
207 inline static const std::string kDeletedRecords = "deleted-records";
209 inline static const std::string kTotalRecords = "total-records";
211 inline static const std::string kAddedFileSize = "added-files-size";
213 inline static const std::string kRemovedFileSize = "removed-files-size";
215 inline static const std::string kTotalFileSize = "total-files-size";
217 inline static const std::string kAddedPosDeletes = "added-position-deletes";
219 inline static const std::string kRemovedPosDeletes = "removed-position-deletes";
221 inline static const std::string kTotalPosDeletes = "total-position-deletes";
223 inline static const std::string kAddedEqDeletes = "added-equality-deletes";
225 inline static const std::string kRemovedEqDeletes = "removed-equality-deletes";
227 inline static const std::string kTotalEqDeletes = "total-equality-deletes";
230 inline static const std::string kDeletedDuplicatedFiles = "deleted-duplicate-files";
232 inline static const std::string kChangedPartitionCountProp = "changed-partition-count";
234 inline static const std::string kManifestsCreated = "manifests-created";
236 inline static const std::string kManifestsKept = "manifests-kept";
238 inline static const std::string kManifestsReplaced = "manifests-replaced";
240 inline static const std::string kEntriesProcessed = "entries-processed";
242 inline static const std::string kChangedPartitionPrefix = "partitions.";
244 inline static const std::string kPartitionSummaryProp = "partition-summaries-included";
245
247
249 inline static const std::string kWAPId = "wap.id";
251 inline static const std::string kPublishedWAPId = "published-wap-id";
253 inline static const std::string kSourceSnapshotId = "source-snapshot-id";
255 inline static const std::string kEngineName = "engine-name";
257 inline static const std::string kEngineVersion = "engine-version";
259 inline static const std::string kReplacePartitions = "replace-partitions";
260};
261
266class ICEBERG_EXPORT SnapshotSummaryBuilder {
267 private:
269 class UpdateMetrics {
270 public:
271 void Clear();
272 void AddTo(std::unordered_map<std::string, std::string>& builder) const;
273 void AddedFile(const DataFile& file);
274 void RemovedFile(const DataFile& file);
275 void AddedManifest(const ManifestFile& manifest);
276 void Merge(const UpdateMetrics& other);
277
278 private:
279 int64_t added_size_{0};
280 int64_t removed_size_{0};
281 int32_t added_files_{0};
282 int32_t removed_files_{0};
283 int32_t added_eq_delete_files_{0};
284 int32_t removed_eq_delete_files_{0};
285 int32_t added_pos_delete_files_{0};
286 int32_t removed_pos_delete_files_{0};
287 int32_t added_dvs_{0};
288 int32_t removed_dvs_{0};
289 int32_t added_delete_files_{0};
290 int32_t removed_delete_files_{0};
291 int64_t added_records_{0};
292 int64_t deleted_records_{0};
293 int64_t added_pos_deletes_{0};
294 int64_t removed_pos_deletes_{0};
295 int64_t added_eq_deletes_{0};
296 int64_t removed_eq_deletes_{0};
297 bool trust_size_and_delete_counts_{true};
298 };
299
300 public:
301 SnapshotSummaryBuilder() = default;
302
304 void Clear();
305
315 void SetPartitionSummaryLimit(int32_t max);
316
320 void IncrementDuplicateDeletes(int32_t increment = 1);
321
327 Status AddedFile(const PartitionSpec& spec, const DataFile& file);
328
334 Status DeletedFile(const PartitionSpec& spec, const DataFile& file);
335
339 void AddedManifest(const ManifestFile& manifest);
340
345 void Set(const std::string& property, const std::string& value);
346
350 void Merge(const SnapshotSummaryBuilder& other);
351
355 std::unordered_map<std::string, std::string> Build() const;
356
357 private:
358 Status UpdatePartitions(const PartitionSpec& spec, const DataFile& file,
359 bool is_addition);
360 std::string PartitionSummary(const UpdateMetrics& metrics) const;
361
362 std::unordered_map<std::string, std::string> properties_;
363 std::unordered_map<std::string, UpdateMetrics> partition_metrics_;
364 UpdateMetrics metrics_;
365 int32_t max_changed_partitions_for_summaries_{0};
366 int64_t deleted_duplicate_files_{0};
367 bool trust_partition_metrics_{true};
368};
369
375struct ICEBERG_EXPORT DataOperation {
377 inline static const std::string kAppend = "append";
380 inline static const std::string kReplace = "replace";
383 inline static const std::string kOverwrite = "overwrite";
386 inline static const std::string kDelete = "delete";
387};
388
395struct ICEBERG_EXPORT Snapshot {
397 int64_t snapshot_id;
399 std::optional<int64_t> parent_snapshot_id;
407 std::string manifest_list;
409 std::unordered_map<std::string, std::string> summary;
411 std::optional<int32_t> schema_id;
413 std::optional<int64_t> first_row_id;
415 std::optional<int64_t> added_rows;
418 std::optional<std::string> key_id;
419
422 int64_t sequence_number, int64_t snapshot_id,
423 std::optional<int64_t> parent_snapshot_id, TimePointMs timestamp_ms,
424 std::string operation, std::unordered_map<std::string, std::string> summary,
425 std::optional<int32_t> schema_id, std::string manifest_list,
426 std::optional<int64_t> first_row_id = std::nullopt,
427 std::optional<int64_t> added_rows = std::nullopt);
428
434 std::optional<std::string_view> Operation() const;
435
446
458
460 friend bool operator==(const Snapshot& lhs, const Snapshot& rhs) {
461 return lhs.Equals(rhs);
462 }
463
464 private:
466 bool Equals(const Snapshot& other) const;
467};
468
471class ICEBERG_EXPORT SnapshotReader {
472 public:
473 explicit SnapshotReader(const Snapshot* snapshot) : snapshot_(snapshot) {}
474
476 const Snapshot& snapshot() const { return *snapshot_; }
477
483 Result<std::span<ManifestFile>> Manifests(std::shared_ptr<FileIO> file_io) const;
484
489 Result<std::span<ManifestFile>> DataManifests(std::shared_ptr<FileIO> file_io) const;
490
495 Result<std::span<ManifestFile>> DeleteManifests(std::shared_ptr<FileIO> file_io) const;
496
497 private:
502 using ManifestsCache = std::pair<std::vector<ManifestFile>, size_t>;
503
508 static Result<ManifestsCache> InitManifestsCache(const Snapshot* snapshot,
509 std::shared_ptr<FileIO> file_io);
510
512 const Snapshot* snapshot_;
513
515 Lazy<InitManifestsCache> manifests_cache_;
516};
517
518} // namespace iceberg
Definition lazy.h:36
A partition spec for a Table.
Definition partition_spec.h:47
Reads a snapshot's manifests, loading and caching its manifest list on first access.
Definition snapshot.h:471
Result< std::span< ManifestFile > > DataManifests(std::shared_ptr< FileIO > file_io) const
Returns a ManifestFile for each data manifest in this snapshot.
const Snapshot & snapshot() const
Get the underlying Snapshot reference.
Definition snapshot.h:476
Result< std::span< ManifestFile > > DeleteManifests(std::shared_ptr< FileIO > file_io) const
Returns a ManifestFile for each delete manifest in this snapshot.
Result< std::span< ManifestFile > > Manifests(std::shared_ptr< FileIO > file_io) const
Returns all ManifestFile instances for either data or delete manifests in this snapshot.
Helper class for building snapshot summaries.
Definition snapshot.h:266
void Clear()
Clear all tracked metrics and properties.
void IncrementDuplicateDeletes(int32_t increment=1)
Increment the count of duplicate files deleted by a specific amount.
Status DeletedFile(const PartitionSpec &spec, const DataFile &file)
Track a data file being deleted from the snapshot.
void AddedManifest(const ManifestFile &manifest)
Track a manifest being added.
Status AddedFile(const PartitionSpec &spec, const DataFile &file)
Track a data file being added to the snapshot.
void Merge(const SnapshotSummaryBuilder &other)
Merge another builder's metrics into this one.
std::unordered_map< std::string, std::string > Build() const
Build the final summary map.
void SetPartitionSummaryLimit(int32_t max)
Set the maximum number of changed partitions before partition summaries will be excluded.
void Set(const std::string &property, const std::string &value)
Set a custom summary property.
Definition expected.h:1199
Define symbol visibility macros for core Iceberg APIs.
Core Apache Iceberg C++ APIs.
Definition arrow_io_util.h:34
std::chrono::time_point< std::chrono::system_clock, std::chrono::milliseconds > TimePointMs
A time point in milliseconds.
Definition timepoint.h:33
ICEBERG_EXPORT constexpr Result< SnapshotRefType > SnapshotRefTypeFromString(std::string_view str) noexcept
Get the relative snapshot reference type from name.
Definition snapshot.h:64
ICEBERG_EXPORT std::string_view ToString(Expression::Operation op)
Returns a string representation of an expression operation.
void unreachable() noexcept
Marks a branch the program can never take.
Definition unreachable.h:35
SnapshotRefType
The type of snapshot reference.
Definition snapshot.h:44
@ kTag
Tags are labels for individual snapshots.
STL namespace.
Define Result, Status, and error helpers.
DataFile carries data file path, partition tuple, metrics, ...
Definition manifest_entry.h:63
Data operation that produce snapshots.
Definition snapshot.h:375
Entry in a manifest list.
Definition manifest_list.h:86
Definition snapshot.h:75
std::optional< int32_t > min_snapshots_to_keep
Definition snapshot.h:79
std::optional< int64_t > max_snapshot_age_ms
Definition snapshot.h:83
std::optional< int64_t > max_ref_age_ms
Definition snapshot.h:87
friend bool operator==(const Branch &lhs, const Branch &rhs)
Compare two branches for equality.
Definition snapshot.h:90
Definition snapshot.h:99
std::optional< int64_t > max_ref_age_ms
Definition snapshot.h:103
friend bool operator==(const Tag &lhs, const Tag &rhs)
Compare two tags for equality.
Definition snapshot.h:106
A reference to a snapshot, either a branch or a tag.
Definition snapshot.h:72
int64_t snapshot_id
A reference's snapshot ID. The tagged snapshot or latest snapshot of a branch.
Definition snapshot.h:114
std::variant< Branch, Tag > retention
Snapshot retention policy.
Definition snapshot.h:116
Optional Snapshot Summary Fields.
Definition snapshot.h:166
A snapshot of the data in a table at a point in time.
Definition snapshot.h:395
friend bool operator==(const Snapshot &lhs, const Snapshot &rhs)
Compare two snapshots for equality.
Definition snapshot.h:460
int64_t snapshot_id
A unique long ID.
Definition snapshot.h:397
std::optional< int64_t > first_row_id
The row-id of the first newly added row in this snapshot.
Definition snapshot.h:413
int64_t sequence_number
A monotonically increasing long that tracks the order of changes to a table.
Definition snapshot.h:401
std::optional< int32_t > schema_id
ID of the table's current schema when the snapshot was created.
Definition snapshot.h:411
static Result< std::unique_ptr< Snapshot > > Make(int64_t sequence_number, int64_t snapshot_id, std::optional< int64_t > parent_snapshot_id, TimePointMs timestamp_ms, std::string operation, std::unordered_map< std::string, std::string > summary, std::optional< int32_t > schema_id, std::string manifest_list, std::optional< int64_t > first_row_id=std::nullopt, std::optional< int64_t > added_rows=std::nullopt)
Create a new Snapshot instance with validation on the inputs.
Result< std::optional< int64_t > > AddedRows() const
The upper bound of number of rows with assigned row IDs in this snapshot.
TimePointMs timestamp_ms
Definition snapshot.h:404
std::optional< int64_t > added_rows
The upper bound of rows with assigned row IDs in this snapshot.
Definition snapshot.h:415
std::optional< std::string > key_id
Definition snapshot.h:418
std::optional< std::string_view > Operation() const
Return the name of the DataOperations data operation that produced this snapshot.
std::optional< int64_t > parent_snapshot_id
The snapshot ID of the snapshot's parent. Omitted for any snapshot with no parent.
Definition snapshot.h:399
Result< std::optional< int64_t > > FirstRowId() const
The row-id of the first newly added row in this snapshot.
std::string manifest_list
Definition snapshot.h:407
std::unordered_map< std::string, std::string > summary
A string map that summaries the snapshot changes, including operation.
Definition snapshot.h:409
Provide time point conversion helpers.
Portable stand-in for std::unreachable.