mongodb-mongo 0.12 → 0.13

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 (81) hide show
  1. data/README.rdoc +12 -12
  2. data/Rakefile +1 -1
  3. data/bin/bson_benchmark.rb +1 -1
  4. data/bin/mongo_console +3 -3
  5. data/bin/run_test_script +2 -2
  6. data/bin/standard_benchmark +3 -3
  7. data/examples/admin.rb +3 -3
  8. data/examples/benchmarks.rb +2 -2
  9. data/examples/blog.rb +4 -4
  10. data/examples/capped.rb +3 -3
  11. data/examples/cursor.rb +3 -3
  12. data/examples/gridfs.rb +4 -4
  13. data/examples/index_test.rb +11 -11
  14. data/examples/info.rb +3 -3
  15. data/examples/queries.rb +3 -3
  16. data/examples/simple.rb +3 -3
  17. data/examples/strict.rb +3 -3
  18. data/examples/types.rb +4 -9
  19. data/lib/mongo.rb +35 -3
  20. data/lib/mongo/admin.rb +56 -60
  21. data/lib/mongo/collection.rb +368 -320
  22. data/lib/mongo/connection.rb +166 -0
  23. data/lib/mongo/cursor.rb +206 -209
  24. data/lib/mongo/db.rb +478 -489
  25. data/lib/mongo/errors.rb +8 -9
  26. data/lib/mongo/gridfs/chunk.rb +66 -70
  27. data/lib/mongo/gridfs/grid_store.rb +406 -410
  28. data/lib/mongo/message/get_more_message.rb +8 -13
  29. data/lib/mongo/message/insert_message.rb +7 -11
  30. data/lib/mongo/message/kill_cursors_message.rb +7 -12
  31. data/lib/mongo/message/message.rb +58 -62
  32. data/lib/mongo/message/message_header.rb +19 -24
  33. data/lib/mongo/message/msg_message.rb +5 -9
  34. data/lib/mongo/message/opcodes.rb +10 -15
  35. data/lib/mongo/message/query_message.rb +42 -46
  36. data/lib/mongo/message/remove_message.rb +8 -12
  37. data/lib/mongo/message/update_message.rb +9 -13
  38. data/lib/mongo/query.rb +84 -88
  39. data/lib/mongo/types/binary.rb +13 -17
  40. data/lib/mongo/types/code.rb +9 -13
  41. data/lib/mongo/types/dbref.rb +10 -14
  42. data/lib/mongo/types/objectid.rb +103 -107
  43. data/lib/mongo/types/regexp_of_holding.rb +18 -22
  44. data/lib/mongo/types/undefined.rb +7 -10
  45. data/lib/mongo/util/bson.rb +4 -9
  46. data/lib/mongo/util/xml_to_ruby.rb +1 -3
  47. data/mongo-ruby-driver.gemspec +33 -32
  48. data/{tests → test}/mongo-qa/_common.rb +1 -1
  49. data/{tests → test}/mongo-qa/admin +1 -1
  50. data/{tests → test}/mongo-qa/capped +1 -1
  51. data/{tests → test}/mongo-qa/count1 +4 -4
  52. data/{tests → test}/mongo-qa/dbs +1 -1
  53. data/{tests → test}/mongo-qa/find +1 -1
  54. data/{tests → test}/mongo-qa/find1 +1 -1
  55. data/{tests → test}/mongo-qa/gridfs_in +2 -2
  56. data/{tests → test}/mongo-qa/gridfs_out +2 -2
  57. data/{tests → test}/mongo-qa/indices +2 -2
  58. data/{tests → test}/mongo-qa/remove +1 -1
  59. data/{tests → test}/mongo-qa/stress1 +1 -1
  60. data/{tests → test}/mongo-qa/test1 +1 -1
  61. data/{tests → test}/mongo-qa/update +1 -1
  62. data/{tests → test}/test_admin.rb +3 -3
  63. data/{tests → test}/test_bson.rb +4 -4
  64. data/{tests → test}/test_byte_buffer.rb +0 -0
  65. data/{tests → test}/test_chunk.rb +4 -4
  66. data/{tests → test}/test_collection.rb +42 -4
  67. data/{tests/test_mongo.rb → test/test_connection.rb} +35 -11
  68. data/test/test_cursor.rb +223 -0
  69. data/{tests → test}/test_db.rb +12 -12
  70. data/{tests → test}/test_db_api.rb +28 -33
  71. data/{tests → test}/test_db_connection.rb +3 -3
  72. data/{tests → test}/test_grid_store.rb +4 -4
  73. data/{tests → test}/test_message.rb +1 -1
  74. data/{tests → test}/test_objectid.rb +3 -3
  75. data/{tests → test}/test_ordered_hash.rb +0 -0
  76. data/{tests → test}/test_round_trip.rb +6 -2
  77. data/{tests → test}/test_threading.rb +3 -3
  78. data/test/test_xgen.rb +73 -0
  79. metadata +33 -32
  80. data/lib/mongo/mongo.rb +0 -164
  81. data/tests/test_cursor.rb +0 -121
