33#include <unordered_map>
36#include "iceberg/iceberg_export.h"
38#include "iceberg/util/string_util.h"
46 using value_type = std::shared_ptr<DataFile>;
47 using iterator =
typename std::vector<value_type>::iterator;
48 using const_iterator =
typename std::vector<value_type>::const_iterator;
49 using difference_type =
typename std::vector<value_type>::difference_type;
55 elements_ = other.elements_;
67 std::pair<iterator, bool>
insert(
const value_type& file) {
return InsertImpl(file); }
70 std::pair<iterator, bool>
insert(value_type&& file) {
71 return InsertImpl(std::move(file));
76 return index_by_path_.contains(file.
file_path);
81 return file !=
nullptr && contains(*file);
85 size_t size()
const {
return elements_.size(); }
88 bool empty()
const {
return elements_.empty(); }
93 index_by_path_.clear();
97 iterator
begin() {
return elements_.begin(); }
98 const_iterator begin()
const {
return elements_.begin(); }
99 const_iterator cbegin()
const {
return elements_.cbegin(); }
102 iterator
end() {
return elements_.end(); }
103 const_iterator end()
const {
return elements_.end(); }
104 const_iterator cend()
const {
return elements_.cend(); }
107 std::span<const value_type>
as_span()
const {
return elements_; }
110 std::pair<iterator, bool> InsertImpl(value_type file) {
112 return {elements_.end(),
false};
115 auto [index_iter, inserted] =
116 index_by_path_.try_emplace(file->file_path, elements_.size());
118 auto pos =
static_cast<difference_type
>(index_iter->second);
119 return {elements_.begin() + pos,
false};
122 elements_.push_back(std::move(file));
123 return {std::prev(elements_.end()),
true};
126 void RebuildIndex() {
127 index_by_path_.clear();
128 for (
size_t i = 0; i < elements_.size(); ++i) {
129 if (elements_[i] !=
nullptr) {
130 index_by_path_.try_emplace(elements_[i]->file_path, i);
136 std::vector<value_type> elements_;
137 std::unordered_map<std::string_view, size_t, StringHash, StringEqual> index_by_path_;
146 using value_type = std::shared_ptr<DataFile>;
147 using iterator =
typename std::vector<value_type>::iterator;
148 using const_iterator =
typename std::vector<value_type>::const_iterator;
149 using difference_type =
typename std::vector<value_type>::difference_type;
156 if (
this != &other) {
157 elements_ = other.elements_;
169 std::pair<iterator, bool>
insert(
const value_type& file) {
return InsertImpl(file); }
172 std::pair<iterator, bool>
insert(value_type&& file) {
173 return InsertImpl(std::move(file));
178 return index_by_file_.contains(DeleteFileKey(file));
183 return file !=
nullptr && contains(*file);
187 size_t size()
const {
return elements_.size(); }
190 bool empty()
const {
return elements_.empty(); }
195 index_by_file_.clear();
199 iterator
begin() {
return elements_.begin(); }
200 const_iterator begin()
const {
return elements_.begin(); }
201 const_iterator cbegin()
const {
return elements_.cbegin(); }
204 iterator
end() {
return elements_.end(); }
205 const_iterator end()
const {
return elements_.end(); }
206 const_iterator cend()
const {
return elements_.cend(); }
209 std::span<const value_type>
as_span()
const {
return elements_; }
212 struct DeleteFileKey {
213 explicit DeleteFileKey(
const DataFile& file)
214 : path(file.file_path),
215 content_offset(file.content_offset),
216 content_size_in_bytes(file.content_size_in_bytes) {}
219 std::optional<int64_t> content_offset;
220 std::optional<int64_t> content_size_in_bytes;
222 bool operator==(
const DeleteFileKey& other)
const =
default;
225 struct DeleteFileKeyHash {
226 size_t operator()(
const DeleteFileKey& key)
const {
227 size_t hash = std::hash<std::string>{}(key.path);
228 auto combine = [&hash](
const auto& value) {
229 size_t value_hash = value.has_value() ? std::hash<int64_t>{}(*value) : 0;
230 hash ^= value_hash + 0x9e3779b9 + (hash << 6) + (hash >> 2);
232 combine(key.content_offset);
233 combine(key.content_size_in_bytes);
238 std::pair<iterator, bool> InsertImpl(value_type file) {
240 return {elements_.end(),
false};
243 auto [index_iter, inserted] =
244 index_by_file_.try_emplace(DeleteFileKey(*file), elements_.size());
246 auto pos =
static_cast<difference_type
>(index_iter->second);
247 return {elements_.begin() + pos,
false};
250 elements_.push_back(std::move(file));
251 return {std::prev(elements_.end()),
true};
254 void RebuildIndex() {
255 index_by_file_.clear();
256 for (
size_t i = 0; i < elements_.size(); ++i) {
257 if (elements_[i] !=
nullptr) {
258 index_by_file_.try_emplace(DeleteFileKey(*elements_[i]), i);
264 std::vector<value_type> elements_;
265 std::unordered_map<DeleteFileKey, size_t, DeleteFileKeyHash> index_by_file_;
A set of DataFile pointers with insertion order preserved and deduplicated by file path.
Definition data_file_set.h:44
bool contains(const value_type &file) const
Returns whether an equivalent data file exists in the set.
Definition data_file_set.h:80
bool contains(const DataFile &file) const
Returns whether an equivalent data file exists in the set.
Definition data_file_set.h:75
iterator end()
Get iterator to the end.
Definition data_file_set.h:102
void clear()
Clear all elements from the set.
Definition data_file_set.h:91
std::span< const value_type > as_span() const
Get a non-owning view of the data files in insertion order.
Definition data_file_set.h:107
size_t size() const
Get the number of elements in the set.
Definition data_file_set.h:85
std::pair< iterator, bool > insert(const value_type &file)
Insert a data file into the set.
Definition data_file_set.h:67
iterator begin()
Get iterator to the beginning.
Definition data_file_set.h:97
bool empty() const
Check if the set is empty.
Definition data_file_set.h:88
std::pair< iterator, bool > insert(value_type &&file)
Insert a data file into the set (move version).
Definition data_file_set.h:70
A set of delete-file pointers deduplicated by delete-file identity.
Definition data_file_set.h:144
bool contains(const DataFile &file) const
Returns whether an equivalent delete file exists in the set.
Definition data_file_set.h:177
bool contains(const value_type &file) const
Returns whether an equivalent delete file exists in the set.
Definition data_file_set.h:182
std::pair< iterator, bool > insert(const value_type &file)
Insert a delete file into the set.
Definition data_file_set.h:169
std::span< const value_type > as_span() const
Get a non-owning view of the delete files in insertion order.
Definition data_file_set.h:209
std::pair< iterator, bool > insert(value_type &&file)
Insert a delete file into the set (move version).
Definition data_file_set.h:172
iterator begin()
Get iterator to the beginning.
Definition data_file_set.h:199
bool empty() const
Check if the set is empty.
Definition data_file_set.h:190
iterator end()
Get iterator to the end.
Definition data_file_set.h:204
void clear()
Clear all elements from the set.
Definition data_file_set.h:193
size_t size() const
Get the number of elements in the set.
Definition data_file_set.h:187
DataFile carries data file path, partition tuple, metrics, ...
Definition manifest_entry.h:62
std::string file_path
Definition manifest_entry.h:76