Class: Mongo::Admin

Inherits:
Object
  • Object
show all
Defined in:
lib/mongo/admin.rb

Overview

Provide administrative database methods: those having to do with profiling and validation.

Instance Method Summary collapse

Constructor Details

#initialize(db) ⇒ Admin

Returns a new instance of Admin.



25
26
27
# File 'lib/mongo/admin.rb', line 25

def initialize(db)
  @db = db
end

Instance Method Details

#profiling_infoObject

Return an array contining current profiling information from the database.



66
67
68
# File 'lib/mongo/admin.rb', line 66

def profiling_info
  @db.query(Collection.new(@db, DB::SYSTEM_PROFILE_COLLECTION), Query.new({})).to_a
end

#profiling_levelObject

Return the current database profiling level.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/mongo/admin.rb', line 30

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

#profiling_level=(level) ⇒ Object

Set database profiling level to :off, :slow_only, or :all.



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/mongo/admin.rb', line 48

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

#validate_collection(name) ⇒ Object

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.



73
74
75
76
77
78
79
80
# File 'lib/mongo/admin.rb', line 73

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
end