mongo 1.9.2 → 1.10.0.rc0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (113) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data.tar.gz.sig +0 -0
  4. data/LICENSE +1 -1
  5. data/README.md +94 -334
  6. data/Rakefile +6 -4
  7. data/VERSION +1 -1
  8. data/bin/mongo_console +13 -6
  9. data/lib/mongo.rb +22 -27
  10. data/lib/mongo/bulk_write_collection_view.rb +352 -0
  11. data/lib/mongo/collection.rb +128 -188
  12. data/lib/mongo/collection_writer.rb +348 -0
  13. data/lib/mongo/connection.rb +19 -0
  14. data/lib/mongo/{util → connection}/node.rb +15 -1
  15. data/lib/mongo/{util → connection}/pool.rb +34 -19
  16. data/lib/mongo/{util → connection}/pool_manager.rb +8 -2
  17. data/lib/mongo/{util → connection}/sharding_pool_manager.rb +1 -1
  18. data/lib/mongo/connection/socket.rb +18 -0
  19. data/lib/mongo/{util → connection/socket}/socket_util.rb +5 -2
  20. data/lib/mongo/{util → connection/socket}/ssl_socket.rb +3 -4
  21. data/lib/mongo/{util → connection/socket}/tcp_socket.rb +25 -15
  22. data/lib/mongo/{util → connection/socket}/unix_socket.rb +6 -4
  23. data/lib/mongo/cursor.rb +113 -47
  24. data/lib/mongo/db.rb +203 -131
  25. data/lib/mongo/{exceptions.rb → exception.rb} +7 -1
  26. data/lib/mongo/functional.rb +19 -0
  27. data/lib/mongo/functional/authentication.rb +303 -0
  28. data/lib/mongo/{util → functional}/logging.rb +1 -1
  29. data/lib/mongo/{util → functional}/read_preference.rb +49 -1
  30. data/lib/mongo/{util → functional}/uri_parser.rb +81 -69
  31. data/lib/mongo/{util → functional}/write_concern.rb +2 -1
  32. data/{test/unit/pool_test.rb → lib/mongo/gridfs.rb} +5 -10
  33. data/lib/mongo/gridfs/grid.rb +1 -3
  34. data/lib/mongo/gridfs/grid_ext.rb +1 -1
  35. data/lib/mongo/gridfs/grid_file_system.rb +1 -1
  36. data/lib/mongo/gridfs/grid_io.rb +1 -1
  37. data/lib/mongo/legacy.rb +63 -8
  38. data/lib/mongo/mongo_client.rb +128 -154
  39. data/lib/mongo/mongo_replica_set_client.rb +17 -11
  40. data/lib/mongo/mongo_sharded_client.rb +2 -1
  41. data/lib/mongo/networking.rb +19 -10
  42. data/lib/mongo/utils.rb +19 -0
  43. data/lib/mongo/{util → utils}/conversions.rb +1 -1
  44. data/lib/mongo/{util → utils}/core_ext.rb +1 -1
  45. data/lib/mongo/{util → utils}/server_version.rb +1 -1
  46. data/lib/mongo/{util → utils}/support.rb +10 -57
  47. data/lib/mongo/{util → utils}/thread_local_variable_manager.rb +1 -1
  48. data/test/functional/authentication_test.rb +8 -21
  49. data/test/functional/bulk_write_collection_view_test.rb +782 -0
  50. data/test/functional/{connection_test.rb → client_test.rb} +153 -78
  51. data/test/functional/collection_test.rb +343 -97
  52. data/test/functional/collection_writer_test.rb +83 -0
  53. data/test/functional/conversions_test.rb +1 -3
  54. data/test/functional/cursor_fail_test.rb +3 -3
  55. data/test/functional/cursor_message_test.rb +3 -3
  56. data/test/functional/cursor_test.rb +38 -3
  57. data/test/functional/db_api_test.rb +5 -5
  58. data/test/functional/db_connection_test.rb +2 -2
  59. data/test/functional/db_test.rb +35 -11
  60. data/test/functional/grid_file_system_test.rb +2 -2
  61. data/test/functional/grid_io_test.rb +2 -2
  62. data/test/functional/grid_test.rb +2 -2
  63. data/test/functional/pool_test.rb +2 -3
  64. data/test/functional/safe_test.rb +5 -5
  65. data/test/functional/ssl_test.rb +22 -102
  66. data/test/functional/support_test.rb +1 -1
  67. data/test/functional/timeout_test.rb +6 -22
  68. data/test/functional/uri_test.rb +113 -12
  69. data/test/functional/write_concern_test.rb +6 -6
  70. data/test/helpers/general.rb +50 -0
  71. data/test/helpers/test_unit.rb +309 -0
  72. data/test/replica_set/authentication_test.rb +8 -23
  73. data/test/replica_set/basic_test.rb +41 -14
  74. data/test/replica_set/client_test.rb +179 -117
  75. data/test/replica_set/complex_connect_test.rb +6 -7
  76. data/test/replica_set/connection_test.rb +46 -38
  77. data/test/replica_set/count_test.rb +2 -2
  78. data/test/replica_set/cursor_test.rb +8 -8
  79. data/test/replica_set/insert_test.rb +64 -2
  80. data/test/replica_set/max_values_test.rb +59 -10
  81. data/test/replica_set/pinning_test.rb +2 -2
  82. data/test/replica_set/query_test.rb +2 -2
  83. data/test/replica_set/read_preference_test.rb +6 -6
  84. data/test/replica_set/refresh_test.rb +7 -7
  85. data/test/replica_set/replication_ack_test.rb +5 -5
  86. data/test/replica_set/ssl_test.rb +24 -106
  87. data/test/sharded_cluster/basic_test.rb +43 -15
  88. data/test/shared/authentication/basic_auth_shared.rb +215 -0
  89. data/test/shared/authentication/sasl_plain_shared.rb +96 -0
  90. data/test/shared/ssl_shared.rb +173 -0
  91. data/test/test_helper.rb +31 -199
  92. data/test/threading/basic_test.rb +29 -3
  93. data/test/tools/mongo_config.rb +45 -20
  94. data/test/tools/mongo_config_test.rb +1 -1
  95. data/test/unit/client_test.rb +136 -57
  96. data/test/unit/collection_test.rb +31 -55
  97. data/test/unit/connection_test.rb +135 -72
  98. data/test/unit/cursor_test.rb +2 -2
  99. data/test/unit/db_test.rb +19 -15
  100. data/test/unit/grid_test.rb +2 -2
  101. data/test/unit/mongo_sharded_client_test.rb +17 -15
  102. data/test/unit/node_test.rb +2 -2
  103. data/test/unit/pool_manager_test.rb +7 -5
  104. data/test/unit/read_pref_test.rb +82 -2
  105. data/test/unit/read_test.rb +14 -14
  106. data/test/unit/safe_test.rb +9 -9
  107. data/test/unit/sharding_pool_manager_test.rb +11 -5
  108. data/test/unit/write_concern_test.rb +9 -9
  109. metadata +71 -56
  110. metadata.gz.sig +0 -0
  111. data/test/functional/threading_test.rb +0 -109
  112. data/test/shared/authentication.rb +0 -121
  113. data/test/unit/util_test.rb +0 -69
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2013 10gen Inc.
1
+ # Copyright (C) 2009-2013 MongoDB, Inc.
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -52,6 +52,7 @@ module Mongo
52
52
  }
