iceberg-cpp
Loading...
Searching...
No Matches
expire_snapshots.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
22#include <cstdint>
23#include <functional>
24#include <memory>
25#include <string>
26#include <unordered_map>
27#include <unordered_set>
28#include <vector>
29
30#include "iceberg/iceberg_export.h"
31#include "iceberg/result.h"
32#include "iceberg/type_fwd.h"
34#include "iceberg/util/timepoint.h"
35
38
39namespace iceberg {
40
42enum class CleanupLevel : uint8_t {
44 kNone,
49 kAll
50};
51
66class ICEBERG_EXPORT ExpireSnapshots : public PendingUpdate {
67 public:
68 static Result<std::shared_ptr<ExpireSnapshots>> Make(
69 std::shared_ptr<TransactionContext> ctx);
70
71 ~ExpireSnapshots() override;
72
73 struct ApplyResult {
74 std::vector<std::string> refs_to_remove;
75 std::vector<int64_t> snapshot_ids_to_remove;
76 std::vector<int32_t> partition_spec_ids_to_remove;
77 std::unordered_set<int32_t> schema_ids_to_remove;
78 std::shared_ptr<const TableMetadata> metadata_before_expiration;
79 };
80
85 ExpireSnapshots& ExpireSnapshotId(int64_t snapshot_id);
86
91 ExpireSnapshots& ExpireOlderThan(int64_t timestamp_millis);
92
106 ExpireSnapshots& RetainLast(int num_snapshots);
107
119 ExpireSnapshots& DeleteWith(std::function<void(const std::string&)> delete_func);
120
135
140 ExpireSnapshots& CleanExpiredMetadata(bool clean);
141
142 Kind kind() const final { return Kind::kExpireSnapshots; }
143 bool IsRetryable() const override { return true; }
144
147 Result<ApplyResult> Apply();
148
158 Status Finalize(Result<const TableMetadata*> commit_result) override;
159
160 private:
161 explicit ExpireSnapshots(std::shared_ptr<TransactionContext> ctx);
162
163 using SnapshotToRef = std::unordered_map<std::string, std::shared_ptr<SnapshotRef>>;
164
165 Result<SnapshotToRef> ComputeRetainedRefs(const SnapshotToRef& refs) const;
166
167 Result<std::unordered_set<int64_t>> ComputeBranchSnapshotsToRetain(
168 int64_t snapshot_id, TimePointMs expire_snapshot_older_than,
169 int32_t min_snapshots_to_keep) const;
170
171 Result<std::unordered_set<int64_t>> ComputeAllBranchSnapshotIdsToRetain(
172 const SnapshotToRef& refs) const;
173
174 Result<std::unordered_set<int64_t>> UnreferencedSnapshotIdsToRetain(
175 const SnapshotToRef& refs) const;
176
177 const TimePointMs current_time_ms_;
178 const int64_t default_max_ref_age_ms_;
179 int32_t default_min_num_snapshots_;
180 TimePointMs default_expire_older_than_;
181 std::function<void(const std::string&)> delete_func_;
182 std::vector<int64_t> snapshot_ids_to_expire_;
183 enum CleanupLevel cleanup_level_ { CleanupLevel::kAll };
184 bool clean_expired_metadata_{false};
185 bool specified_snapshot_id_{false};
186
188 std::optional<ApplyResult> apply_result_;
189};
190
191} // namespace iceberg
API for removing old snapshots from a table.
Definition expire_snapshots.h:66
bool IsRetryable() const override
Whether this update can be retried after a commit conflict.
Definition expire_snapshots.h:143
Kind kind() const final
Return the kind of this pending update.
Definition expire_snapshots.h:142
Base class for all kinds of table metadata updates.
Definition pending_update.h:41
CleanupLevel
An enum representing possible clean up levels used in snapshot expiration.
Definition expire_snapshots.h:42
Definition expire_snapshots.h:73