Skip to content

Commit 9cb2094

Browse files
committed
feat: add UpdateSnapshotReference
test cases will be added in the SnapshotManager PR.
1 parent 437f252 commit 9cb2094

File tree

12 files changed

+439
-1
lines changed

12 files changed

+439
-1
lines changed

src/iceberg/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ set(ICEBERG_SOURCES
9292
update/update_properties.cc
9393
update/update_schema.cc
9494
update/update_sort_order.cc
95+
update/update_snapshot_reference.cc
9596
util/bucket_util.cc
9697
util/content_file_util.cc
9798
util/conversions.cc

src/iceberg/meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ iceberg_sources = files(
109109
'update/update_partition_spec.cc',
110110
'update/update_properties.cc',
111111
'update/update_schema.cc',
112+
'update/update_snapshot_reference.cc',
112113
'update/update_sort_order.cc',
113114
'util/bucket_util.cc',
114115
'util/content_file_util.cc',

src/iceberg/parquet/parquet_data_util.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ Result<std::shared_ptr<::arrow::Array>> ProjectStructArray(
148148
return output_array;
149149
}
150150

151-
/// Templated implementation for projecting list arrays.
151+
/// \brief Templated implementation for projecting list arrays.
152152
/// Works with both ListArray/ListType (32-bit offsets) and
153153
/// LargeListArray/LargeListType (64-bit offsets).
154154
template <typename ArrowListArrayType, typename ArrowListType>

src/iceberg/transaction.cc

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#include "iceberg/update/update_partition_spec.h"
3838
#include "iceberg/update/update_properties.h"
3939
#include "iceberg/update/update_schema.h"
40+
#include "iceberg/update/update_snapshot_reference.h"
4041
#include "iceberg/update/update_sort_order.h"
4142
#include "iceberg/util/checked_cast.h"
4243
#include "iceberg/util/location_util.h"
@@ -189,6 +190,24 @@ Status Transaction::Apply(PendingUpdate& update) {
189190
ICEBERG_ASSIGN_OR_RAISE(auto location, update_location.Apply());
190191
metadata_builder_->SetLocation(location);
191192
} break;
193+
case PendingUpdate::Kind::kUpdateSnapshotReference: {
194+
auto& update_ref = internal::checked_cast<UpdateSnapshotReference&>(update);
195+
ICEBERG_ASSIGN_OR_RAISE(auto updated_refs, update_ref.Apply());
196+
const auto& current_refs = current().refs;
197+
// Identify references which have been removed
198+
for (const auto& [name, ref] : current_refs) {
199+
if (updated_refs.find(name) == updated_refs.end()) {
200+
metadata_builder_->RemoveRef(name);
201+
}
202+
}
203+
// Identify references which have been created or updated
204+
for (const auto& [name, ref] : updated_refs) {
205+
auto current_it = current_refs.find(name);
206+
if (current_it == current_refs.end() || *current_it->second != *ref) {
207+
metadata_builder_->SetRef(name, ref);
208+
}
209+
}
210+
} break;
192211
default:
193212
return NotSupported("Unsupported pending update: {}",
194213
static_cast<int32_t>(update.kind()));
@@ -293,4 +312,12 @@ Result<std::shared_ptr<UpdateLocation>> Transaction::NewUpdateLocation() {
293312
return update_location;
294313
}
295314

315+
Result<std::shared_ptr<UpdateSnapshotReference>>
316+
Transaction::NewUpdateSnapshotReference() {
317+
ICEBERG_ASSIGN_OR_RAISE(std::shared_ptr<UpdateSnapshotReference> update_ref,
318+
UpdateSnapshotReference::Make(shared_from_this()));
319+
ICEBERG_RETURN_UNEXPECTED(AddUpdate(update_ref));
320+
return update_ref;
321+
}
322+
296323
} // namespace iceberg

src/iceberg/transaction.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@ class ICEBERG_EXPORT Transaction : public std::enable_shared_from_this<Transacti
8686
/// changes.
8787
Result<std::shared_ptr<UpdateLocation>> NewUpdateLocation();
8888

89+
/// \brief Create a new UpdateSnapshotReference to update snapshot references (branches
90+
/// and tags) and commit the changes.
91+
Result<std::shared_ptr<UpdateSnapshotReference>> NewUpdateSnapshotReference();
92+
8993
private:
9094
Transaction(std::shared_ptr<Table> table, Kind kind, bool auto_commit,
9195
std::unique_ptr<TableMetadataBuilder> metadata_builder);

src/iceberg/type_fwd.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ class UpdatePartitionSpec;
195195
class UpdateProperties;
196196
class UpdateSchema;
197197
class UpdateSortOrder;
198+
class UpdateSnapshotReference;
198199

199200
/// ----------------------------------------------------------------------------
200201
/// TODO: Forward declarations below are not added yet.

src/iceberg/update/meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ install_headers(
2323
'update_location.h',
2424
'update_partition_spec.h',
2525
'update_schema.h',
26+
'update_snapshot_reference.h',
2627
'update_sort_order.h',
2728
'update_properties.h',
2829
],

src/iceberg/update/pending_update.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ class ICEBERG_EXPORT PendingUpdate : public ErrorCollector {
4949
kUpdateSchema,
5050
kUpdateSnapshot,
5151
kUpdateSortOrder,
52+
kUpdateSnapshotReference,
5253
};
5354

5455
/// \brief Return the kind of this pending update.
Lines changed: 228 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,228 @@
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+
#include "iceberg/update/update_snapshot_reference.h"
21+
22+
#include <memory>
23+
#include <optional>
24+
#include <string>
25+
#include <unordered_map>
26+
27+
#include "iceberg/result.h"
28+
#include "iceberg/snapshot.h"
29+
#include "iceberg/table_metadata.h"
30+
#include "iceberg/transaction.h"
31+
#include "iceberg/util/error_collector.h"
32+
#include "iceberg/util/macros.h"
33+
#include "iceberg/util/snapshot_util_internal.h"
34+
35+
namespace iceberg {
36+
37+
Result<std::shared_ptr<UpdateSnapshotReference>> UpdateSnapshotReference::Make(
38+
std::shared_ptr<Transaction> transaction) {
39+
ICEBERG_PRECHECK(transaction != nullptr,
40+
"Cannot create UpdateSnapshotReference without a transaction");
41+
return std::shared_ptr<UpdateSnapshotReference>(
42+
new UpdateSnapshotReference(std::move(transaction)));
43+
}
44+
45+
UpdateSnapshotReference::UpdateSnapshotReference(std::shared_ptr<Transaction> transaction)
46+
: PendingUpdate(std::move(transaction)) {
47+
// Initialize updated_refs_ with current refs from base metadata
48+
for (const auto& [name, ref] : base().refs) {
49+
updated_refs_[name] = ref;
50+
}
51+
}
52+
53+
UpdateSnapshotReference::~UpdateSnapshotReference() = default;
54+
55+
UpdateSnapshotReference& UpdateSnapshotReference::CreateBranch(const std::string& name,
56+
int64_t snapshot_id) {
57+
ICEBERG_BUILDER_CHECK(!name.empty(), "Branch name cannot be empty");
58+
ICEBERG_BUILDER_ASSIGN_OR_RETURN(auto branch, SnapshotRef::MakeBranch(snapshot_id));
59+
auto [_, inserted] = updated_refs_.emplace(name, std::move(branch));
60+
ICEBERG_BUILDER_CHECK(inserted, "Ref '{}' already exists", name);
61+
return *this;
62+
}
63+
64+
UpdateSnapshotReference& UpdateSnapshotReference::CreateTag(const std::string& name,
65+
int64_t snapshot_id) {
66+
ICEBERG_BUILDER_CHECK(!name.empty(), "Tag name cannot be empty");
67+
ICEBERG_BUILDER_ASSIGN_OR_RETURN(auto tag, SnapshotRef::MakeTag(snapshot_id));
68+
auto [_, inserted] = updated_refs_.emplace(name, std::move(tag));
69+
ICEBERG_BUILDER_CHECK(inserted, "Ref '{}' already exists", name);
70+
return *this;
71+
}
72+
73+
UpdateSnapshotReference& UpdateSnapshotReference::RemoveBranch(const std::string& name) {
74+
ICEBERG_BUILDER_CHECK(!name.empty(), "Branch name cannot be empty");
75+
ICEBERG_BUILDER_CHECK(name != SnapshotRef::kMainBranch, "Cannot remove main branch");
76+
auto it = updated_refs_.find(name);
77+
ICEBERG_BUILDER_CHECK(it != updated_refs_.end(), "Branch does not exist: {}", name);
78+
ICEBERG_BUILDER_CHECK(it->second->type() == SnapshotRefType::kBranch,
79+
"Ref '{}' is a tag not a branch", name);
80+
updated_refs_.erase(it);
81+
return *this;
82+
}
83+
84+
UpdateSnapshotReference& UpdateSnapshotReference::RemoveTag(const std::string& name) {
85+
ICEBERG_BUILDER_CHECK(!name.empty(), "Tag name cannot be empty");
86+
auto it = updated_refs_.find(name);
87+
ICEBERG_BUILDER_CHECK(it != updated_refs_.end(), "Tag does not exist: {}", name);
88+
ICEBERG_BUILDER_CHECK(it->second->type() == SnapshotRefType::kTag,
89+
"Ref '{}' is a branch not a tag", name);
90+
updated_refs_.erase(it);
91+
return *this;
92+
}
93+
94+
UpdateSnapshotReference& UpdateSnapshotReference::RenameBranch(
95+
const std::string& name, const std::string& new_name) {
96+
ICEBERG_BUILDER_CHECK(!name.empty(), "Branch to rename cannot be empty");
97+
ICEBERG_BUILDER_CHECK(!new_name.empty(), "New branch name cannot be empty");
98+
ICEBERG_BUILDER_CHECK(name != SnapshotRef::kMainBranch, "Cannot rename main branch");
99+
auto it = updated_refs_.find(name);
100+
ICEBERG_BUILDER_CHECK(it != updated_refs_.end(), "Branch does not exist: {}", name);
101+
ICEBERG_BUILDER_CHECK(it->second->type() == SnapshotRefType::kBranch,
102+
"Ref '{}' is a tag not a branch", name);
103+
auto [_, inserted] = updated_refs_.emplace(new_name, it->second);
104+
ICEBERG_BUILDER_CHECK(inserted, "Ref '{}' already exists", new_name);
105+
updated_refs_.erase(it);
106+
return *this;
107+
}
108+
109+
UpdateSnapshotReference& UpdateSnapshotReference::ReplaceBranch(const std::string& name,
110+
int64_t snapshot_id) {
111+
ICEBERG_BUILDER_CHECK(!name.empty(), "Branch name cannot be empty");
112+
auto it = updated_refs_.find(name);
113+
ICEBERG_BUILDER_CHECK(it != updated_refs_.end(), "Branch does not exist: {}", name);
114+
ICEBERG_BUILDER_CHECK(it->second->type() == SnapshotRefType::kBranch,
115+
"Ref '{}' is a tag not a branch", name);
116+
it->second->snapshot_id = snapshot_id;
117+
return *this;
118+
}
119+
120+
UpdateSnapshotReference& UpdateSnapshotReference::ReplaceBranch(const std::string& from,
121+
const std::string& to) {
122+
return ReplaceBranchInternal(from, to, false);
123+
}
124+
125+
UpdateSnapshotReference& UpdateSnapshotReference::FastForward(const std::string& from,
126+
const std::string& to) {
127+
return ReplaceBranchInternal(from, to, true);
128+
}
129+
130+
UpdateSnapshotReference& UpdateSnapshotReference::ReplaceBranchInternal(
131+
const std::string& from, const std::string& to, bool fast_forward) {
132+
ICEBERG_BUILDER_CHECK(!from.empty(), "Branch to update cannot be empty");
133+
ICEBERG_BUILDER_CHECK(!to.empty(), "Destination ref cannot be empty");
134+
auto to_it = updated_refs_.find(to);
135+
ICEBERG_BUILDER_CHECK(to_it != updated_refs_.end(), "Ref does not exist: {}", to);
136+
137+
auto from_it = updated_refs_.find(from);
138+
if (from_it == updated_refs_.end()) {
139+
return CreateBranch(from, to_it->second->snapshot_id);
140+
}
141+
142+
ICEBERG_BUILDER_CHECK(from_it->second->type() == SnapshotRefType::kBranch,
143+
"Ref '{}' is a tag not a branch", from);
144+
145+
// Nothing to replace if snapshot IDs are the same
146+
if (to_it->second->snapshot_id == from_it->second->snapshot_id) {
147+
return *this;
148+
}
149+
150+
if (fast_forward) {
151+
const auto& base_metadata = transaction_->current();
152+
ICEBERG_BUILDER_ASSIGN_OR_RETURN(
153+
auto target_is_ancestor,
154+
SnapshotUtil::IsAncestorOf(
155+
from_it->second->snapshot_id, to_it->second->snapshot_id,
156+
[&base_metadata](int64_t id) { return base_metadata.SnapshotById(id); }));
157+
158+
ICEBERG_BUILDER_CHECK(target_is_ancestor,
159+
"Cannot fast-forward: {} is not an ancestor of {}", from, to);
160+
}
161+
162+
from_it->second->snapshot_id = to_it->second->snapshot_id;
163+
return *this;
164+
}
165+
166+
UpdateSnapshotReference& UpdateSnapshotReference::ReplaceTag(const std::string& name,
167+
int64_t snapshot_id) {
168+
ICEBERG_BUILDER_CHECK(!name.empty(), "Tag name cannot be empty");
169+
auto it = updated_refs_.find(name);
170+
ICEBERG_BUILDER_CHECK(it != updated_refs_.end(), "Tag does not exist: {}", name);
171+
ICEBERG_BUILDER_CHECK(it->second->type() == SnapshotRefType::kTag,
172+
"Ref '{}' is a branch not a tag", name);
173+
it->second->snapshot_id = snapshot_id;
174+
return *this;
175+
}
176+
177+
UpdateSnapshotReference& UpdateSnapshotReference::SetMinSnapshotsToKeep(
178+
const std::string& name, int32_t min_snapshots_to_keep) {
179+
ICEBERG_BUILDER_CHECK(!name.empty(), "Branch name cannot be empty");
180+
auto it = updated_refs_.find(name);
181+
ICEBERG_BUILDER_CHECK(it != updated_refs_.end(), "Branch does not exist: {}", name);
182+
ICEBERG_BUILDER_CHECK(it->second->type() == SnapshotRefType::kBranch,
183+
"Ref '{}' is a tag not a branch", name);
184+
std::get<SnapshotRef::Branch>(it->second->retention).min_snapshots_to_keep =
185+
min_snapshots_to_keep;
186+
ICEBERG_BUILDER_CHECK(it->second->Validate(),
187+
"Invalid min_snapshots_to_keep {} for branch '{}'",
188+
min_snapshots_to_keep, name);
189+
return *this;
190+
}
191+
192+
UpdateSnapshotReference& UpdateSnapshotReference::SetMaxSnapshotAgeMs(
193+
const std::string& name, int64_t max_snapshot_age_ms) {
194+
ICEBERG_BUILDER_CHECK(!name.empty(), "Branch name cannot be empty");
195+
auto it = updated_refs_.find(name);
196+
ICEBERG_BUILDER_CHECK(it != updated_refs_.end(), "Branch does not exist: {}", name);
197+
ICEBERG_BUILDER_CHECK(it->second->type() == SnapshotRefType::kBranch,
198+
"Ref '{}' is a tag not a branch", name);
199+
std::get<SnapshotRef::Branch>(it->second->retention).max_snapshot_age_ms =
200+
max_snapshot_age_ms;
201+
ICEBERG_BUILDER_CHECK(it->second->Validate(),
202+
"Invalid max_snapshot_age_ms {} for branch '{}'",
203+
max_snapshot_age_ms, name);
204+
return *this;
205+
}
206+
207+
UpdateSnapshotReference& UpdateSnapshotReference::SetMaxRefAgeMs(const std::string& name,
208+
int64_t max_ref_age_ms) {
209+
ICEBERG_BUILDER_CHECK(!name.empty(), "Reference name cannot be empty");
210+
auto it = updated_refs_.find(name);
211+
ICEBERG_BUILDER_CHECK(it != updated_refs_.end(), "Ref does not exist: {}", name);
212+
if (it->second->type() == SnapshotRefType::kBranch) {
213+
std::get<SnapshotRef::Branch>(it->second->retention).max_ref_age_ms = max_ref_age_ms;
214+
} else {
215+
std::get<SnapshotRef::Tag>(it->second->retention).max_ref_age_ms = max_ref_age_ms;
216+
}
217+
ICEBERG_BUILDER_CHECK(it->second->Validate(), "Invalid max_ref_age_ms {} for ref '{}'",
218+
max_ref_age_ms, name);
219+
return *this;
220+
}
221+
222+
Result<std::unordered_map<std::string, std::shared_ptr<SnapshotRef>>>
223+
UpdateSnapshotReference::Apply() {
224+
ICEBERG_RETURN_UNEXPECTED(CheckErrors());
225+
return updated_refs_;
226+
}
227+
228+
} // namespace iceberg

0 commit comments

Comments
 (0)