|
| 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 | + const auto& base_refs = transaction_->current().refs; |
| 49 | + for (const auto& [name, ref] : base_refs) { |
| 50 | + updated_refs_[name] = ref; |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +UpdateSnapshotReference::~UpdateSnapshotReference() = default; |
| 55 | + |
| 56 | +UpdateSnapshotReference& UpdateSnapshotReference::CreateBranch(const std::string& name, |
| 57 | + int64_t snapshot_id) { |
| 58 | + ICEBERG_BUILDER_CHECK(!name.empty(), "Branch name cannot be empty"); |
| 59 | + // TODO(anyone): use MakeBranch after #408 got merged |
| 60 | + auto branch = std::make_shared<SnapshotRef>( |
| 61 | + SnapshotRef{.snapshot_id = snapshot_id, .retention = SnapshotRef::Branch{}}); |
| 62 | + auto [_, inserted] = updated_refs_.emplace(name, std::move(branch)); |
| 63 | + ICEBERG_BUILDER_CHECK(inserted, "Ref '{}' already exists", name); |
| 64 | + return *this; |
| 65 | +} |
| 66 | + |
| 67 | +UpdateSnapshotReference& UpdateSnapshotReference::CreateTag(const std::string& name, |
| 68 | + int64_t snapshot_id) { |
| 69 | + ICEBERG_BUILDER_CHECK(!name.empty(), "Tag name cannot be empty"); |
| 70 | + auto tag = std::make_shared<SnapshotRef>( |
| 71 | + SnapshotRef{.snapshot_id = snapshot_id, .retention = SnapshotRef::Tag{}}); |
| 72 | + auto [_, inserted] = updated_refs_.emplace(name, std::move(tag)); |
| 73 | + ICEBERG_BUILDER_CHECK(inserted, "Ref '{}' already exists", name); |
| 74 | + return *this; |
| 75 | +} |
| 76 | + |
| 77 | +UpdateSnapshotReference& UpdateSnapshotReference::RemoveBranch(const std::string& name) { |
| 78 | + ICEBERG_BUILDER_CHECK(!name.empty(), "Branch name cannot be empty"); |
| 79 | + ICEBERG_BUILDER_CHECK(name != SnapshotRef::kMainBranch, "Cannot remove main branch"); |
| 80 | + auto it = updated_refs_.find(name); |
| 81 | + ICEBERG_BUILDER_CHECK(it != updated_refs_.end(), "Branch does not exist: {}", name); |
| 82 | + ICEBERG_BUILDER_CHECK(it->second->type() == SnapshotRefType::kBranch, |
| 83 | + "Ref '{}' is a tag not a branch", name); |
| 84 | + updated_refs_.erase(it); |
| 85 | + return *this; |
| 86 | +} |
| 87 | + |
| 88 | +UpdateSnapshotReference& UpdateSnapshotReference::RemoveTag(const std::string& name) { |
| 89 | + ICEBERG_BUILDER_CHECK(!name.empty(), "Tag name cannot be empty"); |
| 90 | + auto it = updated_refs_.find(name); |
| 91 | + ICEBERG_BUILDER_CHECK(it != updated_refs_.end(), "Tag does not exist: {}", name); |
| 92 | + ICEBERG_BUILDER_CHECK(it->second->type() == SnapshotRefType::kTag, |
| 93 | + "Ref '{}' is a branch not a tag", name); |
| 94 | + updated_refs_.erase(it); |
| 95 | + return *this; |
| 96 | +} |
| 97 | + |
| 98 | +UpdateSnapshotReference& UpdateSnapshotReference::RenameBranch( |
| 99 | + const std::string& name, const std::string& new_name) { |
| 100 | + ICEBERG_BUILDER_CHECK(!name.empty(), "Branch to rename cannot be empty"); |
| 101 | + ICEBERG_BUILDER_CHECK(!new_name.empty(), "New branch name cannot be empty"); |
| 102 | + ICEBERG_BUILDER_CHECK(name != SnapshotRef::kMainBranch, "Cannot rename main branch"); |
| 103 | + auto it = updated_refs_.find(name); |
| 104 | + ICEBERG_BUILDER_CHECK(it != updated_refs_.end(), "Branch does not exist: {}", name); |
| 105 | + ICEBERG_BUILDER_CHECK(it->second->type() == SnapshotRefType::kBranch, |
| 106 | + "Ref '{}' is a tag not a branch", name); |
| 107 | + auto [_, inserted] = updated_refs_.emplace(new_name, it->second); |
| 108 | + ICEBERG_BUILDER_CHECK(inserted, "Ref '{}' already exists", new_name); |
| 109 | + updated_refs_.erase(it); |
| 110 | + return *this; |
| 111 | +} |
| 112 | + |
| 113 | +UpdateSnapshotReference& UpdateSnapshotReference::ReplaceBranch(const std::string& name, |
| 114 | + int64_t snapshot_id) { |
| 115 | + ICEBERG_BUILDER_CHECK(!name.empty(), "Branch name cannot be empty"); |
| 116 | + auto it = updated_refs_.find(name); |
| 117 | + ICEBERG_BUILDER_CHECK(it != updated_refs_.end(), "Branch does not exist: {}", name); |
| 118 | + ICEBERG_BUILDER_CHECK(it->second->type() == SnapshotRefType::kBranch, |
| 119 | + "Ref '{}' is a tag not a branch", name); |
| 120 | + it->second->snapshot_id = snapshot_id; |
| 121 | + return *this; |
| 122 | +} |
| 123 | + |
| 124 | +UpdateSnapshotReference& UpdateSnapshotReference::ReplaceBranch(const std::string& from, |
| 125 | + const std::string& to) { |
| 126 | + return ReplaceBranchInternal(from, to, false); |
| 127 | +} |
| 128 | + |
| 129 | +UpdateSnapshotReference& UpdateSnapshotReference::FastForward(const std::string& from, |
| 130 | + const std::string& to) { |
| 131 | + return ReplaceBranchInternal(from, to, true); |
| 132 | +} |
| 133 | + |
| 134 | +UpdateSnapshotReference& UpdateSnapshotReference::ReplaceBranchInternal( |
| 135 | + const std::string& from, const std::string& to, bool fast_forward) { |
| 136 | + ICEBERG_BUILDER_CHECK(!from.empty(), "Branch to update cannot be empty"); |
| 137 | + ICEBERG_BUILDER_CHECK(!to.empty(), "Destination ref cannot be empty"); |
| 138 | + auto to_it = updated_refs_.find(to); |
| 139 | + ICEBERG_BUILDER_CHECK(to_it != updated_refs_.end(), "Ref does not exist: {}", to); |
| 140 | + |
| 141 | + auto from_it = updated_refs_.find(from); |
| 142 | + if (from_it == updated_refs_.end()) { |
| 143 | + // Create branch if it doesn't exist |
| 144 | + return CreateBranch(from, to_it->second->snapshot_id); |
| 145 | + } |
| 146 | + |
| 147 | + ICEBERG_BUILDER_CHECK(from_it->second->type() == SnapshotRefType::kBranch, |
| 148 | + "Ref '{}' is a tag not a branch", from); |
| 149 | + |
| 150 | + // Nothing to replace if snapshot IDs are the same |
| 151 | + if (to_it->second->snapshot_id == from_it->second->snapshot_id) { |
| 152 | + return *this; |
| 153 | + } |
| 154 | + |
| 155 | + if (fast_forward) { |
| 156 | + // Validate that current branch snapshot (from) is an ancestor of target snapshot (to) |
| 157 | + // This ensures we can fast-forward from 'from' to 'to' |
| 158 | + // TODO(anyone): use base() after #408 |
| 159 | + const auto& base_metadata = transaction_->current(); |
| 160 | + auto lookup = [&base_metadata](int64_t id) -> Result<std::shared_ptr<Snapshot>> { |
| 161 | + return base_metadata.SnapshotById(id); |
| 162 | + }; |
| 163 | + ICEBERG_BUILDER_ASSIGN_OR_RETURN( |
| 164 | + bool target_is_ancestor, |
| 165 | + SnapshotUtil::IsAncestorOf(to_it->second->snapshot_id, |
| 166 | + from_it->second->snapshot_id, lookup)); |
| 167 | + ICEBERG_BUILDER_CHECK(target_is_ancestor, |
| 168 | + "Cannot fast-forward: {} is not an ancestor of {}", from, to); |
| 169 | + } |
| 170 | + |
| 171 | + from_it->second->snapshot_id = to_it->second->snapshot_id; |
| 172 | + return *this; |
| 173 | +} |
| 174 | + |
| 175 | +UpdateSnapshotReference& UpdateSnapshotReference::ReplaceTag(const std::string& name, |
| 176 | + int64_t snapshot_id) { |
| 177 | + ICEBERG_BUILDER_CHECK(!name.empty(), "Tag name cannot be empty"); |
| 178 | + auto it = updated_refs_.find(name); |
| 179 | + ICEBERG_BUILDER_CHECK(it != updated_refs_.end(), "Tag does not exist: {}", name); |
| 180 | + ICEBERG_BUILDER_CHECK(it->second->type() == SnapshotRefType::kTag, |
| 181 | + "Ref '{}' is a branch not a tag", name); |
| 182 | + it->second->snapshot_id = snapshot_id; |
| 183 | + return *this; |
| 184 | +} |
| 185 | + |
| 186 | +UpdateSnapshotReference& UpdateSnapshotReference::SetMinSnapshotsToKeep( |
| 187 | + const std::string& name, int32_t min_snapshots_to_keep) { |
| 188 | + ICEBERG_BUILDER_CHECK(!name.empty(), "Branch name cannot be empty"); |
| 189 | + auto it = updated_refs_.find(name); |
| 190 | + ICEBERG_BUILDER_CHECK(it != updated_refs_.end(), "Branch does not exist: {}", name); |
| 191 | + ICEBERG_BUILDER_CHECK(it->second->type() == SnapshotRefType::kBranch, |
| 192 | + "Ref '{}' is a tag not a branch", name); |
| 193 | + // TODO(anyone): use interface added in #408 |
| 194 | + std::get<SnapshotRef::Branch>(it->second->retention).min_snapshots_to_keep = |
| 195 | + min_snapshots_to_keep; |
| 196 | + return *this; |
| 197 | +} |
| 198 | + |
| 199 | +UpdateSnapshotReference& UpdateSnapshotReference::SetMaxSnapshotAgeMs( |
| 200 | + const std::string& name, int64_t max_snapshot_age_ms) { |
| 201 | + ICEBERG_BUILDER_CHECK(!name.empty(), "Branch name cannot be empty"); |
| 202 | + auto it = updated_refs_.find(name); |
| 203 | + ICEBERG_BUILDER_CHECK(it != updated_refs_.end(), "Branch does not exist: {}", name); |
| 204 | + ICEBERG_BUILDER_CHECK(it->second->type() == SnapshotRefType::kBranch, |
| 205 | + "Ref '{}' is a tag not a branch", name); |
| 206 | + std::get<SnapshotRef::Branch>(it->second->retention).max_snapshot_age_ms = |
| 207 | + max_snapshot_age_ms; |
| 208 | + return *this; |
| 209 | +} |
| 210 | + |
| 211 | +UpdateSnapshotReference& UpdateSnapshotReference::SetMaxRefAgeMs(const std::string& name, |
| 212 | + int64_t max_ref_age_ms) { |
| 213 | + ICEBERG_BUILDER_CHECK(!name.empty(), "Reference name cannot be empty"); |
| 214 | + auto it = updated_refs_.find(name); |
| 215 | + ICEBERG_BUILDER_CHECK(it != updated_refs_.end(), "Ref does not exist: {}", name); |
| 216 | + if (it->second->type() == SnapshotRefType::kBranch) { |
| 217 | + std::get<SnapshotRef::Branch>(it->second->retention).max_ref_age_ms = max_ref_age_ms; |
| 218 | + } else { |
| 219 | + std::get<SnapshotRef::Tag>(it->second->retention).max_ref_age_ms = max_ref_age_ms; |
| 220 | + } |
| 221 | + return *this; |
| 222 | +} |
| 223 | + |
| 224 | +Result<std::unordered_map<std::string, std::shared_ptr<SnapshotRef>>> |
| 225 | +UpdateSnapshotReference::Apply() { |
| 226 | + ICEBERG_RETURN_UNEXPECTED(CheckErrors()); |
| 227 | + return updated_refs_; |
| 228 | +} |
| 229 | + |
| 230 | +} // namespace iceberg |
0 commit comments