mongo 1.3.0 → 1.12.5

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 (185) hide show
  1. checksums.yaml +7 -0
  2. checksums.yaml.gz.sig +0 -0
  3. data/{LICENSE.txt → LICENSE} +1 -1
  4. data/README.md +122 -271
  5. data/Rakefile +25 -209
  6. data/VERSION +1 -0
  7. data/bin/mongo_console +31 -9
  8. data/lib/mongo/bulk_write_collection_view.rb +387 -0
  9. data/lib/mongo/collection.rb +576 -269
  10. data/lib/mongo/collection_writer.rb +364 -0
  11. data/lib/mongo/connection/node.rb +249 -0
  12. data/lib/mongo/connection/pool.rb +340 -0
  13. data/lib/mongo/connection/pool_manager.rb +320 -0
  14. data/lib/mongo/connection/sharding_pool_manager.rb +67 -0
  15. data/lib/mongo/connection/socket/socket_util.rb +37 -0
  16. data/lib/mongo/connection/socket/ssl_socket.rb +95 -0
  17. data/lib/mongo/connection/socket/tcp_socket.rb +87 -0
  18. data/lib/mongo/connection/socket/unix_socket.rb +39 -0
  19. data/lib/mongo/connection/socket.rb +18 -0
  20. data/lib/mongo/connection.rb +7 -875
  21. data/lib/mongo/cursor.rb +403 -117
  22. data/lib/mongo/db.rb +444 -243
  23. data/lib/mongo/exception.rb +145 -0
  24. data/lib/mongo/functional/authentication.rb +455 -0
  25. data/lib/mongo/functional/logging.rb +85 -0
  26. data/lib/mongo/functional/read_preference.rb +183 -0
  27. data/lib/mongo/functional/scram.rb +556 -0
  28. data/lib/mongo/functional/uri_parser.rb +409 -0
  29. data/lib/mongo/functional/write_concern.rb +66 -0
  30. data/lib/mongo/functional.rb +20 -0
  31. data/lib/mongo/gridfs/grid.rb +30 -24
  32. data/lib/mongo/gridfs/grid_ext.rb +6 -10
  33. data/lib/mongo/gridfs/grid_file_system.rb +38 -20
  34. data/lib/mongo/gridfs/grid_io.rb +84 -75
  35. data/lib/mongo/gridfs.rb +18 -0
  36. data/lib/mongo/legacy.rb +140 -0
  37. data/lib/mongo/mongo_client.rb +697 -0
  38. data/lib/mongo/mongo_replica_set_client.rb +535 -0
  39. data/lib/mongo/mongo_sharded_client.rb +159 -0
  40. data/lib/mongo/networking.rb +372 -0
  41. data/lib/mongo/{util → utils}/conversions.rb +29 -8
  42. data/lib/mongo/{util → utils}/core_ext.rb +28 -18
  43. data/lib/mongo/{util → utils}/server_version.rb +4 -6
  44. data/lib/mongo/{util → utils}/support.rb +29 -31
  45. data/lib/mongo/utils/thread_local_variable_manager.rb +25 -0
  46. data/lib/mongo/utils.rb +19 -0
  47. data/lib/mongo.rb +51 -50
  48. data/mongo.gemspec +29 -32
  49. data/test/functional/authentication_test.rb +39 -0
  50. data/test/functional/bulk_api_stress_test.rb +133 -0
  51. data/test/functional/bulk_write_collection_view_test.rb +1198 -0
  52. data/test/functional/client_test.rb +627 -0
  53. data/test/functional/collection_test.rb +2175 -0
  54. data/test/functional/collection_writer_test.rb +83 -0
  55. data/test/{conversions_test.rb → functional/conversions_test.rb} +47 -3
  56. data/test/functional/cursor_fail_test.rb +57 -0
  57. data/test/functional/cursor_message_test.rb +56 -0
  58. data/test/functional/cursor_test.rb +683 -0
  59. data/test/functional/db_api_test.rb +835 -0
  60. data/test/functional/db_connection_test.rb +25 -0
  61. data/test/functional/db_test.rb +348 -0
  62. data/test/functional/grid_file_system_test.rb +285 -0
  63. data/test/{grid_io_test.rb → functional/grid_io_test.rb} +72 -11
  64. data/test/{grid_test.rb → functional/grid_test.rb} +88 -15
  65. data/test/functional/pool_test.rb +136 -0
  66. data/test/functional/safe_test.rb +98 -0
  67. data/test/functional/ssl_test.rb +29 -0
  68. data/test/functional/support_test.rb +62 -0
  69. data/test/functional/timeout_test.rb +60 -0
  70. data/test/functional/uri_test.rb +446 -0
  71. data/test/functional/write_concern_test.rb +118 -0
  72. data/test/helpers/general.rb +50 -0
  73. data/test/helpers/test_unit.rb +476 -0
  74. data/test/replica_set/authentication_test.rb +37 -0
  75. data/test/replica_set/basic_test.rb +189 -0
  76. data/test/replica_set/client_test.rb +393 -0
  77. data/test/replica_set/connection_test.rb +138 -0
  78. data/test/replica_set/count_test.rb +66 -0
  79. data/test/replica_set/cursor_test.rb +220 -0
  80. data/test/replica_set/insert_test.rb +157 -0
  81. data/test/replica_set/max_values_test.rb +151 -0
  82. data/test/replica_set/pinning_test.rb +105 -0
  83. data/test/replica_set/query_test.rb +73 -0
  84. data/test/replica_set/read_preference_test.rb +219 -0
  85. data/test/replica_set/refresh_test.rb +211 -0
  86. data/test/replica_set/replication_ack_test.rb +95 -0
  87. data/test/replica_set/ssl_test.rb +32 -0
  88. data/test/sharded_cluster/basic_test.rb +203 -0
  89. data/test/shared/authentication/basic_auth_shared.rb +260 -0
  90. data/test/shared/authentication/bulk_api_auth_shared.rb +249 -0
  91. data/test/shared/authentication/gssapi_shared.rb +176 -0
  92. data/test/shared/authentication/sasl_plain_shared.rb +96 -0
  93. data/test/shared/authentication/scram_shared.rb +92 -0
  94. data/test/shared/ssl_shared.rb +235 -0
  95. data/test/test_helper.rb +53 -94
  96. data/test/threading/basic_test.rb +120 -0
  97. data/test/tools/mongo_config.rb +708 -0
  98. data/test/tools/mongo_config_test.rb +160 -0
  99. data/test/unit/client_test.rb +381 -0
  100. data/test/unit/collection_test.rb +89 -53
  101. data/test/unit/connection_test.rb +282 -32
  102. data/test/unit/cursor_test.rb +206 -8
  103. data/test/unit/db_test.rb +55 -13
  104. data/test/unit/grid_test.rb +43 -16
  105. data/test/unit/mongo_sharded_client_test.rb +48 -0
  106. data/test/unit/node_test.rb +93 -0
  107. data/test/unit/pool_manager_test.rb +111 -0
  108. data/test/unit/read_pref_test.rb +406 -0
  109. data/test/unit/read_test.rb +159 -0
  110. data/test/unit/safe_test.rb +69 -36
  111. data/test/unit/sharding_pool_manager_test.rb +84 -0
  112. data/test/unit/write_concern_test.rb +175 -0
  113. data.tar.gz.sig +3 -0
  114. metadata +227 -216
  115. metadata.gz.sig +0 -0
  116. data/docs/CREDITS.md +0 -123
  117. data/docs/FAQ.md +0 -116
  118. data/docs/GridFS.md +0 -158
  119. data/docs/HISTORY.md +0 -244
  120. data/docs/RELEASES.md +0 -33
  121. data/docs/REPLICA_SETS.md +0 -72
  122. data/docs/TUTORIAL.md +0 -247
  123. data/docs/WRITE_CONCERN.md +0 -28
  124. data/lib/mongo/exceptions.rb +0 -71
  125. data/lib/mongo/gridfs/grid_io_fix.rb +0 -38
  126. data/lib/mongo/repl_set_connection.rb +0 -342
  127. data/lib/mongo/test.rb +0 -20
  128. data/lib/mongo/util/pool.rb +0 -177
  129. data/lib/mongo/util/uri_parser.rb +0 -185
  130. data/test/async/collection_test.rb +0 -224
  131. data/test/async/connection_test.rb +0 -24
  132. data/test/async/cursor_test.rb +0 -162
  133. data/test/async/worker_pool_test.rb +0 -99
  134. data/test/auxillary/1.4_features.rb +0 -166
  135. data/test/auxillary/authentication_test.rb +0 -68
  136. data/test/auxillary/autoreconnect_test.rb +0 -41
  137. data/test/auxillary/fork_test.rb +0 -30
  138. data/test/auxillary/repl_set_auth_test.rb +0 -58
  139. data/test/auxillary/slave_connection_test.rb +0 -36
  140. data/test/auxillary/threaded_authentication_test.rb +0 -101
  141. data/test/bson/binary_test.rb +0 -15
  142. data/test/bson/bson_test.rb +0 -649
  143. data/test/bson/byte_buffer_test.rb +0 -208
  144. data/test/bson/hash_with_indifferent_access_test.rb +0 -38
  145. data/test/bson/json_test.rb +0 -17
  146. data/test/bson/object_id_test.rb +0 -154
  147. data/test/bson/ordered_hash_test.rb +0 -204
  148. data/test/bson/timestamp_test.rb +0 -24
  149. data/test/collection_test.rb +0 -910
  150. data/test/connection_test.rb +0 -309
  151. data/test/cursor_fail_test.rb +0 -75
  152. data/test/cursor_message_test.rb +0 -43
  153. data/test/cursor_test.rb +0 -483
  154. data/test/db_api_test.rb +0 -726
  155. data/test/db_connection_test.rb +0 -15
  156. data/test/db_test.rb +0 -287
  157. data/test/grid_file_system_test.rb +0 -243
  158. data/test/load/resque/load.rb +0 -21
  159. data/test/load/resque/processor.rb +0 -26
  160. data/test/load/thin/load.rb +0 -24
  161. data/test/load/unicorn/load.rb +0 -23
  162. data/test/load/unicorn/unicorn.rb +0 -29
  163. data/test/replica_sets/connect_test.rb +0 -94
  164. data/test/replica_sets/connection_string_test.rb +0 -32
  165. data/test/replica_sets/count_test.rb +0 -35
  166. data/test/replica_sets/insert_test.rb +0 -53
  167. data/test/replica_sets/pooled_insert_test.rb +0 -55
  168. data/test/replica_sets/query_secondaries.rb +0 -96
  169. data/test/replica_sets/query_test.rb +0 -51
  170. data/test/replica_sets/replication_ack_test.rb +0 -66
  171. data/test/replica_sets/rs_test_helper.rb +0 -27
  172. data/test/safe_test.rb +0 -68
  173. data/test/support/hash_with_indifferent_access.rb +0 -186
  174. data/test/support/keys.rb +0 -45
  175. data/test/support_test.rb +0 -18
  176. data/test/threading/threading_with_large_pool_test.rb +0 -90
  177. data/test/threading_test.rb +0 -87
  178. data/test/tools/auth_repl_set_manager.rb +0 -14
  179. data/test/tools/load.rb +0 -58
  180. data/test/tools/repl_set_manager.rb +0 -266
  181. data/test/tools/sharding_manager.rb +0 -202
  182. data/test/tools/test.rb +0 -4
  183. data/test/unit/pool_test.rb +0 -9
  184. data/test/unit/repl_set_connection_test.rb +0 -59
  185. data/test/uri_test.rb +0 -91