53
53
  write_concern.merge!(parent.write_concern) if parent
54
54
  write_concern.merge!(opts.reject {|k,v| !write_concern.keys.include?(k)})
55
+ write_concern[:w] = write_concern[:w].to_s if write_concern[:w].is_a?(Symbol)
55
56
  write_concern
56
57
  end
57
58
 
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2013 10gen Inc.
1
+ # Copyright (C) 2009-2013 MongoDB, Inc.
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -12,12 +12,7 @@
12
12
  # See the License for the specific language governing permissions and
13
13
  # limitations under the License.
14
14
 
15
- require 'test_helper'
16
- include Mongo
17
-
18
- class PoolTest < Test::Unit::TestCase
19
- context "Initialization: " do
20
- should "do" do
21
- end
22
- end
23
- end
15
+ require 'mongo/gridfs/grid_ext'
16
+ require 'mongo/gridfs/grid'
17
+ require 'mongo/gridfs/grid_file_system'
18
+ require 'mongo/gridfs/grid_io'
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2013 10gen Inc.
1
+ # Copyright (C) 2009-2013 MongoDB, Inc.
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -23,8 +23,6 @@ module Mongo
23
23
  # Initialize a new Grid instance, consisting of a MongoDB database
24
24
  # and a filesystem prefix if not using the default.
