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
39private:
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
51public:
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
67private:
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 {
79public:
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
101private:
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
113public:
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
126private:
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
138public:
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
155private:
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
167public:
168 ReplaceOne(formats::bson::Document selector, formats::bson::Document replacement);
169 ~ReplaceOne();
170
171 ReplaceOne(const ReplaceOne&);
172 ReplaceOne(ReplaceOne&&) noexcept;
173 ReplaceOne& operator=(const ReplaceOne&);
174 ReplaceOne& operator=(ReplaceOne&&) noexcept;
175
176 void SetOption(options::Upsert);
177 void SetOption(options::WriteConcern::Level);
178 void SetOption(const options::WriteConcern&);
179 void SetOption(options::SuppressServerExceptions);
180
181private:
182 friend class storages::mongo::impl::cdriver::CDriverCollectionImpl;
183
184 class Impl;
185 static constexpr size_t kSize = 96;
186 static constexpr size_t kAlignment = 8;
187 // MAC_COMPAT: std::string size differs
188 utils::FastPimpl<Impl, kSize, kAlignment, false> impl_;
189};
190
191/// Updates documents
192class Update {
193public:
194 enum class Mode { kSingle, kMulti };
195
196 Update(Mode mode, formats::bson::Document selector, formats::bson::Document update);
197 ~Update();
198
199 Update(const Update&);
200 Update(Update&&) noexcept;
201 Update& operator=(const Update&);
202 Update& operator=(Update&&) noexcept;
203
204 void SetOption(options::Upsert);
205 void SetOption(options::RetryDuplicateKey);
206 void SetOption(options::WriteConcern::Level);
207 void SetOption(const options::WriteConcern&);
208 void SetOption(options::SuppressServerExceptions);
209 void SetOption(const options::ArrayFilters&);
210
211 /// @note Available starting in MongoDB 4.2.1
212 void SetOption(const options::Hint&);
213
214private:
215 friend class storages::mongo::impl::cdriver::CDriverCollectionImpl;
216
217 class Impl;
218 static constexpr size_t kSize = 96;
219 static constexpr size_t kAlignment = 8;
220 // MAC_COMPAT: std::string size differs
221 utils::FastPimpl<Impl, kSize, kAlignment, false> impl_;
222};
223
224/// Deletes documents
225class Delete {
226public:
227 enum class Mode { kSingle, kMulti };
228
229 Delete(Mode mode, formats::bson::Document selector);
230 ~Delete();
231
232 Delete(const Delete&);
233 Delete(Delete&&) noexcept;
234 Delete& operator=(const Delete&);
235 Delete& operator=(Delete&&) noexcept;
236
237 void SetOption(options::WriteConcern::Level);
238 void SetOption(const options::WriteConcern&);
239 void SetOption(options::SuppressServerExceptions);
240
241private:
242 friend class storages::mongo::impl::cdriver::CDriverCollectionImpl;
243
244 class Impl;
245 static constexpr size_t kSize = 80;
246 static constexpr size_t kAlignment = 8;
247 // MAC_COMPAT: std::string size differs
248 utils::FastPimpl<Impl, kSize, kAlignment, false> impl_;
249};
250
251/// Atomically updates a document and returns either previous or new version
253public:
254 FindAndModify(formats::bson::Document query, const formats::bson::Document& update);
255 ~FindAndModify();
256
257 FindAndModify(const FindAndModify&) = delete;
258 FindAndModify(FindAndModify&&) noexcept;
259 FindAndModify& operator=(const FindAndModify&) = delete;
260 FindAndModify& operator=(FindAndModify&&) noexcept;
261
262 void SetOption(options::ReturnNew);
263 void SetOption(options::Upsert);
264 void SetOption(options::RetryDuplicateKey);
265 void SetOption(const options::Sort&);
266 void SetOption(options::Projection);
267 void SetOption(options::WriteConcern::Level);
268 void SetOption(const options::WriteConcern&);
269 void SetOption(const options::MaxServerTime&);
270 void SetOption(const options::ArrayFilters&);
271
272private:
273 friend class storages::mongo::impl::cdriver::CDriverCollectionImpl;
274
275 class Impl;
276 static constexpr size_t kSize = 80;
277 static constexpr size_t kAlignment = 8;
278 // MAC_COMPAT: std::string size differs
279 utils::FastPimpl<Impl, kSize, kAlignment, false> impl_;
280};
281
282/// Atomically removes a document and returns it
284public:
285 explicit FindAndRemove(formats::bson::Document query);
286 ~FindAndRemove();
287
288 FindAndRemove(const FindAndRemove&) = delete;
289 FindAndRemove(FindAndRemove&&) noexcept;
290 FindAndRemove& operator=(const FindAndRemove&) = delete;
291 FindAndRemove& operator=(FindAndRemove&&) noexcept;
292
293 void SetOption(const options::Sort&);
294 void SetOption(options::Projection);
295 void SetOption(options::WriteConcern::Level);
296 void SetOption(const options::WriteConcern&);
297 void SetOption(const options::MaxServerTime&);
298
299private:
300 friend class storages::mongo::impl::cdriver::CDriverCollectionImpl;
301
302 class Impl;
303 static constexpr size_t kSize = 72;
304 static constexpr size_t kAlignment = 8;
305 // MAC_COMPAT: std::string size differs
306 utils::FastPimpl<Impl, kSize, kAlignment, false> impl_;
307};
308
309/// Runs an aggregation pipeline
311public:
312 explicit Aggregate(formats::bson::Value pipeline);
313 ~Aggregate();
314
315 Aggregate(const Aggregate&);
316 Aggregate(Aggregate&&) noexcept;
317 Aggregate& operator=(const Aggregate&);
318 Aggregate& operator=(Aggregate&&) noexcept;
319
320 void SetOption(const options::ReadPreference&);
321 void SetOption(options::ReadPreference::Mode);
322 void SetOption(options::ReadConcern);
323 void SetOption(const options::WriteConcern&);
324 void SetOption(options::WriteConcern::Level);
325 void SetOption(const options::Hint&);
326 void SetOption(const options::Comment&);
327 void SetOption(const options::MaxServerTime&);
328
329private:
330 friend class storages::mongo::impl::cdriver::CDriverCollectionImpl;
331
332 class Impl;
333 static constexpr size_t kSize = 120;
334 static constexpr size_t kAlignment = 8;
335 // MAC_COMPAT: std::string size differs
336 utils::FastPimpl<Impl, kSize, kAlignment, false> impl_;
337};
338
339class Drop {
340public:
341 Drop();
342 ~Drop();
343
344 Drop(const Drop&);
345 Drop(Drop&&) noexcept;
346 Drop& operator=(const Drop&);
347 Drop& operator=(Drop&&) noexcept;
348
349 void SetOption(const options::WriteConcern&);
350 void SetOption(options::WriteConcern::Level);
351
352private:
353 friend class storages::mongo::impl::cdriver::CDriverCollectionImpl;
354
355 class Impl;
356 static constexpr size_t kSize = 56;
357 static constexpr size_t kAlignment = 8;
358 // MAC_COMPAT: std::string size differs
359 utils::FastPimpl<Impl, kSize, kAlignment, false> impl_;
360};
361
362} // namespace storages::mongo::operations
363
364USERVER_NAMESPACE_END