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.
Files changed (129) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data/LICENSE +1 -1
  4. data/README.md +114 -282
  5. data/Rakefile +18 -4
  6. data/VERSION +1 -1
  7. data/bin/mongo_console +27 -5
  8. data/lib/mongo/bulk_write_collection_view.rb +387 -0
  9. data/lib/mongo/collection.rb +283 -222
  10. data/lib/mongo/collection_writer.rb +364 -0
  11. data/lib/mongo/{util → connection}/node.rb +58 -6
  12. data/lib/mongo/{util → connection}/pool.rb +61 -37
  13. data/lib/mongo/{util → connection}/pool_manager.rb +72 -22
  14. data/lib/mongo/{util → connection}/sharding_pool_manager.rb +13 -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 +19 -0
  21. data/lib/mongo/cursor.rb +183 -57
  22. data/lib/mongo/db.rb +302 -138
  23. data/lib/mongo/exception.rb +145 -0
  24. data/lib/mongo/functional/authentication.rb +455 -0
  25. data/lib/mongo/{util → functional}/logging.rb +23 -7
  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/{util → functional}/write_concern.rb +21 -9
  30. data/lib/mongo/functional.rb +20 -0
  31. data/lib/mongo/gridfs/grid.rb +19 -8
  32. data/lib/mongo/gridfs/grid_ext.rb +14 -0
  33. data/lib/mongo/gridfs/grid_file_system.rb +17 -4
  34. data/lib/mongo/gridfs/grid_io.rb +21 -9
  35. data/lib/mongo/gridfs.rb +18 -0
  36. data/lib/mongo/legacy.rb +76 -7
  37. data/lib/mongo/mongo_client.rb +246 -206
  38. data/lib/mongo/mongo_replica_set_client.rb +65 -15
  39. data/lib/mongo/mongo_sharded_client.rb +18 -3
  40. data/lib/mongo/networking.rb +47 -18
  41. data/lib/mongo/{util → utils}/conversions.rb +18 -3
  42. data/lib/mongo/{util → utils}/core_ext.rb +15 -32
  43. data/lib/mongo/{util → utils}/server_version.rb +15 -0
  44. data/lib/mongo/{util → utils}/support.rb +22 -55
  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 +44 -26
  48. data/mongo.gemspec +2 -2
  49. data/test/functional/authentication_test.rb +31 -10
  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 +1419 -654
  54. data/test/functional/collection_writer_test.rb +83 -0
  55. data/test/functional/conversions_test.rb +46 -2
  56. data/test/functional/cursor_fail_test.rb +17 -9
  57. data/test/functional/cursor_message_test.rb +28 -15
  58. data/test/functional/cursor_test.rb +300 -165
  59. data/test/functional/db_api_test.rb +294 -264
  60. data/test/functional/db_connection_test.rb +15 -3
  61. data/test/functional/db_test.rb +165 -99
  62. data/test/functional/grid_file_system_test.rb +124 -112
  63. data/test/functional/grid_io_test.rb +17 -3
  64. data/test/functional/grid_test.rb +16 -2
  65. data/test/functional/pool_test.rb +99 -10
  66. data/test/functional/safe_test.rb +18 -4
  67. data/test/functional/ssl_test.rb +29 -0
  68. data/test/functional/support_test.rb +14 -0
  69. data/test/functional/timeout_test.rb +27 -27
  70. data/test/functional/uri_test.rb +268 -22
  71. data/test/functional/write_concern_test.rb +19 -5
  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 +28 -11
  75. data/test/replica_set/basic_test.rb +79 -23
  76. data/test/replica_set/client_test.rb +253 -124
  77. data/test/replica_set/connection_test.rb +59 -37
  78. data/test/replica_set/count_test.rb +18 -2
  79. data/test/replica_set/cursor_test.rb +30 -8
  80. data/test/replica_set/insert_test.rb +109 -2
  81. data/test/replica_set/max_values_test.rb +85 -10
  82. data/test/replica_set/pinning_test.rb +66 -2
  83. data/test/replica_set/query_test.rb +17 -3
  84. data/test/replica_set/read_preference_test.rb +115 -96
  85. data/test/replica_set/refresh_test.rb +59 -9
  86. data/test/replica_set/replication_ack_test.rb +32 -11
  87. data/test/replica_set/ssl_test.rb +32 -0
  88. data/test/sharded_cluster/basic_test.rb +73 -25
  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 +47 -196
  96. data/test/threading/basic_test.rb +42 -2
  97. data/test/tools/mongo_config.rb +175 -35
  98. data/test/tools/mongo_config_test.rb +15 -1
  99. data/test/unit/client_test.rb +186 -57
  100. data/test/unit/collection_test.rb +44 -54
  101. data/test/unit/connection_test.rb +160 -71
  102. data/test/unit/cursor_test.rb +37 -3
  103. data/test/unit/db_test.rb +38 -14
  104. data/test/unit/grid_test.rb +15 -1
  105. data/test/unit/mongo_sharded_client_test.rb +30 -14
  106. data/test/unit/node_test.rb +16 -1
  107. data/test/unit/pool_manager_test.rb +21 -4
  108. data/test/unit/read_pref_test.rb +386 -1
  109. data/test/unit/read_test.rb +27 -13
  110. data/test/unit/safe_test.rb +22 -8
  111. data/test/unit/sharding_pool_manager_test.rb +25 -4
  112. data/test/unit/write_concern_test.rb +23 -9
  113. data.tar.gz.sig +0 -0
  114. metadata +80 -54
  115. metadata.gz.sig +0 -0
  116. data/lib/mongo/exceptions.rb +0 -65
  117. data/lib/mongo/util/read_preference.rb +0 -112
  118. data/lib/mongo/util/socket_util.rb +0 -20
  119. data/lib/mongo/util/ssl_socket.rb +0 -51
  120. data/lib/mongo/util/tcp_socket.rb +0 -62
  121. data/lib/mongo/util/thread_local_variable_manager.rb +0 -11
  122. data/lib/mongo/util/unix_socket.rb +0 -23
  123. data/lib/mongo/util/uri_parser.rb +0 -337
  124. data/test/functional/connection_test.rb +0 -449
  125. data/test/functional/threading_test.rb +0 -95
  126. data/test/replica_set/complex_connect_test.rb +0 -64
  127. data/test/shared/authentication.rb +0 -66
  128. data/test/unit/pool_test.rb +0 -9
  129. data/test/unit/util_test.rb +0 -55