25
25
  #
26
- # @core gridfs
27
- #
28
26
  # @see GridFileSystem
29
27
  def initialize(db, fs_name=DEFAULT_FS_NAME)
30
28
  raise MongoArgumentError, "db must be a Mongo::DB." unless db.is_a?(Mongo::DB)
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2013 10gen Inc.
1
+ # Copyright (C) 2009-2013 MongoDB, Inc.
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2013 10gen Inc.
1
+ # Copyright (C) 2009-2013 MongoDB, Inc.
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2013 10gen Inc.
1
+ # Copyright (C) 2009-2013 MongoDB, Inc.
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2013 10gen Inc.
1
+ # Copyright (C) 2009-2013 MongoDB, Inc.
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -38,8 +38,8 @@ module Mongo
38
38
  end
39
39
 
40
40
  module Mongo
41
- # @deprecated Use Mongo::MongoClient instead. Support will be removed after v2.0
42
- # Please see old documentation for the Connection class
41
+ # @deprecated Use Mongo::MongoClient instead. Support will be removed after
42
+ # v2.0. Please see old documentation for the Connection class.
43
43
  class Connection < MongoClient
44
44
  include Mongo::LegacyWriteConcern
45
45
 
@@ -53,8 +53,9 @@ module Mongo
53
53
  end
54
54
  end
55
55
 
56
- # @deprecated Use Mongo::MongoReplicaSetClient instead. Support will be removed after v2.0
57
- # Please see old documentation for the ReplSetConnection class
56
+ # @deprecated Use Mongo::MongoReplicaSetClient instead. Support will be
57
+ # removed after v2.0. Please see old documentation for the
58
+ # ReplSetConnection class.
58
59
  class ReplSetConnection < MongoReplicaSetClient
59
60
  include Mongo::LegacyWriteConcern
60
61
 
@@ -68,8 +69,8 @@ module Mongo
68
69
  end
69
70
  end
70
71
 
71
- # @deprecated Use Mongo::MongoShardedClient instead. Support will be removed after v2.0
72
- # Please see old documentation for the ShardedConnection class
72
+ # @deprecated Use Mongo::MongoShardedClient instead. Support will be removed
73
+ # after v2.0. Please see old documentation for the ShardedConnection class.
73
74
  class ShardedConnection < MongoShardedClient
74
75
  include Mongo::LegacyWriteConcern
75
76
 
@@ -82,4 +83,58 @@ module Mongo
82
83
  super
83
84
  end
84
85
  end
85
- end
86
+
87
+ class MongoClient
88
+ # @deprecated This method is no longer in use and never needs to be called
89
+ # directly. Support will be removed after v2.0
90
+ def authenticate_pools
91
+ @primary_pool.authenticate_existing
92
+ end
93
+
94
+ # @deprecated This method is no longer in use and never needs to be called
95
+ # directly. Support will be removed after v2.0
96
+ def logout_pools(database)
97
+ @primary_pool.logout_existing(database)
98
+ end
99
+
100
+ # @deprecated This method is no longer in use and never needs to be called
101
+ # directly. Support will be removed after v2.0
102
+ def apply_saved_authentication
103
+ true
104
+ end
105
+ end
106
+
107
+ class MongoReplicaSetClient
108
+ # @deprecated This method is no longer in use and never needs to be called
109
+ # directly. Support will be removed after v2.0
110
+ def authenticate_pools
111
+ @manager.pools.each { |pool| pool.authenticate_existing }
112
+ end
113
+
114
+ # @deprecated This method is no longer in use and never needs to be called
115
+ # directly. Support will be removed after v2.0
116
+ def logout_pools(database)
117
+ @manager.pools.each { |pool| pool.logout_existing(database) }
118
+ end
119
+ end
120
+
121
+ class DB
122
+ # @deprecated Please use MongoClient#issue_authentication instead. Support
123
+ # will be removed after v2.0
124
+ def issue_authentication(username, password, save_auth=true, opts={})
125
+ auth = Authentication.validate_credentials({
126
+ :db_name => self.name,
127
+ :username => username,
128
+ :password => password
129
+ })
130
+ opts[:save_auth] = save_auth
131
+ @client.issue_authentication(auth, opts)
132
+ end
133
+
134
+ # @deprecated Please use MongoClient#issue_logout instead. Support will be
135
+ # removed after v2.0
136
+ def issue_logout(opts={})
137
+ @client.issue_logout(self.name, opts)
138
+ end
139
+ end
140
+ end
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2013 10gen Inc.
1
+ # Copyright (C) 2009-2013 MongoDB, Inc.
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -12,10 +12,6 @@
12
12
  # See the License for the specific language governing permissions and
