userver: userver/storages/clickhouse/io/columns/column_iterator.hpp Source File
Loading...
Searching...
No Matches
column_iterator.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/storages/clickhouse/io/columns/column_iterator.hpp
4/// @brief @copybrief storages::clickhouse::io::columns::BaseIterator
5
6#include <iterator>
7#include <optional>
8
9#include <userver/utils/assert.hpp>
10
11#include <userver/storages/clickhouse/io/columns/column_wrapper.hpp>
12
13USERVER_NAMESPACE_BEGIN
14
15namespace storages::clickhouse::io {
16
17class IteratorsTester;
18
19namespace columns {
20
21/// @brief Forward-iterator for iterating over column of type ColumnType
22template <typename ColumnType>
23class ColumnIterator final {
24public:
25 using iterator_category = std::forward_iterator_tag;
26 using difference_type = std::ptrdiff_t;
27 using value_type = typename ColumnType::cpp_type;
28 using reference = value_type&;
29 using pointer = value_type*;
30
31 ColumnIterator() = default;
32 ColumnIterator(ColumnRef column);
33
34 static ColumnIterator End(ColumnRef column);
35
36 ColumnIterator operator++(int);
37 ColumnIterator& operator++();
38 reference operator*() const;
39 pointer operator->() const;
40
41 bool operator==(const ColumnIterator& other) const;
42 bool operator!=(const ColumnIterator& other) const;
43
44 enum class IteratorPosition { kBegin, kEnd };
45
46 friend class storages::clickhouse::io::IteratorsTester;
47
48private:
49 ColumnIterator(IteratorPosition iter_position, ColumnRef&& column);
50
51 class DataHolder final {
52 public:
53 DataHolder() = default;
54 DataHolder(IteratorPosition iter_position, ColumnRef&& column);
55
56 DataHolder operator++(int);
57 DataHolder& operator++();
58 value_type& UpdateValue();
59 bool operator==(const DataHolder& other) const;
60
61 value_type Get() const;
62
63 friend class storages::clickhouse::io::IteratorsTester;
64
65 private:
66 ColumnRef column_;
67 size_t ind_{0};
68
69 std::optional<value_type> current_value_ = std::nullopt;
70 };
71
72 template <typename T, typename = void>
73 struct IteratorDataHolder final {
74 using type = DataHolder;
75 };
76
77 template <typename T>
78 struct IteratorDataHolder<T, std::void_t<typename T::iterator_data>> final {
79 using type = typename T::iterator_data;
80 };
81
82 using data_holder = typename IteratorDataHolder<ColumnType>::type;
83
84 mutable data_holder data_;
85};
86
87template <typename ColumnType>
88ColumnIterator<ColumnType>::ColumnIterator(ColumnRef column)
89 : ColumnIterator{IteratorPosition::kBegin, std::move(column)} {}
90
91template <typename ColumnType>
92ColumnIterator<ColumnType> ColumnIterator<ColumnType>::End(ColumnRef column) {
93 return ColumnIterator{IteratorPosition::kEnd, std::move(column)};
94}
95
96template <typename ColumnType>
97ColumnIterator<ColumnType>::ColumnIterator(IteratorPosition iter_position, ColumnRef&& column)
98 : data_{data_holder{iter_position, std::move(column)}} {}
99
100template <typename ColumnType>
101ColumnIterator<ColumnType> ColumnIterator<ColumnType>::operator++(int) {
102 ColumnIterator<ColumnType> old{};
103 old.data_ = data_++;
104
105 return old;
106}
107
108template <typename ColumnType>
109ColumnIterator<ColumnType>& ColumnIterator<ColumnType>::operator++() {
110 ++data_;
111 return *this;
112}
113
114template <typename ColumnType>
115typename ColumnIterator<ColumnType>::reference ColumnIterator<ColumnType>::operator*() const {
116 return data_.UpdateValue();
117}
118
119template <typename ColumnType>
120typename ColumnIterator<ColumnType>::pointer ColumnIterator<ColumnType>::operator->() const {
121 return &data_.UpdateValue();
122}
123
124template <typename ColumnType>
125bool ColumnIterator<ColumnType>::operator==(const ColumnIterator& other) const {
126 return data_ == other.data_;
127}
128
129template <typename ColumnType>
130bool ColumnIterator<ColumnType>::operator!=(const ColumnIterator<ColumnType>& other) const {
131 return !((*this) == other);
132}
133
134template <typename ColumnType>
135ColumnIterator<ColumnType>::DataHolder::DataHolder(IteratorPosition iter_position, ColumnRef&& column)
136 : column_{std::move(column)} {
137 switch (iter_position) {
138 case IteratorPosition::kBegin: {
139 ind_ = 0;
140 break;
141 }
142 case IteratorPosition::kEnd: {
143 ind_ = GetColumnSize(column_);
144 break;
145 }
146 }
147}
148
149template <typename ColumnType>
150typename ColumnIterator<ColumnType>::DataHolder ColumnIterator<ColumnType>::DataHolder::operator++(int) {
151 DataHolder old{};
152 old.column_ = column_;
153 old.ind_ = ind_++;
154 old.current_value_ = std::move_if_noexcept(current_value_);
155 current_value_.reset();
156
157 return old;
158}
159
160template <typename ColumnType>
161typename ColumnIterator<ColumnType>::DataHolder& ColumnIterator<ColumnType>::DataHolder::operator++() {
162 ++ind_;
163 current_value_.reset();
164
165 return *this;
166}
167
168template <typename ColumnType>
169typename ColumnIterator<ColumnType>::value_type& ColumnIterator<ColumnType>::DataHolder::UpdateValue() {
170 UASSERT(ind_ < GetColumnSize(column_));
171 if (!current_value_.has_value()) {
172 current_value_.emplace(Get());
173 }
174 return *current_value_;
175}
176
177template <typename ColumnType>
178bool ColumnIterator<ColumnType>::DataHolder::operator==(const DataHolder& other) const {
179 return ind_ == other.ind_ && column_.get() == other.column_.get();
180}
181
182} // namespace columns
183
184} // namespace storages::clickhouse::io
185
186USERVER_NAMESPACE_END