write_concern.hpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. #pragma once
  15. #include <chrono>
  16. #include <cstdint>
  17. #include <memory>
  18. #include <stdexcept>
  19. #include <bsoncxx/document/value.hpp>
  20. #include <bsoncxx/stdx/optional.hpp>
  21. #include <bsoncxx/stdx/string_view.hpp>
  22. #include <mongocxx/options/transaction.hpp>
  23. #include <mongocxx/stdx.hpp>
  24. #include <mongocxx/config/prelude.hpp>
  25. namespace mongocxx {
  26. MONGOCXX_INLINE_NAMESPACE_BEGIN
  27. class bulk_write;
  28. class client;
  29. class collection;
  30. class database;
  31. class uri;
  32. ///
  33. /// Class representing the server-side requirement for reporting the success of a write
  34. /// operation. The strength of the write concern setting determines the level of guarantees
  35. /// that you will receive from MongoDB regarding write durability.
  36. ///
  37. /// Weaker requirements that provide fewer guarantees report on success quickly while stronger
  38. /// requirements that provide greater guarantees will take longer (or potentially forever, if
  39. /// the write_concern's requirements are not satisfied and no timeout is set).
  40. ///
  41. /// MongoDB drivers allow for different levels of write concern to better address the specific
  42. /// needs of applications. Clients may adjust write concern to ensure that the most important
  43. /// operations persist successfully to an entire MongoDB deployment. However, for other less
  44. /// critical operations, clients can adjust the write concern to ensure better performance
  45. /// rather than persistence to the entire deployment.
  46. ///
  47. /// @see https://docs.mongodb.com/master/core/write-concern/
  48. ///
  49. class MONGOCXX_API write_concern {
  50. public:
  51. ///
  52. /// A class to represent the special case values for write_concern::nodes.
  53. /// @see https://docs.mongodb.com/master/reference/write-concern/#w-option
  54. ///
  55. enum class level { k_default, k_majority, k_tag, k_unacknowledged, k_acknowledged };
  56. ///
  57. /// Constructs a new write_concern.
  58. ///
  59. write_concern();
  60. ///
  61. /// Copy constructs a write_concern.
  62. ///
  63. write_concern(const write_concern&);
  64. ///
  65. /// Copy assigns a write_concern.
  66. ///
  67. write_concern& operator=(const write_concern&);
  68. ///
  69. /// Move constructs a write_concern.
  70. ///
  71. write_concern(write_concern&&) noexcept;
  72. ///
  73. /// Move assigns a write_concern.
  74. ///
  75. write_concern& operator=(write_concern&&) noexcept;
  76. ///
  77. /// Destroys a write_concern.
  78. ///
  79. ~write_concern();
  80. ///
  81. /// Sets the journal parameter for this write concern.
  82. ///
  83. /// @param journal
  84. /// If @c true confirms that the mongod instance has written the data to the on-disk journal
  85. /// before reporting a write operations was successful. This ensures that data is not lost if
  86. /// the mongod instance shuts down unexpectedly.
  87. ///
  88. void journal(bool journal);
  89. ///
  90. /// Sets the number of nodes that are required to acknowledge the write before the operation is
  91. /// considered successful. Write operations will block until they have been replicated to the
  92. /// specified number of servers in a replica set.
  93. ///
  94. /// @param confirm_from
  95. /// The number of replica set nodes that must acknowledge the write.
  96. ///
  97. /// @warning Setting the number of nodes to 0 disables write acknowledgment and all other
  98. /// write concern options.
  99. ///
  100. /// @warning Setting the number of nodes required to an amount greater than the number of
  101. /// available nodes will cause writes using this write concern to block forever if no timeout
  102. /// is set.
  103. ///
  104. void nodes(std::int32_t confirm_from);
  105. ///
  106. /// Sets the acknowledge level.
  107. /// @see https://docs.mongodb.com/master/reference/write-concern/#w-option
  108. ///
  109. /// @param confirm_level
  110. /// Either level::k_unacknowledged, level::k_acknowledged, level::k_default, or
  111. /// level::k_majority.
  112. ///
  113. /// @note
  114. /// the acknowledge level of level::k_tag is set automatically when a tag is set.
  115. ///
  116. /// @warning
  117. /// Setting this to level::k_unacknowledged disables write acknowledgment and all other
  118. /// write concern options.
  119. ///
  120. /// @exception
  121. /// Throws mongocxx::exception for setting a tag acknowledge level. Use tag() instead.
  122. ///
  123. void acknowledge_level(level confirm_level);
  124. ///
  125. /// Requires that a majority of the nodes in a replica set acknowledge a write operation before
  126. /// it is considered a success.
  127. ///
  128. /// @param timeout
  129. /// The amount of time to wait before the write operation times out if it cannot reach
  130. /// the majority of nodes in the replica set. If the value is zero, then no timeout is set.
  131. ///
  132. /// @throws mongocxx::logic_error for an invalid timeout value.
  133. ///
  134. void majority(std::chrono::milliseconds timeout);
  135. ///
  136. /// Sets the name representing the server-side getLastErrorMode entry containing the list of
  137. /// nodes that must acknowledge a write operation before it is considered a success.
  138. ///
  139. /// @note the acknowledge level of level::k_tag is set automatically when a tag is set.
  140. ///
  141. /// @param tag
  142. /// The string representing on of the "getLastErrorModes" in the replica set configuration.
  143. ///
  144. void tag(stdx::string_view tag);
  145. ///
  146. /// Sets an upper bound on the time a write concern can take to be satisfied. If the write
  147. /// concern cannot be satisfied within the timeout, the operation is considered a failure.
  148. ///
  149. /// @param timeout
  150. /// The timeout (in milliseconds) for this write concern. If the value is zero, then no
  151. /// timeout is set.
  152. ///
  153. /// @throws mongocxx::logic_error for an invalid timeout value.
  154. ///
  155. void timeout(std::chrono::milliseconds timeout);
  156. ///
  157. /// Gets the current status of the journal parameter.
  158. ///
  159. /// @return @c true if journal is required, @c false if not.
  160. ///
  161. bool journal() const;
  162. ///
  163. /// Gets the current number of nodes that this write_concern requires operations to reach.
  164. /// This value will be unset if the acknowledge_level is set to majority, default, or tag.
  165. ///
  166. /// This is unset by default.
  167. ///
  168. /// @see https://docs.mongodb.com/master/reference/write-concern/#w-option
  169. ///
  170. /// @return The number of required nodes.
  171. ///
  172. stdx::optional<std::int32_t> nodes() const;
  173. ///
  174. /// Gets the current acknowledgment level.
  175. ///
  176. /// @see https://docs.mongodb.com/master/reference/write-concern/#w-option
  177. ///
  178. /// @return The acknowledgment level.
  179. ///
  180. level acknowledge_level() const;
  181. ///
  182. /// Gets the current getLastErrorMode that is required by this write_concern.
  183. ///
  184. /// @return The current getLastErrorMode.
  185. ///
  186. stdx::optional<std::string> tag() const;
  187. ///
  188. /// Gets whether the majority of nodes is currently required by this write_concern.
  189. ///
  190. /// @return The current majority setting.
  191. ///
  192. bool majority() const;
  193. ///
  194. /// Gets the current timeout for this write_concern.
  195. ///
  196. /// @return Current timeout in milliseconds.
  197. ///
  198. std::chrono::milliseconds timeout() const;
  199. ///
  200. /// Gets whether this write_concern requires an acknowledged write.
  201. ///
  202. /// @return Whether this write concern requires an acknowledged write.
  203. ///
  204. bool is_acknowledged() const;
  205. ///
  206. /// Gets the document form of this write_concern.
  207. ///
  208. /// @return
  209. /// Document representation of this write_concern.
  210. ///
  211. bsoncxx::document::value to_document() const;
  212. private:
  213. friend bulk_write;
  214. friend client;
  215. friend collection;
  216. friend database;
  217. friend options::transaction;
  218. friend uri;
  219. ///
  220. /// @{
  221. ///
  222. /// Compares two write_concern objects for (in)-equality.
  223. ///
  224. /// @relates: write_concern
  225. ///
  226. friend MONGOCXX_API bool MONGOCXX_CALL operator==(const write_concern&, const write_concern&);
  227. friend MONGOCXX_API bool MONGOCXX_CALL operator!=(const write_concern&, const write_concern&);
  228. ///
  229. /// @}
  230. ///
  231. class MONGOCXX_PRIVATE impl;
  232. MONGOCXX_PRIVATE write_concern(std::unique_ptr<impl>&& implementation);
  233. std::unique_ptr<impl> _impl;
  234. };
  235. MONGOCXX_INLINE_NAMESPACE_END
  236. } // namespace mongocxx
  237. #include <mongocxx/config/postlude.hpp>