MongoDB C++ Driver 4.2.0
Loading...
Searching...
No Matches
read_preference.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/read_preference-fwd.hpp> // IWYU pragma: export
18
19//
20
22
24
25#include <chrono>
26#include <cstdint>
27#include <memory> // IWYU pragma: keep: backward compatibility, to be removed.
28#include <string> // IWYU pragma: keep: backward compatibility, to be removed.
29#include <utility>
30
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/events/topology_description-fwd.hpp> // IWYU pragma: keep: backward compatibility, to be removed.
35#include <mongocxx/options/transaction-fwd.hpp> // IWYU pragma: keep: backward compatibility, to be removed.
36#include <mongocxx/search_index_view-fwd.hpp> // IWYU pragma: keep: backward compatibility, to be removed.
37#include <mongocxx/uri-fwd.hpp> // IWYU pragma: keep: backward compatibility, to be removed.
38
44
45#include <mongocxx/options/transaction.hpp> // IWYU pragma: keep: backward compatibility, to be removed.
46
48
49namespace mongocxx {
50namespace v_noabi {
51
60 private:
62
63 public:
102
106 read_preference() = default;
107
111 /* explicit(false) */ read_preference(v1::read_preference rp) : _rp{std::move(rp)} {}
112
121 explicit operator v1::read_preference() && {
122 return std::move(_rp);
123 }
124
128 explicit operator v1::read_preference() const& {
129 return _rp;
130 }
131
132 // @cond DOXYGEN_DISABLE
133 struct deprecated_tag {};
134 // @endcond
135
145
146 // @cond DOXYGEN_DISABLE
147 read_preference(read_mode mode, deprecated_tag) {
148 this->mode(mode);
149 }
150 // @endcond
151
167
168 // @cond DOXYGEN_DISABLE
170 this->mode(mode).tags(tags.view());
171 }
172 // @endcond
173
185 _rp.mode(static_cast<v1::read_preference::read_mode>(mode));
186 return *this;
187 }
188
194 read_mode mode() const {
195 return static_cast<read_mode>(_rp.mode());
196 }
197
212 _rp.tags(bsoncxx::v_noabi::to_v1(tag_set_list.view()));
213 return *this;
214 }
215
230 _rp.tags(bsoncxx::v_noabi::to_v1(tag_set_list.view()));
231 return *this;
232 }
233
244
245 auto const v = _rp.tags();
246
247 if (!v.empty()) {
248 ret.emplace(v);
249 }
250
251 return ret;
252 }
253
285
292 return _rp.max_staleness();
293 }
294
295 BSONCXX_PRIVATE_WARNINGS_PUSH();
296 BSONCXX_PRIVATE_WARNINGS_DISABLE(GNU("-Wdeprecated-declarations"));
297 BSONCXX_PRIVATE_WARNINGS_DISABLE(MSVC(4996));
298
323
333 if (auto const opt = _rp.hedge()) {
334 ret.emplace(*opt);
335 }
336 return ret;
337 }
338
339 BSONCXX_PRIVATE_WARNINGS_POP();
340
347 friend bool operator==(read_preference const& lhs, read_preference const& rhs) {
348 return (lhs.mode() == rhs.mode()) && (lhs.tags() == rhs.tags()) && (lhs.max_staleness() == rhs.max_staleness());
349 }
350
351 friend bool operator!=(read_preference const& lhs, read_preference const& rhs) {
352 return !(lhs == rhs);
353 }
354
356
357 class internal;
358};
359
364 return {std::move(v)};
365}
366
373
374} // namespace v_noabi
375} // namespace mongocxx
376
378
Provides bsoncxx::v_noabi::array::view_or_value.
For internal use only!
A polyfill for std::optional<T>.
Definition optional.hpp:799
View const & view() const
Get a View for the type.
Definition view_or_value.hpp:135
read_preference()=default
Constructs a new read_preference with read_mode set to k_primary.
Options related to a MongoDB Read Preference.
Definition read_preference.hpp:49
read_mode
The read preference mode.
Definition read_preference.hpp:62
Describes how MongoDB clients route read operations to the members of a replica set or sharded cluste...
Definition read_preference.hpp:59
read_preference(read_mode mode)
Constructs a new read_preference.
Definition read_preference.hpp:144
read_preference(v1::read_preference rp)
Construct with the mongocxx::v1 equivalent.
Definition read_preference.hpp:111
read_preference & tags(bsoncxx::v_noabi::array::view_or_value tag_set_list)
Sets or updates the tag set list for this read_preference.
Definition read_preference.hpp:229
read_preference(read_mode mode, bsoncxx::v_noabi::document::view_or_value tags)
Constructs a new read_preference with tags.
Definition read_preference.hpp:165
bsoncxx::v_noabi::stdx::optional< bsoncxx::v_noabi::document::view > tags() const
Returns the current tag set list for this read_preference.
Definition read_preference.hpp:242
read_preference & mode(read_mode mode)
Sets a new mode for this read_preference.
Definition read_preference.hpp:184
read_preference & max_staleness(std::chrono::seconds max_staleness)
Sets the max staleness setting for this read_preference. Secondary servers with an estimated lag grea...
bsoncxx::v_noabi::stdx::optional< std::chrono::seconds > max_staleness() const
Returns the current max staleness setting for this read_preference.
Definition read_preference.hpp:291
read_mode
Determines which members in a replica set are acceptable to read from.
Definition read_preference.hpp:76
@ k_nearest
Read from the node with the lowest latency irrespective of state.
Definition read_preference.hpp:100
@ k_primary
Only read from a primary node.
Definition read_preference.hpp:80
@ k_secondary_preferred
Prefer to read from secondary nodes.
Definition read_preference.hpp:95
@ k_secondary
Only read from secondary nodes.
Definition read_preference.hpp:90
@ k_primary_preferred
Prefer to read from a primary node.
Definition read_preference.hpp:85
friend bool operator!=(read_preference const &lhs, read_preference const &rhs)
Compares two read_preference objects for (in)-equality.
Definition read_preference.hpp:351
bsoncxx::v_noabi::stdx::optional< bsoncxx::v_noabi::document::view > const hedge() const
Gets the current hedge document to be used for the read preference.
Definition read_preference.hpp:331
friend bool operator==(read_preference const &lhs, read_preference const &rhs)
Compares two read_preference objects for (in)-equality.
Definition read_preference.hpp:347
read_preference & hedge(bsoncxx::v_noabi::document::view_or_value hedge)
Sets the hedge document to be used for the read preference. Sharded clusters running MongoDB 4....
Definition read_preference.hpp:319
read_mode mode() const
Returns the current read_mode for this read_preference.
Definition read_preference.hpp:194
read_preference & tags(bsoncxx::v_noabi::document::view_or_value tag_set_list)
Sets or updates the tag set list for this read_preference.
Definition read_preference.hpp:211
read_preference()=default
Constructs a new read_preference with read_mode set to k_primary.
Provides bsoncxx::v_noabi::document::view_or_value.
#define MONGOCXX_DEPRECATED
Declares the associated entity as deprecated.
Definition export.hpp:25
#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::view_or_value< view, value > view_or_value
Equivalent to v_noabi::view_or_value<v_noabi::array::view, v_noabi::array::value>.
Definition view_or_value.hpp:31
v_noabi::view_or_value< v_noabi::document::view, v_noabi::document::value > view_or_value
Equivalent to v_noabi::view_or_value<v_noabi::document::view, v_noabi::document::value>.
Definition view_or_value.hpp:30
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 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::search_index_view.
Declares mongocxx::v_noabi::options::transaction.
Provides mongocxx::v_noabi::options::transaction.
Provides mongocxx::v1::read_preference.
Provides bsoncxx::v_noabi::array::view.
Provides bsoncxx::v_noabi::document::view.
Provides std::optional-related polyfills for library API usage.
Declares mongocxx::v_noabi::client.
Declares mongocxx::v_noabi::collection.
Declares mongocxx::v_noabi::database.
Declares mongocxx::v_noabi::events::topology_description.
Declares mongocxx::v_noabi::read_preference.
Declares mongocxx::v_noabi::uri.