Class: Mongo::Connection
- Extended by:
- JavaImpl::Connection_::ClassMethods, JavaImpl::NoImplYetClass
- Defined in:
- lib/jmongo/connection.rb
Constant Summary collapse
- DEFAULT_PORT =
27017
Constants included from JavaImpl::Connection_::ClassMethods
JavaImpl::Connection_::ClassMethods::OPTS_KEYS, JavaImpl::Connection_::ClassMethods::URI_RE
Constants included from JavaImpl::Utils
Instance Attribute Summary collapse
-
#auths ⇒ Object
readonly
Returns the value of attribute auths.
-
#connection ⇒ Object
readonly
Returns the value of attribute connection.
-
#connector ⇒ Object
readonly
Returns the value of attribute connector.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#primary ⇒ Object
readonly
Returns the value of attribute primary.
Class Method Summary collapse
-
.from_uri(uri, opts = {}) ⇒ Mongo::Connection
Initialize a connection to MongoDB using the MongoDB URI spec:.
- .paired(nodes, opts = {}) ⇒ Object
Instance Method Summary collapse
-
#[](db_name) ⇒ Mongo::DB
Shortcut for returning a database.
-
#add_auth(db_name, username, password) ⇒ Hash
Save an authentication to this connection.
-
#apply_saved_authentication ⇒ Boolean
Apply each of the saved database authentications.
-
#clear_auths ⇒ true
Remove all authenication information stored in this connection.
- #close ⇒ Object
- #connect ⇒ Object (also: #reconnect)
- #connect_to_master ⇒ Object
- #connected? ⇒ Boolean
-
#copy_database(from, to, from_host = "localhost", username = nil, password = nil) ⇒ Object
Copy the database
from
toto
on localhost. -
#database_info ⇒ Hash
Return a hash with all database names and their respective sizes on disk.
-
#database_names ⇒ Array
Return an array of database names.
-
#db(db_name, options = {}) ⇒ Mongo::DB
Return a database with the given name.
-
#drop_database(name) ⇒ Object
Drop a database.
-
#get_request_id ⇒ Object
Increment and return the next available request id.
-
#initialize(host = nil, port = nil, opts = {}) ⇒ Connection
constructor
A new instance of Connection.
- #log_operation(name, payload) ⇒ Object
-
#ping ⇒ Hash
Checks if a server is alive.
- #receive_message(operation, message, log_message = nil, socket = nil) ⇒ Object
-
#remove_auth(db_name) ⇒ Boolean
Remove a saved authentication for this connection.
-
#send_message(operation, message, log_message = nil) ⇒ True
Send a message to MongoDB, adding the necessary headers.
- #send_message_with_safe_check(operation, message, db_name, log_message = nil) ⇒ Object
-
#server_info ⇒ Hash
Get the build information for the current connection.
-
#server_version ⇒ Mongo::ServerVersion
Get the build version of the current server.
-
#slave_ok? ⇒ Boolean
Is it okay to connect to a slave?.
Methods included from JavaImpl::NoImplYetClass
Methods included from JavaImpl::Connection_::ClassMethods
Methods included from JavaImpl::Utils
#from_dbobject, #prep_fields, #prep_sort, #raise_not_implemented, #sort_value, #to_dbobject, #trap_raise
Constructor Details
#initialize(host = nil, port = nil, opts = {}) ⇒ Connection
Returns a new instance of Connection.
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/jmongo/connection.rb', line 27 def initialize host = nil, port = nil, opts = {} if opts.has_key?(: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) end @connector = @connection.connector @logger = opts[:logger] @auths = opts.fetch(:auths, []) add = @connector.address @primary = [add.host, add.port] end |
Instance Attribute Details
#auths ⇒ Object (readonly)
Returns the value of attribute auths.
23 24 25 |
# File 'lib/jmongo/connection.rb', line 23 def auths @auths end |
#connection ⇒ Object (readonly)
Returns the value of attribute connection.
23 24 25 |
# File 'lib/jmongo/connection.rb', line 23 def connection @connection end |
#connector ⇒ Object (readonly)
Returns the value of attribute connector.
23 24 25 |
# File 'lib/jmongo/connection.rb', line 23 def connector @connector end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
23 24 25 |
# File 'lib/jmongo/connection.rb', line 23 def logger @logger end |
#primary ⇒ Object (readonly)
Returns the value of attribute primary.
23 24 25 |
# File 'lib/jmongo/connection.rb', line 23 def primary @primary end |
Class Method Details
.from_uri(uri, opts = {}) ⇒ Mongo::Connection
Initialize a connection to MongoDB using the MongoDB URI spec:
61 62 63 |
# File 'lib/jmongo/connection.rb', line 61 def self.from_uri(uri, opts={}) _from_uri(uri,opts) end |
.paired(nodes, opts = {}) ⇒ Object
49 50 51 |
# File 'lib/jmongo/connection.rb', line 49 def self.paired(nodes, opts={}) raise_not_implemented end |
Instance Method Details
#[](db_name) ⇒ Mongo::DB
Shortcut for returning a database. Use DB#db to accept options.
156 157 158 |
# File 'lib/jmongo/connection.rb', line 156 def [](db_name) db db_name end |
#add_auth(db_name, username, password) ⇒ Hash
Save an authentication to this connection. When connecting, the connection will attempt to re-authenticate on every db specificed in the list of auths. This method is called automatically by DB#authenticate.
90 91 92 93 94 95 96 97 98 |
# File 'lib/jmongo/connection.rb', line 90 def add_auth(db_name, username, password) remove_auth(db_name) auth = {} auth['db_name'] = db_name auth['username'] = username auth['password'] = password @auths << auth auth end |
#apply_saved_authentication ⇒ Boolean
Apply each of the saved database authentications.
72 73 74 75 76 77 78 |
# File 'lib/jmongo/connection.rb', line 72 def apply_saved_authentication return false if @auths.empty? @auths.each do |auth| self[auth['db_name']].authenticate(auth['username'], auth['password'], false) end true end |
#clear_auths ⇒ true
Remove all authenication information stored in this connection.
117 118 119 120 |
# File 'lib/jmongo/connection.rb', line 117 def clear_auths @auths = [] true end |
#close ⇒ Object
265 266 267 268 |
# File 'lib/jmongo/connection.rb', line 265 def close @connection.close if @connection @connection = @connector = nil end |
#connect ⇒ Object Also known as: reconnect
254 255 256 257 258 259 260 261 262 |
# File 'lib/jmongo/connection.rb', line 254 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 |
#connect_to_master ⇒ Object
246 247 248 |
# File 'lib/jmongo/connection.rb', line 246 def connect_to_master connect end |
#connected? ⇒ Boolean
250 251 252 |
# File 'lib/jmongo/connection.rb', line 250 def connected? @connection && @connector && @connector.is_open end |
#copy_database(from, to, from_host = "localhost", username = nil, password = nil) ⇒ Object
Copy the database from
to to
on localhost. The from
database is assumed to be on localhost, but an alternate host can be specified.
175 176 177 |
# File 'lib/jmongo/connection.rb', line 175 def copy_database(from, to, from_host="localhost", username=nil, password=nil) raise_not_implemented end |
#database_info ⇒ Hash
Return a hash with all database names and their respective sizes on disk.
126 127 128 |
# File 'lib/jmongo/connection.rb', line 126 def database_info raise_not_implemented end |
#database_names ⇒ Array
Return an array of database names.
133 134 135 |
# File 'lib/jmongo/connection.rb', line 133 def database_names get_db_names end |
#db(db_name, options = {}) ⇒ Mongo::DB
Return a database with the given name. See DB#new for valid options hash parameters.
145 146 147 |
# File 'lib/jmongo/connection.rb', line 145 def db(db_name, ={}) DB.new db_name, self, end |
#drop_database(name) ⇒ Object
Drop a database.
163 164 165 |
# File 'lib/jmongo/connection.rb', line 163 def drop_database(name) drop_a_db name end |
#get_request_id ⇒ Object
Increment and return the next available request id.
return [Integer]
182 183 184 |
# File 'lib/jmongo/connection.rb', line 182 def get_request_id raise_not_implemented end |
#log_operation(name, payload) ⇒ Object
215 216 217 218 219 220 221 222 223 |
# File 'lib/jmongo/connection.rb', line 215 def log_operation(name, payload) return unless @logger msg = "#{payload[:database]}['#{payload[:collection]}'].#{name}(" msg += payload.values_at(:selector, :document, :documents, :fields ).compact.map(&:inspect).join(', ') + ")" msg += ".skip(#{payload[:skip]})" if payload[:skip] msg += ".limit(#{payload[:limit]})" if payload[:limit] msg += ".sort(#{payload[:order]})" if payload[:order] @logger.debug "MONGODB #{msg}" end |
#ping ⇒ Hash
Checks if a server is alive. This command will return immediately even if the server is in a lock.
190 191 192 |
# File 'lib/jmongo/connection.rb', line 190 def ping db("admin").command('ping') end |
#receive_message(operation, message, log_message = nil, socket = nil) ⇒ Object
242 243 244 |
# File 'lib/jmongo/connection.rb', line 242 def (operation, , =nil, socket=nil) raise_not_implemented end |
#remove_auth(db_name) ⇒ Boolean
Remove a saved authentication for this connection.
105 106 107 108 109 110 111 112 |
# File 'lib/jmongo/connection.rb', line 105 def remove_auth(db_name) return unless @auths if @auths.reject! { |a| a['db_name'] == db_name } true else false end end |
#send_message(operation, message, log_message = nil) ⇒ True
Send a message to MongoDB, adding the necessary headers.
234 235 236 |
# File 'lib/jmongo/connection.rb', line 234 def (operation, , =nil) raise_not_implemented end |
#send_message_with_safe_check(operation, message, db_name, log_message = nil) ⇒ Object
238 239 240 |
# File 'lib/jmongo/connection.rb', line 238 def (operation, , db_name, =nil) raise_not_implemented end |
#server_info ⇒ Hash
Get the build information for the current connection.
196 197 198 |
# File 'lib/jmongo/connection.rb', line 196 def server_info db("admin").command('buildinfo') end |
#server_version ⇒ Mongo::ServerVersion
Get the build version of the current server.
204 205 206 |
# File 'lib/jmongo/connection.rb', line 204 def server_version ServerVersion.new(server_info["version"]) end |
#slave_ok? ⇒ Boolean
Is it okay to connect to a slave?
211 212 213 |
# File 'lib/jmongo/connection.rb', line 211 def slave_ok? raise_not_implemented end |