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
35#include "iceberg/result.h"
36#include "iceberg/type_fwd.h"
37#include "iceberg/util/lazy.h"
39
40namespace iceberg {
41
43enum class SnapshotRefType {
47 kBranch,
49 kTag,
50};
51
53ICEBERG_EXPORT constexpr std::string_view ToString(SnapshotRefType type) noexcept {
54 switch (type) {
56 return "branch";
58 return "tag";
59 }
60 std::unreachable();
61}
64 std::string_view str) noexcept {
65 if (str == "branch") return SnapshotRefType::kBranch;
66 if (str == "tag") return SnapshotRefType::kTag;
67 return InvalidArgument("Invalid snapshot reference type: {}", str);
68}
69
71struct ICEBERG_EXPORT SnapshotRef {
72 static constexpr std::string_view kMainBranch = "main";
73
74 struct ICEBERG_EXPORT Branch {
78 std::optional<int32_t> min_snapshots_to_keep;
82 std::optional<int64_t> max_snapshot_age_ms;
86 std::optional<int64_t> max_ref_age_ms;
87
89 friend bool operator==(const Branch& lhs, const Branch& rhs) {
90 return lhs.Equals(rhs);
91 }
92
93 private:
95 bool Equals(const Branch& other) const;
96 };
97
98 struct ICEBERG_EXPORT Tag {
102 std::optional<int64_t> max_ref_age_ms;
103
105 friend bool operator==(const Tag& lhs, const Tag& rhs) { return lhs.Equals(rhs); }
106
107 private:
109 bool Equals(const Tag& other) const;
110 };
111
113 int64_t snapshot_id;
115 std::variant<Branch, Tag> retention;
116
117 SnapshotRefType type() const noexcept;
118
119 std::optional<int64_t> max_ref_age_ms() const noexcept;
120
129 static Result<std::unique_ptr<SnapshotRef>> MakeBranch(
130 int64_t snapshot_id, std::optional<int32_t> min_snapshots_to_keep = std::nullopt,
131 std::optional<int64_t> max_snapshot_age_ms = std::nullopt,
132 std::optional<int64_t> max_ref_age_ms = std::nullopt);
133
140 static Result<std::unique_ptr<SnapshotRef>> MakeTag(
141 int64_t snapshot_id, std::optional<int64_t> max_ref_age_ms = std::nullopt);
142
148 std::unique_ptr<SnapshotRef> Clone(
149 std::optional<int64_t> new_snapshot_id = std::nullopt) const;
150
152 Status Validate() const;
153
155 friend bool operator==(const SnapshotRef& lhs, const SnapshotRef& rhs) {
156 return lhs.Equals(rhs);
157 }
158
159 private:
161 bool Equals(const SnapshotRef& other) const;
162};
163
165struct ICEBERG_EXPORT SnapshotSummaryFields {
167 inline static const std::string kOperation = "operation";
169 inline static const std::string kFirstRowId = "first-row-id";
171 inline static const std::string kAddedRows = "added-rows";
172
174
176 inline static const std::string kAddedDataFiles = "added-data-files";
178 inline static const std::string kDeletedDataFiles = "deleted-data-files";
180 inline static const std::string kTotalDataFiles = "total-data-files";
183 inline static const std::string kAddedDeleteFiles = "added-delete-files";
185 inline static const std::string kAddedEqDeleteFiles = "added-equality-delete-files";
187 inline static const std::string kRemovedEqDeleteFiles = "removed-equality-delete-files";
189 inline static const std::string kAddedPosDeleteFiles = "added-position-delete-files";
191 inline static const std::string kRemovedPosDeleteFiles =
192 "removed-position-delete-files";
194 inline static const std::string kAddedDVs = "added-dvs";
196 inline static const std::string kRemovedDVs = "removed-dvs";
199 inline static const std::string kRemovedDeleteFiles = "removed-delete-files";
202 inline static const std::string kTotalDeleteFiles = "total-delete-files";
204 inline static const std::string kAddedRecords = "added-records";
206 inline static const std::string kDeletedRecords = "deleted-records";
208 inline static const std::string kTotalRecords = "total-records";
210 inline static const std::string kAddedFileSize = "added-files-size";
212 inline static const std::string kRemovedFileSize = "removed-files-size";
214 inline static const std::string kTotalFileSize = "total-files-size";
216 inline static const std::string kAddedPosDeletes = "added-position-deletes";
218 inline static const std::string kRemovedPosDeletes = "removed-position-deletes";
220 inline static const std::string kTotalPosDeletes = "total-position-deletes";
222 inline static const std::string kAddedEqDeletes = "added-equality-deletes";
224 inline static const std::string kRemovedEqDeletes = "removed-equality-deletes";
226 inline static const std::string kTotalEqDeletes = "total-equality-deletes";
229 inline static const std::string kDeletedDuplicatedFiles = "deleted-duplicate-files";
231 inline static const std::string kChangedPartitionCountProp = "changed-partition-count";
233 inline static const std::string kManifestsCreated = "manifests-created";
235 inline static const std::string kManifestsKept = "manifests-kept";
237 inline static const std::string kManifestsReplaced = "manifests-replaced";
239 inline static const std::string kEntriesProcessed = "entries-processed";
241 inline static const std::string kChangedPartitionPrefix = "partitions.";
243 inline static const std::string kPartitionSummaryProp = "partition-summaries-included";
244
246
248 inline static const std::string kWAPId = "wap.id";
250 inline static const std::string kPublishedWAPId = "published-wap-id";
252 inline static const std::string kSourceSnapshotId = "source-snapshot-id";
254 inline static const std::string kEngineName = "engine-name";
256 inline static const std::string kEngineVersion = "engine-version";
258 inline static const std::string kReplacePartitions = "replace-partitions";
259};
260
265class ICEBERG_EXPORT SnapshotSummaryBuilder {
266 private:
268 class UpdateMetrics {
269 public:
270 void Clear();
271 void AddTo(std::unordered_map<std::string, std::string>& builder) const;
272 void AddedFile(const DataFile& file);
273 void RemovedFile(const DataFile& file);
274 void AddedManifest(const ManifestFile& manifest);
275 void Merge(const UpdateMetrics& other);
276
277 private:
278 int64_t added_size_{0};
279 int64_t removed_size_{0};
280 int32_t added_files_{0};
281 int32_t removed_files_{0};
282 int32_t added_eq_delete_files_{0};
283 int32_t removed_eq_delete_files_{0};
284 int32_t added_pos_delete_files_{0};
285 int32_t removed_pos_delete_files_{0};
286 int32_t added_dvs_{0};
287 int32_t removed_dvs_{0};
288 int32_t added_delete_files_{0};
289 int32_t removed_delete_files_{0};
290 int64_t added_records_{0};
291 int64_t deleted_records_{0};
292 int64_t added_pos_deletes_{0};
293 int64_t removed_pos_deletes_{0};
294 int64_t added_eq_deletes_{0};
295 int64_t removed_eq_deletes_{0};
296 bool trust_size_and_delete_counts_{true};
297 };
298
299 public:
300 SnapshotSummaryBuilder() = default;
301
303 void Clear();
304
314 void SetPartitionSummaryLimit(int32_t max);
315
319 void IncrementDuplicateDeletes(int32_t increment = 1);
320
326 Status AddedFile(const PartitionSpec& spec, const DataFile& file);
327
333 Status DeletedFile(const PartitionSpec& spec, const DataFile& file);
334
338 void AddedManifest(const ManifestFile& manifest);
339
344 void Set(const std::string& property, const std::string& value);
345
349 void Merge(const SnapshotSummaryBuilder& other);
350
354 std::unordered_map<std::string, std::string> Build() const;
355
356 private:
357 Status UpdatePartitions(const PartitionSpec& spec, const DataFile& file,
358 bool is_addition);
359 std::string PartitionSummary(const UpdateMetrics& metrics) const;
360
361 std::unordered_map<std::string, std::string> properties_;
362 std::unordered_map<std::string, UpdateMetrics> partition_metrics_;
363 UpdateMetrics metrics_;
364 int32_t max_changed_partitions_for_summaries_{0};
365 int64_t deleted_duplicate_files_{0};
366 bool trust_partition_metrics_{true};
367};
368
374struct ICEBERG_EXPORT DataOperation {
376 inline static const std::string kAppend = "append";
379 inline static const std::string kReplace = "replace";
382 inline static const std::string kOverwrite = "overwrite";
385 inline static const std::string kDelete = "delete";
386};
387
394struct ICEBERG_EXPORT Snapshot {
396 int64_t snapshot_id;
398 std::optional<int64_t> parent_snapshot_id;
406 std::string manifest_list;
408 std::unordered_map<std::string, std::string> summary;
410 std::optional<int32_t> schema_id;
412 std::optional<int64_t> first_row_id;
414 std::optional<int64_t> added_rows;
415
418 int64_t sequence_number, int64_t snapshot_id,
419 std::optional<int64_t> parent_snapshot_id, TimePointMs timestamp_ms,
420 std::string operation, std::unordered_map<std::string, std::string> summary,
421 std::optional<int32_t> schema_id, std::string manifest_list,
422 std::optional<int64_t> first_row_id = std::nullopt,
423 std::optional<int64_t> added_rows = std::nullopt);
424
430 std::optional<std::string_view> Operation() const;
431
442
454
456 friend bool operator==(const Snapshot& lhs, const Snapshot& rhs) {
457 return lhs.Equals(rhs);
458 }
459
460 private:
462 bool Equals(const Snapshot& other) const;
463};
464
468class ICEBERG_EXPORT SnapshotCache {
469 public:
470 explicit SnapshotCache(const Snapshot* snapshot) : snapshot_(snapshot) {}
471
473 const Snapshot& snapshot() const { return *snapshot_; }
474
480 Result<std::span<ManifestFile>> Manifests(std::shared_ptr<FileIO> file_io) const;
481
486 Result<std::span<ManifestFile>> DataManifests(std::shared_ptr<FileIO> file_io) const;
487
492 Result<std::span<ManifestFile>> DeleteManifests(std::shared_ptr<FileIO> file_io) const;
493
494 private:
499 using ManifestsCache = std::pair<std::vector<ManifestFile>, size_t>;
500
505 static Result<ManifestsCache> InitManifestsCache(const Snapshot* snapshot,
506 std::shared_ptr<FileIO> file_io);
507
509 const Snapshot* snapshot_;
510
512 Lazy<InitManifestsCache> manifests_cache_;
513};
514
515} // namespace iceberg
Definition lazy.h:36
A partition spec for a Table.
Definition partition_spec.h:47
A snapshot with cached manifest loading capabilities.
Definition snapshot.h:468
const Snapshot & snapshot() const
Get the underlying Snapshot reference.
Definition snapshot.h:473
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.
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 > > DataManifests(std::shared_ptr< FileIO > file_io) const
Returns a ManifestFile for each data manifest in this snapshot.
Helper class for building snapshot summaries.
Definition snapshot.h:265
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.
Define symbol visibility macros for core Iceberg APIs.
Core Apache Iceberg C++ APIs.
Definition arrow_io_util.h:33
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:63
ICEBERG_EXPORT std::string_view ToString(Expression::Operation op)
Returns a string representation of an expression operation.
SnapshotRefType
The type of snapshot reference.
Definition snapshot.h:43
@ kTag
Tags are labels for individual snapshots.
std::expected< T, E > Result
Result alias.
Definition result.h:88
STL namespace.
Define Result, Status, and error helpers.
DataFile carries data file path, partition tuple, metrics, ...
Definition manifest_entry.h:62
Data operation that produce snapshots.
Definition snapshot.h:374
Entry in a manifest list.
Definition manifest_list.h:85
Definition snapshot.h:74
std::optional< int32_t > min_snapshots_to_keep
Definition snapshot.h:78
std::optional< int64_t > max_snapshot_age_ms
Definition snapshot.h:82
std::optional< int64_t > max_ref_age_ms
Definition snapshot.h:86
friend bool operator==(const Branch &lhs, const Branch &rhs)
Compare two branches for equality.
Definition snapshot.h:89
Definition snapshot.h:98
std::optional< int64_t > max_ref_age_ms
Definition snapshot.h:102
friend bool operator==(const Tag &lhs, const Tag &rhs)
Compare two tags for equality.
Definition snapshot.h:105
A reference to a snapshot, either a branch or a tag.
Definition snapshot.h:71
int64_t snapshot_id
A reference's snapshot ID. The tagged snapshot or latest snapshot of a branch.
Definition snapshot.h:113
std::variant< Branch, Tag > retention
Snapshot retention policy.
Definition snapshot.h:115
Optional Snapshot Summary Fields.
Definition snapshot.h:165
A snapshot of the data in a table at a point in time.
Definition snapshot.h:394
friend bool operator==(const Snapshot &lhs, const Snapshot &rhs)
Compare two snapshots for equality.
Definition snapshot.h:456
int64_t snapshot_id
A unique long ID.
Definition snapshot.h:396
std::optional< int64_t > first_row_id
The row-id of the first newly added row in this snapshot.
Definition snapshot.h:412
int64_t sequence_number
A monotonically increasing long that tracks the order of changes to a table.
Definition snapshot.h:400
std::optional< int32_t > schema_id
ID of the table's current schema when the snapshot was created.
Definition snapshot.h:410
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:403
std::optional< int64_t > added_rows
The upper bound of rows with assigned row IDs in this snapshot.
Definition snapshot.h:414
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:398
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:406
std::unordered_map< std::string, std::string > summary
A string map that summaries the snapshot changes, including operation.
Definition snapshot.h:408
Provide time point conversion helpers.