read_concern.hpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. // Copyright 2015 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 <memory>
  16. #include <bsoncxx/document/value.hpp>
  17. #include <bsoncxx/stdx/optional.hpp>
  18. #include <bsoncxx/stdx/string_view.hpp>
  19. #include <mongocxx/options/transaction.hpp>
  20. #include <mongocxx/stdx.hpp>
  21. #include <mongocxx/config/prelude.hpp>
  22. namespace mongocxx {
  23. MONGOCXX_INLINE_NAMESPACE_BEGIN
  24. class client;
  25. class collection;
  26. class database;
  27. class uri;
  28. ///
  29. /// A class to represent the read concern. Read concern can be set at the client, database, or
  30. /// collection level. The read concern can also be provided via connection string, and will be
  31. /// parsed and set on the client constructed for the URI.
  32. ///
  33. /// For the WiredTiger storage engine, MongoDB 3.2 introduced the readConcern option for replica
  34. /// sets and replica set shards. The readConcern option allows clients to choose a level of
  35. /// isolation for their reads. You can specify a readConcern of "majority" to read data that has
  36. /// been written to a majority of nodes and thus cannot be rolled back. By default, MongoDB uses a
  37. /// readConcern of "local" which does not guarantee that the read data would not be rolled back.
  38. ///
  39. /// MongoDB 3.4 introduces a read concern level of "linearizable" to read data that has been written
  40. /// to a majority of nodes (i.e. cannot be rolled back) @b and is not stale. Linearizable read
  41. /// concern is available for all MongoDB supported storage engines and applies to read operations on
  42. /// a single document. Note that writes must be made with majority write concern in order for reads
  43. /// to be linearizable.
  44. ///
  45. /// @see https://docs.mongodb.com/master/reference/read-concern/
  46. ///
  47. class MONGOCXX_API read_concern {
  48. public:
  49. ///
  50. /// A class to represent the read concern level.
  51. ///
  52. /// @see https://docs.mongodb.com/master/reference/read-concern/#read-concern-levels
  53. ///
  54. enum class level {
  55. k_local,
  56. k_majority,
  57. k_linearizable,
  58. k_server_default,
  59. k_unknown,
  60. k_available,
  61. k_snapshot
  62. };
  63. ///
  64. /// Constructs a new read_concern with default acknowledge_level of k_server_default.
  65. ///
  66. /// The k_server_default acknowledge level has an empty acknowledge_string. Queries that
  67. /// run with this read_concern will use the server's default read_concern instead of
  68. /// specifying one.
  69. ///
  70. read_concern();
  71. ///
  72. /// Copy constructs a read_concern.
  73. ///
  74. read_concern(const read_concern&);
  75. ///
  76. /// Copy assigns a read_concern.
  77. ///
  78. read_concern& operator=(const read_concern&);
  79. ///
  80. /// Move constructs a read_concern.
  81. ///
  82. read_concern(read_concern&&) noexcept;
  83. ///
  84. /// Move assigns a read_concern.
  85. ///
  86. read_concern& operator=(read_concern&&) noexcept;
  87. ///
  88. /// Destroys a read_concern.
  89. ///
  90. ~read_concern();
  91. ///
  92. /// Sets the read concern level.
  93. ///
  94. /// @param rc_level
  95. /// Either k_local, k_majority, k_linearizable, or k_server_default.
  96. ///
  97. /// @throws
  98. /// mongocxx::exception if rc_level is not k_local, k_majority, k_linearizable, or
  99. /// k_server_default.
  100. ///
  101. void acknowledge_level(level rc_level);
  102. ///
  103. /// Gets the current read concern level.
  104. ///
  105. /// If this was set with acknowledge_string to anything other than "local", "majority",
  106. /// "linearizable", or an empty string, this will return k_unknown.
  107. ///
  108. /// @return The read concern level.
  109. ///
  110. level acknowledge_level() const;
  111. ///
  112. /// Sets the read concern string. Any valid read concern string (e.g. "local",
  113. /// "majority", "linearizable", "") may be passed in. For forward-compatibility
  114. /// with read concern levels introduced in the future, no validation is performed on
  115. /// this string.
  116. ///
  117. /// @param rc_string
  118. /// The read concern string.
  119. ///
  120. void acknowledge_string(stdx::string_view rc_string);
  121. ///
  122. /// Gets the current read concern string.
  123. ///
  124. /// If the read concern level was set with acknowledge_level, this will return either "local",
  125. /// "majority", "linearizable", or an empty string for k_server_default.
  126. ///
  127. /// @return The read concern string.
  128. ///
  129. stdx::string_view acknowledge_string() const;
  130. ///
  131. /// Gets the document form of this read_concern.
  132. ///
  133. /// @return
  134. /// Document representation of this read_concern.
  135. ///
  136. bsoncxx::document::value to_document() const;
  137. private:
  138. friend client;
  139. friend collection;
  140. friend database;
  141. friend options::transaction;
  142. friend uri;
  143. ///
  144. /// @{
  145. ///
  146. /// Compares two read_concern objects for (in)-equality.
  147. ///
  148. /// @relates: read_concern
  149. ///
  150. friend MONGOCXX_API bool MONGOCXX_CALL operator==(const read_concern&, const read_concern&);
  151. friend MONGOCXX_API bool MONGOCXX_CALL operator!=(const read_concern&, const read_concern&);
  152. ///
  153. /// @}
  154. ///
  155. class MONGOCXX_PRIVATE impl;
  156. MONGOCXX_PRIVATE read_concern(std::unique_ptr<impl>&& implementation);
  157. std::unique_ptr<impl> _impl;
  158. };
  159. MONGOCXX_INLINE_NAMESPACE_END
  160. } // namespace mongocxx
  161. #include <mongocxx/config/postlude.hpp>