Loading...
Searching...
No Matches
read_preference.hpp
Go to the documentation of this file.
// Copyright 2009-present MongoDB, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#pragma once
#include <mongocxx/read_preference-fwd.hpp> // IWYU pragma: export
//
#include <bsoncxx/v1/detail/macros.hpp>
#include <mongocxx/v1/read_preference.hpp>
#include <chrono>
#include <cstdint>
#include <memory> // IWYU pragma: keep: backward compatibility, to be removed.
#include <string> // IWYU pragma: keep: backward compatibility, to be removed.
#include <utility>
#include <mongocxx/client-fwd.hpp> // IWYU pragma: keep: backward compatibility, to be removed.
#include <mongocxx/collection-fwd.hpp> // IWYU pragma: keep: backward compatibility, to be removed.
#include <mongocxx/database-fwd.hpp> // IWYU pragma: keep: backward compatibility, to be removed.
#include <mongocxx/events/topology_description-fwd.hpp> // IWYU pragma: keep: backward compatibility, to be removed.
#include <mongocxx/options/transaction-fwd.hpp> // IWYU pragma: keep: backward compatibility, to be removed.
#include <mongocxx/search_index_view-fwd.hpp> // IWYU pragma: keep: backward compatibility, to be removed.
#include <mongocxx/uri-fwd.hpp> // IWYU pragma: keep: backward compatibility, to be removed.
#include <bsoncxx/array/view.hpp>
#include <bsoncxx/array/view_or_value.hpp>
#include <bsoncxx/document/view.hpp>
#include <bsoncxx/document/view_or_value.hpp>
#include <bsoncxx/stdx/optional.hpp>
#include <mongocxx/options/transaction.hpp> // IWYU pragma: keep: backward compatibility, to be removed.
#include <mongocxx/config/prelude.hpp>
namespace mongocxx {
namespace v_noabi {
class read_preference {
private:
v1::read_preference _rp;
public:
enum class read_mode : std::uint8_t {
k_primary,
k_primary_preferred,
k_secondary,
k_secondary_preferred,
k_nearest
};
read_preference() = default;
/* explicit(false) */ read_preference(v1::read_preference rp) : _rp{std::move(rp)} {}
explicit operator v1::read_preference() && {
return std::move(_rp);
}
explicit operator v1::read_preference() const& {
return _rp;
}
// @cond DOXYGEN_DISABLE
struct deprecated_tag {};
// @endcond
MONGOCXX_DEPRECATED read_preference(read_mode mode) : read_preference{mode, deprecated_tag{}} {}
// @cond DOXYGEN_DISABLE
read_preference(read_mode mode, deprecated_tag) {
this->mode(mode);
}
// @endcond
MONGOCXX_DEPRECATED read_preference(read_mode mode, bsoncxx::v_noabi::document::view_or_value tags)
: read_preference{mode, tags, deprecated_tag{}} {}
// @cond DOXYGEN_DISABLE
read_preference(read_mode mode, bsoncxx::v_noabi::document::view_or_value tags, deprecated_tag) {
this->mode(mode).tags(tags.view());
}
// @endcond
read_preference& mode(read_mode mode) {
_rp.mode(static_cast<v1::read_preference::read_mode>(mode));
return *this;
}
read_mode mode() const {
return static_cast<read_mode>(_rp.mode());
}
read_preference& tags(bsoncxx::v_noabi::document::view_or_value tag_set_list) {
_rp.tags(bsoncxx::v_noabi::to_v1(tag_set_list.view()));
return *this;
}
read_preference& tags(bsoncxx::v_noabi::array::view_or_value tag_set_list) {
_rp.tags(bsoncxx::v_noabi::to_v1(tag_set_list.view()));
return *this;
}
bsoncxx::v_noabi::stdx::optional<bsoncxx::v_noabi::document::view> tags() const {
bsoncxx::v_noabi::stdx::optional<bsoncxx::v_noabi::document::view> ret;
auto const v = _rp.tags();
if (!v.empty()) {
ret.emplace(v);
}
return ret;
}
MONGOCXX_ABI_EXPORT_CDECL_UNSTABLE(read_preference&) max_staleness(std::chrono::seconds max_staleness);
bsoncxx::v_noabi::stdx::optional<std::chrono::seconds> max_staleness() const {
return _rp.max_staleness();
}
BSONCXX_PRIVATE_WARNINGS_PUSH();
BSONCXX_PRIVATE_WARNINGS_DISABLE(GNU("-Wdeprecated-declarations"));
BSONCXX_PRIVATE_WARNINGS_DISABLE(MSVC(4996));
MONGOCXX_DEPRECATED read_preference& hedge(bsoncxx::v_noabi::document::view_or_value hedge) {
_rp.hedge(bsoncxx::v_noabi::to_v1(hedge.view()));
return *this;
}
MONGOCXX_DEPRECATED bsoncxx::v_noabi::stdx::optional<bsoncxx::v_noabi::document::view> const hedge() const {
bsoncxx::v_noabi::stdx::optional<bsoncxx::v_noabi::document::view> ret;
if (auto const opt = _rp.hedge()) {
ret.emplace(*opt);
}
return ret;
}
BSONCXX_PRIVATE_WARNINGS_POP();
friend bool operator==(read_preference const& lhs, read_preference const& rhs) {
return (lhs.mode() == rhs.mode()) && (lhs.tags() == rhs.tags()) && (lhs.max_staleness() == rhs.max_staleness());
}
friend bool operator!=(read_preference const& lhs, read_preference const& rhs) {
return !(lhs == rhs);
}
class internal;
};
inline v_noabi::read_preference from_v1(v1::read_preference v) {
return {std::move(v)};
}
inline v1::read_preference to_v1(v_noabi::read_preference v) {
return v1::read_preference{std::move(v)};
}
} // namespace v_noabi
} // namespace mongocxx
#include <mongocxx/config/postlude.hpp>
view_or_value.hpp
Provides bsoncxx::v_noabi::array::view_or_value.
macros.hpp
For internal use only!
bsoncxx::v1::stdx::optional
A polyfill for std::optional<T>.
Definition optional.hpp:799
bsoncxx::v_noabi::view_or_value::view
View const & view() const
Get a View for the type.
Definition view_or_value.hpp:135
mongocxx::read_preference::read_preference
read_preference()=default
Constructs a new read_preference with read_mode set to k_primary.
mongocxx::v1::read_preference
Options related to a MongoDB Read Preference.
Definition read_preference.hpp:47
mongocxx::v1::read_preference::read_mode
read_mode
The read preference mode.
Definition read_preference.hpp:58
mongocxx::v_noabi::read_preference
Describes how MongoDB clients route read operations to the members of a replica set or sharded cluste...
Definition read_preference.hpp:59
mongocxx::v_noabi::read_preference::read_preference
read_preference(read_mode mode)
Constructs a new read_preference.
Definition read_preference.hpp:144
mongocxx::v_noabi::read_preference::read_preference
read_preference(v1::read_preference rp)
Construct with the mongocxx::v1 equivalent.
Definition read_preference.hpp:111
mongocxx::v_noabi::read_preference::tags
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
mongocxx::v_noabi::read_preference::read_preference
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
mongocxx::v_noabi::read_preference::tags
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
mongocxx::v_noabi::read_preference::mode
read_preference & mode(read_mode mode)
Sets a new mode for this read_preference.
Definition read_preference.hpp:184
mongocxx::v_noabi::read_preference::max_staleness
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...
mongocxx::v_noabi::read_preference::max_staleness
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
mongocxx::v_noabi::read_preference::read_mode
read_mode
Determines which members in a replica set are acceptable to read from.
Definition read_preference.hpp:76
mongocxx::v_noabi::read_preference::read_mode::k_nearest
@ k_nearest
Read from the node with the lowest latency irrespective of state.
Definition read_preference.hpp:100
mongocxx::v_noabi::read_preference::read_mode::k_primary
@ k_primary
Only read from a primary node.
Definition read_preference.hpp:80
mongocxx::v_noabi::read_preference::read_mode::k_secondary_preferred
@ k_secondary_preferred
Prefer to read from secondary nodes.
Definition read_preference.hpp:95
mongocxx::v_noabi::read_preference::read_mode::k_secondary
@ k_secondary
Only read from secondary nodes.
Definition read_preference.hpp:90
mongocxx::v_noabi::read_preference::read_mode::k_primary_preferred
@ k_primary_preferred
Prefer to read from a primary node.
Definition read_preference.hpp:85
mongocxx::v_noabi::read_preference::operator!=
friend bool operator!=(read_preference const &lhs, read_preference const &rhs)
Compares two read_preference objects for (in)-equality.
Definition read_preference.hpp:351
mongocxx::v_noabi::read_preference::hedge
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
mongocxx::v_noabi::read_preference::operator==
friend bool operator==(read_preference const &lhs, read_preference const &rhs)
Compares two read_preference objects for (in)-equality.
Definition read_preference.hpp:347
mongocxx::v_noabi::read_preference::hedge
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
mongocxx::v_noabi::read_preference::mode
read_mode mode() const
Returns the current read_mode for this read_preference.
Definition read_preference.hpp:194
mongocxx::v_noabi::read_preference::tags
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
mongocxx::v_noabi::read_preference::read_preference
read_preference()=default
Constructs a new read_preference with read_mode set to k_primary.
view_or_value.hpp
Provides bsoncxx::v_noabi::document::view_or_value.
MONGOCXX_DEPRECATED
#define MONGOCXX_DEPRECATED
Declares the associated entity as deprecated.
Definition export.hpp:25
MONGOCXX_ABI_EXPORT_CDECL_UNSTABLE
#define MONGOCXX_ABI_EXPORT_CDECL_UNSTABLE(...)
Equivalent to MONGOCXX_ABI_EXPORT_UNSTABLE with MONGOCXX_ABI_CDECL.
Definition config.hpp:28
postlude.hpp
The mongocxx v_noabi macro guard postlude header.
prelude.hpp
The mongocxx v_noabi macro guard prelude header.
bsoncxx::v_noabi::array::view_or_value
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
bsoncxx::v_noabi::document::view_or_value
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
bsoncxx::v_noabi::to_v1
v1::element::view to_v1(v_noabi::array::element const &v)
Convert to the bsoncxx::v1 equivalent of v.
Definition element.hpp:132
mongocxx::v_noabi
Declares entities whose ABI stability is NOT guaranteed.
mongocxx::v_noabi::to_v1
v1::bulk_write to_v1(v_noabi::bulk_write v)
Convert to the mongocxx::v1 equivalent of v.
Definition bulk_write.hpp:162
mongocxx::v_noabi::from_v1
v_noabi::bulk_write from_v1(v1::bulk_write v)
Convert to the mongocxx::v_noabi equivalent of v.
Definition bulk_write.hpp:155
mongocxx
The top-level namespace within which all mongocxx library entities are declared.
search_index_view-fwd.hpp
Declares mongocxx::v_noabi::search_index_view.
transaction-fwd.hpp
Declares mongocxx::v_noabi::options::transaction.
transaction.hpp
Provides mongocxx::v_noabi::options::transaction.
read_preference.hpp
Provides mongocxx::v1::read_preference.
view.hpp
Provides bsoncxx::v_noabi::array::view.
view.hpp
Provides bsoncxx::v_noabi::document::view.
optional.hpp
Provides std::optional-related polyfills for library API usage.
client-fwd.hpp
Declares mongocxx::v_noabi::client.
collection-fwd.hpp
Declares mongocxx::v_noabi::collection.
database-fwd.hpp
Declares mongocxx::v_noabi::database.
topology_description-fwd.hpp
Declares mongocxx::v_noabi::events::topology_description.
read_preference-fwd.hpp
Declares mongocxx::v_noabi::read_preference.
uri-fwd.hpp
Declares mongocxx::v_noabi::uri.