MongoDB C++ Driver current
Loading...
Searching...
No Matches
client_session.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/options/client_session-fwd.hpp> // IWYU pragma: export
18
19//
20
21#include <mongocxx/v1/client_session.hpp> // IWYU pragma: export
22
23#include <utility>
24
25#include <mongocxx/client_session-fwd.hpp> // IWYU pragma: keep: backward compatibility, to be removed.
26
28#include <bsoncxx/types.hpp>
29
31
33
34namespace mongocxx {
35namespace v_noabi {
36namespace options {
37
42 public:
46 client_session() = default;
47
51 /* explicit(false) */ client_session(v1::client_session::options const& opts)
52 : _causal_consistency{opts.causal_consistency()},
53 _snapshot{opts.snapshot()},
54 _default_transaction_opts{opts.default_transaction_opts()} {
55 if (auto const st = opts.snapshot_time()) {
56 _snapshot_time = bsoncxx::v_noabi::from_v1(*st);
57 }
58 }
59
63 explicit operator v1::client_session::options() const {
65
67
68 if (_causal_consistency) {
69 ret.causal_consistency(*_causal_consistency);
70 }
71
72 if (_snapshot) {
73 ret.snapshot(*_snapshot);
74 }
75
76 if (_snapshot_time) {
77 ret.snapshot_time(bsoncxx::v_noabi::to_v1(*_snapshot_time));
78 }
79
80 if (_default_transaction_opts) {
81 ret.default_transaction_opts(to_v1(*_default_transaction_opts));
82 }
83
84 return ret;
85 }
86
106 _causal_consistency = causal_consistency;
107 return *this;
108 }
109
114 bool causal_consistency() const noexcept {
115 return _causal_consistency.value_or(true);
116 }
117
132 client_session& snapshot(bool enable_snapshot_reads) noexcept {
133 _snapshot = enable_snapshot_reads;
134 return *this;
135 }
136
140 bool snapshot() const noexcept {
141 return _snapshot.value_or(false);
142 }
143
162
169
181 _default_transaction_opts = std::move(default_transaction_opts);
182 return *this;
183 }
184
191 return _default_transaction_opts;
192 }
193
194 private:
195 bsoncxx::v_noabi::stdx::optional<bool> _causal_consistency;
198 bsoncxx::v_noabi::stdx::optional<transaction> _default_transaction_opts;
199};
200
201} // namespace options
202} // namespace v_noabi
203} // namespace mongocxx
204
205namespace mongocxx {
206namespace v_noabi {
207
214
221
222} // namespace v_noabi
223} // namespace mongocxx
224
226
A polyfill for std::optional<T>.
Definition optional.hpp:799
client_session()=default
Default initialization.
Options for mongocxx::v1::client_session.
Definition client_session.hpp:292
options & default_transaction_opts(v1::transaction_options const &v)
Set the "defaultTransactionOptions" field.
options & snapshot_time(bsoncxx::v1::types::b_timestamp v)
Set the "snapshotTime" ("atClusterTime") field.
options & snapshot(bool v)
Set the "snapshot" field.
options & causal_consistency(bool v)
Set the "causalConsistency" field.
Used by client sessions.
Definition client_session.hpp:41
client_session & snapshot(bool enable_snapshot_reads) noexcept
Sets the read concern "snapshot" (not enabled by default).
Definition client_session.hpp:132
client_session(v1::client_session::options const &opts)
Construct with the mongocxx::v1 equivalent.
Definition client_session.hpp:51
client_session & default_transaction_opts(transaction default_transaction_opts)
Sets the default transaction options.
Definition client_session.hpp:180
bsoncxx::v_noabi::stdx::optional< bsoncxx::v_noabi::types::b_timestamp > const & snapshot_time() const
Gets the value of the snapshot_time option.
Definition client_session.hpp:166
client_session & causal_consistency(bool causal_consistency) noexcept
Sets the causal_consistency option.
Definition client_session.hpp:105
client_session()=default
Default initialization.
bool causal_consistency() const noexcept
Gets the value of the causal_consistency option.
Definition client_session.hpp:114
bool snapshot() const noexcept
Gets the value of the snapshot_reads option.
Definition client_session.hpp:140
bsoncxx::v_noabi::stdx::optional< transaction > const & default_transaction_opts() const
Gets the current default transaction options.
Definition client_session.hpp:190
client_session & snapshot_time(bsoncxx::v_noabi::types::b_timestamp snapshot_time)
Sets the snapshot time ("atClusterTime") to use for a snapshot session.
Definition client_session.hpp:158
Used by MongoDB transaction operations.
Definition transaction.hpp:43
The mongocxx v_noabi macro guard postlude header.
The mongocxx v_noabi macro guard prelude header.
v1::element::view to_v1(v_noabi::array::element const &v)
Convert to the bsoncxx::v1 equivalent of v.
Definition element.hpp:132
Declares entities representing options to use with various commands.
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.
A BSON replication timestamp value.
Definition types.hpp:601
Provides mongocxx::v_noabi::options::transaction.
Provides entities used to represent BSON types.
Provides mongocxx::v1::client_session.
Provides std::optional-related polyfills for library API usage.
Declares mongocxx::v_noabi::client_session.
Declares mongocxx::v_noabi::options::client.