mongo 0.1.0 → 0.15

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 (89) hide show
  1. data/README.rdoc +268 -71
  2. data/Rakefile +27 -62
  3. data/bin/bson_benchmark.rb +59 -0
  4. data/bin/mongo_console +3 -3
  5. data/bin/run_test_script +19 -0
  6. data/bin/standard_benchmark +109 -0
  7. data/examples/admin.rb +41 -0
  8. data/examples/benchmarks.rb +42 -0
  9. data/examples/blog.rb +76 -0
  10. data/examples/capped.rb +23 -0
  11. data/examples/cursor.rb +47 -0
  12. data/examples/gridfs.rb +87 -0
  13. data/examples/index_test.rb +125 -0
  14. data/examples/info.rb +30 -0
  15. data/examples/queries.rb +69 -0
  16. data/examples/simple.rb +23 -0
  17. data/examples/strict.rb +34 -0
  18. data/examples/types.rb +35 -0
  19. data/lib/mongo.rb +9 -2
  20. data/lib/mongo/admin.rb +65 -68
  21. data/lib/mongo/collection.rb +379 -117
  22. data/lib/mongo/connection.rb +151 -0
  23. data/lib/mongo/cursor.rb +271 -216
  24. data/lib/mongo/db.rb +500 -315
  25. data/lib/mongo/errors.rb +26 -0
  26. data/lib/mongo/gridfs.rb +16 -0
  27. data/lib/mongo/gridfs/chunk.rb +92 -0
  28. data/lib/mongo/gridfs/grid_store.rb +464 -0
  29. data/lib/mongo/message.rb +16 -0
  30. data/lib/mongo/message/get_more_message.rb +24 -13
  31. data/lib/mongo/message/insert_message.rb +29 -11
  32. data/lib/mongo/message/kill_cursors_message.rb +23 -12
  33. data/lib/mongo/message/message.rb +74 -62
  34. data/lib/mongo/message/message_header.rb +35 -24
  35. data/lib/mongo/message/msg_message.rb +21 -9
  36. data/lib/mongo/message/opcodes.rb +26 -15
  37. data/lib/mongo/message/query_message.rb +63 -43
  38. data/lib/mongo/message/remove_message.rb +29 -12
  39. data/lib/mongo/message/update_message.rb +30 -13
  40. data/lib/mongo/query.rb +97 -89
  41. data/lib/mongo/types/binary.rb +25 -21
  42. data/lib/mongo/types/code.rb +30 -0
  43. data/lib/mongo/types/dbref.rb +19 -23
  44. data/lib/mongo/types/objectid.rb +130 -116
  45. data/lib/mongo/types/regexp_of_holding.rb +27 -31
  46. data/lib/mongo/util/bson.rb +273 -160
  47. data/lib/mongo/util/byte_buffer.rb +32 -28
  48. data/lib/mongo/util/ordered_hash.rb +88 -42
  49. data/lib/mongo/util/xml_to_ruby.rb +18 -15
  50. data/mongo-ruby-driver.gemspec +103 -0
  51. data/test/mongo-qa/_common.rb +8 -0
  52. data/test/mongo-qa/admin +26 -0
  53. data/test/mongo-qa/capped +22 -0
  54. data/test/mongo-qa/count1 +18 -0
  55. data/test/mongo-qa/dbs +22 -0
  56. data/test/mongo-qa/find +10 -0
  57. data/test/mongo-qa/find1 +15 -0
  58. data/test/mongo-qa/gridfs_in +16 -0
  59. data/test/mongo-qa/gridfs_out +17 -0
  60. data/test/mongo-qa/indices +49 -0
  61. data/test/mongo-qa/remove +25 -0
  62. data/test/mongo-qa/stress1 +35 -0
  63. data/test/mongo-qa/test1 +11 -0
  64. data/test/mongo-qa/update +18 -0
  65. data/{tests → test}/test_admin.rb +25 -16
  66. data/test/test_bson.rb +268 -0
  67. data/{tests → test}/test_byte_buffer.rb +0 -0
  68. data/test/test_chunk.rb +84 -0
  69. data/test/test_collection.rb +282 -0
  70. data/test/test_connection.rb +101 -0
  71. data/test/test_cursor.rb +321 -0
  72. data/test/test_db.rb +196 -0
  73. data/test/test_db_api.rb +798 -0
  74. data/{tests → test}/test_db_connection.rb +4 -3
  75. data/test/test_grid_store.rb +284 -0
  76. data/{tests → test}/test_message.rb +1 -1
  77. data/test/test_objectid.rb +105 -0
  78. data/{tests → test}/test_ordered_hash.rb +55 -0
  79. data/{tests → test}/test_round_trip.rb +13 -9
  80. data/test/test_threading.rb +37 -0
  81. metadata +74 -32
  82. data/bin/validate +0 -51
  83. data/lib/mongo/mongo.rb +0 -74
  84. data/lib/mongo/types/undefined.rb +0 -31
  85. data/tests/test_bson.rb +0 -135
  86. data/tests/test_cursor.rb +0 -66
  87. data/tests/test_db.rb +0 -51
  88. data/tests/test_db_api.rb +0 -349
  89. data/tests/test_objectid.rb +0 -88
