Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Initialize MongoShardedClient with MONGODB_URI. #160

Merged
merged 1 commit into from

2 participants

@durran
Collaborator

This fixes the errors that were occurring when instantiating a
MongoShardedClient when a MONGODB_URI environment variable is present.
This also fixes MongoShardedClient.from_uri to properly get a
MongoShardedClient connection from the URIParser.

[ RUBY-541 ]

@durran durran Initialize MongoShardedClient with MONGODB_URI.
This fixes the errors that were occurring when instantiating a
MongoShardedClient when a MONGODB_URI environment variable is present.
This also fixes MongoShardedClient.from_uri to properly get a
MongoShardedClient connection from the URIParser.

[ RUBY-541 ]
387686f
@brandonblack

The build failures aren't related. This looks good. Ship it.

@durran durran merged commit 387686f into from
@TylerBrock TylerBrock deleted the branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Feb 11, 2013
  1. @durran

    Initialize MongoShardedClient with MONGODB_URI.

    durran authored
    This fixes the errors that were occurring when instantiating a
    MongoShardedClient when a MONGODB_URI environment variable is present.
    This also fixes MongoShardedClient.from_uri to properly get a
    MongoShardedClient connection from the URIParser.
    
    [ RUBY-541 ]
This page is out of date. Refresh to see the latest.
View
19 lib/mongo/mongo_sharded_client.rb
@@ -16,11 +16,8 @@ def initialize(*args)
if nodes.empty? and ENV.has_key?('MONGODB_URI')
parser = URIParser.new ENV['MONGODB_URI']
- if parser.direct?
- raise MongoArgumentError, "Mongo::MongoShardedClient.new called with no arguments, but ENV['MONGODB_URI'] implies a direct connection."
- end
opts = parser.connection_options.merge! opts
- nodes = [parser.nodes]
+ nodes = parser.node_strings
end
unless nodes.length > 0
@@ -61,7 +58,7 @@ def initialize(*args)
end
def valid_opts
- GENERIC_OPTS + SHARDED_CLUSTER_OPTS
+ GENERIC_OPTS + SHARDED_CLUSTER_OPTS + READ_PREFERENCE_OPTS + WRITE_CONCERN_OPTS
end
def inspect
@@ -135,5 +132,17 @@ def checkout(&block)
tries < 2 ? retry : raise
end
end
+
+ # Initialize a connection to MongoDB using the MongoDB URI spec.
+ #
+ # @param uri [ String ] string of the format:
+ # mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/database]
+ #
+ # @param opts [ Hash ] Any of the options available for MongoShardedClient.new
+ #
+ # @return [ Mongo::MongoShardedClient ] The sharded client.
+ def self.from_uri(uri = ENV['MONGODB_URI'], options = {})
+ URIParser.new(uri).connection(options, false, true)
+ end
end
end
View
8 lib/mongo/util/uri_parser.rb
@@ -140,8 +140,8 @@ def initialize(uri)
# @note Don't confuse this with attribute getter method #connect.
#
# @return [MongoClient,MongoReplicaSetClient]
- def connection(extra_opts, legacy=false)
- opts = connection_options.merge! extra_opts
+ def connection(extra_opts, legacy = false, sharded = false)
+ opts = connection_options.merge!(extra_opts)
if(legacy)
if replicaset?
ReplSetConnection.new(node_strings, opts)
@@ -149,7 +149,9 @@ def connection(extra_opts, legacy=false)
Connection.new(host, port, opts)
end
else
- if replicaset?
+ if sharded
+ MongoShardedClient.new(node_strings, opts)
+ elsif replicaset?
MongoReplicaSetClient.new(node_strings, opts)
else
MongoClient.new(host, port, opts)
View
23 test/functional/uri_test.rb
@@ -174,4 +174,27 @@ def test_read_preference_when_no_replica_set
parser = Mongo::URIParser.new("mongodb://localhost:27018?readPreference=nearest")
assert_nil parser.connection_options[:read]
end
+
+ def test_connection_when_sharded_with_no_options
+ parser = Mongo::URIParser.new("mongodb://localhost:27017,localhost:27018")
+ client = parser.connection({}, false, true)
+ assert_equal [[ "localhost", 27017 ], [ "localhost", 27018 ]], client.seeds
+ assert_true client.mongos?
+ end
+
+ def test_connection_when_sharded_with_options
+ parser = Mongo::URIParser.new("mongodb://localhost:27017,localhost:27018")
+ client = parser.connection({ :refresh_interval => 10 }, false, true)
+ assert_equal [[ "localhost", 27017 ], [ "localhost", 27018 ]], client.seeds
+ assert_equal 10, client.refresh_interval
+ assert_true client.mongos?
+ end
+
+ def test_connection_when_sharded_with_uri_options
+ parser = Mongo::URIParser.new("mongodb://localhost:27017,localhost:27018?readPreference=nearest")
+ client = parser.connection({}, false, true)
+ assert_equal [[ "localhost", 27017 ], [ "localhost", 27018 ]], client.seeds
+ assert_equal :nearest, client.read
+ assert_true client.mongos?
+ end
end
View
32 test/unit/mongo_sharded_client_test.rb
@@ -0,0 +1,32 @@
+require "test_helper"
+
+class MongoShardedClientTest < Test::Unit::TestCase
+ include Mongo
+
+ def setup
+ ENV["MONGODB_URI"] = nil
+ end
+
+ def test_initialize_with_single_mongos_uri
+ ENV["MONGODB_URI"] = "mongodb://localhost:27017"
+ client = MongoShardedClient.new
+ assert_equal [[ "localhost", 27017 ]], client.seeds
+ end
+
+ def test_initialize_with_multiple_mongos_uris
+ ENV["MONGODB_URI"] = "mongodb://localhost:27017,localhost:27018"
+ client = MongoShardedClient.new
+ assert_equal [[ "localhost", 27017 ], [ "localhost", 27018 ]], client.seeds
+ end
+
+ def test_from_uri_with_string
+ client = MongoShardedClient.from_uri("mongodb://localhost:27017,localhost:27018")
+ assert_equal [[ "localhost", 27017 ], [ "localhost", 27018 ]], client.seeds
+ end
+
+ def test_from_uri_with_env_variable
+ ENV["MONGODB_URI"] = "mongodb://localhost:27017,localhost:27018"
+ client = MongoShardedClient.from_uri
+ assert_equal [[ "localhost", 27017 ], [ "localhost", 27018 ]], client.seeds
+ end
+end
Something went wrong with that request. Please try again.