Skip to content

Commit 3b31742

Browse files
estolfoTylerBrock
authored andcommitted
RUBY-558 apply batch size on initial query
1 parent daa77bd commit 3b31742

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed

lib/mongo/cursor.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -552,7 +552,7 @@ def construct_query_message
552552
message.put_int(@options)
553553
BSON::BSON_RUBY.serialize_cstr(message, "#{@db.name}.#{@collection.name}")
554554
message.put_int(@skip)
555-
message.put_int(@limit)
555+
@batch_size > 1 ? message.put_int(@batch_size) : message.put_int(@limit)
556556
spec = query_contains_special_fields? ? construct_query_spec : @selector
557557
message.put_binary(BSON::BSON_CODER.serialize(spec, false, false, @connection.max_bson_size).to_s)
558558
message.put_binary(BSON::BSON_CODER.serialize(@fields, false, false, @connection.max_bson_size).to_s) if @fields

test/functional/collection_test.rb

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -902,6 +902,66 @@ def test_save_symbol_find_string
902902
# assert_equal :mike, @@test.find_one("foo" => "mike")["foo"]
903903
end
904904

905+
def test_batch_size
906+
n_docs = 6
907+
batch_size = n_docs/2
908+
n_docs.times do |i|
909+
@@test.save(:foo => i)
910+
end
911+
912+
doc_count = 0
913+
cursor = @@test.find({}, :batch_size => batch_size)
914+
cursor.next
915+
assert_equal batch_size, cursor.instance_variable_get(:@returned)
916+
doc_count += batch_size
917+
batch_size.times { cursor.next }
918+
assert_equal doc_count + batch_size, cursor.instance_variable_get(:@returned)
919+
doc_count += batch_size
920+
assert_equal n_docs, doc_count
921+
end
922+
923+
def test_batch_size_with_smaller_limit
924+
n_docs = 6
925+
batch_size = n_docs/2
926+
n_docs.times do |i|
927+
@@test.insert(:foo => i)
928+
end
929+
930+
cursor = @@test.find({}, :batch_size => batch_size, :limit => 2)
931+
cursor.next
932+
assert_equal 2, cursor.instance_variable_get(:@returned)
933+
end
934+
935+
def test_batch_size_with_larger_limit
936+
n_docs = 6
937+
batch_size = n_docs/2
938+
n_docs.times do |i|
939+
@@test.insert(:foo => i)
940+
end
941+
942+
doc_count = 0
943+
cursor = @@test.find({}, :batch_size => batch_size, :limit => n_docs + 5)
944+
cursor.next
945+
assert_equal batch_size, cursor.instance_variable_get(:@returned)
946+
doc_count += batch_size
947+
batch_size.times { cursor.next }
948+
assert_equal doc_count + batch_size, cursor.instance_variable_get(:@returned)
949+
doc_count += batch_size
950+
assert_equal n_docs, doc_count
951+
end
952+
953+
def test_batch_size_with_negative_limit
954+
n_docs = 6
955+
batch_size = n_docs/2
956+
n_docs.times do |i|
957+
@@test.insert(:foo => i)
958+
end
959+
960+
cursor = @@test.find({}, :batch_size => batch_size, :limit => -7)
961+
cursor.next
962+
assert_equal n_docs, cursor.instance_variable_get(:@returned)
963+
end
964+
905965
def test_limit_and_skip
906966
10.times do |i|
907967
@@test.save(:foo => i)

0 commit comments

Comments
 (0)