@@ -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
  # Instantiates and manages connections to a MongoDB replica set.
@@ -33,10 +47,16 @@ module Mongo
33
47
  # @param [Array<String>, Array<Array(String, Integer)>] seeds
34
48
  #
35
49
  # @option opts [String, Integer, Symbol] :w (1) Set default number of nodes to which a write
36
- # should be acknowledged
37
- # @option opts [Boolean] :j (false) Set journal acknowledgement
38
- # @option opts [Integer] :wtimeout (nil) Set acknowledgement timeout
39
- # @option opts [Boolean] :fsync (false) Set fsync acknowledgement.
50
+ # should be acknowledged.
51
+ # @option opts [Integer] :wtimeout (nil) Set replica set acknowledgement timeout.
52
+ # @option opts [Boolean] :j (false) If true, block until write operations have been committed
53
+ # to the journal. Cannot be used in combination with 'fsync'. Prior to MongoDB 2.6 this option was
54
+ # ignored if the server was running without journaling. Starting with MongoDB 2.6, write operations will
55
+ # fail with an exception if this option is used when the server is running without journaling.
56
+ # @option opts [Boolean] :fsync (false) If true, and the server is running without journaling, blocks until
57
+ # the server has synced all data files to disk. If the server is running with journaling, this acts the same as
58
+ # the 'j' option, blocking until write operations have been committed to the journal.
59
+ # Cannot be used in combination with 'j'.
40
60
  #
41
61
  # Notes about write concern options:
42
62
  # Write concern options are propagated to objects instantiated from this MongoReplicaSetClient.
