Skip to content

Commit

Permalink
RUBY-494 new write-concern defaults and 'w' parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
TylerBrock authored and Brandon Black committed Nov 18, 2012
1 parent 43cef18 commit f91a41d
Show file tree
Hide file tree
Showing 79 changed files with 1,381 additions and 2,006 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1,5 +1,6 @@
docs
.yardoc
coverage
*.gem
nbproject
*.bundle
Expand Down
2 changes: 1 addition & 1 deletion .travis.yml
Expand Up @@ -12,7 +12,7 @@ script: bundle exec rake test:ruby

notifications:
email: false
#TODO: flowdock: [api token]
flowdock: 1da4416b8ff98d1880986472428b1b1b

services:
- mongodb
Expand Down
1 change: 1 addition & 0 deletions Gemfile
Expand Up @@ -14,6 +14,7 @@ group :deploy do
end

group :testing do
gem 'simplecov'
gem 'test-unit'
gem 'mocha', '0.12.7'
gem 'shoulda'
Expand Down
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -45,7 +45,7 @@ Here's a quick code sample. Again, see the [MongoDB Ruby Tutorial](https://githu
require 'rubygems'
require 'mongo'

@client = Mongo::Client.new('localhost', 27017, :safe => true)
@client = Mongo::Client.new('localhost', 27017)
@db = @client['sample-db']
@coll = @db['test']

Expand Down
2 changes: 1 addition & 1 deletion bin/mongo_console
Expand Up @@ -14,7 +14,7 @@ port = org_argv[1] || ENV['MONGO_RUBY_DRIVER_PORT'] || Client::DEFAULT_PORT
dbnm = org_argv[2] || ENV['MONGO_RUBY_DRIVER_DB'] || 'ruby-mongo-console'

puts "Connecting to #{host}:#{port} (CLIENT) on with database #{dbnm} (DB)"
CLIENT = Client.new(host, port, :safe => true)
CLIENT = Client.new(host, port)
DB = CLIENT.db(dbnm)

puts "Starting IRB session..."
Expand Down
2 changes: 1 addition & 1 deletion examples/admin.rb
Expand Up @@ -9,7 +9,7 @@
port = ENV['MONGO_RUBY_DRIVER_PORT'] || Client::DEFAULT_PORT

puts "Connecting to #{host}:#{port}"
client = Mongo::Client.new(host, port, :safe => true)
client = Mongo::Client.new(host, port)
db = client.db('ruby-mongo-examples')
coll = db.create_collection('test')

Expand Down
2 changes: 1 addition & 1 deletion examples/capped.rb
Expand Up @@ -7,7 +7,7 @@
port = ENV['MONGO_RUBY_DRIVER_PORT'] || Client::DEFAULT_PORT

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

# A capped collection has a max size and, optionally, a max number of records.
Expand Down
2 changes: 1 addition & 1 deletion examples/cursor.rb
Expand Up @@ -9,7 +9,7 @@
port = ENV['MONGO_RUBY_DRIVER_PORT'] || Client::DEFAULT_PORT

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

# Erase all records from collection, if any
Expand Down
2 changes: 1 addition & 1 deletion examples/gridfs.rb
Expand Up @@ -10,7 +10,7 @@ def assert
port = ENV['MONGO_RUBY_DRIVER_PORT'] || Client::DEFAULT_PORT

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

data = "hello, world!"

Expand Down
2 changes: 1 addition & 1 deletion examples/index_test.rb
Expand Up @@ -8,7 +8,7 @@
port = ENV['MONGO_RUBY_DRIVER_PORT'] || Client::DEFAULT_PORT

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

class Exception
def errmsg
Expand Down
2 changes: 1 addition & 1 deletion examples/info.rb
Expand Up @@ -8,7 +8,7 @@
port = ENV['MONGO_RUBY_DRIVER_PORT'] || Client::DEFAULT_PORT

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

# Erase all records from collection, if any
Expand Down
2 changes: 1 addition & 1 deletion examples/queries.rb
Expand Up @@ -9,7 +9,7 @@
port = ENV['MONGO_RUBY_DRIVER_PORT'] || Client::DEFAULT_PORT

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

# Remove all records, if any
Expand Down
2 changes: 1 addition & 1 deletion examples/simple.rb
Expand Up @@ -9,7 +9,7 @@
port = ENV['MONGO_RUBY_DRIVER_PORT'] || Client::DEFAULT_PORT

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

