RUBY-113 allow has_next for cursors
This commit is contained in:
parent
3e8c8d5c93
commit
4008752b02
|
@ -78,6 +78,13 @@ module Mongo
|
||||||
doc
|
doc
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Determine whether this cursor has any remaining results.
|
||||||
|
#
|
||||||
|
# @return [Boolean]
|
||||||
|
def has_next?
|
||||||
|
num_remaining > 0
|
||||||
|
end
|
||||||
|
|
||||||
# Get the size of the result set for this query.
|
# Get the size of the result set for this query.
|
||||||
#
|
#
|
||||||
# @return [Integer] the number of objects in the result set for this query. Does
|
# @return [Integer] the number of objects in the result set for this query. Does
|
||||||
|
@ -169,7 +176,7 @@ module Mongo
|
||||||
# end
|
# end
|
||||||
def each
|
def each
|
||||||
num_returned = 0
|
num_returned = 0
|
||||||
while more? && (@limit <= 0 || num_returned < @limit)
|
while has_next? && (@limit <= 0 || num_returned < @limit)
|
||||||
yield next_document
|
yield next_document
|
||||||
num_returned += 1
|
num_returned += 1
|
||||||
end
|
end
|
||||||
|
@ -188,7 +195,7 @@ module Mongo
|
||||||
raise InvalidOperation, "can't call Cursor#to_a on a used cursor" if @query_run
|
raise InvalidOperation, "can't call Cursor#to_a on a used cursor" if @query_run
|
||||||
rows = []
|
rows = []
|
||||||
num_returned = 0
|
num_returned = 0
|
||||||
while more? && (@limit <= 0 || num_returned < @limit)
|
while has_next? && (@limit <= 0 || num_returned < @limit)
|
||||||
rows << next_document
|
rows << next_document
|
||||||
num_returned += 1
|
num_returned += 1
|
||||||
end
|
end
|
||||||
|
@ -305,13 +312,6 @@ module Mongo
|
||||||
@cache.length
|
@cache.length
|
||||||
end
|
end
|
||||||
|
|
||||||
# Internal method, not for general use. Return +true+ if there are
|
|
||||||
# more records to retrieve. This method does not check @limit;
|
|
||||||
# Cursor#each is responsible for doing that.
|
|
||||||
def more?
|
|
||||||
num_remaining > 0
|
|
||||||
end
|
|
||||||
|
|
||||||
def refill_via_get_more
|
def refill_via_get_more
|
||||||
return if send_initial_query || @cursor_id.zero?
|
return if send_initial_query || @cursor_id.zero?
|
||||||
message = ByteBuffer.new([0, 0, 0, 0])
|
message = ByteBuffer.new([0, 0, 0, 0])
|
||||||
|
|
|
@ -376,4 +376,18 @@ class CursorTest < Test::Unit::TestCase
|
||||||
assert_equal(1, @@coll.find({}, :fields => ["a"]).count())
|
assert_equal(1, @@coll.find({}, :fields => ["a"]).count())
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_has_next
|
||||||
|
@@coll.remove
|
||||||
|
200.times do |n|
|
||||||
|
@@coll.save("x" => n)
|
||||||
|
end
|
||||||
|
|
||||||
|
cursor = @@coll.find
|
||||||
|
while cursor.has_next?
|
||||||
|
assert cursor.next_document
|
||||||
|
end
|
||||||
|
|
||||||
|
assert_equal false, cursor.has_next?
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue