|
| 1 | +# Copyright (C) 2017 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 | +require 'mongo/collection/view/change_stream/retryable' |
| 16 | + |
| 17 | +module Mongo |
| 18 | + class Collection |
| 19 | + class View |
| 20 | + |
| 21 | + # Provides behaviour around a `$changeStream` pipeline stage in the |
| 22 | + # aggregation framework. Specifying this stage allows users to request that |
| 23 | + # notifications are sent for all changes to a particular collection or database. |
| 24 | + # |
| 25 | + # @note Only available in server versions 3.6 and higher. |
| 26 | + # @note ChangeStreams do not work properly with JRuby because of the issue documented |
| 27 | + # here: https://github.com/jruby/jruby/issues/4212 |
| 28 | + # Namely, JRuby eagerly evaluates #next on an Enumerator in a background green thread. |
| 29 | + # So calling #next on the change stream will cause getmores to be called in a loop in the background. |
| 30 | + # |
| 31 | + # |
| 32 | + # @since 2.5.0 |
| 33 | + class ChangeStream < Aggregation |
| 34 | + include Retryable |
| 35 | + |
| 36 | + # @return [ String ] The fullDocument option default value. |
| 37 | + # |
| 38 | + # @since 2.5.0 |
| 39 | + FULL_DOCUMENT_DEFAULT = 'default'.freeze |
| 40 | + |
| 41 | + # @return [ BSON::Document ] The change stream options. |
| 42 | + # |
| 43 | + # @since 2.5.0 |
| 44 | + attr_reader :options |
| 45 | + |
| 46 | + # Initialize the change stream for the provided collection view, pipeline |
| 47 | + # and options. |
| 48 | + # |
| 49 | + # @example Create the new change stream view. |
| 50 | + # ChangeStream.new(view, pipeline, options) |
| 51 | + # |
| 52 | + # @param [ Collection::View ] view The collection view. |
| 53 | + # @param [ Array<Hash> ] pipeline The pipeline of operators to filter the change notifications. |
| 54 | + # @param [ Hash ] opts The change stream options. |
| 55 | + # |
| 56 | + # @option options [ String ] :full_document Allowed values: ‘default’, ‘updateLookup’. Defaults to ‘default’. |
| 57 | + # When set to ‘updateLookup’, the change notification for partial updates will include both a delta |
| 58 | + # describing the changes to the document, as well as a copy of the entire document that was changed |
| 59 | + # from some time after the change occurred. |
| 60 | + # @option options [ BSON::Document, Hash ] :resume_after Specifies the logical starting point for the |
| 61 | + # new change stream. |
| 62 | + # @option options [ Integer ] :max_await_time_ms The maximum amount of time for the server to wait |
| 63 | + # on new documents to satisfy a change stream query. |
| 64 | + # @option options [ Integer ] :batch_size The number of documents to return per batch. |
| 65 | + # @option options [ BSON::Document, Hash ] :collation The collation to use. |
| 66 | + # |
| 67 | + # @since 2.5.0 |
| 68 | + def initialize(view, pipeline, options = {}) |
| 69 | + @view = view |
| 70 | + @change_stream_filters = pipeline && pipeline.dup |
| 71 | + @options = options && options.dup.freeze |
| 72 | + @resume_token = @options[:resume_after] |
| 73 | + read_with_one_retry { create_cursor! } |
| 74 | + end |
| 75 | + |
| 76 | + # Iterate through documents returned by the change stream. |
| 77 | + # |
| 78 | + # @example Iterate through the stream of documents. |
| 79 | + # stream.each do |document| |
| 80 | + # p document |
| 81 | + # end |
| 82 | + # |
| 83 | + # @return [ Enumerator ] The enumerator. |
| 84 | + # |
| 85 | + # @since 2.5.0 |
| 86 | + # |
| 87 | + # @yieldparam [ BSON::Document ] Each change stream document. |
| 88 | + def each |
| 89 | + raise StopIteration.new if closed? |
| 90 | + begin |
| 91 | + @cursor.each do |doc| |
| 92 | + cache_resume_token(doc) |
| 93 | + yield doc |
| 94 | + end if block_given? |
| 95 | + @cursor.to_enum |
| 96 | + rescue => e |
| 97 | + close |
| 98 | + if retryable?(e) |
| 99 | + create_cursor! |
| 100 | + retry |
| 101 | + end |
| 102 | + raise |
| 103 | + end |
| 104 | + end |
| 105 | + |
| 106 | + # Close the change stream. |
| 107 | + # |
| 108 | + # @example Close the change stream. |
| 109 | + # stream.close |
| 110 | + # |
| 111 | + # @return [ nil ] nil. |
| 112 | + # |
| 113 | + # @since 2.5.0 |
| 114 | + def close |
| 115 | + unless closed? |
| 116 | + begin; @cursor.send(:kill_cursors); rescue; end |
| 117 | + @cursor = nil |
| 118 | + end |
| 119 | + end |
| 120 | + |
| 121 | + # Is the change stream closed? |
| 122 | + # |
| 123 | + # @example Determine whether the change stream is closed. |
| 124 | + # stream.closed? |
| 125 | + # |
| 126 | + # @return [ true, false ] If the change stream is closed. |
| 127 | + # |
| 128 | + # @since 2.5.0 |
| 129 | + def closed? |
| 130 | + @cursor.nil? |
| 131 | + end |
| 132 | + |
| 133 | + private |
| 134 | + |
| 135 | + def cache_resume_token(doc) |
| 136 | + unless @resume_token = (doc[:_id] && doc[:_id].dup) |
| 137 | + raise Error::MissingResumeToken.new |
| 138 | + end |
| 139 | + end |
| 140 | + |
| 141 | + def create_cursor! |
| 142 | + server = server_selector.select_server(cluster, false) |
| 143 | + result = send_initial_query(server) |
| 144 | + @cursor = Cursor.new(view, result, server, disable_retry: true) |
| 145 | + end |
| 146 | + |
| 147 | + def pipeline |
| 148 | + change_doc = { fullDocument: ( @options[:full_document] || FULL_DOCUMENT_DEFAULT ) } |
| 149 | + change_doc[:resumeAfter] = @resume_token if @resume_token |
| 150 | + [{ '$changeStream' => change_doc }] + @change_stream_filters |
| 151 | + end |
| 152 | + |
| 153 | + def send_initial_query(server) |
| 154 | + initial_query_op.execute(server) |
| 155 | + end |
| 156 | + end |
| 157 | + end |
| 158 | + end |
| 159 | +end |
0 commit comments