Skip to content

Commit 6079140

Browse files
committed
Merge pull request #558 from estolfo/RUBY-844-ns-cursor
RUBY-844 Allow alternate namespace for cursors
2 parents 6fe9383 + abe8f7f commit 6079140

File tree

3 files changed

+23
-10
lines changed

3 files changed

+23
-10
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: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ class Cursor
2323
include Mongo::ReadPreference
2424

2525
attr_reader :collection, :selector, :fields,
26-
:order, :hint, :snapshot, :timeout,
27-
:full_collection_name, :transformer,
26+
:order, :hint, :snapshot, :timeout, :transformer,
2827
:options, :cursor_id, :show_disk_loc,
2928
:comment, :compile_regex, :read, :tag_sets,
3029
:acceptable_latency
@@ -40,6 +39,7 @@ def initialize(collection, opts={})
4039
@cursor_id = opts.delete(:cursor_id)
4140
@db = collection.db
4241
@collection = collection
42+
@ns = opts.delete(:ns)
4343
@connection = @db.connection
4444
@logger = @connection.logger
4545

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

8484
batch_size(opts.delete(:batch_size) || 0)
8585

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

@@ -124,6 +123,10 @@ def alive?
124123
@cursor_id && @cursor_id != 0
125124
end
126125

126+
def full_collection_name
127+
@ns || "#{@collection.db.name}.#{@collection.name}"
128+
end
129+
127130
# Get the next document specified the cursor options.
128131
#
129132
# @return [Hash, Nil] the next document or Nil if no documents remain.
@@ -484,7 +487,7 @@ def query_options_hash
484487

485488
# Clean output for inspect.
486489
def inspect
487-
"<Mongo::Cursor:0x#{object_id.to_s(16)} namespace='#{@db.name}.#{@collection.name}' " +
490+
"<Mongo::Cursor:0x#{object_id.to_s(16)} namespace='#{full_collection_name}' " +
488491
"@selector=#{@selector.inspect} @cursor_id=#{@cursor_id}>"
489492
end
490493

@@ -580,7 +583,7 @@ def send_get_more
580583
message = BSON::ByteBuffer.new([0, 0, 0, 0])
581584

582585
# DB name.
583-
BSON::BSON_RUBY.serialize_cstr(message, "#{@db.name}.#{@collection.name}")
586+
BSON::BSON_RUBY.serialize_cstr(message, full_collection_name)
584587

585588
# Number of results to return.
586589
if @limit > 0
@@ -636,7 +639,7 @@ def checkin_socket(sock)
636639
def construct_query_message
637640
message = BSON::ByteBuffer.new("", @connection.max_bson_size + MongoClient::COMMAND_HEADROOM)
638641
message.put_int(@options)
639-
BSON::BSON_RUBY.serialize_cstr(message, "#{@db.name}.#{@collection.name}")
642+
BSON::BSON_RUBY.serialize_cstr(message, full_collection_name)
640643
message.put_int(@skip)
641644
@batch_size > 1 ? message.put_int(@batch_size) : message.put_int(@limit)
642645
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+
cursor = Cursor.new(@collection, :ns => 'other_db.other_collection')
145+
assert_equal('other_db.other_collection', cursor.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)