vkdb
A time series database engine in C++.
Loading...
Searching...
No Matches
time_series_key.h
1#ifndef STORAGE_TIME_SERIES_KEY_H
2#define STORAGE_TIME_SERIES_KEY_H
3
4#include <vkdb/concepts.h>
5#include <cstdint>
6#include <iomanip>
7#include <limits>
8#include <map>
9#include <optional>
10#include <string>
11#include <sstream>
12
13namespace vkdb {
18using Timestamp = uint64_t;
19
24using Metric = std::string;
25
30using TagKey = std::string;
31
36using TagValue = std::string;
37
42using Tag = std::pair<TagKey, TagValue>;
43
48using TagTable = std::map<TagKey, TagValue>;
49
55template <ArithmeticNoCVRefQuals TValue>
56struct DataPoint {
61 Timestamp timestamp;
62
67 Metric metric;
68
73 TagTable tags;
74
79 TValue value;
80};
81
87public:
92 static constexpr auto TIMESTAMP_WIDTH{20};
93
98 static constexpr auto MAX_METRIC_LENGTH{15};
99
104 TimeSeriesKey() noexcept = default;
105
113 explicit TimeSeriesKey(std::string&& str);
114
123 explicit TimeSeriesKey(
124 Timestamp timestamp,
125 Metric metric,
126 TagTable tags
127 ) noexcept;
128
133 TimeSeriesKey(TimeSeriesKey&&) noexcept = default;
134
139 TimeSeriesKey& operator=(TimeSeriesKey&&) noexcept = default;
140
145 TimeSeriesKey(const TimeSeriesKey&) noexcept = default;
146
151 TimeSeriesKey& operator=(const TimeSeriesKey&) noexcept = default;
152
157 ~TimeSeriesKey() noexcept = default;
158
167 [[nodiscard]] bool operator==(const TimeSeriesKey& other) const noexcept;
168
177 [[nodiscard]] bool operator!=(const TimeSeriesKey& other) const noexcept;
178
194 [[nodiscard]] bool operator<(const TimeSeriesKey& other) const noexcept;
195
204 [[nodiscard]] bool operator>(const TimeSeriesKey& other) const noexcept;
205
214 [[nodiscard]] bool operator<=(const TimeSeriesKey& other) const noexcept;
215
224 [[nodiscard]] bool operator>=(const TimeSeriesKey& other) const noexcept;
225
231 [[nodiscard]] Timestamp timestamp() const noexcept;
232
238 [[nodiscard]] Metric metric() const noexcept;
239
245 [[nodiscard]] const TagTable& tags() const noexcept;
246
252 [[nodiscard]] std::string str() const noexcept;
253
254private:
259 Timestamp timestamp_;
260
265 Metric metric_;
266
271 TagTable tags_;
272
277 std::string str_;
278
283 size_t hash_;
284};
285
291template <ArithmeticNoCVRefQuals TValue>
292using TimeSeriesEntry = std::pair<const TimeSeriesKey, std::optional<TValue>>;
293} // namespace vkdb
294
303std::ostream& operator<<(std::ostream& os, const vkdb::TimeSeriesKey& key);
304
313std::istream& operator>>(std::istream& is, vkdb::TimeSeriesKey& key);
314
319static const vkdb::TimeSeriesKey MIN_TIME_SERIES_KEY{
320 0,
321 "MIN_TIME_SERIES_KEY",
322 {{"MIN_TIME_SERIES_KEY", "MIN_TIME_SERIES_KEY"}}
323};
324
329static const vkdb::TimeSeriesKey MAX_TIME_SERIES_KEY{
330 std::numeric_limits<vkdb::Timestamp>::max(),
331 "MAX_TIME_SERIES_KEY",
332 {{"MAX_TIME_SERIES_KEY", "MAX_TIME_SERIES_KEY"}}
333};
334
339const vkdb::Metric MIN_METRIC{std::string()};
340
345const vkdb::Metric MAX_METRIC{
346 std::string(vkdb::TimeSeriesKey::MAX_METRIC_LENGTH + 1, '\xFF')
347};
348
349namespace std {
355template <>
356struct hash<vkdb::TimeSeriesKey> {
357 size_t operator()(const vkdb::TimeSeriesKey& key) const noexcept {
358 return hash<string>{}(key.str());
359 }
360};
361} // namespace std
362
363
368static const size_t MIN_TIME_SERIES_KEY_HASH{
369 std::hash<vkdb::TimeSeriesKey>{}(MIN_TIME_SERIES_KEY)
370};
371
376static const size_t MAX_TIME_SERIES_KEY_HASH{
377 std::hash<vkdb::TimeSeriesKey>{}(MAX_TIME_SERIES_KEY)
378};
379
380#endif // STORAGE_TIME_SERIES_KEY_H
Represents a key in vkdb.
static constexpr auto TIMESTAMP_WIDTH
Timestamp width.
const TagTable & tags() const noexcept
Get the tags.
TimeSeriesKey() noexcept=default
Construct a new TimeSeriesKey object.
Metric metric() const noexcept
Get the metric.
std::string str() const noexcept
Get the string representation of the key.
static constexpr auto MAX_METRIC_LENGTH
Metric width.
Timestamp timestamp() const noexcept
Get the timestamp.
Concept for arithmetic types that have no cv- or ref-qualifiers.
Definition concepts.h:47
STL namespace.
Represents a datapoint in vkdb.
Metric metric
Metric.
TagTable tags
Tags.
Timestamp timestamp
Timestamp.
TValue value
Value.