userver: userver/formats/bson/types.hpp Source File
Loading...
Searching...
No Matches
types.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/formats/bson/types.hpp
4/// @brief BSON-specific types
5
6#include <chrono>
7#include <cstdint>
8#include <functional>
9#include <memory>
10#include <string>
11#include <unordered_map>
12#include <vector>
13
14#include <bson/bson.h>
15
16#include <userver/formats/common/path.hpp>
17
18USERVER_NAMESPACE_BEGIN
19
20namespace formats::bson {
21namespace impl {
22// https://jira.mongodb.org/browse/CDRIVER-3378
23#pragma GCC diagnostic push
24#pragma GCC diagnostic ignored "-Wignored-attributes"
25using BsonHolder = std::shared_ptr<const bson_t>;
26#pragma GCC diagnostic pop
27
28class ValueImpl;
29using ValueImplPtr = std::shared_ptr<ValueImpl>;
30
31using ParsedArray = std::vector<ValueImplPtr>;
32using ParsedDocument = std::unordered_map<std::string, ValueImplPtr>;
33} // namespace impl
34
35using formats::common::Path;
36
37inline constexpr auto kNull = nullptr;
38
39/// BSON ObjectId
40class Oid {
41public:
42 /// Generates a new id
43 Oid();
44
45 /// Constructor from hex-encoded form, zero terminator is not required
46 explicit Oid(std::string_view hex_encoded);
47
48 /// Creates a minimal Oid with specified time point up to a second precision
49 ///
50 /// @throws formats::bson::BsonException if `time` is too big to be stored in
51 /// ObjectId without narrowing.
52 static Oid MakeMinimalFor(std::chrono::system_clock::time_point time);
53
54 /// @cond
55 /// Constructor from native type
56 /* implicit */ Oid(const bson_oid_t&);
57 /// @endcond
58
59 /// Returns hex-encoded value
60 std::string ToString() const;
61
62 /// @name Raw value access
63 /// @{
64 const uint8_t* Data() const;
65
66 constexpr static size_t Size() { return 12; }
67 /// @}
68
69 /// Returns stored unix timestamp
70 time_t GetTimestamp() const;
71
72 /// Returns stored time point
73 std::chrono::system_clock::time_point GetTimePoint() const;
74
75 /// @cond
76 /// Native type access, internal use only
77 const bson_oid_t* GetNative() const;
78 /// @endcond
79
80 bool operator==(const Oid&) const;
81 bool operator!=(const Oid&) const;
82 bool operator<(const Oid&) const;
83 bool operator>(const Oid&) const;
84 bool operator<=(const Oid&) const;
85 bool operator>=(const Oid&) const;
86
87private:
88 friend struct std::hash<Oid>;
89
90 bson_oid_t oid_;
91};
92
93/// BSON Binary
94class Binary {
95public:
96 /// Constructor from a string storage
97 explicit Binary(std::string data) : data_(std::move(data)) {}
98
99 /// @name Raw data access
100 /// @{
101 const std::string& ToString() const& { return data_; }
102 std::string&& ToString() && { return std::move(data_); }
103
104 const uint8_t* Data() const { return reinterpret_cast<const uint8_t*>(data_.data()); }
105 size_t Size() const { return data_.size(); }
106 /// @}
107
108 bool operator==(const Binary& rhs) const { return data_ == rhs.data_; }
109 bool operator!=(const Binary& rhs) const { return data_ != rhs.data_; }
110 bool operator<(const Binary& rhs) const { return data_ < rhs.data_; }
111 bool operator>(const Binary& rhs) const { return data_ > rhs.data_; }
112 bool operator<=(const Binary& rhs) const { return data_ <= rhs.data_; }
113 bool operator>=(const Binary& rhs) const { return data_ >= rhs.data_; }
114
115private:
116 friend struct std::hash<Binary>;
117
118 std::string data_;
119};
120
121/// @brief BSON Decimal128
122/// @see
123/// https://github.com/mongodb/specifications/blob/master/source/bson-decimal128/decimal128.rst
125public:
126 /// Constructor from a string form
127 explicit Decimal128(const std::string& value);
128
129 /// @cond
130 /// Constructor from native type
131 /* implicit */ Decimal128(const bson_decimal128_t&);
132 /// @endcond
133
134 /// Returns string form
135 std::string ToString() const;
136
137 /// Returns an infinite value
139
140 /// Returns a not-a-number value
141 static Decimal128 NaN();
142
143 /// @cond
144 /// Native type access, internal use only
145 const bson_decimal128_t* GetNative() const;
146 /// @endcond
147
148 bool operator==(const Decimal128&) const;
149 bool operator!=(const Decimal128&) const;
150
151private:
152 bson_decimal128_t decimal_;
153};
154
155/// BSON MinKey
156class MinKey {};
157
158/// BSON MaxKey
159class MaxKey {};
160
161/// @brief BSON Timestamp
162/// @warning Do not use this type for time point representation!
163/// It is very limited and intended for internal MongoDB use.
165public:
166 /// @brief Creates an empty timestamp
167 /// @note MongoDB only replaces empty timestamps in top-level fields.
169
170 /// Creates a timestamp with specified values
171 Timestamp(uint32_t timestamp, uint32_t increment);
172
173 /// Returns stored unix timestamp
174 time_t GetTimestamp() const;
175
176 /// Returns stored increment
177 uint32_t GetIncrement() const;
178
179 /// Returns packed 64-bit timestamp value
180 uint64_t Pack() const;
181
182 /// Restores a timestamp from the packed form
183 static Timestamp Unpack(uint64_t);
184
185 bool operator==(const Timestamp&) const;
186 bool operator!=(const Timestamp&) const;
187 bool operator<(const Timestamp&) const;
188 bool operator>(const Timestamp&) const;
189 bool operator<=(const Timestamp&) const;
190 bool operator>=(const Timestamp&) const;
191
192private:
193 uint32_t timestamp_{0};
194 uint32_t increment_{0};
195};
196
197} // namespace formats::bson
198
199USERVER_NAMESPACE_END
200
201namespace std {
202
203template <>
204struct hash<USERVER_NAMESPACE::formats::bson::Oid> {
205 size_t operator()(const USERVER_NAMESPACE::formats::bson::Oid&) const;
206};
207
208template <>
209struct hash<USERVER_NAMESPACE::formats::bson::Binary> {
210 size_t operator()(const USERVER_NAMESPACE::formats::bson::Binary& binary) const {
211 return hash<string>()(binary.data_);
212 }
213};
214
215template <>
216struct hash<USERVER_NAMESPACE::formats::bson::Timestamp> {
217 size_t operator()(const USERVER_NAMESPACE::formats::bson::Timestamp& timestamp) const;
218};
219
220} // namespace std