RUBY-283 add_option for cursors. Deprecate Cursor#query_opts.

This commit is contained in:
Kyle Banker 2011-08-05 11:52:45 -04:00
parent 5950f6850f
commit 237dc98fbf
3 changed files with 44 additions and 9 deletions

View File

@ -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

View File

@ -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)

View File

@ -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)