iceberg-cpp
Loading...
Searching...
No Matches
rewrite_manifests.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
23
24#include <functional>
25#include <memory>
26#include <span>
27#include <string>
28#include <unordered_map>
29#include <unordered_set>
30#include <vector>
31
33#include "iceberg/result.h"
34#include "iceberg/type_fwd.h"
36
37namespace iceberg {
38
53class ICEBERG_EXPORT RewriteManifests : public SnapshotUpdate {
54 public:
55 using ClusterByFunc = std::function<std::string(const DataFile&)>;
56 using RewritePredicate = std::function<bool(const ManifestFile&)>;
57
59 std::string table_name, std::shared_ptr<TransactionContext> ctx);
60
61 ~RewriteManifests() override;
62
70 RewriteManifests& ClusterBy(ClusterByFunc func);
71
76 RewriteManifests& RewriteIf(RewritePredicate predicate);
77
80
96
97 protected:
98 std::string operation() override;
99
101 const TableMetadata& metadata_to_update,
102 const std::shared_ptr<Snapshot>& snapshot) override;
103 std::unordered_map<std::string, std::string> Summary() override;
104 Status CleanUncommitted(const std::unordered_set<std::string>& committed) override;
105
106 explicit RewriteManifests(std::string table_name,
107 std::shared_ptr<TransactionContext> ctx);
108
109 private:
110 struct WriterKey {
111 std::string cluster_key;
112 int32_t partition_spec_id;
113
114 friend bool operator==(const WriterKey&, const WriterKey&) = default;
115 };
116
117 struct WriterKeyHash {
118 size_t operator()(const WriterKey& key) const;
119 };
120
121 class WriterWrapper;
122
123 bool RequiresRewrite(const std::unordered_set<ManifestFile>& current_manifests) const;
124 bool MatchesPredicate(const ManifestFile& manifest) const;
125 bool ContainsDeletes(const ManifestFile& manifest) const;
126 void KeepActiveManifests(std::span<const ManifestFile> current_manifests);
127 Status ValidateDeletedManifests(
128 const std::unordered_set<ManifestFile>& current_manifests,
129 int64_t current_snapshot_id) const;
130 Status ValidateFilesCounts() const;
131
132 Result<ManifestFile> CopyManifest(const ManifestFile& manifest);
133 Status PerformRewrite(std::span<const ManifestFile> current_manifests);
134 Status AppendEntry(const ManifestEntry& entry, const ManifestFile& manifest,
135 const std::shared_ptr<PartitionSpec>& spec);
136 Result<WriterWrapper*> GetWriter(const WriterKey& key,
137 const std::shared_ptr<PartitionSpec>& spec);
138 Result<std::unique_ptr<ManifestWriter>> NewManifestWriter(
139 const std::shared_ptr<PartitionSpec>& spec);
140 Status CloseWriters();
141
142 void DeleteUncommitted(std::vector<ManifestFile>& manifests,
143 const std::unordered_set<std::string>& committed);
144 void Reset();
145
146 private:
147 std::unordered_set<ManifestFile> deleted_manifests_;
148 std::vector<ManifestFile> added_manifests_;
149 std::vector<ManifestFile> added_manifests_to_copy_;
150 std::vector<ManifestFile> rewritten_added_manifests_;
151
152 std::vector<ManifestFile> kept_manifests_;
153 std::vector<ManifestFile> new_manifests_;
154 std::unordered_set<ManifestFile> rewritten_manifests_;
155 std::unordered_map<WriterKey, std::unique_ptr<WriterWrapper>, WriterKeyHash> writers_;
156
157 int64_t entry_count_{0};
158
159 ClusterByFunc cluster_by_func_;
160 RewritePredicate predicate_;
161 bool rewrite_dirty_{true};
162};
163
164} // namespace iceberg
API for rewriting manifests for a table.
Definition rewrite_manifests.h:53
RewriteManifests & ClusterBy(ClusterByFunc func)
Groups an existing data file by a cluster key produced by a function.
Status CleanUncommitted(const std::unordered_set< std::string > &committed) override
Clean up any uncommitted manifests that were created.
std::string operation() override
A string that describes the action that produced the new snapshot.
RewriteManifests & AddManifest(const ManifestFile &manifest)
Adds a manifest file to the table.
RewriteManifests & RewriteIf(RewritePredicate predicate)
Determines which existing manifest files for the table should be rewritten.
Result< std::vector< ManifestFile > > Apply(const TableMetadata &metadata_to_update, const std::shared_ptr< Snapshot > &snapshot) override
Apply the update's changes to the given metadata and snapshot.
RewriteManifests & DeleteManifest(const ManifestFile &manifest)
Deletes a manifest file from the table.
std::unordered_map< std::string, std::string > Summary() override
Get the summary map for this operation.
API for table changes that produce snapshots.
Definition snapshot_update.h:61
Definition expected.h:1199
Define symbol visibility macros for core Iceberg APIs.
Core Apache Iceberg C++ APIs.
Definition arrow_io_util.h:34
Define Result, Status, and error helpers.
Define base APIs for snapshot updates.
DataFile carries data file path, partition tuple, metrics, ...
Definition manifest_entry.h:63
Entry in a manifest list.
Definition manifest_list.h:86
Represents the metadata for an Iceberg table.
Definition table_metadata.h:73