userver: userver/storages/mongo/bulk_ops.hpp Source File
Loading...
Searching...
No Matches
bulk_ops.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/storages/mongo/bulk_ops.hpp
4/// @brief Bulk sub-operation models
5
6#include <userver/compiler/select.hpp>
7#include <userver/formats/bson/document.hpp>
8#include <userver/formats/bson/value.hpp>
9#include <userver/storages/mongo/options.hpp>
10#include <userver/utils/fast_pimpl.hpp>
11
12USERVER_NAMESPACE_BEGIN
13
14namespace storages::mongo::operations {
15class Bulk;
16} // namespace storages::mongo::operations
17
18/// Bulk sub-operations
19namespace storages::mongo::bulk_ops {
20
21/// Inserts a single document as part of bulk operation
22class InsertOne {
23public:
24 explicit InsertOne(formats::bson::Document document);
25 ~InsertOne();
26
27 InsertOne(const InsertOne&);
28 InsertOne(InsertOne&&) noexcept;
29 InsertOne& operator=(const InsertOne&);
30 InsertOne& operator=(InsertOne&&) noexcept;
31
32 void SetOption() const {}
33
34private:
35 friend class storages::mongo::operations::Bulk;
36
37 class Impl;
38 static constexpr std::size_t kSize =
40 .For64Bit(16)
41 .For32Bit(8);
42 static constexpr size_t kAlignment = alignof(void*);
43 utils::FastPimpl<Impl, kSize, kAlignment, utils::kStrictMatch> impl_;
44};
45
46/// Replaces a single document as part of bulk operation
48public:
49 ReplaceOne(formats::bson::Document selector, formats::bson::Document replacement);
50 ~ReplaceOne();
51
52 ReplaceOne(const ReplaceOne&);
53 ReplaceOne(ReplaceOne&&) noexcept;
54 ReplaceOne& operator=(const ReplaceOne&);
55 ReplaceOne& operator=(ReplaceOne&&) noexcept;
56
57 void SetOption(options::Upsert);
58
59private:
60 friend class storages::mongo::operations::Bulk;
61
62 class Impl;
63 static constexpr std::size_t kSize =
65 .For64Bit(48)
66 .For32Bit(24);
67 static constexpr size_t kAlignment = alignof(void*);
68 utils::FastPimpl<Impl, kSize, kAlignment, utils::kStrictMatch> impl_;
69};
70
71/// Updates documents as part of bulk operation
72class Update {
73public:
74 enum class Mode { kSingle, kMulti };
75
76 Update(Mode mode, formats::bson::Document selector, formats::bson::Document update);
77
78 /// @brief Creates an update operation with an aggregation pipeline
79 /// @note `update` must be either an update document or an aggregation pipeline array
80 /// @note Available starting in MongoDB 4.2
81 Update(Mode mode, formats::bson::Document selector, formats::bson::Value update);
82
83 ~Update();
84
85 Update(const Update&);
86 Update(Update&&) noexcept;
87 Update& operator=(const Update&);
88 Update& operator=(Update&&) noexcept;
89
90 void SetOption(options::Upsert);
91 void SetOption(const options::ArrayFilters&);
92
93 /// @note Available starting in MongoDB 4.2
94 void SetOption(const options::Hint&);
95
96private:
97 friend class storages::mongo::operations::Bulk;
98
99 class Impl;
100 static constexpr std::size_t kSize =
101 compiler::SelectSize() //
102 .For64Bit(56)
103 .For32Bit(28);
104 static constexpr size_t kAlignment = alignof(void*);
105 utils::FastPimpl<Impl, kSize, kAlignment, utils::kStrictMatch> impl_;
106};
107
108/// Deletes documents as part of bulk operation
109class Delete {
110public:
111 enum class Mode { kSingle, kMulti };
112
113 Delete(Mode mode, formats::bson::Document selector);
114 ~Delete();
115
116 Delete(const Delete&);
117 Delete(Delete&&) noexcept;
118 Delete& operator=(const Delete&);
119 Delete& operator=(Delete&&) noexcept;
120
121 /// @note Available starting in MongoDB 4.2
122 void SetOption(const options::Hint&);
123
124private:
125 friend class storages::mongo::operations::Bulk;
126
127 class Impl;
128 static constexpr std::size_t kSize =
129 compiler::SelectSize() //
130 .For64Bit(40)
131 .For32Bit(20);
132 static constexpr size_t kAlignment = alignof(void*);
133 utils::FastPimpl<Impl, kSize, kAlignment, utils::kStrictMatch> impl_;
134};
135
136} // namespace storages::mongo::bulk_ops
137
138USERVER_NAMESPACE_END