vkdb
A time series database engine in C++.
Loading...
Searching...
No Matches
data_range.h
1#ifndef STORAGE_DATA_RANGE_H
2#define STORAGE_DATA_RANGE_H
3
4#include <vkdb/concepts.h>
5#include <vkdb/time_series_key.h>
6#include <iostream>
7
8namespace vkdb {
14template <RegularNoCVRefQuals TData>
15 requires std::totally_ordered<TData>
16class DataRange {
17public:
18 using data_type = TData;
19
24 DataRange() noexcept
25 : is_set_{false} {}
26
33 DataRange(const data_type& start, const data_type& end) noexcept
34 : is_set_{true}, range_{start, end} {}
35
43 DataRange(std::string&& str) {
44 if (str == "null") {
45 return;
46 }
47
48 auto ampersand_pos{str.find('&')};
49 if (ampersand_pos == std::string::npos) {
50 throw std::invalid_argument{
51 "DataRange::DataRange(): Invalid range string '" + str + "'."
52 };
53 }
54
55 if constexpr (std::is_same_v<data_type, TimeSeriesKey>) {
56 range_.first = TimeSeriesKey{str.substr(0, ampersand_pos)};
57 range_.second = TimeSeriesKey{str.substr(ampersand_pos + 1)};
58 } else {
59 range_.first = std::stod(str.substr(0, ampersand_pos));
60 range_.second = std::stod(str.substr(ampersand_pos + 1));
61 }
62
63 is_set_ = true;
64 }
65
70 DataRange(DataRange&&) noexcept = default;
71
76 DataRange& operator=(DataRange&&) noexcept = default;
77
82 DataRange(const DataRange&) noexcept = default;
83
88 DataRange& operator=(const DataRange&) noexcept = default;
89
94 ~DataRange() noexcept = default;
95
96 [[nodiscard]] auto operator<=>(const DataRange& other) const noexcept {
97 if (!is_set_) {
98 return std::strong_ordering::less;
99 }
100 if (!other.is_set_) {
101 return std::strong_ordering::greater;
102 }
103 return range_ <=> other.range_;
104 }
105
113 void updateRange(const data_type& data) noexcept {
114 if (!is_set_) {
115 range_.first = data;
116 range_.second = data;
117 is_set_ = true;
118 return;
119 }
120
121 range_.first = std::min(range_.first, data);
122 range_.second = std::max(range_.second, data);
123 }
124
132 [[nodiscard]] bool inRange(const data_type& data) const noexcept {
133 return is_set_ && data >= range_.first && data <= range_.second;
134 }
135
144 [[nodiscard]] bool overlapsWith(
145 const data_type& start,
146 const data_type& end
147 ) const noexcept {
148 return is_set_ && range_.first <= end && range_.second >= start;
149 }
150
158 [[nodiscard]] data_type lower() const {
159 if (!is_set_) {
160 throw std::logic_error{"DataRange::lower(): Range is not set."};
161 }
162 return range_.first;
163 }
164
172 [[nodiscard]] data_type upper() const {
173 if (!is_set_) {
174 throw std::logic_error{"DataRange::upper(): Range is not set."};
175 }
176 return range_.second;
177 }
178
183 void clear() noexcept {
184 is_set_ = false;
185 }
186
192 [[nodiscard]] std::string str() const noexcept {
193 if (!is_set_) {
194 return "null";
195 }
196
197 if constexpr (std::is_same_v<data_type, TimeSeriesKey>) {
198 return range_.first.str() + "&" + range_.second.str();
199 } else {
200 return std::to_string(range_.first)
201 + "&" + std::to_string(range_.second);
202 }
203 }
204
205private:
210 bool is_set_;
211
216 std::pair<data_type, data_type> range_;
217};
218
223using TimeRange = DataRange<Timestamp>;
224
229using KeyRange = DataRange<TimeSeriesKey>;
230} // namespace vkdb
231
232#endif // STORAGE_DATA_RANGE_H
A range of data.
Definition data_range.h:16
DataRange(std::string &&str)
Construct a new DataRange object from the given string.
Definition data_range.h:43
data_type lower() const
Get the lower bound of the range.
Definition data_range.h:158
std::string str() const noexcept
Convert the range to a string.
Definition data_range.h:192
data_type upper() const
Get the upper bound of the range.
Definition data_range.h:172
bool overlapsWith(const data_type &start, const data_type &end) const noexcept
Check if the range overlaps with the given range.
Definition data_range.h:144
DataRange(const data_type &start, const data_type &end) noexcept
Construct a new DataRange object from the given start and end.
Definition data_range.h:33
void clear() noexcept
Clear the range.
Definition data_range.h:183
DataRange(DataRange &&) noexcept=default
Move-construct a DataRange object.
bool inRange(const data_type &data) const noexcept
Check if the data is in the range.
Definition data_range.h:132
DataRange() noexcept
Construct a new DataRange object.
Definition data_range.h:24
void updateRange(const data_type &data) noexcept
Update the range with the given data.
Definition data_range.h:113
Represents a key in vkdb.