mongo 0.19.3 → 0.20
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.
- data/README.rdoc +7 -3
- data/Rakefile +16 -6
- data/bin/bson_benchmark.rb +2 -2
- data/examples/gridfs.rb +3 -2
- data/lib/mongo.rb +3 -26
- data/lib/mongo/collection.rb +69 -50
- data/lib/mongo/connection.rb +16 -41
- data/lib/mongo/cursor.rb +22 -32
- data/lib/mongo/db.rb +13 -5
- data/lib/mongo/exceptions.rb +11 -15
- data/lib/mongo/gridfs/grid.rb +14 -3
- data/lib/mongo/gridfs/grid_file_system.rb +28 -5
- data/lib/mongo/gridfs/grid_io.rb +42 -24
- data/lib/mongo/util/support.rb +13 -2
- data/mongo-ruby-driver.gemspec +3 -1
- data/test/collection_test.rb +62 -9
- data/test/connection_test.rb +21 -32
- data/test/conversions_test.rb +1 -1
- data/test/cursor_test.rb +2 -2
- data/test/db_api_test.rb +28 -27
- data/test/db_connection_test.rb +1 -1
- data/test/db_test.rb +23 -13
- data/test/grid_file_system_test.rb +30 -4
- data/test/grid_io_test.rb +14 -1
- data/test/grid_test.rb +59 -3
- data/test/test_helper.rb +4 -1
- data/test/threading/test_threading_large_pool.rb +1 -1
- data/test/threading_test.rb +1 -1
- data/test/unit/collection_test.rb +2 -2
- data/test/unit/cursor_test.rb +7 -0
- data/test/unit/db_test.rb +8 -8
- metadata +6 -46
- data/bin/gr.rb +0 -14
- data/lib/bson.rb +0 -46
- data/lib/bson/bson_c.rb +0 -20
- data/lib/bson/bson_ruby.rb +0 -601
- data/lib/bson/byte_buffer.rb +0 -224
- data/lib/bson/exceptions.rb +0 -39
- data/lib/bson/ordered_hash.rb +0 -140
- data/lib/bson/types/binary.rb +0 -54
- data/lib/bson/types/code.rb +0 -36
- data/lib/bson/types/dbref.rb +0 -40
- data/lib/bson/types/min_max_keys.rb +0 -58
- data/lib/bson/types/objectid.rb +0 -180
- data/lib/bson/types/regexp_of_holding.rb +0 -45
- data/lib/mongo/gridfs.rb +0 -29
- data/lib/mongo/gridfs/chunk.rb +0 -91
- data/lib/mongo/gridfs/grid_store.rb +0 -580
- data/lib/mongo/types/binary.rb +0 -52
- data/lib/mongo/types/code.rb +0 -36
- data/lib/mongo/types/dbref.rb +0 -40
- data/lib/mongo/types/min_max_keys.rb +0 -58
- data/lib/mongo/types/objectid.rb +0 -180
- data/lib/mongo/types/regexp_of_holding.rb +0 -45
- data/lib/mongo/util/bson_c.rb +0 -18
- data/lib/mongo/util/bson_ruby.rb +0 -606
- data/lib/mongo/util/byte_buffer.rb +0 -222
- data/lib/mongo/util/ordered_hash.rb +0 -140
- data/test/binary_test.rb +0 -15
- data/test/bson_test.rb +0 -459
- data/test/byte_buffer_test.rb +0 -81
- data/test/chunk_test.rb +0 -82
- data/test/grid_store_test.rb +0 -337
- data/test/objectid_test.rb +0 -125
- data/test/ordered_hash_test.rb +0 -172
data/README.rdoc
CHANGED
@@ -30,10 +30,14 @@ Then you can install the mongo gem as follows:
|
|
30
30
|
|
31
31
|
$ gem install mongo
|
32
32
|
|
33
|
-
|
33
|
+
The driver also requires the BSON gem:
|
34
|
+
|
35
|
+
$ gem install bson
|
36
|
+
|
37
|
+
And for a significant performance boost, you'll want to install the C extensions:
|
34
38
|
extensions:
|
35
39
|
|
36
|
-
$ gem install
|
40
|
+
$ gem install bson_ext
|
37
41
|
|
38
42
|
=== From the GitHub source
|
39
43
|
|
@@ -274,7 +278,7 @@ Random cursor fun facts:
|
|
274
278
|
= Testing
|
275
279
|
|
276
280
|
If you have the source code, you can run the tests. There's a separate rake task for testing with
|
277
|
-
the
|
281
|
+
the bson_ext C extension enabled.
|
278
282
|
|
279
283
|
$ rake test:c
|
280
284
|
|
data/Rakefile
CHANGED
@@ -27,6 +27,7 @@ namespace :test do
|
|
27
27
|
ENV['C_EXT'] = 'TRUE'
|
28
28
|
Rake::Task['test:unit'].invoke
|
29
29
|
Rake::Task['test:functional'].invoke
|
30
|
+
Rake::Task['test:bson'].invoke
|
30
31
|
Rake::Task['test:pooled_threading'].invoke
|
31
32
|
Rake::Task['test:drop_databases'].invoke
|
32
33
|
ENV['C_EXT'] = nil
|
@@ -37,10 +38,11 @@ namespace :test do
|
|
37
38
|
ENV['C_EXT'] = nil
|
38
39
|
Rake::Task['test:unit'].invoke
|
39
40
|
Rake::Task['test:functional'].invoke
|
41
|
+
Rake::Task['test:bson'].invoke
|
40
42
|
Rake::Task['test:pooled_threading'].invoke
|
41
43
|
Rake::Task['test:drop_databases'].invoke
|
42
44
|
end
|
43
|
-
|
45
|
+
|
44
46
|
Rake::TestTask.new(:unit) do |t|
|
45
47
|
t.test_files = FileList['test/unit/*_test.rb']
|
46
48
|
t.verbose = true
|
@@ -91,13 +93,18 @@ namespace :test do
|
|
91
93
|
t.verbose = true
|
92
94
|
end
|
93
95
|
|
96
|
+
Rake::TestTask.new(:bson) do |t|
|
97
|
+
t.test_files = FileList['test/mongo_bson/*_test.rb']
|
98
|
+
t.verbose = true
|
99
|
+
end
|
100
|
+
|
94
101
|
task :drop_databases do |t|
|
95
102
|
puts "Dropping test database..."
|
96
|
-
require File.join(File.dirname(__FILE__), '
|
103
|
+
require File.join(File.dirname(__FILE__), 'test', 'test_helper')
|
97
104
|
include Mongo
|
98
105
|
con = Connection.new(ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost',
|
99
106
|
ENV['MONGO_RUBY_DRIVER_PORT'] || Connection::DEFAULT_PORT)
|
100
|
-
con.drop_database(
|
107
|
+
con.drop_database(MONGO_TEST_DB)
|
101
108
|
end
|
102
109
|
end
|
103
110
|
|
@@ -134,9 +141,12 @@ namespace :gem do
|
|
134
141
|
|
135
142
|
desc "Install the optional c extensions"
|
136
143
|
task :install_extensions do
|
137
|
-
sh "gem build
|
138
|
-
sh "gem
|
139
|
-
sh "
|
144
|
+
sh "gem build bson.gemspec"
|
145
|
+
sh "gem build bson_ext.gemspec"
|
146
|
+
sh "gem install bson-*.gem"
|
147
|
+
sh "gem install bson_ext-*.gem"
|
148
|
+
sh "rm bson-*.gem"
|
149
|
+
sh "rm bson_ext-*.gem"
|
140
150
|
end
|
141
151
|
|
142
152
|
end
|
data/bin/bson_benchmark.rb
CHANGED
data/examples/gridfs.rb
CHANGED
@@ -17,7 +17,7 @@ data = "hello, world!"
|
|
17
17
|
grid = Grid.new(db)
|
18
18
|
|
19
19
|
# Write a new file. data can be a string or an io object responding to #read.
|
20
|
-
id = grid.put(data, 'hello.txt')
|
20
|
+
id = grid.put(data, :filename => 'hello.txt')
|
21
21
|
|
22
22
|
# Read it and print out the contents
|
23
23
|
file = grid.get(id)
|
@@ -33,11 +33,12 @@ rescue => e
|
|
33
33
|
end
|
34
34
|
|
35
35
|
# Metadata
|
36
|
-
id = grid.put(data, 'hello.txt', :content_type => 'text/plain', :metadata => {'name' => 'hello'})
|
36
|
+
id = grid.put(data, :filename => 'hello.txt', :content_type => 'text/plain', :metadata => {'name' => 'hello'})
|
37
37
|
file = grid.get(id)
|
38
38
|
|
39
39
|
p file.content_type
|
40
40
|
p file.metadata.inspect
|
41
41
|
p file.chunk_size
|
42
42
|
p file.file_length
|
43
|
+
p file.filename
|
43
44
|
p file.data
|
data/lib/mongo.rb
CHANGED
@@ -1,23 +1,7 @@
|
|
1
1
|
$:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
2
|
|
3
3
|
module Mongo
|
4
|
-
VERSION = "0.
|
5
|
-
end
|
6
|
-
|
7
|
-
begin
|
8
|
-
# Need this for running test with and without c ext in Ruby 1.9.
|
9
|
-
raise LoadError if ENV['TEST_MODE'] && !ENV['C_EXT']
|
10
|
-
require 'mongo_ext/cbson'
|
11
|
-
raise LoadError unless defined?(CBson::VERSION) && CBson::VERSION == Mongo::VERSION
|
12
|
-
require 'mongo/util/bson_c'
|
13
|
-
BSON = BSON_C
|
14
|
-
rescue LoadError
|
15
|
-
require 'mongo/util/bson_ruby'
|
16
|
-
BSON = BSON_RUBY
|
17
|
-
warn "\n**Notice: C extension not loaded. This is required for optimum MongoDB Ruby driver performance."
|
18
|
-
warn " You can install the extension as follows:\n gem install mongo_ext\n"
|
19
|
-
warn " If you continue to receive this message after installing, make sure that the"
|
20
|
-
warn " mongo_ext gem is in your load path and that the mongo_ext and mongo gems are of the same version.\n"
|
4
|
+
VERSION = "0.20"
|
21
5
|
end
|
22
6
|
|
23
7
|
module Mongo
|
@@ -41,25 +25,18 @@ module Mongo
|
|
41
25
|
|
42
26
|
end
|
43
27
|
|
44
|
-
require '
|
45
|
-
require 'mongo/types/code'
|
46
|
-
require 'mongo/types/dbref'
|
47
|
-
require 'mongo/types/objectid'
|
48
|
-
require 'mongo/types/regexp_of_holding'
|
49
|
-
require 'mongo/types/min_max_keys'
|
28
|
+
require 'bson'
|
50
29
|
|
30
|
+
require 'mongo/util/conversions'
|
51
31
|
require 'mongo/util/support'
|
52
32
|
require 'mongo/util/core_ext'
|
53
|
-
require 'mongo/util/conversions'
|
54
33
|
require 'mongo/util/server_version'
|
55
|
-
require 'mongo/util/bson_ruby'
|
56
34
|
|
57
35
|
require 'mongo/collection'
|
58
36
|
require 'mongo/connection'
|
59
37
|
require 'mongo/cursor'
|
60
38
|
require 'mongo/db'
|
61
39
|
require 'mongo/exceptions'
|
62
|
-
require 'mongo/gridfs'
|
63
40
|
require 'mongo/gridfs/grid'
|
64
41
|
require 'mongo/gridfs/grid_io'
|
65
42
|
require 'mongo/gridfs/grid_file_system'
|
data/lib/mongo/collection.rb
CHANGED
@@ -26,7 +26,7 @@ module Mongo
|
|
26
26
|
# @param [DB] db a MongoDB database instance.
|
27
27
|
# @param [String, Symbol] name the name of the collection.
|
28
28
|
#
|
29
|
-
# @raise [
|
29
|
+
# @raise [InvalidNSName]
|
30
30
|
# if collection name is empty, contains '$', or starts or ends with '.'
|
31
31
|
#
|
32
32
|
# @raise [TypeError]
|
@@ -45,18 +45,18 @@ module Mongo
|
|
45
45
|
name = name.to_s
|
46
46
|
|
47
47
|
if name.empty? or name.include? ".."
|
48
|
-
raise
|
48
|
+
raise Mongo::InvalidNSName, "collection names cannot be empty"
|
49
49
|
end
|
50
50
|
if name.include? "$"
|
51
|
-
raise
|
51
|
+
raise Mongo::InvalidNSName, "collection names must not contain '$'" unless name =~ /((^\$cmd)|(oplog\.\$main))/
|
52
52
|
end
|
53
53
|
if name.match(/^\./) or name.match(/\.$/)
|
54
|
-
raise
|
54
|
+
raise Mongo::InvalidNSName, "collection names must not start or end with '.'"
|
55
55
|
end
|
56
56
|
|
57
57
|
@db, @name = db, name
|
58
58
|
@connection = @db.connection
|
59
|
-
@pk_factory = pk_factory || ObjectID
|
59
|
+
@pk_factory = pk_factory || BSON::ObjectID
|
60
60
|
@hint = nil
|
61
61
|
end
|
62
62
|
|
@@ -66,7 +66,7 @@ module Mongo
|
|
66
66
|
# @param [String] name
|
67
67
|
# the collection to return
|
68
68
|
#
|
69
|
-
# @raise [
|
69
|
+
# @raise [Mongo::InvalidNSName]
|
70
70
|
# if passed an invalid collection name
|
71
71
|
#
|
72
72
|
# @return [Collection]
|
@@ -107,9 +107,11 @@ module Mongo
|
|
107
107
|
# a document specifying elements which must be present for a
|
108
108
|
# document to be included in the result set.
|
109
109
|
#
|
110
|
-
# @option opts [Array] :fields field names that should be returned in the result
|
110
|
+
# @option opts [Array, Hash] :fields field names that should be returned in the result
|
111
111
|
# set ("_id" will always be included). By limiting results to a certain subset of fields,
|
112
|
-
# you can cut down on network traffic and decoding time.
|
112
|
+
# you can cut down on network traffic and decoding time. If using a Hash, keys should be field
|
113
|
+
# names and values should be either 1 or 0, depending on whether you want to include or exclude
|
114
|
+
# the given field.
|
113
115
|
# @option opts [Integer] :skip number of documents to skip from the beginning of the result set
|
114
116
|
# @option opts [Integer] :limit maximum number of documents to return
|
115
117
|
# @option opts [Array] :sort an array of [key, direction] pairs to sort by. Direction should
|
@@ -179,7 +181,7 @@ module Mongo
|
|
179
181
|
spec = case spec_or_object_id
|
180
182
|
when nil
|
181
183
|
{}
|
182
|
-
when ObjectID
|
184
|
+
when BSON::ObjectID
|
183
185
|
{:_id => spec_or_object_id}
|
184
186
|
when Hash
|
185
187
|
spec_or_object_id
|
@@ -258,10 +260,10 @@ module Mongo
|
|
258
260
|
# @core remove remove-instance_method
|
259
261
|
def remove(selector={}, opts={})
|
260
262
|
# Initial byte is 0.
|
261
|
-
message = ByteBuffer.new([0, 0, 0, 0])
|
262
|
-
BSON_RUBY.serialize_cstr(message, "#{@db.name}.#{@name}")
|
263
|
+
message = BSON::ByteBuffer.new([0, 0, 0, 0])
|
264
|
+
BSON::BSON_RUBY.serialize_cstr(message, "#{@db.name}.#{@name}")
|
263
265
|
message.put_int(0)
|
264
|
-
message.put_array(BSON.serialize(selector, false, true).to_a)
|
266
|
+
message.put_array(BSON::BSON_CODER.serialize(selector, false, true).to_a)
|
265
267
|
|
266
268
|
if opts[:safe]
|
267
269
|
@connection.send_message_with_safe_check(Mongo::Constants::OP_DELETE, message, @db.name,
|
@@ -297,14 +299,14 @@ module Mongo
|
|
297
299
|
# @core update update-instance_method
|
298
300
|
def update(selector, document, options={})
|
299
301
|
# Initial byte is 0.
|
300
|
-
message = ByteBuffer.new([0, 0, 0, 0])
|
301
|
-
BSON_RUBY.serialize_cstr(message, "#{@db.name}.#{@name}")
|
302
|
+
message = BSON::ByteBuffer.new([0, 0, 0, 0])
|
303
|
+
BSON::BSON_RUBY.serialize_cstr(message, "#{@db.name}.#{@name}")
|
302
304
|
update_options = 0
|
303
305
|
update_options += 1 if options[:upsert]
|
304
306
|
update_options += 2 if options[:multi]
|
305
307
|
message.put_int(update_options)
|
306
|
-
message.put_array(BSON.serialize(selector, false, true).to_a)
|
307
|
-
message.put_array(BSON.serialize(document, false, true).to_a)
|
308
|
+
message.put_array(BSON::BSON_CODER.serialize(selector, false, true).to_a)
|
309
|
+
message.put_array(BSON::BSON_CODER.serialize(document, false, true).to_a)
|
308
310
|
if options[:safe]
|
309
311
|
@connection.send_message_with_safe_check(Mongo::Constants::OP_UPDATE, message, @db.name,
|
310
312
|
"#{@db.name}['#{@name}'].update(#{selector.inspect}, #{document.inspect})")
|
@@ -403,7 +405,6 @@ module Mongo
|
|
403
405
|
|
404
406
|
# Note: calling drop_indexes with no args will drop them all.
|
405
407
|
@db.drop_index(@name, '*')
|
406
|
-
|
407
408
|
end
|
408
409
|
|
409
410
|
# Drop the entire collection. USE WITH CAUTION.
|
@@ -411,17 +412,41 @@ module Mongo
|
|
411
412
|
@db.drop_collection(@name)
|
412
413
|
end
|
413
414
|
|
415
|
+
|
416
|
+
# Atomically update and return a document using MongoDB's findAndModify command. (MongoDB > 1.3.0)
|
417
|
+
#
|
418
|
+
# @option opts [Hash] :update (nil) the update operation to perform on the matched document.
|
419
|
+
# @option opts [Hash] :query ({}) a query selector document for matching the desired document.
|
420
|
+
# @option opts [Array, String, OrderedHash] :sort ({}) specify a sort option for the query using any
|
421
|
+
# of the sort options available for Cursor#sort. Sort order is important if the query will be matching
|
422
|
+
# multiple documents since only the first matching document will be updated and returned.
|
423
|
+
# @option opts [Boolean] :remove (false) If true, removes the the returned document from the collection.
|
424
|
+
# @option opts [Boolean] :new (false) If true, returns the updated document; otherwise, returns the document
|
425
|
+
# prior to update.
|
426
|
+
#
|
427
|
+
# @return [Hash] the matched document.
|
428
|
+
#
|
429
|
+
# @core findandmodify find_and_modify-instance_method
|
430
|
+
def find_and_modify(opts={})
|
431
|
+
cmd = OrderedHash.new
|
432
|
+
cmd[:findandmodify] = @name
|
433
|
+
cmd.merge!(opts)
|
434
|
+
cmd[:sort] = Mongo::Support.format_order_clause(opts[:sort]) if opts[:sort]
|
435
|
+
|
436
|
+
@db.command(cmd, false, true)['value']
|
437
|
+
end
|
438
|
+
|
414
439
|
# Perform a map/reduce operation on the current collection.
|
415
440
|
#
|
416
|
-
# @param [String, Code] map a map function, written in JavaScript.
|
417
|
-
# @param [String, Code] reduce a reduce function, written in JavaScript.
|
441
|
+
# @param [String, BSON::Code] map a map function, written in JavaScript.
|
442
|
+
# @param [String, BSON::Code] reduce a reduce function, written in JavaScript.
|
418
443
|
#
|
419
444
|
# @option opts [Hash] :query ({}) a query selector document, like what's passed to #find, to limit
|
420
445
|
# the operation to a subset of the collection.
|
421
446
|
# @option opts [Array] :sort ([]) an array of [key, direction] pairs to sort by. Direction should
|
422
447
|
# be specified as Mongo::ASCENDING (or :ascending / :asc) or Mongo::DESCENDING (or :descending / :desc)
|
423
448
|
# @option opts [Integer] :limit (nil) if passing a query, number of objects to return from the collection.
|
424
|
-
# @option opts [String, Code] :finalize (nil) a javascript function to apply to the result set after the
|
449
|
+
# @option opts [String, BSON::Code] :finalize (nil) a javascript function to apply to the result set after the
|
425
450
|
# map/reduce operation has finished.
|
426
451
|
# @option opts [String] :out (nil) the name of the output collection. If specified, the collection will not be treated as temporary.
|
427
452
|
# @option opts [Boolean] :keeptemp (false) if true, the generated collection will be persisted. default is false.
|
@@ -433,8 +458,8 @@ module Mongo
|
|
433
458
|
#
|
434
459
|
# @core mapreduce map_reduce-instance_method
|
435
460
|
def map_reduce(map, reduce, opts={})
|
436
|
-
map = Code.new(map) unless map.is_a?(Code)
|
437
|
-
reduce = Code.new(reduce) unless reduce.is_a?(Code)
|
461
|
+
map = BSON::Code.new(map) unless map.is_a?(BSON::Code)
|
462
|
+
reduce = BSON::Code.new(reduce) unless reduce.is_a?(BSON::Code)
|
438
463
|
|
439
464
|
hash = OrderedHash.new
|
440
465
|
hash['mapreduce'] = self.name
|
@@ -452,28 +477,18 @@ module Mongo
|
|
452
477
|
|
453
478
|
# Perform a group aggregation.
|
454
479
|
#
|
455
|
-
# @param [Array, String, Code, Nil] :key either 1) an array of fields to group by,
|
480
|
+
# @param [Array, String, BSON::Code, Nil] :key either 1) an array of fields to group by,
|
456
481
|
# 2) a javascript function to generate the key object, or 3) nil.
|
457
482
|
# @param [Hash] condition an optional document specifying a query to limit the documents over which group is run.
|
458
483
|
# @param [Hash] initial initial value of the aggregation counter object
|
459
|
-
# @param [String, Code] reduce aggregation function, in JavaScript
|
460
|
-
# @param [String, Code] finalize :: optional. a JavaScript function that receives and modifies
|
484
|
+
# @param [String, BSON::Code] reduce aggregation function, in JavaScript
|
485
|
+
# @param [String, BSON::Code] finalize :: optional. a JavaScript function that receives and modifies
|
461
486
|
# each of the resultant grouped objects. Available only when group is run
|
462
487
|
# with command set to true.
|
463
|
-
# @param [Nil] deprecated this param in a placeholder for a deprecated param. It will be removed
|
464
|
-
# in the next release.
|
465
488
|
#
|
466
489
|
# @return [Array] the grouped items.
|
467
|
-
def group(key, condition, initial, reduce, finalize=nil
|
468
|
-
|
469
|
-
# Warn of changed API post eval deprecation.
|
470
|
-
if finalize == true || finalize == false || deprecated
|
471
|
-
warn "The API for Collection#group has changed. 'Finalize' is now the fifth parameter, " +
|
472
|
-
"since it's no longer necessary to specify whether #group is run as a command. " +
|
473
|
-
"See http://api.mongodb.org/ruby/current/Mongo/Collection.html#group-instance_method for details."
|
474
|
-
end
|
475
|
-
|
476
|
-
reduce = Code.new(reduce) unless reduce.is_a?(Code)
|
490
|
+
def group(key, condition, initial, reduce, finalize=nil)
|
491
|
+
reduce = BSON::Code.new(reduce) unless reduce.is_a?(BSON::Code)
|
477
492
|
|
478
493
|
group_command = {
|
479
494
|
"group" => {
|
@@ -491,17 +506,14 @@ module Mongo
|
|
491
506
|
key.each { |k| key_value[k] = 1 }
|
492
507
|
else
|
493
508
|
key_type = "$keyf"
|
494
|
-
key_value = key.is_a?(Code) ? key : Code.new(key)
|
509
|
+
key_value = key.is_a?(BSON::Code) ? key : BSON::Code.new(key)
|
495
510
|
end
|
496
511
|
|
497
512
|
group_command["group"][key_type] = key_value
|
498
513
|
end
|
499
514
|
|
500
|
-
|
501
|
-
|
502
|
-
finalize = deprecated if deprecated.is_a?(String) || deprecated.is_a?(Code)
|
503
|
-
finalize = Code.new(finalize) if finalize.is_a?(String)
|
504
|
-
if finalize.is_a?(Code)
|
515
|
+
finalize = BSON::Code.new(finalize) if finalize.is_a?(String)
|
516
|
+
if finalize.is_a?(BSON::Code)
|
505
517
|
group_command['group']['finalize'] = finalize
|
506
518
|
end
|
507
519
|
|
@@ -556,7 +568,7 @@ module Mongo
|
|
556
568
|
#
|
557
569
|
# @param [String] new_name the new name for this collection
|
558
570
|
#
|
559
|
-
# @raise [
|
571
|
+
# @raise [Mongo::InvalidNSName] if +new_name+ is an invalid collection name.
|
560
572
|
def rename(new_name)
|
561
573
|
case new_name
|
562
574
|
when Symbol, String
|
@@ -567,13 +579,13 @@ module Mongo
|
|
567
579
|
new_name = new_name.to_s
|
568
580
|
|
569
581
|
if new_name.empty? or new_name.include? ".."
|
570
|
-
raise
|
582
|
+
raise Mongo::InvalidNSName, "collection names cannot be empty"
|
571
583
|
end
|
572
584
|
if new_name.include? "$"
|
573
|
-
raise
|
585
|
+
raise Mongo::InvalidNSName, "collection names must not contain '$'"
|
574
586
|
end
|
575
587
|
if new_name.match(/^\./) or new_name.match(/\.$/)
|
576
|
-
raise
|
588
|
+
raise Mongo::InvalidNSName, "collection names must not start or end with '.'"
|
577
589
|
end
|
578
590
|
|
579
591
|
@db.rename_collection(@name, new_name)
|
@@ -596,6 +608,13 @@ module Mongo
|
|
596
608
|
@db.collections_info(@name).next_document['options']
|
597
609
|
end
|
598
610
|
|
611
|
+
# Return stats on the collection. Uses MongoDB's collstats command.
|
612
|
+
#
|
613
|
+
# @return [Hash]
|
614
|
+
def stats
|
615
|
+
@db.command({:collstats => @name})
|
616
|
+
end
|
617
|
+
|
599
618
|
# Get the number of documents in this collection.
|
600
619
|
#
|
601
620
|
# @return [Integer]
|
@@ -629,9 +648,9 @@ module Mongo
|
|
629
648
|
# +check_keys+ setting.
|
630
649
|
def insert_documents(documents, collection_name=@name, check_keys=true, safe=false)
|
631
650
|
# Initial byte is 0.
|
632
|
-
message = ByteBuffer.new([0, 0, 0, 0])
|
633
|
-
BSON_RUBY.serialize_cstr(message, "#{@db.name}.#{collection_name}")
|
634
|
-
documents.each { |doc| message.put_array(BSON.serialize(doc, check_keys, true).to_a) }
|
651
|
+
message = BSON::ByteBuffer.new([0, 0, 0, 0])
|
652
|
+
BSON::BSON_RUBY.serialize_cstr(message, "#{@db.name}.#{collection_name}")
|
653
|
+
documents.each { |doc| message.put_array(BSON::BSON_CODER.serialize(doc, check_keys, true).to_a) }
|
635
654
|
if safe
|
636
655
|
@connection.send_message_with_safe_check(Mongo::Constants::OP_INSERT, message, @db.name,
|
637
656
|
"#{@db.name}['#{collection_name}'].insert(#{documents.inspect})")
|
data/lib/mongo/connection.rb
CHANGED
@@ -44,27 +44,21 @@ module Mongo
|
|
44
44
|
# If connecting to just one server, you may specify whether connection to slave is permitted.
|
45
45
|
# In all cases, the default host is "localhost" and the default port is 27017.
|
46
46
|
#
|
47
|
-
#
|
48
|
-
# * a server name, in which case port is 27017,
|
49
|
-
# * a port number, in which case the server is "localhost", or
|
50
|
-
# * an array containing [server_name, port_number]
|
47
|
+
# To specify a pair, use Connection.paired.
|
51
48
|
#
|
52
49
|
# Note that there are a few issues when using connection pooling with Ruby 1.9 on Windows. These
|
53
50
|
# should be resolved in the next release.
|
54
51
|
#
|
55
|
-
# @param [String, Hash]
|
56
|
-
# @param [Integer] port specify a port number here if only one host is being specified.
|
57
|
-
# specifying a pair of servers in +pair_or_host+.
|
52
|
+
# @param [String, Hash] host.
|
53
|
+
# @param [Integer] port specify a port number here if only one host is being specified.
|
58
54
|
#
|
59
55
|
# @option options [Boolean] :slave_ok (false) Must be set to +true+ when connecting
|
60
56
|
# to a single, slave node.
|
61
57
|
# @option options [Logger, #debug] :logger (nil) Logger instance to receive driver operation log.
|
62
|
-
# @option options [Boolean] :auto_reconnect DEPRECATED. See http://www.mongodb.org/display/DOCS/Replica+Pairs+in+Ruby
|
63
58
|
# @option options [Integer] :pool_size (1) The maximum number of socket connections that can be opened to the database.
|
64
59
|
# @option options [Float] :timeout (5.0) When all of the connections to the pool are checked out,
|
65
60
|
# this is the number of seconds to wait for a new connection to be released before throwing an exception.
|
66
61
|
#
|
67
|
-
#
|
68
62
|
# @example localhost, 27017
|
69
63
|
# Connection.new
|
70
64
|
#
|
@@ -77,25 +71,16 @@ module Mongo
|
|
77
71
|
# @example localhost, 3000, where this node may be a slave
|
78
72
|
# Connection.new("localhost", 3000, :slave_ok => true)
|
79
73
|
#
|
80
|
-
# @example DEPRECATED. To initialize a paired connection, use Connection.paired instead.
|
81
|
-
# Connection.new({:left => ["db1.example.com", 27017],
|
82
|
-
# :right => ["db2.example.com", 27017]})
|
83
|
-
#
|
84
|
-
# @example DEPRECATED. To initialize a paired connection, use Connection.paired instead.
|
85
|
-
# Connection.new({:left => ["db1.example.com", 27017],
|
86
|
-
# :right => ["db2.example.com", 27017]}, nil,
|
87
|
-
# :pool_size => 20, :timeout => 5)
|
88
|
-
#
|
89
74
|
# @see http://www.mongodb.org/display/DOCS/Replica+Pairs+in+Ruby Replica pairs in Ruby
|
90
75
|
#
|
91
76
|
# @core connections
|
92
|
-
def initialize(
|
77
|
+
def initialize(host=nil, port=nil, options={})
|
93
78
|
@auths = []
|
94
79
|
|
95
80
|
if block_given?
|
96
81
|
@nodes = yield self
|
97
82
|
else
|
98
|
-
@nodes = format_pair(
|
83
|
+
@nodes = format_pair(host, port)
|
99
84
|
end
|
100
85
|
|
101
86
|
# Host and port of current master.
|
@@ -118,10 +103,6 @@ module Mongo
|
|
118
103
|
@sockets = []
|
119
104
|
@checked_out = []
|
120
105
|
|
121
|
-
if options[:auto_reconnect]
|
122
|
-
warn(":auto_reconnect is deprecated. see http://www.mongodb.org/display/DOCS/Replica+Pairs+in+Ruby")
|
123
|
-
end
|
124
|
-
|
125
106
|
# slave_ok can be true only if one node is specified
|
126
107
|
@slave_ok = options[:slave_ok] && @nodes.length == 1
|
127
108
|
@logger = options[:logger] || nil
|
@@ -344,7 +325,7 @@ module Mongo
|
|
344
325
|
# Send a message to MongoDB, adding the necessary headers.
|
345
326
|
#
|
346
327
|
# @param [Integer] operation a MongoDB opcode.
|
347
|
-
# @param [ByteBuffer] message a message to send to the database.
|
328
|
+
# @param [BSON::ByteBuffer] message a message to send to the database.
|
348
329
|
# @param [String] log_message text version of +message+ for logging.
|
349
330
|
#
|
350
331
|
# @return [True]
|
@@ -363,7 +344,7 @@ module Mongo
|
|
363
344
|
# an exception if the operation has failed.
|
364
345
|
#
|
365
346
|
# @param [Integer] operation a MongoDB opcode.
|
366
|
-
# @param [ByteBuffer] message a message to send to the database.
|
347
|
+
# @param [BSON::ByteBuffer] message a message to send to the database.
|
367
348
|
# @param [String] db_name the name of the database. used on call to get_last_error.
|
368
349
|
# @param [String] log_message text version of +message+ for logging.
|
369
350
|
#
|
@@ -394,7 +375,7 @@ module Mongo
|
|
394
375
|
# Sends a message to the database and waits for the response.
|
395
376
|
#
|
396
377
|
# @param [Integer] operation a MongoDB opcode.
|
397
|
-
# @param [ByteBuffer] message a message to send to the database.
|
378
|
+
# @param [BSON::ByteBuffer] message a message to send to the database.
|
398
379
|
# @param [String] log_message text version of +message+ for logging.
|
399
380
|
# @param [Socket] socket a socket to use in lieu of checking out a new one.
|
400
381
|
#
|
@@ -479,12 +460,6 @@ module Mongo
|
|
479
460
|
case pair_or_host
|
480
461
|
when String
|
481
462
|
[[pair_or_host, port ? port.to_i : DEFAULT_PORT]]
|
482
|
-
when Hash
|
483
|
-
warn "Initializing a paired connection with Connection.new is deprecated. Use Connection.pair instead."
|
484
|
-
connections = []
|
485
|
-
connections << pair_val_to_connection(pair_or_host[:left])
|
486
|
-
connections << pair_val_to_connection(pair_or_host[:right])
|
487
|
-
connections
|
488
463
|
when nil
|
489
464
|
[['localhost', DEFAULT_PORT]]
|
490
465
|
end
|
@@ -625,7 +600,7 @@ module Mongo
|
|
625
600
|
end
|
626
601
|
|
627
602
|
def receive_header(sock)
|
628
|
-
header = ByteBuffer.new
|
603
|
+
header = BSON::ByteBuffer.new
|
629
604
|
header.put_array(receive_message_on_socket(16, sock).unpack("C*"))
|
630
605
|
unless header.size == STANDARD_HEADER_SIZE
|
631
606
|
raise "Short read for DB response header: " +
|
@@ -639,7 +614,7 @@ module Mongo
|
|
639
614
|
end
|
640
615
|
|
641
616
|
def receive_response_header(sock)
|
642
|
-
header_buf = ByteBuffer.new
|
617
|
+
header_buf = BSON::ByteBuffer.new
|
643
618
|
header_buf.put_array(receive_message_on_socket(RESPONSE_HEADER_SIZE, sock).unpack("C*"))
|
644
619
|
if header_buf.length != RESPONSE_HEADER_SIZE
|
645
620
|
raise "Short read for DB response header; " +
|
@@ -657,32 +632,32 @@ module Mongo
|
|
657
632
|
docs = []
|
658
633
|
number_remaining = number_received
|
659
634
|
while number_remaining > 0 do
|
660
|
-
buf = ByteBuffer.new
|
635
|
+
buf = BSON::ByteBuffer.new
|
661
636
|
buf.put_array(receive_message_on_socket(4, sock).unpack("C*"))
|
662
637
|
buf.rewind
|
663
638
|
size = buf.get_int
|
664
639
|
buf.put_array(receive_message_on_socket(size - 4, sock).unpack("C*"), 4)
|
665
640
|
number_remaining -= 1
|
666
641
|
buf.rewind
|
667
|
-
docs << BSON.deserialize(buf)
|
642
|
+
docs << BSON::BSON_CODER.deserialize(buf)
|
668
643
|
end
|
669
644
|
[docs, number_received, cursor_id]
|
670
645
|
end
|
671
646
|
|
672
647
|
def last_error_message(db_name)
|
673
|
-
message = ByteBuffer.new
|
648
|
+
message = BSON::ByteBuffer.new
|
674
649
|
message.put_int(0)
|
675
|
-
BSON_RUBY.serialize_cstr(message, "#{db_name}.$cmd")
|
650
|
+
BSON::BSON_RUBY.serialize_cstr(message, "#{db_name}.$cmd")
|
676
651
|
message.put_int(0)
|
677
652
|
message.put_int(-1)
|
678
|
-
message.put_array(BSON.serialize({:getlasterror => 1}, false).unpack("C*"))
|
653
|
+
message.put_array(BSON::BSON_CODER.serialize({:getlasterror => 1}, false).unpack("C*"))
|
679
654
|
add_message_headers(Mongo::Constants::OP_QUERY, message)
|
680
655
|
end
|
681
656
|
|
682
657
|
# Prepares a message for transmission to MongoDB by
|
683
658
|
# constructing a valid message header.
|
684
659
|
def add_message_headers(operation, message)
|
685
|
-
headers = ByteBuffer.new
|
660
|
+
headers = BSON::ByteBuffer.new
|
686
661
|
|
687
662
|
# Message size.
|
688
663
|
headers.put_int(16 + message.size)
|