mongo 0.18.2 → 0.18.3
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +37 -28
- data/Rakefile +19 -0
- data/bin/objectid_benchmark.rb +23 -0
- data/examples/admin.rb +7 -6
- data/examples/capped.rb +3 -4
- data/examples/cursor.rb +4 -3
- data/examples/gridfs.rb +3 -2
- data/examples/index_test.rb +10 -9
- data/examples/info.rb +3 -2
- data/examples/queries.rb +3 -2
- data/examples/simple.rb +3 -2
- data/examples/strict.rb +2 -1
- data/examples/types.rb +4 -3
- data/lib/mongo.rb +29 -12
- data/lib/mongo/admin.rb +17 -2
- data/lib/mongo/collection.rb +230 -169
- data/lib/mongo/connection.rb +136 -91
- data/lib/mongo/cursor.rb +68 -40
- data/lib/mongo/db.rb +247 -123
- data/lib/mongo/{errors.rb → exceptions.rb} +6 -5
- data/lib/mongo/gridfs.rb +6 -0
- data/lib/mongo/gridfs/grid_store.rb +142 -94
- data/lib/mongo/types/binary.rb +11 -1
- data/lib/mongo/types/code.rb +6 -1
- data/lib/mongo/types/dbref.rb +7 -2
- data/lib/mongo/types/min_max_keys.rb +58 -0
- data/lib/mongo/types/objectid.rb +76 -20
- data/lib/mongo/types/regexp_of_holding.rb +5 -0
- data/lib/mongo/util/bson_ruby.rb +36 -2
- data/lib/mongo/util/byte_buffer.rb +18 -2
- data/lib/mongo/util/conversions.rb +6 -5
- data/lib/mongo/util/ordered_hash.rb +3 -1
- data/lib/mongo/util/support.rb +3 -0
- data/lib/mongo/util/xml_to_ruby.rb +7 -0
- data/test/test_bson.rb +48 -0
- data/test/test_collection.rb +13 -0
- data/test/test_connection.rb +35 -0
- data/test/test_conversions.rb +1 -1
- data/test/test_cursor.rb +37 -5
- data/test/test_db.rb +51 -2
- data/test/test_db_api.rb +4 -7
- data/test/test_grid_store.rb +10 -0
- data/test/test_objectid.rb +16 -2
- data/test/test_ordered_hash.rb +14 -0
- data/test/threading/test_threading_large_pool.rb +4 -4
- data/test/unit/db_test.rb +43 -0
- metadata +5 -7
- data/examples/benchmarks.rb +0 -42
- data/examples/blog.rb +0 -76
- data/lib/mongo/constants.rb +0 -15
- data/test/mongo-qa/_common.rb +0 -8
data/test/test_collection.rb
CHANGED
@@ -191,6 +191,19 @@ class TestCollection < Test::Unit::TestCase
|
|
191
191
|
end
|
192
192
|
end
|
193
193
|
|
194
|
+
def test_safe_remove
|
195
|
+
@conn = Connection.new
|
196
|
+
@db = @conn['mongo-ruby-test']
|
197
|
+
@test = @db['test-safe-remove']
|
198
|
+
@test.save({:a => 20})
|
199
|
+
@conn.stubs(:receive).returns([[{'ok' => 0, 'err' => 'failed'}], 1, 0])
|
200
|
+
|
201
|
+
assert_raise OperationFailure do
|
202
|
+
@test.remove({}, :safe => true)
|
203
|
+
end
|
204
|
+
@test.drop
|
205
|
+
end
|
206
|
+
|
194
207
|
def test_count
|
195
208
|
@@test.drop
|
196
209
|
|
data/test/test_connection.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
require 'test/test_helper'
|
2
2
|
require 'logger'
|
3
3
|
require 'stringio'
|
4
|
+
require 'thread'
|
4
5
|
|
5
6
|
# NOTE: assumes Mongo is running
|
6
7
|
class TestConnection < Test::Unit::TestCase
|
@@ -122,4 +123,38 @@ class TestConnection < Test::Unit::TestCase
|
|
122
123
|
assert_equal ['bar', Connection::DEFAULT_PORT], nodes[0]
|
123
124
|
assert_equal ['foo', 123], nodes[1]
|
124
125
|
end
|
126
|
+
|
127
|
+
context "Connection exceptions" do
|
128
|
+
setup do
|
129
|
+
@conn = Mongo::Connection.new('localhost', 27017, :pool_size => 10, :timeout => 10)
|
130
|
+
@coll = @conn['mongo-ruby-test']['test-connection-exceptions']
|
131
|
+
end
|
132
|
+
|
133
|
+
should "release connection if an exception is raised on send_message" do
|
134
|
+
@conn.stubs(:send_message_on_socket).raises(ConnectionFailure)
|
135
|
+
assert_equal 0, @conn.checked_out.size
|
136
|
+
assert_raise ConnectionFailure do
|
137
|
+
@coll.insert({:test => "insert"})
|
138
|
+
end
|
139
|
+
assert_equal 0, @conn.checked_out.size
|
140
|
+
end
|
141
|
+
|
142
|
+
should "release connection if an exception is raised on send_with_safe_check" do
|
143
|
+
@conn.stubs(:receive).raises(ConnectionFailure)
|
144
|
+
assert_equal 0, @conn.checked_out.size
|
145
|
+
assert_raise ConnectionFailure do
|
146
|
+
@coll.insert({:test => "insert"}, :safe => true)
|
147
|
+
end
|
148
|
+
assert_equal 0, @conn.checked_out.size
|
149
|
+
end
|
150
|
+
|
151
|
+
should "release connection if an exception is raised on receive_message" do
|
152
|
+
@conn.stubs(:receive).raises(ConnectionFailure)
|
153
|
+
assert_equal 0, @conn.checked_out.size
|
154
|
+
assert_raise ConnectionFailure do
|
155
|
+
@coll.find.to_a
|
156
|
+
end
|
157
|
+
assert_equal 0, @conn.checked_out.size
|
158
|
+
end
|
159
|
+
end
|
125
160
|
end
|
data/test/test_conversions.rb
CHANGED
data/test/test_cursor.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
require 'test/test_helper'
|
2
|
+
require 'logger'
|
2
3
|
|
3
4
|
# NOTE: assumes Mongo is running
|
4
5
|
class CursorTest < Test::Unit::TestCase
|
@@ -17,11 +18,6 @@ class CursorTest < Test::Unit::TestCase
|
|
17
18
|
@@coll_full_name = 'ruby-mongo-test.test'
|
18
19
|
end
|
19
20
|
|
20
|
-
def teardown
|
21
|
-
@@coll.remove
|
22
|
-
@@db.error
|
23
|
-
end
|
24
|
-
|
25
21
|
def test_explain
|
26
22
|
cursor = @@coll.find('a' => 1)
|
27
23
|
explaination = cursor.explain
|
@@ -112,6 +108,42 @@ class CursorTest < Test::Unit::TestCase
|
|
112
108
|
assert_equal 2004, @@coll.find().sort([[:created_at, :desc]]).next_document["created_at"].year
|
113
109
|
end
|
114
110
|
|
111
|
+
def test_sort_min_max_keys
|
112
|
+
@@coll.remove
|
113
|
+
@@coll.insert({"n" => 1000000})
|
114
|
+
@@coll.insert({"n" => -1000000})
|
115
|
+
@@coll.insert({"n" => MaxKey.new})
|
116
|
+
@@coll.insert({"n" => MinKey.new})
|
117
|
+
|
118
|
+
results = @@coll.find.sort([:n, :asc]).to_a
|
119
|
+
|
120
|
+
assert_equal MinKey.new, results[0]['n']
|
121
|
+
assert_equal -1000000, results[1]['n']
|
122
|
+
assert_equal 1000000, results[2]['n']
|
123
|
+
assert_equal MaxKey.new, results[3]['n']
|
124
|
+
end
|
125
|
+
|
126
|
+
def test_id_range_queries
|
127
|
+
@@coll.remove
|
128
|
+
|
129
|
+
t1 = Time.now
|
130
|
+
t1_id = ObjectID.from_time(t1)
|
131
|
+
@@coll.save({:t => 't1'})
|
132
|
+
@@coll.save({:t => 't1'})
|
133
|
+
@@coll.save({:t => 't1'})
|
134
|
+
sleep(2)
|
135
|
+
t2 = Time.now
|
136
|
+
t2_id = ObjectID.from_time(t2)
|
137
|
+
@@coll.save({:t => 't2'})
|
138
|
+
@@coll.save({:t => 't2'})
|
139
|
+
@@coll.save({:t => 't2'})
|
140
|
+
|
141
|
+
assert_equal 3, @@coll.find({'_id' => {'$gt' => t1_id}, '_id' => {'$lt' => t2_id}}).count
|
142
|
+
@@coll.find({'_id' => {'$gt' => t2_id}}).each do |doc|
|
143
|
+
assert_equal 't2', doc['t']
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
115
147
|
def test_limit
|
116
148
|
@@coll.remove
|
117
149
|
|
data/test/test_db.rb
CHANGED
@@ -132,7 +132,7 @@ class DBTest < Test::Unit::TestCase
|
|
132
132
|
db.pk_factory = Object.new
|
133
133
|
fail "error: expected exception"
|
134
134
|
rescue => ex
|
135
|
-
assert_match /
|
135
|
+
assert_match /Cannot change/, ex.to_s
|
136
136
|
ensure
|
137
137
|
conn.close
|
138
138
|
end
|
@@ -145,7 +145,7 @@ class DBTest < Test::Unit::TestCase
|
|
145
145
|
end
|
146
146
|
|
147
147
|
def test_logout
|
148
|
-
@@db.logout
|
148
|
+
assert @@db.logout
|
149
149
|
end
|
150
150
|
|
151
151
|
def test_error
|
@@ -202,4 +202,53 @@ class DBTest < Test::Unit::TestCase
|
|
202
202
|
assert db.collection('users').remove
|
203
203
|
end
|
204
204
|
|
205
|
+
context "database profiling" do
|
206
|
+
setup do
|
207
|
+
@db = @@conn['ruby-mongo-test-admin-functions']
|
208
|
+
@coll = @db['test']
|
209
|
+
@coll.remove
|
210
|
+
@r1 = @coll.insert('a' => 1) # collection not created until it's used
|
211
|
+
end
|
212
|
+
|
213
|
+
should "set default profiling level" do
|
214
|
+
assert_equal :off, @db.profiling_level
|
215
|
+
end
|
216
|
+
|
217
|
+
should "change profiling level" do
|
218
|
+
@db.profiling_level = :slow_only
|
219
|
+
assert_equal :slow_only, @db.profiling_level
|
220
|
+
@db.profiling_level = :off
|
221
|
+
assert_equal :off, @db.profiling_level
|
222
|
+
@db.profiling_level = :all
|
223
|
+
assert_equal :all, @db.profiling_level
|
224
|
+
begin
|
225
|
+
@db.profiling_level = :medium
|
226
|
+
fail "shouldn't be able to do this"
|
227
|
+
rescue
|
228
|
+
end
|
229
|
+
end
|
230
|
+
|
231
|
+
should "return profiling info" do
|
232
|
+
@db.profiling_level = :all
|
233
|
+
@coll.find()
|
234
|
+
@db.profiling_level = :off
|
235
|
+
|
236
|
+
info = @db.profiling_info
|
237
|
+
assert_kind_of Array, info
|
238
|
+
assert info.length >= 1
|
239
|
+
first = info.first
|
240
|
+
assert_kind_of String, first['info']
|
241
|
+
assert_kind_of Time, first['ts']
|
242
|
+
assert_kind_of Numeric, first['millis']
|
243
|
+
end
|
244
|
+
|
245
|
+
should "validate collection" do
|
246
|
+
doc = @db.validate_collection(@coll.name)
|
247
|
+
assert_not_nil doc
|
248
|
+
result = doc['result']
|
249
|
+
assert_not_nil result
|
250
|
+
assert_match /firstExtent/, result
|
251
|
+
end
|
252
|
+
|
253
|
+
end
|
205
254
|
end
|
data/test/test_db_api.rb
CHANGED
@@ -419,6 +419,7 @@ class DBAPITest < Test::Unit::TestCase
|
|
419
419
|
@@db.collection('does-not-exist')
|
420
420
|
fail "expected exception"
|
421
421
|
rescue => ex
|
422
|
+
assert_equal MongoDBError, ex.class
|
422
423
|
assert_equal "Collection does-not-exist doesn't exist. Currently in strict mode.", ex.to_s
|
423
424
|
ensure
|
424
425
|
@@db.strict = false
|
@@ -438,15 +439,11 @@ class DBAPITest < Test::Unit::TestCase
|
|
438
439
|
end
|
439
440
|
|
440
441
|
# Now the collection exists. This time we should see an exception.
|
441
|
-
|
442
|
+
assert_raise MongoDBError do
|
442
443
|
@@db.create_collection('foobar')
|
443
|
-
fail "expected exception"
|
444
|
-
rescue => ex
|
445
|
-
assert_equal "Collection foobar already exists. Currently in strict mode.", ex.to_s
|
446
|
-
ensure
|
447
|
-
@@db.strict = false
|
448
|
-
@@db.drop_collection('foobar')
|
449
444
|
end
|
445
|
+
@@db.strict = false
|
446
|
+
@@db.drop_collection('foobar')
|
450
447
|
|
451
448
|
# Now we're not in strict mode - should succeed
|
452
449
|
@@db.create_collection('foobar')
|
data/test/test_grid_store.rb
CHANGED
@@ -151,6 +151,16 @@ class GridStoreTest < Test::Unit::TestCase
|
|
151
151
|
assert_equal 0, @@files.count
|
152
152
|
assert_equal 0, @@chunks.count
|
153
153
|
end
|
154
|
+
|
155
|
+
def test_mv
|
156
|
+
assert_equal 1, @@files.count
|
157
|
+
assert_equal 1, @@chunks.count
|
158
|
+
GridStore.mv(@@db, 'foobar', 'bazqux')
|
159
|
+
assert_equal 1, @@files.count
|
160
|
+
assert_equal 1, @@chunks.count
|
161
|
+
assert !GridStore.exist?(@@db, 'foobar')
|
162
|
+
assert GridStore.exist?(@@db, 'bazqux')
|
163
|
+
end
|
154
164
|
|
155
165
|
def test_append
|
156
166
|
GridStore.open(@@db, 'foobar', 'w+') { |f| f.write(" how are you?") }
|
data/test/test_objectid.rb
CHANGED
@@ -23,7 +23,7 @@ class ObjectIDTest < Test::Unit::TestCase
|
|
23
23
|
doc = {:name => 'Mongo'}
|
24
24
|
doc = ObjectID.create_pk(doc)
|
25
25
|
assert doc[:_id]
|
26
|
-
|
26
|
+
|
27
27
|
doc = {:name => 'Mongo', :_id => '12345'}
|
28
28
|
doc = ObjectID.create_pk(doc)
|
29
29
|
assert_equal '12345', doc[:_id]
|
@@ -136,7 +136,21 @@ class ObjectIDTest < Test::Unit::TestCase
|
|
136
136
|
def test_generation_time
|
137
137
|
time = Time.now
|
138
138
|
id = ObjectID.new
|
139
|
+
generated_time = id.generation_time
|
140
|
+
|
141
|
+
assert_in_delta time.to_i, generated_time.to_i, 2
|
142
|
+
assert_equal "UTC", generated_time.zone
|
143
|
+
end
|
144
|
+
|
145
|
+
def test_from_time
|
146
|
+
time = Time.now.utc
|
147
|
+
id = ObjectID.from_time(time)
|
148
|
+
|
149
|
+
assert_equal time.to_i, id.generation_time.to_i
|
150
|
+
end
|
139
151
|
|
140
|
-
|
152
|
+
def test_json
|
153
|
+
id = ObjectID.new
|
154
|
+
assert_equal "{\"$oid\": \"#{id}\"}", id.to_json
|
141
155
|
end
|
142
156
|
end
|
data/test/test_ordered_hash.rb
CHANGED
@@ -19,6 +19,12 @@ class OrderedHashTest < Test::Unit::TestCase
|
|
19
19
|
assert_equal a, b
|
20
20
|
end
|
21
21
|
|
22
|
+
def test_hash_code
|
23
|
+
o = OrderedHash.new
|
24
|
+
o['number'] = 50
|
25
|
+
assert o.hash
|
26
|
+
end
|
27
|
+
|
22
28
|
def test_empty
|
23
29
|
assert_equal [], OrderedHash.new.keys
|
24
30
|
end
|
@@ -130,6 +136,14 @@ class OrderedHashTest < Test::Unit::TestCase
|
|
130
136
|
assert_equal ['crab', 'apple', 3, 'foo'], @oh.values
|
131
137
|
end
|
132
138
|
|
139
|
+
def test_update
|
140
|
+
other = OrderedHash.new
|
141
|
+
other['f'] = 'foo'
|
142
|
+
noob = @oh.update(other)
|
143
|
+
assert_equal @ordered_keys + ['f'], noob.keys
|
144
|
+
assert_equal [1, 2, 3, 'foo'], noob.values
|
145
|
+
end
|
146
|
+
|
133
147
|
def test_inspect_retains_order
|
134
148
|
assert_equal '{"c"=>1, "a"=>2, "z"=>3}', @oh.inspect
|
135
149
|
end
|
@@ -25,7 +25,7 @@ class TestThreadingLargePool < Test::Unit::TestCase
|
|
25
25
|
def test_safe_update
|
26
26
|
set_up_safe_data
|
27
27
|
threads = []
|
28
|
-
|
28
|
+
300.times do |i|
|
29
29
|
threads[i] = Thread.new do
|
30
30
|
if i % 2 == 0
|
31
31
|
assert_raise Mongo::OperationFailure do
|
@@ -37,7 +37,7 @@ class TestThreadingLargePool < Test::Unit::TestCase
|
|
37
37
|
end
|
38
38
|
end
|
39
39
|
|
40
|
-
|
40
|
+
300.times do |i|
|
41
41
|
threads[i].join
|
42
42
|
end
|
43
43
|
end
|
@@ -45,7 +45,7 @@ class TestThreadingLargePool < Test::Unit::TestCase
|
|
45
45
|
def test_safe_insert
|
46
46
|
set_up_safe_data
|
47
47
|
threads = []
|
48
|
-
|
48
|
+
300.times do |i|
|
49
49
|
threads[i] = Thread.new do
|
50
50
|
if i % 2 == 0
|
51
51
|
assert_raise Mongo::OperationFailure do
|
@@ -57,7 +57,7 @@ class TestThreadingLargePool < Test::Unit::TestCase
|
|
57
57
|
end
|
58
58
|
end
|
59
59
|
|
60
|
-
|
60
|
+
300.times do |i|
|
61
61
|
threads[i].join
|
62
62
|
end
|
63
63
|
end
|
data/test/unit/db_test.rb
CHANGED
@@ -48,6 +48,49 @@ class DBTest < Test::Unit::TestCase
|
|
48
48
|
@db.command(command, true, true)
|
49
49
|
end
|
50
50
|
end
|
51
|
+
|
52
|
+
should "raise an error if logging out fails" do
|
53
|
+
@db.expects(:command).returns({})
|
54
|
+
assert_raise MongoDBError do
|
55
|
+
@db.logout
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
should "raise an error if collection creation fails" do
|
60
|
+
@db.expects(:collection_names).returns([])
|
61
|
+
@db.expects(:command).returns({})
|
62
|
+
assert_raise MongoDBError do
|
63
|
+
@db.create_collection("foo")
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
should "raise an error if getlasterror fails" do
|
68
|
+
@db.expects(:command).returns({})
|
69
|
+
assert_raise MongoDBError do
|
70
|
+
@db.error
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
should "raise an error if rename fails" do
|
75
|
+
@db.expects(:command).returns({})
|
76
|
+
assert_raise MongoDBError do
|
77
|
+
@db.rename_collection("foo", "bar")
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
should "raise an error if drop_index fails" do
|
82
|
+
@db.expects(:command).returns({})
|
83
|
+
assert_raise MongoDBError do
|
84
|
+
@db.drop_index("foo", "bar")
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
should "raise an error if set_profiling_level fails" do
|
89
|
+
@db.expects(:command).returns({})
|
90
|
+
assert_raise MongoDBError do
|
91
|
+
@db.profiling_level = :slow_only
|
92
|
+
end
|
93
|
+
end
|
51
94
|
end
|
52
95
|
end
|
53
96
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mongo
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.18.
|
4
|
+
version: 0.18.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jim Menard
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date:
|
13
|
+
date: 2010-01-25 00:00:00 -05:00
|
14
14
|
default_executable:
|
15
15
|
dependencies: []
|
16
16
|
|
@@ -30,16 +30,16 @@ files:
|
|
30
30
|
- lib/mongo/admin.rb
|
31
31
|
- lib/mongo/collection.rb
|
32
32
|
- lib/mongo/connection.rb
|
33
|
-
- lib/mongo/constants.rb
|
34
33
|
- lib/mongo/cursor.rb
|
35
34
|
- lib/mongo/db.rb
|
36
|
-
- lib/mongo/
|
35
|
+
- lib/mongo/exceptions.rb
|
37
36
|
- lib/mongo/gridfs/chunk.rb
|
38
37
|
- lib/mongo/gridfs/grid_store.rb
|
39
38
|
- lib/mongo/gridfs.rb
|
40
39
|
- lib/mongo/types/binary.rb
|
41
40
|
- lib/mongo/types/code.rb
|
42
41
|
- lib/mongo/types/dbref.rb
|
42
|
+
- lib/mongo/types/min_max_keys.rb
|
43
43
|
- lib/mongo/types/objectid.rb
|
44
44
|
- lib/mongo/types/regexp_of_holding.rb
|
45
45
|
- lib/mongo/util/bson_c.rb
|
@@ -52,8 +52,6 @@ files:
|
|
52
52
|
- lib/mongo/util/xml_to_ruby.rb
|
53
53
|
- lib/mongo.rb
|
54
54
|
- examples/admin.rb
|
55
|
-
- examples/benchmarks.rb
|
56
|
-
- examples/blog.rb
|
57
55
|
- examples/capped.rb
|
58
56
|
- examples/cursor.rb
|
59
57
|
- examples/gridfs.rb
|
@@ -65,6 +63,7 @@ files:
|
|
65
63
|
- examples/types.rb
|
66
64
|
- bin/bson_benchmark.rb
|
67
65
|
- bin/fail_if_no_c.rb
|
66
|
+
- bin/objectid_benchmark.rb
|
68
67
|
- bin/perf.rb
|
69
68
|
has_rdoc: true
|
70
69
|
homepage: http://www.mongodb.org
|
@@ -97,7 +96,6 @@ signing_key:
|
|
97
96
|
specification_version: 3
|
98
97
|
summary: Ruby driver for the MongoDB
|
99
98
|
test_files:
|
100
|
-
- test/mongo-qa/_common.rb
|
101
99
|
- test/replica/count_test.rb
|
102
100
|
- test/replica/insert_test.rb
|
103
101
|
- test/replica/pooled_insert_test.rb
|