deprecated Admin class; admin method now available in DB class

This commit is contained in:
Kyle Banker 2010-01-06 13:51:12 -05:00
parent c1f3ba2cb9
commit 77df695bca
3 changed files with 123 additions and 3 deletions

View File

@ -16,16 +16,22 @@
module Mongo module Mongo
# Provide administrative database methods: those having to do with # @deprecated this class is deprecated. Methods defined here will
# profiling and validation. # henceforth be available in Mongo::DB.
class Admin class Admin
def initialize(db) def initialize(db)
warn "The Admin class has been DEPRECATED. All admin methods now exist in DB."
@db = db @db = db
end end
# Return the current database profiling level. # Return the current database profiling level.
#
# @return [Symbol] :off, :slow_only, or :all
#
# @deprecated please use DB#profiling_level instead.
def profiling_level def profiling_level
warn "Admin#profiling_level has been DEPRECATED. Please use DB#profiling_level instead."
oh = OrderedHash.new oh = OrderedHash.new
oh[:profile] = -1 oh[:profile] = -1
doc = @db.command(oh) doc = @db.command(oh)
@ -43,7 +49,10 @@ module Mongo
end end
# Set database profiling level to :off, :slow_only, or :all. # Set database profiling level to :off, :slow_only, or :all.
#
# @deprecated please use DB#profiling_level= instead.
def profiling_level=(level) def profiling_level=(level)
warn "Admin#profiling_level= has been DEPRECATED. Please use DB#profiling_level= instead."
oh = OrderedHash.new oh = OrderedHash.new
oh[:profile] = case level oh[:profile] = case level
when :off when :off
@ -60,14 +69,20 @@ module Mongo
end end
# Returns an array containing current profiling information. # Returns an array containing current profiling information.
#
# @deprecated please use DB#profiling_info instead.
def profiling_info def profiling_info
warn "Admin#profiling_info has been DEPRECATED. Please use DB#profiling_info instead."
Cursor.new(Collection.new(@db, DB::SYSTEM_PROFILE_COLLECTION), :selector => {}).to_a Cursor.new(Collection.new(@db, DB::SYSTEM_PROFILE_COLLECTION), :selector => {}).to_a
end end
# Validate a named collection by raising an exception if there is a # Validate a named collection by raising an exception if there is a
# problem or returning an interesting hash (see especially the # problem or returning an interesting hash (see especially the
# 'result' string value) if all is well. # 'result' string value) if all is well.
#
# @deprecated please use DB#validate_collection instead.
def validate_collection(name) def validate_collection(name)
warn "Admin#validate_collection has been DEPRECATED. Please use DB#validate_collection instead."
doc = @db.command(:validate => name) doc = @db.command(:validate => name)
raise "Error with validate command: #{doc.inspect}" unless @db.ok?(doc) raise "Error with validate command: #{doc.inspect}" unless @db.ok?(doc)
result = doc['result'] result = doc['result']

View File

@ -181,7 +181,9 @@ module Mongo
raise "Error creating collection: #{doc.inspect}" raise "Error creating collection: #{doc.inspect}"
end end
# @deprecated all the admin methods are now included in the DB class.
def admin def admin
warn "DB#admin has been DEPRECATED. All the admin functions are now available in the DB class itself."
Admin.new(self) Admin.new(self)
end end
@ -366,7 +368,7 @@ module Mongo
end end
end end
# DEPRECATED: please use DB#command instead. # @deprecated please use DB#command instead.
def db_command(*args) def db_command(*args)
warn "DB#db_command has been DEPRECATED. Please use DB#command instead." warn "DB#db_command has been DEPRECATED. Please use DB#command instead."
command(args[0], args[1]) command(args[0], args[1])
@ -376,6 +378,60 @@ module Mongo
"#{@name}.#{collection_name}" "#{@name}.#{collection_name}"
end end
# 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
private private
def hash_password(username, plaintext) def hash_password(username, plaintext)

View File

@ -202,4 +202,53 @@ class DBTest < Test::Unit::TestCase
assert db.collection('users').remove assert db.collection('users').remove
end end
context "database profiling" do
setup do
@db = @@conn['ruby-mongo-test-admin-functions']
@coll = @db['test']
@coll.remove
@r1 = @coll.insert('a' => 1) # collection not created until it's used
end
should "set default profiling level" do
assert_equal :off, @db.profiling_level
end
should "change profiling level" do
@db.profiling_level = :slow_only
assert_equal :slow_only, @db.profiling_level
@db.profiling_level = :off
assert_equal :off, @db.profiling_level
@db.profiling_level = :all
assert_equal :all, @db.profiling_level
begin
@db.profiling_level = :medium
fail "shouldn't be able to do this"
rescue
end
end
should "return profiling info" do
@db.profiling_level = :all
@coll.find()
@db.profiling_level = :off
info = @db.profiling_info
assert_kind_of Array, info
assert info.length >= 1
first = info.first
assert_kind_of String, first['info']
assert_kind_of Time, first['ts']
assert_kind_of Numeric, first['millis']
end
def test_validate_collection
doc = @db.validate_collection(@coll.name)
assert_not_nil doc
result = doc['result']
assert_not_nil result
assert_match /firstExtent/, result
end
end
end end