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 {
22 public:
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
39 private:
40 friend class storages::mongo::impl::cdriver::CDriverCollectionImpl;
41
42 class Impl;
43 static constexpr size_t kSize = 96;
44 static constexpr size_t kAlignment = 8;
45 // MAC_COMPAT: std::string size differs
46 utils::FastPimpl<Impl, kSize, kAlignment, false> impl_;
47};
48
49/// Returns approximate number of documents in a collection
51 public:
52 CountApprox();
53 ~CountApprox();
54
55 CountApprox(const CountApprox&);
56 CountApprox(CountApprox&&) noexcept;
57 CountApprox& operator=(const CountApprox&);
58 CountApprox& operator=(CountApprox&&) noexcept;
59
60 void SetOption(const options::ReadPreference&);
61 void SetOption(options::ReadPreference::Mode);
62 void SetOption(options::ReadConcern);
63 void SetOption(options::Skip);
64 void SetOption(options::Limit);
65 void SetOption(const options::MaxServerTime&);
66
67 private:
68 friend class storages::mongo::impl::cdriver::CDriverCollectionImpl;
69
70 class Impl;
71 static constexpr size_t kSize = 72;
72 static constexpr size_t kAlignment = 8;
73 // MAC_COMPAT: std::string size differs
74 utils::FastPimpl<Impl, kSize, kAlignment, false> impl_;
75};
76
77/// Retrieves documents matching the filter
78class Find {
79 public:
80 explicit Find(formats::bson::Document filter);
81 ~Find();
82
83 Find(const Find&);
84 Find(Find&&) noexcept;
85 Find& operator=(const Find&);
86 Find& operator=(Find&&) noexcept;
87
88 void SetOption(const options::ReadPreference&);
89 void SetOption(options::ReadPreference::Mode);
90 void SetOption(options::ReadConcern);
91 void SetOption(options::Skip);
92 void SetOption(options::Limit);
93 void SetOption(options::Projection);
94 void SetOption(const options::Sort&);
95 void SetOption(const options::Hint&);
96 void SetOption(options::AllowPartialResults);
97 void SetOption(options::Tailable);
98 void SetOption(const options::Comment&);
99 void SetOption(const options::MaxServerTime&);
100
101 private:
102 friend class storages::mongo::impl::cdriver::CDriverCollectionImpl;
103
104 class Impl;
105 static constexpr size_t kSize = 96;
106 static constexpr size_t kAlignment = 8;
107 // MAC_COMPAT: std::string size differs
108 utils::FastPimpl<Impl, kSize, kAlignment, false> impl_;
109};
110
111/// Inserts a single document
113 public:
114 explicit InsertOne(formats::bson::Document document);
115 ~InsertOne();
116
117 InsertOne(const InsertOne&);
118 InsertOne(InsertOne&&) noexcept;
119 InsertOne& operator=(const InsertOne&);
120 InsertOne& operator=(InsertOne&&) noexcept;
121
122 void SetOption(options::WriteConcern::Level);
123 void SetOption(const options::WriteConcern&);
124 void SetOption(options::SuppressServerExceptions);
125
126 private:
127 friend class storages::mongo::impl::cdriver::CDriverCollectionImpl;
128
129 class Impl;
130 static constexpr size_t kSize = 80;
131 static constexpr size_t kAlignment = 8;
132 // MAC_COMPAT: std::string size differs
133 utils::FastPimpl<Impl, kSize, kAlignment, false> impl_;
134};
135
136/// Inserts multiple documents
138 public:
139 InsertMany();
140 explicit InsertMany(std::vector<formats::bson::Document> documents);
141 ~InsertMany();
142
143 InsertMany(const InsertMany&);
144 InsertMany(InsertMany&&) noexcept;
145 InsertMany& operator=(const InsertMany&);
146 InsertMany& operator=(InsertMany&&) noexcept;
147
148 void Append(formats::bson::Document document);
149
150 void SetOption(options::Unordered);
151 void SetOption(options::WriteConcern::Level);
152 void SetOption(const options::WriteConcern&);
153 void SetOption(options::SuppressServerExceptions);
154
155 private:
156 friend class storages::mongo::impl::cdriver::CDriverCollectionImpl;
157
158 class Impl;
159 static constexpr size_t kSize = 88;
160 static constexpr size_t kAlignment = 8;
161 // MAC_COMPAT: std::string size differs
162 utils::FastPimpl<Impl, kSize, kAlignment, false> impl_;
163};
164
165/// Replaces a single document
167 public:
168 ReplaceOne(formats::bson::Document selector,
169 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
182 private:
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 {
194 public:
195 enum class Mode { kSingle, kMulti };
196
197 Update(Mode mode, formats::bson::Document selector,
198 formats::bson::Document update);
199 ~Update();
200
201 Update(const Update&);
202 Update(Update&&) noexcept;
203 Update& operator=(const Update&);
204 Update& operator=(Update&&) noexcept;
205
206 void SetOption(options::Upsert);
207 void SetOption(options::RetryDuplicateKey);
208 void SetOption(options::WriteConcern::Level);
209 void SetOption(const options::WriteConcern&);
210 void SetOption(options::SuppressServerExceptions);
211 void SetOption(const options::ArrayFilters&);
212
213 /// @note Available starting in MongoDB 4.2.1
214 void SetOption(const options::Hint&);
215
216 private:
217 friend class storages::mongo::impl::cdriver::CDriverCollectionImpl;
218
219 class Impl;
220 static constexpr size_t kSize = 96;
221 static constexpr size_t kAlignment = 8;
222 // MAC_COMPAT: std::string size differs
223 utils::FastPimpl<Impl, kSize, kAlignment, false> impl_;
224};
225
226/// Deletes documents
227class Delete {
228 public:
229 enum class Mode { kSingle, kMulti };
230
231 Delete(Mode mode, formats::bson::Document selector);
232 ~Delete();
233
234 Delete(const Delete&);
235 Delete(Delete&&) noexcept;
236 Delete& operator=(const Delete&);
237 Delete& operator=(Delete&&) noexcept;
238
239 void SetOption(options::WriteConcern::Level);
240 void SetOption(const options::WriteConcern&);
241 void SetOption(options::SuppressServerExceptions);
242
243 private:
244 friend class storages::mongo::impl::cdriver::CDriverCollectionImpl;
245
246 class Impl;
247 static constexpr size_t kSize = 80;
248 static constexpr size_t kAlignment = 8;
249 // MAC_COMPAT: std::string size differs
250 utils::FastPimpl<Impl, kSize, kAlignment, false> impl_;
251};
252
253/// Atomically updates a document and returns either previous or new version
255 public:
256 FindAndModify(formats::bson::Document query,
257 const formats::bson::Document& update);
258 ~FindAndModify();
259
260 FindAndModify(const FindAndModify&) = delete;
261 FindAndModify(FindAndModify&&) noexcept;
262 FindAndModify& operator=(const FindAndModify&) = delete;
263 FindAndModify& operator=(FindAndModify&&) noexcept;
264
265 void SetOption(options::ReturnNew);
266 void SetOption(options::Upsert);
267 void SetOption(options::RetryDuplicateKey);
268 void SetOption(const options::Sort&);
269 void SetOption(options::Projection);
270 void SetOption(options::WriteConcern::Level);
271 void SetOption(const options::WriteConcern&);
272 void SetOption(const options::MaxServerTime&);
273 void SetOption(const options::ArrayFilters&);
274
275 private:
276 friend class storages::mongo::impl::cdriver::CDriverCollectionImpl;
277
278 class Impl;
279 static constexpr size_t kSize = 80;
280 static constexpr size_t kAlignment = 8;
281 // MAC_COMPAT: std::string size differs
282 utils::FastPimpl<Impl, kSize, kAlignment, false> impl_;
283};
284
285/// Atomically removes a document and returns it
287 public:
288 explicit FindAndRemove(formats::bson::Document query);
289 ~FindAndRemove();
290
291 FindAndRemove(const FindAndRemove&) = delete;
292 FindAndRemove(FindAndRemove&&) noexcept;
293 FindAndRemove& operator=(const FindAndRemove&) = delete;
294 FindAndRemove& operator=(FindAndRemove&&) noexcept;
295
296 void SetOption(const options::Sort&);
297 void SetOption(options::Projection);
298 void SetOption(options::WriteConcern::Level);
299 void SetOption(const options::WriteConcern&);
300 void SetOption(const options::MaxServerTime&);
301
302 private:
303 friend class storages::mongo::impl::cdriver::CDriverCollectionImpl;
304
305 class Impl;
306 static constexpr size_t kSize = 72;
307 static constexpr size_t kAlignment = 8;
308 // MAC_COMPAT: std::string size differs
309 utils::FastPimpl<Impl, kSize, kAlignment, false> impl_;
310};
311
312/// Runs an aggregation pipeline
314 public:
315 explicit Aggregate(formats::bson::Value pipeline);
316 ~Aggregate();
317
318 Aggregate(const Aggregate&);
319 Aggregate(Aggregate&&) noexcept;
320 Aggregate& operator=(const Aggregate&);
321 Aggregate& operator=(Aggregate&&) noexcept;
322
323 void SetOption(const options::ReadPreference&);
324 void SetOption(options::ReadPreference::Mode);
325 void SetOption(options::ReadConcern);
326 void SetOption(const options::WriteConcern&);
327 void SetOption(options::WriteConcern::Level);
328 void SetOption(const options::Hint&);
329 void SetOption(const options::Comment&);
330 void SetOption(const options::MaxServerTime&);
331
332 private:
333 friend class storages::mongo::impl::cdriver::CDriverCollectionImpl;
334
335 class Impl;
336 static constexpr size_t kSize = 120;
337 static constexpr size_t kAlignment = 8;
338 // MAC_COMPAT: std::string size differs
339 utils::FastPimpl<Impl, kSize, kAlignment, false> impl_;
340};
341
342class Drop {
343 public:
344 Drop();
345 ~Drop();
346
347 Drop(const Drop&);
348 Drop(Drop&&) noexcept;
349 Drop& operator=(const Drop&);
350 Drop& operator=(Drop&&) noexcept;
351
352 void SetOption(const options::WriteConcern&);
353 void SetOption(options::WriteConcern::Level);
354
355 private:
356 friend class storages::mongo::impl::cdriver::CDriverCollectionImpl;
357
358 class Impl;
359 static constexpr size_t kSize = 56;
360 static constexpr size_t kAlignment = 8;
361 // MAC_COMPAT: std::string size differs
362 utils::FastPimpl<Impl, kSize, kAlignment, false> impl_;
363};
364
365} // namespace storages::mongo::operations
366
367USERVER_NAMESPACE_END