Skip to content

Commit 8c4fb8f

Browse files
committed
simplify a little bit
1 parent 0f36ff0 commit 8c4fb8f

2 files changed

Lines changed: 20 additions & 34 deletions

File tree

src/iceberg/update/update_snapshot_reference.cc

Lines changed: 20 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@ Result<std::shared_ptr<UpdateSnapshotReference>> UpdateSnapshotReference::Make(
4141
}
4242

4343
UpdateSnapshotReference::UpdateSnapshotReference(std::shared_ptr<Transaction> transaction)
44-
: PendingUpdate(std::move(transaction)),
45-
updated_refs_(base().refs.begin(), base().refs.end()) {}
44+
: PendingUpdate(std::move(transaction)), updated_refs_(base().refs) {}
4645

4746
UpdateSnapshotReference::~UpdateSnapshotReference() = default;
4847

@@ -107,20 +106,18 @@ UpdateSnapshotReference& UpdateSnapshotReference::ReplaceBranch(const std::strin
107106
ICEBERG_BUILDER_CHECK(it != updated_refs_.end(), "Branch does not exist: {}", name);
108107
ICEBERG_BUILDER_CHECK(it->second->type() == SnapshotRefType::kBranch,
109108
"Ref '{}' is a tag not a branch", name);
110-
// Clone the ref before modifying to avoid affecting base metadata
111-
auto cloned = it->second->Clone(snapshot_id);
112-
it->second = std::shared_ptr<SnapshotRef>(cloned.release());
109+
it->second = it->second->Clone(snapshot_id);
113110
return *this;
114111
}
115112

116113
UpdateSnapshotReference& UpdateSnapshotReference::ReplaceBranch(const std::string& from,
117114
const std::string& to) {
118-
return ReplaceBranchInternal(from, to, false);
115+
return ReplaceBranchInternal(from, to, /*fast_forward=*/false);
119116
}
120117

121118
UpdateSnapshotReference& UpdateSnapshotReference::FastForward(const std::string& from,
122119
const std::string& to) {
123-
return ReplaceBranchInternal(from, to, true);
120+
return ReplaceBranchInternal(from, to, /*fast_forward=*/true);
124121
}
125122

126123
UpdateSnapshotReference& UpdateSnapshotReference::ReplaceBranchInternal(
@@ -157,9 +154,7 @@ UpdateSnapshotReference& UpdateSnapshotReference::ReplaceBranchInternal(
157154
"Cannot fast-forward: {} is not an ancestor of {}", from, to);
158155
}
159156

160-
// Clone the ref before modifying to avoid affecting base metadata
161-
auto cloned = from_it->second->Clone(to_it->second->snapshot_id);
162-
from_it->second = std::shared_ptr<SnapshotRef>(cloned.release());
157+
from_it->second = from_it->second->Clone(to_it->second->snapshot_id);
163158
return *this;
164159
}
165160

@@ -170,9 +165,7 @@ UpdateSnapshotReference& UpdateSnapshotReference::ReplaceTag(const std::string&
170165
ICEBERG_BUILDER_CHECK(it != updated_refs_.end(), "Tag does not exist: {}", name);
171166
ICEBERG_BUILDER_CHECK(it->second->type() == SnapshotRefType::kTag,
172167
"Ref '{}' is a branch not a tag", name);
173-
// Clone the ref before modifying to avoid affecting base metadata
174-
auto cloned = it->second->Clone(snapshot_id);
175-
it->second = std::shared_ptr<SnapshotRef>(cloned.release());
168+
it->second = it->second->Clone(snapshot_id);
176169
return *this;
177170
}
178171

