mongo 1.7.1 → 1.8.0

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 (138) hide show
  1. data/{LICENSE.txt → LICENSE} +0 -0
  2. data/README.md +124 -111
  3. data/Rakefile +9 -325
  4. data/VERSION +1 -0
  5. data/bin/mongo_console +4 -4
  6. data/examples/admin.rb +43 -0
  7. data/examples/capped.rb +22 -0
  8. data/examples/cursor.rb +48 -0
  9. data/examples/gridfs.rb +44 -0
  10. data/examples/index_test.rb +126 -0
  11. data/examples/info.rb +31 -0
  12. data/examples/queries.rb +74 -0
  13. data/examples/replica_set.rb +26 -0
  14. data/examples/simple.rb +25 -0
  15. data/examples/strict.rb +35 -0
  16. data/examples/types.rb +36 -0
  17. data/{test/load → examples/web}/thin/load.rb +3 -1
  18. data/{test/load → examples/web}/unicorn/load.rb +5 -3
  19. data/lib/mongo.rb +8 -10
  20. data/lib/mongo/collection.rb +134 -114
  21. data/lib/mongo/cursor.rb +21 -14
  22. data/lib/mongo/db.rb +30 -28
  23. data/lib/mongo/exceptions.rb +1 -1
  24. data/lib/mongo/gridfs/grid.rb +8 -7
  25. data/lib/mongo/gridfs/grid_ext.rb +1 -1
  26. data/lib/mongo/gridfs/grid_file_system.rb +6 -5
  27. data/lib/mongo/gridfs/grid_io.rb +22 -19
  28. data/lib/mongo/legacy.rb +82 -0
  29. data/lib/mongo/{connection.rb → mongo_client.rb} +82 -61
  30. data/lib/mongo/{repl_set_connection.rb → mongo_replica_set_client.rb} +54 -39
  31. data/lib/mongo/{sharded_connection.rb → mongo_sharded_client.rb} +9 -9
  32. data/lib/mongo/networking.rb +25 -20
  33. data/lib/mongo/util/conversions.rb +1 -1
  34. data/lib/mongo/util/core_ext.rb +1 -1
  35. data/lib/mongo/util/logging.rb +20 -4
  36. data/lib/mongo/util/node.rb +16 -16
  37. data/lib/mongo/util/pool.rb +56 -27
  38. data/lib/mongo/util/pool_manager.rb +28 -27
  39. data/lib/mongo/util/server_version.rb +1 -1
  40. data/lib/mongo/util/sharding_pool_manager.rb +8 -8
  41. data/lib/mongo/util/ssl_socket.rb +1 -5
  42. data/lib/mongo/util/support.rb +24 -8
  43. data/lib/mongo/util/tcp_socket.rb +0 -4
  44. data/lib/mongo/util/uri_parser.rb +54 -38
  45. data/lib/mongo/util/write_concern.rb +67 -0
  46. data/mongo.gemspec +21 -32
  47. data/test/auxillary/{1.4_features.rb → 1.4_feature_test.rb} +4 -5
  48. data/test/auxillary/authentication_test.rb +18 -20
  49. data/test/auxillary/autoreconnect_test.rb +3 -5
  50. data/test/auxillary/fork_test.rb +5 -7
  51. data/test/auxillary/repl_set_auth_test.rb +13 -15
  52. data/test/auxillary/slave_connection_test.rb +8 -7
  53. data/test/auxillary/threaded_authentication_test.rb +15 -17
  54. data/test/bson/binary_test.rb +1 -1
  55. data/test/bson/bson_test.rb +60 -36
  56. data/test/bson/byte_buffer_test.rb +1 -1
  57. data/test/bson/hash_with_indifferent_access_test.rb +2 -2
  58. data/test/bson/json_test.rb +1 -2
  59. data/test/bson/object_id_test.rb +1 -2
  60. data/test/bson/ordered_hash_test.rb +1 -1
  61. data/test/bson/timestamp_test.rb +1 -1
  62. data/test/{collection_test.rb → functional/collection_test.rb} +57 -57
  63. data/test/{connection_test.rb → functional/connection_test.rb} +75 -89
  64. data/test/{conversions_test.rb → functional/conversions_test.rb} +1 -1
  65. data/test/{cursor_fail_test.rb → functional/cursor_fail_test.rb} +3 -29
  66. data/test/{cursor_message_test.rb → functional/cursor_message_test.rb} +1 -1
  67. data/test/{cursor_test.rb → functional/cursor_test.rb} +5 -1
  68. data/test/{db_api_test.rb → functional/db_api_test.rb} +8 -9
  69. data/test/{db_connection_test.rb → functional/db_connection_test.rb} +3 -5
  70. data/test/{db_test.rb → functional/db_test.rb} +13 -13
  71. data/test/{grid_file_system_test.rb → functional/grid_file_system_test.rb} +2 -2
  72. data/test/{grid_io_test.rb → functional/grid_io_test.rb} +6 -6
  73. data/test/{grid_test.rb → functional/grid_test.rb} +4 -10
  74. data/test/{pool_test.rb → functional/pool_test.rb} +1 -1
  75. data/test/functional/safe_test.rb +84 -0
  76. data/test/{support_test.rb → functional/support_test.rb} +1 -1
  77. data/test/{threading_test.rb → functional/threading_test.rb} +9 -9
  78. data/test/{timeout_test.rb → functional/timeout_test.rb} +1 -1
  79. data/test/{uri_test.rb → functional/uri_test.rb} +1 -1
  80. data/test/functional/write_concern_test.rb +104 -0
  81. data/test/replica_set/basic_test.rb +139 -0
  82. data/test/replica_set/client_test.rb +255 -0
  83. data/test/replica_set/complex_connect_test.rb +62 -0
  84. data/test/replica_set/connection_test.rb +255 -0
  85. data/test/{replica_sets → replica_set}/count_test.rb +17 -14
  86. data/test/replica_set/cursor_test.rb +75 -0
  87. data/test/{replica_sets → replica_set}/insert_test.rb +19 -16
  88. data/test/replica_set/query_test.rb +64 -0
  89. data/test/replica_set/refresh_test.rb +153 -0
  90. data/test/{replica_sets → replica_set}/replication_ack_test.rb +21 -17
  91. data/test/sharded_cluster/basic_test.rb +31 -50
  92. data/test/support/hash_with_indifferent_access.rb +1 -1
  93. data/test/test_helper.rb +56 -9
  94. data/test/threading/threading_with_large_pool_test.rb +8 -8
  95. data/test/tools/mongo_config.rb +270 -58
  96. data/test/tools/mongo_config_test.rb +146 -0
  97. data/test/unit/client_test.rb +230 -0
  98. data/test/unit/collection_test.rb +45 -32
  99. data/test/unit/connection_test.rb +82 -74
  100. data/test/unit/cursor_test.rb +14 -6
  101. data/test/unit/db_test.rb +8 -8
  102. data/test/unit/grid_test.rb +11 -11
  103. data/test/unit/node_test.rb +24 -24
  104. data/test/unit/pool_manager_test.rb +13 -13
  105. data/test/unit/pool_test.rb +1 -1
  106. data/test/unit/read_test.rb +21 -26
  107. data/test/unit/safe_test.rb +52 -33
  108. data/test/unit/util_test.rb +55 -0
  109. data/test/unit/write_concern_test.rb +161 -0
  110. metadata +158 -171
  111. data/docs/CREDITS.md +0 -123
  112. data/docs/FAQ.md +0 -116
  113. data/docs/GRID_FS.md +0 -158
  114. data/docs/HISTORY.md +0 -392
  115. data/docs/READ_PREFERENCE.md +0 -99
  116. data/docs/RELEASES.md +0 -54
  117. data/docs/REPLICA_SETS.md +0 -113
  118. data/docs/TAILABLE_CURSORS.md +0 -51
  119. data/docs/TUTORIAL.md +0 -356
  120. data/docs/WRITE_CONCERN.md +0 -31
  121. data/lib/mongo/gridfs/grid_io_fix.rb +0 -38
  122. data/lib/mongo/version.rb +0 -3
  123. data/test/bson/test_helper.rb +0 -30
  124. data/test/replica_sets/basic_test.rb +0 -119
  125. data/test/replica_sets/complex_connect_test.rb +0 -57
  126. data/test/replica_sets/complex_read_preference_test.rb +0 -237
  127. data/test/replica_sets/connect_test.rb +0 -156
  128. data/test/replica_sets/cursor_test.rb +0 -70
  129. data/test/replica_sets/pooled_insert_test.rb +0 -57
  130. data/test/replica_sets/query_test.rb +0 -50
  131. data/test/replica_sets/read_preference_test.rb +0 -234
  132. data/test/replica_sets/refresh_test.rb +0 -156
  133. data/test/replica_sets/refresh_with_threads_test.rb +0 -60
  134. data/test/replica_sets/rs_test_helper.rb +0 -39
  135. data/test/safe_test.rb +0 -68
  136. data/test/sharded_cluster/mongo_config_test.rb +0 -126
  137. data/test/sharded_cluster/sc_test_helper.rb +0 -39
  138. data/test/tools/repl_set_manager.rb +0 -418
