userver: userver/storages/mongo/bulk.hpp Source File
Loading...
Searching...
No Matches
bulk.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/storages/mongo/bulk.hpp
4/// @brief Bulk collection operation model
5
6#include <userver/storages/mongo/bulk_ops.hpp>
7#include <userver/storages/mongo/options.hpp>
8#include <userver/utils/fast_pimpl.hpp>
9
10USERVER_NAMESPACE_BEGIN
11
12namespace storages::mongo::impl::cdriver {
13class CDriverCollectionImpl;
14} // namespace storages::mongo::impl::cdriver
15
16namespace storages::mongo::operations {
17
18/// Efficiently executes a number of operations over a single collection
19class Bulk {
20 public:
21 enum class Mode { kOrdered, kUnordered };
22
23 explicit Bulk(Mode);
24 ~Bulk();
25
26 Bulk(const Bulk&) = delete;
27 Bulk(Bulk&&) noexcept;
28 Bulk& operator=(const Bulk&) = delete;
29 Bulk& operator=(Bulk&&) noexcept;
30
31 bool IsEmpty() const;
32
33 void SetOption(options::WriteConcern::Level);
34 void SetOption(const options::WriteConcern&);
35 void SetOption(options::SuppressServerExceptions);
36
37 /// Inserts a single document
38 template <typename... Options>
39 void InsertOne(formats::bson::Document document, Options&&... options);
40
41 /// @brief Replaces a single matching document
42 /// @see options::Upsert
43 template <typename... Options>
44 void ReplaceOne(formats::bson::Document selector,
45 formats::bson::Document replacement, Options&&... options);
46
47 /// @brief Updates a single matching document
48 /// @see options::Upsert
49 template <typename... Options>
50 void UpdateOne(formats::bson::Document selector,
51 formats::bson::Document update, Options&&... options);
52
53 /// @brief Updates all matching documents
54 /// @see options::Upsert
55 template <typename... Options>
56 void UpdateMany(formats::bson::Document selector,
57 formats::bson::Document update, Options&&... options);
58
59 /// Deletes a single matching document
60 template <typename... Options>
61 void DeleteOne(formats::bson::Document selector, Options&&... options);
62
63 /// Deletes all matching documents
64 template <typename... Options>
65 void DeleteMany(formats::bson::Document selector, Options&&... options);
66
67 /// @name Prepared sub-operation inserters
68 /// @{
69 void Append(const bulk_ops::InsertOne&);
70 void Append(const bulk_ops::ReplaceOne&);
71 void Append(const bulk_ops::Update&);
72 void Append(const bulk_ops::Delete&);
73 /// @}
74
75 private:
76 friend class storages::mongo::impl::cdriver::CDriverCollectionImpl;
77
78 class Impl;
79 static constexpr size_t kSize = 56;
80 static constexpr size_t kAlignment = 8;
81 // MAC_COMPAT: std::string size differs
82 utils::FastPimpl<Impl, kSize, kAlignment, false> impl_;
83};
84
85template <typename... Options>
86void Bulk::InsertOne(formats::bson::Document document, Options&&... options) {
87 bulk_ops::InsertOne insert_subop(std::move(document));
88 (insert_subop.SetOption(std::forward<Options>(options)), ...);
89 Append(insert_subop);
90}
91
92template <typename... Options>
93void Bulk::ReplaceOne(formats::bson::Document selector,
94 formats::bson::Document replacement,
95 Options&&... options) {
96 bulk_ops::ReplaceOne replace_subop(std::move(selector),
97 std::move(replacement));
98 (replace_subop.SetOption(std::forward<Options>(options)), ...);
99 Append(replace_subop);
100}
101
102template <typename... Options>
103void Bulk::UpdateOne(formats::bson::Document selector,
104 formats::bson::Document update, Options&&... options) {
105 bulk_ops::Update update_subop(bulk_ops::Update::Mode::kSingle,
106 std::move(selector), std::move(update));
107 (update_subop.SetOption(std::forward<Options>(options)), ...);
108 Append(update_subop);
109}
110
111template <typename... Options>
112void Bulk::UpdateMany(formats::bson::Document selector,
113 formats::bson::Document update, Options&&... options) {
114 bulk_ops::Update update_subop(bulk_ops::Update::Mode::kMulti,
115 std::move(selector), std::move(update));
116 (update_subop.SetOption(std::forward<Options>(options)), ...);
117 Append(update_subop);
118}
119
120template <typename... Options>
121void Bulk::DeleteOne(formats::bson::Document selector, Options&&... options) {
122 bulk_ops::Delete delete_subop(bulk_ops::Delete::Mode::kSingle,
123 std::move(selector));
124 (delete_subop.SetOption(std::forward<Options>(options)), ...);
125 Append(delete_subop);
126}
127
128template <typename... Options>
129void Bulk::DeleteMany(formats::bson::Document selector, Options&&... options) {
130 bulk_ops::Delete delete_subop(bulk_ops::Delete::Mode::kMulti,
131 std::move(selector));
132 (delete_subop.SetOption(std::forward<Options>(options)), ...);
133 Append(delete_subop);
134}
135
136} // namespace storages::mongo::operations
137
138USERVER_NAMESPACE_END