@@ -65,10 +85,19 @@ module Mongo
65
85
  # @option opts [Float] :pool_timeout (5.0) When all of the connections a pool are checked out,
66
86
  # this is the number of seconds to wait for a new connection to be released before throwing an exception.
67
87
  # Note: this setting is relevant only for multi-threaded applications.
68
- # @option opts [Float] :op_timeout (nil) The number of seconds to wait for a read operation to time out.
88
+ # @option opts [Float] :op_timeout (DEFAULT_OP_TIMEOUT) The number of seconds to wait for a read operation to time out.
89
+ # Set to DEFAULT_OP_TIMEOUT (20) by default. A value of nil may be specified explicitly.
69
90
  # @option opts [Float] :connect_timeout (30) The number of seconds to wait before timing out a
70
91
  # connection attempt.
71
92
  # @option opts [Boolean] :ssl (false) If true, create the connection to the server using SSL.
93
+ # @option opts [String] :ssl_cert (nil) The certificate file used to identify the local connection against MongoDB.
94
+ # @option opts [String] :ssl_key (nil) The private keyfile used to identify the local connection against MongoDB.
95
+ # Note that even if the key is stored in the same file as the certificate, both need to be explicitly specified.
96
+ # @option opts [String] :ssl_key_pass_phrase (nil) A passphrase for the private key.
97
+ # @option opts [Boolean] :ssl_verify (nil) Specifies whether or not peer certification validation should occur.
98
+ # @option opts [String] :ssl_ca_cert (nil) The ca_certs file contains a set of concatenated "certification authority"
99
+ # certificates, which are used to validate certificates passed from the other end of the connection.
100
+ # Required for :ssl_verify.
72
101
  # @option opts [Boolean] :refresh_mode (false) Set this to :sync to periodically update the
73
102
  # state of the connection every :refresh_interval seconds. Replica set connection failures
74
103
  # will always trigger a complete refresh. This option is useful when you want to add new nodes
@@ -188,12 +217,22 @@ module Mongo
188
217
  if @manager.pools.empty?
189
218
  close
190
219
  raise ConnectionFailure, "Failed to connect to any node."
191
- else
192
- @connected = true
193
220
  end
221
+ check_wire_version_in_range
222
+ @connected = true
194
223
  end
195
224
  end
196
225
 
226
+ # Reconnect the replica set client.
227
+ #
228
+ # @return [Boolean] +true+ unless the refresh lock can't be acquired.
229
+ #
230
+ # @since 1.12.4
231
+ def reconnect
232
+ close
233
+ refresh
234
+ end
235
+
197
236
  # Determine whether a replica set refresh is
198
237
  # required. If so, run a hard refresh. You can
199
238
  # force a hard refresh by running
@@ -304,14 +343,6 @@ module Mongo
304
343
  @read != :primary
305
344
  end
306
345
 
307
- def authenticate_pools
308
- @manager.pools.each { |pool| pool.authenticate_existing }
309
- end
310
-
311
- def logout_pools(db)
312
- @manager.pools.each { |pool| pool.logout_existing(db) }
313
- end
314
-
315
346
  # Generic socket checkout
316
347
  # Takes a block that returns a socket from pool
317
348
  def checkout
@@ -436,6 +467,25 @@ module Mongo
436
467
  max_bson_size * MESSAGE_SIZE_FACTOR
437
468
  end
438
469
 
470
+ def max_wire_version
471
+ return local_manager.max_wire_version if local_manager
472
+ 0
473
+ end
474
+
475
+ def min_wire_version
476
+ return local_manager.min_wire_version if local_manager
477
+ 0
478
+ end
479
+
480
+ def primary_wire_version_feature?(feature)
481
+ local_manager && local_manager.primary_pool && local_manager.primary_pool.node.wire_version_feature?(feature)
482
+ end
483
+
484
+ def max_write_batch_size
485
+ local_manager && local_manager.primary_pool && local_manager.primary_pool.node.max_write_batch_size ||
486
+ DEFAULT_MAX_WRITE_BATCH_SIZE
487
+ end
488
+
439
489
  private
440
490
 
