mongo 0.18.3 → 0.19

Sign up to get free protection for your applications and to get access to all the features.
Files changed (62) hide show
  1. data/README.rdoc +41 -50
  2. data/Rakefile +14 -4
  3. data/examples/gridfs.rb +25 -70
  4. data/lib/mongo.rb +4 -2
  5. data/lib/mongo/collection.rb +70 -89
  6. data/lib/mongo/connection.rb +203 -43
  7. data/lib/mongo/cursor.rb +7 -7
  8. data/lib/mongo/db.rb +61 -18
  9. data/lib/mongo/exceptions.rb +7 -1
  10. data/lib/mongo/gridfs.rb +8 -1
  11. data/lib/mongo/gridfs/chunk.rb +2 -1
  12. data/lib/mongo/gridfs/grid.rb +90 -0
  13. data/lib/mongo/gridfs/grid_file_system.rb +113 -0
  14. data/lib/mongo/gridfs/grid_io.rb +339 -0
  15. data/lib/mongo/gridfs/grid_store.rb +43 -18
  16. data/lib/mongo/types/binary.rb +5 -1
  17. data/lib/mongo/types/code.rb +1 -1
  18. data/lib/mongo/types/dbref.rb +3 -1
  19. data/lib/mongo/types/min_max_keys.rb +1 -1
  20. data/lib/mongo/types/objectid.rb +16 -55
  21. data/lib/mongo/types/regexp_of_holding.rb +1 -1
  22. data/lib/mongo/util/bson_c.rb +2 -2
  23. data/lib/mongo/util/bson_ruby.rb +22 -11
  24. data/lib/mongo/util/byte_buffer.rb +1 -1
  25. data/lib/mongo/util/conversions.rb +1 -1
  26. data/lib/mongo/util/ordered_hash.rb +6 -1
  27. data/lib/mongo/util/server_version.rb +1 -1
  28. data/lib/mongo/util/support.rb +1 -1
  29. data/mongo-ruby-driver.gemspec +1 -1
  30. data/test/auxillary/authentication_test.rb +68 -0
  31. data/test/auxillary/autoreconnect_test.rb +41 -0
  32. data/test/binary_test.rb +15 -0
  33. data/test/{test_bson.rb → bson_test.rb} +63 -6
  34. data/test/{test_byte_buffer.rb → byte_buffer_test.rb} +0 -0
  35. data/test/{test_chunk.rb → chunk_test.rb} +0 -0
  36. data/test/{test_collection.rb → collection_test.rb} +35 -39
  37. data/test/{test_connection.rb → connection_test.rb} +33 -3
  38. data/test/{test_conversions.rb → conversions_test.rb} +0 -0
  39. data/test/{test_cursor.rb → cursor_test.rb} +0 -7
  40. data/test/{test_db_api.rb → db_api_test.rb} +3 -6
  41. data/test/{test_db_connection.rb → db_connection_test.rb} +0 -0
  42. data/test/{test_db.rb → db_test.rb} +33 -15
  43. data/test/grid_file_system_test.rb +210 -0
  44. data/test/grid_io_test.rb +78 -0
  45. data/test/{test_grid_store.rb → grid_store_test.rb} +33 -2
  46. data/test/grid_test.rb +87 -0
  47. data/test/{test_objectid.rb → objectid_test.rb} +2 -33
  48. data/test/{test_ordered_hash.rb → ordered_hash_test.rb} +4 -0
  49. data/test/{test_slave_connection.rb → slave_connection_test.rb} +0 -0
  50. data/test/test_helper.rb +2 -2
  51. data/test/{test_threading.rb → threading_test.rb} +0 -0
  52. data/test/unit/collection_test.rb +12 -3
  53. data/test/unit/connection_test.rb +85 -24
  54. data/test/unit/cursor_test.rb +1 -2
  55. data/test/unit/db_test.rb +70 -69
  56. metadata +27 -23
  57. data/bin/objectid_benchmark.rb +0 -23
  58. data/bin/perf.rb +0 -30
  59. data/lib/mongo/admin.rb +0 -95
  60. data/lib/mongo/util/xml_to_ruby.rb +0 -112
  61. data/test/test_admin.rb +0 -67
  62. data/test/test_round_trip.rb +0 -114
