Skip to content

Commit 1aa753e

Browse files
author
Brandon Black
committed
RUBY-494 cleaning up all references to Connection, ReplSetConnection, and ShardedConnection
1 parent ec4d139 commit 1aa753e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

82 files changed

+989
-1018
lines changed

README.md

+12-12
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ Here's a quick code sample. Again, see the [MongoDB Ruby Tutorial](https://githu
4444
require 'rubygems'
4545
require 'mongo'
4646

47-
@conn = Mongo::Connection.new('localhost', 27017, :safe => true)
48-
@db = @conn['sample-db']
49-
@coll = @db['test']
47+
@client = Mongo::Client.new('localhost', 27017, :safe => true)
48+
@db = @client['sample-db']
49+
@coll = @db['test']
5050

5151
@coll.remove
5252
3.times do |i|
@@ -164,7 +164,7 @@ timeout for waiting for old connections to be released to the pool.
164164

165165
To set up a pooled connection to a single MongoDB instance:
166166

167-
@conn = Connection.new("localhost", 27017, :safe => true, :pool_size => 5, :timeout => 5)
167+
@client = Client.new("localhost", 27017, :safe => true, :pool_size => 5, :timeout => 5)
168168

169169
Though the pooling architecture will undoubtedly evolve, it currently owes much credit
170170
to the connection pooling implementations in ActiveRecord and PyMongo.
@@ -177,13 +177,13 @@ of v1.3.0, the Ruby driver detects forking and reconnects automatically.
177177

178178
## Environment variable `MONGODB_URI`
179179

180-
`Mongo::Connection.from_uri`, `Mongo::Connection.new` and `Mongo::ReplSetConnection.new` will use <code>ENV["MONGODB_URI"]</code> if no other args are provided.
180+
`Mongo::Client.from_uri`, `Mongo::Client.new` and `Mongo::ReplSetClient.new` will use <code>ENV["MONGODB_URI"]</code> if no other args are provided.
181181

182182
The URI must fit this specification:
183183

184184
mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]
185185

186-
If the type of connection (direct or replica set) should be determined entirely from <code>ENV["MONGODB_URI"]</code>, you may want to use `Mongo::Connection.from_uri` because it will return either `Mongo::Connection` or a `Mongo::ReplSetConnection` depending on how many hosts are specified. Trying to use `Mongo::Connection.new` with multiple hosts in <code>ENV["MONGODB_URI"]</code> will raise an exception.
186+
If the type of connection (direct or replica set) should be determined entirely from <code>ENV["MONGODB_URI"]</code>, you may want to use `Mongo::Client.from_uri` because it will return either `Mongo::Client` or a `Mongo::ReplSetClient` depending on how many hosts are specified. Trying to use `Mongo::Client.new` with multiple hosts in <code>ENV["MONGODB_URI"]</code> will raise an exception.
187187

188188
## String Encoding
189189

@@ -213,9 +213,9 @@ generate _id values. If you want to control _id values or even their types,
213213
using a PK factory lets you do so.
214214

215215
You can tell the Ruby Mongo driver how to create primary keys by passing in
216-
the :pk option to the Connection#db method.
216+
the :pk option to the Client#db method.
217217

218-
db = Mongo::Connection.new('localhost', 27017, :safe => true).db('dbname', :pk => MyPKFactory.new)
218+
db = Mongo::Client.new('localhost', 27017, :safe => true).db('dbname', :pk => MyPKFactory.new)
219219

220220
A primary key factory object must respond to :create_pk, which should
221221
take a hash and return a hash which merges the original hash with any
@@ -268,7 +268,7 @@ completely harmless; strict mode is a programmer convenience only.
268268
To turn on strict mode, either pass in :strict => true when obtaining a DB
269269
object or call the `:strict=` method:
270270

271-
db = Connection.new('localhost', 27017, :safe => true).db('dbname', :strict => true)
271+
db = Client.new('localhost', 27017, :safe => true).db('dbname', :strict => true)
272272
# I'm feeling lax
273273
db.strict = false
274274
# No, I'm not!
@@ -291,10 +291,10 @@ Notes:
291291
## Socket timeouts
292292

293293
The Ruby driver support timeouts on socket read operations. To enable them, set the
294-
`:op_timeout` option when you create a `Mongo::Connection` object.
294+
`:op_timeout` option when you create a `Mongo::Client` object.
295295

296296
If implementing higher-level timeouts, using tools like `Rack::Timeout`, it's very important
297-
to call `Mongo::Connection#close` to prevent the subsequent operation from receiving the previous
297+
to call `Mongo::Client#close` to prevent the subsequent operation from receiving the previous
298298
request.
299299