# Erase all records from collection, if any
Expand Down
2 changes: 1 addition & 1 deletion examples/strict.rb
Expand Up @@ -8,7 +8,7 @@
port = ENV['MONGO_RUBY_DRIVER_PORT'] || Client::DEFAULT_PORT

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

db.drop_collection('does-not-exist')
db.create_collection('test')
Expand Down
2 changes: 1 addition & 1 deletion examples/types.rb
Expand Up @@ -9,7 +9,7 @@
port = ENV['MONGO_RUBY_DRIVER_PORT'] || Client::DEFAULT_PORT

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

# Remove all records, if any
Expand Down
2 changes: 1 addition & 1 deletion examples/web/thin/load.rb
@@ -1,7 +1,7 @@
require File.join(File.dirname(__FILE__), '..', '..', '..', 'lib', 'mongo')
require 'logger'

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

class Load < Sinatra::Base
Expand Down
2 changes: 1 addition & 1 deletion examples/web/unicorn/load.rb
@@ -1,6 +1,6 @@
require File.join(File.dirname(__FILE__), '..', '..', 'lib', 'mongo')

$client = Mongo::Client.new('localhost', 27017, :safe => true)
$client = Mongo::Client.new('localhost', 27017)
$db = $client['foo']

class Load < Sinatra::Base
Expand Down
2 changes: 1 addition & 1 deletion examples/web/unicorn/unicorn.rb.template
Expand Up @@ -23,7 +23,7 @@ stdout_path "#{@dir}log/unicorn.stdout.log"
# NOTE: You need this when using forking web servers!
after_fork do |server, worker|
$client.close if $client
$client = Mongo::Client.new('localhost', 27017, :safe => true)
$client = Mongo::Client.new('localhost', 27017)
$db = $client['foo']
STDERR << "FORKED #{server} #{worker}"
end
3 changes: 0 additions & 3 deletions lib/bson/version.rb

This file was deleted.

6 changes: 4 additions & 2 deletions lib/mongo.rb
Expand Up @@ -52,6 +52,7 @@ module Constants

require 'mongo/util/conversions'
require 'mongo/util/support'
require 'mongo/util/write_concern'
require 'mongo/util/core_ext'
require 'mongo/util/logging'
require 'mongo/util/node'
Expand All @@ -63,15 +64,16 @@ module Constants
require 'mongo/util/tcp_socket'
require 'mongo/util/uri_parser'

require 'mongo/collection'

require 'mongo/networking'
require 'mongo/client'
require 'mongo/repl_set_client'
require 'mongo/sharded_client'
require 'mongo/legacy'
require 'mongo/collection'
require 'mongo/cursor'
require 'mongo/db'
require 'mongo/exceptions'
require 'mongo/legacy'
require 'mongo/gridfs/grid_ext'
require 'mongo/gridfs/grid'
require 'mongo/gridfs/grid_io'
Expand Down
77 changes: 49 additions & 28 deletions lib/mongo/client.rb
Expand Up @@ -19,28 +19,44 @@
require 'set'
require 'socket'
require 'thread'

module Mongo

# Instantiates and manages self.connections to MongoDB.
class Client
include Mongo::Logging
include Mongo::Networking
include Mongo::WriteConcern

TCPSocket = Mongo::TCPSocket
Mutex = ::Mutex
ConditionVariable = ::ConditionVariable
TCPSocket = Mongo::TCPSocket
Mutex = ::Mutex
ConditionVariable = ::ConditionVariable

DEFAULT_HOST = 'localhost'
DEFAULT_PORT = 27017
DEFAULT_DB_NAME = 'test'
GENERIC_OPTS = [:ssl, :auths, :pool_size, :pool_timeout, :timeout, :op_timeout, :connect_timeout, :safe, :logger, :connect]
CONNECTION_OPTS = [:slave_ok]
DEFAULT_HOST = 'localhost'
DEFAULT_PORT = 27017
DEFAULT_DB_NAME = 'test'
GENERIC_OPTS = [:ssl, :auths, :logger, :connect]
TIMEOUT_OPTS = [:timeout, :op_timeout, :connect_timeout]
POOL_OPTS = [:pool_size, :pool_timeout]
WRITE_CONCERN_OPTS = [:w, :j, :fsync, :wtimeout]
CLIENT_ONLY_OPTS = [:slave_ok]

mongo_thread_local_accessor :connections

attr_reader :logger, :size, :auths, :primary, :safe, :host_to_try,
:pool_size, :connect_timeout, :pool_timeout,
:primary_pool, :socket_class, :op_timeout, :tag_sets, :acceptable_latency
attr_reader :logger,
:size,
:auths,
:primary,
:write_concern,
:host_to_try,
:pool_size,
:connect_timeout,
:pool_timeout,
:primary_pool,
:socket_class,
:op_timeout,
:tag_sets,
:acceptable_latency

