change_stream.hpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. // Copyright 2018-present 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 <bsoncxx/document/view_or_value.hpp>
  17. #include <bsoncxx/string/view_or_value.hpp>
  18. #include <bsoncxx/types.hpp>
  19. #include <mongocxx/stdx.hpp>
  20. #include <mongocxx/config/prelude.hpp>
  21. namespace mongocxx {
  22. MONGOCXX_INLINE_NAMESPACE_BEGIN
  23. class client;
  24. class collection;
  25. class database;
  26. namespace options {
  27. class MONGOCXX_API change_stream {
  28. public:
  29. change_stream();
  30. ///
  31. /// Sets the fullDocument stage for the $changeStream.
  32. ///
  33. /// The allowed values are: ‘default’, ‘updateLookup’.
  34. /// If none set, defaults to ‘default’.
  35. ///
  36. /// When set to ‘updateLookup’, the change stream will include both a delta describing the
  37. /// changes to
  38. /// the document, as well as a copy of the entire document that was changed from some time after
  39. /// the change occurred. This will be stored in the "fullDocument" field of the notification.
  40. ///
  41. /// @param full_doc
  42. /// The fullDocument setting to use on this stream.
  43. ///
  44. /// @return
  45. /// A reference to the object on which this member function is being called. This facilitates
  46. /// method chaining.
  47. ///
  48. change_stream& full_document(bsoncxx::string::view_or_value full_doc);
  49. ///
  50. /// Gets the current fullDocument setting.
  51. ///
  52. /// @return
  53. /// The current fullDocument setting.
  54. ///
  55. const bsoncxx::stdx::optional<bsoncxx::string::view_or_value>& full_document() const;
  56. ///
  57. /// Sets the number of documents to return per batch.
  58. ///
  59. /// @param batch_size
  60. /// The size of the batches to request.
  61. ///
  62. /// @return
  63. /// A reference to the object on which this member function is being called. This facilitates
  64. /// method chaining.
  65. ///
  66. change_stream& batch_size(std::int32_t batch_size);
  67. ///
  68. /// The current batch size setting.
  69. ///
  70. /// @return
  71. /// The current batch size.
  72. ///
  73. const stdx::optional<std::int32_t>& batch_size() const;
  74. ///
  75. /// Specifies the logical starting point for the new change stream.
  76. ///
  77. /// The value returned by calling change_stream::get_resume_token can be used here.
  78. ///
  79. /// start_after, resume_after, and start_at_operation_time are mutually exclusive options.
  80. /// Setting more than one of these will result in a server error.
  81. ///
  82. /// @param resume_after
  83. /// The resumeToken to use when starting the change stream.
  84. ///
  85. /// @return
  86. /// A reference to the object on which this member function is being called. This facilitates
  87. /// method chaining.
  88. ///
  89. change_stream& resume_after(bsoncxx::document::view_or_value resume_after);
  90. ///
  91. /// Retrieves the current resumeToken for this change stream.
  92. ///
  93. /// @return
  94. /// The current resumeToken.
  95. ///
  96. const stdx::optional<bsoncxx::document::view_or_value>& resume_after() const;
  97. ///
  98. /// Specifies the logical starting point of the new change stream. The new stream will
  99. /// return the first notification after the given token.
  100. ///
  101. /// The value returned by calling change_stream::get_resume_token can be used here.
  102. ///
  103. /// Unlike resumeAfter, this can resume notifications after an "invalidate" event.
  104. ///
  105. /// start_after, resume_after, and start_at_operation_time are mutually exclusive options.
  106. /// Setting more than one of these will result in a server error.
  107. ///
  108. /// @param token
  109. /// The token representing the logical starting point of the change stream.
  110. ///
  111. /// @return
  112. /// A reference to the object on which this function is being called.
  113. ///
  114. change_stream& start_after(bsoncxx::document::view_or_value token);
  115. ///
  116. /// Retrieves the current startAfter token for this change stream.
  117. ///
  118. /// @return
  119. /// The current startAfter token.
  120. ///
  121. const stdx::optional<bsoncxx::document::view_or_value>& start_after() const;
  122. ///
  123. /// Sets the collation for this operation.
  124. ///
  125. /// @param collation
  126. /// The new collation.
  127. ///
  128. /// @return
  129. /// A reference to the object on which this member function is being called. This facilitates
  130. /// method chaining.
  131. ///
  132. change_stream& collation(bsoncxx::document::view_or_value collation);
  133. ///
  134. /// Retrieves the current collation for this operation.
  135. ///
  136. /// @return
  137. /// The current collation.
  138. ///
  139. const stdx::optional<bsoncxx::document::view_or_value>& collation() const;
  140. ///
  141. /// Sets the maximum amount of time for for the server to wait on new documents to satisfy a
  142. /// change stream query.
  143. ///
  144. /// @param max_time
  145. /// The max amount of time (in milliseconds) for the server to wait on new documents.
  146. ///
  147. /// @return
  148. /// A reference to the object on which this member function is being called. This facilitates
  149. /// method chaining.
  150. ///
  151. change_stream& max_await_time(std::chrono::milliseconds max_time);
  152. ///
  153. /// The current max_time setting.
  154. ///
  155. /// @return
  156. /// The current max time (in milliseconds).
  157. ///
  158. const stdx::optional<std::chrono::milliseconds>& max_await_time() const;
  159. ///
  160. /// Specifies the logical starting point for the new change stream. Changes are returned at or
  161. /// after the specified operation time.
  162. ///
  163. /// start_after, resume_after, and start_at_operation_time are mutually exclusive options.
  164. /// Setting more than one of these will result in a server error.
  165. ///
  166. /// @param timestamp
  167. /// The starting operation time.
  168. ///
  169. /// @return
  170. /// A reference to the object on which this member function is being called. This facilitates
  171. /// method chaining.
  172. ///
  173. change_stream& start_at_operation_time(bsoncxx::types::b_timestamp timestamp);
  174. private:
  175. friend class ::mongocxx::client;
  176. friend class ::mongocxx::collection;
  177. friend class ::mongocxx::database;
  178. bsoncxx::document::value as_bson() const;
  179. stdx::optional<bsoncxx::string::view_or_value> _full_document;
  180. stdx::optional<std::int32_t> _batch_size;
  181. stdx::optional<bsoncxx::document::view_or_value> _collation;
  182. stdx::optional<bsoncxx::document::view_or_value> _resume_after;
  183. stdx::optional<bsoncxx::document::view_or_value> _start_after;
  184. stdx::optional<std::chrono::milliseconds> _max_await_time;
  185. // _start_at_operation_time is not wrapped in a stdx::optional because of a longstanding bug in
  186. // the MNMLSTC polyfill that has been fixed on master, but not in the latest release:
  187. // https://github.com/mnmlstc/core/pull/23
  188. bsoncxx::types::b_timestamp _start_at_operation_time;
  189. bool _start_at_operation_time_set = false;
  190. };
  191. } // namespace options
  192. MONGOCXX_INLINE_NAMESPACE_END
  193. } // namespace mongocxx
  194. #include <mongocxx/config/postlude.hpp>