mongo 1.3.0 → 1.12.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (185) hide show
  1. checksums.yaml +7 -0
  2. checksums.yaml.gz.sig +0 -0
  3. data/{LICENSE.txt → LICENSE} +1 -1
  4. data/README.md +122 -271
  5. data/Rakefile +25 -209
  6. data/VERSION +1 -0
  7. data/bin/mongo_console +31 -9
  8. data/lib/mongo/bulk_write_collection_view.rb +387 -0
  9. data/lib/mongo/collection.rb +576 -269
  10. data/lib/mongo/collection_writer.rb +364 -0
  11. data/lib/mongo/connection/node.rb +249 -0
  12. data/lib/mongo/connection/pool.rb +340 -0
  13. data/lib/mongo/connection/pool_manager.rb +320 -0
  14. data/lib/mongo/connection/sharding_pool_manager.rb +67 -0
  15. data/lib/mongo/connection/socket/socket_util.rb +37 -0
  16. data/lib/mongo/connection/socket/ssl_socket.rb +95 -0
  17. data/lib/mongo/connection/socket/tcp_socket.rb +87 -0
  18. data/lib/mongo/connection/socket/unix_socket.rb +39 -0
  19. data/lib/mongo/connection/socket.rb +18 -0
  20. data/lib/mongo/connection.rb +7 -875
  21. data/lib/mongo/cursor.rb +403 -117
  22. data/lib/mongo/db.rb +444 -243
  23. data/lib/mongo/exception.rb +145 -0
  24. data/lib/mongo/functional/authentication.rb +455 -0
  25. data/lib/mongo/functional/logging.rb +85 -0
  26. data/lib/mongo/functional/read_preference.rb +183 -0
  27. data/lib/mongo/functional/scram.rb +556 -0
  28. data/lib/mongo/functional/uri_parser.rb +409 -0
  29. data/lib/mongo/functional/write_concern.rb +66 -0
  30. data/lib/mongo/functional.rb +20 -0
  31. data/lib/mongo/gridfs/grid.rb +30 -24
  32. data/lib/mongo/gridfs/grid_ext.rb +6 -10
  33. data/lib/mongo/gridfs/grid_file_system.rb +38 -20
  34. data/lib/mongo/gridfs/grid_io.rb +84 -75
  35. data/lib/mongo/gridfs.rb +18 -0
  36. data/lib/mongo/legacy.rb +140 -0
  37. data/lib/mongo/mongo_client.rb +697 -0
  38. data/lib/mongo/mongo_replica_set_client.rb +535 -0
  39. data/lib/mongo/mongo_sharded_client.rb +159 -0
  40. data/lib/mongo/networking.rb +372 -0
  41. data/lib/mongo/{util → utils}/conversions.rb +29 -8
  42. data/lib/mongo/{util → utils}/core_ext.rb +28 -18
  43. data/lib/mongo/{util → utils}/server_version.rb +4 -6
  44. data/lib/mongo/{util → utils}/support.rb +29 -31
  45. data/lib/mongo/utils/thread_local_variable_manager.rb +25 -0
  46. data/lib/mongo/utils.rb +19 -0
  47. data/lib/mongo.rb +51 -50
  48. data/mongo.gemspec +29 -32
  49. data/test/functional/authentication_test.rb +39 -0
  50. data/test/functional/bulk_api_stress_test.rb +133 -0
  51. data/test/functional/bulk_write_collection_view_test.rb +1198 -0
  52. data/test/functional/client_test.rb +627 -0
  53. data/test/functional/collection_test.rb +2175 -0
  54. data/test/functional/collection_writer_test.rb +83 -0
  55. data/test/{conversions_test.rb → functional/conversions_test.rb} +47 -3
  56. data/test/functional/cursor_fail_test.rb +57 -0
  57. data/test/functional/cursor_message_test.rb +56 -0
  58. data/test/functional/cursor_test.rb +683 -0
  59. data/test/functional/db_api_test.rb +835 -0
  60. data/test/functional/db_connection_test.rb +25 -0
  61. data/test/functional/db_test.rb +348 -0
  62. data/test/functional/grid_file_system_test.rb +285 -0
  63. data/test/{grid_io_test.rb → functional/grid_io_test.rb} +72 -11
  64. data/test/{grid_test.rb → functional/grid_test.rb} +88 -15
  65. data/test/functional/pool_test.rb +136 -0
  66. data/test/functional/safe_test.rb +98 -0
  67. data/test/functional/ssl_test.rb +29 -0
  68. data/test/functional/support_test.rb +62 -0
  69. data/test/functional/timeout_test.rb +60 -0
  70. data/test/functional/uri_test.rb +446 -0
  71. data/test/functional/write_concern_test.rb +118 -0
  72. data/test/helpers/general.rb +50 -0
  73. data/test/helpers/test_unit.rb +476 -0
  74. data/test/replica_set/authentication_test.rb +37 -0
  75. data/test/replica_set/basic_test.rb +189 -0
  76. data/test/replica_set/client_test.rb +393 -0
  77. data/test/replica_set/connection_test.rb +138 -0
  78. data/test/replica_set/count_test.rb +66 -0
  79. data/test/replica_set/cursor_test.rb +220 -0
  80. data/test/replica_set/insert_test.rb +157 -0
  81. data/test/replica_set/max_values_test.rb +151 -0
  82. data/test/replica_set/pinning_test.rb +105 -0
  83. data/test/replica_set/query_test.rb +73 -0
  84. data/test/replica_set/read_preference_test.rb +219 -0
  85. data/test/replica_set/refresh_test.rb +211 -0
  86. data/test/replica_set/replication_ack_test.rb +95 -0
  87. data/test/replica_set/ssl_test.rb +32 -0
  88. data/test/sharded_cluster/basic_test.rb +203 -0
  89. data/test/shared/authentication/basic_auth_shared.rb +260 -0
  90. data/test/shared/authentication/bulk_api_auth_shared.rb +249 -0
  91. data/test/shared/authentication/gssapi_shared.rb +176 -0
  92. data/test/shared/authentication/sasl_plain_shared.rb +96 -0
  93. data/test/shared/authentication/scram_shared.rb +92 -0
  94. data/test/shared/ssl_shared.rb +235 -0
  95. data/test/test_helper.rb +53 -94
  96. data/test/threading/basic_test.rb +120 -0
  97. data/test/tools/mongo_config.rb +708 -0
  98. data/test/tools/mongo_config_test.rb +160 -0
  99. data/test/unit/client_test.rb +381 -0
  100. data/test/unit/collection_test.rb +89 -53
  101. data/test/unit/connection_test.rb +282 -32
  102. data/test/unit/cursor_test.rb +206 -8
  103. data/test/unit/db_test.rb +55 -13
  104. data/test/unit/grid_test.rb +43 -16
  105. data/test/unit/mongo_sharded_client_test.rb +48 -0
  106. data/test/unit/node_test.rb +93 -0
  107. data/test/unit/pool_manager_test.rb +111 -0
  108. data/test/unit/read_pref_test.rb +406 -0
  109. data/test/unit/read_test.rb +159 -0
  110. data/test/unit/safe_test.rb +69 -36
  111. data/test/unit/sharding_pool_manager_test.rb +84 -0
  112. data/test/unit/write_concern_test.rb +175 -0
  113. data.tar.gz.sig +3 -0
  114. metadata +227 -216
  115. metadata.gz.sig +0 -0
  116. data/docs/CREDITS.md +0 -123
  117. data/docs/FAQ.md +0 -116
  118. data/docs/GridFS.md +0 -158
  119. data/docs/HISTORY.md +0 -244
  120. data/docs/RELEASES.md +0 -33
  121. data/docs/REPLICA_SETS.md +0 -72
  122. data/docs/TUTORIAL.md +0 -247
  123. data/docs/WRITE_CONCERN.md +0 -28
  124. data/lib/mongo/exceptions.rb +0 -71
  125. data/lib/mongo/gridfs/grid_io_fix.rb +0 -38
  126. data/lib/mongo/repl_set_connection.rb +0 -342
  127. data/lib/mongo/test.rb +0 -20
  128. data/lib/mongo/util/pool.rb +0 -177
  129. data/lib/mongo/util/uri_parser.rb +0 -185
  130. data/test/async/collection_test.rb +0 -224
  131. data/test/async/connection_test.rb +0 -24
  132. data/test/async/cursor_test.rb +0 -162
  133. data/test/async/worker_pool_test.rb +0 -99
  134. data/test/auxillary/1.4_features.rb +0 -166
  135. data/test/auxillary/authentication_test.rb +0 -68
  136. data/test/auxillary/autoreconnect_test.rb +0 -41
  137. data/test/auxillary/fork_test.rb +0 -30
  138. data/test/auxillary/repl_set_auth_test.rb +0 -58
  139. data/test/auxillary/slave_connection_test.rb +0 -36
  140. data/test/auxillary/threaded_authentication_test.rb +0 -101
  141. data/test/bson/binary_test.rb +0 -15
  142. data/test/bson/bson_test.rb +0 -649
  143. data/test/bson/byte_buffer_test.rb +0 -208
  144. data/test/bson/hash_with_indifferent_access_test.rb +0 -38
  145. data/test/bson/json_test.rb +0 -17
  146. data/test/bson/object_id_test.rb +0 -154
  147. data/test/bson/ordered_hash_test.rb +0 -204
  148. data/test/bson/timestamp_test.rb +0 -24
  149. data/test/collection_test.rb +0 -910
  150. data/test/connection_test.rb +0 -309
  151. data/test/cursor_fail_test.rb +0 -75
  152. data/test/cursor_message_test.rb +0 -43
  153. data/test/cursor_test.rb +0 -483
  154. data/test/db_api_test.rb +0 -726
  155. data/test/db_connection_test.rb +0 -15
  156. data/test/db_test.rb +0 -287
  157. data/test/grid_file_system_test.rb +0 -243
  158. data/test/load/resque/load.rb +0 -21
  159. data/test/load/resque/processor.rb +0 -26
  160. data/test/load/thin/load.rb +0 -24
  161. data/test/load/unicorn/load.rb +0 -23
  162. data/test/load/unicorn/unicorn.rb +0 -29
  163. data/test/replica_sets/connect_test.rb +0 -94
  164. data/test/replica_sets/connection_string_test.rb +0 -32
  165. data/test/replica_sets/count_test.rb +0 -35
  166. data/test/replica_sets/insert_test.rb +0 -53
  167. data/test/replica_sets/pooled_insert_test.rb +0 -55
  168. data/test/replica_sets/query_secondaries.rb +0 -96
  169. data/test/replica_sets/query_test.rb +0 -51
  170. data/test/replica_sets/replication_ack_test.rb +0 -66
  171. data/test/replica_sets/rs_test_helper.rb +0 -27
  172. data/test/safe_test.rb +0 -68
  173. data/test/support/hash_with_indifferent_access.rb +0 -186
  174. data/test/support/keys.rb +0 -45
  175. data/test/support_test.rb +0 -18
  176. data/test/threading/threading_with_large_pool_test.rb +0 -90
  177. data/test/threading_test.rb +0 -87
  178. data/test/tools/auth_repl_set_manager.rb +0 -14
  179. data/test/tools/load.rb +0 -58
  180. data/test/tools/repl_set_manager.rb +0 -266
  181. data/test/tools/sharding_manager.rb +0 -202
  182. data/test/tools/test.rb +0 -4
  183. data/test/unit/pool_test.rb +0 -9
  184. data/test/unit/repl_set_connection_test.rb +0 -59
  185. data/test/uri_test.rb +0 -91