@@ -20,11 +20,10 @@ require 'test/unit'
20
20
 
21
21
  # NOTE: assumes Mongo is running
22
22
  class TestCollection < Test::Unit::TestCase
23
- include XGen::Mongo
24
- include XGen::Mongo::Driver
23
+ include Mongo
25
24
 
26
- @@db = Mongo.new(ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost',
27
- ENV['MONGO_RUBY_DRIVER_PORT'] || Mongo::DEFAULT_PORT).db('ruby-mongo-test')
25
+ @@db = Connection.new(ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost',
26
+ ENV['MONGO_RUBY_DRIVER_PORT'] || Connection::DEFAULT_PORT).db('ruby-mongo-test')
28
27
  @@test = @@db.collection("test")
29
28
 
30
29
  def setup
@@ -100,6 +99,18 @@ class TestCollection < Test::Unit::TestCase
100
99
  end
101
100
  end
102
101
 
102
+ def test_count
103
+ @@test.drop
104
+
105
+ assert_equal 0, @@test.count
106
+ @@test.save("x" => 1)
107
+ @@test.save("x" => 2)
108
+ assert_equal 2, @@test.count
109
+
110
+ # TODO remove this test - it's deprecated
111
+ assert_equal 1, @@test.count("x" => 1)
112
+ end
113
+
103
114
  def test_find_one
104
115
  id = @@test.save("hello" => "world", "foo" => "bar")
105
116
 
@@ -112,6 +123,7 @@ class TestCollection < Test::Unit::TestCase
112
123
 
113
124
  assert @@test.find_one(nil, :fields => ["hello"]).include?("hello")
114
125
  assert !@@test.find_one(nil, :fields => ["foo"]).include?("hello")
126
+ assert_equal ["_id"], @@test.find_one(nil, :fields => []).keys()
115
127
 
116
128
  assert_equal nil, @@test.find_one("hello" => "foo")
117
129
  assert_equal nil, @@test.find_one(OrderedHash["hello", "foo"])
@@ -139,5 +151,31 @@ class TestCollection < Test::Unit::TestCase
139
151
  @@test.save(doc)
140
152
  assert doc.include? :_id
141
153
  end
154
+
155
+ def test_optional_find_block
156
+ 10.times do |i|
157
+ @@test.save("i" => i)
158
+ end
159
+
160
+ x = nil
161
+ @@test.find("i" => 2) { |cursor|
162
+ x = cursor.count()
163
+ }
164
+ assert_equal 1, x
165
+
166
+ i = 0
167
+ @@test.find({}, :offset => 5) do |cursor|
168
+ cursor.each do |doc|
169
+ i = i + 1
170
+ end
171
+ end
172
+ assert_equal 5, i
173
+
174
+ c = nil
175
+ @@test.find() do |cursor|
176
+ c = cursor
177
+ end
178
+ assert c.closed?
179
+ end
142
180
  end
143
181
 
@@ -3,14 +3,14 @@ require 'mongo'
3
3
  require 'test/unit'
4
4
 