@@ -1,7 +1,7 @@
1
1
  module Mongo
2
2
  class PoolManager
3
3
 
4
- attr_reader :connection, :arbiters, :primary, :secondaries, :primary_pool,
4
+ attr_reader :client, :arbiters, :primary, :secondaries, :primary_pool,
5
5
  :secondary_pool, :secondary_pools, :hosts, :nodes, :members, :seeds,
6
6
  :max_bson_size
7
7
 
@@ -14,10 +14,10 @@ module Mongo
14
14
  # the user may pass an additional list of seeds nodes discovered in real
15
15
  # time. The union of these lists will be used when attempting to connect,
16
16
  # with the newly-discovered nodes being used first.
17
- def initialize(connection, seeds=[])
18
- @pinned_pools = {}
19
- @connection = connection
20
- @seeds = seeds
17
+ def initialize(client, seeds=[])
18
+ @pinned_pools = {}
19
+ @client = client
20
+ @seeds = seeds
21
21
  @previously_connected = false
22
22
  end
23
23
 
@@ -99,15 +99,16 @@ module Mongo
99
99
  read_pool.host_port
100
100
  end
101
101
 
102
- def read_pool(mode=@connection.read_preference, tags=@connection.tag_sets,
103
- acceptable_latency=@connection.acceptable_latency)
102
+ def read_pool(mode=@client.read_preference,
103
+ tags=@client.tag_sets,
104
+ acceptable_latency=@client.acceptable_latency)
104
105
 
