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.
Files changed (89) hide show
  1. data/README.rdoc +268 -71
  2. data/Rakefile +27 -62
  3. data/bin/bson_benchmark.rb +59 -0
  4. data/bin/mongo_console +3 -3
  5. data/bin/run_test_script +19 -0
  6. data/bin/standard_benchmark +109 -0
  7. data/examples/admin.rb +41 -0
  8. data/examples/benchmarks.rb +42 -0
  9. data/examples/blog.rb +76 -0
  10. data/examples/capped.rb +23 -0
  11. data/examples/cursor.rb +47 -0
  12. data/examples/gridfs.rb +87 -0
  13. data/examples/index_test.rb +125 -0
  14. data/examples/info.rb +30 -0
  15. data/examples/queries.rb +69 -0
  16. data/examples/simple.rb +23 -0
  17. data/examples/strict.rb +34 -0
  18. data/examples/types.rb +35 -0
  19. data/lib/mongo.rb +9 -2
  20. data/lib/mongo/admin.rb +65 -68
  21. data/lib/mongo/collection.rb +379 -117
  22. data/lib/mongo/connection.rb +151 -0
  23. data/lib/mongo/cursor.rb +271 -216
  24. data/lib/mongo/db.rb +500 -315
  25. data/lib/mongo/errors.rb +26 -0
  26. data/lib/mongo/gridfs.rb +16 -0
  27. data/lib/mongo/gridfs/chunk.rb +92 -0
  28. data/lib/mongo/gridfs/grid_store.rb +464 -0
  29. data/lib/mongo/message.rb +16 -0
  30. data/lib/mongo/message/get_more_message.rb +24 -13
  31. data/lib/mongo/message/insert_message.rb +29 -11
  32. data/lib/mongo/message/kill_cursors_message.rb +23 -12
  33. data/lib/mongo/message/message.rb +74 -62
  34. data/lib/mongo/message/message_header.rb +35 -24
  35. data/lib/mongo/message/msg_message.rb +21 -9
  36. data/lib/mongo/message/opcodes.rb +26 -15
  37. data/lib/mongo/message/query_message.rb +63 -43
  38. data/lib/mongo/message/remove_message.rb +29 -12
  39. data/lib/mongo/message/update_message.rb +30 -13
  40. data/lib/mongo/query.rb +97 -89
  41. data/lib/mongo/types/binary.rb +25 -21
  42. data/lib/mongo/types/code.rb +30 -0
  43. data/lib/mongo/types/dbref.rb +19 -23
  44. data/lib/mongo/types/objectid.rb +130 -116
  45. data/lib/mongo/types/regexp_of_holding.rb +27 -31
  46. data/lib/mongo/util/bson.rb +273 -160
  47. data/lib/mongo/util/byte_buffer.rb +32 -28
  48. data/lib/mongo/util/ordered_hash.rb +88 -42
  49. data/lib/mongo/util/xml_to_ruby.rb +18 -15
  50. data/mongo-ruby-driver.gemspec +103 -0
  51. data/test/mongo-qa/_common.rb +8 -0
  52. data/test/mongo-qa/admin +26 -0
  53. data/test/mongo-qa/capped +22 -0
  54. data/test/mongo-qa/count1 +18 -0
  55. data/test/mongo-qa/dbs +22 -0
  56. data/test/mongo-qa/find +10 -0
  57. data/test/mongo-qa/find1 +15 -0
  58. data/test/mongo-qa/gridfs_in +16 -0
  59. data/test/mongo-qa/gridfs_out +17 -0
  60. data/test/mongo-qa/indices +49 -0
  61. data/test/mongo-qa/remove +25 -0
  62. data/test/mongo-qa/stress1 +35 -0
  63. data/test/mongo-qa/test1 +11 -0
  64. data/test/mongo-qa/update +18 -0
  65. data/{tests → test}/test_admin.rb +25 -16
  66. data/test/test_bson.rb +268 -0
  67. data/{tests → test}/test_byte_buffer.rb +0 -0
  68. data/test/test_chunk.rb +84 -0
  69. data/test/test_collection.rb +282 -0
  70. data/test/test_connection.rb +101 -0
  71. data/test/test_cursor.rb +321 -0
  72. data/test/test_db.rb +196 -0
  73. data/test/test_db_api.rb +798 -0
  74. data/{tests → test}/test_db_connection.rb +4 -3
  75. data/test/test_grid_store.rb +284 -0
  76. data/{tests → test}/test_message.rb +1 -1
  77. data/test/test_objectid.rb +105 -0
  78. data/{tests → test}/test_ordered_hash.rb +55 -0
  79. data/{tests → test}/test_round_trip.rb +13 -9
  80. data/test/test_threading.rb +37 -0
  81. metadata +74 -32
  82. data/bin/validate +0 -51
  83. data/lib/mongo/mongo.rb +0 -74
  84. data/lib/mongo/types/undefined.rb +0 -31
  85. data/tests/test_bson.rb +0 -135
  86. data/tests/test_cursor.rb +0 -66
  87. data/tests/test_db.rb +0 -51
  88. data/tests/test_db_api.rb +0 -349
  89. data/tests/test_objectid.rb +0 -88