5
5
  # NOTE: assumes Mongo is running
6
- class MongoTest < Test::Unit::TestCase
6
+ class TestConnection < Test::Unit::TestCase
7
7
 
8
- include XGen::Mongo::Driver
8
+ include Mongo
9
9
 
10
10
  def setup
11
11
  @host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
12
- @port = ENV['MONGO_RUBY_DRIVER_PORT'] || Mongo::DEFAULT_PORT
13
- @mongo = Mongo.new(@host, @port)
12
+ @port = ENV['MONGO_RUBY_DRIVER_PORT'] || Connection::DEFAULT_PORT
13
+ @mongo = Connection.new(@host, @port)
14
14
  end
15
15
 
16
16
  def teardown
@@ -67,23 +67,47 @@ class MongoTest < Test::Unit::TestCase
67
67
  end
68
68
 
69
69
  def test_pair
70
- db = Mongo.new({:left => ['foo', 123]})
70
+ db = Connection.new({:left => ['foo', 123]})
71
71
  pair = db.instance_variable_get('@pair')
72
72
  assert_equal 2, pair.length
73
73
  assert_equal ['foo', 123], pair[0]
74
- assert_equal ['localhost', Mongo::DEFAULT_PORT], pair[1]
74
+ assert_equal ['localhost', Connection::DEFAULT_PORT], pair[1]
75
75
 
76
- db = Mongo.new({:right => 'bar'})
76
+ db = Connection.new({:right => 'bar'})
77
77
  pair = db.instance_variable_get('@pair')
78
78
  assert_equal 2, pair.length
79
- assert_equal ['localhost', Mongo::DEFAULT_PORT], pair[0]
80
- assert_equal ['bar', Mongo::DEFAULT_PORT], pair[1]
79
+ assert_equal ['localhost', Connection::DEFAULT_PORT], pair[0]
80
+ assert_equal ['bar', Connection::DEFAULT_PORT], pair[1]
81
81
 
82
- db = Mongo.new({:right => ['foo', 123], :left => 'bar'})
82
+ db = Connection.new({:right => ['foo', 123], :left => 'bar'})
83
83
  pair = db.instance_variable_get('@pair')
84
84
  assert_equal 2, pair.length
85
- assert_equal ['bar', Mongo::DEFAULT_PORT], pair[0]
85
+ assert_equal ['bar', Connection::DEFAULT_PORT], pair[0]
86
86
  assert_equal ['foo', 123], pair[1]
87
87
  end
88
+ end
89
+
90
+ # Test for deprecated Mongo class
91
+ class TestMongo < Test::Unit::TestCase
92
+
93
+ include Mongo
94
+
95
+ def setup
96
+ @host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
97
+ @port = ENV['MONGO_RUBY_DRIVER_PORT'] || Mongo::DEFAULT_PORT
98
+ @mongo = Mongo.new(@host, @port)
99
+ end
88
100
 
101
+ def test_database_info
102
+ @mongo.drop_database('ruby-mongo-info-test')
103
+ @mongo.db('ruby-mongo-info-test').collection('info-test').insert('a' => 1)
104
+
105
+ info = @mongo.database_info
106
+ assert_not_nil info
107
+ assert_kind_of Hash, info
108
+ assert_not_nil info['ruby-mongo-info-test']
109
+ assert info['ruby-mongo-info-test'] > 0
110
+
111
+ @mongo.drop_database('ruby-mongo-info-test')
112
+ end
89
113
  end
