lib/jmongo/connection.rb in jmongo-1.0.3 vs lib/jmongo/connection.rb in jmongo-1.1.0
- old
+ new
@@ -18,27 +18,34 @@
include Mongo::JavaImpl::Utils
extend Mongo::JavaImpl::NoImplYetClass
include Mongo::JavaImpl::Connection_::InstanceMethods
extend Mongo::JavaImpl::Connection_::ClassMethods
- attr_reader :connection, :logger, :auths
+ attr_reader :connection, :connector, :logger, :auths, :primary
+ DEFAULT_PORT = 27017
+
def initialize host = nil, port = nil, opts = {}
if opts.has_key?(:new_from_uri)
- @connection = opts[:new_from_uri]
+ @options = opts
+ @mongo_uri = opts[:new_from_uri]
+ @connection = JMongo::Mongo.new(@mongo_uri)
else
@logger = opts[:logger]
@host = host || 'localhost'
@port = port || 27017
- server_address = JMongo::ServerAddress.new @host, @port
- options = JMongo::MongoOptions.new
- options.connectionsPerHost = opts[:pool_size] || 1
- options.socketTimeout = opts[:timeout].to_i * 1000 || 5000
- @connection = JMongo::Mongo.new(server_address, options)
+ @server_address = JMongo::ServerAddress.new @host, @port
+ @options = JMongo::MongoOptions.new
+ @options.connectionsPerHost = opts[:pool_size] || 1
+ @options.socketTimeout = opts[:timeout].to_i * 1000 || 5000
+ @connection = JMongo::Mongo.new(@server_address, @options)
end
+ @connector = @connection.connector
@logger = opts[:logger]
@auths = opts.fetch(:auths, [])
+ add = @connector.address
+ @primary = [add.host, add.port]
end
def self.paired(nodes, opts={})
raise_not_implemented
end
@@ -145,11 +152,11 @@
#
# @return [Mongo::DB]
#
# @core databases []-instance_method
def [](db_name)
- DB.new db_name, self
+ db db_name
end
# Drop a database.
#
# @param [String] name name of an existing database.
@@ -174,23 +181,30 @@
# return [Integer]
def get_request_id
raise_not_implemented
end
+ # Checks if a server is alive. This command will return immediately
+ # even if the server is in a lock.
+ #
+ # @return [Hash]
+ def ping
+ db("admin").command('ping')
+ end
# Get the build information for the current connection.
#
# @return [Hash]
def server_info
- raise_not_implemented
+ db("admin").command('buildinfo')
end
# Get the build version of the current server.
#
# @return [Mongo::ServerVersion]
# object allowing easy comparability of version.
def server_version
- _server_version
+ ServerVersion.new(server_info["version"])
end
# Is it okay to connect to a slave?
#
# @return [Boolean]
@@ -228,18 +242,30 @@
def receive_message(operation, message, log_message=nil, socket=nil)
raise_not_implemented
end
def connect_to_master
- raise_not_implemented
+ connect
end
def connected?
- raise_not_implemented
+ @connection && @connector && @connector.is_open
end
+ def connect
+ close
+ if @mongo_uri
+ @connection = JMongo::Mongo.new(@mongo_uri)
+ else
+ @connection = JMongo::Mongo.new(@server_address, @options)
+ end
+ @connector = @connection.connector
+ end
+ alias :reconnect :connect
+
def close
- raise_not_implemented
+ @connection.close if @connection
+ @connection = @connector = nil
end
end # class Connection
end # module Mongo