@@ -1,66 +0,0 @@
1
- $LOAD_PATH[0,0] = File.join(File.dirname(__FILE__), '..', 'lib')
2
- require 'mongo'
3
- require 'test/unit'
4
-
5
- # NOTE: assumes Mongo is running
6
- class CursorTest < Test::Unit::TestCase
7
-
8
- include XGen::Mongo::Driver
9
-
10
- def setup
11
- host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
12
- port = ENV['MONGO_RUBY_DRIVER_PORT'] || Mongo::DEFAULT_PORT
13
- @db = Mongo.new(host, port).db('ruby-mongo-test')
14
- @coll = @db.collection('test')
15
- @coll.clear
16
- @r1 = @coll.insert('a' => 1) # collection not created until it's used
17
- @coll_full_name = 'ruby-mongo-test.test'
18
- end
19
-
20
- def teardown
21
- if @db.connected?
22
- @coll.clear if @coll
23
- @db.close
24
- end
25
- end
26
-
27
- def test_explain
28
- cursor = @coll.find('a' => 1)
29
- explaination = cursor.explain
30
- assert_not_nil explaination['cursor']
31
- assert_kind_of Numeric, explaination['n']
32
- assert_kind_of Numeric, explaination['millis']
33
- assert_kind_of Numeric, explaination['nscanned']
34
- end
35
-
36
- def test_close_no_query_sent
37
- begin
38
- cursor = @coll.find('a' => 1)
39
- cursor.close
40
- assert cursor.closed?
41
- rescue => ex
42
- fail ex.to_s
43
- end
44
- end
45
-
46
- def test_close_after_query_sent
47
- begin
48
- cursor = @coll.find('a' => 1)
49
- cursor.next_object
50
- cursor.close
51
- assert cursor.closed?
52
- rescue => ex
53
- fail ex.to_s
54
- end
55
- end
56
-
57
- def test_hint
58
- begin
59
- cursor = @coll.find('a' => 1).hint('a')
60
- assert_equal 1, cursor.to_a.size
61
- rescue => ex
62
- fail ex.to_s
63
- end
64
- end
65
-
66
- end
@@ -1,51 +0,0 @@
1
- $LOAD_PATH[0,0] = File.join(File.dirname(__FILE__), '..', 'lib')
2
- require 'mongo'
3
- require 'test/unit'
4
-
5
- # NOTE: assumes Mongo is running
6
- class DBAPITest < Test::Unit::TestCase
7
-
8
- include XGen::Mongo::Driver
9
-
10
- def setup
11
- @host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
12
- @port = ENV['MONGO_RUBY_DRIVER_PORT'] || Mongo::DEFAULT_PORT
13
- @db = Mongo.new(@host, @port).db('ruby-mongo-test')
14
- end
15
-
16
- def teardown
17
- if @db.connected?
18
- @db.close
19
- end
20
- end
21
-
22
- def test_close
23
- @db.close
24
- assert !@db.connected?
25
- begin
26
- @db.collection('test').insert('a' => 1)
27
- fail "expected 'NilClass' exception"
28
- rescue => ex
29
- assert_match /NilClass/, ex.to_s
30
- end
31
- end
32
-
33
- def test_full_coll_name
34
- coll = @db.collection('test')
35
- assert_equal 'ruby-mongo-test.test', @db.full_coll_name(coll.name)
36
- end
37
-
38
- def test_master
39
- # Doesn't really test anything since we probably only have one database
40
- # during this test.
41
- @db.switch_to_master
42
- assert @db.connected?
43
- end
44
-
45
- def test_array
46
- @db.close
47
- @db = Mongo.new([["nosuch.example.com"], [@host, @port]]).db('ruby-mongo-test')
48
- assert @db.connected?
49
- end
50
-
51
- end
@@ -1,349 +0,0 @@
1
- $LOAD_PATH[0,0] = File.join(File.dirname(__FILE__), '..', 'lib')
2
- require 'mongo'
3
- require 'test/unit'
4
-
5
- # NOTE: assumes Mongo is running
6
- class DBAPITest < Test::Unit::TestCase
7
-
8
- include XGen::Mongo::Driver
9
-
10
- def setup
11
- host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
12
- port = ENV['MONGO_RUBY_DRIVER_PORT'] || Mongo::DEFAULT_PORT
13
- @db = Mongo.new(host, port).db('ruby-mongo-test')
14
- @coll = @db.collection('test')
15
- @coll.clear
16
- @r1 = @coll.insert('a' => 1) # collection not created until it's used
17
- @coll_full_name = 'ruby-mongo-test.test'
18
- end
19
-
20
- def teardown
21
- if @db.connected?
22
- @coll.clear unless @coll == nil
23
- @db.close
24
- end
25
- end
26
-
27
- def test_clear
28
- assert_equal 1, @coll.count
29
- @coll.clear
30
- assert_equal 0, @coll.count
31
- end
32
-
33
- def test_insert
34
- @coll.insert('a' => 2)
35
- @coll.insert('b' => 3)
36
-
37
- assert_equal 3, @coll.count
38
- docs = @coll.find().to_a
39
- assert_equal 3, docs.length
40
- assert docs.detect { |row| row['a'] == 1 }
41
- assert docs.detect { |row| row['a'] == 2 }
42
- assert docs.detect { |row| row['b'] == 3 }
43
-
44
- @coll << {'b' => 4}
45
- docs = @coll.find().to_a
46
- assert_equal 4, docs.length
47
- assert docs.detect { |row| row['b'] == 4 }
48
- end
49
-
50
- def test_find_simple
51
- @r2 = @coll.insert('a' => 2)
52
- @r3 = @coll.insert('b' => 3)
53
- # Check sizes
54
- docs = @coll.find().to_a
55
- assert_equal 3, docs.size
56
- assert_equal 3, @coll.count
57
-
58
- # Find by other value
59
- docs = @coll.find('a' => @r1['a']).to_a
60
- assert_equal 1, docs.size
61
- doc = docs.first
62
- assert_equal doc['_id'], @r1['_id']
63
- assert_equal doc['a'], @r1['a']
64
- end
65
-
66
- def test_find_advanced
67
- @coll.insert('a' => 2)
68
- @coll.insert('b' => 3)
69
-
70
- # Find by advanced query (less than)
71
- docs = @coll.find('a' => { '$lt' => 10 }).to_a
72
- assert_equal 2, docs.size
73
- assert docs.detect { |row| row['a'] == 1 }
74
- assert docs.detect { |row| row['a'] == 2 }
75
-
76
- # Find by advanced query (greater than)
77
- docs = @coll.find('a' => { '$gt' => 1 }).to_a
78
- assert_equal 1, docs.size
79
- assert docs.detect { |row| row['a'] == 2 }
80
-
81
- # Find by advanced query (less than or equal to)
82
- docs = @coll.find('a' => { '$lte' => 1 }).to_a
83
- assert_equal 1, docs.size
84
- assert docs.detect { |row| row['a'] == 1 }
85
-
86
- # Find by advanced query (greater than or equal to)
87
- docs = @coll.find('a' => { '$gte' => 1 }).to_a
88
- assert_equal 2, docs.size
89
- assert docs.detect { |row| row['a'] == 1 }
90
- assert docs.detect { |row| row['a'] == 2 }
91
-
92
- # Find by advanced query (between)
93
- docs = @coll.find('a' => { '$gt' => 1, '$lt' => 3 }).to_a
94
- assert_equal 1, docs.size
95
- assert docs.detect { |row| row['a'] == 2 }
96
-
97
- # Find by advanced query (in clause)
98
- docs = @coll.find('a' => {'$in' => [1,2]}).to_a
99
- assert_equal 2, docs.size
100
- assert docs.detect { |row| row['a'] == 1 }
101
- assert docs.detect { |row| row['a'] == 2 }
102
-
103
- # Find by advanced query (regexp)
104
- docs = @coll.find('a' => /[1|2]/).to_a
105
- assert_equal 2, docs.size
106
- assert docs.detect { |row| row['a'] == 1 }
107
- assert docs.detect { |row| row['a'] == 2 }
108
- end
109
-
110
- def test_find_sorting
111
- @coll.clear
112
- @coll.insert('a' => 1, 'b' => 2)
113
- @coll.insert('a' => 2, 'b' => 1)
114
- @coll.insert('a' => 3, 'b' => 2)
115
- @coll.insert('a' => 4, 'b' => 1)
116
-
117
- # Sorting (ascending)
118
- docs = @coll.find({'a' => { '$lt' => 10 }}, :sort => {'a' => 1}).to_a
119
- assert_equal 4, docs.size
120
- assert_equal 1, docs[0]['a']
121
- assert_equal 2, docs[1]['a']
122
- assert_equal 3, docs[2]['a']
123
- assert_equal 4, docs[3]['a']
124
-
125
- # Sorting (descending)
126
- docs = @coll.find({'a' => { '$lt' => 10 }}, :sort => {'a' => -1}).to_a
127
- assert_equal 4, docs.size
128
- assert_equal 4, docs[0]['a']
129
- assert_equal 3, docs[1]['a']
130
- assert_equal 2, docs[2]['a']
131
- assert_equal 1, docs[3]['a']
132
-
133
- # Sorting using array of names; assumes ascending order.
134
- docs = @coll.find({'a' => { '$lt' => 10 }}, :sort => ['a']).to_a
135
- assert_equal 4, docs.size
136
- assert_equal 1, docs[0]['a']
137
- assert_equal 2, docs[1]['a']
138
- assert_equal 3, docs[2]['a']
139
- assert_equal 4, docs[3]['a']
140
-
141
- # Sorting using single name; assumes ascending order.
142
- docs = @coll.find({'a' => { '$lt' => 10 }}, :sort => 'a').to_a
143
- assert_equal 4, docs.size
144
- assert_equal 1, docs[0]['a']
145
- assert_equal 2, docs[1]['a']
146
- assert_equal 3, docs[2]['a']
147
- assert_equal 4, docs[3]['a']
148
-
149
- docs = @coll.find({'a' => { '$lt' => 10 }}, :sort => ['b', 'a']).to_a
150
- assert_equal 4, docs.size
151
- assert_equal 2, docs[0]['a']
152
- assert_equal 4, docs[1]['a']
153
- assert_equal 1, docs[2]['a']
154
- assert_equal 3, docs[3]['a']
155
-
156
- # Sorting using empty array; no order guarantee but should not blow up.
157
- docs = @coll.find({'a' => { '$lt' => 10 }}, :sort => []).to_a
158
- assert_equal 4, docs.size
159
-
160
- # Sorting using ordered hash. You can use an unordered one, but then the
161
- # order of the keys won't be guaranteed thus your sort won't make sense.
162
- oh = OrderedHash.new
163
- oh['a'] = -1
164
- docs = @coll.find({'a' => { '$lt' => 10 }}, :sort => oh).to_a
165
- assert_equal 4, docs.size
166
- assert_equal 4, docs[0]['a']
167
- assert_equal 3, docs[1]['a']
168
- assert_equal 2, docs[2]['a']
169
- assert_equal 1, docs[3]['a']
170
-
171
- # TODO this will not pass due to known Mongo bug #898
172
- # oh = OrderedHash.new
173
- # oh['b'] = -1
174
- # oh['a'] = 1
175
- # docs = @coll.find({'a' => { '$lt' => 10 }}, :sort => oh).to_a
176
- # assert_equal 4, docs.size
177
- # assert_equal 1, docs[0]['a']
178
- # assert_equal 3, docs[1]['a']
179
- # assert_equal 2, docs[2]['a']
180
- # assert_equal 4, docs[3]['a']
181
- end
182
-
183
- def test_find_limits
184
- @coll.insert('b' => 2)
185
- @coll.insert('c' => 3)
186
- @coll.insert('d' => 4)
187
-
188
- docs = @coll.find({}, :limit => 1).to_a
189
- assert_equal 1, docs.size
190
- docs = @coll.find({}, :limit => 2).to_a
191
- assert_equal 2, docs.size
192
- docs = @coll.find({}, :limit => 3).to_a
193
- assert_equal 3, docs.size
194
- docs = @coll.find({}, :limit => 4).to_a
195
- assert_equal 4, docs.size
196
- docs = @coll.find({}).to_a
197
- assert_equal 4, docs.size
198
- docs = @coll.find({}, :limit => 99).to_a
199
- assert_equal 4, docs.size
200
- end
201
-
202
- def test_drop_collection
203
- assert @db.drop_collection(@coll.name), "drop of collection #{@coll.name} failed"
204
- assert !@db.collection_names.include?(@coll_full_name)
205
- @coll = nil
206
- end
207
-
208
- def test_collection_names
209
- names = @db.collection_names
210
- assert names.length >= 1
211
- assert names.include?(@coll_full_name)
212
-
213
- coll2 = @db.collection('test2')
214
- coll2.insert('a' => 1) # collection not created until it's used
215
- names = @db.collection_names
216
- assert names.length >= 2
217
- assert names.include?(@coll_full_name)
218
- assert names.include?('ruby-mongo-test.test2')
219
- ensure
220
- @db.drop_collection('test2')
221
- end
222
-
223
- def test_collections_info
224
- cursor = @db.collections_info
225
- rows = cursor.to_a
226
- assert rows.length >= 1
227
- row = rows.detect { |r| r['name'] == @coll_full_name }
228
- assert_not_nil row
229
- assert_equal @coll.name, row['options']['create']
230
- end
231
-
232
- def test_collection_options
233
- @db.drop_collection('foobar')
234
- @db.strict = true
235
-
236
- begin
237
- coll = @db.create_collection('foobar', :capped => true, :size => 1024)
238
- options = coll.options()
239
- assert_equal 'foobar', options['create']
240
- assert_equal true, options['capped']
241
- assert_equal 1024, options['size']
242
- rescue => ex
243
- @db.drop_collection('foobar')
244
- fail "did not expect exception \"#{ex}\""
245
- end
246
- end
247
-
248
- def test_index_information
249
- @db.create_index(@coll.name, 'index_name', ['a'])
250
- list = @db.index_information(@coll.name)
251
- assert_equal 1, list.length
252
-
253
- info = list[0]
254
- assert_equal 'index_name', info[:name]
255
- assert_equal 1, info[:keys]['a']
256
- end
257
-
258
- def test_array
259
- @coll << {'b' => [1, 2, 3]}
260
- rows = @coll.find({}, {:fields => ['b']}).to_a
261
- assert_equal 1, rows.length
262
- assert_equal [1, 2, 3], rows[0]['b']
263
- end
264
-
265
- def test_regex
266
- regex = /foobar/i
267
- @coll << {'b' => regex}
268
- rows = @coll.find({}, {:fields => ['b']}).to_a
269
- assert_equal 1, rows.length
270
- assert_equal regex, rows[0]['b']
271
- end
272
-
273
- def test_strict
274
- assert !@db.strict?
275
- @db.strict = true
276
- assert @db.strict?
277
- end
278
-
279
- def test_strict_access_collection
280
- @db.strict = true
281
- begin
282
- @db.collection('does-not-exist')
283
- fail "expected exception"
284
- rescue => ex
285
- assert_equal "Collection does-not-exist doesn't exist. Currently in strict mode.", ex.to_s
286
- ensure
287
- @db.strict = false
288
- @db.drop_collection('does-not-exist')
289
- end
290
- end
291
-
292
- def test_strict_create_collection
293
- @db.drop_collection('foobar')
294
- @db.strict = true
295
-
296
- begin
297
- @db.create_collection('foobar')
298
- assert true
299
- rescue => ex
300
- fail "did not expect exception \"#{ex}\""
301
- end
302
-
303
- # Now the collection exists. This time we should see an exception.
304
- begin
305
- @db.create_collection('foobar')
306
- fail "expected exception"
307
- rescue => ex
308
- assert_equal "Collection foobar already exists. Currently in strict mode.", ex.to_s
309
- ensure
310
- @db.strict = false
311
- @db.drop_collection('foobar')
312
- end
313
- end
314
-
315
- def test_to_a
316
- cursor = @coll.find()
317
- rows = cursor.to_a
318
-
319
- # Make sure we get back exactly the same array the next time we ask
320
- rows2 = cursor.to_a
321
- assert_same rows, rows2
322
-
323
- # Make sure we can still iterate after calling to_a
324
- rows_with_each = cursor.collect{|row| row}
325
- assert_equal rows, rows_with_each
326
-
327
- # Make sure we can iterate more than once after calling to_a
328
- end
329
-
330
- def test_to_a_after_each
331
- cursor = @coll.find
332
- cursor.each { |row| row }
333
- begin
334
- cursor.to_a
335
- fail "expected \"can't call\" error"
336
- rescue => ex
337
- assert_equal "can't call Cursor#to_a after calling Cursor#each", ex.to_s
338
- end
339
- end
340
-
341
- def test_ismaster
342
- assert @db.master?
343
- end
344
-
345
- def test_master
346
- assert_equal "#{@db.host}:#{@db.port}", @db.master
347
- end
348
-
349
- end