vkdb
A time series database engine in C++.
Loading...
Searching...
No Matches
time_series_key.cpp
1#include <vkdb/time_series_key.h>
2
3namespace vkdb {
4TimeSeriesKey::TimeSeriesKey(std::string&& str) {
5 auto timestamp_end{str.find('}')};
6 auto metric_start{str.find('{', timestamp_end) + 1};
7 auto metric_end{str.find('}', metric_start)};
8 auto tags_start{str.find('{', metric_end) + 1};
9 auto tags_end{str.find('}', tags_start)};
10
11 timestamp_ = std::stoull(str.substr(1, timestamp_end - 1));
12 metric_ = {str.substr(metric_start, metric_end - metric_start)};
13 auto tags_str{str.substr(tags_start, tags_end - tags_start)};
14
15 TagKey key;
16 TagValue value;
17 std::istringstream ss{tags_str};
18 while (std::getline(ss, key, ':')) {
19 std::getline(ss, value, ',');
20 tags_[key] = value;
21 }
22
23 str_ = std::move(str);
24 hash_ = std::hash<std::string>{}(str_);
25}
26
28 Timestamp timestamp,
29 Metric metric,
30 TagTable tags
31) noexcept
32 : timestamp_{timestamp}
33 , metric_{std::move(metric)}
34 , tags_{std::move(tags)} {
35 std::stringstream ss;
36 ss << "{" << std::setw(TIMESTAMP_WIDTH) << std::setfill('0');
37 ss << timestamp_ << "}{" << metric_ << "}{";
38 for (const auto& [key, value] : tags_) {
39 ss << key << ":" << value << ",";
40 }
41 ss.seekp(-!tags_.empty(), std::ios_base::end);
42 ss << "}";
43 str_ = ss.str();
44 hash_ = std::hash<std::string>{}(str_);
45}
46
47bool TimeSeriesKey::operator==(const TimeSeriesKey& other) const noexcept {
48 return hash_ == other.hash_;
49}
50
51bool TimeSeriesKey::operator!=(const TimeSeriesKey& other) const noexcept {
52 return !(*this == other);
53}
54
55bool TimeSeriesKey::operator<(const TimeSeriesKey& other) const noexcept {
56 if (other.hash_ == MIN_TIME_SERIES_KEY_HASH) {
57 return false;
58 }
59 if (hash_ == MIN_TIME_SERIES_KEY_HASH) {
60 return true;
61 }
62 if (hash_ == MAX_TIME_SERIES_KEY_HASH) {
63 return false;
64 }
65 if (other.hash_ == MAX_TIME_SERIES_KEY_HASH) {
66 return true;
67 }
68 if (timestamp_ != other.timestamp_) {
69 return timestamp_ < other.timestamp_;
70 }
71 if (metric_ != other.metric_) {
72 return metric_ < other.metric_;
73 }
74 return tags_ < other.tags_;
75}
76
77bool TimeSeriesKey::operator>(const TimeSeriesKey& other) const noexcept {
78 return other < *this;
79}
80
81bool TimeSeriesKey::operator<=(const TimeSeriesKey& other) const noexcept {
82 return !(*this > other);
83}
84
85bool TimeSeriesKey::operator>=(const TimeSeriesKey& other) const noexcept {
86 return !(*this < other);
87}
88
89Timestamp TimeSeriesKey::timestamp() const noexcept {
90 return timestamp_;
91}
92
93Metric TimeSeriesKey::metric() const noexcept {
94 return metric_;
95}
96
97const TagTable& TimeSeriesKey::tags() const noexcept {
98 return tags_;
99}
100
101std::string TimeSeriesKey::str() const noexcept {
102 return str_;
103}
104} // namespace vkdb
105
106std::ostream& operator<<(std::ostream& os, const vkdb::TimeSeriesKey& key) {
107 os << key.str();
108 return os;
109}
110
111std::istream& operator>>(std::istream& is, vkdb::TimeSeriesKey& key) {
112 std::string str;
113 is >> str;
114 key = vkdb::TimeSeriesKey{std::move(str)};
115 return is;
116}
Represents a key in vkdb.
const TagTable & tags() const noexcept
Get the tags.
TimeSeriesKey() noexcept=default
Construct a new TimeSeriesKey object.
Metric metric() const noexcept
Get the metric.
bool operator>=(const TimeSeriesKey &other) const noexcept
Greater-than-or-equal-to operator.
bool operator==(const TimeSeriesKey &other) const noexcept
Equality operator.
bool operator<=(const TimeSeriesKey &other) const noexcept
Less-than-or-equal-to operator.
bool operator>(const TimeSeriesKey &other) const noexcept
Greater-than operator.
std::string str() const noexcept
Get the string representation of the key.
bool operator<(const TimeSeriesKey &other) const noexcept
Less-than operator.
Timestamp timestamp() const noexcept
Get the timestamp.
bool operator!=(const TimeSeriesKey &other) const noexcept
Inequality operator.