jonbell-mongo 1.3.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE.txt +190 -0
- data/README.md +333 -0
- data/Rakefile +215 -0
- data/bin/mongo_console +21 -0
- data/docs/CREDITS.md +123 -0
- data/docs/FAQ.md +116 -0
- data/docs/GridFS.md +158 -0
- data/docs/HISTORY.md +263 -0
- data/docs/RELEASES.md +33 -0
- data/docs/REPLICA_SETS.md +72 -0
- data/docs/TUTORIAL.md +247 -0
- data/docs/WRITE_CONCERN.md +28 -0
- data/lib/mongo.rb +97 -0
- data/lib/mongo/collection.rb +895 -0
- data/lib/mongo/connection.rb +926 -0
- data/lib/mongo/cursor.rb +474 -0
- data/lib/mongo/db.rb +617 -0
- data/lib/mongo/exceptions.rb +71 -0
- data/lib/mongo/gridfs/grid.rb +107 -0
- data/lib/mongo/gridfs/grid_ext.rb +57 -0
- data/lib/mongo/gridfs/grid_file_system.rb +146 -0
- data/lib/mongo/gridfs/grid_io.rb +485 -0
- data/lib/mongo/gridfs/grid_io_fix.rb +38 -0
- data/lib/mongo/repl_set_connection.rb +356 -0
- data/lib/mongo/util/conversions.rb +89 -0
- data/lib/mongo/util/core_ext.rb +60 -0
- data/lib/mongo/util/pool.rb +177 -0
- data/lib/mongo/util/server_version.rb +71 -0
- data/lib/mongo/util/support.rb +82 -0
- data/lib/mongo/util/uri_parser.rb +185 -0
- data/mongo.gemspec +34 -0
- data/test/auxillary/1.4_features.rb +166 -0
- data/test/auxillary/authentication_test.rb +68 -0
- data/test/auxillary/autoreconnect_test.rb +41 -0
- data/test/auxillary/fork_test.rb +30 -0
- data/test/auxillary/repl_set_auth_test.rb +58 -0
- data/test/auxillary/slave_connection_test.rb +36 -0
- data/test/auxillary/threaded_authentication_test.rb +101 -0
- data/test/bson/binary_test.rb +15 -0
- data/test/bson/bson_test.rb +654 -0
- data/test/bson/byte_buffer_test.rb +208 -0
- data/test/bson/hash_with_indifferent_access_test.rb +38 -0
- data/test/bson/json_test.rb +17 -0
- data/test/bson/object_id_test.rb +154 -0
- data/test/bson/ordered_hash_test.rb +210 -0
- data/test/bson/timestamp_test.rb +24 -0
- data/test/collection_test.rb +910 -0
- data/test/connection_test.rb +324 -0
- data/test/conversions_test.rb +119 -0
- data/test/cursor_fail_test.rb +75 -0
- data/test/cursor_message_test.rb +43 -0
- data/test/cursor_test.rb +483 -0
- data/test/db_api_test.rb +738 -0
- data/test/db_connection_test.rb +15 -0
- data/test/db_test.rb +315 -0
- data/test/grid_file_system_test.rb +259 -0
- data/test/grid_io_test.rb +209 -0
- data/test/grid_test.rb +258 -0
- data/test/load/thin/load.rb +24 -0
- data/test/load/unicorn/load.rb +23 -0
- data/test/replica_sets/connect_test.rb +112 -0
- data/test/replica_sets/connection_string_test.rb +32 -0
- data/test/replica_sets/count_test.rb +35 -0
- data/test/replica_sets/insert_test.rb +53 -0
- data/test/replica_sets/pooled_insert_test.rb +55 -0
- data/test/replica_sets/query_secondaries.rb +108 -0
- data/test/replica_sets/query_test.rb +51 -0
- data/test/replica_sets/replication_ack_test.rb +66 -0
- data/test/replica_sets/rs_test_helper.rb +27 -0
- data/test/safe_test.rb +68 -0
- data/test/support/hash_with_indifferent_access.rb +186 -0
- data/test/support/keys.rb +45 -0
- data/test/support_test.rb +18 -0
- data/test/test_helper.rb +102 -0
- data/test/threading/threading_with_large_pool_test.rb +90 -0
- data/test/threading_test.rb +87 -0
- data/test/tools/auth_repl_set_manager.rb +14 -0
- data/test/tools/repl_set_manager.rb +266 -0
- data/test/unit/collection_test.rb +130 -0
- data/test/unit/connection_test.rb +85 -0
- data/test/unit/cursor_test.rb +109 -0
- data/test/unit/db_test.rb +94 -0
- data/test/unit/grid_test.rb +49 -0
- data/test/unit/pool_test.rb +9 -0
- data/test/unit/repl_set_connection_test.rb +59 -0
- data/test/unit/safe_test.rb +125 -0
- data/test/uri_test.rb +91 -0
- metadata +224 -0
data/LICENSE.txt
ADDED
@@ -0,0 +1,190 @@
|
|
1
|
+
Apache License
|
2
|
+
Version 2.0, January 2004
|
3
|
+
http://www.apache.org/licenses/
|
4
|
+
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
6
|
+
|
7
|
+
1. Definitions.
|
8
|
+
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
11
|
+
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
13
|
+
the copyright owner that is granting the License.
|
14
|
+
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
16
|
+
other entities that control, are controlled by, or are under common
|
17
|
+
control with that entity. For the purposes of this definition,
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
19
|
+
direction or management of such entity, whether by contract or
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
22
|
+
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
24
|
+
exercising permissions granted by this License.
|
25
|
+
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
27
|
+
including but not limited to software source code, documentation
|
28
|
+
source, and configuration files.
|
29
|
+
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
31
|
+
transformation or translation of a Source form, including but
|
32
|
+
not limited to compiled object code, generated documentation,
|
33
|
+
and conversions to other media types.
|
34
|
+
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
36
|
+
Object form, made available under the License, as indicated by a
|
37
|
+
copyright notice that is included in or attached to the work
|
38
|
+
(an example is provided in the Appendix below).
|
39
|
+
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
46
|
+
the Work and Derivative Works thereof.
|
47
|
+
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
49
|
+
the original version of the Work and any modifications or additions
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
61
|
+
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
64
|
+
subsequently incorporated within the Work.
|
65
|
+
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
72
|
+
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
78
|
+
where such license applies only to those patent claims licensable
|
79
|
+
by such Contributor that are necessarily infringed by their
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
82
|
+
institute patent litigation against any entity (including a
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
85
|
+
or contributory patent infringement, then any patent licenses
|
86
|
+
granted to You under this License for that Work shall terminate
|
87
|
+
as of the date such litigation is filed.
|
88
|
+
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
91
|
+
modifications, and in Source or Object form, provided that You
|
92
|
+
meet the following conditions:
|
93
|
+
|
94
|
+
(a) You must give any other recipients of the Work or
|
95
|
+
Derivative Works a copy of this License; and
|
96
|
+
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
98
|
+
stating that You changed the files; and
|
99
|
+
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
102
|
+
attribution notices from the Source form of the Work,
|
103
|
+
excluding those notices that do not pertain to any part of
|
104
|
+
the Derivative Works; and
|
105
|
+
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
108
|
+
include a readable copy of the attribution notices contained
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
111
|
+
of the following places: within a NOTICE text file distributed
|
112
|
+
as part of the Derivative Works; within the Source form or
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
114
|
+
within a display generated by the Derivative Works, if and
|
115
|
+
wherever such third-party notices normally appear. The contents
|
116
|
+
of the NOTICE file are for informational purposes only and
|
117
|
+
do not modify the License. You may add Your own attribution
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
120
|
+
that such additional attribution notices cannot be construed
|
121
|
+
as modifying the License.
|
122
|
+
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
124
|
+
may provide additional or different license terms and conditions
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
128
|
+
the conditions stated in this License.
|
129
|
+
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
133
|
+
this License, without any additional terms or conditions.
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
135
|
+
the terms of any separate license agreement you may have executed
|
136
|
+
with Licensor regarding such Contributions.
|
137
|
+
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
140
|
+
except as required for reasonable and customary use in describing the
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
142
|
+
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
152
|
+
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
158
|
+
incidental, or consequential damages of any character arising as a
|
159
|
+
result of this License or out of the use or inability to use the
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
162
|
+
other commercial damages or losses), even if such Contributor
|
163
|
+
has been advised of the possibility of such damages.
|
164
|
+
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
168
|
+
or other liability obligations and/or rights consistent with this
|
169
|
+
License. However, in accepting such obligations, You may act only
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
174
|
+
of your accepting any such warranty or additional liability.
|
175
|
+
|
176
|
+
END OF TERMS AND CONDITIONS
|
177
|
+
|
178
|
+
Copyright 2008-2010 10gen, Inc.
|
179
|
+
|
180
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
181
|
+
you may not use this file except in compliance with the License.
|
182
|
+
You may obtain a copy of the License at
|
183
|
+
|
184
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
185
|
+
|
186
|
+
Unless required by applicable law or agreed to in writing, software
|
187
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
188
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
189
|
+
See the License for the specific language governing permissions and
|
190
|
+
limitations under the License.
|
data/README.md
ADDED
@@ -0,0 +1,333 @@
|
|
1
|
+
# Introduction
|
2
|
+
|
3
|
+
This is the 10gen-supported Ruby driver for [MongoDB](http://www.mongodb.org).
|
4
|
+
|
5
|
+
This documentation includes other articles of interest, include:
|
6
|
+
|
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).
|
15
|
+
|
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:
|
18
|
+
|
19
|
+
require 'rubygems'
|
20
|
+
require 'mongo'
|
21
|
+
|
22
|
+
@conn = Mongo::Connection.new
|
23
|
+
@db = @conn['sample-db']
|
24
|
+
@coll = @db['test']
|
25
|
+
|
26
|
+
@coll.remove
|
27
|
+
3.times do |i|
|
28
|
+
@coll.insert({'a' => i+1})
|
29
|
+
end
|
30
|
+
|
31
|
+
puts "There are #{@coll.count} records. Here they are:"
|
32
|
+
@coll.find.each { |doc| puts doc.inspect }
|
33
|
+
|
34
|
+
# Installation
|
35
|
+
|
36
|
+
### Ruby Versions
|
37
|
+
|
38
|
+
The driver works and is consistently tested on Ruby 1.8.6, 1.8.7, and 1.9.2, and JRuby 1.5.1.
|
39
|
+
|
40
|
+
Note that if you're on 1.8.7, be sure that you're using a patchlevel >= 249. There
|
41
|
+
are some IO bugs in earlier versions.
|
42
|
+
|
43
|
+
### Gems
|
44
|
+
|
45
|
+
The driver's gems are hosted at [Rubygems.org](http://rubygems.org). Make sure you're
|
46
|
+
using the latest version of rubygems:
|
47
|
+
|
48
|
+
$ gem update --system
|
49
|
+
|
50
|
+
Then you can install the mongo gem as follows:
|
51
|
+
|
52
|
+
$ gem install mongo
|
53
|
+
|
54
|
+
The driver also requires the bson gem:
|
55
|
+
|
56
|
+
$ gem install bson
|
57
|
+
|
58
|
+
And for a significant performance boost, you'll want to install the C extensions:
|
59
|
+
|
60
|
+
$ gem install bson_ext
|
61
|
+
|
62
|
+
Note that bson_ext isn't used with JRuby. Instead, some native Java extensions are bundled with the bson gem.
|
63
|
+
If you ever need to modify these extenions, you can recompile with the following rake task:
|
64
|
+
|
65
|
+
$ rake build:java
|
66
|
+
|
67
|
+
### From the GitHub source
|
68
|
+
|
69
|
+
The source code is available at http://github.com/mongodb/mongo-ruby-driver.
|
70
|
+
You can either clone the git repository or download a tarball or zip file.
|
71
|
+
Once you have the source, you can use it from wherever you downloaded it or
|
72
|
+
you can install it as a gem from the source by typing
|
73
|
+
|
74
|
+
$ rake gem:install
|
75
|
+
|
76
|
+
To install the C extensions from source, type this instead:
|
77
|
+
|
78
|
+
$ rake gem:install_extensions
|
79
|
+
|
80
|
+
That's all there is to it!
|
81
|
+
|
82
|
+
# Examples
|
83
|
+
|
84
|
+
For extensive examples, see the [MongoDB Ruby Tutorial](http://www.mongodb.org/display/DOCS/Ruby+Tutorial).
|
85
|
+
|
86
|
+
Bundled with the driver are many examples, located in the "docs/examples" subdirectory. Samples include using
|
87
|
+
the driver and using the GridFS class GridStore. MongoDB must be running for
|
88
|
+
these examples to work, of course.
|
89
|
+
|
90
|
+
Here's how to start MongoDB and run the "simple.rb" example:
|
91
|
+
|
92
|
+
$ cd path/to/mongo
|
93
|
+
$ ./mongod run
|
94
|
+
... then in another window ...
|
95
|
+
$ cd path/to/mongo-ruby-driver
|
96
|
+
$ ruby docs/examples/simple.rb
|
97
|
+
|
98
|
+
See also the test code, especially test/test_db_api.rb.
|
99
|
+
|
100
|
+
# GridFS
|
101
|
+
|
102
|
+
The Ruby driver include two abstractions for storing large files: Grid and GridFileSystem.
|
103
|
+
The Grid class is a Ruby implementation of MongoDB's GridFS file storage
|
104
|
+
specification. GridFileSystem is essentailly the same, but provides a more filesystem-like API
|
105
|
+
and assumes that filenames are unique.
|
106
|
+
|
107
|
+
An instance of both classes represents an individual file store. See the API reference
|
108
|
+
for details, and see examples/gridfs.rb for code that uses many of the Grid
|
109
|
+
features (metadata, content type, seek, tell, etc).
|
110
|
+
|
111
|
+
Examples:
|
112
|
+
# Write a file on disk to the Grid
|
113
|
+
file = File.open('image.jpg')
|
114
|
+
grid = Grid.new(db)
|
115
|
+
id = grid.put(file)
|
116
|
+
|
117
|
+
# Retrieve the file
|
118
|
+
file = grid.get(id)
|
119
|
+
file.read
|
120
|
+
|
121
|
+
# Get all the file's metata
|
122
|
+
file.filename
|
123
|
+
file.content_type
|
124
|
+
file.metadata
|
125
|
+
|
126
|
+
# Notes
|
127
|
+
|
128
|
+
## Thread Safety
|
129
|
+
|
130
|
+
The driver is thread-safe.
|
131
|
+
|
132
|
+
## Connection Pooling
|
133
|
+
|
134
|
+
The driver implements connection pooling. By default, only one
|
135
|
+
socket connection will be opened to MongoDB. However, if you're running a
|
136
|
+
multi-threaded application, you can specify a maximum pool size and a maximum
|
137
|
+
timeout for waiting for old connections to be released to the pool.
|
138
|
+
|
139
|
+
To set up a pooled connection to a single MongoDB instance:
|
140
|
+
|
141
|
+
@conn = Connection.new("localhost", 27017, :pool_size => 5, :timeout => 5)
|
142
|
+
|
143
|
+
Though the pooling architecture will undoubtedly evolve, it currently owes much credit
|
144
|
+
to the connection pooling implementations in ActiveRecord and PyMongo.
|
145
|
+
|
146
|
+
## Forking
|
147
|
+
|
148
|
+
Certain Ruby application servers work by forking, and it has long been necessary to
|
149
|
+
re-establish the child process's connection to the database after fork. But with the release
|
150
|
+
of v1.3.0, the Ruby driver detects forking and reconnects automatically.
|
151
|
+
|
152
|
+
## String Encoding
|
153
|
+
|
154
|
+
The BSON ("Binary JSON") format used to communicate with Mongo requires that
|
155
|
+
strings be UTF-8 (http://en.wikipedia.org/wiki/UTF-8).
|
156
|
+
|
157
|
+
Ruby 1.9 has built-in character encoding support. All strings sent to Mongo
|
158
|
+
and received from Mongo are converted to UTF-8 when necessary, and strings
|
159
|
+
read from Mongo will have their character encodings set to UTF-8.
|
160
|
+
|
161
|
+
When used with Ruby 1.8, the bytes in each string are written to and read from
|
162
|
+
Mongo as is. If the string is ASCII, all is well, because ASCII is a subset of
|
163
|
+
UTF-8. If the string is not ASCII, it may not be a well-formed UTF-8
|
164
|
+
string.
|
165
|
+
|
166
|
+
## Primary Keys
|
167
|
+
|
168
|
+
The `_id` field is a primary key. It is treated specially by the database, and
|
169
|
+
its use makes many operations more efficient. The value of an _id may be of
|
170
|
+
any type. The database itself inserts an _id value if none is specified when
|
171
|
+
a record is inserted.
|
172
|
+
|
173
|
+
### Primary Key Factories
|
174
|
+
|
175
|
+
A primary key factory is a class you supply to a DB object that knows how to
|
176
|
+
generate _id values. If you want to control _id values or even their types,
|
177
|
+
using a PK factory lets you do so.
|
178
|
+
|
179
|
+
You can tell the Ruby Mongo driver how to create primary keys by passing in
|
180
|
+
the :pk option to the Connection#db method.
|
181
|
+
|
182
|
+
db = Mongo::Connection.new.db('dbname', :pk => MyPKFactory.new)
|
183
|
+
|
184
|
+
A primary key factory object must respond to :create_pk, which should
|
185
|
+
take a hash and return a hash which merges the original hash with any
|
186
|
+
primary key fields the factory wishes to inject.
|
187
|
+
|
188
|
+
NOTE: if the object already has a primary key, the factory should not
|
189
|
+
inject a new key; this means that the object may already exist in the
|
190
|
+
database. The idea here is that whenever a record is inserted, the
|
191
|
+
:pk object's +create_pk+ method will be called and the new hash
|
192
|
+
returned will be inserted.
|
193
|
+
|
194
|
+
Here is a sample primary key factory, taken from the tests:
|
195
|
+
|
196
|
+
class TestPKFactory
|
197
|
+
def create_pk(row)
|
198
|
+
row['_id'] ||= Mongo::ObjectID.new
|
199
|
+
row
|
200
|
+
end
|
201
|
+
end
|
202
|
+
|
203
|
+
Here's a slightly more sophisticated one that handles both symbol and string
|
204
|
+
keys. This is the PKFactory that comes with the MongoRecord code (an
|
205
|
+
ActiveRecord-like framework for non-Rails apps) and the AR Mongo adapter code
|
206
|
+
(for Rails):
|
207
|
+
|
208
|
+
class PKFactory
|
209
|
+
def create_pk(row)
|
210
|
+
return row if row[:_id]
|
211
|
+
row.delete(:_id) # in case it exists but the value is nil
|
212
|
+
row['_id'] ||= Mongo::ObjectID.new
|
213
|
+
row
|
214
|
+
end
|
215
|
+
end
|
216
|
+
|
217
|
+
A database's PK factory object may be set either when a DB object is created
|
218
|
+
or immediately after you obtain it, but only once. The only reason it is
|
219
|
+
changeable at all is so that libraries such as MongoRecord that use this
|
220
|
+
driver can set the PK factory after obtaining the database but before using it
|
221
|
+
for the first time.
|
222
|
+
|
223
|
+
## The DB Class
|
224
|
+
|
225
|
+
### Strict mode
|
226
|
+
|
227
|
+
Each database has an optional strict mode. If strict mode is on, then asking
|
228
|
+
for a collection that does not exist will raise an error, as will asking to
|
229
|
+
create a collection that already exists. Note that both these operations are
|
230
|
+
completely harmless; strict mode is a programmer convenience only.
|
231
|
+
|
232
|
+
To turn on strict mode, either pass in :strict => true when obtaining a DB
|
233
|
+
object or call the `:strict=` method:
|
234
|
+
|
235
|
+
db = Connection.new.db('dbname', :strict => true)
|
236
|
+
# I'm feeling lax
|
237
|
+
db.strict = false
|
238
|
+
# No, I'm not!
|
239
|
+
db.strict = true
|
240
|
+
|
241
|
+
The method DB#strict? returns the current value of that flag.
|
242
|
+
|
243
|
+
## Cursors
|
244
|
+
|
245
|
+
Notes:
|
246
|
+
|
247
|
+
* Cursors are enumerable (and have a #to_a method).
|
248
|
+
|
249
|
+
* The query doesn't get run until you actually attempt to retrieve data from a
|
250
|
+
cursor.
|
251
|
+
|
252
|
+
* Cursors will timeout on the server after 10 minutes. If you need to keep a cursor
|
253
|
+
open for more than 10 minutes, specify `:timeout => false` when you create the cursor.
|
254
|
+
|
255
|
+
## Socket timeouts
|
256
|
+
|
257
|
+
The Ruby driver support timeouts on socket read operations. To enable them, set the
|
258
|
+
`:op_timeout` option when you create a `Mongo::Connection` object.
|
259
|
+
|
260
|
+
If implementing higher-level timeouts, using tools like `Rack::Timeout`, it's very important
|
261
|
+
to call `Mongo::Connection#close` to prevent the subsequent operation from receiving the previous
|
262
|
+
request.
|
263
|
+
|
264
|
+
# Testing
|
265
|
+
|
266
|
+
If you have the source code, you can run the tests.
|
267
|
+
|
268
|
+
$ rake test:c
|
269
|
+
|
270
|
+
If you want to test the basic Ruby encoder, without the C extension, or if you're running JRuby:
|
271
|
+
|
272
|
+
$ rake test:ruby
|
273
|
+
|
274
|
+
These will run both unit and functional tests. To run these tests alone:
|
275
|
+
|
276
|
+
$ rake test:unit
|
277
|
+
$ rake test:functional
|
278
|
+
|
279
|
+
To run any individual rake tasks with the C extension enabled, just pass C_EXT=true to the task:
|
280
|
+
|
281
|
+
$ rake test:unit C_EXT=true
|
282
|
+
|
283
|
+
If you want to test replica set, you can run the following tests
|
284
|
+
individually:
|
285
|
+
|
286
|
+
$ rake test:replica_set_count
|
287
|
+
$ rake test:replica_set_insert
|
288
|
+
$ rake test:replica_set_query
|
289
|
+
|
290
|
+
### Shoulda and Mocha
|
291
|
+
|
292
|
+
Running the test suite requires shoulda and mocha. You can install them as follows:
|
293
|
+
|
294
|
+
$ gem install shoulda
|
295
|
+
$ gem install mocha
|
296
|
+
|
297
|
+
The tests assume that the Mongo database is running on the default port. You
|
298
|
+
can override the default host (localhost) and port (Connection::DEFAULT_PORT) by
|
299
|
+
using the environment variables MONGO_RUBY_DRIVER_HOST and
|
300
|
+
MONGO_RUBY_DRIVER_PORT.
|
301
|
+
|
302
|
+
# Documentation
|
303
|
+
|
304
|
+
This documentation is available online at [http://api.mongodb.org/ruby](http://api.mongodb.org/ruby). You can
|
305
|
+
generate the documentation if you have the source by typing
|
306
|
+
|
307
|
+
$ rake ydoc
|
308
|
+
|
309
|
+
Then open the file +ydoc/index.html+.
|
310
|
+
|
311
|
+
# Release Notes
|
312
|
+
|
313
|
+
See HISTORY.
|
314
|
+
|
315
|
+
# Credits
|
316
|
+
|
317
|
+
See CREDITS.
|
318
|
+
|
319
|
+
# License
|
320
|
+
|
321
|
+
Copyright 2008-2010 10gen Inc.
|
322
|
+
|
323
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
324
|
+
you may not use this file except in compliance with the License.
|
325
|
+
You may obtain a copy of the License at
|
326
|
+
|
327
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
328
|
+
|
329
|
+
Unless required by applicable law or agreed to in writing, software
|
330
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
331
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
332
|
+
See the License for the specific language governing permissions and
|
333
|
+
limitations under the License.
|