iceberg-cpp
Loading...
Searching...
No Matches
snapshot_util.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
24
25#include <functional>
26#include <memory>
27#include <optional>
28#include <string>
29#include <vector>
30
32#include "iceberg/result.h"
33#include "iceberg/type_fwd.h"
35
36namespace iceberg {
37
40class ICEBERG_EXPORT SnapshotUtil {
41 public:
48 int64_t snapshot_id);
49
56 const TableMetadata& metadata, int64_t snapshot_id);
57
64 int64_t snapshot_id,
65 const std::function<Result<std::shared_ptr<Snapshot>>(int64_t)>& lookup);
66
73 static Result<bool> IsAncestorOf(const Table& table, int64_t ancestor_snapshot_id);
74
81 static Result<bool> IsAncestorOf(const TableMetadata& metadata,
82 int64_t ancestor_snapshot_id);
83
90 static Result<bool> IsAncestorOf(const Table& table, int64_t snapshot_id,
91 int64_t ancestor_snapshot_id);
92
99 static Result<bool> IsAncestorOf(const TableMetadata& metadata, int64_t snapshot_id,
100 int64_t ancestor_snapshot_id);
101
110 int64_t snapshot_id, int64_t ancestor_snapshot_id,
111 const std::function<Result<std::shared_ptr<Snapshot>>(int64_t)>& lookup);
112
120 static Result<bool> IsParentAncestorOf(const Table& table, int64_t snapshot_id,
121 int64_t ancestor_parent_snapshot_id);
122
131 int64_t snapshot_id,
132 int64_t ancestor_parent_snapshot_id);
133
142 int64_t snapshot_id, int64_t ancestor_parent_snapshot_id,
143 const std::function<Result<std::shared_ptr<Snapshot>>(int64_t)>& lookup);
144
151 const Table& table);
152
159 const TableMetadata& metadata);
160
170
177 const Table& table);
178
190 const Table& table, int64_t snapshot_id);
191
203 const TableMetadata& metadata, int64_t snapshot_id);
204
214 const Table& table, TimePointMs timestamp_ms);
215
225 int64_t from_snapshot_id,
226 int64_t to_snapshot_id);
227
236 const Table& table, int64_t latest_snapshot_id,
237 const std::optional<int64_t>& oldest_snapshot_id);
238
247 const Table& table, int64_t latest_snapshot_id,
248 std::optional<int64_t> oldest_snapshot_id);
249
258 const TableMetadata& metadata, int64_t latest_snapshot_id,
259 std::optional<int64_t> oldest_snapshot_id);
260
268 int64_t snapshot_id);
269
275 static Result<int64_t> SnapshotIdAsOfTime(const Table& table, TimePointMs timestamp_ms);
276
283 TimePointMs timestamp_ms);
284
291 static std::optional<int64_t> OptionalSnapshotIdAsOfTime(const Table& table,
292 TimePointMs timestamp_ms);
293
300 int64_t snapshot_id);
301
308 TimePointMs timestamp_ms);
309
320 const std::string& ref);
321
332 const std::string& ref);
333
344 const std::string& branch);
345
360 const std::string& branch);
361
372 const TableMetadata& metadata, const std::string& branch);
373
375 static int64_t GenerateSnapshotId();
376
381 static int64_t GenerateSnapshotId(const TableMetadata& metadata);
382
383 private:
390 const TableMetadata& metadata, const std::shared_ptr<Snapshot>& snapshot);
391
398 const std::shared_ptr<Snapshot>& snapshot,
399 const std::function<Result<std::shared_ptr<Snapshot>>(int64_t)>& lookup);
400
405 static Result<std::vector<int64_t>> ToIds(
406 const std::vector<std::shared_ptr<Snapshot>>& snapshots);
407};
408
409} // namespace iceberg
Utility functions for working with snapshots.
Definition snapshot_util.h:40
static Result< std::shared_ptr< Schema > > SchemaFor(const Table &table, const std::string &ref)
Return the schema of the snapshot at a given ref.
static Result< std::optional< std::shared_ptr< Snapshot > > > OldestAncestor(const Table &table)
Traverses the history of the table's current snapshot and finds the oldest Snapshot.
static Result< std::vector< std::shared_ptr< Snapshot > > > AncestorsBetween(const Table &table, int64_t latest_snapshot_id, std::optional< int64_t > oldest_snapshot_id)
Returns a vector of ancestors between two snapshots.
static Result< std::shared_ptr< Schema > > SchemaFor(const Table &table, int64_t snapshot_id)
Returns the schema of the table for the specified snapshot.
static Result< std::vector< std::shared_ptr< Snapshot > > > AncestorsOf(int64_t snapshot_id, const std::function< Result< std::shared_ptr< Snapshot > >(int64_t)> &lookup)
Returns a vector of ancestors of the given snapshot.
static Result< bool > IsAncestorOf(int64_t snapshot_id, int64_t ancestor_snapshot_id, const std::function< Result< std::shared_ptr< Snapshot > >(int64_t)> &lookup)
Returns whether ancestor_snapshot_id is an ancestor of snapshot_id using the given lookup function.
static Result< std::shared_ptr< Snapshot > > LatestSnapshot(const TableMetadata &metadata, const std::string &branch)
Fetch the snapshot at the head of the given branch in the given table.
static Result< std::shared_ptr< Snapshot > > OptionalLatestSnapshot(const TableMetadata &metadata, const std::string &branch)
Fetch the snapshot at the head of the given branch in the given table.
static Result< std::optional< std::shared_ptr< Snapshot > > > OldestAncestorOf(const TableMetadata &metadata, int64_t snapshot_id)
Traverses the history and finds the oldest ancestor of the specified snapshot.
static Result< std::vector< std::shared_ptr< Snapshot > > > AncestorsOf(const Table &table, int64_t snapshot_id)
Returns a vector of ancestors of the given snapshot.
static Result< std::vector< std::shared_ptr< Snapshot > > > AncestorsBetween(const TableMetadata &metadata, int64_t latest_snapshot_id, std::optional< int64_t > oldest_snapshot_id)
Returns a vector of ancestors between two snapshots.
static Result< bool > IsAncestorOf(const Table &table, int64_t ancestor_snapshot_id)
Returns whether ancestor_snapshot_id is an ancestor of the table's current state.
static Result< std::shared_ptr< Snapshot > > SnapshotAfter(const Table &table, int64_t snapshot_id)
Traverses the history of the table's current snapshot and finds the snapshot with the given snapshot ...
static int64_t GenerateSnapshotId(const TableMetadata &metadata)
Generate a new snapshot ID for the given metadata.
static Result< std::vector< int64_t > > AncestorIdsBetween(const Table &table, int64_t latest_snapshot_id, const std::optional< int64_t > &oldest_snapshot_id)
Returns a vector of ancestor IDs between two snapshots.
static std::optional< int64_t > OptionalSnapshotIdAsOfTime(const Table &table, TimePointMs timestamp_ms)
Returns the ID of the most recent snapshot for the table as of the timestamp, or nullopt if not found...
static Result< std::vector< std::shared_ptr< Snapshot > > > CurrentAncestors(const TableMetadata &metadata)
Returns a vector that traverses the metadata's snapshots from the current to the last known ancestor.
static Result< bool > IsParentAncestorOf(const TableMetadata &metadata, int64_t snapshot_id, int64_t ancestor_parent_snapshot_id)
Returns whether some ancestor of snapshot_id has parentId matches ancestor_parent_snapshot_id.
static Result< std::shared_ptr< Schema > > SchemaFor(const Table &table, TimePointMs timestamp_ms)
Returns the schema of the table for the specified timestamp.
static Result< bool > IsParentAncestorOf(int64_t snapshot_id, int64_t ancestor_parent_snapshot_id, const std::function< Result< std::shared_ptr< Snapshot > >(int64_t)> &lookup)
Returns whether some ancestor of snapshot_id has parentId matches ancestor_parent_snapshot_id.
static Result< bool > IsParentAncestorOf(const Table &table, int64_t snapshot_id, int64_t ancestor_parent_snapshot_id)
Returns whether some ancestor of snapshot_id has parentId matches ancestor_parent_snapshot_id.
static Result< int64_t > SnapshotIdAsOfTime(const TableMetadata &metadata, TimePointMs timestamp_ms)
Returns the ID of the most recent snapshot for the table as of the timestamp.
static Result< std::shared_ptr< Schema > > SchemaFor(const TableMetadata &metadata, const std::string &ref)
Return the schema of the snapshot at a given ref.
static Result< std::optional< std::shared_ptr< Snapshot > > > OldestAncestorOf(const Table &table, int64_t snapshot_id)
Traverses the history and finds the oldest ancestor of the specified snapshot.
static Result< std::vector< int64_t > > SnapshotIdsBetween(const Table &table, int64_t from_snapshot_id, int64_t to_snapshot_id)
Returns list of snapshot ids in the range (from_snapshot_id,to_snapshot_id].
static int64_t GenerateSnapshotId()
Generate a new snapshot ID.
static Result< std::vector< int64_t > > CurrentAncestorIds(const Table &table)
Returns the snapshot IDs for the ancestors of the current table state.
static Result< std::optional< std::shared_ptr< Snapshot > > > OldestAncestorAfter(const Table &table, TimePointMs timestamp_ms)
Traverses the history of the table's current snapshot, finds the oldest snapshot that was committed e...
static Result< std::shared_ptr< Snapshot > > LatestSnapshot(const Table &table, const std::string &branch)
Fetch the snapshot at the head of the given branch in the given table.
static Result< std::vector< std::shared_ptr< Snapshot > > > CurrentAncestors(const Table &table)
Returns a vector that traverses the table's snapshots from the current to the last known ancestor.
static Result< bool > IsAncestorOf(const TableMetadata &metadata, int64_t ancestor_snapshot_id)
Returns whether ancestor_snapshot_id is an ancestor of the metadata's current state.
static Result< std::vector< std::shared_ptr< Snapshot > > > AncestorsOf(const TableMetadata &metadata, int64_t snapshot_id)
Returns a vector of ancestors of the given snapshot.
static Result< bool > IsAncestorOf(const TableMetadata &metadata, int64_t snapshot_id, int64_t ancestor_snapshot_id)
Returns whether ancestor_snapshot_id is an ancestor of snapshot_id.
static Result< int64_t > SnapshotIdAsOfTime(const Table &table, TimePointMs timestamp_ms)
Returns the ID of the most recent snapshot for the table as of the timestamp.
static Result< bool > IsAncestorOf(const Table &table, int64_t snapshot_id, int64_t ancestor_snapshot_id)
Returns whether ancestor_snapshot_id is an ancestor of snapshot_id.
Represents an Iceberg table.
Definition table.h:45
Definition expected.h:1199
Define symbol visibility macros for core Iceberg APIs.
Core Apache Iceberg C++ APIs.
Definition arrow_io_util.h:34
std::chrono::time_point< std::chrono::system_clock, std::chrono::milliseconds > TimePointMs
A time point in milliseconds.
Definition timepoint.h:33
Define Result, Status, and error helpers.
Represents the metadata for an Iceberg table.
Definition table_metadata.h:73
Provide time point conversion helpers.