Beginnings of Admin implementation.

This commit is contained in:
Jim Menard 2009-01-07 15:36:12 -05:00
parent 036c6b41dc
commit 15055794c7
5 changed files with 123 additions and 5 deletions

View File

@ -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.

View File

@ -4,3 +4,4 @@ require 'mongo/message'
require 'mongo/db'
require 'mongo/cursor'
require 'mongo/collection'
require 'mongo/admin'

78
lib/mongo/admin.rb Normal file
View File

@ -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 <http://www.gnu.org/licenses/>.
# ++
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

View File

@ -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

40
tests/test_admin.rb Normal file
View File

@ -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