@@ -1,17 +1,17 @@
1
1
  # --
2
2
  # Copyright (C) 2008-2009 10gen Inc.
3
3
  #
4
- # This program is free software: you can redistribute it and/or modify it
5
- # under the terms of the GNU Affero General Public License, version 3, as
6
- # published by the Free Software Foundation.
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
7
  #
8
- # This program is distributed in the hope that it will be useful, but WITHOUT
9
- # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10
- # FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License
11
- # for more details.
8
+ # http://www.apache.org/licenses/LICENSE-2.0
12
9
  #
13
- # You should have received a copy of the GNU Affero General Public License
14
- # along with this program. If not, see <http://www.gnu.org/licenses/>.
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
15
  # ++
16
16
 
17
17
  # A byte buffer.
@@ -66,14 +66,10 @@ class ByteBuffer
66
66
  @cursor += array.length
67
67
  end
68
68
 
69
- if RUBY_VERSION >= '1.9'
70
- def put_int(i, offset=nil)
71
- put_array([i].pack(@int_pack_order).split(//).collect{|c| c.bytes.first}, offset)
72
- end
73
- else
74
- def put_int(i, offset=nil)
75
- put_array([i].pack(@int_pack_order).split(//).collect{|c| c[0]}, offset)
76
- end
69
+ def put_int(i, offset=nil)
70
+ a = []
71
+ [i].pack(@int_pack_order).each_byte { |b| a << b }
72
+ put_array(a, offset)
77
73
  end
78
74
 
79
75
  def put_long(i, offset=nil)
@@ -87,14 +83,10 @@ class ByteBuffer
87
83
  end
88
84
  end
89
85
 
90
- if RUBY_VERSION >= '1.9'
91
- def put_double(d, offset=nil)
92
- put_array([d].pack(@double_pack_order).split(//).collect{|c| c.bytes.first}, offset)
93
- end
94
- else
95
- def put_double(d, offset=nil)
96
- put_array([d].pack(@double_pack_order).split(//).collect{|c| c[0]}, offset)
97
- end
86
+ def put_double(d, offset=nil)
87
+ a = []
88
+ [d].pack(@double_pack_order).each_byte { |b| a << b }
89
+ put_array(a, offset)
98
90
  end
99
91
 
100
92
  # If +size+ == nil, returns one byte. Else returns array of bytes of length
@@ -108,7 +100,11 @@ class ByteBuffer
108
100
  if one_byte
109
101
  @buf[start]
110
102
  else
111
- @buf[start, len]
103
+ if @buf.respond_to? "unpack"
104
+ @buf[start, len].unpack("C*")
105
+ else
106
+ @buf[start, len]
107
+ end
112
108
  end
113
109
  end
114
110
 
@@ -143,11 +139,19 @@ class ByteBuffer
143
139
  end
144
140
 
145
141
  def to_a
146
- @buf
142
+ if @buf.respond_to? "unpack"
143
+ @buf.unpack("C*")
144
+ else
145
+ @buf
146
+ end
147
147
  end
148
148
 
149
149
  def to_s
150
- @buf.pack("C*")
150
+ if @buf.respond_to? "pack"
151
+ @buf.pack("C*")
152
+ else
153
+ @buf
154
+ end
151
155
  end
152
156
 
153
157
  def dump
@@ -1,17 +1,17 @@
1
1
  # --
2
2
  # Copyright (C) 2008-2009 10gen Inc.
3
3
  #
4
- # This program is free software: you can redistribute it and/or modify it
5
- # under the terms of the GNU Affero General Public License, version 3, as
6
- # published by the Free Software Foundation.
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
7
  #
8
- # This program is distributed in the hope that it will be useful, but WITHOUT
9
- # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10
- # FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License
11
- # for more details.
8
+ # http://www.apache.org/licenses/LICENSE-2.0
12
9
  #
13
- # You should have received a copy of the GNU Affero General Public License
14
- # along with this program. If not, see <http://www.gnu.org/licenses/>.
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
15
  # ++
16
16
 
17
17
  # A hash in which the order of keys are preserved.
@@ -20,49 +20,95 @@
20
20
  # Hash already keeps its keys ordered by order of insertion.
21
21
  class OrderedHash < Hash
22
22
 
23
+ def ==(other)
24
+ begin
25
+ !other.nil? &&
26
+ keys == other.keys &&
27
+ values == other.values
28
+ rescue
29
+ false
30
+ end
31
+ end
32
+
23
33
  # We only need the body of this class if the RUBY_VERSION is before 1.9
24
34
  if RUBY_VERSION < '1.9'
35
+ attr_accessor :ordered_keys
25
36
 
26
- attr_accessor :ordered_keys
37
+ def self.[] *args
38
+ oh = OrderedHash.new
39
+ if Hash === args[0]
40
+ oh.merge! args[0]
41
+ elsif (args.size % 2) != 0
42
+ raise ArgumentError, "odd number of elements for Hash"
43
+ else
44
+ 0.step(args.size - 1, 2) do |key|
45
+ value = key + 1
46
+ oh[args[key]] = args[value]
47
+ end
48
+ end
49
+ oh
50
+ end
27
51
 
28
- def keys
29
- @ordered_keys || []
30
- end
52
+ def initialize(*a, &b)
53
+ super
54
+ @ordered_keys = []
55
+ end
31
56
 
32
- def []=(key, value)
33
- @ordered_keys ||= []
34
- @ordered_keys << key unless @ordered_keys.include?(key)
35
- super(key, value)
36
- end
57
+ def keys
58
+ @ordered_keys || []
59
+ end
37
60
 
38
- def each
39
- @ordered_keys ||= []
40
- @ordered_keys.each { |k| yield k, self[k] }
41
- end
61
+ def []=(key, value)
62
+ @ordered_keys ||= []
63
+ @ordered_keys << key unless @ordered_keys.include?(key)
64
+ super(key, value)
65
+ end
42
66
 
43
- def values
44
- collect { |k, v| v }
45
- end
67
+ def each
68
+ @ordered_keys ||= []
69
+ @ordered_keys.each { |k| yield k, self[k] }
70
+ self
71
+ end
46
72
 
47
- def merge(other)
48
- oh = self.dup
49
- oh.merge!(other)
50
- oh
51
- end
73
+ def values
74
+ collect { |k, v| v }
75
+ end
52
76
 
53
- def merge!(other)
54
- @ordered_keys ||= []
55
- @ordered_keys += other.keys # unordered if not an OrderedHash
56
- @ordered_keys.uniq!
57
- super(other)
58
- end
77
+ def merge(other)
78
+ oh = self.dup
79
+ oh.merge!(other)
80
+ oh
81
+ end
59
82
 
60
- def inspect
61
- str = '{'
62
- str << (@ordered_keys || []).collect { |k| "\"#{k}\"=>#{self.[](k).inspect}" }.join(", ")
63
- str << '}'
64
- end
83
+ def merge!(other)
84
+ @ordered_keys ||= []
85
+ @ordered_keys += other.keys # unordered if not an OrderedHash
86
+ @ordered_keys.uniq!
87
+ super(other)
88
+ end
65
89
 
66
- end # Ruby before 1.9
90
+ def inspect
91
+ str = '{'
92
+ str << (@ordered_keys || []).collect { |k| "\"#{k}\"=>#{self.[](k).inspect}" }.join(", ")
93
+ str << '}'
94
+ end
67
95
 
96
+ def delete(key, &block)
97
+ @ordered_keys.delete(key) if @ordered_keys
98
+ super
99
+ end
100
+
101
+ def delete_if(&block)
102
+ self.each { |k,v|
103
+ if yield k, v
104
+ delete(k)
105
+ end
106
+ }
107
+ end
108
+
109
+ def clear
110
+ super
111
+ @ordered_keys = []
112
+ end
113
+ end
68
114
  end
@@ -1,17 +1,17 @@
1
1
  # --
2
2
  # Copyright (C) 2008-2009 10gen Inc.
3
3
  #
4
- # This program is free software: you can redistribute it and/or modify it
5
- # under the terms of the GNU Affero General Public License, version 3, as
6
- # published by the Free Software Foundation.
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
7
  #
8
- # This program is distributed in the hope that it will be useful, but WITHOUT
9
- # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10
- # FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License
11
- # for more details.
8
+ # http://www.apache.org/licenses/LICENSE-2.0
12
9
  #
13
- # You should have received a copy of the GNU Affero General Public License
14
- # along with this program. If not, see <http://www.gnu.org/licenses/>.
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
15
  # ++
16
16
 
17
17
  require 'rexml/document'
@@ -21,7 +21,7 @@ require 'mongo'
21
21
  # an OrderedHash.
22
22
  class XMLToRuby
23
23
 
24
- include XGen::Mongo::Driver
24
+ include Mongo
25
25
 
26
26
  def xml_to_ruby(io)
27
27
  doc = REXML::Document.new(io)
@@ -42,10 +42,15 @@ class XMLToRuby
42
42
  e.text.to_i
43
43
  when 'number'
44
44
  e.text.to_f
45
- when 'string', 'code'
45
+ when 'string'
46
46
  e.text.to_s
47
+ when 'code'
48
+ Code.new(e.text.to_s)
47
49
  when 'binary'
48
- Base64.decode64(e.text.to_s).to_mongo_binary
50
+ bin = Binary.new
51
+ decoded = Base64.decode64(e.text.to_s)
52
+ decoded.each_byte { |b| bin.put(b) }
53
+ bin
49
54
  when 'symbol'
50
55
  e.text.to_s.intern
51
56
  when 'boolean'
@@ -58,8 +63,6 @@ class XMLToRuby
58
63
  regex_to_ruby(e.elements)
59
64
  when 'null'
60
65
  nil
61
- when 'undefined'
62
- Undefined.new
63
66
  when 'doc'
64
67
  doc_to_ruby(e)
65
68
  else
@@ -96,7 +99,7 @@ class XMLToRuby
96
99
  def dbref_to_ruby(elements)
97
100
  ns = elements['ns'].text
98
101
  oid_str = elements['oid'].text
99
- DBRef.new(nil, nil, nil, ns, ObjectID.from_string(oid_str))
102
+ DBRef.new(ns, ObjectID.from_string(oid_str))
100
103
  end
101
104
 
102
105
  end
@@ -0,0 +1,103 @@
1
+ # We need to list all of the included files because we aren't allowed to use
2
+ # Dir[...] in the github sandbox.
3
+ PACKAGE_FILES = ['README.rdoc', 'Rakefile', 'mongo-ruby-driver.gemspec',
4
+ 'bin/bson_benchmark.rb',
5
+ 'bin/mongo_console',
6
+ 'bin/run_test_script',
7
+ 'bin/standard_benchmark',
8
+ 'examples/admin.rb',
9
+ 'examples/benchmarks.rb',
10
+ 'examples/blog.rb',
11
+ 'examples/capped.rb',
12
+ 'examples/cursor.rb',
13
+ 'examples/gridfs.rb',
14
+ 'examples/index_test.rb',
15
+ 'examples/info.rb',
16
+ 'examples/queries.rb',
17
+ 'examples/simple.rb',
18
+ 'examples/strict.rb',
19
+ 'examples/types.rb',
20
+ 'lib/mongo/admin.rb',
21
+ 'lib/mongo/collection.rb',
22
+ 'lib/mongo/connection.rb',
23
+ 'lib/mongo/cursor.rb',
24
+ 'lib/mongo/db.rb',
25
+ 'lib/mongo/gridfs/chunk.rb',
26
+ 'lib/mongo/gridfs/grid_store.rb',
27
+ 'lib/mongo/gridfs.rb',
28
+ 'lib/mongo/errors.rb',
29
+ 'lib/mongo/message/get_more_message.rb',
30
+ 'lib/mongo/message/insert_message.rb',
31
+ 'lib/mongo/message/kill_cursors_message.rb',
32
+ 'lib/mongo/message/message.rb',
33
+ 'lib/mongo/message/message_header.rb',
34
+ 'lib/mongo/message/msg_message.rb',
35
+ 'lib/mongo/message/opcodes.rb',
36
+ 'lib/mongo/message/query_message.rb',
37
+ 'lib/mongo/message/remove_message.rb',
38
+ 'lib/mongo/message/update_message.rb',
39
+ 'lib/mongo/message.rb',
40
+ 'lib/mongo/query.rb',
41
+ 'lib/mongo/types/binary.rb',
42
+ 'lib/mongo/types/code.rb',
43
+ 'lib/mongo/types/dbref.rb',
44
+ 'lib/mongo/types/objectid.rb',
45
+ 'lib/mongo/types/regexp_of_holding.rb',
46
+ 'lib/mongo/util/bson.rb',
47
+ 'lib/mongo/util/byte_buffer.rb',
48
+ 'lib/mongo/util/ordered_hash.rb',
49
+ 'lib/mongo/util/xml_to_ruby.rb',
50
+ 'lib/mongo.rb']
51
+ TEST_FILES = ['test/mongo-qa/_common.rb',
52
+ 'test/mongo-qa/admin',
53
+ 'test/mongo-qa/capped',
54
+ 'test/mongo-qa/count1',
55
+ 'test/mongo-qa/dbs',
56
+ 'test/mongo-qa/find',
57
+ 'test/mongo-qa/find1',
58
+ 'test/mongo-qa/gridfs_in',
59
+ 'test/mongo-qa/gridfs_out',
60
+ 'test/mongo-qa/indices',
61
+ 'test/mongo-qa/remove',
62
+ 'test/mongo-qa/stress1',
63
+ 'test/mongo-qa/test1',
64
+ 'test/mongo-qa/update',
65
+ 'test/test_admin.rb',
66
+ 'test/test_bson.rb',
67
+ 'test/test_byte_buffer.rb',
68
+ 'test/test_chunk.rb',
69
+ 'test/test_collection.rb',
70
+ 'test/test_connection.rb',
71
+ 'test/test_cursor.rb',
72
+ 'test/test_db.rb',
73
+ 'test/test_db_api.rb',
74
+ 'test/test_db_connection.rb',
75
+ 'test/test_grid_store.rb',
76
+ 'test/test_message.rb',
77
+ 'test/test_objectid.rb',
78
+ 'test/test_ordered_hash.rb',
79
+ 'test/test_threading.rb',
80
+ 'test/test_round_trip.rb']
81
+
82
+ Gem::Specification.new do |s|
83
+ s.name = 'mongo'
84
+
85
+ # be sure to change this constant in lib/mongo.rb as well
86
+ s.version = '0.15'
87
+
88
+ s.platform = Gem::Platform::RUBY
89
+ s.summary = 'Ruby driver for the MongoDB'
90
+ s.description = 'A Ruby driver for MongoDB. For more information about Mongo, see http://www.mongodb.org.'
91
+
92
+ s.require_paths = ['lib']
93
+ s.files = PACKAGE_FILES
94
+ s.test_files = TEST_FILES
95
+
96
+ s.has_rdoc = true
97
+ s.rdoc_options = ['--main', 'README.rdoc', '--inline-source']
98
+ s.extra_rdoc_files = ['README.rdoc']
99
+
100
+ s.authors = ['Jim Menard', 'Mike Dirolf']
101
+ s.email = 'mongodb-dev@googlegroups.com'
102
+ s.homepage = 'http://www.mongodb.org'
103
+ end
@@ -0,0 +1,8 @@
1
+ $LOAD_PATH[0,0] = File.join(File.dirname(__FILE__), '../..', 'lib')
2
+ require 'mongo'
3
+
4
+ DEFAULT_HOST = '127.0.0.1'
5
+ DEFAULT_PORT = 27017
6
+ DEFAULT_DB = 'driver_test_framework'
7
+
8
+ include Mongo
@@ -0,0 +1,26 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require File.join(File.dirname(__FILE__), '_common.rb')
4
+ db = Connection.new(DEFAULT_HOST, DEFAULT_PORT).db(DEFAULT_DB)
5
+
6
+ db.collection('test').insert({'test' => 1})
7
+ admin = db.admin
8
+
9
+ if $DEBUG
10
+ db.drop_collection('tester')
11
+ admin.profiling_level = :slow_only
12
+ end
13
+
14
+ ['test', 'pdlskwmf', '$'].each { |name|
15
+ begin
16
+ admin.validate_collection(name)
17
+ puts 'true'
18
+ rescue => ex
19
+ puts 'false'
20
+ end
21
+ }
22
+
23
+ level_xlation = {:off => 'off', :all => 'all', :slow_only => 'slowOnly'}
24
+ puts level_xlation[admin.profiling_level]
25
+ admin.profiling_level = :off
26
+ puts level_xlation[admin.profiling_level]