105
106
  if mode == :primary && !tags.empty?
106
107
  raise MongoArgumentError, "Read preferecy :primary cannot be combined with tags"
107
108
  end
108
109
 
109
110
  pinned = @pinned_pools[Thread.current]
110
- if mode && pinned && select_pool([pinned], tags, acceptable_latency) && !pinned.closed?
111
+ if pinned && pinned.matches_mode(mode) && pinned.matches_tag_sets(tags) && pinned.up?
111
112
  pool = pinned
112
113
  else
113
114
  pool = case mode
@@ -159,18 +160,18 @@ module Mongo
159
160
  end
160
161
 
161
162
  def initialize_data
162
- @primary = nil
163
- @primary_pool = nil
164
- @read = nil
165
- @read_pool = nil
166
- @arbiters = []
167
- @secondaries = []
168
- @secondary_pool = nil
169
- @secondary_pools = []
170
- @hosts = Set.new
171
- @members = Set.new
163
+ @primary = nil
164
+ @primary_pool = nil
165
+ @read = nil
166
+ @read_pool = nil
167
+ @arbiters = []
168
+ @secondaries = []
169
+ @secondary_pool = nil
170
+ @secondary_pools = []
171
+ @hosts = Set.new
172
+ @members = Set.new
172
173
  @refresh_required = false
173
- @pinned_pools = {}
174
+ @pinned_pools = {}
174
175
  end
175
176
 
176
177
  # Connect to each member of the replica set
@@ -182,7 +183,7 @@ module Mongo
182
183
  seed = get_valid_seed_node
183
184
 
184
185
  seed.node_list.each do |host|
185
- node = Mongo::Node.new(self.connection, host)
186
+ node = Mongo::Node.new(self.client, host)
186
187
  if node.connect && node.set_config && node.healthy?
187
188
  members << node
188
189
  end
