mongo 1.0 → 1.1.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 (95) hide show
  1. data/LICENSE.txt +1 -13
  2. data/{README.rdoc → README.md} +129 -149
  3. data/Rakefile +94 -58
  4. data/bin/mongo_console +21 -0
  5. data/docs/1.0_UPGRADE.md +21 -0
  6. data/docs/CREDITS.md +123 -0
  7. data/docs/FAQ.md +112 -0
  8. data/docs/GridFS.md +158 -0
  9. data/docs/HISTORY.md +185 -0
  10. data/docs/REPLICA_SETS.md +75 -0
  11. data/docs/TUTORIAL.md +247 -0
  12. data/docs/WRITE_CONCERN.md +28 -0
  13. data/lib/mongo/collection.rb +225 -105
  14. data/lib/mongo/connection.rb +374 -315
  15. data/lib/mongo/cursor.rb +122 -77
  16. data/lib/mongo/db.rb +109 -85
  17. data/lib/mongo/exceptions.rb +6 -0
  18. data/lib/mongo/gridfs/grid.rb +19 -11
  19. data/lib/mongo/gridfs/grid_ext.rb +36 -9
  20. data/lib/mongo/gridfs/grid_file_system.rb +15 -9
  21. data/lib/mongo/gridfs/grid_io.rb +49 -16
  22. data/lib/mongo/gridfs/grid_io_fix.rb +38 -0
  23. data/lib/mongo/repl_set_connection.rb +290 -0
  24. data/lib/mongo/util/conversions.rb +3 -1
  25. data/lib/mongo/util/core_ext.rb +17 -4
  26. data/lib/mongo/util/pool.rb +125 -0
  27. data/lib/mongo/util/server_version.rb +2 -0
  28. data/lib/mongo/util/support.rb +12 -0
  29. data/lib/mongo/util/uri_parser.rb +71 -0
  30. data/lib/mongo.rb +23 -7
  31. data/{mongo-ruby-driver.gemspec → mongo.gemspec} +9 -7
  32. data/test/auxillary/1.4_features.rb +2 -2
  33. data/test/auxillary/authentication_test.rb +1 -1
  34. data/test/auxillary/autoreconnect_test.rb +1 -1
  35. data/test/{slave_connection_test.rb → auxillary/slave_connection_test.rb} +6 -6
  36. data/test/bson/binary_test.rb +15 -0
  37. data/test/bson/bson_test.rb +537 -0
  38. data/test/bson/byte_buffer_test.rb +190 -0
  39. data/test/bson/hash_with_indifferent_access_test.rb +38 -0
  40. data/test/bson/json_test.rb +17 -0
  41. data/test/bson/object_id_test.rb +141 -0
  42. data/test/bson/ordered_hash_test.rb +197 -0
  43. data/test/collection_test.rb +195 -15
  44. data/test/connection_test.rb +93 -56
  45. data/test/conversions_test.rb +1 -1
  46. data/test/cursor_fail_test.rb +75 -0
  47. data/test/cursor_message_test.rb +43 -0
  48. data/test/cursor_test.rb +93 -32
  49. data/test/db_api_test.rb +28 -55
  50. data/test/db_connection_test.rb +2 -3
  51. data/test/db_test.rb +45 -40
  52. data/test/grid_file_system_test.rb +14 -6
  53. data/test/grid_io_test.rb +36 -7
  54. data/test/grid_test.rb +54 -10
  55. data/test/replica_sets/connect_test.rb +84 -0
  56. data/test/replica_sets/count_test.rb +35 -0
  57. data/test/{replica → replica_sets}/insert_test.rb +17 -14
  58. data/test/replica_sets/pooled_insert_test.rb +55 -0
  59. data/test/replica_sets/query_secondaries.rb +80 -0
  60. data/test/replica_sets/query_test.rb +41 -0
  61. data/test/replica_sets/replication_ack_test.rb +64 -0
  62. data/test/replica_sets/rs_test_helper.rb +29 -0
  63. data/test/safe_test.rb +68 -0
  64. data/test/support/hash_with_indifferent_access.rb +199 -0
  65. data/test/support/keys.rb +45 -0
  66. data/test/support_test.rb +19 -0
  67. data/test/test_helper.rb +53 -15
  68. data/test/threading/{test_threading_large_pool.rb → threading_with_large_pool_test.rb} +2 -2
  69. data/test/threading_test.rb +2 -2
  70. data/test/tools/repl_set_manager.rb +241 -0
  71. data/test/tools/test.rb +13 -0
  72. data/test/unit/collection_test.rb +70 -7
  73. data/test/unit/connection_test.rb +18 -39
  74. data/test/unit/cursor_test.rb +7 -8
  75. data/test/unit/db_test.rb +14 -17
  76. data/test/unit/grid_test.rb +49 -0
  77. data/test/unit/pool_test.rb +9 -0
  78. data/test/unit/repl_set_connection_test.rb +82 -0
  79. data/test/unit/safe_test.rb +125 -0
  80. metadata +132 -51
  81. data/bin/bson_benchmark.rb +0 -59
  82. data/bin/fail_if_no_c.rb +0 -11
  83. data/examples/admin.rb +0 -43
  84. data/examples/capped.rb +0 -22
  85. data/examples/cursor.rb +0 -48
  86. data/examples/gridfs.rb +0 -44
  87. data/examples/index_test.rb +0 -126
  88. data/examples/info.rb +0 -31
  89. data/examples/queries.rb +0 -70
  90. data/examples/simple.rb +0 -24
  91. data/examples/strict.rb +0 -35
  92. data/examples/types.rb +0 -36
  93. data/test/replica/count_test.rb +0 -34
  94. data/test/replica/pooled_insert_test.rb +0 -54
  95. data/test/replica/query_test.rb +0 -39
