mongo 0.1.0 → 0.15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.rdoc +268 -71
- data/Rakefile +27 -62
- data/bin/bson_benchmark.rb +59 -0
- data/bin/mongo_console +3 -3
- data/bin/run_test_script +19 -0
- data/bin/standard_benchmark +109 -0
- data/examples/admin.rb +41 -0
- data/examples/benchmarks.rb +42 -0
- data/examples/blog.rb +76 -0
- data/examples/capped.rb +23 -0
- data/examples/cursor.rb +47 -0
- data/examples/gridfs.rb +87 -0
- data/examples/index_test.rb +125 -0
- data/examples/info.rb +30 -0
- data/examples/queries.rb +69 -0
- data/examples/simple.rb +23 -0
- data/examples/strict.rb +34 -0
- data/examples/types.rb +35 -0
- data/lib/mongo.rb +9 -2
- data/lib/mongo/admin.rb +65 -68
- data/lib/mongo/collection.rb +379 -117
- data/lib/mongo/connection.rb +151 -0
- data/lib/mongo/cursor.rb +271 -216
- data/lib/mongo/db.rb +500 -315
- data/lib/mongo/errors.rb +26 -0
- data/lib/mongo/gridfs.rb +16 -0
- data/lib/mongo/gridfs/chunk.rb +92 -0
- data/lib/mongo/gridfs/grid_store.rb +464 -0
- data/lib/mongo/message.rb +16 -0
- data/lib/mongo/message/get_more_message.rb +24 -13
- data/lib/mongo/message/insert_message.rb +29 -11
- data/lib/mongo/message/kill_cursors_message.rb +23 -12
- data/lib/mongo/message/message.rb +74 -62
- data/lib/mongo/message/message_header.rb +35 -24
- data/lib/mongo/message/msg_message.rb +21 -9
- data/lib/mongo/message/opcodes.rb +26 -15
- data/lib/mongo/message/query_message.rb +63 -43
- data/lib/mongo/message/remove_message.rb +29 -12
- data/lib/mongo/message/update_message.rb +30 -13
- data/lib/mongo/query.rb +97 -89
- data/lib/mongo/types/binary.rb +25 -21
- data/lib/mongo/types/code.rb +30 -0
- data/lib/mongo/types/dbref.rb +19 -23
- data/lib/mongo/types/objectid.rb +130 -116
- data/lib/mongo/types/regexp_of_holding.rb +27 -31
- data/lib/mongo/util/bson.rb +273 -160
- data/lib/mongo/util/byte_buffer.rb +32 -28
- data/lib/mongo/util/ordered_hash.rb +88 -42
- data/lib/mongo/util/xml_to_ruby.rb +18 -15
- data/mongo-ruby-driver.gemspec +103 -0
- data/test/mongo-qa/_common.rb +8 -0
- data/test/mongo-qa/admin +26 -0
- data/test/mongo-qa/capped +22 -0
- data/test/mongo-qa/count1 +18 -0
- data/test/mongo-qa/dbs +22 -0
- data/test/mongo-qa/find +10 -0
- data/test/mongo-qa/find1 +15 -0
- data/test/mongo-qa/gridfs_in +16 -0
- data/test/mongo-qa/gridfs_out +17 -0
- data/test/mongo-qa/indices +49 -0
- data/test/mongo-qa/remove +25 -0
- data/test/mongo-qa/stress1 +35 -0
- data/test/mongo-qa/test1 +11 -0
- data/test/mongo-qa/update +18 -0
- data/{tests → test}/test_admin.rb +25 -16
- data/test/test_bson.rb +268 -0
- data/{tests → test}/test_byte_buffer.rb +0 -0
- data/test/test_chunk.rb +84 -0
- data/test/test_collection.rb +282 -0
- data/test/test_connection.rb +101 -0
- data/test/test_cursor.rb +321 -0
- data/test/test_db.rb +196 -0
- data/test/test_db_api.rb +798 -0
- data/{tests → test}/test_db_connection.rb +4 -3
- data/test/test_grid_store.rb +284 -0
- data/{tests → test}/test_message.rb +1 -1
- data/test/test_objectid.rb +105 -0
- data/{tests → test}/test_ordered_hash.rb +55 -0
- data/{tests → test}/test_round_trip.rb +13 -9
- data/test/test_threading.rb +37 -0
- metadata +74 -32
- data/bin/validate +0 -51
- data/lib/mongo/mongo.rb +0 -74
- data/lib/mongo/types/undefined.rb +0 -31
- data/tests/test_bson.rb +0 -135
- data/tests/test_cursor.rb +0 -66
- data/tests/test_db.rb +0 -51
- data/tests/test_db_api.rb +0 -349
- data/tests/test_objectid.rb +0 -88
@@ -0,0 +1,125 @@
|
|
1
|
+
class Exception
|
2
|
+
def errmsg
|
3
|
+
"%s: %s\n%s" % [self.class, message, (backtrace || []).join("\n") << "\n"]
|
4
|
+
end
|
5
|
+
end
|
6
|
+
|
7
|
+
$LOAD_PATH[0,0] = File.join(File.dirname(__FILE__), '..', 'lib')
|
8
|
+
require 'mongo'
|
9
|
+
|
10
|
+
include Mongo
|
11
|
+
|
12
|
+
host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
|
13
|
+
port = ENV['MONGO_RUBY_DRIVER_PORT'] || Connection::DEFAULT_PORT
|
14
|
+
|
15
|
+
puts ">> Connecting to #{host}:#{port}"
|
16
|
+
db = Connection.new(host, port).db('ruby-mongo-index_test')
|
17
|
+
|
18
|
+
puts ">> Dropping collection test"
|
19
|
+
begin
|
20
|
+
res = db.drop_collection('test')
|
21
|
+
puts "dropped : #{res.inspect}"
|
22
|
+
rescue => e
|
23
|
+
puts "Error: #{e.errmsg}"
|
24
|
+
end
|
25
|
+
|
26
|
+
puts ">> Creating collection test"
|
27
|
+
begin
|
28
|
+
coll = db.collection('test')
|
29
|
+
puts "created : #{coll.inspect}"
|
30
|
+
rescue => e
|
31
|
+
puts "Error: #{e.errmsg}"
|
32
|
+
end
|
33
|
+
|
34
|
+
OBJS_COUNT = 100
|
35
|
+
|
36
|
+
puts ">> Generating test data"
|
37
|
+
msgs = %w{hola hello aloha ciao}
|
38
|
+
arr = (0...OBJS_COUNT).collect {|x| { :number => x, :rndm => (rand(5)+1), :msg => msgs[rand(4)] }}
|
39
|
+
puts "generated"
|
40
|
+
|
41
|
+
puts ">> Inserting data (#{arr.size})"
|
42
|
+
coll.insert(arr)
|
43
|
+
puts "inserted"
|
44
|
+
|
45
|
+
puts ">> Creating index"
|
46
|
+
res = coll.create_index "all", :_id => 1, :number => 1, :rndm => 1, :msg => 1
|
47
|
+
# res = coll.create_index "all", '_id' => 1, 'number' => 1, 'rndm' => 1, 'msg' => 1
|
48
|
+
puts "created index: #{res.inspect}"
|
49
|
+
# ============================ Mongo Log ============================
|
50
|
+
# Fri Dec 5 14:45:02 Adding all existing records for ruby-mongo-console.test to new index
|
51
|
+
# ***
|
52
|
+
# Bad data or size in BSONElement::size()
|
53
|
+
# bad type:30
|
54
|
+
# totalsize:11 fieldnamesize:4
|
55
|
+
# lastrec:
|
56
|
+
# Fri Dec 5 14:45:02 ruby-mongo-console.system.indexes Assertion failure false jsobj.cpp a0
|
57
|
+
# Fri Dec 5 14:45:02 database: ruby-mongo-console op:7d2 0
|
58
|
+
# Fri Dec 5 14:45:02 ns: ruby-mongo-console.system.indexes
|
59
|
+
|
60
|
+
puts ">> Gathering index information"
|
61
|
+
begin
|
62
|
+
res = coll.index_information
|
63
|
+
puts "index_information : #{res.inspect}"
|
64
|
+
rescue => e
|
65
|
+
puts "Error: #{e.errmsg}"
|
66
|
+
end
|
67
|
+
# ============================ Console Output ============================
|
68
|
+
# RuntimeError: Keys for index on return from db was nil. Coll = ruby-mongo-console.test
|
69
|
+
# from ./bin/../lib/mongo/db.rb:135:in `index_information'
|
70
|
+
# from (irb):11:in `collect'
|
71
|
+
# from ./bin/../lib/mongo/cursor.rb:47:in `each'
|
72
|
+
# from ./bin/../lib/mongo/db.rb:130:in `collect'
|
73
|
+
# from ./bin/../lib/mongo/db.rb:130:in `index_information'
|
74
|
+
# from ./bin/../lib/mongo/collection.rb:74:in `index_information'
|
75
|
+
# from (irb):11
|
76
|
+
|
77
|
+
puts ">> Dropping index"
|
78
|
+
begin
|
79
|
+
res = coll.drop_index "all_1"
|
80
|
+
puts "dropped : #{res.inspect}"
|
81
|
+
rescue => e
|
82
|
+
puts "Error: #{e.errmsg}"
|
83
|
+
end
|
84
|
+
|
85
|
+
# ============================ Console Output ============================
|
86
|
+
# => {"nIndexesWas"=>2.0, "ok"=>1.0}
|
87
|
+
# ============================ Mongo Log ============================
|
88
|
+
# 0x41802a 0x411549 0x42bac6 0x42c1f6 0x42c55b 0x42e6f7 0x41631e 0x41a89d 0x41ade2 0x41b448 0x4650d2 0x4695ad
|
89
|
+
# db/db(_Z12sayDbContextPKc+0x17a) [0x41802a]
|
90
|
+
# db/db(_Z8assertedPKcS0_j+0x9) [0x411549]
|
91
|
+
# db/db(_ZNK11BSONElement4sizeEv+0x1f6) [0x42bac6]
|
92
|
+
# db/db(_ZN7BSONObj8getFieldEPKc+0xa6) [0x42c1f6]
|
93
|
+
# db/db(_ZN7BSONObj14getFieldDottedEPKc+0x11b) [0x42c55b]
|
94
|
+
# db/db(_ZN7BSONObj19extractFieldsDottedES_R14BSONObjBuilder+0x87) [0x42e6f7]
|
95
|
+
# db/db(_ZN12IndexDetails17getKeysFromObjectER7BSONObjRSt3setIS0_St4lessIS0_ESaIS0_EE+0x24e) [0x41631e]
|
96
|
+
# db/db(_Z12_indexRecordR12IndexDetailsR7BSONObj7DiskLoc+0x5d) [0x41a89d]
|
97
|
+
# db/db(_Z18addExistingToIndexPKcR12IndexDetails+0xb2) [0x41ade2]
|
98
|
+
# db/db(_ZN11DataFileMgr6insertEPKcPKvib+0x508) [0x41b448]
|
99
|
+
# db/db(_Z14receivedInsertR7MessageRSt18basic_stringstreamIcSt11char_traitsIcESaIcEE+0x112) [0x4650d2]
|
100
|
+
# db/db(_Z10connThreadv+0xb4d) [0x4695ad]
|
101
|
+
# Fri Dec 5 14:45:02 ruby-mongo-console.system.indexes Caught Assertion insert, continuing
|
102
|
+
# Fri Dec 5 14:47:59 CMD: deleteIndexes ruby-mongo-console.test
|
103
|
+
# d->nIndexes was 2
|
104
|
+
# alpha implementation, space not reclaimed
|
105
|
+
|
106
|
+
puts ">> Gathering index information"
|
107
|
+
begin
|
108
|
+
res = coll.index_information
|
109
|
+
puts "index_information : #{res.inspect}"
|
110
|
+
rescue => e
|
111
|
+
puts "Error: #{e.errmsg}"
|
112
|
+
end
|
113
|
+
# ============================ Console Output ============================
|
114
|
+
# RuntimeError: Keys for index on return from db was nil. Coll = ruby-mongo-console.test
|
115
|
+
# from ./bin/../lib/mongo/db.rb:135:in `index_information'
|
116
|
+
# from (irb):15:in `collect'
|
117
|
+
# from ./bin/../lib/mongo/cursor.rb:47:in `each'
|
118
|
+
# from ./bin/../lib/mongo/db.rb:130:in `collect'
|
119
|
+
# from ./bin/../lib/mongo/db.rb:130:in `index_information'
|
120
|
+
# from ./bin/../lib/mongo/collection.rb:74:in `index_information'
|
121
|
+
# from (irb):15
|
122
|
+
|
123
|
+
puts ">> Closing connection"
|
124
|
+
db.close
|
125
|
+
puts "closed"
|
data/examples/info.rb
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
$LOAD_PATH[0,0] = File.join(File.dirname(__FILE__), '..', 'lib')
|
2
|
+
require 'mongo'
|
3
|
+
|
4
|
+
include Mongo
|
5
|
+
|
6
|
+
host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
|
7
|
+
port = ENV['MONGO_RUBY_DRIVER_PORT'] || Connection::DEFAULT_PORT
|
8
|
+
|
9
|
+
puts "Connecting to #{host}:#{port}"
|
10
|
+
db = Connection.new(host, port).db('ruby-mongo-examples')
|
11
|
+
coll = db.collection('test')
|
12
|
+
|
13
|
+
# Erase all records from collection, if any
|
14
|
+
coll.clear
|
15
|
+
|
16
|
+
# Insert 3 records
|
17
|
+
3.times { |i| coll.insert({'a' => i+1}) }
|
18
|
+
|
19
|
+
# Collection names in database
|
20
|
+
p db.collection_names
|
21
|
+
|
22
|
+
# More information about each collection
|
23
|
+
p db.collections_info
|
24
|
+
|
25
|
+
# Index information
|
26
|
+
db.create_index('test', 'a')
|
27
|
+
p db.index_information('test')
|
28
|
+
|
29
|
+
# Destroy the collection
|
30
|
+
coll.drop
|
data/examples/queries.rb
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
$LOAD_PATH[0,0] = File.join(File.dirname(__FILE__), '..', 'lib')
|
2
|
+
require 'mongo'
|
3
|
+
require 'pp'
|
4
|
+
|
5
|
+
include Mongo
|
6
|
+
|
7
|
+
host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
|
8
|
+
port = ENV['MONGO_RUBY_DRIVER_PORT'] || Connection::DEFAULT_PORT
|
9
|
+
|
10
|
+
puts "Connecting to #{host}:#{port}"
|
11
|
+
db = Connection.new(host, port).db('ruby-mongo-examples')
|
12
|
+
coll = db.collection('test')
|
13
|
+
|
14
|
+
# Remove all records, if any
|
15
|
+
coll.clear
|
16
|
+
|
17
|
+
# Insert three records
|
18
|
+
coll.insert('a' => 1)
|
19
|
+
coll.insert('a' => 2)
|
20
|
+
coll.insert('b' => 3)
|
21
|
+
|
22
|
+
# Count.
|
23
|
+
puts "There are #{coll.count()} records."
|
24
|
+
|
25
|
+
# Find all records. find() returns a Cursor.
|
26
|
+
cursor = coll.find()
|
27
|
+
|
28
|
+
# Print them. Note that all records have an _id automatically added by the
|
29
|
+
# database. See pk.rb for an example of how to use a primary key factory to
|
30
|
+
# generate your own values for _id.
|
31
|
+
cursor.each { |row| pp row }
|
32
|
+
|
33
|
+
# Cursor has a to_a method that slurps all records into memory.
|
34
|
+
rows = coll.find().to_a
|
35
|
+
rows.each { |row| pp row }
|
36
|
+
|
37
|
+
# See Collection#find. From now on in this file, we won't be printing the
|
38
|
+
# records we find.
|
39
|
+
coll.find('a' => 1)
|
40
|
+
|
41
|
+
# Find records sort by 'a', skip 1, limit 2 records.
|
42
|
+
# Sort can be single name, array, or hash.
|
43
|
+
coll.find({}, {:skip => 1, :limit => 2, :sort => 'a'})
|
44
|
+
|
45
|
+
# Find all records with 'a' > 1. There is also $lt, $gte, and $lte.
|
46
|
+
coll.find({'a' => {'$gt' => 1}})
|
47
|
+
coll.find({'a' => {'$gt' => 1, '$lte' => 3}})
|
48
|
+
|
49
|
+
# Find all records with 'a' in a set of values.
|
50
|
+
coll.find('a' => {'$in' => [1,2]})
|
51
|
+
|
52
|
+
# Find by regexp
|
53
|
+
coll.find('a' => /[1|2]/)
|
54
|
+
|
55
|
+
# Print query explanation
|
56
|
+
pp coll.find('a' => /[1|2]/).explain()
|
57
|
+
|
58
|
+
# Use a hint with a query. Need an index. Hints can be stored with the
|
59
|
+
# collection, in which case they will be used with all queries, or they can be
|
60
|
+
# specified per query, in which case that hint overrides the hint associated
|
61
|
+
# with the collection if any.
|
62
|
+
coll.create_index('a')
|
63
|
+
coll.hint = 'a'
|
64
|
+
|
65
|
+
# You will see a different explanation now that the hint is in place
|
66
|
+
pp coll.find('a' => /[1|2]/).explain()
|
67
|
+
|
68
|
+
# Override hint for single query
|
69
|
+
coll.find({'a' => 1}, :hint => 'b')
|
data/examples/simple.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
$LOAD_PATH[0,0] = File.join(File.dirname(__FILE__), '..', 'lib')
|
2
|
+
require 'mongo'
|
3
|
+
|
4
|
+
include Mongo
|
5
|
+
|
6
|
+
host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
|
7
|
+
port = ENV['MONGO_RUBY_DRIVER_PORT'] || Connection::DEFAULT_PORT
|
8
|
+
|
9
|
+
puts "Connecting to #{host}:#{port}"
|
10
|
+
db = Connection.new(host, port).db('ruby-mongo-examples')
|
11
|
+
coll = db.collection('test')
|
12
|
+
|
13
|
+
# Erase all records from collection, if any
|
14
|
+
coll.clear
|
15
|
+
|
16
|
+
# Insert 3 records
|
17
|
+
3.times { |i| coll.insert({'a' => i+1}) }
|
18
|
+
|
19
|
+
puts "There are #{coll.count()} records in the test collection. Here they are:"
|
20
|
+
coll.find().each { |doc| puts doc.inspect }
|
21
|
+
|
22
|
+
# Destroy the collection
|
23
|
+
coll.drop
|
data/examples/strict.rb
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
$LOAD_PATH[0,0] = File.join(File.dirname(__FILE__), '..', 'lib')
|
2
|
+
require 'mongo'
|
3
|
+
|
4
|
+
include Mongo
|
5
|
+
|
6
|
+
host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
|
7
|
+
port = ENV['MONGO_RUBY_DRIVER_PORT'] || Connection::DEFAULT_PORT
|
8
|
+
|
9
|
+
puts "Connecting to #{host}:#{port}"
|
10
|
+
db = Connection.new(host, port).db('ruby-mongo-examples')
|
11
|
+
|
12
|
+
db.drop_collection('does-not-exist')
|
13
|
+
db.create_collection('test')
|
14
|
+
|
15
|
+
db.strict = true
|
16
|
+
|
17
|
+
begin
|
18
|
+
# Can't reference collection that does not exist
|
19
|
+
db.collection('does-not-exist')
|
20
|
+
puts "error: expected exception"
|
21
|
+
rescue => ex
|
22
|
+
puts "expected exception: #{ex}"
|
23
|
+
end
|
24
|
+
|
25
|
+
begin
|
26
|
+
# Can't create collection that already exists
|
27
|
+
db.create_collection('test')
|
28
|
+
puts "error: expected exception"
|
29
|
+
rescue => ex
|
30
|
+
puts "expected exception: #{ex}"
|
31
|
+
end
|
32
|
+
|
33
|
+
db.strict = false
|
34
|
+
db.drop_collection('test')
|
data/examples/types.rb
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
$LOAD_PATH[0,0] = File.join(File.dirname(__FILE__), '..', 'lib')
|
2
|
+
require 'mongo'
|
3
|
+
require 'pp'
|
4
|
+
|
5
|
+
include Mongo
|
6
|
+
|
7
|
+
host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
|
8
|
+
port = ENV['MONGO_RUBY_DRIVER_PORT'] || Connection::DEFAULT_PORT
|
9
|
+
|
10
|
+
puts "Connecting to #{host}:#{port}"
|
11
|
+
db = Connection.new(host, port).db('ruby-mongo-examples')
|
12
|
+
coll = db.collection('test')
|
13
|
+
|
14
|
+
# Remove all records, if any
|
15
|
+
coll.clear
|
16
|
+
|
17
|
+
# Insert record with all sorts of values
|
18
|
+
coll.insert('array' => [1, 2, 3],
|
19
|
+
'string' => 'hello',
|
20
|
+
'hash' => {'a' => 1, 'b' => 2},
|
21
|
+
'date' => Time.now, # milliseconds only; microseconds are not stored
|
22
|
+
'oid' => ObjectID.new,
|
23
|
+
'binary' => Binary.new([1, 2, 3]),
|
24
|
+
'int' => 42,
|
25
|
+
'float' => 33.33333,
|
26
|
+
'regex' => /foobar/i,
|
27
|
+
'boolean' => true,
|
28
|
+
'where' => Code.new('this.x == 3'),
|
29
|
+
'dbref' => DBRef.new(coll.name, ObjectID.new),
|
30
|
+
'null' => nil,
|
31
|
+
'symbol' => :zildjian)
|
32
|
+
|
33
|
+
pp coll.find().next_object
|
34
|
+
|
35
|
+
coll.clear
|
data/lib/mongo.rb
CHANGED
@@ -2,11 +2,18 @@ require 'mongo/types/binary'
|
|
2
2
|
require 'mongo/types/dbref'
|
3
3
|
require 'mongo/types/objectid'
|
4
4
|
require 'mongo/types/regexp_of_holding'
|
5
|
-
require 'mongo/types/undefined'
|
6
5
|
|
7
|
-
require 'mongo/
|
6
|
+
require 'mongo/errors'
|
7
|
+
require 'mongo/connection'
|
8
8
|
require 'mongo/message'
|
9
9
|
require 'mongo/db'
|
10
10
|
require 'mongo/cursor'
|
11
11
|
require 'mongo/collection'
|
12
12
|
require 'mongo/admin'
|
13
|
+
|
14
|
+
module Mongo
|
15
|
+
ASCENDING = 1
|
16
|
+
DESCENDING = -1
|
17
|
+
|
18
|
+
VERSION = "0.15"
|
19
|
+
end
|
data/lib/mongo/admin.rb
CHANGED
@@ -1,86 +1,83 @@
|
|
1
1
|
# --
|
2
2
|
# Copyright (C) 2008-2009 10gen Inc.
|
3
3
|
#
|
4
|
-
#
|
5
|
-
#
|
6
|
-
#
|
4
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
5
|
+
# you may not use this file except in compliance with the License.
|
6
|
+
# You may obtain a copy of the License at
|
7
7
|
#
|
8
|
-
#
|
9
|
-
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
10
|
-
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License
|
11
|
-
# for more details.
|
8
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
12
9
|
#
|
13
|
-
#
|
14
|
-
#
|
10
|
+
# Unless required by applicable law or agreed to in writing, software
|
11
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
12
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13
|
+
# See the License for the specific language governing permissions and
|
14
|
+
# limitations under the License.
|
15
15
|
# ++
|
16
16
|
|
17
17
|
require 'mongo/util/ordered_hash'
|
18
18
|
|
19
|
-
module
|
20
|
-
module Mongo
|
21
|
-
module Driver
|
19
|
+
module Mongo
|
22
20
|
|
23
|
-
|
24
|
-
|
25
|
-
|
21
|
+
# Provide administrative database methods: those having to do with
|
22
|
+
# profiling and validation.
|
23
|
+
class Admin
|
26
24
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
# Return the current database profiling level.
|
32
|
-
def profiling_level
|
33
|
-
oh = OrderedHash.new
|
34
|
-
oh[:profile] = -1
|
35
|
-
doc = @db.db_command(oh)
|
36
|
-
raise "Error with profile command: #{doc.inspect}" unless @db.ok?(doc) && doc['was'].kind_of?(Numeric)
|
37
|
-
case doc['was'].to_i
|
38
|
-
when 0
|
39
|
-
:off
|
40
|
-
when 1
|
41
|
-
:slow_only
|
42
|
-
when 2
|
43
|
-
:all
|
44
|
-
else
|
45
|
-
raise "Error: illegal profiling level value #{doc['was']}"
|
46
|
-
end
|
47
|
-
end
|
25
|
+
def initialize(db)
|
26
|
+
@db = db
|
27
|
+
end
|
48
28
|
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
29
|
+
# Return the current database profiling level.
|
30
|
+
def profiling_level
|
31
|
+
oh = OrderedHash.new
|
32
|
+
oh[:profile] = -1
|
33
|
+
doc = @db.db_command(oh)
|
34
|
+
raise "Error with profile command: #{doc.inspect}" unless @db.ok?(doc) && doc['was'].kind_of?(Numeric)
|
35
|
+
case doc['was'].to_i
|
36
|
+
when 0
|
37
|
+
:off
|
38
|
+
when 1
|
39
|
+
:slow_only
|
40
|
+
when 2
|
41
|
+
:all
|
42
|
+
else
|
43
|
+
raise "Error: illegal profiling level value #{doc['was']}"
|
44
|
+
end
|
45
|
+
end
|
65
46
|
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
47
|
+
# Set database profiling level to :off, :slow_only, or :all.
|
48
|
+
def profiling_level=(level)
|
49
|
+
oh = OrderedHash.new
|
50
|
+
oh[:profile] = case level
|
51
|
+
when :off
|
52
|
+
0
|
53
|
+
when :slow_only
|
54
|
+
1
|
55
|
+
when :all
|
56
|
+
2
|
57
|
+
else
|
58
|
+
raise "Error: illegal profiling level value #{level}"
|
59
|
+
end
|
60
|
+
doc = @db.db_command(oh)
|
61
|
+
raise "Error with profile command: #{doc.inspect}" unless @db.ok?(doc)
|
62
|
+
end
|
71
63
|
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
result = doc['result']
|
78
|
-
raise "Error with validation data: #{doc.inspect}" unless result.kind_of?(String)
|
79
|
-
raise "Error: invalid collection #{name}: #{doc.inspect}" if result =~ /\b(exception|corrupt)\b/i
|
80
|
-
true
|
81
|
-
end
|
64
|
+
# Return an array contining current profiling information from the
|
65
|
+
# database.
|
66
|
+
def profiling_info
|
67
|
+
@db.query(Collection.new(@db, DB::SYSTEM_PROFILE_COLLECTION), Query.new({})).to_a
|
68
|
+
end
|
82
69
|
|
83
|
-
|
70
|
+
# Validate a named collection by raising an exception if there is a
|
71
|
+
# problem or returning an interesting hash (see especially the
|
72
|
+
# 'result' string value) if all is well.
|
73
|
+
def validate_collection(name)
|
74
|
+
doc = @db.db_command(:validate => name)
|
75
|
+
raise "Error with validate command: #{doc.inspect}" unless @db.ok?(doc)
|
76
|
+
result = doc['result']
|
77
|
+
raise "Error with validation data: #{doc.inspect}" unless result.kind_of?(String)
|
78
|
+
raise "Error: invalid collection #{name}: #{doc.inspect}" if result =~ /\b(exception|corrupt)\b/i
|
79
|
+
doc
|
84
80
|
end
|
81
|
+
|
85
82
|
end
|
86
83
|
end
|