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
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 756bee5f8dcecc237006f5ebfe8d4087ae06f855
4
+ data.tar.gz: c504ef1e1492dee4ff6e5bdab83fac92cdf54069
5
+ SHA512:
6
+ metadata.gz: d87e98fa9b33d9d446128a61efc85e0b7e0c67ebfd58bc2e42758156a4794d2c5aa09ff4dd3a87900b1593213145d813aa663df58d6d1fb11325441214fc29fe
7
+ data.tar.gz: 07ec6387420ed99a2308266fa50342de858f0fbac16ee308853f2fe36e633901e61c160b67ad292e1cc25b47135ae19267ec08d14fea3cb8a7e3f3b4fa6d4916
checksums.yaml.gz.sig ADDED
Binary file
@@ -175,7 +175,7 @@
175
175
 
176
176
  END OF TERMS AND CONDITIONS
177
177
 
178
- Copyright 2008-2010 10gen, Inc.
178
+ Copyright (C) 2008-2013 MongoDB, Inc.
179
179
 
180
180
  Licensed under the Apache License, Version 2.0 (the "License");
181
181
  you may not use this file except in compliance with the License.
data/README.md CHANGED
@@ -1,332 +1,183 @@
1
- # Introduction
1
+ MongoDB Ruby Driver [![Build Status][travis-img]][travis-url] [![Code Climate][codeclimate-img]][codeclimate-url] [![Coverage Status][coveralls-img]][coveralls-url] [![Gem Version][rubygems-img]][rubygems-url]
2
+ -----
3
+ The officially supported Ruby driver for [MongoDB](http://www.mongodb.org).
2
4
 
3
- This is the 10gen-supported Ruby driver for [MongoDB](http://www.mongodb.org).
5
+ Installation
6
+ -----
4
7
 
5
- This documentation includes other articles of interest, include:
8
+ **Gem Installation**<br>
9
+ The Ruby driver is released and distributed through RubyGems and it can be installed with the following command:
6
10
 
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
- 6. [Release plan](http://api.mongodb.org/ruby/current/file.RELEASES.html).
14
- 7. [Credits](http://api.mongodb.org/ruby/current/file.CREDITS.html).
11
+ ```bash
12
+ gem install mongo
13
+ ```
15
14
 
16
- Here's a quick code sample. Again, see the [MongoDB Ruby Tutorial](http://api.mongodb.org/ruby/current/file.TUTORIAL.html)
17
- for much more:
15
+ For a significant performance boost, you'll want to install the C-extension:
18
16
 
19
- require 'rubygems'
20
- require 'mongo'
21
- include Mongo
17
+ ```bash
18
+ gem install bson_ext
19
+ ```
22
20
 
23
- db = Connection.new.db('sample-db')
24
- coll = db.collection('test')
21
+ **Github Installation**<br>
22
+ For development and test environments (not recommended for production) you can also install the Ruby driver directly from source:
25
23
 
26
- coll.remove
27
- 3.times do |i|
28
- coll.insert({'a' => i+1})
29
- end
30
- puts "There are #{coll.count()} records. Here they are:"
31
- coll.find().each { |doc| puts doc.inspect }
24
+ ```bash
25
+ # clone the repository
26
+ git clone https://github.com/mongodb/mongo-ruby-driver.git
27
+ cd mongo-ruby-driver
32
28
 
33
- # Installation
29
+ # checkout a specific version by tag (optional)
30
+ git checkout 1.x.x
34
31
 
35
- ### Ruby Versions
32
+ # install all development dependencies
33
+ gem install bundler
34
+ bundle install
36
35
 
37
- The driver works and is consistently tested on Ruby 1.8.6, 1.8.7, and 1.9.2, and JRuby 1.5.1.
36
+ # install the ruby driver
37
+ rake install
38
+ ```
38
39
 
39
- Note that if you're on 1.8.7, be sure that you're using a patchlevel >= 249. There
40
- are some IO bugs in earlier versions.
40
+ To be able to use the driver with Kerberos authentication enabled, install the
41
+ `mongo_kerberos` gem and add it instead of mongo to your application:
41
42
 
42
- ### Gems
43
+ ```bash
44
+ gem install mongo_kerberos
45
+ ```
43
46
 
44
- The driver's gems are hosted at [Rubygems.org](http://rubygems.org). Make sure you're
45
- using the latest version of rubygems:
47
+ ```ruby
48
+ require 'mongo_kerberos'
49
+ ```
46
50
 
47
- $ gem update --system
51
+ Usage
52
+ -----
53
+ Here is a quick example of basic usage for the Ruby driver:
48
54
 
49
- Then you can install the mongo gem as follows:
55
+ ```ruby
56
+ require 'mongo'
57
+ include Mongo
50
58
 
51
- $ gem install mongo
59
+ # connecting to the database
60
+ client = MongoClient.new # defaults to localhost:27017
61
+ db = client['example-db']
62
+ coll = db['example-collection']
52
63
 
53
- The driver also requires the bson gem:
64
+ # inserting documents
65
+ 10.times { |i| coll.insert({ :count => i+1 }) }
54
66
 
55
- $ gem install bson
67
+ # finding documents
68
+ puts "There are #{coll.count} total documents. Here they are:"
69
+ coll.find.each { |doc| puts doc.inspect }
56
70
 
57
- And for a significant performance boost, you'll want to install the C extensions:
71
+ # updating documents
72
+ coll.update({ :count => 5 }, { :count => 'foobar' })
58
73
 
59
- $ gem install bson_ext
74
+ # removing documents
75
+ coll.remove({ :count => 8 })
76
+ coll.remove
77
+ ```
60
78
 
61
- Note that bson_ext isn't used with JRuby. Instead, some native Java extensions are bundled with the bson gem.
62
- If you ever need to modify these extenions, you can recompile with the following rake task:
79
+ Wiki - Tutorials & Examples
80
+ -----
81
+ For many more usage examples and a full tutorial, please visit our [wiki](https://github.com/mongodb/mongo-ruby-driver/wiki).<br>
63
82
 
64
- $ rake build:java
83
+ API Reference Documentation
84
+ -----
85
+ For API reference documentation, please visit [here](http://api.mongodb.org/ruby).
65
86
 
66
- ### From the GitHub source
87
+ Compatibility
88
+ -----
89
+ The MongoDB Ruby driver requires Ruby 1.8.7 or greater and is regularly tested against the platforms and environments listed below.
67
90
 
68
- The source code is available at http://github.com/mongodb/mongo-ruby-driver.
69
- You can either clone the git repository or download a tarball or zip file.
70
- Once you have the source, you can use it from wherever you downloaded it or
71
- you can install it as a gem from the source by typing
91
+ Ruby Platforms | Operating Systems | Architectures
92
+ -------------- | ----------------- | -------------
93
+ MRI 1.8.7, 1.9.3, 2.0.0<br>JRuby 1.7.x | Windows<br>Linux<br>OS X | x86<br>x64<br>ARM
72
94
 
73
- $ rake gem:install
95
+ Support & Feedback
96
+ -----
74
97
 
75
- To install the C extensions from source, type this instead:
98
+ **Support Channels**
76
99
 
77
- $ rake gem:install_extensions
100
+ For issues, questions or feedback related to the Ruby driver, please look into our [support channels](http://www.mongodb.org/about/support).
101
+ Please do not email any of the Ruby developers directly with issues or questions. You'll get a quicker answer on the [mongodb-user list](http://groups.google.com/group/mongodb-user) Google Group.
78
102
 
79
- That's all there is to it!
103
+ Bugs & Feature Requests
104
+ -----
80
105
 
81
- # Examples
106
+ Do you have a bug to report or a feature request to make?
82
107
 
83
- For extensive examples, see the [MongoDB Ruby Tutorial](http://www.mongodb.org/display/DOCS/Ruby+Tutorial).
108
+ 1. Visit [our issue tracker](https://jira.mongodb.org) and login (or create an account if necessary).
109
+ 2. Navigate to the [RUBY](https://jira.mongodb.org/browse/RUBY) project.
110
+ 3. Click 'Create Issue' and fill out all the applicable form fields.
84
111
 
85
- Bundled with the driver are many examples, located in the "docs/examples" subdirectory. Samples include using
86
- the driver and using the GridFS class GridStore. MongoDB must be running for
87
- these examples to work, of course.
112
+ When reporting an issue, please keep in mind that all information in JIRA for all driver projects (ex. RUBY, CSHARP, JAVA) and the Core Server (ex. SERVER) project is **PUBLICLY** visible.
88
113
 
89
- Here's how to start MongoDB and run the "simple.rb" example:
114
+ **HOW TO ASK FOR HELP**
90
115
 
91
- $ cd path/to/mongo
92
- $ ./mongod run
93
- ... then in another window ...
94
- $ cd path/to/mongo-ruby-driver
95
- $ ruby docs/examples/simple.rb
116
+ Providing enough information so we can reproduce the issue immediately will reduce roundtrip communications and get you a useful response as quickly as possible.
117
+ That said, please provide the following information when logging an issue:
96
118
 
97
- See also the test code, especially test/test_db_api.rb.
119
+ 1. Environment
120
+ 2. Ruby version, including patch-level
121
+ 3. MongoDB version
122
+ 4. A test case or code snippets
123
+ 5. Stack traces and log data, keeping in mind that this info is public
98
124
 
99
- # GridFS
125
+ **PLEASE DO NOT**
100
126
 
101
- The Ruby driver include two abstractions for storing large files: Grid and GridFileSystem.
102
- The Grid class is a Ruby implementation of MongoDB's GridFS file storage
103
- specification. GridFileSystem is essentailly the same, but provides a more filesystem-like API
104
- and assumes that filenames are unique.
127
+ * Provide any sensitive data or server logs.
128
+ * Report potential security issues publicly (see 'Security Issues').
105
129
 
106
- An instance of both classes represents an individual file store. See the API reference
107
- for details, and see examples/gridfs.rb for code that uses many of the Grid
108
- features (metadata, content type, seek, tell, etc).
130
+ **EXAMPLE BUG REPORT**
109
131
 
110
- Examples:
111
- # Write a file on disk to the Grid
112
- file = File.open('image.jpg')
113
- grid = Grid.new(db)
114
- id = grid.put(file)
132
+ Example taken from [RUBY-775](https://jira.mongodb.org/browse/RUBY-775)
115
133
 
116
- # Retrieve the file
117
- file = grid.get(id)
118
- file.read
134
+ ```
135
+ There appears to be a recursive locking condition in the replica set connection pooling.
119
136
 
120
- # Get all the file's metata
121
- file.filename
122
- file.content_type
123
- file.metadata
137
+ Environment: AWS Linux 3.10.37-47.135.amzn1.x86_64 / jruby-1.7.12 / JDK java-1.7.0-openjdk-1.7.0.55-2.4.7.1.40.amzn1.x86_64
124
138
 
125
- # Notes
139
+ Component: Connection Pooling / Replica set
126
140
 
127
- ## Thread Safety
141
+ Here is a stack trace:
142
+ https://gist.githubusercontent.com/cheald/5ed01172c5b2c9943c87/raw/63075158dac4c78c1775cac8bf84ba3b6537bc1e/gistfile1.txt
128
143
 
129
- The driver is thread-safe.
144
+ The original lock occurs [here](https://github.com/mongodb/mongo-ruby-driver/blob/1.x-stable/lib/mongo/connection/pool_manager.rb#L60)
130
145
 
131
- ## Connection Pooling
146
+ and then the process of reconnecting ends up attempting to resynchronize the same lock [here](https://github.com/mongodb/mongo-ruby-driver/blob/1.x-stable/lib/mongo/connection/pool_manager.rb#L150)
147
+ ```
132
148
 
133
- The driver implements connection pooling. By default, only one
134
- socket connection will be opened to MongoDB. However, if you're running a
135
- multi-threaded application, you can specify a maximum pool size and a maximum
136
- timeout for waiting for old connections to be released to the pool.
149
+ Security Issues
150
+ -----
137
151
 
138
- To set up a pooled connection to a single MongoDB instance:
152
+ If you’ve identified a potential security related issue in a driver or any other MongoDB project, please report it by following the [instructions here](http://docs.mongodb.org/manual/tutorial/create-a-vulnerability-report).
139
153
 
140
- @conn = Connection.new("localhost", 27017, :pool_size => 5, :timeout => 5)
154
+ Release History
155
+ -----
141
156
 
142
- Though the pooling architecture will undoubtedly evolve, it currently owes much credit
143
- to the connection pooling implementations in ActiveRecord and PyMongo.
157
+ Full release notes and release history are available [here](https://github.com/mongodb/mongo-ruby-driver/releases).
144
158
 
145
- ## Forking
159
+ License
160
+ -----
146
161
 
147
- Certain Ruby application servers work by forking, and it has long been necessary to
148
- re-establish the child process's connection to the database after fork. But with the release
149
- of v1.3.0, the Ruby driver detects forking and reconnects automatically.
150
-
151
- ## String Encoding
152
-
153
- The BSON ("Binary JSON") format used to communicate with Mongo requires that
154
- strings be UTF-8 (http://en.wikipedia.org/wiki/UTF-8).
155
-
156
- Ruby 1.9 has built-in character encoding support. All strings sent to Mongo
157
- and received from Mongo are converted to UTF-8 when necessary, and strings
158
- read from Mongo will have their character encodings set to UTF-8.
159
-
160
- When used with Ruby 1.8, the bytes in each string are written to and read from
161
- Mongo as is. If the string is ASCII, all is well, because ASCII is a subset of
162
- UTF-8. If the string is not ASCII, it may not be a well-formed UTF-8
163
- string.
164
-
165
- ## Primary Keys
166
-
167
- The `_id` field is a primary key. It is treated specially by the database, and
168
- its use makes many operations more efficient. The value of an _id may be of
169
- any type. The database itself inserts an _id value if none is specified when
170
- a record is inserted.
171
-
172
- ### Primary Key Factories
173
-
174
- A primary key factory is a class you supply to a DB object that knows how to
175
- generate _id values. If you want to control _id values or even their types,
176
- using a PK factory lets you do so.
177
-
178
- You can tell the Ruby Mongo driver how to create primary keys by passing in
179
- the :pk option to the Connection#db method.
180
-
181
- db = Mongo::Connection.new.db('dbname', :pk => MyPKFactory.new)
182
-
183
- A primary key factory object must respond to :create_pk, which should
184
- take a hash and return a hash which merges the original hash with any
185
- primary key fields the factory wishes to inject.
186
-
187
- NOTE: if the object already has a primary key, the factory should not
188
- inject a new key; this means that the object may already exist in the
189
- database. The idea here is that whenever a record is inserted, the
190
- :pk object's +create_pk+ method will be called and the new hash
191
- returned will be inserted.
192
-
193
- Here is a sample primary key factory, taken from the tests:
194
-
195
- class TestPKFactory
196
- def create_pk(row)
197
- row['_id'] ||= Mongo::ObjectID.new
198
- row
199
- end
200
- end
201
-
202
- Here's a slightly more sophisticated one that handles both symbol and string
203
- keys. This is the PKFactory that comes with the MongoRecord code (an
204
- ActiveRecord-like framework for non-Rails apps) and the AR Mongo adapter code
205
- (for Rails):
206
-
207
- class PKFactory
208
- def create_pk(row)
209
- return row if row[:_id]
210
- row.delete(:_id) # in case it exists but the value is nil
211
- row['_id'] ||= Mongo::ObjectID.new
212
- row
213
- end
214
- end
215
-
216
- A database's PK factory object may be set either when a DB object is created
217
- or immediately after you obtain it, but only once. The only reason it is
218
- changeable at all is so that libraries such as MongoRecord that use this
219
- driver can set the PK factory after obtaining the database but before using it
220
- for the first time.
221
-
222
- ## The DB Class
223
-
224
- ### Strict mode
225
-
226
- Each database has an optional strict mode. If strict mode is on, then asking
227
- for a collection that does not exist will raise an error, as will asking to
228
- create a collection that already exists. Note that both these operations are
229
- completely harmless; strict mode is a programmer convenience only.
230
-
231
- To turn on strict mode, either pass in :strict => true when obtaining a DB
232
- object or call the `:strict=` method:
233
-
234
- db = Connection.new.db('dbname', :strict => true)
235
- # I'm feeling lax
236
- db.strict = false
237
- # No, I'm not!
238
- db.strict = true
239
-
240
- The method DB#strict? returns the current value of that flag.
241
-
242
- ## Cursors
243
-
244
- Notes:
245
-
246
- * Cursors are enumerable (and have a #to_a method).
247
-
248
- * The query doesn't get run until you actually attempt to retrieve data from a
249
- cursor.
250
-
251
- * Cursors will timeout on the server after 10 minutes. If you need to keep a cursor
252
- open for more than 10 minutes, specify `:timeout => false` when you create the cursor.
253
-
254
- ## Socket timeouts
255
-
256
- The Ruby driver support timeouts on socket read operations. To enable them, set the
257
- `:op_timeout` option when you create a `Mongo::Connection` object.
258
-
259
- If implementing higher-level timeouts, using tools like `Rack::Timeout`, it's very important
260
- to call `Mongo::Connection#close` to prevent the subsequent operation from receiving the previous
261
- request.
262
-
263
- # Testing
264
-
265
- If you have the source code, you can run the tests.
266
-
267
- $ rake test:c
268
-
269
- If you want to test the basic Ruby encoder, without the C extension, or if you're running JRuby:
270
-
271
- $ rake test:ruby
272
-
273
- These will run both unit and functional tests. To run these tests alone:
274
-
275
- $ rake test:unit
276
- $ rake test:functional
277
-
278
- To run any individual rake tasks with the C extension enabled, just pass C_EXT=true to the task:
279
-
280
- $ rake test:unit C_EXT=true
281
-
282
- If you want to test replica set, you can run the following tests
283
- individually:
284
-
285
- $ rake test:replica_set_count
286
- $ rake test:replica_set_insert
287
- $ rake test:replica_set_query
288
-
289
- ### Shoulda and Mocha
290
-
291
- Running the test suite requires shoulda and mocha. You can install them as follows:
292
-
293
- $ gem install shoulda
294
- $ gem install mocha
295
-
296
- The tests assume that the Mongo database is running on the default port. You
297
- can override the default host (localhost) and port (Connection::DEFAULT_PORT) by
298
- using the environment variables MONGO_RUBY_DRIVER_HOST and
299
- MONGO_RUBY_DRIVER_PORT.
300
-
301
- # Documentation
302
-
303
- This documentation is available online at [http://api.mongodb.org/ruby](http://api.mongodb.org/ruby). You can
304
- generate the documentation if you have the source by typing
305
-
306
- $ rake ydoc
307
-
308
- Then open the file +ydoc/index.html+.
309
-
310
- # Release Notes
311
-
312
- See HISTORY.
313
-
314
- # Credits
315
-
316
- See CREDITS.
317
-
318
- # License
319
-
320
- Copyright 2008-2010 10gen Inc.
162
+ Copyright (C) 2009-2013 MongoDB, Inc.
321
163
 
322
164
  Licensed under the Apache License, Version 2.0 (the "License");
323
165
  you may not use this file except in compliance with the License.
324
166
  You may obtain a copy of the License at
325
167
 
326
- http://www.apache.org/licenses/LICENSE-2.0
168
+ http://www.apache.org/licenses/LICENSE-2.0
327
169
 
328
170
  Unless required by applicable law or agreed to in writing, software
329
171
  distributed under the License is distributed on an "AS IS" BASIS,
330
172
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
331
173
  See the License for the specific language governing permissions and
332
174
  limitations under the License.
175
+
176
+ [rubygems-img]: https://badge.fury.io/rb/mongo.png
177
+ [rubygems-url]: http://badge.fury.io/rb/mongo
178
+ [travis-img]: https://secure.travis-ci.org/mongodb/mongo-ruby-driver.png?branch=1.x-stable
179
+ [travis-url]: http://travis-ci.org/mongodb/mongo-ruby-driver?branch=1.x-stable
180
+ [codeclimate-img]: https://codeclimate.com/github/mongodb/mongo-ruby-driver.png?branch=1.x-stable
181
+ [codeclimate-url]: https://codeclimate.com/github/mongodb/mongo-ruby-driver?branch=1.x-stable
182
+ [coveralls-img]: https://coveralls.io/repos/mongodb/mongo-ruby-driver/badge.png?branch=1.x-stable
183
+ [coveralls-url]: https://coveralls.io/r/mongodb/mongo-ruby-driver?branch=1.x-stable