From 91308cdc6007433b8e4ff1ac9f127ebe70d3fb2c Mon Sep 17 00:00:00 2001 From: Kyle Banker Date: Fri, 5 Aug 2011 17:51:37 -0400 Subject: [PATCH] RUBY-167 Cursor#alive? --- lib/mongo/cursor.rb | 12 ++++++++++++ test/cursor_test.rb | 16 ++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/lib/mongo/cursor.rb b/lib/mongo/cursor.rb index 4179c2c..2d31add 100644 --- a/lib/mongo/cursor.rb +++ b/lib/mongo/cursor.rb @@ -93,6 +93,18 @@ module Mongo end end + # Guess whether the cursor is alive on the server. + # + # Note that this method only checks whether we have + # a cursor id. The cursor may still have timed out + # on the server. This will be indicated in the next + # call to Cursor#next_document. + # + # @return [Boolean] + def alive? + @cursor_id && @cursor_id != 0 + end + # Get the next document specified the cursor options. # # @return [Hash, Nil] the next document or Nil if no documents remain. diff --git a/test/cursor_test.rb b/test/cursor_test.rb index b06b58d..8ed4989 100644 --- a/test/cursor_test.rb +++ b/test/cursor_test.rb @@ -16,6 +16,22 @@ class CursorTest < Test::Unit::TestCase @@coll_full_name = "#{MONGO_TEST_DB}.test" end + def test_alive + batch = [] + 5000.times do |n| + batch << {:a => n} + end + + @@coll.insert(batch) + cursor = @@coll.find + assert !cursor.alive? + cursor.next + assert cursor.alive? + cursor.close + assert !cursor.alive? + @@coll.remove + end + def test_add_options c = @@coll.find c.add_option(OP_QUERY_EXHAUST)