441
491
  # Parse option hash
@@ -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
  # Instantiates and manages connections to a MongoDB sharded cluster for high availability.
@@ -52,7 +66,7 @@ module Mongo
52
66
  end
53
67
 
54
68
  def valid_opts
55
- GENERIC_OPTS + SHARDED_CLUSTER_OPTS + READ_PREFERENCE_OPTS + WRITE_CONCERN_OPTS
69
+ super + SHARDED_CLUSTER_OPTS
56
70
  end
57
71
 
58
72
  def inspect
@@ -80,6 +94,7 @@ module Mongo
80
94
  @manager = ShardingPoolManager.new(self, @seeds)
81
95
  ensure_manager
82
96
  @manager.connect
97
+ check_wire_version_in_range
83
98
  end
84
99
  ensure
85
100
  thread_local[:locks][:connecting] = false
@@ -133,10 +148,10 @@ module Mongo
133
148
  # @param uri [ String ] string of the format:
134
149
  # mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/database]
135
150
  #
136
- # @param opts [ Hash ] Any of the options available for MongoShardedClient.new
151
+ # @param options [ Hash ] Any of the options available for MongoShardedClient.new
137
152
  #
138
153
  # @return [ Mongo::MongoShardedClient ] The sharded client.
139
- def self.from_uri(uri, options = {})
154
+ def self.from_uri(uri, options={})
140
155
  uri ||= ENV['MONGODB_URI']
141
156
  URIParser.new(uri).connection(options, false, true)
142
157
  end
@@ -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 Networking
3
17
 
@@ -44,6 +58,7 @@ module Mongo
44
58
  sock.checkin
45
59
  end
46
60
  end
61
+ true
47
62
  end
48
63
 
49
64
  # Sends a message to the database, waits for a response, and raises
@@ -52,8 +67,8 @@ module Mongo
52
67
  # @param [Integer] operation a MongoDB opcode.
53
68
  # @param [BSON::ByteBuffer] message a message to send to the database.
54
69
  # @param [String] db_name the name of the database. used on call to get_last_error.
55
- # @param [Hash] last_error_params parameters to be sent to getLastError. See DB#error for
56
- # available options.
70
+ # @param [String] log_message this is currently a no-op and will be removed.
71
+ # @param [Hash] write_concern write concern.
57
72
  #
58
73
  # @see DB#get_last_error for valid last error params.
59
74
  #
@@ -71,22 +86,30 @@ module Mongo
71
86
  sock = checkout_writer
72
87
  send_message_on_socket(packed_message, sock)
73
88
  docs, num_received, cursor_id = receive(sock, last_error_id)
74
- checkin(sock)
75
89
  rescue ConnectionFailure, OperationFailure, OperationTimeout => ex
76
- checkin(sock)
77
90
  raise ex
78
91
  rescue SystemStackError, NoMemoryError, SystemCallError => ex
79
92
  close
93
+ sock = nil
80
94
  raise ex
95
+ ensure
96
+ checkin(sock) if sock
97
+ sock = nil
81
98
  end
82
99
 
83
- if num_received == 1 && (error = docs[0]['err'] || docs[0]['errmsg'])
84
- if error.include?("not master")
100
+ if num_received == 1
101
+ error = docs[0]['err'] || docs[0]['errmsg']
102
+ if error && error.include?("not master")
85
103
  close
86
104
  raise ConnectionFailure.new(docs[0]['code'].to_s + ': ' + error, docs[0]['code'], docs[0])
87
- else
105
+ elsif (!error.nil? && note = docs[0]['jnote'] || docs[0]['wnote']) # assignment
106
+ code = docs[0]['code'] || Mongo::ErrorCode::BAD_VALUE # as of server version 2.5.5
107
+ raise WriteConcernError.new(code.to_s + ': ' + note, code, docs[0])
108
+ elsif error
109
+ code = docs[0]['code'] || Mongo::ErrorCode::UNKNOWN_ERROR
88
110
  error = "wtimeout" if error == "timeout"
