userver: userver/storages/mongo/operations.hpp Source File
Loading...
Searching...
No Matches
operations.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/storages/mongo/operations.hpp
4/// @brief Collection operation models
5
6#include <userver/formats/bson/document.hpp>
7#include <userver/formats/bson/value.hpp>
8#include <userver/storages/mongo/options.hpp>
9#include <userver/utils/fast_pimpl.hpp>
10
11USERVER_NAMESPACE_BEGIN
12
13namespace storages::mongo::impl::cdriver {
14class CDriverCollectionImpl;
15} // namespace storages::mongo::impl::cdriver
16
17/// Collection operations
18namespace storages::mongo::operations {
19
20/// Counts documents matching the filter
21class Count {
22public:
23 explicit Count(formats::bson::Document filter);
24 ~Count();
25
26 Count(const Count&);
27 Count(Count&&) noexcept;
28 Count& operator=(const Count&);
29 Count& operator=(Count&&) noexcept;
30
31 void SetOption(const options::ReadPreference&);
32 void SetOption(options::ReadPreference::Mode);
33 void SetOption(options::ReadConcern);
34 void SetOption(options::Skip);
35 void SetOption(options::Limit);
36 void SetOption(options::ForceCountImpl);
37 void SetOption(const options::MaxServerTime&);
38 void SetOption(const options::Hint&);
39
40private:
41 friend class storages::mongo::impl::cdriver::CDriverCollectionImpl;
42
43 class Impl;
44 static constexpr size_t kSize = 96;
45 static constexpr size_t kAlignment = 8;
46 // MAC_COMPAT: std::string size differs
47 utils::FastPimpl<Impl, kSize, kAlignment, false> impl_;
48};
49
50/// Returns approximate number of documents in a collection
52public:
53 CountApprox();
54 ~CountApprox();
55
56 CountApprox(const CountApprox&);
57 CountApprox(CountApprox&&) noexcept;
58 CountApprox& operator=(const CountApprox&);
59 CountApprox& operator=(CountApprox&&) noexcept;
60
61 void SetOption(const options::ReadPreference&);
62 void SetOption(options::ReadPreference::Mode);
63 void SetOption(options::ReadConcern);
64 void SetOption(options::Skip);
65 void SetOption(options::Limit);
66 void SetOption(const options::MaxServerTime&);
67
68private:
69 friend class storages::mongo::impl::cdriver::CDriverCollectionImpl;
70
71 class Impl;
72 static constexpr size_t kSize = 72;
73 static constexpr size_t kAlignment = 8;
74 // MAC_COMPAT: std::string size differs
75 utils::FastPimpl<Impl, kSize, kAlignment, false> impl_;
76};
77
78/// Retrieves documents matching the filter
79class Find {
80public:
81 explicit Find(formats::bson::Document filter);
82 ~Find();
83
84 Find(const Find&);
85 Find(Find&&) noexcept;
86 Find& operator=(const Find&);
87 Find& operator=(Find&&) noexcept;
88
89 void SetOption(const options::ReadPreference&);
90 void SetOption(options::ReadPreference::Mode);
91 void SetOption(options::ReadConcern);
92 void SetOption(options::Skip);
93 void SetOption(options::Limit);
94 void SetOption(options::Projection);
95 void SetOption(const options::Sort&);
96 void SetOption(const options::Hint&);
97 void SetOption(options::AllowPartialResults);
98 void SetOption(options::Tailable);
99 void SetOption(const options::Comment&);
100 void SetOption(const options::MaxServerTime&);
101
102private:
103 friend class storages::mongo::impl::cdriver::CDriverCollectionImpl;
104
105 class Impl;
106 static constexpr size_t kSize = 96;
107 static constexpr size_t kAlignment = 8;
108 // MAC_COMPAT: std::string size differs
109 utils::FastPimpl<Impl, kSize, kAlignment, false> impl_;
110};
111
112/// Inserts a single document
114public:
115 explicit InsertOne(formats::bson::Document document);
116 ~InsertOne();
117
118 InsertOne(const InsertOne&);
119 InsertOne(InsertOne&&) noexcept;
120 InsertOne& operator=(const InsertOne&);
121 InsertOne& operator=(InsertOne&&) noexcept;
122
123 void SetOption(options::WriteConcern::Level);
124 void SetOption(const options::WriteConcern&);
125 void SetOption(options::SuppressServerExceptions);
126
127private:
128 friend class storages::mongo::impl::cdriver::CDriverCollectionImpl;
129
130 class Impl;
131 static constexpr size_t kSize = 80;
132 static constexpr size_t kAlignment = 8;
133 // MAC_COMPAT: std::string size differs
134 utils::FastPimpl<Impl, kSize, kAlignment, false> impl_;
135};
136
137/// Inserts multiple documents
139public:
140 InsertMany();
141 explicit InsertMany(std::vector<formats::bson::Document> documents);
142 ~InsertMany();
143
144 InsertMany(const InsertMany&);
145 InsertMany(InsertMany&&) noexcept;
146 InsertMany& operator=(const InsertMany&);
147 InsertMany& operator=(InsertMany&&) noexcept;
148
149 void Append(formats::bson::Document document);
150
151 void SetOption(options::Unordered);
152 void SetOption(options::WriteConcern::Level);
153 void SetOption(const options::WriteConcern&);
154 void SetOption(options::SuppressServerExceptions);
155
156private:
157 friend class storages::mongo::impl::cdriver::CDriverCollectionImpl;
158
159 class Impl;
160 static constexpr size_t kSize = 88;
161 static constexpr size_t kAlignment = 8;
162 // MAC_COMPAT: std::string size differs
163 utils::FastPimpl<Impl, kSize, kAlignment, false> impl_;
164};
165
166/// Replaces a single document
168public:
169 ReplaceOne(formats::bson::Document selector, formats::bson::Document replacement);
170 ~ReplaceOne();
171
172 ReplaceOne(const ReplaceOne&);
173 ReplaceOne(ReplaceOne&&) noexcept;
174 ReplaceOne& operator=(const ReplaceOne&);
175 ReplaceOne& operator=(ReplaceOne&&) noexcept;
176
177 void SetOption(options::Upsert);
178 void SetOption(options::WriteConcern::Level);
179 void SetOption(const options::WriteConcern&);
180 void SetOption(options::SuppressServerExceptions);
181
182private:
183 friend class storages::mongo::impl::cdriver::CDriverCollectionImpl;
184
185 class Impl;
186 static constexpr size_t kSize = 96;
187 static constexpr size_t kAlignment = 8;
188 // MAC_COMPAT: std::string size differs
189 utils::FastPimpl<Impl, kSize, kAlignment, false> impl_;
190};
191
192/// Updates documents
193class Update {
194public:
195 enum class Mode { kSingle, kMulti };
196
197 Update(Mode mode, formats::bson::Document selector, formats::bson::Document update);
198 ~Update();
199
200 Update(const Update&);
201 Update(Update&&) noexcept;
202 Update& operator=(const Update&);
203 Update& operator=(Update&&) noexcept;
204
205 void SetOption(options::Upsert);
206 void SetOption(options::RetryDuplicateKey);
207 void SetOption(options::WriteConcern::Level);
208 void SetOption(const options::WriteConcern&);
209 void SetOption(options::SuppressServerExceptions);
210 void SetOption(const options::ArrayFilters&);
211
212 /// @note Available starting in MongoDB 4.2.1
213 void SetOption(const options::Hint&);
214
215private:
216 friend class storages::mongo::impl::cdriver::CDriverCollectionImpl;
217
218 class Impl;
219 static constexpr size_t kSize = 96;
220 static constexpr size_t kAlignment = 8;
221 // MAC_COMPAT: std::string size differs
222 utils::FastPimpl<Impl, kSize, kAlignment, false> impl_;
223};
224
225/// Deletes documents
226class Delete {
227public:
228 enum class Mode { kSingle, kMulti };
229
230 Delete(Mode mode, formats::bson::Document selector);
231 ~Delete();
232
233 Delete(const Delete&);
234 Delete(Delete&&) noexcept;
235 Delete& operator=(const Delete&);
236 Delete& operator=(Delete&&) noexcept;
237
238 void SetOption(options::WriteConcern::Level);
239 void SetOption(const options::WriteConcern&);
240 void SetOption(options::SuppressServerExceptions);
241
242private:
243 friend class storages::mongo::impl::cdriver::CDriverCollectionImpl;
244
245 class Impl;
246 static constexpr size_t kSize = 80;
247 static constexpr size_t kAlignment = 8;
248 // MAC_COMPAT: std::string size differs
249 utils::FastPimpl<Impl, kSize, kAlignment, false> impl_;
250};
251
252/// Atomically updates a document and returns either previous or new version
254public:
255 FindAndModify(formats::bson::Document query, const formats::bson::Document& update);
256 ~FindAndModify();
257
258 FindAndModify(const FindAndModify&) = delete;
259 FindAndModify(FindAndModify&&) noexcept;
260 FindAndModify& operator=(const FindAndModify&) = delete;
261 FindAndModify& operator=(FindAndModify&&) noexcept;
262
263 void SetOption(options::ReturnNew);
264 void SetOption(options::Upsert);
265 void SetOption(options::RetryDuplicateKey);
266 void SetOption(const options::Sort&);
267 void SetOption(options::Projection);
268 void SetOption(options::WriteConcern::Level);
269 void SetOption(const options::WriteConcern&);
270 void SetOption(const options::MaxServerTime&);
271 void SetOption(const options::ArrayFilters&);
272
273private:
274 friend class storages::mongo::impl::cdriver::CDriverCollectionImpl;
275
276 class Impl;
277 static constexpr size_t kSize = 80;
278 static constexpr size_t kAlignment = 8;
279 // MAC_COMPAT: std::string size differs
280 utils::FastPimpl<Impl, kSize, kAlignment, false> impl_;
281};
282
283/// Atomically removes a document and returns it
285public:
286 explicit FindAndRemove(formats::bson::Document query);
287 ~FindAndRemove();
288
289 FindAndRemove(const FindAndRemove&) = delete;
290 FindAndRemove(FindAndRemove&&) noexcept;
291 FindAndRemove& operator=(const FindAndRemove&) = delete;
292 FindAndRemove& operator=(FindAndRemove&&) noexcept;
293
294 void SetOption(const options::Sort&);
295 void SetOption(options::Projection);
296 void SetOption(options::WriteConcern::Level);
297 void SetOption(const options::WriteConcern&);
298 void SetOption(const options::MaxServerTime&);
299
300private:
301 friend class storages::mongo::impl::cdriver::CDriverCollectionImpl;
302
303 class Impl;
304 static constexpr size_t kSize = 72;
305 static constexpr size_t kAlignment = 8;
306 // MAC_COMPAT: std::string size differs
307 utils::FastPimpl<Impl, kSize, kAlignment, false> impl_;
308};
309
310/// Runs an aggregation pipeline
312public:
313 explicit Aggregate(formats::bson::Value pipeline);
314 ~Aggregate();
315
316 Aggregate(const Aggregate&);
317 Aggregate(Aggregate&&) noexcept;
318 Aggregate& operator=(const Aggregate&);
319 Aggregate& operator=(Aggregate&&) noexcept;
320
321 void SetOption(const options::ReadPreference&);
322 void SetOption(options::ReadPreference::Mode);
323 void SetOption(options::ReadConcern);
324 void SetOption(const options::WriteConcern&);
325 void SetOption(options::WriteConcern::Level);
326 void SetOption(const options::Hint&);
327 void SetOption(const options::Comment&);
328 void SetOption(const options::MaxServerTime&);
329
330private:
331 friend class storages::mongo::impl::cdriver::CDriverCollectionImpl;
332
333 class Impl;
334 static constexpr size_t kSize = 120;
335 static constexpr size_t kAlignment = 8;
336 // MAC_COMPAT: std::string size differs
337 utils::FastPimpl<Impl, kSize, kAlignment, false> impl_;
338};
339
340class Drop {
341public:
342 Drop();
343 ~Drop();
344
345 Drop(const Drop&);
346 Drop(Drop&&) noexcept;
347 Drop& operator=(const Drop&);
348 Drop& operator=(Drop&&) noexcept;
349
350 void SetOption(const options::WriteConcern&);
351 void SetOption(options::WriteConcern::Level);
352
353private:
354 friend class storages::mongo::impl::cdriver::CDriverCollectionImpl;
355
356 class Impl;
357 static constexpr size_t kSize = 56;
358 static constexpr size_t kAlignment = 8;
359 // MAC_COMPAT: std::string size differs
360 utils::FastPimpl<Impl, kSize, kAlignment, false> impl_;
361};
362
363} // namespace storages::mongo::operations
364
365USERVER_NAMESPACE_END