300300
### Test-Unit, Shoulda, and Mocha
@@ -306,7 +306,7 @@ Running the test suite requires test-unit, shoulda, and mocha. You can install
306306
$ gem install mocha
307307

308308
The tests assume that the Mongo database is running on the default port. You
309-
can override the default host (localhost) and port (Connection::DEFAULT_PORT) by
309+
can override the default host (localhost) and port (Client::DEFAULT_PORT) by
310310
using the environment variables MONGO_RUBY_DRIVER_HOST and
311311
MONGO_RUBY_DRIVER_PORT.
312312

Rakefile

+4-4
Original file line numberDiff line numberDiff line change
@@ -150,10 +150,10 @@ namespace :test do
150150
task :drop_databases => :path do |t|
151151
puts "Dropping test databases..."
152152
require 'mongo'
153-
con = Mongo::Connection.new(ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost',
154-
ENV['MONGO_RUBY_DRIVER_PORT'] || Mongo::Connection::DEFAULT_PORT, :safe => true)
155-
con.database_names.each do |name|
156-
con.drop_database(name) if name =~ /^ruby-test/
153+
client = Mongo::Client.new(ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost',
154+
ENV['MONGO_RUBY_DRIVER_PORT'] || Mongo::Client::DEFAULT_PORT, :safe => true)
155+
client.database_names.each do |name|
156+
client.drop_database(name) if name =~ /^ruby-test/
157157
end
158158
end
159159
end

bench/batch_size_benchmark

+2-3
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,8 @@ large = {
2828
}
2929

3030

31-
con = Mongo::Connection.new
32-
33-
db = con['benchmark']
31+
client = Mongo::Client.new
32+
db = client['benchmark']
3433
col = db['bigdocs']
3534

3635
col.remove()

bench/exp_series.rb

+6-6
Original file line numberDiff line numberDiff line change
@@ -178,9 +178,9 @@ def test_multi_doc
178178
### Mac OS X - sysctl -a hw
179179

180180
def setup_test_set
181-
@conn = Mongo::Connection.new
182-
@conn.drop_database($db_name)
183-
@db = @conn.db($db_name)
181+
@client = Mongo::Client.new
182+
@client.drop_database($db_name)
183+
@db = @client.db($db_name)
184184
@coll = @db.collection($collection_name)
185185
@coll.remove
186186
@results = []
@@ -195,7 +195,7 @@ def teardown_test_set
195195
File.open($file_name, 'a'){|f|
196196
f.puts(@results.to_json.gsub(/\[/, "").gsub(/}[\],]/, "},\n")) if @results != []
197197
}
198-
@conn.drop_database($db_name)
198+
@client.drop_database($db_name)
199199
end
200200

201201
def estimate_iterations(db, coll, doc, setup, teardown)
@@ -225,9 +225,9 @@ def measure_iterations(db, coll, doc, setup, teardown, iterations)
225225
end
226226

227227
def valuate(db, coll, doc, setup, teardown)
228-
@conn.drop_database($db_name) # hack to reduce paging
228+
@client.drop_database($db_name) # hack to reduce paging
229229
iterations, etime = estimate_iterations(db, coll, doc, setup, teardown) { yield }
230-
@conn.drop_database($db_name) # hack to reduce paging
230+
@client.drop_database($db_name) # hack to reduce paging
231231
utime, rtime = measure_iterations(db, coll, doc, setup, teardown, iterations) { yield }
232232
return [iterations, utime, rtime, etime]
233233
end

bench/gridstore_benchmark

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ require 'mongo'
44

55
include Mongo
66

7-
db = Connection.new['benchmark-gridfs']
7+
db = Client.new['benchmark-gridfs']
88
sample_data = File.open(File.join(File.dirname(__FILE__), 'sample_file.pdf'), 'r').read
99
db['fs.files'].remove
1010
db['fs.chunks'].remove

bench/profile_array.rb

+2-3
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,8 @@ def ruby_prof(iterations)
7373
# system(cmd)
7474
#end
7575

76-
conn = Mongo::Connection.new
77-
78-
db = conn['benchmark']
76+
client = Mongo::Client.new
77+
db = client['benchmark']
7978
coll = db['profile']
8079

8180
coll.remove

bench/standard_benchmark

+2-2
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,9 @@ def benchmark(str, n, coll_name, data, create_index=false, verbosity=true)
8484
end
8585

8686
host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
87-
port = ENV['MONGO_RUBY_DRIVER_PORT'] || Connection::DEFAULT_PORT
87+
port = ENV['MONGO_RUBY_DRIVER_PORT'] || Client::DEFAULT_PORT
8888

