mongo 1.0.1 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -134,18 +134,18 @@ namespace :gem do
134
134
 
135
135
  desc "Install the gem locally"
136
136
  task :install do
137
+ sh "gem build bson.gemspec"
138
+ sh "gem install bson-*.gem"
137
139
  sh "gem build mongo-ruby-driver.gemspec"
138
140
  sh "gem install mongo-*.gem"
139
141
  sh "rm mongo-*.gem"
142
+ sh "rm bson-*.gem"
140
143
  end
141
144
 
142
145
  desc "Install the optional c extensions"
143
146
  task :install_extensions do
144
- sh "gem build bson.gemspec"
145
147
  sh "gem build bson_ext.gemspec"
146
- sh "gem install bson-*.gem"
147
148
  sh "gem install bson_ext-*.gem"
148
- sh "rm bson-*.gem"
149
149
  sh "rm bson_ext-*.gem"
150
150
  end
151
151
 
@@ -3,7 +3,7 @@
3
3
  $:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
4
4
 
5
5
  module Mongo
6
- VERSION = "1.0.1"
6
+ VERSION = "1.0.2"
7
7
  end
8
8
 
9
9
  module Mongo
@@ -24,8 +24,6 @@ module Mongo
24
24
  OP_QUERY_TAILABLE = 2
25
25
  OP_QUERY_SLAVE_OK = 4
26
26
  OP_QUERY_NO_CURSOR_TIMEOUT = 16
27
-
28
- DEFAULT_BATCH_SIZE = 100
29
27
  end
30
28
 
31
29
  end
@@ -452,7 +452,7 @@ module Mongo
452
452
  cmd.merge!(opts)
453
453
  cmd[:sort] = Mongo::Support.format_order_clause(opts[:sort]) if opts[:sort]
454
454
 
455
- @db.command(cmd, false, true)['value']
455
+ @db.command(cmd)['value']
456
456
  end
457
457
 
458
458
  # Perform a map/reduce operation on the current collection.
@@ -536,9 +536,9 @@ module Mongo
536
536
  group_command['group']['finalize'] = finalize
537
537
  end
538
538
 
539
- result = @db.command group_command
539
+ result = @db.command(group_command)
540
540
 
541
- if result["ok"] == 1
541
+ if Mongo::Support.ok?(result)
542
542
  result["retval"]
543
543
  else
544
544
  raise OperationFailure, "group command failed: #{result['errmsg']}"
@@ -219,7 +219,7 @@ module Mongo
219
219
  #
220
220
  # @return [Hash]
221
221
  def database_info
222
- doc = self['admin'].command({:listDatabases => 1}, false, true)
222
+ doc = self['admin'].command({:listDatabases => 1})
223
223
  returning({}) do |info|
224
224
  doc['databases'].each { |db| info[db['name']] = db['sizeOnDisk'].to_i }
225
225
  end
@@ -283,12 +283,12 @@ module Mongo
283
283
  nonce_cmd = BSON::OrderedHash.new
284
284
  nonce_cmd[:copydbgetnonce] = 1
285
285
  nonce_cmd[:fromhost] = from_host
286
- result = self["admin"].command(nonce_cmd, true, true)
286
+ result = self["admin"].command(nonce_cmd)
287
287
  oh[:nonce] = result["nonce"]
288
288
  oh[:username] = username
289
289
  oh[:key] = Mongo::Support.auth_key(username, password, oh[:nonce])
290
290
  end
291
- self["admin"].command(oh, true, true)
291
+ self["admin"].command(oh)
292
292
  end
293
293
 
294
294
  # Increment and return the next available request id.
@@ -306,7 +306,7 @@ module Mongo
306
306
  #
307
307
  # @return [Hash]
308
308
  def server_info
309
- self["admin"].command({:buildinfo => 1}, false, true)
309
+ self["admin"].command({:buildinfo => 1})
310
310
  end
311
311
 
312
312
  # Get the build version of the current server.
@@ -422,8 +422,9 @@ module Mongo
422
422
  socket.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1)