@@ -216,9 +217,9 @@ module Mongo
216
217
  def assign_primary(member)
217
218
  member.last_state = :primary
218
219
  @primary = member.host_port
219
- @primary_pool = Pool.new(self.connection, member.host, member.port,
220
- :size => self.connection.pool_size,
221
- :timeout => self.connection.pool_timeout,
220
+ @primary_pool = Pool.new(self.client, member.host, member.port,
221
+ :size => self.client.pool_size,
222
+ :timeout => self.client.pool_timeout,
222
223
  :node => member
223
224
  )
224
225
  end
@@ -226,9 +227,9 @@ module Mongo
226
227
  def assign_secondary(member)
227
228
  member.last_state = :secondary
228
229
  @secondaries << member.host_port
229
- pool = Pool.new(self.connection, member.host, member.port,
230
- :size => self.connection.pool_size,
231
- :timeout => self.connection.pool_timeout,
230
+ pool = Pool.new(self.client, member.host, member.port,
231
+ :size => self.client.pool_size,
232
+ :timeout => self.client.pool_timeout,
232
233
  :node => member
233
234
  )
234
235
  @secondary_pools << pool
@@ -268,7 +269,7 @@ module Mongo
268
269
  # If we don't get a response, raise an exception.
269
270
  def get_valid_seed_node
270
271
  @seeds.each do |seed|
271
- node = Mongo::Node.new(self.connection, seed)
272
+ node = Mongo::Node.new(self.client, seed)
272
273
  if !node.connect
273
274
  next
274
275
  elsif node.set_config && node.healthy?
@@ -1,7 +1,7 @@
1
1
  # encoding: UTF-8
2
2
 
3
3
  # --
4
- # Copyright (C) 2008-2011 10gen Inc.
4
+ # Copyright (C) 2008-2012 10gen Inc.
5
5
  #
6
6
  # Licensed under the Apache License, Version 2.0 (the "License");
7
7
  # you may not use this file except in compliance with the License.
@@ -3,15 +3,15 @@ module Mongo
3
3
  module ShardingNode
4
4
  def set_config
5
5
  begin
6
- @config = @connection['admin'].command({:ismaster => 1}, :socket => @socket)
6
+ @config = @client['admin'].command({:ismaster => 1}, :socket => @socket)
7
7
 
8
8
  # warning: instance variable @logger not initialized
9
9
  #if @config['msg'] && @logger
10
- # @connection.log(:warn, "#{config['msg']}")
10
+ # @client.log(:warn, "#{config['msg']}")
11
11
  #end
12
12
 
13
13
  rescue ConnectionFailure, OperationFailure, OperationTimeout, SocketError, SystemCallError, IOError => ex
14
- @connection.log(:warn, "Attempted connection to node #{host_string} raised " +
14
+ @client.log(:warn, "Attempted connection to node #{host_string} raised " +
15
15
  "#{ex.class}: #{ex.message}")
16
16
 
17
17
  # Socket may already be nil from issuing command
@@ -39,7 +39,7 @@ module Mongo
39
39
 
40
40
  class ShardingPoolManager < PoolManager
41
41
 
42
- attr_reader :connection, :primary, :primary_pool, :hosts, :nodes,
42
+ attr_reader :client, :primary, :primary_pool, :hosts, :nodes,
43
43
  :max_bson_size, :members
44
44
 
45
45
  # Create a new set of connection pools.
@@ -49,7 +49,7 @@ module Mongo
49
49
  # the user may pass an additional list of seeds nodes discovered in real
50
50
  # time. The union of these lists will be used when attempting to connect,
51
51
  # with the newly-discovered nodes being used first.
52
- def initialize(connection, seeds=[])
52
+ def initialize(client, seeds=[])
53
53
  super
54
54
  end
55
55
 
@@ -81,7 +81,7 @@ module Mongo
81
81
  # The config.mongos find can't be part of the connect call chain due to infinite recursion
82
82
  def check_connection_health
83
83
  begin
84
- seeds = @connection['config']['mongos'].find.to_a.map{|doc| doc['_id']}
84
+ seeds = @client['config']['mongos'].find.to_a.map{|doc| doc['_id']}
85
85
  if @seeds != seeds
86
86
  @seeds = seeds
87
87
  @refresh_required = true
@@ -102,7 +102,7 @@ module Mongo
102
102
  seed = get_valid_seed_node
103
103
 
104
104
  seed.node_list.each do |host|
105
- node = Mongo::Node.new(self.connection, host)
105
+ node = Mongo::Node.new(self.client, host)
106
106
  node.extend ShardingNode
107
107
  if node.connect && node.set_config
108
108
  members << node
@@ -124,7 +124,7 @@ module Mongo
124
124
  # If we don't get a response, raise an exception.
125
125
  def get_valid_seed_node
126
126
  @seeds.each do |seed|
127
- node = Mongo::Node.new(self.connection, seed)
127
+ node = Mongo::Node.new(self.client, seed)
128
128
  node.extend ShardingNode
129
129
  if !node.connect
130
130
  next
@@ -20,7 +20,7 @@ module Mongo
20
20
 
21
21
  @ssl = OpenSSL::SSL::SSLSocket.new(@socket)
22
22
  @ssl.sync_close = true
23
-
23
+
24
24
  connect
25
25
  end
26
26
 
@@ -48,10 +48,6 @@ module Mongo
48
48
  end
49
49
  end
50
50
 
51
- def setsockopt(key, value, n)
52
- @ssl.setsockopt(key, value, n)
53
- end
54
-
55
51
  def close
56
52
  @ssl.close
57
53
  end
@@ -1,7 +1,7 @@
1
1
  # encoding: UTF-8
2
2
 
3
3
  # --
4
- # Copyright (C) 2008-2011 10gen Inc.
4
+ # Copyright (C) 2008-2012 10gen Inc.
5
5
  #
6
6
  # Licensed under the Apache License, Version 2.0 (the "License");
7
7
  # you may not use this file except in compliance with the License.
@@ -20,15 +20,31 @@ require 'digest/md5'
20
20
 
21
21
  module Mongo
22
22
  module Support
23
+
23
24
  include Mongo::Conversions
24
25
  extend self
25
26
 
26
- READ_PREFERENCES = [:primary, :primary_preferred, :secondary, :secondary_preferred, :nearest]
27
+ READ_PREFERENCES = [
28
+ :primary,
29
+ :primary_preferred,
30
+ :secondary,
31
+ :secondary_preferred,
32
+ :nearest
33
+ ]
27
34
 
28
35
  # Commands that may be sent to replica-set secondaries, depending on
29
36
  # read preference and tags. All other commands are always run on the primary.
30
- SECONDARY_OK_COMMANDS = ['group', 'aggregate', 'collstats', 'dbstats', 'count', 'distinct',
31
- 'geonear', 'geosearch', 'geowalk']
37
+ SECONDARY_OK_COMMANDS = [
38
+ 'group',
39
+ 'aggregate',
40
+ 'collstats',
41
+ 'dbstats',
42
+ 'count',
43
+ 'distinct',
44
+ 'geonear',
45
+ 'geosearch',
46
+ 'geowalk'
47
+ ]
32
48
 
33
49
  # Generate an MD5 for authentication.
34
50
  #
@@ -69,10 +85,10 @@ module Mongo
69
85
  command = selector.keys.first.to_s.downcase
70
86
 
71
87
  if command == 'mapreduce'
72
- map_reduce = selector[command]
73
- if map_reduce && map_reduce.is_a?(Hash) && map_reduce.has_key?('out')
74
- map_reduce['out'] == 'inline' ? false : true
75
- end
88
+ out = selector.select { |k, v| k.to_s.downcase == 'out' }.first.last
89
+ # mongo looks at the first key in the out object, and doesn't
90
+ # look at the value
91
+ out.is_a?(Hash) && out.keys.first.to_s.downcase == 'inline' ? true : false
76
92
  else
77
93
  SECONDARY_OK_COMMANDS.member?(command)
78
94
  end
@@ -58,10 +58,6 @@ module Mongo
58
58
  end
59
59
  end
60
60
 
61
- def setsockopt(key, value, n)
62
- @socket.setsockopt(key, value, n)
63
- end
64
-
65
61
  def close
66
62
  @socket.close
67
63
  end
@@ -1,7 +1,7 @@
1
1
  # encoding: UTF-8
2
2
 
3
3
  # --
4
- # Copyright (C) 2008-2011 10gen Inc.
4
+ # Copyright (C) 2008-2012 10gen Inc.
5
5
  #
6
6
  # Licensed under the Apache License, Version 2.0 (the "License");
7
7
  # you may not use this file except in compliance with the License.
@@ -35,7 +35,20 @@ module Mongo
35
35
  MONGODB_URI_SPEC = "mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]"
36
36
 
37
37
  SPEC_ATTRS = [:nodes, :auths]
38
- OPT_ATTRS = [:connect, :replicaset, :slaveok, :safe, :w, :wtimeout, :fsync, :journal, :connecttimeoutms, :sockettimeoutms, :wtimeoutms]
38
+
39
+ OPT_ATTRS = [
40
+ :connect,
41
+ :replicaset,
42
+ :slaveok,
43
+ :safe,
44
+ :w,
45
+ :wtimeout,
46
+ :fsync,
47
+ :journal,
48
+ :connecttimeoutms,
49
+ :sockettimeoutms,
50
+ :wtimeoutms
51
+ ]
39
52
 
40
53
  OPT_VALID = {:connect => lambda {|arg| ['direct', 'replicaset', 'true', 'false', true, false].include?(arg)},
41
54
  :replicaset => lambda {|arg| arg.length > 0},
@@ -76,9 +89,20 @@ module Mongo
76
89
  :wtimeoutms => lambda {|arg| arg.to_i }
77
90
  }
78
91
 
79
- attr_reader :auths, :connect, :replicaset, :slaveok, :safe, :w, :wtimeout, :fsync, :journal, :connecttimeoutms, :sockettimeoutms, :wtimeoutms
80
-
81
- # Parse a MongoDB URI. This method is used by Connection.from_uri.
92
+ attr_reader :auths,
93
+ :connect,
94
+ :replicaset,
95
+ :slaveok,
96
+ :safe,
97
+ :w,
98
+ :wtimeout,
99
+ :fsync,
100
+ :journal,
101
+ :connecttimeoutms,
102
+ :sockettimeoutms,
103
+ :wtimeoutms
104
+
105
+ # Parse a MongoDB URI. This method is used by MongoClient.from_uri.
82
106
  # Returns an array of nodes and an array of db authorizations, if applicable.
83
107
  #
84
108
  # @note Passwords can contain any character except for ','
@@ -100,17 +124,25 @@ module Mongo
100
124
  validate_connect
101
125
  end
102
126
 
103
- # Create a Mongo::Connection or a Mongo::ReplSetConnection based on the URI.
127
+ # Create a Mongo::MongoClient or a Mongo::MongoReplicaSetClient based on the URI.
104
128
  #
105
129
  # @note Don't confuse this with attribute getter method #connect.
106
130
  #
107
- # @return [Connection,ReplSetConnection]
108
- def connection(extra_opts)
131
+ # @return [MongoClient,MongoReplicaSetClient]
132
+ def connection(extra_opts, legacy=false)
109
133
  opts = connection_options.merge! extra_opts
110
- if replicaset?
111
- ReplSetConnection.new(nodes, opts)
134
+ if(legacy)
135
+ if replicaset?
136
+ ReplSetConnection.new(nodes, opts)
137
+ else
138
+ Connection.new(host, port, opts)
139
+ end
112
140
  else
113
- Connection.new(host, port, opts)
141
+ if replicaset?
142
+ MongoReplicaSetClient.new(nodes, opts)
143
+ else
144
+ MongoClient.new(host, port, opts)
145
+ end
114
146
  end
115
147
  end
116
148
 
@@ -147,42 +179,26 @@ module Mongo
147
179
  nodes[0][1].to_i
148
180
  end
149
181
 
150
- # Options that can be passed to Mongo::Connection.new or Mongo::ReplSetConnection.new
182
+ # Options that can be passed to MongoClient.new or MongoReplicaSetClient.new
151
183
  # @return [Hash]
152
184
  def connection_options
153
185
  opts = {}
154
186
 
155
- if (@w || @journal || @wtimeout || @fsync || @wtimeoutms) && !@safe
156
- raise MongoArgumentError, "Safe must be true if w, journal, wtimeoutMS, or fsync is specified"
187
+ if @wtimeout
188
+ warn "Using wtimeout in a URI is deprecated, please use wtimeoutMS. It will be removed in v2.0."
189
+ opts[:wtimeout] = @wtimeout
157
190
  end
191
+ opts[:wtimeout] = @wtimeoutms
158
192
 
159
- if @safe
160
- if @w || @journal || @wtimeout || @fsync || @wtimeoutms
161
- safe_opts = {}
162
- safe_opts[:w] = @w if @w
163
- safe_opts[:j] = @journal if @journal
164
-
165
- if @wtimeout
166
- warn "Using wtimeout in a URI is deprecated, please use wtimeoutMS. It will be removed in v2.0."
167
- safe_opts[:wtimeout] = @wtimeout
168
- end
169
-
170
- if @wtimeoutms
171
- safe_opts[:wtimeout] = @wtimeoutms
172
- end
173
-
174
- safe_opts[:fsync] = @fsync if @fsync
175
- else
176
- safe_opts = true
177
- end
193
+ opts[:w] = 1 if @safe
194
+ opts[:w] = @w if @w
195
+ opts[:j] = @journal
196
+ opts[:fsync] = @fsync
178
197
 
179
- opts[:safe] = safe_opts
180
- end
181
-
182
198
  if @connecttimeoutms
183
199
  opts[:connect_timeout] = @connecttimeoutms
184
200
  end
185
-
201
+
186
202
  if @sockettimeoutms
187
203
  opts[:op_timeout] = @sockettimeoutms
188
204
  end
@@ -235,7 +251,7 @@ module Mongo
235
251
 
236
252
  hosturis.each do |hosturi|
237
253
  # If port is present, use it, otherwise use default port
238
- host, port = hosturi.split(':') + [Connection::DEFAULT_PORT]
254
+ host, port = hosturi.split(':') + [MongoClient::DEFAULT_PORT]
239
255
 
240
256
  if !(port.to_s =~ /^\d+$/)
241
257
  raise MongoArgumentError, "Invalid port #{port}; port must be specified as digits."
@@ -0,0 +1,67 @@
1
+ # encoding: UTF-8
2
+
3
+ # --
4
+ # Copyright (C) 2008-2011 10gen Inc.
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ # ++
18
+
19
+ module Mongo
20
+ module WriteConcern
21
+
22
+ attr_reader :legacy_write_concern
23
+
24
+ @@safe_warn = nil
25
+ def write_concern_from_legacy(opts)
26
+ # Warn if 'safe' parameter is being used,
27
+ if opts.key?(:safe) && !@@safe_warn && !ENV['TEST_MODE']
28
+ warn "[DEPRECATED] The 'safe' write concern option has been deprecated in favor of 'w'."
29
+ @@safe_warn = true
30
+ end
31
+
32
+ # nil: set :w => 0
33
+ # false: set :w => 0
34
+ # true: set :w => 1
35
+ # hash: set :w => 0 and merge with opts
36
+
37
+ unless opts.has_key?(:w)
38
+ opts[:w] = 0 # legacy default, unacknowledged
39
+ safe = opts.delete(:safe)
40
+ if(safe && safe.is_a?(Hash))
41
+ opts.merge!(safe)
42
+ elsif(safe == true)
43
+ opts[:w] = 1
44
+ end
45
+ end
46
+ end
47
+
48
+ # todo: throw exception for conflicting write concern options
49
+ def get_write_concern(opts, parent=nil)
50
+ write_concern_from_legacy(opts) if opts.key?(:safe) || @legacy_write_concern
51
+ write_concern = {
52
+ :w => 1,
53
+ :j => false,
54
+ :fsync => false,
55
+ :wtimeout => false
56
+ }
57
+ write_concern.merge!(parent.write_concern) if parent
58
+ write_concern.merge!(opts.reject {|k,v| !write_concern.keys.include?(k)})
59
+ write_concern
60
+ end
61
+
62
+ def self.gle?(write_concern)
63
+ write_concern[:w] > 0 || write_concern[:j] || write_concern[:fsync] || write_concern[:wtimeout]
64
+ end
65
+
66
+ end
67
+ end