From f0dd3173d17ac5255df52303d575dd6af97d5ea7 Mon Sep 17 00:00:00 2001 From: Emily Stolfo Date: Thu, 18 Dec 2014 17:23:44 -0500 Subject: [PATCH 1/3] RUBY-844 Allow alternate namespace for cursors --- lib/mongo/collection.rb | 6 ++++-- lib/mongo/cursor.rb | 10 +++++++--- test/unit/cursor_test.rb | 12 ++++++++++-- 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/lib/mongo/collection.rb b/lib/mongo/collection.rb index 6e6a86e33c..e575f864e8 100644 --- a/lib/mongo/collection.rb +++ b/lib/mongo/collection.rb @@ -729,7 +729,8 @@ def aggregate(pipeline=nil, opts={}) seed = { :cursor_id => cursor_info['id'], :first_batch => cursor_info['firstBatch'], - :pool => pinned_pool + :pool => pinned_pool, + :ns => cursor_info['ns'] } return Cursor.new(self, seed.merge!(opts)) @@ -901,7 +902,8 @@ def parallel_scan(num_cursors, opts={}) seed = { :cursor_id => cursor_info['cursor']['id'], :first_batch => cursor_info['cursor']['firstBatch'], - :pool => pinned_pool + :pool => pinned_pool, + :ns => cursor_info['ns'] } Cursor.new(self, seed.merge!(opts)) end diff --git a/lib/mongo/cursor.rb b/lib/mongo/cursor.rb index 983d5837ae..74b0f72460 100644 --- a/lib/mongo/cursor.rb +++ b/lib/mongo/cursor.rb @@ -40,6 +40,7 @@ def initialize(collection, opts={}) @cursor_id = opts.delete(:cursor_id) @db = collection.db @collection = collection + @ns = opts.delete(:ns) @connection = @db.connection @logger = @connection.logger @@ -83,7 +84,6 @@ def initialize(collection, opts={}) batch_size(opts.delete(:batch_size) || 0) - @full_collection_name = "#{@collection.db.name}.#{@collection.name}" @cache = opts.delete(:first_batch) || [] @returned = 0 @@ -124,6 +124,10 @@ def alive? @cursor_id && @cursor_id != 0 end + def full_collection_name + @ns || "#{@collection.db.name}.#{@collection.name}" + end + # Get the next document specified the cursor options. # # @return [Hash, Nil] the next document or Nil if no documents remain. @@ -580,7 +584,7 @@ def send_get_more message = BSON::ByteBuffer.new([0, 0, 0, 0]) # DB name. - BSON::BSON_RUBY.serialize_cstr(message, "#{@db.name}.#{@collection.name}") + BSON::BSON_RUBY.serialize_cstr(message, full_collection_name) # Number of results to return. if @limit > 0 @@ -636,7 +640,7 @@ def checkin_socket(sock) def construct_query_message message = BSON::ByteBuffer.new("", @connection.max_bson_size + MongoClient::COMMAND_HEADROOM) message.put_int(@options) - BSON::BSON_RUBY.serialize_cstr(message, "#{@db.name}.#{@collection.name}") + BSON::BSON_RUBY.serialize_cstr(message, full_collection_name) message.put_int(@skip) @batch_size > 1 ? message.put_int(@batch_size) : message.put_int(@limit) if query_contains_special_fields? && @bson # costs two serialize calls diff --git a/test/unit/cursor_test.rb b/test/unit/cursor_test.rb index 749198ab5f..7717a94783 100644 --- a/test/unit/cursor_test.rb +++ b/test/unit/cursor_test.rb @@ -138,7 +138,15 @@ class Mongo::Cursor assert_equal 100, @cursor.batch_size end - context "conected to mongos" do + context 'when an alternate namespace is specified' do + + should 'use the alternate namespace' do + assert_equal('other_db.other_collection', + Cursor.new(@collection, :ns => 'other_db.other_collection').full_collection_name) + end + end + + context "connected to mongos" do setup do @connection.stubs(:mongos?).returns(true) @tag_sets = [{:dc => "ny"}] @@ -211,7 +219,7 @@ class Mongo::Cursor end end - context "not conected to mongos" do + context "not connected to mongos" do setup do @connection.stubs(:mongos?).returns(false) end From 0889081d1e82614865a1b0439c00b80778c092b0 Mon Sep 17 00:00:00 2001 From: Emily Stolfo Date: Fri, 19 Dec 2014 12:38:25 -0500 Subject: [PATCH 2/3] RUBY-844 remove attr_reader and clean up test --- lib/mongo/cursor.rb | 3 +-- test/unit/cursor_test.rb | 4 ++-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/lib/mongo/cursor.rb b/lib/mongo/cursor.rb index 74b0f72460..b56d2df803 100644 --- a/lib/mongo/cursor.rb +++ b/lib/mongo/cursor.rb @@ -23,8 +23,7 @@ class Cursor include Mongo::ReadPreference attr_reader :collection, :selector, :fields, - :order, :hint, :snapshot, :timeout, - :full_collection_name, :transformer, + :order, :hint, :snapshot, :timeout, :transformer, :options, :cursor_id, :show_disk_loc, :comment, :compile_regex, :read, :tag_sets, :acceptable_latency diff --git a/test/unit/cursor_test.rb b/test/unit/cursor_test.rb index 7717a94783..a8c9ab04ae 100644 --- a/test/unit/cursor_test.rb +++ b/test/unit/cursor_test.rb @@ -141,8 +141,8 @@ class Mongo::Cursor context 'when an alternate namespace is specified' do should 'use the alternate namespace' do - assert_equal('other_db.other_collection', - Cursor.new(@collection, :ns => 'other_db.other_collection').full_collection_name) + cursor = Cursor.new(@collection, :ns => 'other_db.other_collection') + assert_equal('other_db.other_collection', cursor.full_collection_name) end end From abe8f7f571d854789a2c0b870309d3d089ca9505 Mon Sep 17 00:00:00 2001 From: Emily Stolfo Date: Fri, 19 Dec 2014 15:18:40 -0500 Subject: [PATCH 3/3] RUBY-844 Update cursor inspect string with namespace --- lib/mongo/cursor.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/mongo/cursor.rb b/lib/mongo/cursor.rb index b56d2df803..5837729ade 100644 --- a/lib/mongo/cursor.rb +++ b/lib/mongo/cursor.rb @@ -487,7 +487,7 @@ def query_options_hash # Clean output for inspect. def inspect - "" end