remove redundant num_to_return from Cursor class. credits. clean up last commit
This commit is contained in:
parent
c397758d1d
commit
3321a90739
@ -329,6 +329,9 @@ Cyril Mougel, cyril.mougel@gmail.com
|
|||||||
Jack Chen, chendo on github
|
Jack Chen, chendo on github
|
||||||
* Test case + fix for deserializing pre-epoch Time instances
|
* Test case + fix for deserializing pre-epoch Time instances
|
||||||
|
|
||||||
|
Kyle Banker, banker on github
|
||||||
|
* #limit and #skip methods for Cursor instances
|
||||||
|
|
||||||
= License
|
= License
|
||||||
|
|
||||||
Copyright 2008-2009 10gen Inc.
|
Copyright 2008-2009 10gen Inc.
|
||||||
|
@ -32,7 +32,6 @@ module Mongo
|
|||||||
# Should not be called directly by application developers.
|
# Should not be called directly by application developers.
|
||||||
def initialize(db, collection, query, admin=false)
|
def initialize(db, collection, query, admin=false)
|
||||||
@db, @collection, @query, @admin = db, collection, query, admin
|
@db, @collection, @query, @admin = db, collection, query, admin
|
||||||
@num_to_return = @query.number_to_return || 0
|
|
||||||
@cache = []
|
@cache = []
|
||||||
@closed = false
|
@closed = false
|
||||||
@query_run = false
|
@query_run = false
|
||||||
@ -74,25 +73,23 @@ module Mongo
|
|||||||
raise OperationFailure, "Count failed: #{response['errmsg']}"
|
raise OperationFailure, "Count failed: #{response['errmsg']}"
|
||||||
end
|
end
|
||||||
|
|
||||||
# Sets a limit on the number of results returned by the query.
|
# Limits the number of results to be returned by this cursor.
|
||||||
# Returns a cursor object.
|
|
||||||
#
|
#
|
||||||
# Note: this method overrides any limit specified in the #find method.
|
# Note: this method overrides any limit specified in the #find method.
|
||||||
def limit(number_to_return)
|
def limit(number_to_return)
|
||||||
check_modifiable
|
check_modifiable
|
||||||
raise ArgumentError, "limit requires an integer" unless number_to_return.is_a? Integer
|
raise ArgumentError, "limit requires an integer" unless number_to_return.is_a? Integer
|
||||||
|
|
||||||
@number_to_return = number_to_return
|
|
||||||
@query.number_to_return = number_to_return
|
@query.number_to_return = number_to_return
|
||||||
return self
|
return self
|
||||||
end
|
end
|
||||||
|
|
||||||
# Sets an offset on the query results. Returns a cursor object.
|
# Skips the first +number_to_skip+ results of this cursor.
|
||||||
#
|
#
|
||||||
# Note: this method overrides any offset specified in the #find method.
|
# Note: this method overrides any offset specified in the #find method.
|
||||||
def offset(number_to_skip)
|
def skip(number_to_skip)
|
||||||
check_modifiable
|
check_modifiable
|
||||||
raise ArgumentError, "limit requires an integer" unless number_to_skip.is_a? Integer
|
raise ArgumentError, "skip requires an integer" unless number_to_skip.is_a? Integer
|
||||||
|
|
||||||
@query.number_to_skip = number_to_skip
|
@query.number_to_skip = number_to_skip
|
||||||
return self
|
return self
|
||||||
@ -104,7 +101,7 @@ module Mongo
|
|||||||
# Iterating over an entire cursor will close it.
|
# Iterating over an entire cursor will close it.
|
||||||
def each
|
def each
|
||||||
num_returned = 0
|
num_returned = 0
|
||||||
while more? && (@num_to_return <= 0 || num_returned < @num_to_return)
|
while more? && (@query.number_to_return <= 0 || num_returned < @query.number_to_return)
|
||||||
yield next_object()
|
yield next_object()
|
||||||
num_returned += 1
|
num_returned += 1
|
||||||
end
|
end
|
||||||
@ -121,7 +118,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? && (@num_to_return <= 0 || num_returned < @num_to_return)
|
while more? && (@query.number_to_return <= 0 || num_returned < @query.number_to_return)
|
||||||
rows << next_object()
|
rows << next_object()
|
||||||
num_returned += 1
|
num_returned += 1
|
||||||
end
|
end
|
||||||
@ -198,8 +195,8 @@ module Mongo
|
|||||||
end
|
end
|
||||||
|
|
||||||
# Internal method, not for general use. Return +true+ if there are
|
# Internal method, not for general use. Return +true+ if there are
|
||||||
# more records to retrieve. We do not check @num_to_return; #each is
|
# more records to retrieve. We do not check @query.number_to_return;
|
||||||
# responsible for doing that.
|
# #each is responsible for doing that.
|
||||||
def more?
|
def more?
|
||||||
num_remaining > 0
|
num_remaining > 0
|
||||||
end
|
end
|
||||||
|
@ -71,24 +71,24 @@ class CursorTest < Test::Unit::TestCase
|
|||||||
end
|
end
|
||||||
|
|
||||||
def test_limit_exceptions
|
def test_limit_exceptions
|
||||||
assert_raise ArgumentError do
|
assert_raise ArgumentError do
|
||||||
cursor = @@coll.find().limit('not-an-integer')
|
cursor = @@coll.find().limit('not-an-integer')
|
||||||
end
|
end
|
||||||
|
|
||||||
cursor = @@coll.find()
|
cursor = @@coll.find()
|
||||||
firstResult = cursor.next_object()
|
firstResult = cursor.next_object()
|
||||||
assert_raise InvalidOperation, "Cannot modify the query once it has been run or closed." do
|
assert_raise InvalidOperation, "Cannot modify the query once it has been run or closed." do
|
||||||
cursor.limit(1)
|
cursor.limit(1)
|
||||||
end
|
end
|
||||||
|
|
||||||
cursor = @@coll.find()
|
cursor = @@coll.find()
|
||||||
cursor.close
|
cursor.close
|
||||||
assert_raise InvalidOperation, "Cannot modify the query once it has been run or closed." do
|
assert_raise InvalidOperation, "Cannot modify the query once it has been run or closed." do
|
||||||
cursor.limit(1)
|
cursor.limit(1)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_offset
|
def test_skip
|
||||||
@@coll.clear
|
@@coll.clear
|
||||||
|
|
||||||
10.times do |i|
|
10.times do |i|
|
||||||
@ -97,41 +97,41 @@ class CursorTest < Test::Unit::TestCase
|
|||||||
assert_equal 10, @@coll.find().count()
|
assert_equal 10, @@coll.find().count()
|
||||||
|
|
||||||
all_results = @@coll.find().to_a
|
all_results = @@coll.find().to_a
|
||||||
offset_results = @@coll.find().offset(2).to_a
|
skip_results = @@coll.find().skip(2).to_a
|
||||||
assert_equal 10, all_results.length
|
assert_equal 10, all_results.length
|
||||||
assert_equal 8, offset_results.length
|
assert_equal 8, skip_results.length
|
||||||
|
|
||||||
assert_equal all_results.slice(2...10), offset_results
|
assert_equal all_results.slice(2...10), skip_results
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_offset_exceptions
|
def test_skip_exceptions
|
||||||
assert_raise ArgumentError do
|
assert_raise ArgumentError do
|
||||||
cursor = @@coll.find().offset('not-an-integer')
|
cursor = @@coll.find().skip('not-an-integer')
|
||||||
end
|
end
|
||||||
|
|
||||||
cursor = @@coll.find()
|
cursor = @@coll.find()
|
||||||
firstResult = cursor.next_object()
|
firstResult = cursor.next_object()
|
||||||
assert_raise InvalidOperation, "Cannot modify the query once it has been run or closed." do
|
assert_raise InvalidOperation, "Cannot modify the query once it has been run or closed." do
|
||||||
cursor.offset(1)
|
cursor.skip(1)
|
||||||
end
|
end
|
||||||
|
|
||||||
cursor = @@coll.find()
|
cursor = @@coll.find()
|
||||||
cursor.close
|
cursor.close
|
||||||
assert_raise InvalidOperation, "Cannot modify the query once it has been run or closed." do
|
assert_raise InvalidOperation, "Cannot modify the query once it has been run or closed." do
|
||||||
cursor.offset(1)
|
cursor.skip(1)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_limit_offset_chaining
|
def test_limit_skip_chaining
|
||||||
@@coll.clear
|
@@coll.clear
|
||||||
10.times do |i|
|
10.times do |i|
|
||||||
@@coll.save("x" => i)
|
@@coll.save("x" => i)
|
||||||
end
|
end
|
||||||
|
|
||||||
all_results = @@coll.find().to_a
|
all_results = @@coll.find().to_a
|
||||||
limited_offset_results = @@coll.find().limit(5).offset(3).to_a
|
limited_skip_results = @@coll.find().limit(5).skip(3).to_a
|
||||||
|
|
||||||
assert_equal all_results.slice(3...8), limited_offset_results
|
assert_equal all_results.slice(3...8), limited_skip_results
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_close_no_query_sent
|
def test_close_no_query_sent
|
||||||
|
Loading…
Reference in New Issue
Block a user