MongoDB C++ Driver  mongocxx-3.1.0
read_preference.hpp
1 // Copyright 2014 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 <chrono>
18 #include <cstdint>
19 #include <memory>
20 #include <string>
21 
22 #include <bsoncxx/document/view_or_value.hpp>
23 #include <bsoncxx/stdx/optional.hpp>
24 #include <mongocxx/stdx.hpp>
25 
26 #include <mongocxx/config/prelude.hpp>
27 
28 namespace mongocxx {
29 MONGOCXX_INLINE_NAMESPACE_BEGIN
30 
31 class client;
32 class collection;
33 class database;
34 class uri;
35 
57 class MONGOCXX_API read_preference {
58  public:
70  enum class read_mode : std::uint8_t {
74  k_primary = 0x01,
75 
79  k_primary_preferred = 0x05,
80 
84  k_secondary = 0x02,
85 
89  k_secondary_preferred = 0x06,
90 
94  k_nearest = 0x0A
95  };
96 
103  explicit read_preference(read_mode mode = read_mode::k_primary);
104 
116 
121 
125  read_preference& operator=(const read_preference&);
126 
130  read_preference(read_preference&&) noexcept;
131 
135  read_preference& operator=(read_preference&&) noexcept;
136 
140  ~read_preference();
141 
148  void mode(read_mode mode);
149 
155  read_mode mode() const;
156 
165  void tags(bsoncxx::document::view_or_value tags);
166 
174  stdx::optional<bsoncxx::document::view> tags() const;
175 
202  void max_staleness(std::chrono::seconds max_staleness);
203 
209  stdx::optional<std::chrono::seconds> max_staleness() const;
210 
211  private:
212  friend client;
213  friend collection;
214  friend database;
215  friend uri;
216  friend MONGOCXX_API bool MONGOCXX_CALL
217  operator==(const read_preference&, const read_preference&);
218 
219  class MONGOCXX_PRIVATE impl;
220 
221  MONGOCXX_PRIVATE read_preference(std::unique_ptr<impl>&& implementation);
222 
223  std::unique_ptr<impl> _impl;
224 };
225 
226 MONGOCXX_INLINE_NAMESPACE_END
227 } // namespace mongocxx
228 
229 #include <mongocxx/config/postlude.hpp>
Class representing a MongoDB connection string URI.
Definition: uri.hpp:40
Definition: bulk_write.hpp:22
Definition: error_code.hpp:72
Class representing a client connection to MongoDB.
Definition: client.hpp:49
Class representing a MongoDB database.
Definition: database.hpp:44
Class representing a preference for how the driver routes read operations to members of a replica set...
Definition: read_preference.hpp:57
read_mode
Determines which members in a replica set are acceptable to read from.
Definition: read_preference.hpp:70
Definition: element.hpp:24
Class representing server side document groupings within a MongoDB database.
Definition: collection.hpp:74