423
423
 
424
424
  # If we're connected to master, set the @host and @port
425
- result = self['admin'].command({:ismaster => 1}, false, false, socket)
426
- if result['ok'] == 1 && ((is_master = result['ismaster'] == 1) || @slave_ok)
425
+ result = self['admin'].command({:ismaster => 1}, :check_response => false, :sock => socket)
426
+ if Mongo::Support.ok?(result) &&
427
+ ((is_master = result['ismaster'] == 1) || @slave_ok)
427
428
  @host, @port = host, port
428
429
  apply_saved_authentication
429
430
  end
@@ -40,6 +40,9 @@ module Mongo
40
40
 
41
41
  @selector = convert_selector_for_query(options[:selector])
42
42
  @fields = convert_fields_for_query(options[:fields])
43
+ if options[:admin]
44
+ warn "The admin option to Cursor#new has been deprecated. The cursor should now be passed the admin collection explicitly."
45
+ end
43
46
  @admin = options[:admin] || false
44
47
  @skip = options[:skip] || 0
45
48
  @limit = options[:limit] || 0
@@ -50,7 +53,7 @@ module Mongo
50
53
  @explain = options[:explain]
51
54
  @socket = options[:socket]
52
55
  @tailable = options[:tailable] || false
53
- @batch_size = options[:batch_size] || Mongo::Constants::DEFAULT_BATCH_SIZE
56
+ @batch_size = options[:batch_size] || 0
54
57
 
55
58
  @full_collection_name = "#{@collection.db.name}.#{@collection.name}"
56
59
  @cache = []
@@ -100,7 +103,7 @@ module Mongo
100
103
  "query", @selector,
101
104
  "fields", @fields]
102
105
  response = @db.command(command)
103
- return response['n'].to_i if response['ok'] == 1
106
+ return response['n'].to_i if Mongo::Support.ok?(response)
104
107
  return 0 if response['errmsg'] == "ns missing"
105
108
  raise OperationFailure, "Count failed: #{response['errmsg']}"
106
109
  end
@@ -264,7 +267,6 @@ module Mongo
264
267
  def query_options_hash
