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 {
41 public:
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
87 private:
88 friend struct std::hash<Oid>;
89
90 bson_oid_t oid_;
91};
92
93/// BSON Binary
94class Binary {
95 public:
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 {
105 return reinterpret_cast<const uint8_t*>(data_.data());
106 }
107 size_t Size() const { return data_.size(); }
108 /// @}
109
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 bool operator<=(const Binary& rhs) const { return data_ <= rhs.data_; }
115 bool operator>=(const Binary& rhs) const { return data_ >= rhs.data_; }
116
117 private:
118 friend struct std::hash<Binary>;
119
120 std::string data_;
121};
122
123/// @brief BSON Decimal128
124/// @see
125/// https://github.com/mongodb/specifications/blob/master/source/bson-decimal128/decimal128.rst
127 public:
128 /// Constructor from a string form
129 explicit Decimal128(const std::string& value);
130
131 /// @cond
132 /// Constructor from native type
133 /* implicit */ Decimal128(const bson_decimal128_t&);
134 /// @endcond
135
136 /// Returns string form
137 std::string ToString() const;
138
139 /// Returns an infinite value
141
142 /// Returns a not-a-number value
143 static Decimal128 NaN();
144
145 /// @cond
146 /// Native type access, internal use only
147 const bson_decimal128_t* GetNative() const;
148 /// @endcond
149
150 bool operator==(const Decimal128&) const;
151 bool operator!=(const Decimal128&) const;
152
153 private:
154 bson_decimal128_t decimal_;
155};
156
157/// BSON MinKey
158class MinKey {};
159
160/// BSON MaxKey
161class MaxKey {};
162
163/// @brief BSON Timestamp
164/// @warning Do not use this type for time point representation!
165/// It is very limited and intended for internal MongoDB use.
167 public:
168 /// @brief Creates an empty timestamp
169 /// @note MongoDB only replaces empty timestamps in top-level fields.
171
172 /// Creates a timestamp with specified values
173 Timestamp(uint32_t timestamp, uint32_t increment);
174
175 /// Returns stored unix timestamp
176 time_t GetTimestamp() const;
177
178 /// Returns stored increment
179 uint32_t GetIncrement() const;
180
181 /// Returns packed 64-bit timestamp value
182 uint64_t Pack() const;
183
184 /// Restores a timestamp from the packed form
185 static Timestamp Unpack(uint64_t);
186
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 bool operator<=(const Timestamp&) const;
192 bool operator>=(const Timestamp&) const;
193
194 private:
195 uint32_t timestamp_{0};
196 uint32_t increment_{0};
197};
198
199} // namespace formats::bson
200
201USERVER_NAMESPACE_END
202
203namespace std {
204
205template <>
206struct hash<USERVER_NAMESPACE::formats::bson::Oid> {
207 size_t operator()(const USERVER_NAMESPACE::formats::bson::Oid&) const;
208};
209
210template <>
211struct hash<USERVER_NAMESPACE::formats::bson::Binary> {
212 size_t operator()(
213 const USERVER_NAMESPACE::formats::bson::Binary& binary) const {
214 return hash<string>()(binary.data_);
215 }
216};
217
218template <>
219struct hash<USERVER_NAMESPACE::formats::bson::Timestamp> {
220 size_t operator()(
221 const USERVER_NAMESPACE::formats::bson::Timestamp& timestamp) const;
222};
223
224} // namespace std