89
- raise OperationFailure.new(docs[0]['code'].to_s + ': ' + error, docs[0]['code'], docs[0])
111
+ raise WriteConcernError.new(code.to_s + ': ' + error, code, docs[0]) if error == "wtimeout"
112
+ raise OperationFailure.new(code.to_s + ': ' + error, code, docs[0])
90
113
  end
91
114
  end
92
115
 
@@ -101,22 +124,26 @@ module Mongo
101
124
  # @param [Socket] socket a socket to use in lieu of checking out a new one.
102
125
  # @param [Boolean] command (false) indicate whether this is a command. If this is a command,
103
126
  # the message will be sent to the primary node.
104
- # @param [Boolean] command (false) indicate whether the cursor should be exhausted. Set
127
+ # @param [Symbol] read the read preference.
128
+ # @param [Boolean] exhaust (false) indicate whether the cursor should be exhausted. Set
105
129
  # this to true only when the OP_QUERY_EXHAUST flag is set.
130
+ # @param [Boolean] compile_regex whether BSON regex objects should be compiled into Ruby regexes.
106
131
  #
107
132
  # @return [Array]
108
133
  # An array whose indexes include [0] documents returned, [1] number of document received,
109
134
  # and [3] a cursor_id.
110
135
  def receive_message(operation, message, log_message=nil, socket=nil, command=false,
111
- read=:primary, exhaust=false)
112
- request_id = add_message_headers(message, operation)
136
+ read=:primary, exhaust=false, compile_regex=true)
137
+ request_id = add_message_headers(message, operation)
113
138
  packed_message = message.to_s
139
+ opts = { :exhaust => exhaust,
140
+ :compile_regex => compile_regex }
114
141
 
115
142
  result = ''
116
143
 
117
144
  begin
118
145
  send_message_on_socket(packed_message, socket)
119
- result = receive(socket, request_id, exhaust)
146
+ result = receive(socket, request_id, opts)
120
147
  rescue ConnectionFailure => ex
121
148
  socket.close
122
149
  checkin(socket)
@@ -135,7 +162,9 @@ module Mongo
135
162
 
136
163
  private
137
164
 
138
- def receive(sock, cursor_id, exhaust=false)
165
+ def receive(sock, cursor_id, opts={})
166
+ exhaust = !!opts.delete(:exhaust)
167
+
139
168
  if exhaust
140
169
  docs = []
141
170
  num_received = 0
@@ -143,7 +172,7 @@ module Mongo
143
172
  while(cursor_id != 0) do
144
173
  receive_header(sock, cursor_id, exhaust)
145
174
  number_received, cursor_id = receive_response_header(sock)
146
- new_docs, n = read_documents(number_received, sock)
175
+ new_docs, n = read_documents(number_received, sock, opts)
147
176
  docs += new_docs
148
177
  num_received += n
149
178
  end
@@ -152,7 +181,7 @@ module Mongo
152
181
  else
153
182
  receive_header(sock, cursor_id, exhaust)
154
183
  number_received, cursor_id = receive_response_header(sock)
155
- docs, num_received = read_documents(number_received, sock)
184
+ docs, num_received = read_documents(number_received, sock, opts)
156
185
 
157
186
  return [docs, num_received, cursor_id]
158
187
  end
@@ -194,11 +223,11 @@ module Mongo
194
223
  raise Mongo::OperationFailure, "Query response returned CURSOR_NOT_FOUND. " +
195
224
  "Either an invalid cursor was specified, or the cursor may have timed out on the server."
196
225
  elsif flags & Mongo::Constants::REPLY_QUERY_FAILURE != 0
197
- # Getting odd failures when a exception is raised here.
226
+ # Mongo query reply failures are handled in Cursor#next.
198
227
  end
199
228
  end
200
229
 
201
- def read_documents(number_received, sock)
230
+ def read_documents(number_received, sock, opts)
202
231
  docs = []
203
232
  number_remaining = number_received
204
233
  while number_remaining > 0 do
@@ -206,7 +235,7 @@ module Mongo
206
235
  size = buf.unpack('V')[0]
207
236
  buf << receive_message_on_socket(size - 4, sock)
