change_stream.hpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 <memory>
  16. #include <bsoncxx/document/view.hpp>
  17. #include <bsoncxx/stdx/optional.hpp>
  18. #include <mongocxx/config/prelude.hpp>
  19. namespace mongocxx {
  20. MONGOCXX_INLINE_NAMESPACE_BEGIN
  21. class client;
  22. class collection;
  23. class database;
  24. class MONGOCXX_API change_stream {
  25. public:
  26. class MONGOCXX_API iterator;
  27. ///
  28. /// Move constructs a change_stream.
  29. ///
  30. change_stream(change_stream&& other) noexcept;
  31. ///
  32. /// Move assigns a change_stream.
  33. ///
  34. change_stream& operator=(change_stream&& other) noexcept;
  35. ///
  36. /// Destroys a change_stream.
  37. ///
  38. ~change_stream();
  39. ///
  40. /// A change_stream::iterator points to the beginning of any
  41. /// available notifications. Each call to begin() advances to the next
  42. /// available notification. The state of all iterators is tracked by the
  43. /// change_stream itself, so advancing one iterator advances all iterators.
  44. ///
  45. /// change_stream::begin() and the increment operators are blocking operations.
  46. /// They will not return until a notification is available, the max_await_time (from
  47. /// the options::change_stream) milliseconds have elapsed, or a server
  48. /// error is encountered.
  49. ///
  50. /// When change_stream.begin() == change_stream.end(), no notifications
  51. /// are available. Each call to change_stream.begin() checks again for
  52. /// newly-available notifications.
  53. ///
  54. /// @return
  55. /// The change_stream::iterator
  56. /// @exception
  57. /// Throws mongocxx::query_exception if the query failed.
  58. ///
  59. iterator begin() const;
  60. ///
  61. /// A change_stream::iterator indicating stream exhaustion, meaning that
  62. /// no notifications are available from the stream.
  63. ///
  64. /// @return
  65. /// The change_stream::iterator indicating exhaustion
  66. ///
  67. iterator end() const;
  68. ///
  69. /// Returns a resume token for this change stream.
  70. ///
  71. /// If the change stream has not been iterated, and either resume_after or
  72. /// start_after was specified in the options to this change stream, the
  73. /// specified value will be returned by this method. If neither resume_after or
  74. /// start_after was set on the options for this change stream, and it has
  75. /// not been iterated, this method will return no token.
  76. ///
  77. /// Once this change stream has been iterated, this method will return the
  78. /// resume token of the most recently returned document in the stream, or a
  79. /// postBatchResumeToken if the current batch of documents has been exhausted.
  80. ///
  81. /// @see https://docs.mongodb.com/manual/changeStreams/#change-stream-resume-token
  82. ///
  83. /// The returned document::view is valid for the lifetime of the stream and
  84. /// its data may be updated if the change stream is iterated after this function.
  85. /// The value may be copied to extend its lifetime or preserve the
  86. /// current resume token.
  87. ///
  88. /// @return
  89. /// The token.
  90. ///
  91. bsoncxx::stdx::optional<bsoncxx::document::view> get_resume_token() const;
  92. private:
  93. friend class client;
  94. friend class collection;
  95. friend class database;
  96. friend class change_stream::iterator;
  97. MONGOCXX_PRIVATE change_stream(void* change_stream_ptr);
  98. class MONGOCXX_PRIVATE impl;
  99. std::unique_ptr<impl> _impl;
  100. };
  101. class MONGOCXX_API change_stream::iterator {
  102. public:
  103. // Support input-iterator (caveat of post-increment returning void)
  104. using difference_type = std::int64_t;
  105. using value_type = const bsoncxx::document::view;
  106. using pointer = std::add_pointer<value_type>::type;
  107. using reference = std::add_lvalue_reference<value_type>::type;
  108. using iterator_category = std::input_iterator_tag;
  109. ///
  110. /// Default-construct an iterator.
  111. /// Default-constucted iterators can be compared (all default-constructed
  112. /// iterators are ==), assigned, and copied.
  113. ///
  114. iterator();
  115. ///
  116. /// Dereferences the view for the document currently being pointed to.
  117. ///
  118. const bsoncxx::document::view& operator*() const;
  119. ///
  120. /// Accesses a member of the dereferenced document currently being pointed to.
  121. ///
  122. const bsoncxx::document::view* operator->() const;
  123. ///
  124. /// Pre-increments the iterator to move to the next document.
  125. ///
  126. /// change_stream::begin() and increment operators are blocking operations.
  127. /// They will not return until a notification is available, the max_await_time (from
  128. /// the options::change_stream) miliseconds have elapsed, or a server
  129. /// error is encountered.
  130. ///
  131. /// @throws mongocxx::query_exception if the query failed
  132. ///
  133. iterator& operator++();
  134. ///
  135. /// Post-increments the iterator to move to the next document.
  136. ///
  137. /// change_stream::begin() and increment operators are blocking operations.
  138. /// They will not return until a notification is available, the max_await_time (from
  139. /// the options::change_stream) miliseconds have elapsed, or a server
  140. /// error is encountered.
  141. ///
  142. /// @throws mongocxx::query_exception if the query failed
  143. ///
  144. void operator++(int);
  145. private:
  146. friend class change_stream;
  147. enum class iter_type { k_tracking, k_default_constructed, k_end };
  148. MONGOCXX_PRIVATE explicit iterator(iter_type type, const change_stream* change_stream);
  149. ///
  150. /// @{
  151. ///
  152. /// Compare two iterators for (in)-equality. Iterators compare equal if
  153. /// they point to the same underlying change_stream or if both are exhausted.
  154. ///
  155. /// @relates iterator
  156. ///
  157. friend MONGOCXX_API bool MONGOCXX_CALL operator==(const change_stream::iterator&,
  158. const change_stream::iterator&) noexcept;
  159. friend MONGOCXX_API bool MONGOCXX_CALL operator!=(const change_stream::iterator&,
  160. const change_stream::iterator&) noexcept;
  161. ///
  162. /// @}
  163. ///
  164. MONGOCXX_PRIVATE bool is_exhausted() const;
  165. // iter_type==k_default_constructed is equivalent to _change_stream==nullptr
  166. iter_type _type;
  167. const change_stream* _change_stream;
  168. };
  169. MONGOCXX_INLINE_NAMESPACE_END
  170. } // namespace mongocxx
  171. #include <mongocxx/config/postlude.hpp>