mongo 0.19.3 → 0.20

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 (65) hide show
  1. data/README.rdoc +7 -3
  2. data/Rakefile +16 -6
  3. data/bin/bson_benchmark.rb +2 -2
  4. data/examples/gridfs.rb +3 -2
  5. data/lib/mongo.rb +3 -26
  6. data/lib/mongo/collection.rb +69 -50
  7. data/lib/mongo/connection.rb +16 -41
  8. data/lib/mongo/cursor.rb +22 -32
  9. data/lib/mongo/db.rb +13 -5
  10. data/lib/mongo/exceptions.rb +11 -15
  11. data/lib/mongo/gridfs/grid.rb +14 -3
  12. data/lib/mongo/gridfs/grid_file_system.rb +28 -5
  13. data/lib/mongo/gridfs/grid_io.rb +42 -24
  14. data/lib/mongo/util/support.rb +13 -2
  15. data/mongo-ruby-driver.gemspec +3 -1
  16. data/test/collection_test.rb +62 -9
  17. data/test/connection_test.rb +21 -32
  18. data/test/conversions_test.rb +1 -1
  19. data/test/cursor_test.rb +2 -2
  20. data/test/db_api_test.rb +28 -27
  21. data/test/db_connection_test.rb +1 -1
  22. data/test/db_test.rb +23 -13
  23. data/test/grid_file_system_test.rb +30 -4
  24. data/test/grid_io_test.rb +14 -1
  25. data/test/grid_test.rb +59 -3
  26. data/test/test_helper.rb +4 -1
  27. data/test/threading/test_threading_large_pool.rb +1 -1
  28. data/test/threading_test.rb +1 -1
  29. data/test/unit/collection_test.rb +2 -2
  30. data/test/unit/cursor_test.rb +7 -0
  31. data/test/unit/db_test.rb +8 -8
  32. metadata +6 -46
  33. data/bin/gr.rb +0 -14
  34. data/lib/bson.rb +0 -46
  35. data/lib/bson/bson_c.rb +0 -20
  36. data/lib/bson/bson_ruby.rb +0 -601
  37. data/lib/bson/byte_buffer.rb +0 -224
  38. data/lib/bson/exceptions.rb +0 -39
  39. data/lib/bson/ordered_hash.rb +0 -140
  40. data/lib/bson/types/binary.rb +0 -54
  41. data/lib/bson/types/code.rb +0 -36
  42. data/lib/bson/types/dbref.rb +0 -40
  43. data/lib/bson/types/min_max_keys.rb +0 -58
  44. data/lib/bson/types/objectid.rb +0 -180
  45. data/lib/bson/types/regexp_of_holding.rb +0 -45
  46. data/lib/mongo/gridfs.rb +0 -29
  47. data/lib/mongo/gridfs/chunk.rb +0 -91
  48. data/lib/mongo/gridfs/grid_store.rb +0 -580
  49. data/lib/mongo/types/binary.rb +0 -52
  50. data/lib/mongo/types/code.rb +0 -36
  51. data/lib/mongo/types/dbref.rb +0 -40
  52. data/lib/mongo/types/min_max_keys.rb +0 -58
  53. data/lib/mongo/types/objectid.rb +0 -180
  54. data/lib/mongo/types/regexp_of_holding.rb +0 -45
  55. data/lib/mongo/util/bson_c.rb +0 -18
  56. data/lib/mongo/util/bson_ruby.rb +0 -606
  57. data/lib/mongo/util/byte_buffer.rb +0 -222
  58. data/lib/mongo/util/ordered_hash.rb +0 -140
  59. data/test/binary_test.rb +0 -15
  60. data/test/bson_test.rb +0 -459
  61. data/test/byte_buffer_test.rb +0 -81
  62. data/test/chunk_test.rb +0 -82
  63. data/test/grid_store_test.rb +0 -337
  64. data/test/objectid_test.rb +0 -125
  65. data/test/ordered_hash_test.rb +0 -172