13
13
  # limitations under the License.
14
14
 
15
- require 'set'
16
- require 'socket'
17
- require 'thread'
18
-
19
15
  module Mongo
20
16
 
21
17
  # Instantiates and manages self.connections to MongoDB.
@@ -23,6 +19,19 @@ module Mongo
23
19
  include Mongo::Logging
24
20
  include Mongo::Networking
25
21
  include Mongo::WriteConcern
22
+ include Mongo::Authentication
23
+
24
+ # Wire version
25
+ RELEASE_2_4_AND_BEFORE = 0 # Everything before we started tracking.
26
+ AGG_RETURNS_CURSORS = 1 # The aggregation command may now be requested to return cursors.
27
+ BATCH_COMMANDS = 2 # insert, update, and delete batch command
28
+ MAX_WIRE_VERSION = BATCH_COMMANDS # supported by this client implementation
29
+ MIN_WIRE_VERSION = RELEASE_2_4_AND_BEFORE # supported by this client implementation
30
+
31
+ # Server command headroom
32
+ COMMAND_HEADROOM = 16_384
33
+ APPEND_HEADROOM = COMMAND_HEADROOM / 2
34
+ SERIALIZE_HEADROOM = APPEND_HEADROOM / 2
26
35
 
27
36
  Mutex = ::Mutex
28
37
  ConditionVariable = ::ConditionVariable
@@ -30,7 +39,7 @@ module Mongo
30
39
  DEFAULT_HOST = 'localhost'
31
40
  DEFAULT_PORT = 27017
32
41
  DEFAULT_DB_NAME = 'test'
33
- GENERIC_OPTS = [:auths, :logger, :connect, :default_db]
42
+ GENERIC_OPTS = [:auths, :logger, :connect, :db_name]
34
43
  TIMEOUT_OPTS = [:timeout, :op_timeout, :connect_timeout]
35
44
  SSL_OPTS = [:ssl, :ssl_key, :ssl_cert, :ssl_verify, :ssl_ca_cert]
36
45
  POOL_OPTS = [:pool_size, :pool_timeout]
@@ -55,7 +64,9 @@ module Mongo
55
64
  :op_timeout,
56
65
  :tag_sets,
57
66
  :acceptable_latency,
58
- :read
67
+ :read,
68
+ :max_wire_version,
69
+ :min_wire_version
59
70
 
60
71
  # Create a connection to single MongoDB instance.
61
72
  #
@@ -130,10 +141,8 @@ module Mongo
130
141
  # driver fails to connect to a replica set with that name.
131
142
  #
132
143
  # @raise [MongoArgumentError] If called with no arguments and <code>ENV["MONGODB_URI"]</code> implies a replica set.
133
- #
134
- # @core self.connections
135
144
  def initialize(*args)
136
- opts = args.last.is_a?(Hash) ? args.pop : {}
145
+ opts = args.last.is_a?(Hash) ? args.pop : {}
137
146
  @host, @port = parse_init(args[0], args[1], opts)
138
147
 
139
148
  # Lock for request ids.
@@ -145,11 +154,13 @@ module Mongo
145
154
  @mongos = false
146
155
 
147
156
  # Not set for direct connection
148
- @tag_sets = []
157
+ @tag_sets = []
149
158
  @acceptable_latency = 15
