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 {
20public:
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, formats::bson::Document replacement, Options&&... options);
45
46 /// @brief Updates a single matching document
47 /// @see options::Upsert
48 template <typename... Options>
49 void UpdateOne(formats::bson::Document selector, formats::bson::Document update, Options&&... options);
50
51 /// @brief Updates all matching documents
52 /// @see options::Upsert
53 template <typename... Options>
54 void UpdateMany(formats::bson::Document selector, formats::bson::Document update, Options&&... options);
55
56 /// Deletes a single matching document
57 template <typename... Options>
58 void DeleteOne(formats::bson::Document selector, Options&&... options);
59
60 /// Deletes all matching documents
61 template <typename... Options>
62 void DeleteMany(formats::bson::Document selector, Options&&... options);
63
64 /// @name Prepared sub-operation inserters
65 /// @{
66 void Append(const bulk_ops::InsertOne&);
67 void Append(const bulk_ops::ReplaceOne&);
68 void Append(const bulk_ops::Update&);
69 void Append(const bulk_ops::Delete&);
70 /// @}
71
72private:
73 friend class storages::mongo::impl::cdriver::CDriverCollectionImpl;
74
75 class Impl;
76 static constexpr size_t kSize = 56;
77 static constexpr size_t kAlignment = 8;
78 // MAC_COMPAT: std::string size differs
79 utils::FastPimpl<Impl, kSize, kAlignment, false> impl_;
80};
81
82template <typename... Options>
83void Bulk::InsertOne(formats::bson::Document document, Options&&... options) {
84 bulk_ops::InsertOne insert_subop(std::move(document));
85 (insert_subop.SetOption(std::forward<Options>(options)), ...);
86 Append(insert_subop);
87}
88
89template <typename... Options>
90void Bulk::ReplaceOne(formats::bson::Document selector, formats::bson::Document replacement, Options&&... options) {
91 bulk_ops::ReplaceOne replace_subop(std::move(selector), std::move(replacement));
92 (replace_subop.SetOption(std::forward<Options>(options)), ...);
93 Append(replace_subop);
94}
95
96template <typename... Options>
97void Bulk::UpdateOne(formats::bson::Document selector, formats::bson::Document update, Options&&... options) {
98 bulk_ops::Update update_subop(bulk_ops::Update::Mode::kSingle, std::move(selector), std::move(update));
99 (update_subop.SetOption(std::forward<Options>(options)), ...);
100 Append(update_subop);
101}
102
103template <typename... Options>
104void Bulk::UpdateMany(formats::bson::Document selector, formats::bson::Document update, Options&&... options) {
105 bulk_ops::Update update_subop(bulk_ops::Update::Mode::kMulti, std::move(selector), std::move(update));
106 (update_subop.SetOption(std::forward<Options>(options)), ...);
107 Append(update_subop);
108}
109
110template <typename... Options>
111void Bulk::DeleteOne(formats::bson::Document selector, Options&&... options) {
112 bulk_ops::Delete delete_subop(bulk_ops::Delete::Mode::kSingle, std::move(selector));
113 (delete_subop.SetOption(std::forward<Options>(options)), ...);
114 Append(delete_subop);
115}
116
117template <typename... Options>
118void Bulk::DeleteMany(formats::bson::Document selector, Options&&... options) {
119 bulk_ops::Delete delete_subop(bulk_ops::Delete::Mode::kMulti, std::move(selector));
120 (delete_subop.SetOption(std::forward<Options>(options)), ...);
121 Append(delete_subop);
122}
123
124} // namespace storages::mongo::operations
125
126USERVER_NAMESPACE_END