@@ -0,0 +1,223 @@
1
+ $LOAD_PATH[0,0] = File.join(File.dirname(__FILE__), '..', 'lib')
2
+ require 'mongo'
3
+ require 'test/unit'
4
+
5
+ # NOTE: assumes Mongo is running
6
+ class CursorTest < Test::Unit::TestCase
7
+
8
+ include Mongo
9
+
10
+ @@db = Connection.new(ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost',
11
+ ENV['MONGO_RUBY_DRIVER_PORT'] || Connection::DEFAULT_PORT).db('ruby-mongo-test')
12
+ @@coll = @@db.collection('test')
13
+
14
+ def setup
15
+ @@coll.clear
16
+ @@coll.insert('a' => 1) # collection not created until it's used
17
+ @@coll_full_name = 'ruby-mongo-test.test'
18
+ end
19
+
20
+ def teardown
21
+ @@coll.clear
22
+ @@db.error
23
+ end
24
+
25
+ def test_explain
26
+ cursor = @@coll.find('a' => 1)
27
+ explaination = cursor.explain
28
+ assert_not_nil explaination['cursor']
29
+ assert_kind_of Numeric, explaination['n']
30
+ assert_kind_of Numeric, explaination['millis']
31
+ assert_kind_of Numeric, explaination['nscanned']
32
+ end
33
+
34
+ def test_count
35
+ @@coll.clear
36
+
37
+ assert_equal 0, @@coll.find().count()
38
+
39
+ 10.times do |i|
40
+ @@coll.save("x" => i)
41
+ end
42
+
43
+ assert_equal 10, @@coll.find().count()
44
+ assert_kind_of Integer, @@coll.find().count()
45
+ assert_equal 10, @@coll.find({}, :limit => 5).count()
46
+ assert_equal 10, @@coll.find({}, :offset => 5).count()
47
+
48
+ assert_equal 1, @@coll.find({"x" => 1}).count()
49
+ assert_equal 5, @@coll.find({"x" => {"$lt" => 5}}).count()
50
+
51
+ a = @@coll.find()
52
+ b = a.count()
53
+ a.each do |doc|
54
+ break
55
+ end
56
+ assert_equal b, a.count()
57
+
58
+ assert_equal 0, @@db['acollectionthatdoesn'].count()
59
+ end
60
+
61
+ def test_close_no_query_sent
62
+ begin
63
+ cursor = @@coll.find('a' => 1)
64
+ cursor.close
65
+ assert cursor.closed?
66
+ rescue => ex
67
+ fail ex.to_s
68
+ end
69
+ end
70
+
71
+ def test_refill_via_get_more
72
+ begin
73
+ assert_equal 1, @@coll.count
74
+ 1000.times { |i|
75
+ assert_equal 1 + i, @@coll.count
76
+ @@coll.insert('a' => i)
77
+ }
78
+
79
+ assert_equal 1001, @@coll.count
80
+ count = 0
81
+ @@coll.find.each { |obj|
82
+ count += obj['a']
83
+ }
84
+ assert_equal 1001, @@coll.count
85
+
86
+ # do the same thing again for debugging
87
+ assert_equal 1001, @@coll.count
88
+ count2 = 0
89
+ @@coll.find.each { |obj|
90
+ count2 += obj['a']
91
+ }
92
+ assert_equal 1001, @@coll.count
93
+
94
+ assert_equal count, count2
95
+ assert_equal 499501, count
96
+ rescue Test::Unit::AssertionFailedError => ex
97
+ p @@db.collection_names
98
+ Process.exit 1
99
+ end
100
+ end
101
+
102
+ def test_refill_via_get_more_alt_coll
103
+ begin
104
+ coll = @@db.collection('test-alt-coll')
105
+ coll.clear
106
+ coll.insert('a' => 1) # collection not created until it's used
107
+ assert_equal 1, coll.count
108
+
109
+ 1000.times { |i|
110
+ assert_equal 1 + i, coll.count
111
+ coll.insert('a' => i)
112
+ }
113
+
114
+ assert_equal 1001, coll.count
115
+ count = 0
116
+ coll.find.each { |obj|
117
+ count += obj['a']
118
+ }
119
+ assert_equal 1001, coll.count
120
+
121
+ # do the same thing again for debugging
122
+ assert_equal 1001, coll.count
123
+ count2 = 0
124
+ coll.find.each { |obj|
125
+ count2 += obj['a']
126
+ }
127
+ assert_equal 1001, coll.count
128
+
129
+ assert_equal count, count2
130
+ assert_equal 499501, count
131
+ rescue Test::Unit::AssertionFailedError => ex
132
+ p @@db.collection_names
133
+ Process.exit 1
134
+ end
135
+ end
136
+
137
+ def test_close_after_query_sent
138
+ begin
139
+ cursor = @@coll.find('a' => 1)
140
+ cursor.next_object
141
+ cursor.close
142
+ assert cursor.closed?
143
+ rescue => ex
144
+ fail ex.to_s
145
+ end
146
+ end
147
+
148
+ def test_kill_cursors
149
+ @@coll.drop
150
+
151
+ client_cursors = @@db.db_command("cursorInfo" => 1)["clientCursors_size"]
152
+ by_location = @@db.db_command("cursorInfo" => 1)["byLocation_size"]
153
+
154
+ 10000.times do |i|
155
+ @@coll.insert("i" => i)
156
+ end
157
+
158
+ assert_equal(client_cursors,
159
+ @@db.db_command("cursorInfo" => 1)["clientCursors_size"])
160
+ assert_equal(by_location,
161
+ @@db.db_command("cursorInfo" => 1)["byLocation_size"])
162
+
163
+ 10.times do |i|
164
+ @@coll.find_one()
165
+ end
166
+
167
+ assert_equal(client_cursors,
168
+ @@db.db_command("cursorInfo" => 1)["clientCursors_size"])
169
+ assert_equal(by_location,
170
+ @@db.db_command("cursorInfo" => 1)["byLocation_size"])
171
+
172
+ 10.times do |i|
173
+ a = @@coll.find()
174
+ a.next_object()
175
+ a.close()
176
+ end
177
+
178
+ assert_equal(client_cursors,
179
+ @@db.db_command("cursorInfo" => 1)["clientCursors_size"])
180
+ assert_equal(by_location,
181
+ @@db.db_command("cursorInfo" => 1)["byLocation_size"])
182
+
183
+ a = @@coll.find()
184
+ a.next_object()
185
+
186
+ assert_not_equal(client_cursors,
187
+ @@db.db_command("cursorInfo" => 1)["clientCursors_size"])
188
+ assert_not_equal(by_location,
189
+ @@db.db_command("cursorInfo" => 1)["byLocation_size"])
190
+
191
+ a.close()
192
+
193
+ assert_equal(client_cursors,
194
+ @@db.db_command("cursorInfo" => 1)["clientCursors_size"])
195
+ assert_equal(by_location,
196
+ @@db.db_command("cursorInfo" => 1)["byLocation_size"])
197
+
198
+ a = @@coll.find({}, :limit => 10).next_object()
199
+
200
+ assert_equal(client_cursors,
201
+ @@db.db_command("cursorInfo" => 1)["clientCursors_size"])
202
+ assert_equal(by_location,
203
+ @@db.db_command("cursorInfo" => 1)["byLocation_size"])
204
+
205
+ @@coll.find() do |cursor|
206
+ cursor.next_object()
207
+ end
208
+
209
+ assert_equal(client_cursors,
210
+ @@db.db_command("cursorInfo" => 1)["clientCursors_size"])
211
+ assert_equal(by_location,
212
+ @@db.db_command("cursorInfo" => 1)["byLocation_size"])
213
+
214
+ @@coll.find() { |cursor|
215
+ cursor.next_object()
216
+ }
217
+
218
+ assert_equal(client_cursors,
219
+ @@db.db_command("cursorInfo" => 1)["clientCursors_size"])
220
+ assert_equal(by_location,
221
+ @@db.db_command("cursorInfo" => 1)["byLocation_size"])
222
+ end
223
+ end
@@ -5,7 +5,7 @@ require 'test/unit'
5
5
 
