|
19 | 19 |
|
20 | 20 | #include "iceberg/snapshot.h" |
21 | 21 |
|
| 22 | +#include <memory> |
| 23 | + |
22 | 24 | #include "iceberg/file_io.h" |
23 | 25 | #include "iceberg/manifest/manifest_list.h" |
24 | 26 | #include "iceberg/manifest/manifest_reader.h" |
@@ -50,67 +52,44 @@ SnapshotRefType SnapshotRef::type() const noexcept { |
50 | 52 | retention); |
51 | 53 | } |
52 | 54 |
|
53 | | -Status SnapshotRef::MinSnapshotsToKeep(std::optional<int32_t> value) { |
54 | | - ICEBERG_PRECHECK(this->type() != SnapshotRefType::kTag, |
55 | | - "Tags do not support setting minSnapshotsToKeep"); |
56 | | - ICEBERG_PRECHECK(!value.has_value() || value.value() > 0, |
57 | | - "Min snapshots to keep must be greater than 0"); |
58 | | - std::get<Branch>(this->retention).min_snapshots_to_keep = value; |
59 | | - return {}; |
60 | | -} |
61 | | - |
62 | | -Status SnapshotRef::MaxSnapshotAgeMs(std::optional<int64_t> value) { |
63 | | - ICEBERG_PRECHECK(this->type() != SnapshotRefType::kTag, |
64 | | - "Tags do not support setting maxSnapshotAgeMs"); |
65 | | - ICEBERG_PRECHECK(!value.has_value() || value.value() > 0, |
66 | | - "Max snapshot age must be greater than 0 ms"); |
67 | | - std::get<Branch>(this->retention).max_snapshot_age_ms = value; |
68 | | - return {}; |
69 | | -} |
70 | | - |
71 | | -Status SnapshotRef::MaxRefAgeMs(std::optional<int64_t> value) { |
72 | | - ICEBERG_PRECHECK(!value.has_value() || value.value() > 0, |
73 | | - "Max reference age must be greater than 0"); |
74 | | - if (this->type() == SnapshotRefType::kBranch) { |
75 | | - std::get<Branch>(this->retention).max_ref_age_ms = value; |
| 55 | +Status SnapshotRef::Validate() const { |
| 56 | + if (type() == SnapshotRefType::kBranch) { |
| 57 | + const auto& branch = std::get<Branch>(this->retention); |
| 58 | + ICEBERG_CHECK(!branch.min_snapshots_to_keep.has_value() || |
| 59 | + branch.min_snapshots_to_keep.value() > 0, |
| 60 | + "Min snapshots to keep must be greater than 0"); |
| 61 | + ICEBERG_CHECK( |
| 62 | + !branch.max_snapshot_age_ms.has_value() || branch.max_snapshot_age_ms.value() > 0, |
| 63 | + "Max snapshot age must be greater than 0 ms"); |
| 64 | + ICEBERG_CHECK(!branch.max_ref_age_ms.has_value() || branch.max_ref_age_ms.value() > 0, |
| 65 | + "Max reference age must be greater than 0"); |
76 | 66 | } else { |
77 | | - std::get<Tag>(this->retention).max_ref_age_ms = value; |
| 67 | + const auto& tag = std::get<Tag>(this->retention); |
| 68 | + ICEBERG_CHECK(!tag.max_ref_age_ms.has_value() || tag.max_ref_age_ms.value() > 0, |
| 69 | + "Max reference age must be greater than 0"); |
78 | 70 | } |
79 | 71 | return {}; |
80 | 72 | } |
81 | 73 |
|
82 | 74 | Result<std::unique_ptr<SnapshotRef>> SnapshotRef::MakeBranch( |
83 | 75 | int64_t snapshot_id, std::optional<int32_t> min_snapshots_to_keep, |
84 | 76 | std::optional<int64_t> max_snapshot_age_ms, std::optional<int64_t> max_ref_age_ms) { |
85 | | - // Validate optional parameters |
86 | | - if (min_snapshots_to_keep.has_value() && min_snapshots_to_keep.value() <= 0) { |
87 | | - return InvalidArgument("Min snapshots to keep must be greater than 0"); |
88 | | - } |
89 | | - if (max_snapshot_age_ms.has_value() && max_snapshot_age_ms.value() <= 0) { |
90 | | - return InvalidArgument("Max snapshot age must be greater than 0 ms"); |
91 | | - } |
92 | | - if (max_ref_age_ms.has_value() && max_ref_age_ms.value() <= 0) { |
93 | | - return InvalidArgument("Max reference age must be greater than 0"); |
94 | | - } |
95 | | - |
96 | | - auto ref = std::make_unique<SnapshotRef>(); |
97 | | - ref->snapshot_id = snapshot_id; |
98 | | - ref->retention = Branch{.min_snapshots_to_keep = min_snapshots_to_keep, |
99 | | - .max_snapshot_age_ms = max_snapshot_age_ms, |
100 | | - .max_ref_age_ms = max_ref_age_ms}; |
| 77 | + auto ref = std::make_unique<SnapshotRef>( |
| 78 | + SnapshotRef{.snapshot_id = snapshot_id, |
| 79 | + .retention = Branch{ |
| 80 | + .min_snapshots_to_keep = min_snapshots_to_keep, |
| 81 | + .max_snapshot_age_ms = max_snapshot_age_ms, |
| 82 | + .max_ref_age_ms = max_ref_age_ms, |
| 83 | + }}); |
| 84 | + ICEBERG_RETURN_UNEXPECTED(ref->Validate()); |
101 | 85 | return ref; |
102 | 86 | } |
103 | 87 |
|
104 | 88 | Result<std::unique_ptr<SnapshotRef>> SnapshotRef::MakeTag( |
105 | 89 | int64_t snapshot_id, std::optional<int64_t> max_ref_age_ms) { |
106 | | - // Validate optional parameter |
107 | | - if (max_ref_age_ms.has_value() && max_ref_age_ms.value() <= 0) { |
108 | | - return InvalidArgument("Max reference age must be greater than 0"); |
109 | | - } |
110 | | - |
111 | | - auto ref = std::make_unique<SnapshotRef>(); |
112 | | - ref->snapshot_id = snapshot_id; |
113 | | - ref->retention = Tag{.max_ref_age_ms = max_ref_age_ms}; |
| 90 | + auto ref = std::make_unique<SnapshotRef>(SnapshotRef{ |
| 91 | + .snapshot_id = snapshot_id, .retention = Tag{.max_ref_age_ms = max_ref_age_ms}}); |
| 92 | + ICEBERG_RETURN_UNEXPECTED(ref->Validate()); |
114 | 93 | return ref; |
115 | 94 | } |
116 | 95 |
|
@@ -140,7 +119,7 @@ bool SnapshotRef::Equals(const SnapshotRef& other) const { |
140 | 119 | } |
141 | 120 | } |
142 | 121 |
|
143 | | -std::optional<std::string_view> Snapshot::operation() const { |
| 122 | +std::optional<std::string_view> Snapshot::Operation() const { |
144 | 123 | auto it = summary.find(SnapshotSummaryFields::kOperation); |
145 | 124 | if (it != summary.end()) { |
146 | 125 | return it->second; |
@@ -176,6 +155,37 @@ bool Snapshot::Equals(const Snapshot& other) const { |
176 | 155 | schema_id == other.schema_id; |
177 | 156 | } |
178 | 157 |
|
| 158 | +Result<std::unique_ptr<Snapshot>> Snapshot::Make( |
| 159 | + int64_t sequence_number, int64_t snapshot_id, |
| 160 | + std::optional<int64_t> parent_snapshot_id, TimePointMs timestamp_ms, |
| 161 | + std::string operation, std::unordered_map<std::string, std::string> summary, |
| 162 | + std::optional<int32_t> schema_id, std::string manifest_list, |
| 163 | + std::optional<int64_t> first_row_id, std::optional<int64_t> added_rows) { |
| 164 | + ICEBERG_PRECHECK(!operation.empty(), "Operation cannot be empty"); |
| 165 | + ICEBERG_PRECHECK(!first_row_id.has_value() || first_row_id.value() >= 0, |
| 166 | + "Invalid first-row-id (cannot be negative): {}", first_row_id.value()); |
| 167 | + ICEBERG_PRECHECK(!added_rows.has_value() || added_rows.value() >= 0, |
| 168 | + "Invalid added-rows (cannot be negative): {}", added_rows.value()); |
| 169 | + ICEBERG_PRECHECK(!first_row_id.has_value() || added_rows.has_value(), |
| 170 | + "Missing added-rows when first-row-id is set"); |
| 171 | + summary[SnapshotSummaryFields::kOperation] = operation; |
| 172 | + if (first_row_id.has_value()) { |
| 173 | + summary[SnapshotSummaryFields::kFirstRowId] = std::to_string(first_row_id.value()); |
| 174 | + } |
| 175 | + if (added_rows.has_value()) { |
| 176 | + summary[SnapshotSummaryFields::kAddedRows] = std::to_string(added_rows.value()); |
| 177 | + } |
| 178 | + return std::make_unique<Snapshot>(Snapshot{ |
| 179 | + .snapshot_id = snapshot_id, |
| 180 | + .parent_snapshot_id = parent_snapshot_id, |
| 181 | + .sequence_number = sequence_number, |
| 182 | + .timestamp_ms = timestamp_ms, |
| 183 | + .manifest_list = std::move(manifest_list), |
| 184 | + .summary = std::move(summary), |
| 185 | + .schema_id = schema_id, |
| 186 | + }); |
| 187 | +} |
| 188 | + |
179 | 189 | Result<SnapshotCache::ManifestsCache> SnapshotCache::InitManifestsCache( |
180 | 190 | const Snapshot* snapshot, std::shared_ptr<FileIO> file_io) { |
181 | 191 | if (file_io == nullptr) { |
|
0 commit comments