Class: Mongo::Cursor

Inherits:
Object show all
Includes:
JavaImpl::Utils
Defined in:
lib/jmongo/cursor.rb

Constant Summary collapse

NEXT_DOCUMENT_TIMEOUT =
0.125

Constants included from JavaImpl::Utils

JavaImpl::Utils::SortingHash

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from JavaImpl::Utils

#from_dbobject, #prep_fields, #prep_hint, #prep_id, #prep_sort, #raise_not_implemented, #sort_value, #system_name?, #to_dbobject, #trap_raise, #validate_name

Constructor Details

#initialize(collection, options = {}) ⇒ Cursor

Returns a new instance of Cursor.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/jmongo/cursor.rb', line 27

def initialize(collection, options={})
  @collection = collection
  @j_collection = collection.j_collection
  @selector   = convert_selector_for_query(options[:selector])
  @fields     = convert_fields_for_query(options[:fields])
  @admin      = options.fetch(:admin, false)
  @order      = nil
  @batch_size = Mongo::DEFAULT_BATCH_SIZE
  @skip       = 0
  @limit      = 0
  _skip options[:skip]
  _limit options[:limit]
  _sort options[:order]
  _batch_size options[:batch_size]
  _hint options[:hint]
  @snapshot   = options[:snapshot]
  @explain    = options[:explain]
  @socket     = options[:socket]
  @timeout    = options.fetch(:timeout, true)
  @transformer = options[:transformer]
  @tailable   = options.fetch(:tailable, false)
  @do_tailable_timeout = false

  if @tailable
    @await_data  = options.fetch(:await_data, true)
    @next_timeout = NEXT_DOCUMENT_TIMEOUT
    @is_poison_function = nil
    @poison_doc = default_poison_doc
    @do_tailable_timeout = true
    case @await_data
    when Hash
      @poison_doc = @await_data.fetch(:poison_doc, default_poison_doc)
      @is_poison_function = @await_data[:is_poison_function]
      @next_timeout = @await_data.fetch(:next_timeout, NEXT_DOCUMENT_TIMEOUT).to_f
    when Numeric
      @next_timeout = @await_data.to_f
    end
    @timeout_thread = TimeoutThread.new(@collection, @poison_doc, @next_timeout)
  end

  @full_collection_name = "#{@collection.db.name}.#{@collection.name}"

  spawn_cursor
end

Instance Attribute Details

#collectionObject (readonly)

Returns the value of attribute collection.



22
23
24
# File 'lib/jmongo/cursor.rb', line 22

def collection
  @collection
end

#fieldsObject (readonly)

Returns the value of attribute fields.



22
23
24
# File 'lib/jmongo/cursor.rb', line 22

def fields
  @fields
end

#full_collection_nameObject (readonly)

Returns the value of attribute full_collection_name.



22
23
24
# File 'lib/jmongo/cursor.rb', line 22

def full_collection_name
  @full_collection_name
end

#hintObject (readonly)

Returns the value of attribute hint.



22
23
24
# File 'lib/jmongo/cursor.rb', line 22

def hint
  @hint
end

#j_cursorObject (readonly)

Returns the value of attribute j_cursor.



22
23
24
# File 'lib/jmongo/cursor.rb', line 22

def j_cursor
  @j_cursor
end

#optionsObject (readonly)

Returns the value of attribute options.



22
23
24
# File 'lib/jmongo/cursor.rb', line 22

def options
  @options
end

#orderObject (readonly)

Returns the value of attribute order.



22
23
24
# File 'lib/jmongo/cursor.rb', line 22

def order
  @order
end

#selectorObject (readonly)

Returns the value of attribute selector.



22
23
24
# File 'lib/jmongo/cursor.rb', line 22

def selector
  @selector
end

#snapshotObject (readonly)

Returns the value of attribute snapshot.



22
23
24
# File 'lib/jmongo/cursor.rb', line 22

def snapshot
  @snapshot
end

#timeoutObject (readonly)

Returns the value of attribute timeout.



22
23
24
# File 'lib/jmongo/cursor.rb', line 22

def timeout
  @timeout
end

#transformerObject (readonly)

Returns the value of attribute transformer.



22
23
24
# File 'lib/jmongo/cursor.rb', line 22

def transformer
  @transformer
end

Instance Method Details

#_hint(hint = nil) ⇒ Object



159
160
161
162
# File 'lib/jmongo/cursor.rb', line 159

def _hint(hint = nil)
  return if hint.nil?
  @hint = to_dbobject(hint)
end

#add_option(opt) ⇒ Object



96
97
98
99
100
# File 'lib/jmongo/cursor.rb', line 96

def add_option(opt)
  raise_invalid_op if @j_cursor.num_seen != 0
  @j_cursor.addOption(opt)
  options
end

#alive?Boolean

Returns:

  • (Boolean)


92
93
94
# File 'lib/jmongo/cursor.rb', line 92

def alive?
  cursor_id != 0
end