208
237
  number_remaining -= 1
209
- docs << BSON::BSON_CODER.deserialize(buf)
238
+ docs << BSON::BSON_CODER.deserialize(buf, opts)
210
239
  end
211
240
  [docs, number_received]
212
241
  end
@@ -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 #:nodoc:
2
16
 
3
17
  # Utility module to include when needing to convert certain types of
@@ -23,7 +37,7 @@ module Mongo #:nodoc:
23
37
  )
24
38
  else
25
39
  order_by = value.inject({}) do |memo, (key, direction)|
26
- memo[key.to_s] = sort_value(direction.to_s.downcase)
40
+ memo[key.to_s] = sort_value(direction)
27
41
  memo
28
42
  end
29
43
  end
@@ -31,7 +45,7 @@ module Mongo #:nodoc:
31
45
  end
32
46
 
33
47
  # Converts the supplied +Array+ to a +Hash+ to pass to mongo as
34
- # sorting parameters. The returned +Hash+ will vary depending
48
+ # sorting parameters. The returned +Hash+ will vary depending
35
49
  # on whether the passed +Array+ is one or two dimensional.
36
50
  #
37
51
  # Example:
@@ -73,7 +87,7 @@ module Mongo #:nodoc:
73
87
  { str => 1 }
74
88
  end
75
89
 
76
- # Converts the +String+, +Symbol+, or +Integer+ to the
90
+ # Converts the +String+, +Symbol+, or +Integer+ to the
77
91
  # corresponding sort value in MongoDB.
78
92
  #
79
93
  # Valid conversions (case-insensitive):
@@ -83,6 +97,7 @@ module Mongo #:nodoc:
83
97
  #
84
98
  # If the value is invalid then an error will be raised.
85
99
  def sort_value(value)
100
+ return value if value.is_a?(Hash)
86
101
  val = value.to_s.downcase
87
102
  return 1 if ASCENDING_CONVERSION.include?(val)
88
103
  return -1 if DESCENDING_CONVERSION.include?(val)
@@ -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
  #:nodoc:
2
16
  class Object
3
17
 
@@ -9,17 +23,6 @@ class Object
9
23
 
10
24
  end
11
25
 
12
- #:nodoc:
13
- module Enumerable
14
-
15
- #:nodoc:
16
- def each_with_object(memo)
17
- each { |element| yield(element, memo) }
18
- memo
19
- end unless [].respond_to?(:each_with_object)
20
-
21
- end
22
-
23
26
  #:nodoc:
24
27
  class Hash
25
28
 
@@ -27,7 +30,7 @@ class Hash
27
30
  def assert_valid_keys(*valid_keys)
28
31
  unknown_keys = keys - [valid_keys].flatten
29
32
  raise(ArgumentError, "Unknown key(s): #{unknown_keys.join(", ")}") unless unknown_keys.empty?
30
- end
33
+ end unless instance_methods.include?(:assert_valid_keys)
31
34
 
32
35
  end
33
36
 
@@ -65,23 +68,3 @@ class Class
65
68
  end
66
69
  end
67
70
  end
68
-
69
- # Fix a bug in the interaction of
70
- # mutexes and timeouts in Ruby 1.9.
71
- # See https://jira.mongodb.org/browse/RUBY-364 for details.
72
- if RUBY_VERSION > '1.9'
73
- class Mutex
74
- def lock_with_hack
75
- lock_without_hack
76
- rescue ThreadError => e
77
- if e.message != "deadlock; recursive locking"
78
- raise
79
- else
80
- unlock
81
- lock_without_hack
82
- end
83
- end
84
- alias_method :lock_without_hack, :lock
85
- alias_method :lock, :lock_with_hack
86
- end
87
- end
@@ -1,4 +1,19 @@
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
16
+
2
17
  # Simple class for comparing server versions.
3
18
  class ServerVersion
4
19
  include Comparable
@@ -1,4 +1,16 @@
1
- require 'digest/md5'
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.
2
14
 
3
15
  module Mongo
4
16
  module Support
@@ -6,44 +18,6 @@ module Mongo
6
18
  include Mongo::Conversions