265
268
  { :selector => @selector,
266
269
  :fields => @fields,
267
- :admin => @admin,
268
270
  :skip => @skip_num,
269
271
  :limit => @limit_num,
270
272
  :order => @order,
@@ -87,7 +87,7 @@ module Mongo
87
87
  #
88
88
  # @core authenticate authenticate-instance_method
89
89
  def authenticate(username, password, save_auth=true)
90
- doc = command(:getnonce => 1)
90
+ doc = command({:getnonce => 1}, :check_response => false)
91
91
  raise "error retrieving nonce: #{doc}" unless ok?(doc)
92
92
  nonce = doc['nonce']
93
93
 
@@ -96,7 +96,7 @@ module Mongo
96
96
  auth['user'] = username
97
97
  auth['nonce'] = nonce
98
98
  auth['key'] = Mongo::Support.auth_key(username, password, nonce)
99
- if ok?(command(auth))
99
+ if ok?(self.command(auth, :check_response => false))
100
100
  if save_auth
101
101
  @connection.add_auth(@name, username, password)
102
102
  end
@@ -217,8 +217,7 @@ module Mongo
217
217
  oh = BSON::OrderedHash.new
218
218
  oh[:create] = name
219
219
  doc = command(oh.merge(options || {}))
220
- ok = doc['ok']
221
- return Collection.new(self, name, @pk_factory) if ok.kind_of?(Numeric) && (ok.to_i == 1 || ok.to_i == 0)
220
+ return Collection.new(self, name, @pk_factory) if ok?(doc)
222
221
  raise MongoDBError, "Error creating collection: #{doc.inspect}"
223
222
  end
224
223
 
@@ -260,7 +259,7 @@ module Mongo
260
259
  cmd = BSON::OrderedHash.new
261
260
  cmd[:getlasterror] = 1
262
261
  cmd.merge!(opts) unless opts.empty?
263
- doc = command(cmd)
262
+ doc = command(cmd, :check_response => false)
264
263
  raise MongoDBError, "error retrieving last error: #{doc.inspect}" unless ok?(doc)
265
264
  doc['err']
266
265
  end
@@ -359,7 +358,7 @@ module Mongo
359
358
  oh = BSON::OrderedHash.new
360
359
  oh[:renameCollection] = "#{@name}.#{from}"
361
360
  oh[:to] = "#{@name}.#{to}"
362
- doc = command(oh, true)
361
+ doc = DB.new('admin', @connection).command(oh)
363
362
  ok?(doc) || raise(MongoDBError, "Error renaming collection: #{doc.inspect}")
364
363
  end
365
364
 
@@ -427,8 +426,7 @@ module Mongo
427
426
  #
428
427
  # @return [Boolean]
429
428
  def ok?(doc)
430
- ok = doc['ok']
431
- ok.kind_of?(Numeric) && ok.to_i == 1
429
+ Mongo::Support.ok?(doc)
432
430
  end
433
431
 
434
432
  # Send a command to the database.
@@ -441,29 +439,38 @@ module Mongo
441
439
  # to see how it works.
442
440
  #
443
441
  # @param [OrderedHash, Hash] selector an OrderedHash, or a standard Hash with just one
444
- # key, specifying the command to be performed.
442
+ # key, specifying the command to be performed. In Ruby 1.9, OrderedHash isn't necessary since
443
+ # hashes are ordered by default.
445
444
  #
446
445
  # @param [Boolean] admin If +true+, the command will be executed on the admin
447
- # collection.
446
+ # collection. DEPRECATED.
448
447
  #
449
- # @param [Boolean] check_response If +true+, will raise an exception if the
448
+ # @option opts [Boolean] :check_response (true) If +true+, raises an exception if the
450
449
  # command fails.
451
- #
452
- # @param [Socket] sock a socket to use. This is mainly for internal use.
450
+ # @option opts [Socket] :sock a socket to use for sending the command. This is mainly for internal use.
453
451
  #
454
452
  # @return [Hash]
455
453
  #
456
454
  # @core commands command_instance-method
457
- def command(selector, admin=false, check_response=false, sock=nil)
455
+ def command(selector, opts={}, old_check_response=false, old_sock=nil)
456
+ if opts.is_a?(Hash)
457
+ check_response = opts[:check_response].nil? ? true : opts[:check_response]
458
+ sock = opts[:sock]
459
+ else
460
+ warn "The options passed to DB#command should now be passed as hash keys; the admin option has been deprecated."
461
+ admin = opts
462
+ check_response = old_check_response
463
+ sock = old_sock
464
+ end
458
465
  raise MongoArgumentError, "command must be given a selector" unless selector.is_a?(Hash) && !selector.empty?
459
- if selector.class.eql?(Hash) && selector.keys.length > 1
466
+ if selector.keys.length > 1 && RUBY_VERSION < '1.9' && selector.class != BSON::OrderedHash
460
467
  raise MongoArgumentError, "DB#command requires an OrderedHash when hash contains multiple keys"
461
468
  end
462
469
 
463
470
  result = Cursor.new(system_command_collection, :admin => admin,
464
471
  :limit => -1, :selector => selector, :socket => sock).next_document
465
472
 
466
- if check_response && !ok?(result)
473
+ if result.nil? || (check_response && !ok?(result))
467
474
  raise OperationFailure, "Database command '#{selector.keys.first}' failed: #{result.inspect}"
468
475
  else
469
476
  result
@@ -68,5 +68,15 @@ module Mongo
68
68
  "[['field1', '(ascending|descending)'], ['field2', '(ascending|descending)']]"
69
69
  end
70
70
  end
71
+
72
+ # Determine if a database command has succeeded by
73
+ # checking the document response.
74
+ #
75
+ # @param [Hash] doc
76
+ #
77
+ # @return [Boolean] true if the 'ok' key is either 1 or *true*.
78
+ def ok?(doc)
79
+ doc['ok'] == 1.0 || doc['ok'] == true
80
+ end
71
81
  end
72
82
  end
@@ -21,7 +21,7 @@ class TestConnection < Test::Unit::TestCase
21
21
  def test_server_info
22
22
  server_info = @mongo.server_info
23
23
  assert server_info.keys.include?("version")
24
- assert_equal 1.0, server_info["ok"]
24
+ assert Mongo::Support.ok?(server_info)
25
25
  end
26
26
 
27
27
  def test_server_version
@@ -71,7 +71,7 @@ class TestConnection < Test::Unit::TestCase
71
71
  end
72
72
 
73
73
  result = @mongo.copy_database('old', 'new', 'localhost', 'bob', 'secret')
74
- assert result['ok'].to_i == 1
74
+ assert Mongo::Support.ok?(result)
75
75
 
76
76
  @mongo.drop_database('old')
77
77
  @mongo.drop_database('new')
@@ -152,18 +152,27 @@ class DBTest < Test::Unit::TestCase
152
152
  assert @@db.logout
153
153
  end
154
154
 
155
+ def test_command
156
+ assert_raise OperationFailure do
157
+ @@db.command({:non_command => 1}, :check_response => true)
158
+ end
159
+
160
+ result = @@db.command({:non_command => 1}, :check_response => false)
161
+ assert !Mongo::Support.ok?(result)
162
+ end
163
+
155
164
  def test_error
156
165
  @@db.reset_error_history
157
166
  assert_nil @@db.error
158
167
  assert !@@db.error?
159
168
  assert_nil @@db.previous_error
160
169
 
161
- @@db.send(:command, :forceerror => 1)
170
+ @@db.command({:forceerror => 1}, :check_response => false)
162
171
  assert @@db.error?
163
172
  assert_not_nil @@db.error
164
173
  assert_not_nil @@db.previous_error
165
174
 
166
- @@db.send(:command, :forceerror => 1)
175
+ @@db.command({:forceerror => 1}, :check_response => false)
167
176
  assert @@db.error?
168
177
  assert @@db.error
169
178
  prev_error = @@db.previous_error
@@ -203,7 +212,7 @@ class DBTest < Test::Unit::TestCase
203
212
  def test_check_command_response
204
213
  command = {:forceerror => 1}
205
214
  assert_raise OperationFailure do
206
- @@db.command(command, false, true)
215
+ @@db.command(command)
207
216
  end
208
217
  end
209
218
 
@@ -0,0 +1,19 @@
1
+ require 'test/test_helper'
2
+
3
+ class SupportTest < Test::Unit::TestCase
4
+ include Mongo
5
+
6
+ def test_command_response_succeeds
7
+ assert Support.ok?('ok' => 1)
8
+ assert Support.ok?('ok' => 1.0)
9
+ assert Support.ok?('ok' => true)
10
+ end
11
+
12
+ def test_command_response_fails
13
+ assert !Support.ok?('ok' => 0)
14
+ assert !Support.ok?('ok' => 0.0)
15
+ assert !Support.ok?('ok' => 0.0)
16
+ assert !Support.ok?('ok' => 'str')
17
+ assert !Support.ok?('ok' => false)
18
+ end
19
+ end
@@ -22,8 +22,10 @@ class DBTest < Test::Unit::TestCase
22
22
  end
23
23
 
24
24
  should "raise an error if given a hash with more than one key" do
25
- assert_raise MongoArgumentError do
26
- @db.command(:buildinfo => 1, :somekey => 1)
25
+ if RUBY_VERSION < '1.9'
26
+ assert_raise MongoArgumentError do
27
+ @db.command(:buildinfo => 1, :somekey => 1)
28
+ end
27
29
  end
28
30
  end
29
31
 
@@ -60,7 +62,7 @@ class DBTest < Test::Unit::TestCase
60
62
 
61
63
  should "raise an error if collection creation fails" do
62
64
  @db.expects(:collection_names).returns([])
63
- @db.expects(:command).returns({})
65
+ @db.expects(:command).returns({'ok' => 0})
64
66
  assert_raise Mongo::MongoDBError do
65
67
  @db.create_collection("foo")
66
68
  end
@@ -73,13 +75,6 @@ class DBTest < Test::Unit::TestCase
73
75
  end
74
76
  end
75
77
 
76
- should "raise an error if rename fails" do
77
- @db.expects(:command).returns({})
78
- assert_raise Mongo::MongoDBError do
79
- @db.rename_collection("foo", "bar")
80
- end
81
- end
82
-
83
78
  should "raise an error if drop_index fails" do
84
79
  @db.expects(:command).returns({})
85
80
  assert_raise Mongo::MongoDBError do
metadata CHANGED
@@ -1,29 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongo
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.2
5
5
  platform: ruby
6
6
  authors:
7
- - Jim Menard
8
- - Mike Dirolf
9
- - Kyle Banker
7
+ - Jim Menard
8
+ - Mike Dirolf
9
+ - Kyle Banker
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
13
 
14
- date: 2010-05-07 00:00:00 -04:00
14
+ date: 2010-06-05 00:00:00 -04:00
15
15
  default_executable:
16
16
  dependencies:
17
- - !ruby/object:Gem::Dependency
18
- name: bson
19
- type: :runtime
20
- version_requirement:
21
- version_requirements: !ruby/object:Gem::Requirement
22
- requirements:
23
- - - "="
24
- - !ruby/object:Gem::Version
25
- version: 1.0.1
26
- version:
17
+ - !ruby/object:Gem::Dependency
18
+ name: bson
19
+ type: :runtime
20
+ version_requirement:
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - "="
24
+ - !ruby/object:Gem::Version
25
+ version: 1.0.2
26
+ version:
27
27
  description: A Ruby driver for MongoDB. For more information about Mongo, see http://www.mongodb.org.
28
28
  email: mongodb-dev@googlegroups.com
29
29
  executables: []
@@ -31,60 +31,60 @@ executables: []
31
31
  extensions: []
32
32
 
33
33
  extra_rdoc_files:
34
- - README.rdoc
34
+ - README.rdoc
35
35
  files:
36
- - README.rdoc
37
- - Rakefile
38
- - mongo-ruby-driver.gemspec
39
- - LICENSE.txt
40
- - lib/mongo.rb
41
- - lib/mongo/collection.rb
42
- - lib/mongo/connection.rb
43
- - lib/mongo/cursor.rb
44
- - lib/mongo/db.rb
45
- - lib/mongo/exceptions.rb
46
- - lib/mongo/gridfs/grid.rb
47
- - lib/mongo/gridfs/grid_ext.rb
48
- - lib/mongo/gridfs/grid_file_system.rb
49
- - lib/mongo/gridfs/grid_io.rb
50
- - lib/mongo/util/conversions.rb
51
- - lib/mongo/util/core_ext.rb
52
- - lib/mongo/util/server_version.rb
53
- - lib/mongo/util/support.rb
54
- - examples/admin.rb
55
- - examples/capped.rb
56
- - examples/cursor.rb
57
- - examples/gridfs.rb
58
- - examples/index_test.rb
59
- - examples/info.rb
60
- - examples/queries.rb
61
- - examples/simple.rb
62
- - examples/strict.rb
63
- - examples/types.rb
64
- - bin/bson_benchmark.rb
65
- - bin/fail_if_no_c.rb
36
+ - README.rdoc
37
+ - Rakefile
38
+ - mongo-ruby-driver.gemspec
39
+ - LICENSE.txt
40
+ - lib/mongo.rb
41
+ - lib/mongo/collection.rb
42
+ - lib/mongo/connection.rb
43
+ - lib/mongo/cursor.rb
44
+ - lib/mongo/db.rb
45
+ - lib/mongo/exceptions.rb
46
+ - lib/mongo/gridfs/grid.rb
47
+ - lib/mongo/gridfs/grid_ext.rb
48
+ - lib/mongo/gridfs/grid_file_system.rb
49
+ - lib/mongo/gridfs/grid_io.rb
50
+ - lib/mongo/util/conversions.rb
51
+ - lib/mongo/util/core_ext.rb
52
+ - lib/mongo/util/server_version.rb
53
+ - lib/mongo/util/support.rb
54
+ - examples/admin.rb
55
+ - examples/capped.rb
56
+ - examples/cursor.rb
57
+ - examples/gridfs.rb
58
+ - examples/index_test.rb
59
+ - examples/info.rb
60
+ - examples/queries.rb
61
+ - examples/simple.rb
62
+ - examples/strict.rb
63
+ - examples/types.rb
64
+ - bin/bson_benchmark.rb
65
+ - bin/fail_if_no_c.rb
66
66
  has_rdoc: true
67
67
  homepage: http://www.mongodb.org
68
68
  licenses: []
69
69
 
70
70
  post_install_message:
71
71
  rdoc_options:
72
- - --main
73
- - README.rdoc
74
- - --inline-source
72
+ - --main
73
+ - README.rdoc
74
+ - --inline-source
75
75
  require_paths:
76
- - lib
76
+ - lib
77
77
  required_ruby_version: !ruby/object:Gem::Requirement
78
78
  requirements:
79
- - - ">="
80
- - !ruby/object:Gem::Version
81
- version: "0"
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: "0"
82
82
  version:
83
83
  required_rubygems_version: !ruby/object:Gem::Requirement
84
84
  requirements:
85
- - - ">="
86
- - !ruby/object:Gem::Version
87
- version: "0"
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: "0"
88
88
  version:
89
89
  requirements: []
90
90
 
@@ -94,28 +94,29 @@ signing_key:
94
94
  specification_version: 3
95
95
  summary: Ruby driver for the MongoDB
96
96
  test_files:
97
- - test/auxillary/1.4_features.rb
98
- - test/auxillary/authentication_test.rb
99
- - test/auxillary/autoreconnect_test.rb
100
- - test/collection_test.rb
101
- - test/connection_test.rb
102
- - test/conversions_test.rb
103
- - test/cursor_test.rb
104
- - test/db_api_test.rb
105
- - test/db_connection_test.rb
106
- - test/db_test.rb
107
- - test/grid_file_system_test.rb
108
- - test/grid_io_test.rb
109
- - test/grid_test.rb
110
- - test/replica/count_test.rb
111
- - test/replica/insert_test.rb
112
- - test/replica/pooled_insert_test.rb
113
- - test/replica/query_test.rb
114
- - test/slave_connection_test.rb
115
- - test/test_helper.rb
116
- - test/threading/test_threading_large_pool.rb
117
- - test/threading_test.rb
118
- - test/unit/collection_test.rb
119
- - test/unit/connection_test.rb
120
- - test/unit/cursor_test.rb
121
- - test/unit/db_test.rb
97
+ - test/collection_test.rb
98
+ - test/connection_test.rb
99
+ - test/conversions_test.rb
100
+ - test/cursor_test.rb
101
+ - test/db_api_test.rb
102
+ - test/db_connection_test.rb
103
+ - test/db_test.rb
104
+ - test/grid_file_system_test.rb
105
+ - test/grid_io_test.rb
106
+ - test/grid_test.rb
107
+ - test/slave_connection_test.rb
108
+ - test/support_test.rb
109
+ - test/test_helper.rb
110
+ - test/threading_test.rb
111
+ - test/auxillary/1.4_features.rb
112
+ - test/auxillary/authentication_test.rb
113
+ - test/auxillary/autoreconnect_test.rb
114
+ - test/replica/count_test.rb
115
+ - test/replica/insert_test.rb
116
+ - test/replica/pooled_insert_test.rb
117
+ - test/replica/query_test.rb
118
+ - test/threading/test_threading_large_pool.rb
119
+ - test/unit/collection_test.rb
120
+ - test/unit/connection_test.rb
121
+ - test/unit/cursor_test.rb
122
+ - test/unit/db_test.rb