@@ -0,0 +1,67 @@
1
+ # Copyright (C) 2009-2013 MongoDB, Inc.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ module Mongo
16
+ class ShardingPoolManager < PoolManager
17
+ def inspect
18
+ "<Mongo::ShardingPoolManager:0x#{self.object_id.to_s(16)} @seeds=#{@seeds}>"
19
+ end
20
+
21
+ # "Best" should be the member with the fastest ping time
22
+ # but connect/connect_to_members reinitializes @members
23
+ def best(members)
24
+ Array(members.first)
25
+ end
26
+
27
+ def connect
28
+ @connect_mutex.synchronize do
29
+ begin
30
+ thread_local[:locks][:connecting_manager] = true
31
+ @refresh_required = false
32
+ disconnect_old_members
33
+ connect_to_members
34
+ initialize_pools best(@members)
35
+ update_max_sizes
36
+ @seeds = discovered_seeds
37
+ ensure
38
+ thread_local[:locks][:connecting_manager] = false
39
+ end
40
+ end
41
+ end
42
+
43
+ # Checks that each node is healthy (via check_is_master) and that each
44
+ # node is in fact a mongos. If either criteria are not true, a refresh is
45
+ # set to be triggered and close() is called on the node.
46
+ #
47
+ # @return [Boolean] indicating if a refresh is required.
48
+ def check_connection_health
49
+ @refresh_required = false
50
+ @members.each do |member|
51
+ begin
52
+ config = @client.check_is_master([member.host, member.port])
53
+ unless config && config.has_key?('msg')
54
+ @refresh_required = true
55
+ member.close
56
+ end
57
+ rescue OperationTimeout
58
+ @refresh_required = true
59
+ member.close
60
+ end
61
+ break if @refresh_required
62
+ end
63
+ @refresh_required
64
+ end
65
+
66
+ end
67
+ end
@@ -0,0 +1,37 @@
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 'socket'
16
+ require 'timeout'
17
+
18
+ module SocketUtil
19
+
20
+ attr_accessor :pool, :pid, :auths
21
+
22
+ def checkout
23
+ @pool.checkout if @pool
24
+ end
25
+
26
+ def checkin
27
+ @pool.checkin(self) if @pool
28
+ end
29
+
30
+ def close
31
+ @socket.close unless closed?
32
+ end
33
+
34
+ def closed?
35
+ @socket.closed?
36
+ end
37
+ end
@@ -0,0 +1,95 @@
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 'openssl'
16
+
17
+ module Mongo
18
+
19
+ # A basic wrapper over Ruby's SSLSocket that initiates
20
+ # a TCP connection over SSL and then provides an basic interface
21
+ # mirroring Ruby's TCPSocket, vis., TCPSocket#send and TCPSocket#read.
22
+ class SSLSocket
23
+ include SocketUtil
24
+
25
+ def initialize(host, port, op_timeout=nil, connect_timeout=nil, opts={})
26
+ @op_timeout = op_timeout
27
+ @connect_timeout = connect_timeout
28
+ @pid = Process.pid
29
+ @auths = Set.new
30
+
31
+ @tcp_socket = ::TCPSocket.new(host, port)
32
+ @tcp_socket.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1)
33
+
34
+ @context = OpenSSL::SSL::SSLContext.new
35
+
36
+ if opts[:cert]
37
+ @context.cert = OpenSSL::X509::Certificate.new(File.open(opts[:cert]))
38
+ end
39
+
40
+ if opts[:key]
41
+ if opts[:key_pass_phrase]
42
+ @context.key = OpenSSL::PKey::RSA.new(File.open(opts[:key]), opts[:key_pass_phrase])
43
+ else
44
+ @context.key = OpenSSL::PKey::RSA.new(File.open(opts[:key]))
45
+ end
46
+ end
47
+
48
+ if opts[:verify]
49
+ @context.ca_file = opts[:ca_cert]
50
+ @context.verify_mode = OpenSSL::SSL::VERIFY_PEER
51
+ end
52
+
53
+ begin
54
+ @socket = OpenSSL::SSL::SSLSocket.new(@tcp_socket, @context)
55
+ @socket.sync_close = true
56
+ connect
57
+ rescue OpenSSL::SSL::SSLError
58
+ raise ConnectionFailure, "SSL handshake failed. MongoDB may " +
59
+ "not be configured with SSL support."
60
+ end
61
+
62
+ if opts[:verify]
63
+ unless OpenSSL::SSL.verify_certificate_identity(@socket.peer_cert, host)
64
+ raise ConnectionFailure, "SSL handshake failed. Hostname mismatch."
65
+ end
66
+ end
67
+
68
+ self
69
+ end
70
+
71
+ def connect
72
+ if @connect_timeout
73
+ Timeout::timeout(@connect_timeout, ConnectionTimeoutError) do
74
+ @socket.connect
75
+ end
76
+ else
77
+ @socket.connect
78
+ end
79
+ end
80
+
81
+ def send(data)
82
+ @socket.syswrite(data)
83
+ end
84
+
85
+ def read(length, buffer)
86
+ if @op_timeout
87
+ Timeout::timeout(@op_timeout, OperationTimeout) do
88
+ @socket.sysread(length, buffer)
89
+ end
90
+ else
91
+ @socket.sysread(length, buffer)
92
+ end
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,87 @@
1
+ # Copyright (C) 2009-2013 MongoDB, Inc.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ module Mongo
16
+ # Wrapper class for Socket
17
+ #
18
+ # Emulates TCPSocket with operation and connection timeout
19
+ # sans Timeout::timeout
20
+ #
21
+ class TCPSocket
22
+ include SocketUtil
23
+
24
+ def initialize(host, port, op_timeout=nil, connect_timeout=nil, opts={})
25
+ @op_timeout = op_timeout
26
+ @connect_timeout = connect_timeout
27
+ @pid = Process.pid
28
+ @auths = Set.new
29
+
30
+ @socket = handle_connect(host, port)
31
+ end
32
+
33
+ def handle_connect(host, port)
34
+ error = nil
35
+ # Following python's lead (see PYTHON-356)
36
+ family = host == 'localhost' ? Socket::AF_INET : Socket::AF_UNSPEC
37
+ addr_info = Socket.getaddrinfo(host, nil, family, Socket::SOCK_STREAM)
38
+ error = nil
39
+ addr_info.each do |info|
40
+ begin
41
+ sock = Socket.new(info[4], Socket::SOCK_STREAM, 0)
42
+ sock.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1)
43
+ socket_address = Socket.pack_sockaddr_in(port, info[3])
44
+ connect(sock, socket_address)
45
+ return sock
46
+ rescue IOError, SystemCallError => e
47
+ error = e
48
+ sock.close if sock
49
+ end
50
+ end
51
+ raise error
52
+ end
53
+
54
+ def connect(socket, socket_address)
55
+ if @connect_timeout
56
+ Timeout::timeout(@connect_timeout, ConnectionTimeoutError) do
57
+ socket.connect(socket_address)
58
+ end
59
+ else
60
+ socket.connect(socket_address)
61
+ end
62
+ end
63
+
64
+ def send(data)
65
+ @socket.write(data)
66
+ end
67
+
68
+ def read(maxlen, buffer)
69
+ # Block on data to read for @op_timeout seconds
70
+ begin
71
+ ready = IO.select([@socket], nil, [@socket], @op_timeout)
72
+ unless ready
73
+ raise OperationTimeout
74
+ end
75
+ rescue IOError
76
+ raise ConnectionFailure
77
+ end
78
+
79
+ # Read data from socket
80
+ begin
81
+ @socket.sysread(maxlen, buffer)
82
+ rescue SystemCallError, IOError => ex
83
+ raise ConnectionFailure, ex
84
+ end
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,39 @@
1
+ # Copyright (C) 2009-2013 MongoDB, Inc.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ module Mongo
16
+
17
+ attr_accessor :auths
18
+
19
+ # Wrapper class for Socket
20
+ #
21
+ # Emulates UNIXSocket with operation and connection timeout
22
+ # sans Timeout::timeout
23
+ #
24
+ class UNIXSocket < TCPSocket
25
+ def initialize(socket_path, port=:socket, op_timeout=nil, connect_timeout=nil, opts={})
26
+ @op_timeout = op_timeout
27
+ @connect_timeout = connect_timeout
28
+ @pid = Process.pid
29
+ @auths = Set.new
30
+
31
+ @address = socket_path
32
+ @port = :socket # purposely override input
33
+
34
+ @socket_address = Socket.pack_sockaddr_un(@address)
35
+ @socket = Socket.new(Socket::AF_UNIX, Socket::SOCK_STREAM, 0)
36
+ connect(@socket, @socket_address)
37
+ end
38
+ end
39
+ end
@@ -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/connection/socket/socket_util.rb'
16
+ require 'mongo/connection/socket/ssl_socket.rb'
17
+ require 'mongo/connection/socket/tcp_socket.rb'
18
+ require 'mongo/connection/socket/unix_socket.rb'