mongo-ruby-driver/lib/mongo/admin.rb

84 lines
2.6 KiB
Ruby
Raw Normal View History

2009-01-07 20:36:12 +00:00
# --
# Copyright (C) 2008-2009 10gen Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
2009-01-07 20:36:12 +00:00
#
# http://www.apache.org/licenses/LICENSE-2.0
2009-01-07 20:36:12 +00:00
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
2009-01-07 20:36:12 +00:00
# ++
require 'mongo/util/ordered_hash'
module Mongo
2009-01-07 20:36:12 +00:00
# Provide administrative database methods: those having to do with
# profiling and validation.
class Admin
2009-01-07 20:36:12 +00:00
def initialize(db)
@db = db
end
2009-01-07 20:36:12 +00:00
# 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
2009-01-07 20:36:12 +00:00
# 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
2009-01-07 20:36:12 +00:00
# Return an array contining current profiling information from the
# database.
def profiling_info
Cursor.new(Collection.new(@db, DB::SYSTEM_PROFILE_COLLECTION), :selector => {}).to_a
end
2009-01-07 20:36:12 +00:00
# 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 = @db.db_command(:validate => name)
raise "Error with validate command: #{doc.inspect}" unless @db.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
2009-01-07 20:36:12 +00:00
end
2009-01-07 20:36:12 +00:00
end
end