@@ -1,23 +0,0 @@
1
- require 'rubygems'
2
- require 'mongo'
3
- require 'benchmark'
4
-
5
- TRIALS = 500000
6
-
7
- include Mongo
8
-
9
- Benchmark.bm do |x|
10
-
11
- x.report('original') do
12
- TRIALS.times do
13
- ObjectID.new(nil, true)
14
- end
15
- end
16
-
17
- x.report('new') do
18
- TRIALS.times do
19
- ObjectID.new(nil, false)
20
- end
21
- end
22
-
23
- end
data/bin/perf.rb DELETED
@@ -1,30 +0,0 @@
1
- #!/usr/bin/env ruby
2
- require 'rubygems'
3
- require 'mongo'
4
- require 'benchmark'
5
- require 'socket'
6
- require 'digest/md5'
7
- require 'thread'
8
-
9
- include Mongo
10
-
11
- c = Mongo::Connection.new('localhost', 27017, :pool_size => 10)
12
- coll = c['perf']['docs']
13
- coll.remove
14
-
15
- doc = {'name' => 'kyle',
16
- 'languages' => ['ruby', 'javascript', 'c'],
17
- 'date' => Time.now}
18
-
19
- TRIES = 5000
20
- @threads = []
21
- 100.times do |n|
22
- @threads << Thread.new do
23
- 50.times do |n|
24
- doc['n'] = n*n
25
- coll.insert(doc)
26
- doc.delete(:_id)
27
- end
28
- end
29
- end
30
- @threads.join
data/lib/mongo/admin.rb DELETED
@@ -1,95 +0,0 @@
1
- # --
2
- # Copyright (C) 2008-2009 10gen Inc.
3
- #
4
- # Licensed under the Apache License, Version 2.0 (the "License");
5
- # you may not use this file except in compliance with the License.
6
- # You may obtain a copy of the License at
7
- #
8
- # http://www.apache.org/licenses/LICENSE-2.0
9
- #
10
- # Unless required by applicable law or agreed to in writing, software
11
- # distributed under the License is distributed on an "AS IS" BASIS,
12
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
- # See the License for the specific language governing permissions and
14
- # limitations under the License.
15
- # ++
16
-
17
- module Mongo
18
-
19
- # @deprecated this class is deprecated. Methods defined here will
20
- # henceforth be available in Mongo::DB.
21
- class Admin
22
-
23
- def initialize(db)
24
- warn "The Admin class has been DEPRECATED. All admin methods now exist in DB."
25
- @db = db
26
- end
27
-
28
- # Return the current database profiling level.
29
- #
30
- # @return [Symbol] :off, :slow_only, or :all
31
- #
32
- # @deprecated please use DB#profiling_level instead.
33
- def profiling_level
34
- warn "Admin#profiling_level has been DEPRECATED. Please use DB#profiling_level instead."
35
- oh = OrderedHash.new
36
- oh[:profile] = -1
37
- doc = @db.command(oh)
38
- raise "Error with profile command: #{doc.inspect}" unless @db.ok?(doc) && doc['was'].kind_of?(Numeric)
39
- case doc['was'].to_i
40
- when 0
41
- :off
42
- when 1
43
- :slow_only
44
- when 2
45
- :all
46
- else
47
- raise "Error: illegal profiling level value #{doc['was']}"
48
- end
49
- end
50
-
51
- # Set database profiling level to :off, :slow_only, or :all.
52
- #
53
- # @deprecated please use DB#profiling_level= instead.
54
- def profiling_level=(level)
55
- warn "Admin#profiling_level= has been DEPRECATED. Please use DB#profiling_level= instead."
56
- oh = OrderedHash.new
57
- oh[:profile] = case level
58
- when :off
59
- 0
60
- when :slow_only
61
- 1
62
- when :all
63
- 2
64
- else
65
- raise "Error: illegal profiling level value #{level}"
66
- end
67
- doc = @db.command(oh)
68
- raise "Error with profile command: #{doc.inspect}" unless @db.ok?(doc)
69
- end
70
-
71
- # Returns an array containing current profiling information.
72
- #
73
- # @deprecated please use DB#profiling_info instead.
74
- def profiling_info
75
- warn "Admin#profiling_info has been DEPRECATED. Please use DB#profiling_info instead."
76
- Cursor.new(Collection.new(@db, DB::SYSTEM_PROFILE_COLLECTION), :selector => {}).to_a
77
- end
78
-
79
- # Validate a named collection by raising an exception if there is a
80
- # problem or returning an interesting hash (see especially the
81
- # 'result' string value) if all is well.
82
- #
83
- # @deprecated please use DB#validate_collection instead.
84
- def validate_collection(name)
85
- warn "Admin#validate_collection has been DEPRECATED. Please use DB#validate_collection instead."
86
- doc = @db.command(:validate => name)
87
- raise "Error with validate command: #{doc.inspect}" unless @db.ok?(doc)
88
- result = doc['result']
89
- raise "Error with validation data: #{doc.inspect}" unless result.kind_of?(String)
90
- raise "Error: invalid collection #{name}: #{doc.inspect}" if result =~ /\b(exception|corrupt)\b/i
91
- doc
92
- end
93
-
94
- end
95
- end
@@ -1,112 +0,0 @@
1
- # --
2
- # Copyright (C) 2008-2009 10gen Inc.
3
- #
4
- # Licensed under the Apache License, Version 2.0 (the "License");
5
- # you may not use this file except in compliance with the License.
6
- # You may obtain a copy of the License at
7
- #
8
- # http://www.apache.org/licenses/LICENSE-2.0
9
- #
10
- # Unless required by applicable law or agreed to in writing, software
11
- # distributed under the License is distributed on an "AS IS" BASIS,
12
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
- # See the License for the specific language governing permissions and
14
- # limitations under the License.
15
- # ++
16
-
17
- require 'rexml/document'
18
- require 'mongo'
19
-
20
- # @deprecated
21
- # Converts a .xson file (an XML file that describes a Mongo-type document) to
22
- # an OrderedHash.
23
- class XMLToRuby
24
-
25
- include Mongo
26
-
27
- def xml_to_ruby(io)
28
- warn "XMLToRuby is deprecated. The .xson format is not longer in use."
29
- doc = REXML::Document.new(io)
30
- doc_to_ruby(doc.root.elements['doc'])
31
- end
32
-
33
- protected
34
-
35
- def element_to_ruby(e)
36
- warn "XMLToRuby is deprecated. The .xson format is not longer in use."
37
- type = e.name
38
- child = e.elements[1]
39
- case type
40
- when 'oid'
41
- ObjectID.from_string(e.text)
42
- when 'ref'
43
- dbref_to_ruby(e.elements)
44
- when 'int'
45
- e.text.to_i
46
- when 'number'
47
- e.text.to_f
48
- when 'string'
49
- e.text.to_s
50
- when 'code'
51
- Code.new(e.text.to_s)
52
- when 'binary'
53
- bin = Binary.new
54
- decoded = Base64.decode64(e.text.to_s)
55
- decoded.each_byte { |b| bin.put(b) }
56
- bin
57
- when 'symbol'
58
- e.text.to_s.intern
59
- when 'boolean'
60
- e.text.to_s == 'true'
61
- when 'array'
62
- array_to_ruby(e.elements)
63
- when 'date'
64
- Time.at(e.text.to_f / 1000.0)
65
- when 'regex'
66
- regex_to_ruby(e.elements)
67
- when 'null'
68
- nil
69
- when 'doc'
70
- doc_to_ruby(e)
71
- else
72
- raise "Unknown type #{type} in element with name #{e.attributes['name']}"
73
- end
74
- end
75
-
76
- def doc_to_ruby(element)
77
- warn "XMLToRuby is deprecated. The .xson format is not longer in use."
78
- oh = OrderedHash.new
79
- element.elements.each { |e| oh[e.attributes['name']] = element_to_ruby(e) }
80
- oh
81
- end
82
-
83
- def array_to_ruby(elements)
84
- warn "XMLToRuby is deprecated. The .xson format is not longer in use."
85
- a = []
86
- elements.each { |e|
87
- index_str = e.attributes['name']
88
- a[index_str.to_i] = element_to_ruby(e)
89
- }
90
- a
91
- end
92
-
93
- def regex_to_ruby(elements)
94
- warn "XMLToRuby is deprecated. The .xson format is not longer in use."
95
- pattern = elements['pattern'].text
96
- options_str = elements['options'].text || ''
97
-
98
- options = 0
99
- options |= Regexp::IGNORECASE if options_str.include?('i')
100
- options |= Regexp::MULTILINE if options_str.include?('m')
101
- options |= Regexp::EXTENDED if options_str.include?('x')
102
- Regexp.new(pattern, options)
103
- end
104
-
105
- def dbref_to_ruby(elements)
106
- warn "XMLToRuby is deprecated. The .xson format is not longer in use."
107
- ns = elements['ns'].text
108
- oid_str = elements['oid'].text
109
- DBRef.new(ns, ObjectID.from_string(oid_str))
110
- end
111
-
112
- end
data/test/test_admin.rb DELETED
@@ -1,67 +0,0 @@
1
- require 'test/test_helper'
2
-
3
- # NOTE: assumes Mongo is running
4
- class AdminTest < Test::Unit::TestCase
5
-
6
- include Mongo
7
-
8
- @@db = Connection.new(ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost',
9
- ENV['MONGO_RUBY_DRIVER_PORT'] || Connection::DEFAULT_PORT).db('ruby-mongo-test')
10
- @@coll = @@db.collection('test')
11
-
12
- def setup
13
- # Insert some data to make sure the database itself exists.
14
- @@coll.remove
15
- @r1 = @@coll.insert('a' => 1) # collection not created until it's used
16
- @@coll_full_name = 'ruby-mongo-test.test'
17
- @admin = @@db.admin
18
- end
19
-
20
- def teardown
21
- @admin.profiling_level = :off
22
- @@coll.remove if @@coll
23
- @@db.error
24
- end
25
-
26
- def test_default_profiling_level
27
- assert_equal :off, @admin.profiling_level
28
- end
29
-
30
- def test_change_profiling_level
31
- @admin.profiling_level = :slow_only
32
- assert_equal :slow_only, @admin.profiling_level
33
- @admin.profiling_level = :off
34
- assert_equal :off, @admin.profiling_level
35
- @admin.profiling_level = :all
36
- assert_equal :all, @admin.profiling_level
37
- begin
38
- @admin.profiling_level = :medium
39
- fail "shouldn't be able to do this"
40
- rescue
41
- end
42
- end
43
-
44
- def test_profiling_info
45
- # Perform at least one query while profiling so we have something to see.
46
- @admin.profiling_level = :all
47
- @@coll.find()
48
- @admin.profiling_level = :off
49
-
50
- info = @admin.profiling_info
51
- assert_kind_of Array, info
52
- assert info.length >= 1
53
- first = info.first
54
- assert_kind_of String, first['info']
55
- assert_kind_of Time, first['ts']
56
- assert_kind_of Numeric, first['millis']
57
- end
58
-
59
- def test_validate_collection
60
- doc = @admin.validate_collection(@@coll.name)
61
- assert_not_nil doc
62
- result = doc['result']
63
- assert_not_nil result
64
- assert_match /firstExtent/, result
65
- end
66
-
67
- end
@@ -1,114 +0,0 @@
1
- HERE = File.dirname(__FILE__)
2
- $LOAD_PATH[0,0] = File.join(HERE, '..', 'lib')
3
- require 'test/test_helper'
4
- require 'mongo/util/xml_to_ruby'
5
-
6
- # For each xml/bson file in the data subdirectory, we turn the XML into an
7
- # OrderedHash and then test both Ruby-to-BSON and BSON-to-Ruby translations.
8
- #
9
- # There is a whole other project that includes similar tests
10
- # (http://github.com/mongodb/mongo-qa). If the directory ../../mongo-qa
11
- # exists, (that is, the top-level dir of mongo-qa is next to the top-level dir
12
- # of this project), then we find the BSON test files there and use those, too.
13
- class RoundTripTest < Test::Unit::TestCase
14
-
15
- include Mongo
16
-
17
- @@ruby = nil
18
-
19
- def setup
20
- unless @@ruby
21
- names = Dir[File.join(HERE, 'data', '*.xml')].collect {|f| File.basename(f).sub(/\.xml$/, '') }
22
- @@ruby = {}
23
- names.each { |name|
24
- File.open(File.join(HERE, 'data', "#{name}.xml")) { |f|
25
- @@ruby[name] = XMLToRuby.new.xml_to_ruby(f)
26
- }
27
- }
28
- end
29
- end
30
-
31
- def test_dummy
32
- assert true
33
- end
34
-
35
- def self.create_test_for_round_trip_files_in_dir(dir)
36
- names = Dir[File.join(dir, '*.xson')].collect {|f| File.basename(f).sub(/\.xson$/, '') }
37
- names.each { |name|
38
- eval <<EOS
39
- def test_#{name}_#{dir.gsub(/[^a-zA-Z0-9_]/, '_')}
40
- one_round_trip("#{dir}", "#{name}")
41
- end
42
- EOS
43
- }
44
- end
45
-
46
- # Dynamically generate one test for each test file. This way, if one test
47
- # fails the others will still run.
48
- create_test_for_round_trip_files_in_dir(File.join(HERE, 'data'))
49
- mongo_qa_dir = File.join(HERE, '../..', 'mongo-qa/modules/bson_tests/tests')
50
- if File.exist?(mongo_qa_dir)
51
- %w(basic_types complex single_types).each { |subdir_name|
52
- create_test_for_round_trip_files_in_dir(File.join(mongo_qa_dir, subdir_name))
53
- }
54
- end
55
-
56
- # Round-trip comparisons of Ruby-to-BSON and back.
57
- # * Take the objects that were read from XML
58
- # * Turn them into BSON bytes
59
- # * Compare that with the BSON files we have
60
- # * Turn those BSON bytes back in to Ruby objects
61
- # * Turn them back into BSON bytes
62
- # * Compare that with the BSON files we have (or the bytes that were already
63
- # generated)
64
- def one_round_trip(dir, name)
65
- obj = File.open(File.join(dir, "#{name}.xson")) { |f|
66
- begin
67
- XMLToRuby.new.xml_to_ruby(f)
68
- rescue => ex # unsupported type
69
- return
70
- end
71
- }
72
-
73
- File.open(File.join(dir, "#{name}.bson"), 'rb') { |f|
74
- # Read the BSON from the file
75
- bson = f.read
76
-
77
- # Turn the Ruby object into BSON bytes and compare with the BSON bytes
78
- # from the file.
79
- bson_from_ruby = BSON.serialize(obj)
80
-
81
- begin
82
- assert_equal bson.length, bson_from_ruby.to_s.length
83
- assert_equal bson, bson_from_ruby.to_s
84
- rescue => ex
85
- # File.open(File.join(dir, "#{name}_out_a.bson"), 'wb') { |f| # DEBUG
86
- # bson_from_ruby.each { |b| f.putc(b) }
87
- # }
88
- raise ex
89
- end
90
-
91
- # Turn those BSON bytes back into a Ruby object.
92
- #
93
- # We're passing a nil db to the contructor here, but that's OK because
94
- # the BSON DBRef bytes don't contain the db object in any case, and we
95
- # don't care what the database is.
96
- obj_from_bson = BSON.deserialize(bson_from_ruby)
97
- assert_kind_of OrderedHash, obj_from_bson
98
-
99
- # Turn that Ruby object into BSON and compare it to the original BSON
100
- # bytes.
101
- bson_from_ruby = BSON.serialize(obj_from_bson)
102
- begin
103
- assert_equal bson.length, bson_from_ruby.to_s.length
104
- assert_equal bson, bson_from_ruby.to_s
105
- rescue => ex
106
- # File.open(File.join(dir, "#{name}_out_b.bson"), 'wb') { |f| # DEBUG
107
- # bson_from_ruby.each { |b| f.putc(b) }
108
- # }
109
- raise ex
110
- end
111
- }
112
- end
113
-
114
- end