# Create a connection to single MongoDB instance.
#
Expand All @@ -59,10 +75,10 @@ class Client
# @param [String, Hash] host
# @param [Integer] port specify a port number here if only one host is being specified.
#
# @option opts [Boolean, Hash] :safe (false) Set the default safe-mode options
# propagated to DB objects instantiated off of this Client. This
# default can be overridden upon instantiation of any DB by explicitly setting a :safe value
# on initialization.
# @option opts [Hash] :w (1), :j (false), :wtimeout (false), :fsync (false) Set the default write concern
# options propagated to DB objects instantiated off of this Client.
# This default can be overridden upon instantiation of any DB by explicitly setting an options hash
# on initialization. It can also be overridden at instantiation of a collection or at the time of a write operation.
# @option opts [Boolean] :slave_ok (false) Must be set to +true+ when connecting
# to a single, slave node.
# @option opts [Logger, #debug] :logger (nil) A Logger instance for debugging driver ops. Note that
Expand Down Expand Up @@ -104,7 +120,7 @@ def initialize(host=nil, port=nil, opts={})
if parser.replicaset?
raise MongoArgumentError, "Mongo::Client.new called with no arguments, but ENV['MONGODB_URI'] implies a replica set."
end
opts = parser.connection_options.merge! opts
opts.merge!(parser.connection_options)
@host_to_try = [parser.host, parser.port]
elsif host.is_a?(String) && (port || DEFAULT_PORT).respond_to?(:to_i)
@host_to_try = [host, (port || DEFAULT_PORT).to_i]
Expand Down Expand Up @@ -169,7 +185,8 @@ def self.multi(nodes, opts={})

# Initialize a connection to MongoDB using the MongoDB URI spec.
#
# Since Client.new cannot be used with any <code>ENV["MONGODB_URI"]</code> that has multiple hosts (implying a replicaset), you may use this when the type of your connection varies by environment and should be determined solely from <code>ENV["MONGODB_URI"]</code>.
# Since Client.new cannot be used with any <code>ENV["MONGODB_URI"]</code> that has multiple hosts (implying a replicaset),
# you may use this when the type of your connection varies by environment and should be determined solely from <code>ENV["MONGODB_URI"]</code>.
#
# @param uri [String]
# A string of the format mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/database]
Expand Down Expand Up @@ -530,7 +547,11 @@ def checkin(socket)
protected

def valid_opts
GENERIC_OPTS + CONNECTION_OPTS
GENERIC_OPTS +
CLIENT_ONLY_OPTS +
POOL_OPTS +
WRITE_CONCERN_OPTS +
TIMEOUT_OPTS
end

def check_opts(opts)
Expand All @@ -544,38 +565,38 @@ def check_opts(opts)
# Parse option hash
def setup(opts)
# slave_ok can be true only if one node is specified
@slave_ok = opts[:slave_ok]
@slave_ok = opts.delete(:slave_ok)

# Determine whether to use SSL.
@ssl = opts.fetch(:ssl, false)
@ssl = opts.delete(:ssl)
if @ssl
@socket_class = Mongo::SSLSocket
else
@socket_class = Mongo::TCPSocket
end

# Authentication objects
@auths = opts.fetch(:auths, [])
@auths = opts.delete(:auths) || []

# Pool size and timeout.
@pool_size = opts[:pool_size] || 1
@pool_size = opts.delete(:pool_size) || 1
if opts[:timeout]
warn "The :timeout option has been deprecated " +
"and will be removed in the 2.0 release. Use :pool_timeout instead."
end
@pool_timeout = opts[:pool_timeout] || opts[:timeout] || 5.0
@pool_timeout = opts.delete(:pool_timeout) || opts.delete(:timeout) || 5.0

# Timeout on socket read operation.
@op_timeout = opts[:op_timeout] || nil
@op_timeout = opts.delete(:op_timeout) || nil

# Timeout on socket connect.
@connect_timeout = opts[:connect_timeout] || nil

# Global safe option. This is false by default.
@safe = opts[:safe] || false
@connect_timeout = opts.delete(:connect_timeout) || nil

@logger = opts.fetch(:logger, nil)

# Connection level write concern options.
@write_concern = get_write_concern(opts)

if @logger
write_logging_startup_message
end
Expand Down

0 comments on commit f91a41d

Please sign in to comment.