89-
connection = Connection.new(host, port)
89+
connection = Client.new(host, port)
9090
connection.drop_database("benchmark")
9191
@db = connection.db('benchmark')
9292

bin/mongo_console

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ require 'mongo'
1010
include Mongo
1111

1212
host = org_argv[0] || ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
13-
port = org_argv[1] || ENV['MONGO_RUBY_DRIVER_PORT'] || Connection::DEFAULT_PORT
13+
port = org_argv[1] || ENV['MONGO_RUBY_DRIVER_PORT'] || Client::DEFAULT_PORT
1414
dbnm = org_argv[2] || ENV['MONGO_RUBY_DRIVER_DB'] || 'ruby-mongo-console'
1515

1616
puts "Connecting to #{host}:#{port} (CONN) on with database #{dbnm} (DB)"
17-
CONN = Connection.new(host, port, :safe => true)
17+
CONN = Client.new(host, port, :safe => true)
1818
DB = CONN.db(dbnm)
1919

2020
puts "Starting IRB session..."

examples/admin.rb

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,17 @@
66
include Mongo
77

88
host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
9-
port = ENV['MONGO_RUBY_DRIVER_PORT'] || Connection::DEFAULT_PORT
9+
port = ENV['MONGO_RUBY_DRIVER_PORT'] || Client::DEFAULT_PORT
1010

1111
puts "Connecting to #{host}:#{port}"
12-
con = Mongo::Connection.new(host, port, :safe => true)
13-
db = con.db('ruby-mongo-examples')
12+
client = Mongo::Client.new(host, port, :safe => true)
13+
db = client.db('ruby-mongo-examples')
1414
coll = db.create_collection('test')
1515

1616
# Erase all records from collection, if any
1717
coll.remove
1818

19-
admin = con['admin']
19+
admin = client['admin']
2020

2121
# Profiling level set/get
2222
puts "Profiling level: #{admin.profiling_level}"

examples/capped.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
include Mongo
55

66
host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
7-
port = ENV['MONGO_RUBY_DRIVER_PORT'] || Connection::DEFAULT_PORT
7+
port = ENV['MONGO_RUBY_DRIVER_PORT'] || Client::DEFAULT_PORT
88

99
puts "Connecting to #{host}:#{port}"
10-
db = Connection.new(host, port, :safe => true).db('ruby-mongo-examples')
10+
db = Client.new(host, port, :safe => true).db('ruby-mongo-examples')
1111
db.drop_collection('test')
1212

1313
# A capped collection has a max size and, optionally, a max number of records.

examples/cursor.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
include Mongo
77

88
host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
9-
port = ENV['MONGO_RUBY_DRIVER_PORT'] || Connection::DEFAULT_PORT
9+
port = ENV['MONGO_RUBY_DRIVER_PORT'] || Client::DEFAULT_PORT
1010

1111
puts "Connecting to #{host}:#{port}"
12-
db = Connection.new(host, port, :safe => true).db('ruby-mongo-examples')
12+
db = Client.new(host, port, :safe => true).db('ruby-mongo-examples')
1313
coll = db.collection('test')
1414

1515
# Erase all records from collection, if any

examples/gridfs.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ def assert
77
include Mongo
88

99
host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
10-
port = ENV['MONGO_RUBY_DRIVER_PORT'] || Connection::DEFAULT_PORT
10+
port = ENV['MONGO_RUBY_DRIVER_PORT'] || Client::DEFAULT_PORT
1111

1212
puts "Connecting to #{host}:#{port}"
13-
db = Connection.new(host, port, :safe => true).db('ruby-mongo-examples')
13+
db = Client.new(host, port, :safe => true).db('ruby-mongo-examples')
1414

1515
data = "hello, world!"
1616

examples/index_test.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
include Mongo
66

77
host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
8-
port = ENV['MONGO_RUBY_DRIVER_PORT'] || Connection::DEFAULT_PORT
8+
port = ENV['MONGO_RUBY_DRIVER_PORT'] || Client::DEFAULT_PORT
99

1010
puts ">> Connecting to #{host}:#{port}"
11-
db = Connection.new(host, port, :safe => true).db('ruby-mongo-index_test')
11+
db = Client.new(host, port, :safe => true).db('ruby-mongo-index_test')
1212

1313
class Exception
1414
def errmsg

examples/info.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
include Mongo
66

77
host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
8-
port = ENV['MONGO_RUBY_DRIVER_PORT'] || Connection::DEFAULT_PORT
8+
port = ENV['MONGO_RUBY_DRIVER_PORT'] || Client::DEFAULT_PORT
99

