2008-12-17 16:49:06 +00:00
|
|
|
# --
|
2009-01-06 15:51:01 +00:00
|
|
|
# Copyright (C) 2008-2009 10gen Inc.
|
2008-11-22 01:00:51 +00:00
|
|
|
#
|
2009-02-15 13:24:14 +00:00
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
2008-11-22 01:00:51 +00:00
|
|
|
#
|
2009-02-15 13:24:14 +00:00
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
2008-11-22 01:00:51 +00:00
|
|
|
#
|
2009-02-15 13:24:14 +00:00
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
2008-12-17 16:49:06 +00:00
|
|
|
# ++
|
2008-11-22 01:00:51 +00:00
|
|
|
|
|
|
|
require 'socket'
|
2009-11-20 22:48:41 +00:00
|
|
|
require 'timeout'
|
2009-01-21 16:52:43 +00:00
|
|
|
require 'digest/md5'
|
2009-10-19 15:46:59 +00:00
|
|
|
require 'thread'
|
2008-11-22 01:00:51 +00:00
|
|
|
|
2009-08-20 14:50:48 +00:00
|
|
|
module Mongo
|
2009-01-16 21:10:52 +00:00
|
|
|
|
2010-01-09 00:11:49 +00:00
|
|
|
# A MongoDB database.
|
2009-08-20 14:50:48 +00:00
|
|
|
class DB
|
2009-08-13 20:26:51 +00:00
|
|
|
|
2009-08-20 14:50:48 +00:00
|
|
|
SYSTEM_NAMESPACE_COLLECTION = "system.namespaces"
|
|
|
|
SYSTEM_INDEX_COLLECTION = "system.indexes"
|
|
|
|
SYSTEM_PROFILE_COLLECTION = "system.profile"
|
|
|
|
SYSTEM_USER_COLLECTION = "system.users"
|
|
|
|
SYSTEM_COMMAND_COLLECTION = "$cmd"
|
2009-08-13 20:26:51 +00:00
|
|
|
|
2009-10-19 21:14:41 +00:00
|
|
|
# Counter for generating unique request ids.
|
|
|
|
@@current_request_id = 0
|
|
|
|
|
2009-08-20 14:50:48 +00:00
|
|
|
# Strict mode enforces collection existence checks. When +true+,
|
2009-11-23 20:20:05 +00:00
|
|
|
# asking for a collection that does not exist, or trying to create a
|
|
|
|
# collection that already exists, raises an error.
|
2009-08-20 14:50:48 +00:00
|
|
|
#
|
2009-11-23 20:20:05 +00:00
|
|
|
# Strict mode is disabled by default, but enabled (+true+) at any time.
|
2009-08-20 14:50:48 +00:00
|
|
|
attr_writer :strict
|
2009-01-14 23:37:28 +00:00
|
|
|
|
2009-08-20 14:50:48 +00:00
|
|
|
# Returns the value of the +strict+ flag.
|
|
|
|
def strict?; @strict; end
|
2008-11-22 01:00:51 +00:00
|
|
|
|
2009-08-20 14:50:48 +00:00
|
|
|
# The name of the database.
|
|
|
|
attr_reader :name
|
2009-01-21 16:26:18 +00:00
|
|
|
|
2009-11-23 20:20:05 +00:00
|
|
|
# The Mongo::Connection instance connecting to the MongoDB server.
|
2009-11-04 22:46:15 +00:00
|
|
|
attr_reader :connection
|
2009-12-16 19:03:15 +00:00
|
|
|
|
2009-08-20 14:50:48 +00:00
|
|
|
# An array of [host, port] pairs.
|
|
|
|
attr_reader :nodes
|
2008-11-22 01:00:51 +00:00
|
|
|
|
2009-11-23 20:20:05 +00:00
|
|
|
# The logger instance if :logger is passed to initialize.
|
2009-10-07 23:39:36 +00:00
|
|
|
attr_reader :logger
|
2008-11-22 01:00:51 +00:00
|
|
|
|
2010-01-09 00:11:49 +00:00
|
|
|
# Instances of DB are normally obtained by calling Mongo#db.
|
2009-08-20 14:50:48 +00:00
|
|
|
#
|
|
|
|
# db_name :: The database name
|
|
|
|
#
|
2009-08-20 22:48:09 +00:00
|
|
|
# nodes :: An array of [host, port] pairs. See Connection#new, which offers
|
2009-08-20 14:50:48 +00:00
|
|
|
# a more flexible way of defining nodes.
|
|
|
|
#
|
|
|
|
# options :: A hash of options.
|
|
|
|
#
|
|
|
|
# Options:
|
|
|
|
#
|
|
|
|
# :strict :: If true, collections must exist to be accessed and must
|
2009-11-23 20:20:05 +00:00
|
|
|
# not exist to be created. See #collection and #create_collection.
|
2009-08-20 14:50:48 +00:00
|
|
|
#
|
|
|
|
# :pk :: A primary key factory object that must respond to :create_pk,
|
|
|
|
# which should take a hash and return a hash which merges the
|
|
|
|
# original hash with any primary key fields the factory wishes
|
|
|
|
# to inject. (NOTE: if the object already has a primary key,
|
|
|
|
# the factory should not inject a new key; this means that the
|
|
|
|
# object is being used in a repsert but it already exists.) The
|
|
|
|
# idea here is that when ever a record is inserted, the :pk
|
|
|
|
# object's +create_pk+ method will be called and the new hash
|
|
|
|
# returned will be inserted.
|
|
|
|
#
|
|
|
|
# :slave_ok :: Only used if +nodes+ contains only one host/port. If
|
|
|
|
# false, when connecting to that host/port we check to
|
|
|
|
# see if the server is the master. If it is not, an error
|
|
|
|
# is thrown.
|
|
|
|
#
|
2009-09-11 16:12:24 +00:00
|
|
|
# :logger :: Optional Logger instance to which driver usage information
|
|
|
|
# will be logged.
|
2009-08-20 14:50:48 +00:00
|
|
|
#
|
2009-11-23 21:03:33 +00:00
|
|
|
# :auto_reconnect :: DEPRECATED. See http://www.mongodb.org/display/DOCS/Replica+Pairs+in+Ruby
|
2009-11-23 20:20:05 +00:00
|
|
|
def initialize(db_name, connection, options={})
|
|
|
|
@name = validate_db_name(db_name)
|
|
|
|
@connection = connection
|
|
|
|
@strict = options[:strict]
|
2009-08-20 14:50:48 +00:00
|
|
|
@pk_factory = options[:pk]
|
|
|
|
end
|
2008-12-16 22:35:31 +00:00
|
|
|
|
2009-08-20 14:50:48 +00:00
|
|
|
# Returns true if +username+ has +password+ in
|
|
|
|
# +SYSTEM_USER_COLLECTION+. +name+ is username, +password+ is
|
|
|
|
# plaintext password.
|
|
|
|
def authenticate(username, password)
|
2009-11-23 20:20:05 +00:00
|
|
|
doc = command(:getnonce => 1)
|
2009-08-20 14:50:48 +00:00
|
|
|
raise "error retrieving nonce: #{doc}" unless ok?(doc)
|
|
|
|
nonce = doc['nonce']
|
|
|
|
|
|
|
|
auth = OrderedHash.new
|
|
|
|
auth['authenticate'] = 1
|
|
|
|
auth['user'] = username
|
|
|
|
auth['nonce'] = nonce
|
|
|
|
auth['key'] = Digest::MD5.hexdigest("#{nonce}#{username}#{hash_password(username, password)}")
|
2009-11-23 20:20:05 +00:00
|
|
|
ok?(command(auth))
|
2009-08-20 14:50:48 +00:00
|
|
|
end
|
2009-01-23 18:47:27 +00:00
|
|
|
|
2009-08-20 14:50:48 +00:00
|
|
|
# Deauthorizes use for this database for this connection.
|
|
|
|
def logout
|
2009-11-23 20:20:05 +00:00
|
|
|
doc = command(:logout => 1)
|
2010-01-09 00:11:49 +00:00
|
|
|
return true if ok?(doc)
|
|
|
|
raise "error logging out: #{doc.inspect}"
|
2009-08-20 14:50:48 +00:00
|
|
|
end
|
2009-01-23 18:47:27 +00:00
|
|
|
|
2009-08-20 14:50:48 +00:00
|
|
|
# Returns an array of collection names in this database.
|
|
|
|
def collection_names
|
|
|
|
names = collections_info.collect { |doc| doc['name'] || '' }
|
|
|
|
names = names.delete_if {|name| name.index(@name).nil? || name.index('$')}
|
|
|
|
names.map {|name| name.sub(@name + '.', '')}
|
|
|
|
end
|
2009-05-26 19:26:20 +00:00
|
|
|
|
2009-12-23 17:12:46 +00:00
|
|
|
# Retuns an array of Collection instances, one for each collection in this
|
2009-08-27 20:29:41 +00:00
|
|
|
# database.
|
|
|
|
def collections
|
|
|
|
collection_names.map do |collection_name|
|
|
|
|
Collection.new(self, collection_name)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-08-20 14:50:48 +00:00
|
|
|
# Returns a cursor over query result hashes. Each hash contains a
|
|
|
|
# 'name' string and optionally an 'options' hash. If +coll_name+ is
|
|
|
|
# specified, an array of length 1 is returned.
|
|
|
|
def collections_info(coll_name=nil)
|
|
|
|
selector = {}
|
2009-10-19 21:14:41 +00:00
|
|
|
selector[:name] = full_collection_name(coll_name) if coll_name
|
2009-10-22 18:10:12 +00:00
|
|
|
Cursor.new(Collection.new(self, SYSTEM_NAMESPACE_COLLECTION), :selector => selector)
|
2009-08-20 14:50:48 +00:00
|
|
|
end
|
2009-05-26 19:26:20 +00:00
|
|
|
|
2009-08-20 14:50:48 +00:00
|
|
|
# Create a collection. If +strict+ is false, will return existing or
|
|
|
|
# new collection. If +strict+ is true, will raise an error if
|
|
|
|
# collection +name+ already exists.
|
|
|
|
#
|
|
|
|
# Options is an optional hash:
|
|
|
|
#
|
|
|
|
# :capped :: Boolean. If not specified, capped is +false+.
|
|
|
|
#
|
|
|
|
# :size :: If +capped+ is +true+, specifies the maximum number of
|
|
|
|
# bytes. If +false+, specifies the initial extent of the
|
|
|
|
# collection.
|
|
|
|
#
|
|
|
|
# :max :: Max number of records in a capped collection. Optional.
|
|
|
|
def create_collection(name, options={})
|
|
|
|
# First check existence
|
|
|
|
if collection_names.include?(name)
|
|
|
|
if strict?
|
|
|
|
raise "Collection #{name} already exists. Currently in strict mode."
|
|
|
|
else
|
|
|
|
return Collection.new(self, name)
|
2008-11-22 01:00:51 +00:00
|
|
|
end
|
2009-08-20 14:50:48 +00:00
|
|
|
end
|
2008-11-22 01:00:51 +00:00
|
|
|
|
2009-08-20 14:50:48 +00:00
|
|
|
# Create new collection
|
|
|
|
oh = OrderedHash.new
|
|
|
|
oh[:create] = name
|
2009-11-23 20:20:05 +00:00
|
|
|
doc = command(oh.merge(options || {}))
|
2009-08-20 14:50:48 +00:00
|
|
|
ok = doc['ok']
|
2009-10-19 21:14:41 +00:00
|
|
|
return Collection.new(self, name, @pk_factory) if ok.kind_of?(Numeric) && (ok.to_i == 1 || ok.to_i == 0)
|
2009-08-20 14:50:48 +00:00
|
|
|
raise "Error creating collection: #{doc.inspect}"
|
|
|
|
end
|
2009-01-13 18:08:04 +00:00
|
|
|
|
2010-01-06 18:51:12 +00:00
|
|
|
# @deprecated all the admin methods are now included in the DB class.
|
2009-08-20 14:50:48 +00:00
|
|
|
def admin
|
2010-01-06 18:51:12 +00:00
|
|
|
warn "DB#admin has been DEPRECATED. All the admin functions are now available in the DB class itself."
|
2009-08-20 14:50:48 +00:00
|
|
|
Admin.new(self)
|
|
|
|
end
|
2009-01-14 23:37:28 +00:00
|
|
|
|
2009-08-20 14:50:48 +00:00
|
|
|
# Return a collection. If +strict+ is false, will return existing or
|
|
|
|
# new collection. If +strict+ is true, will raise an error if
|
|
|
|
# collection +name+ does not already exists.
|
|
|
|
def collection(name)
|
2009-10-19 21:14:41 +00:00
|
|
|
return Collection.new(self, name, @pk_factory) if !strict? || collection_names.include?(name)
|
2009-08-20 14:50:48 +00:00
|
|
|
raise "Collection #{name} doesn't exist. Currently in strict mode."
|
|
|
|
end
|
|
|
|
alias_method :[], :collection
|
2008-11-22 01:00:51 +00:00
|
|
|
|
2009-08-20 14:50:48 +00:00
|
|
|
# Drop collection +name+. Returns +true+ on success or if the
|
|
|
|
# collection does not exist, +false+ otherwise.
|
|
|
|
def drop_collection(name)
|
|
|
|
return true unless collection_names.include?(name)
|
2009-03-04 19:28:00 +00:00
|
|
|
|
2009-11-23 20:20:05 +00:00
|
|
|
ok?(command(:drop => name))
|
2009-08-20 14:50:48 +00:00
|
|
|
end
|
2009-02-26 17:06:03 +00:00
|
|
|
|
2009-08-20 14:50:48 +00:00
|
|
|
# Returns the error message from the most recently executed database
|
|
|
|
# operation for this connection, or +nil+ if there was no error.
|
|
|
|
def error
|
2009-11-23 20:20:05 +00:00
|
|
|
doc = command(:getlasterror => 1)
|
2009-08-20 14:50:48 +00:00
|
|
|
raise "error retrieving last error: #{doc}" unless ok?(doc)
|
|
|
|
doc['err']
|
|
|
|
end
|
2009-01-13 19:02:16 +00:00
|
|
|
|
2009-10-01 16:01:37 +00:00
|
|
|
# Get status information from the last operation on this connection.
|
|
|
|
def last_status
|
2009-11-23 20:20:05 +00:00
|
|
|
command(:getlasterror => 1)
|
2009-10-01 16:01:37 +00:00
|
|
|
end
|
|
|
|
|
2009-08-20 14:50:48 +00:00
|
|
|
# Returns +true+ if an error was caused by the most recently executed
|
|
|
|
# database operation.
|
|
|
|
def error?
|
|
|
|
error != nil
|
|
|
|
end
|
2008-11-22 01:00:51 +00:00
|
|
|
|
2009-08-20 14:50:48 +00:00
|
|
|
# Get the most recent error to have occured on this database
|
|
|
|
#
|
|
|
|
# Only returns errors that have occured since the last call to
|
|
|
|
# DB#reset_error_history - returns +nil+ if there is no such error.
|
|
|
|
def previous_error
|
2009-11-23 20:20:05 +00:00
|
|
|
error = command(:getpreverror => 1)
|
2009-08-20 14:50:48 +00:00
|
|
|
if error["err"]
|
|
|
|
error
|
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
2008-11-22 01:00:51 +00:00
|
|
|
|
2009-08-20 14:50:48 +00:00
|
|
|
# Reset the error history of this database
|
|
|
|
#
|
|
|
|
# Calls to DB#previous_error will only return errors that have occurred
|
|
|
|
# since the most recent call to this method.
|
|
|
|
def reset_error_history
|
2009-11-23 20:20:05 +00:00
|
|
|
command(:reseterror => 1)
|
2009-08-20 14:50:48 +00:00
|
|
|
end
|
2009-04-28 18:55:36 +00:00
|
|
|
|
2009-08-20 14:50:48 +00:00
|
|
|
# Returns a Cursor over the query results.
|
|
|
|
#
|
|
|
|
# Note that the query gets sent lazily; the cursor calls
|
|
|
|
# #send_query_message when needed. If the caller never requests an
|
|
|
|
# object from the cursor, the query never gets sent.
|
|
|
|
def query(collection, query, admin=false)
|
|
|
|
Cursor.new(self, collection, query, admin)
|
|
|
|
end
|
2009-08-04 18:16:02 +00:00
|
|
|
|
2009-08-20 14:50:48 +00:00
|
|
|
# Dereference a DBRef, getting the document it points to.
|
|
|
|
def dereference(dbref)
|
|
|
|
collection(dbref.namespace).find_one("_id" => dbref.object_id)
|
|
|
|
end
|
2008-12-16 22:35:31 +00:00
|
|
|
|
2009-08-20 14:50:48 +00:00
|
|
|
# Evaluate a JavaScript expression on MongoDB.
|
|
|
|
# +code+ should be a string or Code instance containing a JavaScript
|
|
|
|
# expression. Additional arguments will be passed to that expression
|
|
|
|
# when it is run on the server.
|
|
|
|
def eval(code, *args)
|
|
|
|
if not code.is_a? Code
|
|
|
|
code = Code.new(code)
|
|
|
|
end
|
2008-12-08 16:38:42 +00:00
|
|
|
|
2009-08-20 14:50:48 +00:00
|
|
|
oh = OrderedHash.new
|
|
|
|
oh[:$eval] = code
|
|
|
|
oh[:args] = args
|
2009-11-23 20:20:05 +00:00
|
|
|
doc = command(oh)
|
2009-08-20 14:50:48 +00:00
|
|
|
return doc['retval'] if ok?(doc)
|
|
|
|
raise OperationFailure, "eval failed: #{doc['errmsg']}"
|
|
|
|
end
|
2008-11-22 01:00:51 +00:00
|
|
|
|
2009-08-20 14:50:48 +00:00
|
|
|
# Rename collection +from+ to +to+. Meant to be called by
|
|
|
|
# Collection#rename.
|
|
|
|
def rename_collection(from, to)
|
|
|
|
oh = OrderedHash.new
|
|
|
|
oh[:renameCollection] = "#{@name}.#{from}"
|
|
|
|
oh[:to] = "#{@name}.#{to}"
|
2009-11-23 20:20:05 +00:00
|
|
|
doc = command(oh, true)
|
2009-08-20 14:50:48 +00:00
|
|
|
raise "Error renaming collection: #{doc.inspect}" unless ok?(doc)
|
|
|
|
end
|
2009-08-10 20:10:52 +00:00
|
|
|
|
2009-08-20 14:50:48 +00:00
|
|
|
# Drop index +name+ from +collection_name+. Normally called from
|
|
|
|
# Collection#drop_index or Collection#drop_indexes.
|
|
|
|
def drop_index(collection_name, name)
|
|
|
|
oh = OrderedHash.new
|
|
|
|
oh[:deleteIndexes] = collection_name
|
|
|
|
oh[:index] = name
|
2009-11-23 20:20:05 +00:00
|
|
|
doc = command(oh)
|
2009-08-20 14:50:48 +00:00
|
|
|
raise "Error with drop_index command: #{doc.inspect}" unless ok?(doc)
|
|
|
|
end
|
2009-01-21 16:26:18 +00:00
|
|
|
|
2009-08-20 14:50:48 +00:00
|
|
|
# Get information on the indexes for the collection +collection_name+.
|
|
|
|
# Normally called by Collection#index_information. Returns a hash where
|
|
|
|
# the keys are index names (as returned by Collection#create_index and
|
|
|
|
# the values are lists of [key, direction] pairs specifying the index
|
|
|
|
# (as passed to Collection#create_index).
|
|
|
|
def index_information(collection_name)
|
2009-10-19 21:14:41 +00:00
|
|
|
sel = {:ns => full_collection_name(collection_name)}
|
2009-08-20 14:50:48 +00:00
|
|
|
info = {}
|
2009-10-22 18:10:12 +00:00
|
|
|
Cursor.new(Collection.new(self, SYSTEM_INDEX_COLLECTION), :selector => sel).each { |index|
|
2009-11-24 21:13:14 +00:00
|
|
|
info[index['name']] = index['key'].map {|k| k}
|
2009-08-20 14:50:48 +00:00
|
|
|
}
|
|
|
|
info
|
|
|
|
end
|
|
|
|
|
|
|
|
# Create a new index on +collection_name+. +field_or_spec+
|
|
|
|
# should be either a single field name or a Array of [field name,
|
|
|
|
# direction] pairs. Directions should be specified as
|
|
|
|
# Mongo::ASCENDING or Mongo::DESCENDING. Normally called
|
|
|
|
# by Collection#create_index. If +unique+ is true the index will
|
|
|
|
# enforce a uniqueness constraint.
|
|
|
|
def create_index(collection_name, field_or_spec, unique=false)
|
2009-10-19 21:14:41 +00:00
|
|
|
self.collection(collection_name).create_index(field_or_spec, unique)
|
2009-08-20 14:50:48 +00:00
|
|
|
end
|
2009-12-16 19:03:15 +00:00
|
|
|
|
2009-11-05 21:14:37 +00:00
|
|
|
# Return +true+ if +doc+ contains an 'ok' field with the value 1.
|
|
|
|
def ok?(doc)
|
|
|
|
ok = doc['ok']
|
|
|
|
ok.kind_of?(Numeric) && ok.to_i == 1
|
|
|
|
end
|
|
|
|
|
|
|
|
# DB commands need to be ordered, so selector must be an OrderedHash
|
|
|
|
# (or a Hash with only one element). What DB commands really need is
|
|
|
|
# that the "command" key be first.
|
2009-11-23 20:20:05 +00:00
|
|
|
def command(selector, use_admin_db=false, sock=nil)
|
2009-11-05 21:14:37 +00:00
|
|
|
if !selector.kind_of?(OrderedHash)
|
|
|
|
if !selector.kind_of?(Hash) || selector.keys.length > 1
|
2009-11-23 20:20:05 +00:00
|
|
|
raise "command must be given an OrderedHash when there is more than one key"
|
2009-11-05 21:14:37 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-11-23 20:20:05 +00:00
|
|
|
cursor = Cursor.new(Collection.new(self, SYSTEM_COMMAND_COLLECTION), :admin => use_admin_db, :limit => -1, :selector => selector, :socket => sock)
|
2009-12-16 19:03:15 +00:00
|
|
|
cursor.next_document
|
2009-11-05 21:14:37 +00:00
|
|
|
end
|
2009-12-16 19:03:15 +00:00
|
|
|
|
2009-11-05 21:14:37 +00:00
|
|
|
# Sends a command to the database.
|
|
|
|
#
|
|
|
|
# :selector (required) :: An OrderedHash, or a standard Hash with just one
|
|
|
|
# key, specifying the command to be performed.
|
|
|
|
#
|
|
|
|
# :admin (optional) :: If true, the command will be executed on the admin
|
|
|
|
# collection.
|
|
|
|
#
|
|
|
|
# :check_response (optional) :: If true, will raise an exception if the
|
|
|
|
# command fails.
|
|
|
|
#
|
|
|
|
# Note: DB commands must start with the "command" key. For this reason,
|
|
|
|
# any selector containing more than one key must be an OrderedHash.
|
2009-11-23 20:20:05 +00:00
|
|
|
def command(selector, admin=false, check_response=false, sock=nil)
|
2009-11-05 21:14:37 +00:00
|
|
|
raise MongoArgumentError, "command must be given a selector" unless selector.is_a?(Hash) && !selector.empty?
|
2009-12-16 19:03:15 +00:00
|
|
|
if selector.class.eql?(Hash) && selector.keys.length > 1
|
2009-11-05 21:14:37 +00:00
|
|
|
raise MongoArgumentError, "DB#command requires an OrderedHash when hash contains multiple keys"
|
|
|
|
end
|
|
|
|
|
2009-12-16 19:03:15 +00:00
|
|
|
result = Cursor.new(system_command_collection, :admin => admin,
|
|
|
|
:limit => -1, :selector => selector, :socket => sock).next_document
|
2009-11-05 21:14:37 +00:00
|
|
|
|
|
|
|
if check_response && !ok?(result)
|
|
|
|
raise OperationFailure, "Database command '#{selector.keys.first}' failed."
|
|
|
|
else
|
|
|
|
result
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-01-06 18:51:12 +00:00
|
|
|
# @deprecated please use DB#command instead.
|
2009-11-23 20:20:05 +00:00
|
|
|
def db_command(*args)
|
|
|
|
warn "DB#db_command has been DEPRECATED. Please use DB#command instead."
|
|
|
|
command(args[0], args[1])
|
|
|
|
end
|
|
|
|
|
2009-11-05 21:14:37 +00:00
|
|
|
def full_collection_name(collection_name)
|
|
|
|
"#{@name}.#{collection_name}"
|
|
|
|
end
|
|
|
|
|
2010-01-09 00:11:49 +00:00
|
|
|
# The primary key factory object (or +nil+).
|
|
|
|
def pk_factory
|
|
|
|
@pk_factory
|
|
|
|
end
|
|
|
|
|
|
|
|
# Specify a primary key factory if not already set.
|
|
|
|
#
|
|
|
|
# @raise [MongoArgumentError] if the primary key factory has already been set.
|
|
|
|
def pk_factory=(pk_factory)
|
|
|
|
if @pk_factory
|
|
|
|
raise MongoArgumentError, "Cannot change primary key factory once it's been set"
|
|
|
|
end
|
|
|
|
|
|
|
|
@pk_factory = pk_factory
|
|
|
|
end
|
|
|
|
|
2010-01-06 18:51:12 +00:00
|
|
|
# Return the current database profiling level.
|
|
|
|
#
|
|
|
|
# @return [Symbol] :off, :slow_only, or :all
|
|
|
|
def profiling_level
|
|
|
|
oh = OrderedHash.new
|
|
|
|
oh[:profile] = -1
|
|
|
|
doc = command(oh)
|
|
|
|
raise "Error with profile command: #{doc.inspect}" unless ok?(doc) && doc['was'].kind_of?(Numeric)
|
|
|
|
case doc['was'].to_i
|
|
|
|
when 0
|
|
|
|
:off
|
|
|
|
when 1
|
|
|
|
:slow_only
|
|
|
|
when 2
|
|
|
|
:all
|
|
|
|
else
|
|
|
|
raise "Error: illegal profiling level value #{doc['was']}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Set database profiling level to :off, :slow_only, or :all.
|
|
|
|
def profiling_level=(level)
|
|
|
|
oh = OrderedHash.new
|
|
|
|
oh[:profile] = case level
|
|
|
|
when :off
|
|
|
|
0
|
|
|
|
when :slow_only
|
|
|
|
1
|
|
|
|
when :all
|
|
|
|
2
|
|
|
|
else
|
|
|
|
raise "Error: illegal profiling level value #{level}"
|
|
|
|
end
|
|
|
|
doc = command(oh)
|
|
|
|
raise "Error with profile command: #{doc.inspect}" unless ok?(doc)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Returns an array containing current profiling information.
|
|
|
|
def profiling_info
|
|
|
|
Cursor.new(Collection.new(self, DB::SYSTEM_PROFILE_COLLECTION), :selector => {}).to_a
|
|
|
|
end
|
|
|
|
|
|
|
|
# Validate a named collection by raising an exception if there is a
|
|
|
|
# problem or returning an interesting hash (see especially the
|
|
|
|
# 'result' string value) if all is well.
|
|
|
|
def validate_collection(name)
|
|
|
|
doc = command(:validate => name)
|
|
|
|
raise "Error with validate command: #{doc.inspect}" unless ok?(doc)
|
|
|
|
result = doc['result']
|
|
|
|
raise "Error with validation data: #{doc.inspect}" unless result.kind_of?(String)
|
|
|
|
raise "Error: invalid collection #{name}: #{doc.inspect}" if result =~ /\b(exception|corrupt)\b/i
|
|
|
|
doc
|
|
|
|
end
|
|
|
|
|
2009-11-05 21:14:37 +00:00
|
|
|
private
|
|
|
|
|
2009-11-23 20:20:05 +00:00
|
|
|
def hash_password(username, plaintext)
|
|
|
|
Digest::MD5.hexdigest("#{username}:mongo:#{plaintext}")
|
2009-11-04 22:46:15 +00:00
|
|
|
end
|
|
|
|
|
2009-11-23 20:20:05 +00:00
|
|
|
def system_command_collection
|
|
|
|
Collection.new(self, SYSTEM_COMMAND_COLLECTION)
|
2009-08-20 14:50:48 +00:00
|
|
|
end
|
|
|
|
|
2009-11-23 20:20:05 +00:00
|
|
|
def validate_db_name(db_name)
|
|
|
|
unless [String, Symbol].include?(db_name.class)
|
|
|
|
raise TypeError, "db_name must be a string or symbol"
|
2009-10-21 14:11:33 +00:00
|
|
|
end
|
2009-10-20 19:44:46 +00:00
|
|
|
|
2009-11-23 20:20:05 +00:00
|
|
|
[" ", ".", "$", "/", "\\"].each do |invalid_char|
|
|
|
|
if db_name.include? invalid_char
|
|
|
|
raise InvalidName, "database names cannot contain the character '#{invalid_char}'"
|
2009-11-20 22:48:41 +00:00
|
|
|
end
|
2009-10-20 19:44:46 +00:00
|
|
|
end
|
2009-11-23 20:20:05 +00:00
|
|
|
raise InvalidName, "database name cannot be the empty string" if db_name.empty?
|
|
|
|
db_name
|
2009-11-02 18:22:46 +00:00
|
|
|
end
|
2008-11-22 01:00:51 +00:00
|
|
|
end
|
|
|
|
end
|