6
6
  class TestPKFactory
7
7
  def create_pk(row)
8
- row['_id'] ||= XGen::Mongo::Driver::ObjectID.new
8
+ row['_id'] ||= Mongo::ObjectID.new
9
9
  row
10
10
  end
11
11
  end
@@ -13,11 +13,11 @@ end
13
13
  # NOTE: assumes Mongo is running
14
14
  class DBTest < Test::Unit::TestCase
15
15
 
16
- include XGen::Mongo::Driver
16
+ include Mongo
17
17
 
18
18
  @@host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
19
- @@port = ENV['MONGO_RUBY_DRIVER_PORT'] || Mongo::DEFAULT_PORT
20
- @@db = Mongo.new(@@host, @@port).db('ruby-mongo-test')
19
+ @@port = ENV['MONGO_RUBY_DRIVER_PORT'] || Connection::DEFAULT_PORT
20
+ @@db = Connection.new(@@host, @@port).db('ruby-mongo-test')
21
21
  @@users = @@db.collection('system.users')
22
22
 
23
23
  def setup
@@ -41,7 +41,7 @@ class DBTest < Test::Unit::TestCase
41
41
  rescue => ex
42
42
  assert_match /NilClass/, ex.to_s
43
43
  ensure
44
- @@db = Mongo.new(@@host, @@port).db('ruby-mongo-test')
44
+ @@db = Connection.new(@@host, @@port).db('ruby-mongo-test')
45
45
  @@users = @@db.collection('system.users')