150
159
 
160
+ @max_bson_size = nil
151
161
  @max_message_size = nil
152
- @max_bson_size = nil
162
+ @max_wire_version = nil
163
+ @min_wire_version = nil
153
164
 
154
165
  check_opts(opts)
155
166
  setup(opts.dup)
@@ -182,8 +193,7 @@ module Mongo
182
193
  #
183
194
  # @deprecated
184
195
  def self.multi(nodes, opts={})
185
- warn "MongoClient.multi is now deprecated and will be removed in v2.0. Please use MongoReplicaSetClient.new instead."
186
-
196
+ warn 'MongoClient.multi is now deprecated and will be removed in v2.0. Please use MongoReplicaSetClient.new instead.'
187
197
  MongoReplicaSetClient.new(nodes, opts)
188
198
  end
189
199
 
@@ -203,20 +213,6 @@ module Mongo
203
213
  parser.connection(extra_opts)
204
214
  end
205
215
 
206
- def parse_init(host, port, opts)
207
- if host.nil? && port.nil? && ENV.has_key?('MONGODB_URI')
208
- parser = URIParser.new(ENV['MONGODB_URI'])
209
- if parser.replicaset?
210
- raise MongoArgumentError,
211
- "ENV['MONGODB_URI'] implies a replica set."
212
- end
213
- opts.merge!(parser.connection_options)
214
- [parser.host, parser.port]
215
- else
216
- [host || DEFAULT_HOST, port || DEFAULT_PORT]
217
- end
218
- end
219
-
220
216
  # The host name used for this connection.
221
217
  #
222
218
  # @return [String]
@@ -260,76 +256,6 @@ module Mongo
260
256
  self['admin']['$cmd.sys.unlock'].find_one
261
257
  end
262
258
 
263
- # Apply each of the saved database authentications.
264
- #
265
- # @return [Boolean] returns true if authentications exist and succeeds, false
266
- # if none exists.
267
- #
268
- # @raise [AuthenticationError] raises an exception if any one
269
- # authentication fails.
270
- def apply_saved_authentication(opts={})
271
- return false if @auths.empty?
272
- @auths.each do |auth|
273
- self[auth[:db_name]].issue_authentication(auth[:username], auth[:password], false,
274
- :source => auth[:source], :socket => opts[:socket])
275
- end
276
- true
277
- end
278
-
279
- # Save an authentication to this connection. When connecting,
280
- # the connection will attempt to re-authenticate on every db
281
- # specified in the list of auths. This method is called automatically
282
- # by DB#authenticate.
283
- #
284
- # Note: this method will not actually issue an authentication command. To do that,
285
- # either run MongoClient#apply_saved_authentication or DB#authenticate.
286
- #
287
- # @param [String] db_name
288
- # @param [String] username
289
- # @param [String] password
290
- #
291
- # @return [Hash] a hash representing the authentication just added.
292
- def add_auth(db_name, username, password, source)
293
- if @auths.any? {|a| a[:db_name] == db_name}
294
- raise MongoArgumentError, "Cannot apply multiple authentications to database '#{db_name}'"
295
- end
296
-
297
- auth = {
298
- :db_name => db_name,
299
- :username => username,
300
- :password => password,
301
- :source => source
302
- }
303
- @auths << auth
304
- auth
305
- end
306
-
307
- # Remove a saved authentication for this connection.
308
- #
309
- # @param [String] db_name
310
- #
311
- # @return [Boolean]
312
- def remove_auth(db_name)
313
- return unless @auths
314
- @auths.reject! { |a| a[:db_name] == db_name } ? true : false
315
- end
316
-
317
- # Remove all authentication information stored in this connection.
318
- #
319
- # @return [true] this operation return true because it always succeeds.
320
- def clear_auths
321
- @auths = []
322
- true
323
- end
324
-
325
- def authenticate_pools
326
- @primary_pool.authenticate_existing
327
- end
328
-
329
- def logout_pools(db)
330
- @primary_pool.logout_existing(db)
331
- end
332
-
333
259
  # Return a hash with all database names
334
260
  # and their respective sizes on disk.
335
261
  #
@@ -352,55 +278,50 @@ module Mongo
352
278
  # Return a database with the given name.