#batch_size(size = nil) ⇒ Object



171
172
173
174
175
# File 'lib/jmongo/cursor.rb', line 171

def batch_size(size=nil)
  _batch_size(size)
  @j_cursor = @j_cursor.batchSize(@batch_size) if @batch_size
  self
end

#closeObject



77
78
79
80
81
82
# File 'lib/jmongo/cursor.rb', line 77

def close
  if @j_cursor.num_seen == 0 && !@tailable
    @j_cursor.next rescue nil
  end
  @j_cursor.close
end

#closed?Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/jmongo/cursor.rb', line 88

def closed?
  cursor_id == 0
end

#count(skip_and_limit = false) ⇒ Object



225
226
227
228
229
230
231
232
233
# File 'lib/jmongo/cursor.rb', line 225

def count(skip_and_limit = false)
  wrap_invalid_op do
    if skip_and_limit && @skip && @limit
      @j_cursor.size
    else
      @j_cursor.count
    end
  end
end

#current_documentObject



118
119
120
# File 'lib/jmongo/cursor.rb', line 118

def current_document
  _xform(from_dbobject(@j_cursor.curr))
end

#cursor_idObject



84
85
86
# File 'lib/jmongo/cursor.rb', line 84

def cursor_id
  @j_cursor.get_cursor_id
end

#done_sizeObject



261
262
263
# File 'lib/jmongo/cursor.rb', line 261

def done_size
  @j_cursor.num_seen
end

#eachObject

iterate directly from the mongo db



153
154
155
156
157
# File 'lib/jmongo/cursor.rb', line 153

def each
  while has_next?
    yield next_document
  end
end

#explainObject



235
236
237
# File 'lib/jmongo/cursor.rb', line 235

def explain
  from_dbobject @j_cursor.explain
end

#has_next?Boolean

Returns:

  • (Boolean)


144
145
146
147
148
149
150
# File 'lib/jmongo/cursor.rb', line 144

def has_next?
  if @tailable
    true
  else
    @j_cursor.has_next?
  end
end

#limit(number_to_return = nil) ⇒ Object



184
185
186
187
188
189
190
# File 'lib/jmongo/cursor.rb', line 184

def limit(number_to_return=nil)
  _limit(number_to_return)
  wrap_invalid_op do
    @j_cursor = @j_cursor.limit(@limit)
  end
  self
end

#map(&block) ⇒ Object



239
240
241
242
243
244
245
246
# File 'lib/jmongo/cursor.rb', line 239

def map(&block)
  ret = []
  rewind! unless has_next?
  while has_next?
    ret << block.call(__next)
  end
  ret
end

#next_documentObject Also known as: next



122
123
124
125
126
127
128
129
130
131
132
# File 'lib/jmongo/cursor.rb', line 122

def next_document
  doc = nil
  trap_raise(Mongo::OperationFailure) do
    if @tailable
      doc = __next
    elsif has_next?
      doc = __next
    end
  end
  _xform(doc)
end

#query_optsObject



106
107
108
109
110
# File 'lib/jmongo/cursor.rb', line 106

def query_opts
  warn "The method Cursor#query_opts has been deprecated " +
    "and will removed in v2.0. Use Cursor#options instead."
  options
end

#remove_option(opt) ⇒ Object



112
113
114
115
116
# File 'lib/jmongo/cursor.rb', line 112

def remove_option(opt)
  raise_invalid_op if @j_cursor.num_seen != 0
  @j_cursor.setOptions(options & ~opt)
  options
end

#rewind!Object



72
73
74
75
# File 'lib/jmongo/cursor.rb', line 72

def rewind!
  close
  spawn_cursor
end

#sizeObject



221
222
223
# File 'lib/jmongo/cursor.rb', line 221

def size
  @j_cursor.size
end

#skip(number_to_skip = nil) ⇒ Object



199
200
201
202
203
204
205
# File 'lib/jmongo/cursor.rb', line 199

def skip(number_to_skip=nil)
  _skip(number_to_skip)
  wrap_invalid_op do
    @j_cursor = @j_cursor.skip(@skip)
  end
  self
end

#sort(key_or_list, direction = nil) ⇒ Object



213
214
215
216
217
218
219
# File 'lib/jmongo/cursor.rb', line 213

def sort(key_or_list, direction=nil)
  _sort(key_or_list, direction)
  wrap_invalid_op do
    @j_cursor = @j_cursor.sort(@order)
  end
  self
end

#to_aObject



248
249
250
251
252
253
254
255
# File 'lib/jmongo/cursor.rb', line 248

def to_a
  ret = []
  rewind! unless has_next?
  while has_next?
    ret << __next
  end
  ret
end

#to_do_sizeObject



265
266
267
# File 'lib/jmongo/cursor.rb', line 265

def to_do_size
  @j_cursor.size - @j_cursor.num_seen
end

#to_setObject



257
258
259
# File 'lib/jmongo/cursor.rb', line 257

def to_set
  Set.new self.to_a
end