46
46
  end
47
47
  end
@@ -66,15 +66,15 @@ class DBTest < Test::Unit::TestCase
66
66
  def test_pair
67
67
  @@db.close
68
68
  @@users = nil
69
- @@db = Mongo.new({:left => "this-should-fail", :right => [@@host, @@port]}).db('ruby-mongo-test')
69
+ @@db = Connection.new({:left => "this-should-fail", :right => [@@host, @@port]}).db('ruby-mongo-test')
70
70
  assert @@db.connected?
71
71
  ensure
72
- @@db = Mongo.new(@@host, @@port).db('ruby-mongo-test') unless @@db.connected?
72
+ @@db = Connection.new(@@host, @@port).db('ruby-mongo-test') unless @@db.connected?
73
73
  @@users = @@db.collection('system.users')
74
74
  end
75
75
 
76
76
  def test_pk_factory
77
- db = Mongo.new(@@host, @@port).db('ruby-mongo-test', :pk => TestPKFactory.new)
77
+ db = Connection.new(@@host, @@port).db('ruby-mongo-test', :pk => TestPKFactory.new)
78
78
  coll = db.collection('test')
79
79
  coll.clear
80
80
 
@@ -85,7 +85,7 @@ class DBTest < Test::Unit::TestCase
85
85
  assert_not_nil oid
86
86
  assert_equal insert_id, oid
87
87
 
88
- oid = XGen::Mongo::Driver::ObjectID.new
88
+ oid = ObjectID.new
89
89
  data = {'_id' => oid, 'name' => 'Barney', 'age' => 41}
90
90
  coll.insert(data)
91
91
  row = coll.find_one({'name' => data['name']})
@@ -97,7 +97,7 @@ class DBTest < Test::Unit::TestCase
97
97
  end
98
98
 
99
99
  def test_pk_factory_reset
100
- db = Mongo.new(@@host, @@port).db('ruby-mongo-test')
100
+ db = Connection.new(@@host, @@port).db('ruby-mongo-test')
101
101
  db.pk_factory = Object.new # first time
102
102
  begin
103
103
  db.pk_factory = Object.new
@@ -121,7 +121,7 @@ class DBTest < Test::Unit::TestCase
121
121
 
122
122
  def test_auto_connect
123
123
  @@db.close
124
- db = Mongo.new(@@host, @@port, :auto_reconnect => true).db('ruby-mongo-test')
124
+ db = Connection.new(@@host, @@port, :auto_reconnect => true).db('ruby-mongo-test')
125
125
  assert db.connected?
126
126
  assert db.auto_reconnect?
127
127
  db.close
@@ -130,7 +130,7 @@ class DBTest < Test::Unit::TestCase
130
130
  db.collection('test').insert('a' => 1)
131
131
  assert db.connected?
132
132
  ensure
133
- @@db = Mongo.new(@@host, @@port).db('ruby-mongo-test')
133
+ @@db = Connection.new(@@host, @@port).db('ruby-mongo-test')
134
134
  @@users = @@db.collection('system.users')
135
135
  end
136
136