1010
puts "Connecting to #{host}:#{port}"
11-
db = Connection.new(host, port, :safe => true).db('ruby-mongo-examples')
11+
db = Client.new(host, port, :safe => true).db('ruby-mongo-examples')
1212
coll = db.collection('test')
1313

1414
# Erase all records from collection, if any

examples/queries.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
include Mongo
77

88
host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
9-
port = ENV['MONGO_RUBY_DRIVER_PORT'] || Connection::DEFAULT_PORT
9+
port = ENV['MONGO_RUBY_DRIVER_PORT'] || Client::DEFAULT_PORT
1010

1111
puts "Connecting to #{host}:#{port}"
12-
db = Connection.new(host, port, :safe => true).db('ruby-mongo-examples')
12+
db = Client.new(host, port, :safe => true).db('ruby-mongo-examples')
1313
coll = db.collection('test')
1414

1515
# Remove all records, if any

examples/replica_set.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
cons = []
55

66
10.times do
7-
cons << Mongo::ReplSetConnection(['localhost:27017'], :read => :secondary)
7+
cons << Mongo::ReplSetClient(['localhost:27017'], :read => :secondary)
88
end
99

1010
ports = cons.map do |con|

examples/simple.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
include Mongo
77

88
host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
9-
port = ENV['MONGO_RUBY_DRIVER_PORT'] || Connection::DEFAULT_PORT
9+
port = ENV['MONGO_RUBY_DRIVER_PORT'] || Client::DEFAULT_PORT
1010

1111
puts "Connecting to #{host}:#{port}"
12-
db = Connection.new(host, port, :safe => true).db('ruby-mongo-examples')
12+
db = Client.new(host, port, :safe => true).db('ruby-mongo-examples')
1313
coll = db.collection('test')
1414

1515
# Erase all records from collection, if any

examples/strict.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
include Mongo
66

77
host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
8-
port = ENV['MONGO_RUBY_DRIVER_PORT'] || Connection::DEFAULT_PORT
8+
port = ENV['MONGO_RUBY_DRIVER_PORT'] || Client::DEFAULT_PORT
99

1010
puts "Connecting to #{host}:#{port}"
11-
db = Connection.new(host, port, :safe => true).db('ruby-mongo-examples')
11+
db = Client.new(host, port, :safe => true).db('ruby-mongo-examples')
1212

1313
db.drop_collection('does-not-exist')
1414
db.create_collection('test')

examples/types.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
include Mongo
77

88
host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
9-
port = ENV['MONGO_RUBY_DRIVER_PORT'] || Connection::DEFAULT_PORT
9+
port = ENV['MONGO_RUBY_DRIVER_PORT'] || Client::DEFAULT_PORT
1010

1111
puts "Connecting to #{host}:#{port}"
12-
db = Connection.new(host, port, :safe => true).db('ruby-mongo-examples')
12+
db = Client.new(host, port, :safe => true).db('ruby-mongo-examples')
1313
coll = db.collection('test')
1414

1515
# Remove all records, if any

examples/web/thin/load.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
require File.join(File.dirname(__FILE__), '..', '..', '..', 'lib', 'mongo')
22
require 'logger'
33

4-
$con = Mongo::ReplSetConnection.new(['localhost:30000', 'localhost:30001'], :safe => true, :read => :secondary, :refresh_mode => :sync, :refresh_interval => 30)
4+
$con = Mongo::ReplSetClient.new(['localhost:30000', 'localhost:30001'], :safe => true, :read => :secondary, :refresh_mode => :sync, :refresh_interval => 30)
55
$db = $con['foo']
66

77
class Load < Sinatra::Base

examples/web/unicorn/load.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
require File.join(File.dirname(__FILE__), '..', '..', 'lib', 'mongo')
22

3-
$con = Mongo::Connection.new('localhost', 27017, :safe => true)
4-
$db = $con['foo']
3+
$client = Mongo::Client.new('localhost', 27017, :safe => true)
4+
$db = $client['foo']
55

66
class Load < Sinatra::Base
77

examples/web/unicorn/unicorn.rb.template

+3-3
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ stdout_path "#{@dir}log/unicorn.stdout.log"
2222

2323
# NOTE: You need this when using forking web servers!
2424
after_fork do |server, worker|
25-
$con.close if $con
26-
$con = Mongo::Connection.new('localhost', 27017, :safe => true)
27-
$db = $con['foo']
25+
$client.close if $client
26+
$client = Mongo::Client.new('localhost', 27017, :safe => true)
27+
$db = $client['foo']
2828
STDERR << "FORKED #{server} #{worker}"
2929
end

0 commit comments

Comments
 (0)