7
19
  extend self
8
20
 
9
- # Commands that may be sent to replica-set secondaries, depending on
10
- # read preference and tags. All other commands are always run on the primary.
11
- SECONDARY_OK_COMMANDS = [
12
- 'group',
13
- 'aggregate',
14
- 'collstats',
15
- 'dbstats',
16
- 'count',
17
- 'distinct',
18
- 'geonear',
19
- 'geosearch',
20
- 'geowalk',
21
- 'mapreduce',
22
- 'replsetgetstatus',
23
- 'ismaster',
24
- ]
25
-
26
- # Generate an MD5 for authentication.
27
- #
28
- # @param [String] username
29
- # @param [String] password
30
- # @param [String] nonce
31
- #
32
- # @return [String] a key for db authentication.
33
- def auth_key(username, password, nonce)
34
- Digest::MD5.hexdigest("#{nonce}#{username}#{hash_password(username, password)}")
35
- end
36
-
37
- # Return a hashed password for auth.
38
- #
39
- # @param [String] username
40
- # @param [String] plaintext
41
- #
42
- # @return [String]
43
- def hash_password(username, plaintext)
44
- Digest::MD5.hexdigest("#{username}:mongo:#{plaintext}")
45
- end
46
-
47
21
  def validate_db_name(db_name)
48
22
  unless [String, Symbol].include?(db_name.class)
49
23
  raise TypeError, "db_name must be a string or symbol"
@@ -58,19 +32,6 @@ module Mongo
58
32
  db_name
59
33
  end
60
34
 
61
- def secondary_ok?(selector)
62
- command = selector.keys.first.to_s.downcase
63
-
64
- if command == 'mapreduce'
65
- out = selector.select { |k, v| k.to_s.downcase == 'out' }.first.last
66
- # mongo looks at the first key in the out object, and doesn't
67
- # look at the value
68
- out.is_a?(Hash) && out.keys.first.to_s.downcase == 'inline' ? true : false
69
- else
70
- SECONDARY_OK_COMMANDS.member?(command)
71
- end
72
- end
73
-
74
35
  def format_order_clause(order)
75
36
  case order
76
37
  when Hash, BSON::OrderedHash then hash_as_sort_parameters(order)
@@ -87,8 +48,13 @@ module Mongo
87
48
  pairs = [ seeds ] if pairs.last.is_a?(Fixnum)
88
49
  pairs = pairs.collect do |hostport|
89
50
  if hostport.is_a?(String)
90
- host, port = hostport.split(':')
91
- [ host, port && port.to_i || MongoClient::DEFAULT_PORT ]
51
+ if hostport[0,1] == '['
52
+ host, port = hostport.split(']:') << MongoClient::DEFAULT_PORT
53
+ host = host.end_with?(']') ? host[1...-1] : host[1..-1]
54
+ else
55
+ host, port = hostport.split(':') << MongoClient::DEFAULT_PORT
56
+ end
57
+ [ host, port.to_i ]
92
58
  else
93
59
  hostport
94
60
  end
@@ -107,7 +73,8 @@ module Mongo
107
73
  #
108
74
  # @return [Boolean] true if the 'ok' key is either 1 or *true*.
109
75
  def ok?(doc)
110
- doc['ok'] == 1.0 || doc['ok'] == true
76
+ ok = doc['ok']
77
+ ok == 1 || ok == 1.0 || ok == true
111
78
  end
112
79
  end
113
80
  end
@@ -0,0 +1,25 @@
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
+ #:nodoc:
16
+ module Mongo
17
+ module ThreadLocalVariableManager
18
+ def thread_local
19
+ Thread.current[:mongo_thread_locals] ||= Hash.new do |hash, key|
20
+ hash[key] = Hash.new unless hash.key? key
21
+ hash[key]
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,19 @@
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/utils/conversions'
16
+ require 'mongo/utils/core_ext'
17
+ require 'mongo/utils/server_version'
18
+ require 'mongo/utils/support'
19
+ require 'mongo/utils/thread_local_variable_manager'