MongoDB C++ Driver 4.2.0
Loading...
Searching...
No Matches
write_concern.hpp
Go to the documentation of this file.
1// Copyright 2009-present MongoDB, Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#pragma once
16
17#include <mongocxx/write_concern-fwd.hpp> // IWYU pragma: export
18
19//
20
22
23#include <chrono>
24#include <cstdint>
25#include <memory> // IWYU pragma: keep: backward compatibility, to be removed.
26#include <stdexcept> // IWYU pragma: keep: backward compatibility, to be removed.
27#include <string>
28#include <utility>
29
30#include <mongocxx/bulk_write-fwd.hpp> // IWYU pragma: keep: backward compatibility, to be removed.
31#include <mongocxx/client-fwd.hpp> // IWYU pragma: keep: backward compatibility, to be removed.
32#include <mongocxx/collection-fwd.hpp> // IWYU pragma: keep: backward compatibility, to be removed.
33#include <mongocxx/database-fwd.hpp> // IWYU pragma: keep: backward compatibility, to be removed.
34#include <mongocxx/options/transaction-fwd.hpp> // IWYU pragma: keep: backward compatibility, to be removed.
35#include <mongocxx/uri-fwd.hpp> // IWYU pragma: keep: backward compatibility, to be removed.
36
40
41#include <mongocxx/options/transaction.hpp> // IWYU pragma: keep: backward compatibility, to be removed.
42
44
45namespace mongocxx {
46namespace v_noabi {
47
55 private:
57
58 public:
73
77 write_concern() = default;
78
82 /* explicit(false) */ write_concern(v1::write_concern rc) : _wc{std::move(rc)} {}
83
92 explicit operator v1::write_concern() && {
93 return std::move(_wc);
94 }
95
99 explicit operator v1::write_concern() const& {
100 return _wc;
101 }
102
111 void journal(bool journal) {
112 _wc.journal(journal);
113 }
114
130 MONGOCXX_ABI_EXPORT_CDECL(void) nodes(std::int32_t confirm_from);
131
157
168 void majority(std::chrono::milliseconds timeout) {
169 this->timeout(timeout);
171 }
172
183 _wc.tag(tag);
184 }
185
196 MONGOCXX_ABI_EXPORT_CDECL(void) timeout(std::chrono::milliseconds timeout);
197
203 bool journal() const {
204 return _wc.journal().value_or(false);
205 }
206
219 return _wc.nodes();
220 }
221
231 return static_cast<level>(_wc.acknowledge_level());
232 }
233
241 if (auto const opt = _wc.tag()) {
242 ret.emplace(*opt);
243 }
244 return ret;
245 }
246
252 bool majority() const {
253 return _wc.acknowledge_level() == v1::write_concern::level::k_majority;
254 }
255
261 std::chrono::milliseconds timeout() const {
262 return _wc.timeout();
263 }
264
270 bool is_acknowledged() const {
271 return _wc.is_acknowledged();
272 }
273
281 return bsoncxx::v_noabi::from_v1(_wc.to_document());
282 }
283
291
292 friend bool operator!=(write_concern const& lhs, write_concern const& rhs) {
293 return !(lhs == rhs);
294 }
295
297
298 class internal;
299};
300
305 return {std::move(v)};
306}
307
312 return v1::write_concern{std::move(v)};
313}
314
315} // namespace v_noabi
316} // namespace mongocxx
317
319
A polyfill for std::optional<T>.
Definition optional.hpp:799
A polyfill for std::string_view.
Definition string_view.hpp:412
A read-only BSON document that owns its underlying buffer.
Definition value.hpp:48
Options related to a MongoDB Write Concern.
Definition write_concern.hpp:49
@ k_majority
"w": "majority"
Definition write_concern.hpp:72
The level of acknowledgment requested for write operations to a MongoDB server.
Definition write_concern.hpp:54
level acknowledge_level() const
Gets the current acknowledgment level.
Definition write_concern.hpp:230
bool is_acknowledged() const
Gets whether this write_concern requires an acknowledged write.
Definition write_concern.hpp:270
void majority(std::chrono::milliseconds timeout)
Requires that a majority of the nodes in a replica set acknowledge a write operation before it is con...
Definition write_concern.hpp:168
write_concern()=default
Constructs a new write_concern.
void timeout(std::chrono::milliseconds timeout)
Sets an upper bound on the time a write concern can take to be satisfied. If the write concern cannot...
friend bool operator!=(write_concern const &lhs, write_concern const &rhs)
Compares two write_concern objects for (in)-equality.
Definition write_concern.hpp:292
std::chrono::milliseconds timeout() const
Gets the current timeout for this write_concern.
Definition write_concern.hpp:261
bool journal() const
Gets the current status of the journal parameter.
Definition write_concern.hpp:203
level
A class to represent the write concern level for write operations.
Definition write_concern.hpp:66
@ k_unacknowledged
Represent write concern with w: 0.
Definition write_concern.hpp:70
@ k_tag
Represent write concern with w: <custom write concern name>.
Definition write_concern.hpp:69
@ k_acknowledged
Represent write concern with w: 1.
Definition write_concern.hpp:71
@ k_majority
Represent write concern with w: "majority".
Definition write_concern.hpp:68
@ k_default
Represent the implicit default write concern.
Definition write_concern.hpp:67
void journal(bool journal)
Sets the journal parameter for this write concern.
Definition write_concern.hpp:111
void acknowledge_level(level confirm_level)
Sets the acknowledge level.
void nodes(std::int32_t confirm_from)
Sets the number of nodes that are required to acknowledge the write before the operation is considere...
bsoncxx::v_noabi::document::value to_document() const
Gets the document form of this write_concern.
Definition write_concern.hpp:280
bool majority() const
Gets whether the majority of nodes is currently required by this write_concern.
Definition write_concern.hpp:252
friend bool operator==(write_concern const &lhs, write_concern const &rhs)
Compares two write_concern objects for (in)-equality.
write_concern(v1::write_concern rc)
Construct with the mongocxx::v1 equivalent.
Definition write_concern.hpp:82
void tag(bsoncxx::v_noabi::stdx::string_view tag)
Sets the name representing the server-side getLastErrorMode entry containing the list of nodes that m...
Definition write_concern.hpp:182
bsoncxx::v_noabi::stdx::optional< std::string > tag() const
Gets the current getLastErrorMode that is required by this write_concern.
Definition write_concern.hpp:239
bsoncxx::v_noabi::stdx::optional< std::int32_t > nodes() const
Gets the current number of nodes that this write_concern requires operations to reach....
Definition write_concern.hpp:218
#define MONGOCXX_ABI_EXPORT_CDECL(...)
Equivalent to MONGOCXX_ABI_EXPORT with MONGOCXX_ABI_CDECL.
Definition export.hpp:52
The mongocxx v_noabi macro guard postlude header.
The mongocxx v_noabi macro guard prelude header.
v_noabi::array::value from_v1(v1::array::value const &v)
Convert from the bsoncxx::v1 equivalent of v.
Definition value.hpp:267
Declares entities whose ABI stability is NOT guaranteed.
v1::bulk_write to_v1(v_noabi::bulk_write v)
Convert to the mongocxx::v1 equivalent of v.
Definition bulk_write.hpp:162
v_noabi::bulk_write from_v1(v1::bulk_write v)
Convert to the mongocxx::v_noabi equivalent of v.
Definition bulk_write.hpp:155
The top-level namespace within which all mongocxx library entities are declared.
Declares mongocxx::v_noabi::options::transaction.
Provides mongocxx::v_noabi::options::transaction.
Provides mongocxx::v1::write_concern.
Provides bsoncxx::v_noabi::document::value.
Provides std::optional-related polyfills for library API usage.
Provides std::string_view-related polyfills for library API usage.
Declares mongocxx::v_noabi::bulk_write.
Declares mongocxx::v_noabi::client.
Declares mongocxx::v_noabi::collection.
Declares mongocxx::v_noabi::database.
Declares mongocxx::v_noabi::uri.
Declares mongocxx::v_noabi::write_concern.