@@ -1,15 +0,0 @@
1
- require './test/test_helper'
2
-
3
- class DBConnectionTest < Test::Unit::TestCase
4
-
5
- include Mongo
6
-
7
- def test_no_exceptions
8
- host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
9
- port = ENV['MONGO_RUBY_DRIVER_PORT'] || Connection::DEFAULT_PORT
10
- db = Connection.new(host, port).db(MONGO_TEST_DB)
11
- coll = db.collection('test')
12
- coll.remove
13
- db.get_last_error
14
- end
15
- end
data/test/db_test.rb DELETED
@@ -1,287 +0,0 @@
1
- require './test/test_helper'
2
- require 'digest/md5'
3
- require 'stringio'
4
- require 'logger'
5
-
6
- class TestPKFactory
7
- def create_pk(row)
8
- row['_id'] ||= BSON::ObjectId.new
9
- row
10
- end
11
- end
12
-
13
- class DBTest < Test::Unit::TestCase
14
-
15
- include Mongo
16
-
17
- @@conn = standard_connection
18
- @@db = @@conn.db(MONGO_TEST_DB)
19
- @@users = @@db.collection('system.users')
20
- @@version = @@conn.server_version
21
-
22
- def test_close
23
- @@conn.close
24
- assert !@@conn.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
- ensure
31
- @@db = standard_connection.db(MONGO_TEST_DB)
32
- @@users = @@db.collection('system.users')
33
- end
34
- end
35
-
36
- def test_logger
37
- output = StringIO.new
38
- logger = Logger.new(output)
39
- logger.level = Logger::DEBUG
40
- conn = standard_connection(:logger => logger)
41
- assert_equal logger, conn.logger
42
-
43
- conn.logger.debug 'testing'
44
- assert output.string.include?('testing')
45
- end
46
-
47
- def test_full_coll_name
48
- coll = @@db.collection('test')
49
- assert_equal "#{MONGO_TEST_DB}.test", @@db.full_collection_name(coll.name)
50
- end
51
-
52
- def test_collection_names
53
- @@db.collection("test").insert("foo" => 5)
54
- @@db.collection("test.mike").insert("bar" => 0)
55
-
56
- colls = @@db.collection_names()
57
- assert colls.include?("test")
58
- assert colls.include?("test.mike")
59
- colls.each { |name|
60
- assert !name.include?("$")
61
- }
62
- end
63
-
64
- def test_collections
65
- @@db.collection("test.durran").insert("foo" => 5)
66
- @@db.collection("test.les").insert("bar" => 0)
67
-
68
- colls = @@db.collections()
69
- assert_not_nil colls.select { |coll| coll.name == "test.durran" }
70
- assert_not_nil colls.select { |coll| coll.name == "test.les" }
71
- assert_equal [], colls.select { |coll| coll.name == "does_not_exist" }
72
-
73
- assert_kind_of Collection, colls[0]
74
- end
75
-
76
- def test_pk_factory
77
- db = standard_connection.db(MONGO_TEST_DB, :pk => TestPKFactory.new)
78
- coll = db.collection('test')
79
- coll.remove
80
-
81
- insert_id = coll.insert('name' => 'Fred', 'age' => 42)
82
- # new id gets added to returned object
83
- row = coll.find_one({'name' => 'Fred'})
84
- oid = row['_id']
85
- assert_not_nil oid
86
- assert_equal insert_id, oid
87
-
88
- oid = BSON::ObjectId.new
89
- data = {'_id' => oid, 'name' => 'Barney', 'age' => 41}
90
- coll.insert(data)
91
- row = coll.find_one({'name' => data['name']})
92
- db_oid = row['_id']
93
- assert_equal oid, db_oid
94
- assert_equal data, row
95
-
96
- coll.remove
97
- end
98
-
99
- def test_pk_factory_reset
100
- conn = standard_connection
101
- db = conn.db(MONGO_TEST_DB)
102
- db.pk_factory = Object.new # first time
103
- begin
104
- db.pk_factory = Object.new
105
- fail "error: expected exception"
106
- rescue => ex
107
- assert_match(/Cannot change/, ex.to_s)
108
- ensure
109
- conn.close
110
- end
111
- end
112
-
113
- def test_authenticate
114
- @@db.add_user('spongebob', 'squarepants')
115
- assert_raise Mongo::AuthenticationError do
116
- assert !@@db.authenticate('nobody', 'nopassword')
117
- end
118
- assert_raise Mongo::AuthenticationError do
119
- assert !@@db.authenticate('spongebob' , 'squareliederhosen')
120
- end
121
- assert @@db.authenticate('spongebob', 'squarepants')
122
- @@db.logout
123
- @@db.remove_user('spongebob')
124
- end
125
-
126
- def test_authenticate_with_connection_uri
127
- @@db.add_user('spongebob', 'squarepants')
128
- assert Mongo::Connection.from_uri("mongodb://spongebob:squarepants@#{host_port}/#{@@db.name}")
129
-
130
- assert_raise Mongo::AuthenticationError do
131
- Mongo::Connection.from_uri("mongodb://wrong:info@#{host_port}/#{@@db.name}")
132
- end
133
- end
134
-
135
- def test_logout
136
- assert @@db.logout
137
- end
138
-
139
- def test_command
140
- assert_raise OperationFailure do
141
- @@db.command({:non_command => 1}, :check_response => true)
142
- end
143
-
144
- result = @@db.command({:non_command => 1}, :check_response => false)
145
- assert !Mongo::Support.ok?(result)
146
- end
147
-
148
- def test_error
149
- @@db.reset_error_history
150
- assert_nil @@db.get_last_error['err']
151
- assert !@@db.error?
152
- assert_nil @@db.previous_error
153
-
154
- @@db.command({:forceerror => 1}, :check_response => false)
155
- assert @@db.error?
156
- assert_not_nil @@db.get_last_error['err']
157
- assert_not_nil @@db.previous_error
158
-
159
- @@db.command({:forceerror => 1}, :check_response => false)
160
- assert @@db.error?
161
- assert @@db.get_last_error['err']
162
- prev_error = @@db.previous_error
163
- assert_equal 1, prev_error['nPrev']
164
- assert_equal prev_error["err"], @@db.get_last_error['err']
165
-
166
- @@db.collection('test').find_one
167
- assert_nil @@db.get_last_error['err']
168
- assert !@@db.error?
169
- assert @@db.previous_error
170
- assert_equal 2, @@db.previous_error['nPrev']
171
-
172
- @@db.reset_error_history
173
- assert_nil @@db.get_last_error['err']
174
- assert !@@db.error?
175
- assert_nil @@db.previous_error
176
- end
177
-
178
- def test_check_command_response
179
- command = {:forceerror => 1}
180
- assert_raise OperationFailure do
181
- @@db.command(command)
182
- end
183
- end
184
-
185
- def test_last_status
186
- @@db['test'].remove
187
- @@db['test'].save("i" => 1)
188
-
189
- @@db['test'].update({"i" => 1}, {"$set" => {"i" => 2}})
190
- assert @@db.get_last_error()["updatedExisting"]
191
-
192
- @@db['test'].update({"i" => 1}, {"$set" => {"i" => 500}})
193
- assert !@@db.get_last_error()["updatedExisting"]
194
- end
195
-
196
- def test_text_port_number_raises_no_errors
197
- conn = standard_connection
198
- db = conn[MONGO_TEST_DB]
199
- db.collection('users').remove
200
- end
201
-
202
- def test_user_management
203
- @@db.add_user("bob", "secret")
204
- assert @@db.authenticate("bob", "secret")
205
- @@db.logout
206
- assert @@db.remove_user("bob")
207
- assert_raise Mongo::AuthenticationError do
208
- @@db.authenticate("bob", "secret")
209
- end
210
- end
211
-
212
- def test_remove_non_existant_user
213
- assert !@@db.remove_user("joe")
214
- end
215
-
216
- def test_stored_function_management
217
- @@db.add_stored_function("sum", "function (x, y) { return x + y; }")
218
- assert_equal @@db.eval("return sum(2,3);"), 5
219
- assert @@db.remove_stored_function("sum")
220
- assert_raise OperationFailure do
221
- @@db.eval("return sum(2,3);")
222
- end
223
- end
224
-
225
- def test_eval
226
- @@db.eval("db.system.save({_id:'hello', value: function() { print('hello'); } })")
227
- assert_equal 'hello', @@db['system'].find_one['_id']
228
- end
229
-
230
- if @@version >= "1.3.5"
231
- def test_db_stats
232
- stats = @@db.stats
233
- assert stats.has_key?('collections')
234
- assert stats.has_key?('dataSize')
235
- end
236
- end
237
-
238
- context "database profiling" do
239
- setup do
240
- @db = @@conn[MONGO_TEST_DB]
241
- @coll = @db['test']
242
- @coll.remove
243
- @r1 = @coll.insert('a' => 1) # collection not created until it's used
244
- end
245
-
246
- should "set default profiling level" do
247
- assert_equal :off, @db.profiling_level
248
- end
249
-
250
- should "change profiling level" do
251
- @db.profiling_level = :slow_only
252
- assert_equal :slow_only, @db.profiling_level
253
- @db.profiling_level = :off
254
- assert_equal :off, @db.profiling_level
255
- @db.profiling_level = :all
256
- assert_equal :all, @db.profiling_level
257
- begin
258
- @db.profiling_level = :medium
259
- fail "shouldn't be able to do this"
260
- rescue
261
- end
262
- end
263
-
264
- should "return profiling info" do
265
- @db.profiling_level = :all
266
- @coll.find()
267
- @db.profiling_level = :off
268
-
269
- info = @db.profiling_info
270
- assert_kind_of Array, info
271
- assert info.length >= 1
272
- first = info.first
273
- assert_kind_of String, first['info']
274
- assert_kind_of Time, first['ts']
275
- assert_kind_of Numeric, first['millis']
276
- end
277
-
278
- should "validate collection" do
279
- doc = @db.validate_collection(@coll.name)
280
- assert_not_nil doc
281
- result = doc['result']
282
- assert_not_nil result
283
- assert_match(/firstExtent/, result)
284
- end
285
-
286
- end
287
- end
@@ -1,243 +0,0 @@
1
- require './test/test_helper'
2
-
3
- class GridFileSystemTest < Test::Unit::TestCase
4
- context "GridFileSystem:" do
5
- setup do
6
- @con = standard_connection
7
- @db = @con.db(MONGO_TEST_DB)
8
- end
9
-
10
- teardown do
11
- @db.drop_collection('fs.files')
12
- @db.drop_collection('fs.chunks')
13
- end
14
-
15
- context "When reading:" do
16
- setup do
17
- @chunks_data = "CHUNKS" * 50000
18
- @grid = GridFileSystem.new(@db)
19
- @grid.open('sample.file', 'w') do |f|
20
- f.write @chunks_data
21
- end
22
-
23
- @grid = GridFileSystem.new(@db)
24
- end
25
-
26
- should "return existence of the file" do
27
- file = @grid.exist?(:filename => 'sample.file')
28
- assert_equal 'sample.file', file['filename']
29
- end
30
-
31
- should "return nil if the file doesn't exist" do
32
- assert_nil @grid.exist?(:filename => 'foo.file')
33
- end
34
-
35
- should "read sample data" do
36
- data = @grid.open('sample.file', 'r') { |f| f.read }
37
- assert_equal data.length, @chunks_data.length
38
- end
39
-
40
- should "have a unique index on chunks" do
41
- assert @db['fs.chunks'].index_information['files_id_1_n_1']['unique']
42
- end
43
-
44
- should "have an index on filename" do
45
- assert @db['fs.files'].index_information['filename_1_uploadDate_-1']
46
- end
47
-
48
- should "return an empty string if length is zero" do
49
- data = @grid.open('sample.file', 'r') { |f| f.read(0) }
50
- assert_equal '', data
51
- end
52
-
53
- should "return the first n bytes" do
54
- data = @grid.open('sample.file', 'r') {|f| f.read(288888) }
55
- assert_equal 288888, data.length
56
- assert_equal @chunks_data[0...288888], data
57
- end
58
-
59
- should "return the first n bytes even with an offset" do
60
- data = @grid.open('sample.file', 'r') do |f|
61
- f.seek(1000)
62
- f.read(288888)
63
- end
64
- assert_equal 288888, data.length
65
- assert_equal @chunks_data[1000...289888], data
66
- end
67
- end
68
-
69
- context "When writing:" do
70
- setup do
71
- @data = "BYTES" * 50
72
- @grid = GridFileSystem.new(@db)
73
- @grid.open('sample', 'w') do |f|
74
- f.write @data
75
- end
76
- end
77
-
78
- should "read sample data" do
79
- data = @grid.open('sample', 'r') { |f| f.read }
80
- assert_equal data.length, @data.length
81
- end
82
-
83
- should "return the total number of bytes written" do
84
- data = 'a' * 300000
85
- assert_equal 300000, @grid.open('sample', 'w') {|f| f.write(data) }
86
- end
87
-
88
- should "more read sample data" do
89
- data = @grid.open('sample', 'r') { |f| f.read }
90
- assert_equal data.length, @data.length
91
- end
92
-
93
- should "raise exception if file not found" do
94
- assert_raise GridFileNotFound do
95
- @grid.open('io', 'r') { |f| f.write('hello') }
96
- end
97
- end
98
-
99
- should "raise exception if not opened for write" do
100
- assert_raise GridError do
101
- @grid.open('sample', 'r') { |f| f.write('hello') }
102
- end
103
- end
104
-
105
- context "and when overwriting the file" do
106
- setup do
107
- @old = @grid.open('sample', 'r')
108
-
109
- @new_data = "DATA" * 10
110
- sleep(2)
111
- @grid.open('sample', 'w') do |f|
112
- f.write @new_data
113
- end
114
-
115
- @new = @grid.open('sample', 'r')
116
- end
117
-
118
- should "have a newer upload date" do
119
- assert @new.upload_date > @old.upload_date, "New data is not greater than old date."
120
- end
121
-
122
- should "have a different files_id" do
123
- assert_not_equal @new.files_id, @old.files_id
124
- end
125
-
126
- should "contain the new data" do
127
- assert_equal @new_data, @new.read, "Expected DATA"
128
- end
129
-
130
- context "and on a second overwrite" do
131
- setup do
132
- sleep(2)
133
- @new_data = "NEW" * 1000
134
- @grid.open('sample', 'w') do |f|
135
- f.write @new_data
136
- end
137
-
138
- @ids = @db['fs.files'].find({'filename' => 'sample'}).map {|file| file['_id']}
139
- end
140
-
141
- should "write a third version of the file" do
142
- assert_equal 3, @db['fs.files'].find({'filename' => 'sample'}).count
143
- assert_equal 3, @db['fs.chunks'].find({'files_id' => {'$in' => @ids}}).count
144
- end
145
-
146
- should "remove all versions and their data on delete" do
147
- @grid.delete('sample')
148
- assert_equal 0, @db['fs.files'].find({'filename' => 'sample'}).count
149
- assert_equal 0, @db['fs.chunks'].find({'files_id' => {'$in' => @ids}}).count
150
- end
151
-
152
- should "delete old versions on write with :delete_old is passed in" do
153
- @grid.open('sample', 'w', :delete_old => true) do |f|
154
- f.write @new_data
155
- end
156
- @new_ids = @db['fs.files'].find({'filename' => 'sample'}).map {|file| file['_id']}
157
- assert_equal 1, @new_ids.length
158
- id = @new_ids.first
159
- assert !@ids.include?(id)
160
- assert_equal 1, @db['fs.files'].find({'filename' => 'sample'}).count
161
- assert_equal 1, @db['fs.chunks'].find({'files_id' => id}).count
162
- end
163
- end
164
- end
165
- end
166
-
167
- context "When writing chunks:" do
168
- setup do
169
- data = "B" * 50000
170
- @grid = GridFileSystem.new(@db)
171
- @grid.open('sample', 'w', :chunk_size => 1000) do |f|
172
- f.write data
173
- end
174
- end
175
-
176
- should "write the correct number of chunks" do
177
- file = @db['fs.files'].find_one({:filename => 'sample'})
178
- chunks = @db['fs.chunks'].find({'files_id' => file['_id']}).to_a
179
- assert_equal 50, chunks.length
180
- end
181
- end
182
-
183
- context "Positioning:" do
184
- setup do
185
- data = 'hello, world' + '1' * 5000 + 'goodbye!' + '2' * 1000 + '!'
186
- @grid = GridFileSystem.new(@db)
187
- @grid.open('hello', 'w', :chunk_size => 1000) do |f|
188
- f.write data
189
- end
190
- end
191
-
192
- should "seek within chunks" do
193
- @grid.open('hello', 'r') do |f|
194
- f.seek(0)
195
- assert_equal 'h', f.read(1)
196
- f.seek(7)
197
- assert_equal 'w', f.read(1)
198
- f.seek(4)
199
- assert_equal 'o', f.read(1)
200
- f.seek(0)
201
- f.seek(7, IO::SEEK_CUR)
202
- assert_equal 'w', f.read(1)
203
- f.seek(-2, IO::SEEK_CUR)
204
- assert_equal ' ', f.read(1)
205
- f.seek(-4, IO::SEEK_CUR)
206
- assert_equal 'l', f.read(1)
207
- f.seek(3, IO::SEEK_CUR)
208
- assert_equal 'w', f.read(1)
209
- end
210
- end
211
-
212
- should "seek between chunks" do
213
- @grid.open('hello', 'r') do |f|
214
- f.seek(1000)
215
- assert_equal '11111', f.read(5)
216
-
217
- f.seek(5009)
218
- assert_equal '111goodbye!222', f.read(14)
219
-
220
- f.seek(-1, IO::SEEK_END)
221
- assert_equal '!', f.read(1)
222
- f.seek(-6, IO::SEEK_END)
223
- assert_equal '2', f.read(1)
224
- end
225
- end
226
-
227
- should "tell the current position" do
228
- @grid.open('hello', 'r') do |f|
229
- assert_equal 0, f.tell
230
-
231
- f.seek(999)
232
- assert_equal 999, f.tell
233
- end
234
- end
235
-
236
- should "seek only in read mode" do
237
- assert_raise GridError do
238
- @grid.open('hello', 'w') {|f| f.seek(0) }
239
- end
240
- end
241
- end
242
- end
243
- end
@@ -1,21 +0,0 @@
1
- require File.join(File.dirname(__FILE__), '..', '..', '..', 'lib', 'mongo')
2
- require 'logger'
3
- require 'rubygems'
4
- require 'resque'
5
- require 'sinatra'
6
- require File.join(File.dirname(__FILE__), 'processor')
7
-
8
- $con = Mongo::Connection.new
9
- $db = $con['foo']
10
-
11
-
12
- configure do
13
- LOGGER = Logger.new("sinatra.log")
14
- enable :logging, :dump_errors
15
- set :raise_errors, true
16
- end
17
-
18
- get '/' do
19
- Processor.perform(1)
20
- true
21
- end
@@ -1,26 +0,0 @@
1
- require 'logger'
2
-
3
- class Processor
4
- @queue = :processor
5
-
6
- def self.connection
7
- @log ||= Logger.new(STDOUT)
8
- @con ||= Mongo::Connection.new("localhost", 27017)
9
- end
10
-
11
- def self.perform(n)
12
- begin
13
- 100.times do |n|
14
- self.connection['resque']['docs'].insert({:n => n, :data => "0" * 1000}, :safe => true)
15
- end
16
-
17
- 5.times do |n|
18
- num = rand(100)
19
- self.connection['resque']['docs'].find({:n => {"$gt" => num}}).limit(1).to_a
20
- end
21
- rescue => e
22
- @log.warn(e.inspect)
23
- end
24
- end
25
-
26
- end
@@ -1,24 +0,0 @@
1
- require File.join(File.dirname(__FILE__), '..', '..', '..', 'lib', 'mongo')
2
- require 'logger'
3
-
4
- $con = Mongo::Connection.new
5
- $db = $con['foo']
6
-
7
- class Load < Sinatra::Base
8
-
9
- configure do
10
- LOGGER = Logger.new("sinatra.log")
11
- enable :logging, :dump_errors
12
- set :raise_errors, true
13
- end
14
-
15
- get '/' do
16
- 3.times do |n|
17
- if (v=$db.eval("1 + #{n}")) != 1 + n
18
- STDERR << "#{1 + n} expected but got #{v}"
19
- raise StandardError, "#{1 + n} expected but got #{v}"
20
- end
21
- end
22
- end
23
-
24
- end
@@ -1,23 +0,0 @@
1
- require File.join(File.dirname(__FILE__), '..', '..', 'lib', 'mongo')
2
-
3
- $con = Mongo::Connection.new
4
- $db = $con['foo']
5
-
6
- class Load < Sinatra::Base
7
-
8
- configure do
9
- LOGGER = Logger.new("sinatra.log")
10
- enable :logging, :dump_errors
11
- set :raise_errors, true
12
- end
13
-
14
- get '/' do
15
- 3.times do |n|
16
- if (v=$db.eval("1 + #{n}")) != 1 + n
17
- STDERR << "#{1 + n} expected but got #{v}"
18
- raise StandardError, "#{1 + n} expected but got #{v}"
19
- end
20
- end
21
- end
22
-
23
- end
@@ -1,29 +0,0 @@
1
- # set path to app that will be used to configure unicorn,
2
- # # note the trailing slash in this example
3
- @dir = "/home/kyle/work/10gen/ruby-driver/test/load/"
4
-
5
- worker_processes 10
6
- working_directory @dir
7
-
8
- preload_app true
9
-
10
- timeout 30
11
-
12
- # Specify path to socket unicorn listens to,
13
- # we will use this in our nginx.conf later
14
- listen "#{@dir}tmp/sockets/unicorn.sock", :backlog => 64
15
-
16
- # Set process id path
17
- pid "#{@dir}tmp/pids/unicorn.pid"
18
-
19
- # # Set log file paths
20
- stderr_path "#{@dir}log/unicorn.stderr.log"
21
- stdout_path "#{@dir}log/unicorn.stdout.log"
22
-
23
- # NOTE: You need this when using forking web servers!
24
- after_fork do |server, worker|
25
- $con.close if $con
26
- $con = Mongo::Connection.new
27
- $db = $con['foo']
28
- STDERR << "FORKED #{server} #{worker}"
29
- end