From dcda70fdddc4c32563da198a1ccaf94e455c206f Mon Sep 17 00:00:00 2001 From: Mike Dirolf Date: Tue, 18 Aug 2009 17:36:53 -0400 Subject: [PATCH] don't send a getmore immediately after every query --- lib/mongo/cursor.rb | 10 +++++--- test/test_cursor.rb | 57 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 3 deletions(-) diff --git a/lib/mongo/cursor.rb b/lib/mongo/cursor.rb index 9128b14..df5ee73 100644 --- a/lib/mongo/cursor.rb +++ b/lib/mongo/cursor.rb @@ -206,8 +206,9 @@ module XGen end def refill_via_get_more - send_query_if_needed - return if @cursor_id == 0 + if send_query_if_needed or @cursor_id == 0 + return + end @db._synchronize { @db.send_to_db(GetMoreMessage.new(@admin ? 'admin' : @db.name, @collection.name, @cursor_id)) read_all @@ -227,12 +228,15 @@ module XGen def send_query_if_needed # Run query first time we request an object from the wire - unless @query_run + if @query_run + false + else @db._synchronize { @db.send_query_message(QueryMessage.new(@admin ? 'admin' : @db.name, @collection.name, @query)) @query_run = true read_all } + true end end diff --git a/test/test_cursor.rb b/test/test_cursor.rb index ef84078..fa291c8 100644 --- a/test/test_cursor.rb +++ b/test/test_cursor.rb @@ -145,4 +145,61 @@ class CursorTest < Test::Unit::TestCase end end + def test_kill_cursors + @@coll.drop + + client_cursors = @@db.db_command("cursorInfo" => 1)["clientCursors_size"] + by_location = @@db.db_command("cursorInfo" => 1)["byLocation_size"] + + 10000.times do |i| + @@coll.insert("i" => i) + end + + assert_equal(client_cursors, + @@db.db_command("cursorInfo" => 1)["clientCursors_size"]) + assert_equal(by_location, + @@db.db_command("cursorInfo" => 1)["byLocation_size"]) + + 10.times do |i| + @@coll.find_one() + end + + assert_equal(client_cursors, + @@db.db_command("cursorInfo" => 1)["clientCursors_size"]) + assert_equal(by_location, + @@db.db_command("cursorInfo" => 1)["byLocation_size"]) + + 10.times do |i| + a = @@coll.find() + a.next_object() + a.close() + end + + assert_equal(client_cursors, + @@db.db_command("cursorInfo" => 1)["clientCursors_size"]) + assert_equal(by_location, + @@db.db_command("cursorInfo" => 1)["byLocation_size"]) + + a = @@coll.find() + a.next_object() + + assert_not_equal(client_cursors, + @@db.db_command("cursorInfo" => 1)["clientCursors_size"]) + assert_not_equal(by_location, + @@db.db_command("cursorInfo" => 1)["byLocation_size"]) + + a.close() + + assert_equal(client_cursors, + @@db.db_command("cursorInfo" => 1)["clientCursors_size"]) + assert_equal(by_location, + @@db.db_command("cursorInfo" => 1)["byLocation_size"]) + + a = @@coll.find({}, :limit => 10).next_object() + + assert_equal(client_cursors, + @@db.db_command("cursorInfo" => 1)["clientCursors_size"]) + assert_equal(by_location, + @@db.db_command("cursorInfo" => 1)["byLocation_size"]) + end end