mongo 1.8.6 → 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.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +0 -0
- data/LICENSE +1 -1
- data/README.md +114 -282
- data/Rakefile +18 -4
- data/VERSION +1 -1
- data/bin/mongo_console +27 -5
- data/lib/mongo/bulk_write_collection_view.rb +387 -0
- data/lib/mongo/collection.rb +283 -222
- data/lib/mongo/collection_writer.rb +364 -0
- data/lib/mongo/{util → connection}/node.rb +58 -6
- data/lib/mongo/{util → connection}/pool.rb +61 -37
- data/lib/mongo/{util → connection}/pool_manager.rb +72 -22
- data/lib/mongo/{util → connection}/sharding_pool_manager.rb +13 -0
- data/lib/mongo/connection/socket/socket_util.rb +37 -0
- data/lib/mongo/connection/socket/ssl_socket.rb +95 -0
- data/lib/mongo/connection/socket/tcp_socket.rb +87 -0
- data/lib/mongo/connection/socket/unix_socket.rb +39 -0
- data/lib/mongo/connection/socket.rb +18 -0
- data/lib/mongo/connection.rb +19 -0
- data/lib/mongo/cursor.rb +183 -57
- data/lib/mongo/db.rb +302 -138
- data/lib/mongo/exception.rb +145 -0
- data/lib/mongo/functional/authentication.rb +455 -0
- data/lib/mongo/{util → functional}/logging.rb +23 -7
- data/lib/mongo/functional/read_preference.rb +183 -0
- data/lib/mongo/functional/scram.rb +556 -0
- data/lib/mongo/functional/uri_parser.rb +409 -0
- data/lib/mongo/{util → functional}/write_concern.rb +21 -9
- data/lib/mongo/functional.rb +20 -0
- data/lib/mongo/gridfs/grid.rb +19 -8
- data/lib/mongo/gridfs/grid_ext.rb +14 -0
- data/lib/mongo/gridfs/grid_file_system.rb +17 -4
- data/lib/mongo/gridfs/grid_io.rb +21 -9
- data/lib/mongo/gridfs.rb +18 -0
- data/lib/mongo/legacy.rb +76 -7
- data/lib/mongo/mongo_client.rb +246 -206
- data/lib/mongo/mongo_replica_set_client.rb +65 -15
- data/lib/mongo/mongo_sharded_client.rb +18 -3
- data/lib/mongo/networking.rb +47 -18
- data/lib/mongo/{util → utils}/conversions.rb +18 -3
- data/lib/mongo/{util → utils}/core_ext.rb +15 -32
- data/lib/mongo/{util → utils}/server_version.rb +15 -0
- data/lib/mongo/{util → utils}/support.rb +22 -55
- data/lib/mongo/utils/thread_local_variable_manager.rb +25 -0
- data/lib/mongo/utils.rb +19 -0
- data/lib/mongo.rb +44 -26
- data/mongo.gemspec +2 -2
- data/test/functional/authentication_test.rb +31 -10
- data/test/functional/bulk_api_stress_test.rb +133 -0
- data/test/functional/bulk_write_collection_view_test.rb +1198 -0
- data/test/functional/client_test.rb +627 -0
- data/test/functional/collection_test.rb +1419 -654
- data/test/functional/collection_writer_test.rb +83 -0
- data/test/functional/conversions_test.rb +46 -2
- data/test/functional/cursor_fail_test.rb +17 -9
- data/test/functional/cursor_message_test.rb +28 -15
- data/test/functional/cursor_test.rb +300 -165
- data/test/functional/db_api_test.rb +294 -264
- data/test/functional/db_connection_test.rb +15 -3
- data/test/functional/db_test.rb +165 -99
- data/test/functional/grid_file_system_test.rb +124 -112
- data/test/functional/grid_io_test.rb +17 -3
- data/test/functional/grid_test.rb +16 -2
- data/test/functional/pool_test.rb +99 -10
- data/test/functional/safe_test.rb +18 -4
- data/test/functional/ssl_test.rb +29 -0
- data/test/functional/support_test.rb +14 -0
- data/test/functional/timeout_test.rb +27 -27
- data/test/functional/uri_test.rb +268 -22
- data/test/functional/write_concern_test.rb +19 -5
- data/test/helpers/general.rb +50 -0
- data/test/helpers/test_unit.rb +476 -0
- data/test/replica_set/authentication_test.rb +28 -11
- data/test/replica_set/basic_test.rb +79 -23
- data/test/replica_set/client_test.rb +253 -124
- data/test/replica_set/connection_test.rb +59 -37
- data/test/replica_set/count_test.rb +18 -2
- data/test/replica_set/cursor_test.rb +30 -8
- data/test/replica_set/insert_test.rb +109 -2
- data/test/replica_set/max_values_test.rb +85 -10
- data/test/replica_set/pinning_test.rb +66 -2
- data/test/replica_set/query_test.rb +17 -3
- data/test/replica_set/read_preference_test.rb +115 -96
- data/test/replica_set/refresh_test.rb +59 -9
- data/test/replica_set/replication_ack_test.rb +32 -11
- data/test/replica_set/ssl_test.rb +32 -0
- data/test/sharded_cluster/basic_test.rb +73 -25
- data/test/shared/authentication/basic_auth_shared.rb +260 -0
- data/test/shared/authentication/bulk_api_auth_shared.rb +249 -0
- data/test/shared/authentication/gssapi_shared.rb +176 -0
- data/test/shared/authentication/sasl_plain_shared.rb +96 -0
- data/test/shared/authentication/scram_shared.rb +92 -0
- data/test/shared/ssl_shared.rb +235 -0
- data/test/test_helper.rb +47 -196
- data/test/threading/basic_test.rb +42 -2
- data/test/tools/mongo_config.rb +175 -35
- data/test/tools/mongo_config_test.rb +15 -1
- data/test/unit/client_test.rb +186 -57
- data/test/unit/collection_test.rb +44 -54
- data/test/unit/connection_test.rb +160 -71
- data/test/unit/cursor_test.rb +37 -3
- data/test/unit/db_test.rb +38 -14
- data/test/unit/grid_test.rb +15 -1
- data/test/unit/mongo_sharded_client_test.rb +30 -14
- data/test/unit/node_test.rb +16 -1
- data/test/unit/pool_manager_test.rb +21 -4
- data/test/unit/read_pref_test.rb +386 -1
- data/test/unit/read_test.rb +27 -13
- data/test/unit/safe_test.rb +22 -8
- data/test/unit/sharding_pool_manager_test.rb +25 -4
- data/test/unit/write_concern_test.rb +23 -9
- data.tar.gz.sig +0 -0
- metadata +80 -54
- metadata.gz.sig +0 -0
- data/lib/mongo/exceptions.rb +0 -65
- data/lib/mongo/util/read_preference.rb +0 -112
- data/lib/mongo/util/socket_util.rb +0 -20
- data/lib/mongo/util/ssl_socket.rb +0 -51
- data/lib/mongo/util/tcp_socket.rb +0 -62
- data/lib/mongo/util/thread_local_variable_manager.rb +0 -11
- data/lib/mongo/util/unix_socket.rb +0 -23
- data/lib/mongo/util/uri_parser.rb +0 -337
- data/test/functional/connection_test.rb +0 -449
- data/test/functional/threading_test.rb +0 -95
- data/test/replica_set/complex_connect_test.rb +0 -64
- data/test/shared/authentication.rb +0 -66
- data/test/unit/pool_test.rb +0 -9
- data/test/unit/util_test.rb +0 -55
|
@@ -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
|
|
@@ -1,5 +1,21 @@
|
|
|
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
|
+
|
|
1
15
|
module Mongo
|
|
2
16
|
module WriteConcern
|
|
17
|
+
VALID_KEYS = [:w, :j, :fsync, :wtimeout]
|
|
18
|
+
DEFAULT_WRITE_CONCERN = {:w => 1}
|
|
3
19
|
|
|
4
20
|
attr_reader :legacy_write_concern
|
|
5
21
|
|
|
@@ -29,15 +45,11 @@ module Mongo
|
|
|
29
45
|
|
|
30
46
|
# todo: throw exception for conflicting write concern options
|
|
31
47
|
def get_write_concern(opts, parent=nil)
|
|
32
|
-
write_concern_from_legacy(opts) if opts.key?(:safe) ||
|
|
33
|
-
write_concern =
|
|
34
|
-
:w => 1,
|
|
35
|
-
:j => false,
|
|
36
|
-
:fsync => false,
|
|
37
|
-
:wtimeout => nil
|
|
38
|
-
}
|
|
48
|
+
write_concern_from_legacy(opts) if opts.key?(:safe) || legacy_write_concern
|
|
49
|
+
write_concern = DEFAULT_WRITE_CONCERN.dup
|
|
39
50
|
write_concern.merge!(parent.write_concern) if parent
|
|
40
|
-
write_concern.merge!(opts.reject {|k,v| !
|
|
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)
|
|
41
53
|
write_concern
|
|
42
54
|
end
|
|
43
55
|
|
|
@@ -51,4 +63,4 @@ module Mongo
|
|
|
51
63
|
end
|
|
52
64
|
|
|
53
65
|
end
|
|
54
|
-
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'
|
data/lib/mongo/gridfs/grid.rb
CHANGED
|
@@ -1,3 +1,17 @@
|
|
|
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
|
+
|
|
1
15
|
module Mongo
|
|
2
16
|
|
|
3
17
|
# Implementation of the MongoDB GridFS specification. A file store.
|
|
@@ -9,8 +23,6 @@ module Mongo
|
|
|
9
23
|
# Initialize a new Grid instance, consisting of a MongoDB database
|
|
10
24
|
# and a filesystem prefix if not using the default.
|
|
11
25
|
#
|
|
12
|
-
# @core gridfs
|
|
13
|
-
#
|
|
14
26
|
# @see GridFileSystem
|
|
15
27
|
def initialize(db, fs_name=DEFAULT_FS_NAME)
|
|
16
28
|
raise MongoArgumentError, "db must be a Mongo::DB." unless db.is_a?(Mongo::DB)
|
|
@@ -21,7 +33,6 @@ module Mongo
|
|
|
21
33
|
@fs_name = fs_name
|
|
22
34
|
|
|
23
35
|
# This will create indexes only if we're connected to a primary node.
|
|
24
|
-
connection = @db.connection
|
|
25
36
|
begin
|
|
26
37
|
@chunks.ensure_index([['files_id', Mongo::ASCENDING], ['n', Mongo::ASCENDING]], :unique => true)
|
|
27
38
|
rescue Mongo::ConnectionFailure
|
|
@@ -43,11 +54,11 @@ module Mongo
|
|
|
43
54
|
# @option opts [String] :content_type ('binary/octet-stream') If no content type is specified,
|
|
44
55
|
# the content type will may be inferred from the filename extension if the mime-types gem can be
|
|
45
56
|
# loaded. Otherwise, the content type 'binary/octet-stream' will be used.
|
|
46
|
-
# @option opts [Integer] (
|
|
57
|
+
# @option opts [Integer] (261120) :chunk_size size of file chunks in bytes.
|
|
47
58
|
# @option opts [String, Integer, Symbol] :w (1) Set write concern
|
|
48
59
|
#
|
|
49
|
-
# Notes on write concern:
|
|
50
|
-
# When :w > 0, the chunks sent to the server are validated using an md5 hash.
|
|
60
|
+
# Notes on write concern:
|
|
61
|
+
# When :w > 0, the chunks sent to the server are validated using an md5 hash.
|
|
51
62
|
# If validation fails, an exception will be raised.
|
|
52
63
|
#
|
|
53
64
|
# @return [BSON::ObjectId] the file's id.
|
|
@@ -70,7 +81,7 @@ module Mongo
|
|
|
70
81
|
|
|
71
82
|
# Read a file from the file store.
|
|
72
83
|
#
|
|
73
|
-
# @param
|
|
84
|
+
# @param id the file's unique id.
|
|
74
85
|
#
|
|
75
86
|
# @return [Mongo::GridIO]
|
|
76
87
|
def get(id)
|
|
@@ -84,7 +95,7 @@ module Mongo
|
|
|
84
95
|
# is attempting to read a file while it's being deleted. While the odds for this
|
|
85
96
|
# kind of race condition are small, it's important to be aware of.
|
|
86
97
|
#
|
|
87
|
-
# @param
|
|
98
|
+
# @param id
|
|
88
99
|
#
|
|
89
100
|
# @return [Boolean]
|
|
90
101
|
def delete(id)
|
|
@@ -1,3 +1,17 @@
|
|
|
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
|
+
|
|
1
15
|
module Mongo
|
|
2
16
|
module GridExt
|
|
3
17
|
module InstanceMethods
|
|
@@ -1,3 +1,17 @@
|
|
|
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
|
+
|
|
1
15
|
module Mongo
|
|
2
16
|
|
|
3
17
|
# A file store built on the GridFS specification featuring
|
|
@@ -22,7 +36,6 @@ module Mongo
|
|
|
22
36
|
@default_query_opts = {:sort => [['filename', 1], ['uploadDate', -1]], :limit => 1}
|
|
23
37
|
|
|
24
38
|
# This will create indexes only if we're connected to a primary node.
|
|
25
|
-
connection = @db.connection
|
|
26
39
|
begin
|
|
27
40
|
@files.ensure_index([['filename', 1], ['uploadDate', -1]])
|
|
28
41
|
@chunks.ensure_index([['files_id', Mongo::ASCENDING], ['n', Mongo::ASCENDING]], :unique => true)
|
|
@@ -47,7 +60,7 @@ module Mongo
|
|
|
47
60
|
# @option opts [String] :content_type ('binary/octet-stream') If no content type is specified,
|
|
48
61
|
# the content type will may be inferred from the filename extension if the mime-types gem can be
|
|
49
62
|
# loaded. Otherwise, the content type 'binary/octet-stream' will be used.
|
|
50
|
-
# @option opts [Integer] (
|
|
63
|
+
# @option opts [Integer] (261120) :chunk_size size of file chunks in bytes.
|
|
51
64
|
# @option opts [Boolean] :delete_old (false) ensure that old versions of the file are deleted. This option
|
|
52
65
|
# only work in 'w' mode. Certain precautions must be taken when deleting GridFS files. See the notes under
|
|
53
66
|
# GridFileSystem#delete.
|
|
@@ -56,8 +69,8 @@ module Mongo
|
|
|
56
69
|
# Notes on write concern:
|
|
57
70
|
# When :w > 0, the chunks sent to the server
|
|
58
71
|
# will be validated using an md5 hash. If validation fails, an exception will be raised.
|
|
59
|
-
# @option opts [Integer] :versions (false) deletes all versions which exceed the number specified to
|
|
60
|
-
# retain ordered by uploadDate. This option only works in 'w' mode. Certain precautions must be taken when
|
|
72
|
+
# @option opts [Integer] :versions (false) deletes all versions which exceed the number specified to
|
|
73
|
+
# retain ordered by uploadDate. This option only works in 'w' mode. Certain precautions must be taken when
|
|
61
74
|
# deleting GridFS files. See the notes under GridFileSystem#delete.
|
|
62
75
|
#
|
|
63
76
|
# @example
|
data/lib/mongo/gridfs/grid_io.rb
CHANGED
|
@@ -1,13 +1,27 @@
|
|
|
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
|
+
|
|
1
15
|
require 'digest/md5'
|
|
2
16
|
|
|
3
17
|
module Mongo
|
|
4
18
|
|
|
5
19
|
# GridIO objects represent files in the GridFS specification. This class
|
|
6
20
|
# manages the reading and writing of file chunks and metadata.
|
|
7
|
-
class GridIO
|
|
21
|
+
class GridIO
|
|
8
22
|
include Mongo::WriteConcern
|
|
9
|
-
|
|
10
|
-
DEFAULT_CHUNK_SIZE =
|
|
23
|
+
|
|
24
|
+
DEFAULT_CHUNK_SIZE = 255 * 1024
|
|
11
25
|
DEFAULT_CONTENT_TYPE = 'binary/octet-stream'
|
|
12
26
|
PROTECTED_ATTRS = [:files_id, :file_length, :client_md5, :server_md5]
|
|
13
27
|
|
|
@@ -25,7 +39,7 @@ module Mongo
|
|
|
25
39
|
# @option opts [Hash] :query a query selector used when opening the file in 'r' mode.
|
|
26
40
|
# @option opts [Hash] :query_opts any query options to be used when opening the file in 'r' mode.
|
|
27
41
|
# @option opts [String] :fs_name the file system prefix.
|
|
28
|
-
# @option opts [Integer] (
|
|
42
|
+
# @option opts [Integer] (261120) :chunk_size size of file chunks in bytes.
|
|
29
43
|
# @option opts [Hash] :metadata ({}) any additional data to store with the file.
|
|
30
44
|
# @option opts [ObjectId] :_id (ObjectId) a unique id for
|
|
31
45
|
# the file to be use in lieu of an automatically generated one.
|
|
@@ -92,11 +106,9 @@ module Mongo
|
|
|
92
106
|
|
|
93
107
|
# Write the given string (binary) data to the file.
|
|
94
108
|
#
|
|
95
|
-
# @param [String]
|
|
96
|
-
# the data to write
|
|
109
|
+
# @param [String] io the data to write.
|
|
97
110
|
#
|
|
98
|
-
# @return [Integer]
|
|
99
|
-
# the number of bytes written.
|
|
111
|
+
# @return [Integer] the number of bytes written.
|
|
100
112
|
def write(io)
|
|
101
113
|
raise GridError, "file not opened for write" unless @mode[0] == ?w
|
|
102
114
|
if io.is_a? String
|
|
@@ -238,7 +250,7 @@ module Mongo
|
|
|
238
250
|
# @return [Mongo::GridIO] self
|
|
239
251
|
def each
|
|
240
252
|
return read_all unless block_given?
|
|
241
|
-
while chunk = read(chunk_size)
|
|
253
|
+
while chunk = read(chunk_size)
|
|
242
254
|
yield chunk
|
|
243
255
|
break if chunk.empty?
|
|
244
256
|
end
|
data/lib/mongo/gridfs.rb
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
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/gridfs/grid_ext'
|
|
16
|
+
require 'mongo/gridfs/grid'
|
|
17
|
+
require 'mongo/gridfs/grid_file_system'
|
|
18
|
+
require 'mongo/gridfs/grid_io'
|