Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

RUBY-558 apply batch size on initial query #167

Closed
wants to merge 1 commit into from

2 participants

Emily Tyler Brock
Emily
Collaborator

Change to cursor.rb and tests for applying specified batch size to initial query. Get mores have always applied batch size. RUBY-558

Tyler Brock

LGTM, previous discussion in #163.

Tyler Brock TylerBrock closed this March 01, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Mar 01, 2013
Emily RUBY-558 apply batch size on initial query 28bb7de
This page is out of date. Refresh to see the latest.
2  lib/mongo/cursor.rb
@@ -552,7 +552,7 @@ def construct_query_message
552 552
       message.put_int(@options)
553 553
       BSON::BSON_RUBY.serialize_cstr(message, "#{@db.name}.#{@collection.name}")
554 554
       message.put_int(@skip)
555  
-      message.put_int(@limit)
  555
+      @batch_size > 1 ? message.put_int(@batch_size) : message.put_int(@limit)
556 556
       spec = query_contains_special_fields? ? construct_query_spec : @selector
557 557
       message.put_binary(BSON::BSON_CODER.serialize(spec, false, false, @connection.max_bson_size).to_s)
558 558
       message.put_binary(BSON::BSON_CODER.serialize(@fields, false, false, @connection.max_bson_size).to_s) if @fields
60  test/functional/collection_test.rb
@@ -902,6 +902,66 @@ def test_save_symbol_find_string
902 902
     # assert_equal :mike, @@test.find_one("foo" => "mike")["foo"]
903 903
   end
904 904
 
  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
+
905 965
   def test_limit_and_skip
906 966
     10.times do |i|
907 967
       @@test.save(:foo => i)
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.