Repository navigation
Expand file tree
/
Copy pathread_preference.cpp
More file actions
127 lines (97 loc) · 4.67 KB
/
Copy pathread_preference.cpp
File metadata and controls
127 lines (97 loc) · 4.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// Copyright 2014 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.
#include <mongocxx/read_preference.hpp>
#include <bsoncxx/stdx/make_unique.hpp>
#include <mongocxx/exception/error_code.hpp>
#include <mongocxx/exception/logic_error.hpp>
#include <mongocxx/private/conversions.hh>
#include <mongocxx/private/libbson.hh>
#include <mongocxx/private/libmongoc.hh>
#include <mongocxx/private/read_preference.hh>
#include <mongocxx/config/private/prelude.hh>
namespace mongocxx {
MONGOCXX_INLINE_NAMESPACE_BEGIN
read_preference::read_preference(read_preference&&) noexcept = default;
read_preference& read_preference::operator=(read_preference&&) noexcept = default;
read_preference::read_preference(const read_preference& other)
: _impl(stdx::make_unique<impl>(libmongoc::read_prefs_copy(other._impl->read_preference_t))) {}
read_preference& read_preference::operator=(const read_preference& other) {
_impl.reset(stdx::make_unique<impl>(libmongoc::read_prefs_copy(other._impl->read_preference_t))
.release());
return *this;
}
read_preference::read_preference(std::unique_ptr<impl>&& implementation) {
_impl.reset(implementation.release());
}
read_preference::read_preference()
: _impl(stdx::make_unique<impl>(libmongoc::read_prefs_new(
libmongoc::conversions::read_mode_t_from_read_mode(read_mode::k_primary)))) {}
read_preference::read_preference(read_mode mode) : read_preference(mode, deprecated_tag{}) {}
read_preference::read_preference(read_mode mode, deprecated_tag)
: _impl(stdx::make_unique<impl>(
libmongoc::read_prefs_new(libmongoc::conversions::read_mode_t_from_read_mode(mode)))) {}
read_preference::read_preference(read_mode mode, bsoncxx::document::view_or_value tags)
: read_preference(mode, std::move(tags), deprecated_tag{}) {}
read_preference::read_preference(read_mode mode,
bsoncxx::document::view_or_value tags,
deprecated_tag)
: read_preference(mode, deprecated_tag{}) {
read_preference::tags(std::move(tags));
}
read_preference::~read_preference() = default;
read_preference& read_preference::mode(read_mode mode) {
libmongoc::read_prefs_set_mode(_impl->read_preference_t,
libmongoc::conversions::read_mode_t_from_read_mode(mode));
return *this;
}
read_preference& read_preference::tags(bsoncxx::document::view_or_value tags) {
libbson::scoped_bson_t scoped_bson_tags(std::move(tags));
libmongoc::read_prefs_set_tags(_impl->read_preference_t, scoped_bson_tags.bson());
return *this;
}
read_preference::read_mode read_preference::mode() const {
return libmongoc::conversions::read_mode_from_read_mode_t(
libmongoc::read_prefs_get_mode(_impl->read_preference_t));
}
stdx::optional<bsoncxx::document::view> read_preference::tags() const {
const bson_t* bson_tags = libmongoc::read_prefs_get_tags(_impl->read_preference_t);
if (bson_count_keys(bson_tags))
return bsoncxx::document::view(bson_get_data(bson_tags), bson_tags->len);
return stdx::optional<bsoncxx::document::view>{};
}
read_preference& read_preference::max_staleness(std::chrono::seconds max_staleness) {
auto max_staleness_sec = max_staleness.count();
if (max_staleness_sec < -1 || max_staleness_sec == 0) {
throw logic_error{error_code::k_invalid_parameter};
}
libmongoc::read_prefs_set_max_staleness_seconds(_impl->read_preference_t, max_staleness_sec);
return *this;
}
stdx::optional<std::chrono::seconds> read_preference::max_staleness() const {
auto staleness = libmongoc::read_prefs_get_max_staleness_seconds(_impl->read_preference_t);
// libmongoc signals "disabled" with the value -1.
if (staleness == -1) {
return stdx::nullopt;
}
return std::chrono::seconds{staleness};
}
bool MONGOCXX_CALL operator==(const read_preference& lhs, const read_preference& rhs) {
return (lhs.mode() == rhs.mode()) && (lhs.tags() == rhs.tags()) &&
(lhs.max_staleness() == rhs.max_staleness());
}
bool MONGOCXX_CALL operator!=(const read_preference& lhs, const read_preference& rhs) {
return !(lhs == rhs);
}
MONGOCXX_INLINE_NAMESPACE_END
} // namespace mongocxx