@@ -1,81 +0,0 @@
1
- require 'test/test_helper'
2
-
3
- class ByteBufferTest < Test::Unit::TestCase
4
-
5
- def setup
6
- @buf = ByteBuffer.new
7
- end
8
-
9
- def test_nil_get_returns_one_byte
10
- @buf.put_array([1, 2, 3, 4])
11
- @buf.rewind
12
- assert_equal 1, @buf.get
13
- end
14
-
15
- def test_one_get_returns_array_length_one
16
- @buf.put_array([1, 2, 3, 4])
17
- @buf.rewind
18
- assert_equal [1], @buf.get(1)
19
- end
20
-
21
- def test_zero_get_returns_empty_array
22
- @buf.put_array([1, 2, 3, 4])
23
- @buf.rewind
24
- assert_equal [], @buf.get(0)
25
- end
26
-
27
- def test_empty
28
- assert_equal 0, @buf.length
29
- end
30
-
31
- def test_length
32
- @buf.put_int 3
33
- assert_equal 4, @buf.length
34
- end
35
-
36
- def test_default_order
37
- assert_equal :little_endian, @buf.order
38
- end
39
-
40
- def test_long_length
41
- @buf.put_long 1027
42
- assert_equal 8, @buf.length
43
- end
44
-
45
- def test_get_long
46
- @buf.put_long 1027
47
- @buf.rewind
48
- assert_equal 1027, @buf.get_long
49
- end
50
-
51
- def test_get_double
52
- @buf.put_double 41.2
53
- @buf.rewind
54
- assert_equal 41.2, @buf.get_double
55
- end
56
-
57
- def test_rewrite
58
- @buf.put_int(0)
59
- @buf.rewind
60
- @buf.put_int(1027)
61
- assert_equal 4, @buf.length
62
- @buf.rewind
63
- assert_equal 1027, @buf.get_int
64
- assert_equal 4, @buf.position
65
- end
66
-
67
- def test_prepend_byte_buffer
68
- @buf.put_int(4)
69
- new_buf = ByteBuffer.new([5, 0, 0, 0])
70
- @buf.prepend!(new_buf)
71
- assert_equal [5, 0, 0, 0, 4, 0, 0, 0], @buf.to_a
72
- end
73
-
74
- def test_append_byte_buffer
75
- @buf.put_int(4)
76
- new_buf = ByteBuffer.new([5, 0, 0, 0])
77
- @buf.append!(new_buf)
78
- assert_equal [4, 0, 0, 0, 5, 0, 0, 0], @buf.to_a
79
- end
80
-
81
- end
@@ -1,82 +0,0 @@
1
- require 'test/test_helper'
2
- require 'mongo/gridfs'
3
-
4
- class ChunkTest < Test::Unit::TestCase
5
-
6
- include Mongo
7
- include GridFS
8
-
9
- @@db = Connection.new(ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost',
10
- ENV['MONGO_RUBY_DRIVER_PORT'] || Connection::DEFAULT_PORT).db('ruby-mongo-utils-test')
11
- @@files = @@db.collection('gridfs.files')
12
- @@chunks = @@db.collection('gridfs.chunks')
13
-
14
- def setup
15
- @@chunks.remove
16
- @@files.remove
17
-
18
- @f = GridStore.new(@@db, 'foobar', 'w')
19
- @c = @f.instance_variable_get('@curr_chunk')
20
- end
21
-
22
- def teardown
23
- @@chunks.remove
24
- @@files.remove
25
- @@db.error
26
- end
27
-
28
- def test_pos
29
- assert_equal 0, @c.pos
30
- assert @c.eof? # since data is empty
31
-
32
- b = ByteBuffer.new
33
- 3.times { |i| b.put(i) }
34
- c = Chunk.new(@f, 'data' => b)
35
- assert !c.eof?
36
- end
37
-
38
- def test_getc
39
- b = ByteBuffer.new
40
- 3.times { |i| b.put(i) }
41
- c = Chunk.new(@f, 'data' => b)
42
-
43
- assert !c.eof?
44
- assert_equal 0, c.getc
45
- assert !c.eof?
46
- assert_equal 1, c.getc
47
- assert !c.eof?
48
- assert_equal 2, c.getc
49
- assert c.eof?
50
- end
51
-
52
- def test_putc
53
- 3.times { |i| @c.putc(i) }
54
- @c.pos = 0
55
-
56
- assert !@c.eof?
57
- assert_equal 0, @c.getc
58
- assert !@c.eof?
59
- assert_equal 1, @c.getc
60
- assert !@c.eof?
61
- assert_equal 2, @c.getc
62
- assert @c.eof?
63
- end
64
-
65
- def test_truncate
66
- 10.times { |i| @c.putc(i) }
67
- assert_equal 10, @c.size
68
- @c.pos = 3
69
- @c.truncate
70
- assert_equal 3, @c.size
71
-
72
- @c.pos = 0
73
- assert !@c.eof?
74
- assert_equal 0, @c.getc
75
- assert !@c.eof?
76
- assert_equal 1, @c.getc
77
- assert !@c.eof?
78
- assert_equal 2, @c.getc
79
- assert @c.eof?
80
- end
81
-
82
- end
@@ -1,337 +0,0 @@
1
- require 'test/test_helper'
2
- require 'mongo/gridfs'
3
-
4
- class GridStoreTest < Test::Unit::TestCase
5
-
6
- include Mongo
7
- include GridFS
8
-
9
- @@db = Connection.new(ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost',
10
- ENV['MONGO_RUBY_DRIVER_PORT'] || Connection::DEFAULT_PORT).db('ruby-mongo-test')
11
- @@files = @@db.collection('fs.files')
12
- @@chunks = @@db.collection('fs.chunks')
13
-
14
- def setup
15
- @@chunks.remove
16
- @@files.remove
17
- GridStore.open(@@db, 'foobar', 'w') { |f| f.write("hello, world!") }
18
- end
19
-
20
- def teardown
21
- @@chunks.remove
22
- @@files.remove
23
- @@db.error
24
- end
25
-
26
- def test_exist
27
- assert GridStore.exist?(@@db, 'foobar')
28
- assert !GridStore.exist?(@@db, 'does_not_exist')
29
- assert !GridStore.exist?(@@db, 'foobar', 'another_root')
30
- end
31
-
32
- def test_list
33
- assert_equal ['foobar'], GridStore.list(@@db)
34
- assert_equal ['foobar'], GridStore.list(@@db, 'fs')
35
- assert_equal [], GridStore.list(@@db, 'my_fs')
36
-
37
- GridStore.open(@@db, 'test', 'w') { |f| f.write("my file") }
38
-
39
- assert_equal ['foobar', 'test'], GridStore.list(@@db)
40
- end
41
-
42
- def test_small_write
43
- rows = @@files.find({'filename' => 'foobar'}).to_a
44
- assert_not_nil rows
45
- assert_equal 1, rows.length
46
- row = rows[0]
47
- assert_not_nil row
48
-
49
- file_id = row['_id']
50
- assert_kind_of ObjectID, file_id
51
- rows = @@chunks.find({'files_id' => file_id}).to_a
52
- assert_not_nil rows
53
- assert_equal 1, rows.length
54
- end
55
-
56
- def test_small_file
57
- rows = @@files.find({'filename' => 'foobar'}).to_a
58
- assert_not_nil rows
59
- assert_equal 1, rows.length
60
- row = rows[0]
61
- assert_not_nil row
62
- assert_equal "hello, world!", GridStore.read(@@db, 'foobar')
63
- end
64
-
65
- def test_overwrite
66
- GridStore.open(@@db, 'foobar', 'w') { |f| f.write("overwrite") }
67
- assert_equal "overwrite", GridStore.read(@@db, 'foobar')
68
- end
69
-
70
- def test_read_length
71
- assert_equal "hello", GridStore.read(@@db, 'foobar', 5)
72
- end
73
-
74
- def test_read_with_and_without_length
75
- GridStore.open(@@db, 'read-types', 'w') do |f|
76
- f.write('hello, there')
77
- end
78
-
79
- GridStore.open(@@db, 'read-types', 'r') do |f|
80
- assert_equal 'hello, ', f.read(7)
81
- assert_equal 'there', f.read
82
- end
83
- end
84
-
85
- def test_access_length
86
- assert_equal 13, GridStore.new(@@db, 'foobar').length
87
- end
88
-
89
- # Also tests seek
90
- def test_read_with_offset
91
- assert_equal "world!", GridStore.read(@@db, 'foobar', nil, 7)
92
- end
93
-
94
- def test_seek
95
- GridStore.open(@@db, 'foobar', 'r') { |f|
96
- f.seek(0)
97
- assert_equal 'h', f.getc.chr
98
- f.seek(7)
99
- assert_equal 'w', f.getc.chr
100
- f.seek(4)
101
- assert_equal 'o', f.getc.chr
102
-
103
- f.seek(-1, IO::SEEK_END)
104
- assert_equal '!', f.getc.chr
105
- f.seek(-6, IO::SEEK_END)
106
- assert_equal 'w', f.getc.chr
107
-
108
- f.seek(0)
109
- f.seek(7, IO::SEEK_CUR)
110
- assert_equal 'w', f.getc.chr
111
- f.seek(-1, IO::SEEK_CUR)
112
- assert_equal 'w', f.getc.chr
113
- f.seek(-4, IO::SEEK_CUR)
114
- assert_equal 'o', f.getc.chr
115
- f.seek(3, IO::SEEK_CUR)
116
- assert_equal 'o', f.getc.chr
117
- }
118
- end
119
-
120
- def test_multi_chunk
121
- @@chunks.remove
122
- @@files.remove
123
-
124
- size = 512
125
- GridStore.open(@@db, 'biggie', 'w') { |f|
126
- f.chunk_size = size
127
- f.write('x' * size)
128
- f.write('y' * size)
129
- f.write('z' * size)
130
- }
131
-
132
- assert_equal 3, @@chunks.count
133
- end
134
-
135
- def test_binary
136
- file = File.open(File.join(File.dirname(__FILE__), 'data', 'data.tar.gz'), 'r')
137
- GridStore.open(@@db, 'zip', 'w') do |f|
138
- f.write(file.read)
139
- end
140
-
141
- file.rewind
142
- data = file.read
143
- if data.respond_to?(:force_encoding)
144
- data.force_encoding(:binary)
145
- end
146
- GridStore.open(@@db, 'zip', 'r') do |f|
147
- assert_equal data.length, f.read.length
148
- end
149
- end
150
-
151
- def test_puts_and_readlines
152
- GridStore.open(@@db, 'multiline', 'w') { |f|
153
- f.puts "line one"
154
- f.puts "line two\n"
155
- f.puts "line three"
156
- }
157
-
158
- lines = GridStore.readlines(@@db, 'multiline')
159
- assert_equal ["line one\n", "line two\n", "line three\n"], lines
160
- end
161
-
162
- def test_unlink
163
- assert_equal 1, @@files.count
164
- assert_equal 1, @@chunks.count
165
- GridStore.unlink(@@db, 'foobar')
166
- assert_equal 0, @@files.count
167
- assert_equal 0, @@chunks.count
168
- end
169
-
170
- def test_unlink_alternate_root_collection
171
- GridStore.default_root_collection = 'gridfs'
172
- GridStore.open(@@db, 'foobar', 'w') do |f|
173
- f.puts "Hello"
174
- end
175
- assert GridStore.exist?(@@db, 'foobar')
176
-
177
- GridStore.default_root_collection = 'fs'
178
- GridStore.unlink(@@db, 'foobar')
179
- assert !GridStore.exist?(@@db, 'foobar')
180
-
181
- GridStore.default_root_collection = 'gridfs'
182
- GridStore.unlink(@@db, 'foobar')
183
- assert !GridStore.exist?(@@db, 'foobar')
184
- end
185
-
186
- def test_mv
187
- assert_equal 1, @@files.count
188
- assert_equal 1, @@chunks.count
189
- GridStore.mv(@@db, 'foobar', 'bazqux')
190
- assert_equal 1, @@files.count
191
- assert_equal 1, @@chunks.count
192
- assert !GridStore.exist?(@@db, 'foobar')
193
- assert GridStore.exist?(@@db, 'bazqux')
194
- end
195
-
196
- def test_append
197
- GridStore.open(@@db, 'foobar', 'w+') { |f| f.write(" how are you?") }
198
- assert_equal 1, @@chunks.count
199
- assert_equal "hello, world! how are you?", GridStore.read(@@db, 'foobar')
200
- end
201
-
202
- def test_rewind_and_truncate_on_write
203
- GridStore.open(@@db, 'foobar', 'w') { |f|
204
- f.write("some text is inserted here")
205
- f.rewind
206
- f.write("abc")
207
- }
208
- assert_equal "abc", GridStore.read(@@db, 'foobar')
209
- end
210
-
211
- def test_tell
212
- GridStore.open(@@db, 'foobar', 'r') { |f|
213
- f.read(5)
214
- assert_equal 5, f.tell
215
- }
216
- end
217
-
218
- def test_empty_block_ok
219
- GridStore.open(@@db, 'empty', 'w')
220
- end
221
-
222
- def test_save_empty_file
223
- @@chunks.remove
224
- @@files.remove
225
- GridStore.open(@@db, 'empty', 'w') {} # re-write with zero bytes
226
- assert_equal 1, @@files.count
227
- assert_equal 0, @@chunks.count
228
- end
229
-
230
- def test_empty_file_eof
231
- GridStore.open(@@db, 'empty', 'w')
232
- GridStore.open(@@db, 'empty', 'r') { |f|
233
- assert f.eof?
234
- }
235
- end
236
-
237
- def test_cannot_change_chunk_size_on_read
238
- begin
239
- GridStore.open(@@db, 'foobar', 'r') { |f| f.chunk_size = 42 }
240
- fail "should have seen error"
241
- rescue => ex
242
- assert_match /error: can only change chunk size/, ex.to_s
243
- end
244
- end
245
-
246
- def test_cannot_change_chunk_size_after_data_written
247
- begin
248
- GridStore.open(@@db, 'foobar', 'w') { |f|
249
- f.write("some text")
250
- f.chunk_size = 42
251
- }
252
- fail "should have seen error"
253
- rescue => ex
254
- assert_match /error: can only change chunk size/, ex.to_s
255
- end
256
- end
257
-
258
- def test_change_chunk_size
259
- GridStore.open(@@db, 'new-file', 'w') { |f|
260
- f.chunk_size = 42
261
- f.write("foo")
262
- }
263
- GridStore.open(@@db, 'new-file', 'r') { |f|
264
- assert f.chunk_size == 42
265
- }
266
- end
267
-
268
- def test_chunk_size_in_option
269
- GridStore.open(@@db, 'new-file', 'w', :chunk_size => 42) { |f| f.write("foo") }
270
- GridStore.open(@@db, 'new-file', 'r') { |f|
271
- assert f.chunk_size == 42
272
- }
273
- end
274
-
275
- def test_md5
276
- GridStore.open(@@db, 'new-file', 'w') { |f| f.write("hello world\n")}
277
- GridStore.open(@@db, 'new-file', 'r') { |f|
278
- assert f.md5 == '6f5902ac237024bdd0c176cb93063dc4'
279
- begin
280
- f.md5 = 'cant do this'
281
- fail "should have seen error"
282
- rescue => ex
283
- true
284
- end
285
- }
286
- GridStore.open(@@db, 'new-file', 'w') {}
287
- GridStore.open(@@db, 'new-file', 'r') { |f|
288
- assert f.md5 == 'd41d8cd98f00b204e9800998ecf8427e'
289
- }
290
- end
291
-
292
- def test_upload_date
293
- now = Time.now
294
- orig_file_upload_date = nil
295
- GridStore.open(@@db, 'foobar', 'r') { |f| orig_file_upload_date = f.upload_date }
296
- assert_not_nil orig_file_upload_date
297
- assert (orig_file_upload_date - now) < 5 # even a really slow system < 5 secs
298
-
299
- sleep(2)
300
- GridStore.open(@@db, 'foobar', 'w') { |f| f.write "new data" }
301
- file_upload_date = nil
302
- GridStore.open(@@db, 'foobar', 'r') { |f| file_upload_date = f.upload_date }
303
- assert_equal orig_file_upload_date, file_upload_date
304
- end
305
-
306
- def test_content_type
307
- ct = nil
308
- GridStore.open(@@db, 'foobar', 'r') { |f| ct = f.content_type }
309
- assert_equal GridStore::DEFAULT_CONTENT_TYPE, ct
310
-
311
- GridStore.open(@@db, 'foobar', 'w+') { |f| f.content_type = 'text/html' }
312
- ct2 = nil
313
- GridStore.open(@@db, 'foobar', 'r') { |f| ct2 = f.content_type }
314
- assert_equal 'text/html', ct2
315
- end
316
-
317
- def test_content_type_option
318
- GridStore.open(@@db, 'new-file', 'w', :content_type => 'image/jpg') { |f| f.write('foo') }
319
- ct = nil
320
- GridStore.open(@@db, 'new-file', 'r') { |f| ct = f.content_type }
321
- assert_equal 'image/jpg', ct
322
- end
323
-
324
- def test_unknown_mode
325
- GridStore.open(@@db, 'foobar', 'x')
326
- fail 'should have seen "illegal mode" error raised'
327
- rescue => ex
328
- assert_equal "error: illegal mode x", ex.to_s
329
- end
330
-
331
- def test_metadata
332
- GridStore.open(@@db, 'foobar', 'r') { |f| assert_nil f.metadata }
333
- GridStore.open(@@db, 'foobar', 'w+') { |f| f.metadata = {'a' => 1} }
334
- GridStore.open(@@db, 'foobar', 'r') { |f| assert_equal({'a' => 1}, f.metadata) }
335
- end
336
-
337
- end