data/LICENSE.txt CHANGED
@@ -175,18 +175,7 @@
175
175
 
176
176
  END OF TERMS AND CONDITIONS
177
177
 
178
- APPENDIX: How to apply the Apache License to your work.
179
-
180
- To apply the Apache License to your work, attach the following
181
- boilerplate notice, with the fields enclosed by brackets "[]"
182
- replaced with your own identifying information. (Don't include
183
- the brackets!) The text should be enclosed in the appropriate
184
- comment syntax for the file format. We also recommend that a
185
- file or class name and description of purpose be included on the
186
- same "printed page" as the copyright notice for easier
187
- identification within third-party archives.
188
-
189
- Copyright [yyyy] [name of copyright owner]
178
+ Copyright 2008-2010 10gen, Inc.
190
179
 
191
180
  Licensed under the Apache License, Version 2.0 (the "License");
192
181
  you may not use this file except in compliance with the License.
@@ -199,4 +188,3 @@
199
188
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
189
  See the License for the specific language governing permissions and
201
190
  limitations under the License.
202
-
@@ -1,81 +1,101 @@
1
- = Introduction
1
+ # Introduction
2
2
 
3
- This is the 10gen-supported Ruby driver for MongoDB[http://www.mongodb.org].
3
+ This is the 10gen-supported Ruby driver for [MongoDB](http://www.mongodb.org).
4
4
 
5
- Here's a quick code sample. See the MongoDB Ruby Tutorial
6
- (http://www.mongodb.org/display/DOCS/Ruby+Tutorial) for much more.
5
+ This documentation includes other articles of interest, include:
7
6
 
8
- require 'rubygems'
9
- require 'mongo'
10
- include Mongo
7
+ 1. [A tutorial](http://api.mongodb.org/ruby/current/file.TUTORIAL.html).
8
+ 2. [Replica Sets in Ruby](http://api.mongodb.org/ruby/current/file.REPLICA_SETS.html).
9
+ 3. [Write Concern in Ruby](http://api.mongodb.org/ruby/current/file.WRITE_CONCERN.html).
10
+ 4. [GridFS in Ruby](http://api.mongodb.org/ruby/current/file.GridFS.html).
11
+ 5. [Frequently Asked Questions](http://api.mongodb.org/ruby/current/file.FAQ.html).
12
+ 6. [History](http://api.mongodb.org/ruby/current/file.HISTORY.html).
13
+ 7. [Credits](http://api.mongodb.org/ruby/current/file.CREDITS.html).
11
14
 
12
- db = Connection.new.db('sample-db')
13
- coll = db.collection('test')
15
+ Here's a quick code sample. Again, see the [MongoDB Ruby Tutorial](file.TUTORIAL.html)
16
+ for much more:
14
17
 
15
- coll.remove
16
- 3.times do |i|
17
- coll.insert({'a' => i+1})
18
- end
19
- puts "There are #{coll.count()} records. Here they are:"
20
- coll.find().each { |doc| puts doc.inspect }
18
+ require 'rubygems'
19
+ require 'mongo'
20
+ include Mongo
21
+
22
+ db = Connection.new.db('sample-db')
23
+ coll = db.collection('test')
24
+
25
+ coll.remove
26
+ 3.times do |i|
27
+ coll.insert({'a' => i+1})
28
+ end
29
+ puts "There are #{coll.count()} records. Here they are:"
30
+ coll.find().each { |doc| puts doc.inspect }
31
+
32
+ # Installation
33
+
34
+ ### Ruby Versions
35
+
36
+ The driver works and is consistently tested on Ruby 1.8.6, 1.8.7, and 1.9.2, and JRuby 1.5.1.
37
+
38
+ Note that if you're on 1.8.7, be sure that you're using a patchlevel >= 249. There
39
+ are some IO bugs in earlier versions.
21
40
 
22
- = Installation
41
+ ### Gems
23
42
 
24
- The driver's gems are hosted at Rubygems.org[http://rubygems.org]. Make sure you're
43
+ The driver's gems are hosted at [Rubygems.org](http://rubygems.org). Make sure you're
25
44
  using the latest version of rubygems:
26
45
 
27
- $ gem update --system
46
+ $ gem update --system
28
47
 
29
48
  Then you can install the mongo gem as follows:
30
49
 
31
- $ gem install mongo
50
+ $ gem install mongo
32
51
 
33
- The driver also requires the BSON gem:
52
+ The driver also requires the bson gem:
34
53
 
35
- $ gem install bson
54
+ $ gem install bson
36
55
 
37
56
  And for a significant performance boost, you'll want to install the C extensions:
38
57
 
39
- $ gem install bson_ext
58
+ $ gem install bson_ext
40
59
 
41
- === From the GitHub source
60
+ Note that bson_ext isn't used with JRuby. Instead, some native Java extensions are bundled with the bson gem.
61
+ If you ever need to modify these extenions, you can recompile with the following rake task:
62
+
63
+ $ rake build:java
64
+
65
+ ### From the GitHub source
42
66
 
43
67
  The source code is available at http://github.com/mongodb/mongo-ruby-driver.
44
68
  You can either clone the git repository or download a tarball or zip file.
45
69
  Once you have the source, you can use it from wherever you downloaded it or
46
70
  you can install it as a gem from the source by typing
47
71
 
48
- $ rake gem:install
72
+ $ rake gem:install
49
73
 
50
74
  To install the C extensions from source, type this instead:
51
75
 
52
- $ rake gem:install_extensions
76
+ $ rake gem:install_extensions
53
77
 
54
78
  That's all there is to it!
55
79
 
56
- = Examples
80
+ # Examples
57
81
 
58
- For extensive examples, see the MongoDB Ruby Tutorial
59
- (http://www.mongodb.org/display/DOCS/Ruby+Tutorial).
82
+ For extensive examples, see the [MongoDB Ruby Tutorial](http://www.mongodb.org/display/DOCS/Ruby+Tutorial).
60
83
 
61
- Bundled with the driver are many examples, located in the "examples" subdirectory. Samples include using
84
+ Bundled with the driver are many examples, located in the "docs/examples" subdirectory. Samples include using
62
85
  the driver and using the GridFS class GridStore. MongoDB must be running for
63
86
  these examples to work, of course.
64
87
 
65
88
  Here's how to start MongoDB and run the "simple.rb" example:
66
89
 
67
- $ cd path/to/mongo
68
- $ ./mongod run
69
- ... then in another window ...
70
- $ cd path/to/mongo-ruby-driver
71
- $ ruby examples/simple.rb
90
+ $ cd path/to/mongo
91
+ $ ./mongod run
92
+ ... then in another window ...
93
+ $ cd path/to/mongo-ruby-driver
94
+ $ ruby docs/examples/simple.rb
72
95
 
73
96
  See also the test code, especially test/test_db_api.rb.
74
97
 
75
- = GridFS
76
-
77
- Note: The GridStore class has been deprecated. Use either the Grid or GridFileSystem
78
- classes to take advantage of GridFS.
98
+ # GridFS
79
99
 
80
100
  The Ruby driver include two abstractions for storing large files: Grid and GridFileSystem.
81
101
  The Grid class is a Ruby implementation of MongoDB's GridFS file storage
@@ -87,87 +107,56 @@ for details, and see examples/gridfs.rb for code that uses many of the Grid
87
107
  features (metadata, content type, seek, tell, etc).
88
108
 
89
109
  Examples:
90
- include Mongo
91
-
92
- # Get a database
93
- db = Mongo::Connection.new.db('app-db')
94
-
95
- # GridFileSystem. Store the text "Hello, world!" in the fs.
96
- fs = GridFileSystem.new(db)
97
- fs.open('filename', 'w') do |f|
98
- f.write "Hello, world!"
99
- end
100
-
101
- # GridFileSystem. Output "Hello, world!"
102
- fs = GridFileSystem.new(db)
103
- fs.open('filename', 'r') do |f|
104
- puts f.read
105
- end
110
+ # Write a file on disk to the Grid
111
+ file = File.open('image.jpg')
112
+ grid = Grid.new(db)
113
+ id = grid.put(file)
106
114
 
107
- # Write a file on disk to the Grid
108
- file = File.open('image.jpg')
109
- grid = Grid.new(db)
110
- id = grid.put(file)
115
+ # Retrieve the file
116
+ file = grid.get(id)
117
+ file.read
111
118
 
112
- # Retrieve the file
113
- file = grid.get(id)
114
- file.read
119
+ # Get all the file's metata
120
+ file.filename
121
+ file.content_type
122
+ file.metadata
115
123
 
116
- # Get all the file's metata
117
- file.filename
118
- file.content_type
119
- file.metadata
124
+ # Notes
120
125
 
121
- = Notes
122
-
123
- == Thread Safety
126
+ ## Thread Safety
124
127
 
125
128
  The driver is thread-safe.
126
129
 
127
- == Connection Pooling
130
+ ## Connection Pooling
128
131
 
129
- As of 0.18, the driver implements connection pooling. By default, only one
132
+ As of v0.18, the driver implements connection pooling. By default, only one
130
133
  socket connection will be opened to MongoDB. However, if you're running a
131
134
  multi-threaded application, you can specify a maximum pool size and a maximum
132
135
  timeout for waiting for old connections to be released to the pool.
133
136
 
134
137
  To set up a pooled connection to a single MongoDB instance:
135
138
 
136
- @conn = Connection.new("localhost", 27017, :pool_size => 5, :timeout => 5)
137
-
138
- A pooled connection to a paired instance would look like this:
139
-
140
- @conn = Connection.new({:left => ["db1.example.com", 27017],
141
- :right => ["db2.example.com", 27017]}, nil,
142
- :pool_size => 20, :timeout => 5)
139
+ @conn = Connection.new("localhost", 27017, :pool_size => 5, :timeout => 5)
143
140
 
144
141
  Though the pooling architecture will undoubtedly evolve, it currently owes much credit
145
142
  to the connection pooling implementations in ActiveRecord and PyMongo.
146
143
 
147
- == Using with Phusion Passenger
144
+ ## Using with Phusion Passenger and Unicorn
148
145
 
149
- When passenger is in smart spawning mode you need to be sure that child
150
- processes forked by passenger will create a new connection to the database.
151
- activerecord-mongo-adapter handles this for you, so if you are using that
152
- you shouldn't need to worry about it. Otherwise you'll either need to use
153
- conservative spawning[http://www.modrails.org/documentation/Users%20guide.html#RailsSpawnMethod]
154
- or handle reconnecting when passenger forks a new process:
146
+ When Passenger and Unicorn are in smart spawning mode you need to be sure that child
147
+ processes will create a new connection to the database. In Passenger, this can be handled like so:
155
148
 
156
- if defined?(PhusionPassenger)
157
- PhusionPassenger.on_event(:starting_worker_process) do |forked|
158
- if forked
159
- # Call db.connect_to_master to reconnect here
149
+ if defined?(PhusionPassenger)
150
+ PhusionPassenger.on_event(:starting_worker_process) do |forked|
151
+ if forked
152
+ # Create new connection here
153
+ end
160
154
  end
161
155
  end
162
- end
163
156
 
164
- The above code should be put in _environment.rb_ or in an initialization
165
- script.
157
+ The above code should be put into a Rails initializer or other initialization script.
166
158
 
167
- See this thread[http://groups.google.com/group/mongodb-user/browse_thread/thread/f31e2d23de38136a]
168
- for more details on this issue.
169
-
170
- == String Encoding
159
+ ## String Encoding
171
160
 
172
161
  The BSON ("Binary JSON") format used to communicate with Mongo requires that
173
162
  strings be UTF-8 (http://en.wikipedia.org/wiki/UTF-8).
@@ -177,18 +166,18 @@ and received from Mongo are converted to UTF-8 when necessary, and strings
177
166
  read from Mongo will have their character encodings set to UTF-8.
178
167
 
179
168
  When used with Ruby 1.8, the bytes in each string are written to and read from
180
- Mongo as-is. If the string is ASCII all is well, because ASCII is a subset of
169
+ Mongo as is. If the string is ASCII, all is well, because ASCII is a subset of
181
170
  UTF-8. If the string is not ASCII, it may not be a well-formed UTF-8
182
171
  string.
183
172
 
184
- == Primary Keys
173
+ ## Primary Keys
185
174
 
186
- The field _id is a primary key. It is treated specially by the database, and
175
+ The `_id` field is a primary key. It is treated specially by the database, and
187
176
  its use makes many operations more efficient. The value of an _id may be of
188
177
  any type. The database itself inserts an _id value if none is specified when
189
178
  a record is inserted.
190
179
 
191
- === Primary Key Factories
180
+ ### Primary Key Factories
192
181
 
193
182
  A primary key factory is a class you supply to a DB object that knows how to
194
183
  generate _id values. If you want to control _id values or even their types,
@@ -197,7 +186,7 @@ using a PK factory lets you do so.
197
186
  You can tell the Ruby Mongo driver how to create primary keys by passing in
198
187
  the :pk option to the Connection#db method.
199
188
 
200
- db = Mongo::Connection.new.db('dbname', :pk => MyPKFactory.new)
189
+ db = Mongo::Connection.new.db('dbname', :pk => MyPKFactory.new)
201
190
 
202
191
  A primary key factory object must respond to :create_pk, which should
203
192
  take a hash and return a hash which merges the original hash with any
@@ -211,12 +200,12 @@ returned will be inserted.
211
200
 
212
201
  Here is a sample primary key factory, taken from the tests:
213
202
 
214
- class TestPKFactory
215
- def create_pk(row)
216
- row['_id'] ||= Mongo::ObjectID.new
217
- row
203
+ class TestPKFactory
204
+ def create_pk(row)
205
+ row['_id'] ||= Mongo::ObjectID.new
206
+ row
207
+ end
218
208
  end
219
- end
220
209
 
221
210
  Here's a slightly more sophisticated one that handles both symbol and string
222
211
  keys. This is the PKFactory that comes with the MongoRecord code (an
@@ -238,13 +227,9 @@ changeable at all is so that libraries such as MongoRecord that use this
238
227
  driver can set the PK factory after obtaining the database but before using it
239
228
  for the first time.
240
229
 
241
- == The DB Class
242
-
243
- === Primary Key factories
244
-
245
- See the section on "Primary Keys" above.
230
+ ## The DB Class
246
231
 
247
- === Strict mode
232
+ ### Strict mode
248
233
 
249
234
  Each database has an optional strict mode. If strict mode is on, then asking
250
235
  for a collection that does not exist will raise an error, as will asking to
@@ -252,90 +237,85 @@ create a collection that already exists. Note that both these operations are
252
237
  completely harmless; strict mode is a programmer convenience only.
253
238
 
254
239
  To turn on strict mode, either pass in :strict => true when obtaining a DB
255
- object or call the :strict= method:
240
+ object or call the `:strict=` method:
256
241
 
257
- db = Connection.new.db('dbname', :strict => true)
258
- # I'm feeling lax
259
- db.strict = false
260
- # No, I'm not!
261
- db.strict = true
242
+ db = Connection.new.db('dbname', :strict => true)
243
+ # I'm feeling lax
244
+ db.strict = false
245
+ # No, I'm not!
246
+ db.strict = true
262
247
 
263
248
  The method DB#strict? returns the current value of that flag.
264
249
 
265
- == Cursors
250
+ ## Cursors
266
251
 
267
- Random cursor fun facts:
252
+ Notes:
268
253
 
269
- - Cursors are enumerable.
254
+ * Cursors are enumerable (and have a #to_a method).
270
255
 
271
- - The query doesn't get run until you actually attempt to retrieve data from a
256
+ * The query doesn't get run until you actually attempt to retrieve data from a
272
257
  cursor.
273
258
 
274
- - Cursors have a to_a method.
259
+ * Cursors will timeout on the server after 10 minutes. If you need to keep a cursor
260
+ open for more than 10 minutes, specify `:timeout => false` when you create the cursor.
275
261
 
276
262
 
277
- = Testing
263
+ # Testing
278
264
 
279
- If you have the source code, you can run the tests. There's a separate rake task for testing with
280
- the bson_ext C extension enabled.
265
+ If you have the source code, you can run the tests.
281
266
 
282
- $ rake test:c
267
+ $ rake test:c
283
268
 
284
- Or, to test without the extension:
269
+ If you want to test the basic Ruby encoder, without the C extension, or if you're running JRuby:
285
270
 
286
- $ rake test:ruby
271
+ $ rake test:ruby
287
272
 
288
273
  These will run both unit and functional tests. To run these tests alone:
289
274
 
290
- $ rake test:unit
291
- $ rake test:functional
275
+ $ rake test:unit
276
+ $ rake test:functional
292
277
 
293
278
  To run any individual rake tasks with the C extension enabled, just pass C_EXT=true to the task:
294
279
 
295
- $ rake test:unit C_EXT=true
280
+ $ rake test:unit C_EXT=true
296
281
 
297
- If you want to test replica pairs, you can run the following tests
282
+ If you want to test replica set, you can run the following tests
298
283
  individually:
299
284
 
300
- $ rake test:pair_count
301
- $ rake test:pair_insert
302
- $ rake test:pair_query
285
+ $ rake test:replica_set_count
286
+ $ rake test:replica_set_insert
287
+ $ rake test:replica_set_query
303
288
 
304
- It's also possible to test replica pairs with connection pooling:
305
-
306
- $ rake test:pooled_pair_insert
307
-
308
- ===Shoulda and Mocha
289
+ ### Shoulda and Mocha
309
290
 
310
291
  Running the test suite requires shoulda and mocha. You can install them as follows:
311
292
 
312
- $ gem install shoulda
313
- $ gem install mocha
293
+ $ gem install shoulda
294
+ $ gem install mocha
314
295
 
315
296
  The tests assume that the Mongo database is running on the default port. You
316
297
  can override the default host (localhost) and port (Connection::DEFAULT_PORT) by
317
298
  using the environment variables MONGO_RUBY_DRIVER_HOST and
318
299
  MONGO_RUBY_DRIVER_PORT.
319
300
 
320
- = Documentation
301
+ # Documentation
321
302
 
322
- This documentation is available online at http://api.mongodb.org/ruby. You can
303
+ This documentation is available online at [http://api.mongodb.org/ruby](http://api.mongodb.org/ruby). You can
323
304
  generate the documentation if you have the source by typing
324
305
 
325
- $ rake ydoc
306
+ $ rake ydoc
326
307
 
327
308
  Then open the file +ydoc/index.html+.
328
309
 
329
-
330
- = Release Notes
310
+ # Release Notes
331
311
 
332
312
  See HISTORY.
333
313
 
334
- = Credits
314
+ # Credits
335
315
 
336
316
  See CREDITS.
337
317
 
338
- = License
318
+ # License
339
319
 
340
320
  Copyright 2008-2010 10gen Inc.
341
321