From 7291995efc1baeccb229a9cb9654b7ae78961090 Mon Sep 17 00:00:00 2001 From: Jim Menard Date: Fri, 16 Jan 2009 09:52:31 -0500 Subject: [PATCH] Added Mongo#database_info and Mongo#database_names. --- lib/mongo/mongo.rb | 23 +++++++++++++++++++++++ tests/test_mongo.rb | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 tests/test_mongo.rb diff --git a/lib/mongo/mongo.rb b/lib/mongo/mongo.rb index 29bd7cf..3b948dc 100644 --- a/lib/mongo/mongo.rb +++ b/lib/mongo/mongo.rb @@ -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" diff --git a/tests/test_mongo.rb b/tests/test_mongo.rb new file mode 100644 index 0000000..4a07e3f --- /dev/null +++ b/tests/test_mongo.rb @@ -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