353
279
  # See DB#new for valid options hash parameters.
354
280
  #
355
- # @param [String] db_name a valid database name.
356
- # @param [Hash] opts options to be passed to the DB constructor.
281
+ # @param name [String] The name of the database.
282
+ # @param opts [Hash] A hash of options to be passed to the DB constructor.
357
283
  #
358
- # @return [Mongo::DB]
359
- #
360
- # @core databases db-instance_method
361
- def db(db_name = @default_db, opts = {})
362
- DB.new(db_name, self, opts)
284
+ # @return [DB] The DB instance.
285
+ def db(name = nil, opts = {})
286
+ DB.new(name || @db_name || DEFAULT_DB_NAME, self, opts)
363
287
  end
364
288
 
365
- # Shortcut for returning a database. Use DB#db to accept options.
366
- #
367
- # @param [String] db_name a valid database name.
289
+ # Shortcut for returning a database. Use MongoClient#db to accept options.
368
290
  #
369
- # @return [Mongo::DB]
291
+ # @param name [String] The name of the database.
370
292
  #
371
- # @core databases []-instance_method
372
- def [](db_name)
373
- DB.new(db_name, self)
293
+ # @return [DB] The DB instance.
294
+ def [](name)
295
+ DB.new(name, self)
374
296
  end
375
297
 
376
- def refresh
377
- end
298
+ def refresh; end
378
299
 
379
300
  def pinned_pool
380
301
  @primary_pool
381
302
  end
382
303
 
383
- def pin_pool(pool, read_prefs)
384
- end
304
+ def pin_pool(pool, read_prefs); end
385
305
 
386
- def unpin_pool
387
- end
306
+ def unpin_pool; end
388
307
 
389
308
  # Drop a database.
390
309
  #
391
- # @param [String] name name of an existing database.
392
- def drop_database(name)
393
- self[name].command(:dropDatabase => 1)
310
+ # @param database [String] name of an existing database.
311
+ def drop_database(database)
312
+ self[database].command(:dropDatabase => 1)
394
313
  end
395
314
 
396
315
  # Copy the database +from+ to +to+ on localhost. The +from+ database is
397
316
  # assumed to be on localhost, but an alternate host can be specified.
398
317
  #
399
- # @param [String] from name of the database to copy from.
400
- # @param [String] to name of the database to copy to.
401
- # @param [String] from_host host of the 'from' database.
402
- # @param [String] username username for authentication against from_db (>=1.3.x).
403
- # @param [String] password password for authentication against from_db (>=1.3.x).
318
+ # @param from [String] name of the database to copy from.
319
+ # @param to [String] name of the database to copy to.
320
+ # @param from_host [String] host of the 'from' database.
321
+ # @param username [String] username (applies to 'from' db)
322
+ # @param password [String] password (applies to 'from' db)
323
+ #
324
+ # @note This command only supports the MONGODB-CR authentication mechanism.
404
325
  def copy_database(from, to, from_host=DEFAULT_HOST, username=nil, password=nil)
405
326
  oh = BSON::OrderedHash.new
406
327
  oh[:copydb] = 1
@@ -409,17 +330,18 @@ module Mongo
409
330
  oh[:todb] = to
410
331
  if username || password
411
332
  unless username && password
412
- raise MongoArgumentError, "Both username and password must be supplied for authentication."
333
+ raise MongoArgumentError,
334
+ 'Both username and password must be supplied for authentication.'
413
335
  end
414
336
  nonce_cmd = BSON::OrderedHash.new
415
337
  nonce_cmd[:copydbgetnonce] = 1
416
338
  nonce_cmd[:fromhost] = from_host
417
- result = self["admin"].command(nonce_cmd)
418
- oh[:nonce] = result["nonce"]
339
+ result = self['admin'].command(nonce_cmd)
340
+ oh[:nonce] = result['nonce']
419
341
  oh[:username] = username
420
- oh[:key] = Mongo::Support.auth_key(username, password, oh[:nonce])
342
+ oh[:key] = Mongo::Authentication.auth_key(username, password, oh[:nonce])
421
343
  end
422
- self["admin"].command(oh)
344
+ self['admin'].command(oh)
423
345
  end
