From 237dc98fbf3c0a41ef5ca91aafb1789c927f8744 Mon Sep 17 00:00:00 2001 From: Kyle Banker Date: Fri, 5 Aug 2011 11:52:45 -0400 Subject: [PATCH] RUBY-283 add_option for cursors. Deprecate Cursor#query_opts. --- docs/HISTORY.md | 5 +++++ lib/mongo/cursor.rb | 40 ++++++++++++++++++++++++++++++++-------- test/cursor_test.rb | 8 +++++++- 3 files changed, 44 insertions(+), 9 deletions(-) diff --git a/docs/HISTORY.md b/docs/HISTORY.md index 7dedf34..c4fa2d2 100644 --- a/docs/HISTORY.md +++ b/docs/HISTORY.md @@ -1,5 +1,10 @@ # MongoDB Ruby Driver History +### 1.4.0 +UNRELEASED + +* Add Cursor#add_option. Deprecate Cursor#query_opts and replace with Cursor#options. + ### 1.3.1 2011-5-10 diff --git a/lib/mongo/cursor.rb b/lib/mongo/cursor.rb index 4f567b2..4179c2c 100644 --- a/lib/mongo/cursor.rb +++ b/lib/mongo/cursor.rb @@ -20,10 +20,12 @@ module Mongo class Cursor include Mongo::Conversions include Enumerable + include Mongo::Constants attr_reader :collection, :selector, :fields, :order, :hint, :snapshot, :timeout, - :full_collection_name, :transformer + :full_collection_name, :transformer, + :options # Create a new cursor. # @@ -59,6 +61,7 @@ module Mongo @limit = opts[:limit] || 0 @tailable = opts[:tailable] || false @timeout = opts.fetch(:timeout, true) + @options = 0 # Use this socket for the query @socket = opts[:socket] @@ -73,6 +76,16 @@ module Mongo @cache = [] @returned = 0 + if(!@timeout) + add_option(OP_QUERY_NO_CURSOR_TIMEOUT) + end + if(@connection.slave_ok?) + add_option(OP_QUERY_SLAVE_OK) + end + if(@tailable) + add_option(OP_QUERY_TAILABLE) + end + if @collection.name =~ /^\$cmd/ || @collection.name =~ /^system/ @command = true else @@ -274,7 +287,8 @@ module Mongo # # @core explain explain-instance_method def explain - c = Cursor.new(@collection, query_options_hash.merge(:limit => -@limit.abs, :explain => true)) + c = Cursor.new(@collection, + query_options_hash.merge(:limit => -@limit.abs, :explain => true)) explanation = c.next_document c.close @@ -315,11 +329,21 @@ module Mongo # @see http://www.mongodb.org/display/DOCS/Mongo+Wire+Protocol#MongoWireProtocol-Mongo::Constants::OPQUERY # The MongoDB wire protocol. def query_opts - opts = 0 - opts |= Mongo::Constants::OP_QUERY_NO_CURSOR_TIMEOUT unless @timeout - opts |= Mongo::Constants::OP_QUERY_SLAVE_OK if @connection.slave_ok? - opts |= Mongo::Constants::OP_QUERY_TAILABLE if @tailable - opts + warn "The method Cursor#query_opts has been deprecated " + + "and will removed in v2.0. Use Cursor#options instead." + @options + end + + # Add an option to the query options bitfield. + # + # @param opt a valid query option + # + # @return [Integer] the current value of options for this cursor. + # + # @see http://www.mongodb.org/display/DOCS/Mongo+Wire+Protocol#MongoWireProtocol-Mongo::Constants::OPQUERY + def add_option(opt) + @options |= opt + return @options end # Get the query options for this Cursor. @@ -418,7 +442,7 @@ module Mongo def construct_query_message message = BSON::ByteBuffer.new - message.put_int(query_opts) + message.put_int(@options) BSON::BSON_RUBY.serialize_cstr(message, "#{@db.name}.#{@collection.name}") message.put_int(@skip) message.put_int(@limit) diff --git a/test/cursor_test.rb b/test/cursor_test.rb index 7942169..b06b58d 100644 --- a/test/cursor_test.rb +++ b/test/cursor_test.rb @@ -2,8 +2,8 @@ require './test/test_helper' require 'logger' class CursorTest < Test::Unit::TestCase - include Mongo + include Mongo::Constants @@connection = standard_connection @@db = @@connection.db(MONGO_TEST_DB) @@ -16,6 +16,12 @@ class CursorTest < Test::Unit::TestCase @@coll_full_name = "#{MONGO_TEST_DB}.test" end + def test_add_options + c = @@coll.find + c.add_option(OP_QUERY_EXHAUST) + assert c.options & OP_QUERY_EXHAUST + end + def test_inspect selector = {:a => 1} cursor = @@coll.find(selector)