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
@@ -0,0 +1,409 @@
1
+ # Copyright (C) 2009-2013 MongoDB, Inc.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ require 'cgi'
16
+ require 'uri'
17
+
18
+ module Mongo
19
+ class URIParser
20
+
21
+ AUTH_REGEX = /((.+)@)?/
22
+
23
+ HOST_REGEX = /([-.\w]+)|(\[[^\]]+\])/
24
+ PORT_REGEX = /(?::(\w+))?/
25
+ UNIX_SOCK_REGEX = /([\S]+.sock)/
26
+ NODE_REGEX = /((#{HOST_REGEX}#{PORT_REGEX},?)+|#{UNIX_SOCK_REGEX}{1})/
27
+
28
+ PATH_REGEX = /(?:\/([-\w]+))?/
29
+
30
+ MONGODB_URI_MATCHER = /#{AUTH_REGEX}#{NODE_REGEX}#{PATH_REGEX}/
31
+ MONGODB_URI_SPEC = "mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]"
32
+
33
+ SPEC_ATTRS = [:nodes, :auths]
34
+
35
+ READ_PREFERENCES = {
36
+ 'primary' => :primary,
37
+ 'primarypreferred' => :primary_preferred,
38
+ 'secondary' => :secondary,
39
+ 'secondarypreferred' => :secondary_preferred,
40
+ 'nearest' => :nearest
41
+ }
42
+
43
+ OPT_ATTRS = [
44
+ :authmechanism,
45
+ :authmechanismproperties,
46
+ :authsource,
47
+ :connect,
48
+ :connecttimeoutms,
49
+ :fsync,
50
+ :journal,
51
+ :pool_size,
52
+ :readpreference,
53
+ :readpreferencetags,
54
+ :replicaset,
55
+ :safe,
56
+ :slaveok,
57
+ :sockettimeoutms,
58
+ :ssl,
59
+ :w,
60
+ :wtimeout,
61
+ :wtimeoutms
62
+ ]
63
+
64
+ OPT_VALID = {
65
+ :authmechanism => lambda { |arg| Mongo::Authentication.validate_mechanism(arg) },
66
+ :authmechanismproperties => lambda { |arg| arg.length > 0 },
67
+ :authsource => lambda { |arg| arg.length > 0 },
68
+ :connect => lambda { |arg| [ 'direct', 'replicaset', 'true', 'false', true, false ].include?(arg) },
69
+ :connecttimeoutms => lambda { |arg| arg =~ /^\d+$/ },
70
+ :fsync => lambda { |arg| ['true', 'false'].include?(arg) },
71
+ :journal => lambda { |arg| ['true', 'false'].include?(arg) },
72
+ :pool_size => lambda { |arg| arg.to_i > 0 },
73
+ :readpreference => lambda { |arg| READ_PREFERENCES.keys.include?(arg) },
74
+ :readpreferencetags => lambda { |arg| arg.none? { |tags| tags.scan(/(\w+:\w+),?/).empty? } },
75
+ :replicaset => lambda { |arg| arg.length > 0 },
76
+ :safe => lambda { |arg| ['true', 'false'].include?(arg) },
77
+ :slaveok => lambda { |arg| ['true', 'false'].include?(arg) },
78
+ :sockettimeoutms => lambda { |arg| arg =~ /^\d+$/ },
79
+ :ssl => lambda { |arg| ['true', 'false'].include?(arg) },
80
+ :w => lambda { |arg| arg =~ /^\w+$/ },
81
+ :wtimeout => lambda { |arg| arg =~ /^\d+$/ },
82
+ :wtimeoutms => lambda { |arg| arg =~ /^\d+$/ }
83
+ }
84
+
85
+ OPT_ERR = {
86
+ :authmechanism => Mongo::Authentication::MECHANISM_ERROR,
87
+ :authmechanismproperties => "must meet the format requirements of the authentication mechanism's properties",
88
+ :authsource => "must be a string containing the name of the database being used for authentication",
89
+ :connect => "must be 'direct', 'replicaset', 'true', or 'false'",
90
+ :connecttimeoutms => "must be an integer specifying milliseconds",
91
+ :fsync => "must be 'true' or 'false'",
92
+ :journal => "must be 'true' or 'false'",
93
+ :pool_size => "must be an integer greater than zero",
94
+ :readpreference => "must be one of #{READ_PREFERENCES.keys.map(&:inspect).join(",")}",
95
+ :readpreferencetags => "must be a comma-separated list of one or more key:value pairs",
96
+ :replicaset => "must be a string containing the name of the replica set to connect to",
97
+ :safe => "must be 'true' or 'false'",
98
+ :slaveok => "must be 'true' or 'false'",
99
+ :settimeoutms => "must be an integer specifying milliseconds",
100
+ :ssl => "must be 'true' or 'false'",
101
+ :w => "must be an integer indicating number of nodes to replicate to or a string " +
102
+ "specifying that replication is required to the majority or nodes with a " +
103
+ "particilar getLastErrorMode.",
104
+ :wtimeout => "must be an integer specifying milliseconds",
105
+ :wtimeoutms => "must be an integer specifying milliseconds"
106
+ }
107
+
108
+ OPT_CONV = {
109
+ :authmechanism => lambda { |arg| arg.upcase },
110
+ :authmechanismproperties => lambda { |arg| arg },
111
+ :authsource => lambda { |arg| arg },
112
+ :connect => lambda { |arg| arg == 'false' ? false : arg }, # convert 'false' to FalseClass
113
+ :connecttimeoutms => lambda { |arg| arg.to_f / 1000 }, # stored as seconds
114
+ :fsync => lambda { |arg| arg == 'true' ? true : false },
115
+ :journal => lambda { |arg| arg == 'true' ? true : false },
116
+ :pool_size => lambda { |arg| arg.to_i },
117
+ :readpreference => lambda { |arg| READ_PREFERENCES[arg] },
118
+ :readpreferencetags => lambda { |arg| arg.map do |tags|
119
+ tags.scan(/(\w+:\w+),?/).reduce({}) do |tags, pair|
120
+ key, value = pair.first.split(":")
121
+ tags.merge!(key => value)
122
+ end
123
+ end },
124
+ :replicaset => lambda { |arg| arg },
125
+ :safe => lambda { |arg| arg == 'true' ? true : false },
126
+ :slaveok => lambda { |arg| arg == 'true' ? true : false },
127
+ :sockettimeoutms => lambda { |arg| arg.to_f / 1000 }, # stored as seconds
128
+ :ssl => lambda { |arg| arg == 'true' ? true : false },
129
+ :w => lambda { |arg| Mongo::Support.is_i?(arg) ? arg.to_i : arg.to_sym },
130
+ :wtimeout => lambda { |arg| arg.to_i },
131
+ :wtimeoutms => lambda { |arg| arg.to_i }
132
+ }
133
+
134
+ OPT_CASE_SENSITIVE = [ :authsource,
135
+ :authmechanismproperties,
136
+ :replicaset,
137
+ :w
138
+ ]
139
+
140
+ OPT_NOT_STRING = [ :readpreferencetags ]
141
+
142
+ attr_reader :auths,
143
+ :authmechanism,
144
+ :authmechanismproperties,
145
+ :authsource,
146
+ :connect,
147
+ :connecttimeoutms,
148
+ :db_name,
149
+ :fsync,
150
+ :journal,
151
+ :nodes,
152
+ :pool_size,
153
+ :readpreference,
154
+ :readpreferencetags,
155
+ :replicaset,
156
+ :safe,
157
+ :slaveok,
158
+ :sockettimeoutms,
159
+ :ssl,
160
+ :w,
161
+ :wtimeout,
162
+ :wtimeoutms
163
+
164
+ # Parse a MongoDB URI. This method is used by MongoClient.from_uri.
165
+ # Returns an array of nodes and an array of db authorizations, if applicable.
166
+ #
167
+ # @note Passwords can contain any character except for ','
168
+ #
169
+ # @param [String] uri The MongoDB URI string.
170
+ def initialize(uri)
171
+ if uri.start_with?('mongodb://')
172
+ uri = uri[10..-1]
173
+ else
174
+ raise MongoArgumentError, "MongoDB URI must match this spec: #{MONGODB_URI_SPEC}"
175
+ end
176
+
177
+ hosts, opts = uri.split('?')
178
+ parse_options(opts)
179
+ parse_hosts(hosts)
180
+ validate_connect
181
+ end
182
+
183
+ # Create a Mongo::MongoClient or a Mongo::MongoReplicaSetClient based on the URI.
184
+ #
185
+ # @note Don't confuse this with attribute getter method #connect.
186
+ #
187
+ # @return [MongoClient,MongoReplicaSetClient]
188
+ def connection(extra_opts={}, legacy = false, sharded = false)
189
+ opts = connection_options.merge!(extra_opts)
190
+ if(legacy)
191
+ if replicaset?
192
+ ReplSetConnection.new(node_strings, opts)
193
+ else
194
+ Connection.new(host, port, opts)
195
+ end
196
+ else
197
+ if sharded
198
+ MongoShardedClient.new(node_strings, opts)
199
+ elsif replicaset?
200
+ MongoReplicaSetClient.new(node_strings, opts)
201
+ else
202
+ MongoClient.new(host, port, opts)
203
+ end
204
+ end
205
+ end
206
+
207
+ # Whether this represents a replica set.
208
+ # @return [true,false]
209
+ def replicaset?
210
+ replicaset.is_a?(String) || nodes.length > 1
211
+ end
212
+
213
+ # Whether to immediately connect to the MongoDB node[s]. Defaults to true.
214
+ # @return [true, false]
215
+ def connect?
216
+ connect != false
217
+ end
218
+
219
+ # Whether this represents a direct connection.
220
+ #
221
+ # @note Specifying :connect => 'direct' has no effect... other than to raise an exception if other variables suggest a replicaset.
222
+ #
223
+ # @return [true,false]
224
+ def direct?
225
+ !replicaset?
226
+ end
227
+
228
+ # For direct connections, the host of the (only) node.
229
+ # @return [String]
230
+ def host
231
+ nodes[0][0]
232
+ end
233
+
234
+ # For direct connections, the port of the (only) node.
235
+ # @return [Integer]
236
+ def port
237
+ nodes[0][1].to_i
238
+ end
239
+
240
+ # Options that can be passed to MongoClient.new or MongoReplicaSetClient.new
241
+ # @return [Hash]
242
+ def connection_options
243
+ opts = {}
244
+
245
+ if @wtimeout
246
+ warn "Using wtimeout in a URI is deprecated, please use wtimeoutMS. It will be removed in v2.0."
247
+ opts[:wtimeout] = @wtimeout
248
+ end
249
+ opts[:wtimeout] = @wtimeoutms if @wtimeoutms
250
+
251
+ opts[:w] = 1 if @safe
252
+ opts[:w] = @w if @w
253
+ opts[:j] = @journal if @journal
254
+ opts[:fsync] = @fsync if @fsync
255
+
256
+ opts[:connect_timeout] = @connecttimeoutms if @connecttimeoutms
257
+ opts[:op_timeout] = @sockettimeoutms if @sockettimeoutms
258
+ opts[:pool_size] = @pool_size if @pool_size
259
+ opts[:read] = @readpreference if @readpreference
260
+ opts[:tag_sets] = @readpreferencetags if @readpreferencetags
261
+
262
+ if @slaveok && !@readpreference
263
+ unless replicaset?
264
+ opts[:slave_ok] = true
265
+ else
266
+ opts[:read] = :secondary_preferred
267
+ end
268
+ end
269
+
270
+ if replicaset.is_a?(String)
271
+ opts[:name] = replicaset
272
+ end
273
+
274
+ opts[:db_name] = @db_name if @db_name
275
+ opts[:auths] = @auths if @auths
276
+ opts[:ssl] = @ssl if @ssl
277
+ opts[:connect] = connect?
278
+
279
+ opts
280
+ end
281
+
282
+ def node_strings
283
+ nodes.map { |node| node.join(':') }
284
+ end
285
+
286
+ private
287
+
288
+ def parse_hosts(uri_without_protocol)
289
+ @nodes = []
290
+ @auths = Set.new
291
+
292
+ unless matches = MONGODB_URI_MATCHER.match(uri_without_protocol)
293
+ raise MongoArgumentError,
294
+ "MongoDB URI must match this spec: #{MONGODB_URI_SPEC}"
295
+ end
296
+
297
+ user_info = matches[2].split(':') if matches[2]
298
+ host_info = matches[3].split(',')
299
+ @db_name = matches[9]
300
+
301
+ if host_info.first.end_with?('.sock')
302
+ @nodes << [ host_info.first ]
303
+ else
304
+ host_info.each do |host|
305
+ if host[0,1] == '['
306
+ host, port = host.split(']:') << MongoClient::DEFAULT_PORT
307
+ host = host.end_with?(']') ? host[1...-1] : host[1..-1]
308
+ else
309
+ host, port = host.split(':') << MongoClient::DEFAULT_PORT
310
+ end
311
+ unless port.to_s =~ /^\d+$/
312
+ raise MongoArgumentError,
313
+ "Invalid port #{port}; port must be specified as digits."
314
+ end
315
+ @nodes << [host, port.to_i]
316
+ end
317
+ end
318
+
319
+ if @nodes.empty?
320
+ raise MongoArgumentError,
321
+ "No nodes specified. Please ensure that you've provided at " +
322
+ "least one node."
323
+ end
324
+
325
+ # no user info to parse, exit here
326
+ return unless user_info
327
+
328
+ # check for url encoding for username and password
329
+ username, password = user_info
330
+ if user_info.size > 2 ||
331
+ (username && username.include?('@')) ||
332
+ (password && password.include?('@'))
333
+
334
+ raise MongoArgumentError,
335
+ "The characters ':' and '@' in a username or password " +
336
+ "must be escaped (RFC 2396)."
337
+ end
338
+
339
+ # if username exists, proceed adding to auth set
340
+ unless username.nil? || username.empty?
341
+ auth = Authentication.validate_credentials({
342
+ :db_name => @db_name,
343
+ :username => URI.unescape(username),
344
+ :password => password ? URI.unescape(password) : nil,
345
+ :source => @authsource,
346
+ :mechanism => @authmechanism,
347
+ :extra => @authmechanismproperties || {}
348
+ })
349
+
350
+ @auths << auth
351
+ end
352
+ end
353
+
354
+ # This method uses the lambdas defined in OPT_VALID and OPT_CONV to validate
355
+ # and convert the given options.
356
+ def parse_options(string_opts)
357
+ # initialize instance variables for available options
358
+ OPT_VALID.keys.each { |k| instance_variable_set("@#{k}", nil) }
359
+
360
+ string_opts ||= ''
361
+
362
+ return if string_opts.empty?
363
+
364
+ if string_opts.include?(';') and string_opts.include?('&')
365
+ raise MongoArgumentError, 'must not mix URL separators ; and &'
366
+ end
367
+
368
+ opts = CGI.parse(string_opts).inject({}) do |memo, (key, value)|
369
+ key_sym = key.downcase.to_sym
370
+ if OPT_NOT_STRING.include?(key_sym)
371
+ memo[key_sym] = value
372
+ else
373
+ value = value.first
374
+ memo[key_sym] = OPT_CASE_SENSITIVE.include?(key_sym) ? value.strip : value.strip.downcase
375
+ end
376
+ memo
377
+ end
378
+
379
+ opts.each do |key, value|
380
+ if !OPT_ATTRS.include?(key)
381
+ raise MongoArgumentError, "Invalid Mongo URI option #{key}"
382
+ end
383
+ if OPT_VALID[key].call(value)
384
+ instance_variable_set("@#{key}", OPT_CONV[key].call(value))
385
+ else
386
+ raise MongoArgumentError, "Invalid value #{value.inspect} for #{key}: #{OPT_ERR[key]}"
387
+ end
388
+ end
389
+
390
+ if @authmechanismproperties
391
+ @authmechanismproperties = @authmechanismproperties.split(',').reduce({}) do |prop, pair|
392
+ key, value = pair.split(':')
393
+ if ["true", "false"].include?(value.downcase)
394
+ value = value.downcase == "true"
395
+ end
396
+ prop[key.downcase.to_sym] = value
397
+ prop
398
+ end
399
+ end
400
+ end
401
+
402
+ def validate_connect
403
+ if replicaset? and @connect == 'direct'
404
+ # Make sure the user doesn't specify something contradictory
405
+ raise MongoArgumentError, "connect=direct conflicts with setting a replicaset name"
406
+ end
407
+ end
408
+ end
409
+ end
@@ -0,0 +1,66 @@
1
+ # Copyright (C) 2009-2013 MongoDB, Inc.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ module Mongo
16
+ module WriteConcern
17
+ VALID_KEYS = [:w, :j, :fsync, :wtimeout]
18
+ DEFAULT_WRITE_CONCERN = {:w => 1}
19
+
20
+ attr_reader :legacy_write_concern
21
+
22
+ @@safe_warn = nil
23
+ def write_concern_from_legacy(opts)
24
+ # Warn if 'safe' parameter is being used,
25
+ if opts.key?(:safe) && !@@safe_warn && !ENV['TEST_MODE']
26
+ warn "[DEPRECATED] The 'safe' write concern option has been deprecated in favor of 'w'."
27
+ @@safe_warn = true
28
+ end
29
+
30
+ # nil: set :w => 0
31
+ # false: set :w => 0
32
+ # true: set :w => 1
33
+ # hash: set :w => 0 and merge with opts
34
+
35
+ unless opts.has_key?(:w)
36
+ opts[:w] = 0 # legacy default, unacknowledged
37
+ safe = opts.delete(:safe)
38
+ if(safe && safe.is_a?(Hash))
39
+ opts.merge!(safe)
40
+ elsif(safe == true)
41
+ opts[:w] = 1
42
+ end
43
+ end
44
+ end
45
+
46
+ # todo: throw exception for conflicting write concern options
47
+ def get_write_concern(opts, parent=nil)
48
+ write_concern_from_legacy(opts) if opts.key?(:safe) || legacy_write_concern
49
+ write_concern = DEFAULT_WRITE_CONCERN.dup
50
+ write_concern.merge!(parent.write_concern) if parent
51
+ write_concern.merge!(opts.reject {|k,v| !VALID_KEYS.include?(k)})
52
+ write_concern[:w] = write_concern[:w].to_s if write_concern[:w].is_a?(Symbol)
53
+ write_concern
54
+ end
55
+
56
+ def self.gle?(write_concern)
57
+ (write_concern[:w].is_a? Symbol) ||
58
+ (write_concern[:w].is_a? String) ||
59
+ write_concern[:w] > 0 ||
60
+ write_concern[:j] ||
61
+ write_concern[:fsync] ||
62
+ write_concern[:wtimeout]
63
+ end
64
+
65
+ end
66
+ end
@@ -0,0 +1,20 @@
1
+ # Copyright (C) 2009-2013 MongoDB, Inc.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ require 'mongo/functional/authentication'
16
+ require 'mongo/functional/logging'
17
+ require 'mongo/functional/read_preference'
18
+ require 'mongo/functional/write_concern'
19
+ require 'mongo/functional/uri_parser'
20
+ require 'mongo/functional/scram'
@@ -1,20 +1,16 @@
1
- # encoding: UTF-8
2
-
3
- # --
4
- # Copyright (C) 2008-2011 10gen Inc.
1
+ # Copyright (C) 2009-2013 MongoDB, Inc.
5
2
  #
6
3
  # Licensed under the Apache License, Version 2.0 (the "License");
7
4
  # you may not use this file except in compliance with the License.
8
5
  # You may obtain a copy of the License at
9
6
  #
10
- # http://www.apache.org/licenses/LICENSE-2.0
7
+ # http://www.apache.org/licenses/LICENSE-2.0
11
8
  #
12
9
  # Unless required by applicable law or agreed to in writing, software
13
10
  # distributed under the License is distributed on an "AS IS" BASIS,
14
11
  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
12
  # See the License for the specific language governing permissions and
16
13
  # limitations under the License.
17
- # ++
18
14
 
19
15
  module Mongo
20
16
 
@@ -27,8 +23,6 @@ module Mongo
27
23
  # Initialize a new Grid instance, consisting of a MongoDB database
28
24
  # and a filesystem prefix if not using the default.
29
25
  #
30
- # @core gridfs
31
- #
32
26
  # @see GridFileSystem
33
27
  def initialize(db, fs_name=DEFAULT_FS_NAME)
34
28
  raise MongoArgumentError, "db must be a Mongo::DB." unless db.is_a?(Mongo::DB)
@@ -38,16 +32,17 @@ module Mongo
38
32
  @chunks = @db["#{fs_name}.chunks"]
39
33
  @fs_name = fs_name
40
34
 
41
- # Ensure indexes only if not connected to slave.
42
- unless db.connection.slave_ok?
43
- @chunks.create_index([['files_id', Mongo::ASCENDING], ['n', Mongo::ASCENDING]], :unique => true)
35
+ # This will create indexes only if we're connected to a primary node.
36
+ begin
37
+ @chunks.ensure_index([['files_id', Mongo::ASCENDING], ['n', Mongo::ASCENDING]], :unique => true)
38
+ rescue Mongo::ConnectionFailure
44
39
  end
45
40
  end
46
41
 
47
42
  # Store a file in the file store. This method is designed only for writing new files;
48
43
  # if you need to update a given file, first delete it using Grid#delete.
49
44
  #
50
- # Note that arbitary metadata attributes can be saved to the file by passing
45
+ # Note that arbitrary metadata attributes can be saved to the file by passing
51
46
  # them in as options.
52
47
  #
53
48
  # @param [String, #read] data a string or io-like object to store.
@@ -59,23 +54,34 @@ module Mongo
59
54
  # @option opts [String] :content_type ('binary/octet-stream') If no content type is specified,
60
55
  # the content type will may be inferred from the filename extension if the mime-types gem can be
61
56
  # loaded. Otherwise, the content type 'binary/octet-stream' will be used.
62
- # @option opts [Integer] (262144) :chunk_size size of file chunks in bytes.
63
- # @option opts [Boolean] :safe (false) When safe mode is enabled, the chunks sent to the server
64
- # will be validated using an md5 hash. If validation fails, an exception will be raised.
57
+ # @option opts [Integer] (261120) :chunk_size size of file chunks in bytes.
58
+ # @option opts [String, Integer, Symbol] :w (1) Set write concern
65
59
  #
66
- # @return [Mongo::ObjectId] the file's id.
60
+ # Notes on write concern:
61
+ # When :w > 0, the chunks sent to the server are validated using an md5 hash.
62
+ # If validation fails, an exception will be raised.
63
+ #
64
+ # @return [BSON::ObjectId] the file's id.
67
65
  def put(data, opts={})
68
- filename = opts.delete :filename
69
- opts.merge!(default_grid_io_opts)
70
- file = GridIO.new(@files, @chunks, filename, 'w', opts=opts)
71
- file.write(data)
72
- file.close
73
- file.files_id
66
+ begin
67
+ # Ensure there is an index on files_id and n, as state may have changed since instantiation of self.
68
+ # Recall that index definitions are cached with ensure_index so this statement won't unneccesarily repeat index creation.
69
+ @chunks.ensure_index([['files_id', Mongo::ASCENDING], ['n', Mongo::ASCENDING]], :unique => true)
70
+ opts = opts.dup
71
+ filename = opts.delete(:filename)
72
+ opts.merge!(default_grid_io_opts)
73
+ file = GridIO.new(@files, @chunks, filename, 'w', opts)
74
+ file.write(data)
75
+ file.close
76
+ file.files_id
77
+ rescue Mongo::ConnectionFailure => e
78
+ raise e, "Failed to create necessary index and write data."
79
+ end
74
80
  end
75
81
 
76
82
  # Read a file from the file store.
77
83
  #
78
- # @param [] id the file's unique id.
84
+ # @param id the file's unique id.
79
85
  #
80
86
  # @return [Mongo::GridIO]
81
87
  def get(id)
@@ -89,7 +95,7 @@ module Mongo
89
95
  # is attempting to read a file while it's being deleted. While the odds for this
90
96
  # kind of race condition are small, it's important to be aware of.
91
97
  #
92
- # @param [] id
98
+ # @param id
93
99
  #
94
100
  # @return [Boolean]
95
101
  def delete(id)
@@ -1,20 +1,16 @@
1
- # encoding: UTF-8
2
-
3
- # --
4
- # Copyright (C) 2008-2011 10gen Inc.
1
+ # Copyright (C) 2009-2013 MongoDB, Inc.
5
2
  #
6
3
  # Licensed under the Apache License, Version 2.0 (the "License");
7
4
  # you may not use this file except in compliance with the License.
8
5
  # You may obtain a copy of the License at
9
6
  #
10
- # http://www.apache.org/licenses/LICENSE-2.0
7
+ # http://www.apache.org/licenses/LICENSE-2.0
11
8
  #
12
9
  # Unless required by applicable law or agreed to in writing, software
13
10
  # distributed under the License is distributed on an "AS IS" BASIS,
14
11
  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
12
  # See the License for the specific language governing permissions and
16
13
  # limitations under the License.
17
- # ++
18
14
 
19
15
  module Mongo
20
16
  module GridExt
@@ -33,19 +29,19 @@ module Mongo
33
29
  # @example
34
30
  #
35
31
  # # Check for the existence of a given filename
36
- # @grid = GridFileSystem.new(@db)
32
+ # @grid = Mongo::GridFileSystem.new(@db)
37
33
  # @grid.exist?(:filename => 'foo.txt')
38
34
  #
39
35
  # # Check for existence filename and content type
40
- # @grid = GridFileSystem.new(@db)
36
+ # @grid = Mongo::GridFileSystem.new(@db)
41
37
  # @grid.exist?(:filename => 'foo.txt', :content_type => 'image/jpg')
42
38
  #
43
39
  # # Check for existence by _id
44
- # @grid = Grid.new(@db)
40
+ # @grid = Mongo::Grid.new(@db)
45
41
  # @grid.exist?(:_id => BSON::ObjectId.from_string('4bddcd24beffd95a7db9b8c8'))
46
42
  #
47
43
  # # Check for existence by an arbitrary attribute.
48
- # @grid = Grid.new(@db)
44
+ # @grid = Mongo::Grid.new(@db)
49
45
  # @grid.exist?(:tags => {'$in' => ['nature', 'zen', 'photography']})
50
46
  #
51
47
  # @return [nil, Hash] either nil for the file's metadata as a hash.