Added Mongo#database_info and Mongo#database_names.

This commit is contained in:
Jim Menard 2009-01-16 09:52:31 -05:00
parent cd6368316b
commit 7291995efc
2 changed files with 55 additions and 0 deletions

View File

@ -57,6 +57,29 @@ module XGen
XGen::Mongo::Driver::DB.new(db_name, @nodes)
end
# Returns a hash containing database names as keys and disk space for
# each as values.
def database_info
admin_db = nil
begin
admin_db = db('admin')
doc = admin_db.db_command(:listDatabases => 1)
raise "error retrieving database info" unless admin_db.ok?(doc)
h = {}
doc['databases'].each { |db|
h[db['name']] = db['sizeOnDisk'].to_i
}
h
ensure
admin_db.close
end
end
# Returns an array of database names.
def database_names
database_info.keys
end
# Not implemented.
def clone_database(from)
raise "not implemented"

32
tests/test_mongo.rb Normal file
View File

@ -0,0 +1,32 @@
$LOAD_PATH[0,0] = File.join(File.dirname(__FILE__), '..', 'lib')
require 'mongo'
require 'test/unit'
# NOTE: assumes Mongo is running
class MongoTest < Test::Unit::TestCase
include XGen::Mongo::Driver
def setup
@host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
@port = ENV['MONGO_RUBY_DRIVER_PORT'] || Mongo::DEFAULT_PORT
@mongo = Mongo.new(@host, @port)
end
def test_database_info
info = @mongo.database_info
assert_not_nil info
assert_kind_of Hash, info
assert_not_nil info['admin']
assert info['admin'] > 0
end
def test_database_names
names = @mongo.database_names
assert_not_nil names
assert_kind_of Array, names
assert names.length >= 1
assert names.include?('admin')
end
end