Skip to content

Commit f0dd317

Browse files
committed
RUBY-844 Allow alternate namespace for cursors
1 parent 6fe9383 commit f0dd317

File tree

3 files changed

+21
-7
lines changed

3 files changed

+21
-7
lines changed

lib/mongo/collection.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -729,7 +729,8 @@ def aggregate(pipeline=nil, opts={})
729729
seed = {
730730
:cursor_id => cursor_info['id'],
731731
:first_batch => cursor_info['firstBatch'],
732-
:pool => pinned_pool
732+
:pool => pinned_pool,
733+
:ns => cursor_info['ns']
733734
}
734735

735736
return Cursor.new(self, seed.merge!(opts))
@@ -901,7 +902,8 @@ def parallel_scan(num_cursors, opts={})
901902
seed = {
902903
:cursor_id => cursor_info['cursor']['id'],
903904
:first_batch => cursor_info['cursor']['firstBatch'],
904-
:pool => pinned_pool
905+
:pool => pinned_pool,
906+
:ns => cursor_info['ns']
905907
}
906908
Cursor.new(self, seed.merge!(opts))
907909
end

lib/mongo/cursor.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ def initialize(collection, opts={})
4040
@cursor_id = opts.delete(:cursor_id)
4141
@db = collection.db
4242
@collection = collection
43+
@ns = opts.delete(:ns)
4344
@connection = @db.connection
4445
@logger = @connection.logger
4546

@@ -83,7 +84,6 @@ def initialize(collection, opts={})
8384

8485
batch_size(opts.delete(:batch_size) || 0)
8586

86-
@full_collection_name = "#{@collection.db.name}.#{@collection.name}"
8787
@cache = opts.delete(:first_batch) || []
8888
@returned = 0
8989

@@ -124,6 +124,10 @@ def alive?
124124
@cursor_id && @cursor_id != 0
125125
end
126126

127+
def full_collection_name
128+
@ns || "#{@collection.db.name}.#{@collection.name}"
129+
end
130+
127131
# Get the next document specified the cursor options.
128132
#
129133
# @return [Hash, Nil] the next document or Nil if no documents remain.
@@ -580,7 +584,7 @@ def send_get_more
580584
message = BSON::ByteBuffer.new([0, 0, 0, 0])
581585

582586
# DB name.
583-
BSON::BSON_RUBY.serialize_cstr(message, "#{@db.name}.#{@collection.name}")
587+
BSON::BSON_RUBY.serialize_cstr(message, full_collection_name)
584588

585589
# Number of results to return.
586590
if @limit > 0
@@ -636,7 +640,7 @@ def checkin_socket(sock)
636640
def construct_query_message
637641
message = BSON::ByteBuffer.new("", @connection.max_bson_size + MongoClient::COMMAND_HEADROOM)
638642
message.put_int(@options)
639-
BSON::BSON_RUBY.serialize_cstr(message, "#{@db.name}.#{@collection.name}")
643+
BSON::BSON_RUBY.serialize_cstr(message, full_collection_name)
640644
message.put_int(@skip)
641645
@batch_size > 1 ? message.put_int(@batch_size) : message.put_int(@limit)
642646
if query_contains_special_fields? && @bson # costs two serialize calls

test/unit/cursor_test.rb

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,15 @@ class Mongo::Cursor
138138
assert_equal 100, @cursor.batch_size
139139
end
140140

141-
context "conected to mongos" do
141+
context 'when an alternate namespace is specified' do
142+
143+
should 'use the alternate namespace' do
144+
assert_equal('other_db.other_collection',
145+
Cursor.new(@collection, :ns => 'other_db.other_collection').full_collection_name)
146+
end
147+
end
148+
149+
context "connected to mongos" do
142150
setup do
143151
@connection.stubs(:mongos?).returns(true)
144152
@tag_sets = [{:dc => "ny"}]
@@ -211,7 +219,7 @@ class Mongo::Cursor
211219
end
212220
end
213221

214-
context "not conected to mongos" do
222+
context "not connected to mongos" do
215223
setup do
216224
@connection.stubs(:mongos?).returns(false)
217225
end

0 commit comments

Comments
 (0)