Class: Monga::CallbackCursor

Inherits:
Cursor
  • Object
show all
Defined in:
lib/monga/cursor.rb

Constant Summary

Constants inherited from Cursor

Monga::Cursor::CLOSED_CURSOR, Monga::Cursor::CLOSE_TIMEOUT, Monga::Cursor::CURSORS

Instance Attribute Summary

Attributes inherited from Cursor

#cursor_id

Instance Method Summary collapse

Methods inherited from Cursor

batch_kill, #batch_size, create, #explain, #flag, #hint, #initialize, #kill, #limit, #mark_to_kill, #skip, #sort

Constructor Details

This class inherits a constructor from Monga::Cursor

Instance Method Details

#allObject



213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/monga/cursor.rb', line 213

def all
  documents = []
  each_batch do |err, batch, iter|
    if err
      block_given? ? yield(err) : raise(err)
    else
      if iter
        documents += batch
        iter.next
      else
        block_given? ? yield(nil, documents) : documents
      end
    end
  end
end

#each_batch(&blk) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/monga/cursor.rb', line 156

def each_batch(&blk)
  iter_more = true
  iterator = Proc.new do
    if iter_more
      next_batch do |err, batch, more|
        iter_more = more
        (more || batch || err) ? blk.call(err, batch, iterator) : blk.call
      end
    else
      # iteration stopped
      blk.call
    end
  end
  class << iterator
    alias :next :call
  end
  iterator.next
end

#each_doc(&blk) ⇒ Object Also known as: each_document



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/monga/cursor.rb', line 193

def each_doc(&blk)
  iter_more = true
  iterator = Proc.new do
    if iter_more
      next_doc do |err, doc, more|
        iter_more = more
        (more || doc || err) ? blk.call(err, doc, iterator) : blk.call
      end
    else
      # iteration stopped
      blk.call
    end
  end
  class << iterator
    alias :next :call
  end
  iterator.next
end

#firstObject



229
230
231
232
233
234
235
236
237
# File 'lib/monga/cursor.rb', line 229

def first
  limit(1).all do |err, resp|
    if err
      block_given? ? yield(err) : raise(err)
    else
      block_given? ? yield(nil, resp.first) : resp.first
    end
  end
end

#next_batchObject



146
147
148
149
150
151
152
153
154
# File 'lib/monga/cursor.rb', line 146

def next_batch
  get_more(get_batch_size) do |err, batch, more|
    if block_given?
      yield(err, batch, more)
    else
      err ? raise(err) : [batch, more]
    end
  end
end

#next_docObject Also known as: next_document



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/monga/cursor.rb', line 175

def next_doc
  if doc = @fetched_docs.shift
    block_given? ? yield(nil, doc, more?) : [doc, more?]
  else
    get_more(get_batch_size) do |err, batch, more|
      if err
        block_given? ? yield(err, nil, false) : raise(err)
      else
        @fetched_docs = batch
        doc = @fetched_docs.shift
        m = more || @fetched_docs.any?
        block_given? ? yield(err, doc, m) : [doc, m]
      end
    end
  end
end