424
346
 
425
347
  # Checks if a server is alive. This command will return immediately
@@ -427,14 +349,14 @@ module Mongo
427
349
  #
428
350
  # @return [Hash]
429
351
  def ping
430
- self["admin"].command({:ping => 1})
352
+ self['admin'].command({:ping => 1})
431
353
  end
432
354
 
433
355
  # Get the build information for the current connection.
434
356
  #
435
357
  # @return [Hash]
436
358
  def server_info
437
- self["admin"].command({:buildinfo => 1})
359
+ self['admin'].command({:buildinfo => 1})
438
360
  end
439
361
 
440
362
  # Get the build version of the current server.
@@ -442,7 +364,7 @@ module Mongo
442
364
  # @return [Mongo::ServerVersion]
443
365
  # object allowing easy comparability of version.
444
366
  def server_version
445
- ServerVersion.new(server_info["version"])
367
+ ServerVersion.new(server_info['version'])
446
368
  end
447
369
 
448
370
  # Is it okay to connect to a slave?
@@ -477,14 +399,19 @@ module Mongo
477
399
  @mongos = true
478
400
  end
479
401
 
480
- @max_bson_size = config['maxBsonObjectSize']
402
+ @max_bson_size = config['maxBsonObjectSize']
481
403
  @max_message_size = config['maxMessageSizeBytes']
404
+ @max_wire_version = config['maxWireVersion']
405
+ @min_wire_version = config['minWireVersion']
406
+ check_wire_version_in_range
482
407
  set_primary(host_port)
483
408
  end
484
409
 
485
- if !connected?
486
- raise ConnectionFailure, "Failed to connect to a master node at #{host_port.join(":")}"
410
+ unless connected?
411
+ raise ConnectionFailure,
412
+ "Failed to connect to a master node at #{host_port.join(":")}"
487
413
  end
414
+
488
415
  true
489
416
  end
490
417
  alias :reconnect :connect
@@ -532,7 +459,7 @@ module Mongo
532
459
  def close
533
460
  @primary_pool.close if @primary_pool
534
461
  @primary_pool = nil
535
- @primary = nil
462
+ @primary = nil
536
463
  end
537
464
 
538
465
  # Returns the maximum BSON object size as returned by the core server.
@@ -547,6 +474,26 @@ module Mongo
547
474
  @max_message_size || max_bson_size * MESSAGE_SIZE_FACTOR
548
475
  end
549
476
 
477
+ def max_wire_version
478
+ @max_wire_version || 0
479
+ end
480
+
481
+ def min_wire_version
482
+ @min_wire_version || 0
483
+ end
484
+
485
+ def wire_version_feature?(feature)
486
+ min_wire_version <= feature && feature <= max_wire_version
487
+ end
488
+
489
+ def primary_wire_version_feature?(feature)
490
+ min_wire_version <= feature && feature <= max_wire_version
491
+ end
492
+
493
+ def use_write_command?(write_concern)
494
+ write_concern[:w] != 0 && primary_wire_version_feature?(Mongo::MongoClient::BATCH_COMMANDS)
495
+ end
496
+
550
497
  # Checkout a socket for reading (i.e., a secondary node).
551
498
  # Note: this is overridden in MongoReplicaSetClient.
552
499
  def checkout_reader(read_preference)
@@ -582,10 +529,10 @@ module Mongo
582
529
  socket = @socket_class.new(host, port, @op_timeout, @connect_timeout, @socket_opts)
583
530
  if @connect_timeout
584
531
  Timeout::timeout(@connect_timeout, OperationTimeout) do
585
- config = self['admin'].command({:ismaster => 1}, :socket => socket)
532
+ config = self['admin'].command({:isMaster => 1}, :socket => socket)
586
533
  end
587
534
  else
588
- config = self['admin'].command({:ismaster => 1}, :socket => socket)
535
+ config = self['admin'].command({:isMaster => 1}, :socket => socket)
589
536
  end
590
537
  rescue OperationFailure, SocketError, SystemCallError, IOError
591
538
  close
@@ -640,8 +587,8 @@ module Mongo
640
587
  # verify peer requires ca_cert, raise if only one is present