@@ -183,14 +176,12 @@ UpdateSnapshotReference& UpdateSnapshotReference::SetMinSnapshotsToKeep(
183176
ICEBERG_BUILDER_CHECK(it != updated_refs_.end(), "Branch does not exist: {}", name);
184177
ICEBERG_BUILDER_CHECK(it->second->type() == SnapshotRefType::kBranch,
185178
"Ref '{}' is a tag not a branch", name);
186-
// Clone the ref before modifying to avoid affecting base metadata
187-
auto cloned = it->second->Clone();
188-
std::get<SnapshotRef::Branch>(cloned->retention).min_snapshots_to_keep =
179+
it->second = it->second->Clone();
180+
std::get<SnapshotRef::Branch>(it->second->retention).min_snapshots_to_keep =
189181
min_snapshots_to_keep;
190-
ICEBERG_BUILDER_CHECK(cloned->Validate(),
182+
ICEBERG_BUILDER_CHECK(it->second->Validate(),
191183
"Invalid min_snapshots_to_keep {} for branch '{}'",
192184
min_snapshots_to_keep, name);
193-
it->second = std::shared_ptr<SnapshotRef>(cloned.release());
194185
return *this;
195186
}
196187

@@ -201,14 +192,12 @@ UpdateSnapshotReference& UpdateSnapshotReference::SetMaxSnapshotAgeMs(
201192
ICEBERG_BUILDER_CHECK(it != updated_refs_.end(), "Branch does not exist: {}", name);
202193
ICEBERG_BUILDER_CHECK(it->second->type() == SnapshotRefType::kBranch,
203194
"Ref '{}' is a tag not a branch", name);
204-
// Clone the ref before modifying to avoid affecting base metadata
205-
auto cloned = it->second->Clone();
206-
std::get<SnapshotRef::Branch>(cloned->retention).max_snapshot_age_ms =
195+
it->second = it->second->Clone();
196+
std::get<SnapshotRef::Branch>(it->second->retention).max_snapshot_age_ms =
207197
max_snapshot_age_ms;
208-
ICEBERG_BUILDER_CHECK(cloned->Validate(),
198+
ICEBERG_BUILDER_CHECK(it->second->Validate(),
209199
"Invalid max_snapshot_age_ms {} for branch '{}'",
210200
max_snapshot_age_ms, name);
211-
it->second = std::shared_ptr<SnapshotRef>(cloned.release());
212201
return *this;
213202
}
214203

@@ -217,16 +206,14 @@ UpdateSnapshotReference& UpdateSnapshotReference::SetMaxRefAgeMs(const std::stri
217206
ICEBERG_BUILDER_CHECK(!name.empty(), "Reference name cannot be empty");
218207
auto it = updated_refs_.find(name);
219208
ICEBERG_BUILDER_CHECK(it != updated_refs_.end(), "Ref does not exist: {}", name);
220-
// Clone the ref before modifying to avoid affecting base metadata
221-
auto cloned = it->second->Clone();
222-
if (cloned->type() == SnapshotRefType::kBranch) {
223-
std::get<SnapshotRef::Branch>(cloned->retention).max_ref_age_ms = max_ref_age_ms;
209+
it->second = it->second->Clone();
210+
if (it->second->type() == SnapshotRefType::kBranch) {
211+
std::get<SnapshotRef::Branch>(it->second->retention).max_ref_age_ms = max_ref_age_ms;
224212
} else {
225-
std::get<SnapshotRef::Tag>(cloned->retention).max_ref_age_ms = max_ref_age_ms;
213+
std::get<SnapshotRef::Tag>(it->second->retention).max_ref_age_ms = max_ref_age_ms;
226214
}
227-
ICEBERG_BUILDER_CHECK(cloned->Validate(), "Invalid max_ref_age_ms {} for ref '{}'",
215+
ICEBERG_BUILDER_CHECK(it->second->Validate(), "Invalid max_ref_age_ms {} for ref '{}'",
228216
max_ref_age_ms, name);
229-
it->second = std::shared_ptr<SnapshotRef>(cloned.release());
230217
return *this;
231218
}
232219

@@ -238,15 +225,15 @@ Result<UpdateSnapshotReference::ApplyResult> UpdateSnapshotReference::Apply() {
238225

239226
// Identify references which have been removed
240227
for (const auto& [name, ref] : current_refs) {
241-
if (updated_refs_.find(name) == updated_refs_.end()) {
228+
if (!updated_refs_.contains(name)) {
242229
result.to_remove.push_back(name);
243230
}
244231
}
245232

246233
// Identify references which have been created or updated
247234
for (const auto& [name, ref] : updated_refs_) {
248-
auto current_it = current_refs.find(name);
249-
if (current_it == current_refs.end() || *current_it->second != *ref) {
235+
if (auto iter = current_refs.find(name);
236+
iter == current_refs.end() || *iter->second != *ref) {
250237
result.to_set.emplace_back(name, ref);
251238
}
252239
}

src/iceberg/update/update_snapshot_reference.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
#pragma once
2121

2222
#include <memory>
23-
#include <optional>
2423
#include <string>
2524
#include <unordered_map>
2625

0 commit comments

Comments
 (0)