From 15055794c7dcd3b422614e2eae95dbb5dca0b3e6 Mon Sep 17 00:00:00 2001 From: Jim Menard Date: Wed, 7 Jan 2009 15:36:12 -0500 Subject: [PATCH] Beginnings of Admin implementation. --- README.rdoc | 2 +- lib/mongo.rb | 1 + lib/mongo/admin.rb | 78 +++++++++++++++++++++++++++++++++++++++++++++ lib/mongo/db.rb | 7 ++-- tests/test_admin.rb | 40 +++++++++++++++++++++++ 5 files changed, 123 insertions(+), 5 deletions(-) create mode 100644 lib/mongo/admin.rb create mode 100644 tests/test_admin.rb diff --git a/README.rdoc b/README.rdoc index 52c01df..b8a6514 100644 --- a/README.rdoc +++ b/README.rdoc @@ -65,7 +65,7 @@ See the git log comments. * More tests. -* Implement Admin. +* Implement the rest of the Admin interface. * Study src/main/ed/db/{dbcollection,dbcursor,db}.js and ByteEncoder.java in the Babble code. That's what I should be writing to. diff --git a/lib/mongo.rb b/lib/mongo.rb index 6b300b4..d99689d 100644 --- a/lib/mongo.rb +++ b/lib/mongo.rb @@ -4,3 +4,4 @@ require 'mongo/message' require 'mongo/db' require 'mongo/cursor' require 'mongo/collection' +require 'mongo/admin' diff --git a/lib/mongo/admin.rb b/lib/mongo/admin.rb new file mode 100644 index 0000000..cb3a955 --- /dev/null +++ b/lib/mongo/admin.rb @@ -0,0 +1,78 @@ +# -- +# Copyright (C) 2008-2009 10gen Inc. +# +# This program is free software: you can redistribute it and/or modify it +# under the terms of the GNU Affero General Public License, version 3, as +# published by the Free Software Foundation. +# +# This program is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License +# for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . +# ++ + +require 'mongo/util/ordered_hash' + +module XGen + module Mongo + module Driver + + # Provide administrative database methods: those having to do with + # profiling and validation. + class Admin + + def initialize(db) + @db = db + end + + # Return the current database profiling level. + def profiling_level + oh = OrderedHash.new + oh[:profile] = -1 + doc = @db.db_command(oh) + raise "Error with profile command: #{doc.inspect}" unless @db.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 = @db.db_command(oh) + raise "Error with profile command: #{doc.inspect}" unless @db.ok?(doc) + end + + # Return an array contining current profiling information from the + # database. + def profiling_info + end + + # Validate a named collection. + def validate_collection(name) + end + + end + end + end +end diff --git a/lib/mongo/db.rb b/lib/mongo/db.rb index 1287daa..3579787 100644 --- a/lib/mongo/db.rb +++ b/lib/mongo/db.rb @@ -21,6 +21,7 @@ require 'mongo/collection' require 'mongo/message' require 'mongo/query' require 'mongo/util/ordered_hash.rb' +require 'mongo/admin' module XGen module Mongo @@ -116,8 +117,6 @@ module XGen end def admin - # TODO - raise "not implemented" Admin.new(self) end @@ -278,8 +277,6 @@ module XGen "#{@name}.#{collection_name}" end - protected - # Return +true+ if +doc+ contains an 'ok' field with the value 1. def ok?(doc) ok = doc['ok'] @@ -289,6 +286,8 @@ module XGen # 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. + # + # Do not call this. Intended for driver use only. def db_command(selector) if !selector.kind_of?(OrderedHash) if !selector.kind_of?(Hash) || selector.keys.length > 1 diff --git a/tests/test_admin.rb b/tests/test_admin.rb new file mode 100644 index 0000000..308e269 --- /dev/null +++ b/tests/test_admin.rb @@ -0,0 +1,40 @@ +$LOAD_PATH[0,0] = File.join(File.dirname(__FILE__), '..', 'lib') +require 'mongo' +require 'test/unit' + +# NOTE: assumes Mongo is running +class AdminTest < 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 + @db = Mongo.new(host, port).db('ruby-mongo-test') + # Insert some data to make sure the database itself exists. + @coll = @db.collection('test') + @coll.clear + @r1 = @coll.insert('a' => 1) # collection not created until it's used + @coll_full_name = 'ruby-mongo-test.test' + @admin = @db.admin + end + + def teardown + unless @db.socket.closed? + @admin.profiling_level = :off + @coll.clear unless @coll == nil + end + end + + def test_default_profiling_level + assert_equal :off, @admin.profiling_level + end + + def test_change_profiling_level + @admin.profiling_level = :slow_only + assert_equal :slow_only, @admin.profiling_level + @admin.profiling_level = :off + assert_equal :off, @admin.profiling_level + end + +end