641
588
  if @socket_opts[:verify] && !@socket_opts[:ca_cert]
642
589
  raise MongoArgumentError,
643
- "If :ssl_verify_mode has been specified, then you must include " +
644
- ":ssl_ca_cert in order to perform server validation."
590
+ 'If :ssl_verify_mode has been specified, then you must include ' +
591
+ ':ssl_ca_cert in order to perform server validation.'
645
592
  end
646
593
 
647
594
  @socket_class = Mongo::SSLSocket
@@ -651,26 +598,25 @@ module Mongo
651
598
  @socket_class = Mongo::TCPSocket
652
599
  end
653
600
 
654
- # Authentication objects
655
- @auths = opts.delete(:auths) || []
601
+ @db_name = opts.delete(:db_name)
602
+ @auths = opts.delete(:auths) || Set.new
656
603
 
657
604
  # Pool size and timeout.
658
605
  @pool_size = opts.delete(:pool_size) || 1
659
606
  if opts[:timeout]
660
- warn "The :timeout option has been deprecated " +
661
- "and will be removed in the 2.0 release. " +
662
- "Use :pool_timeout instead."
607
+ warn 'The :timeout option has been deprecated ' +
608
+ 'and will be removed in the 2.0 release. ' +
609
+ 'Use :pool_timeout instead.'
663
610
  end
664
611
  @pool_timeout = opts.delete(:pool_timeout) || opts.delete(:timeout) || 5.0
665
612
 
666
613
  # Timeout on socket read operation.
667
- @op_timeout = opts.delete(:op_timeout) || nil
614
+ @op_timeout = opts.delete(:op_timeout)
668
615
 
669
616
  # Timeout on socket connect.
670
617
  @connect_timeout = opts.delete(:connect_timeout) || 30
671
618
 
672
- @logger = opts.delete(:logger) || nil
673
-
619
+ @logger = opts.delete(:logger)
674
620
  if @logger
675
621
  write_logging_startup_message
676
622
  end
@@ -683,7 +629,6 @@ module Mongo
683
629
  end
684
630
  Mongo::ReadPreference::validate(@read)
685
631
 
686
- @default_db = opts.delete(:default_db) || DEFAULT_DB_NAME
687
632
  @tag_sets = opts.delete(:tag_sets) || []
688
633
  @acceptable_latency = opts.delete(:secondary_acceptable_latency_ms) || 15
689
634
 
@@ -695,11 +640,40 @@ module Mongo
695
640
 
696
641
  private
697
642
 
698
- # Set the specified node as primary.
643
+ # Parses client initialization info from MONGODB_URI env variable
644
+ def parse_init(host, port, opts)
645
+ if host.nil? && port.nil? && ENV.has_key?('MONGODB_URI')
646
+ parser = URIParser.new(ENV['MONGODB_URI'])
647
+ if parser.replicaset?
648
+ raise MongoArgumentError,
649
+ 'ENV[\'MONGODB_URI\'] implies a replica set.'
650
+ end
651
+ opts.merge!(parser.connection_options)
652
+ [parser.host, parser.port]
653
+ else
654
+ host = host[1...-1] if host && host[0,1] == '[' # ipv6 support
655
+ [host || DEFAULT_HOST, port || DEFAULT_PORT]
656
+ end
657
+ end
658
+
659
+ # Set the specified node as primary
699
660
  def set_primary(node)
700
- host, port = *node
701
- @primary = [host, port]
661
+ host, port = *node
662
+ @primary = [host, port]
702
663
  @primary_pool = Pool.new(self, host, port, :size => @pool_size, :timeout => @pool_timeout)
703
664
  end
665
+
666
+ # calculate wire version in range
667
+ def check_wire_version_in_range
668
+ unless MIN_WIRE_VERSION <= max_wire_version &&
669
+ MAX_WIRE_VERSION >= min_wire_version
670
+ close
671
+ raise ConnectionFailure,
672
+ "Client wire-version range #{MIN_WIRE_VERSION} to " +
673
+ "#{MAX_WIRE_VERSION} does not support server range " +
674
+ "#{min_wire_version} to #{max_wire_version}, please update " +
675
+ "clients or servers"
676
+ end
677
+ end
704
678
  end
705
679
  end