mongodb-mongo 0.6.4 → 0.6.5

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -8,6 +8,8 @@ begin
8
8
  require 'rake/contrib/rubyforgepublisher'
9
9
  rescue LoadError
10
10
  end
11
+ require 'rbconfig'
12
+ include Config
11
13
 
12
14
  # NOTE: some of the tests assume Mongo is running
13
15
  Rake::TestTask.new do |t|
@@ -26,14 +28,6 @@ task :publish => [:rdoc] do
26
28
  Rake::RubyForgePublisher.new(GEM, RUBYFORGE_USER).upload
27
29
  end
28
30
 
29
- desc "Compile the extension"
30
- task :compile do
31
- cd 'ext/cbson'
32
- ruby 'extconf.rb'
33
- sh 'make'
34
- cp 'cbson.bundle', '../../lib/mongo/ext/cbson.bundle'
35
- end
36
-
37
31
  namespace :gem do
38
32
 
39
33
  desc "Install the gem locally"
@@ -45,6 +39,15 @@ gem build mongo-ruby-driver.gemspec &&
45
39
  EOS
46
40
  end
47
41
 
42
+ desc "Install the optional c extensions"
43
+ task :install_extensions do
44
+ sh <<EOS
45
+ gem build mongo-extensions.gemspec &&
46
+ sudo gem install mongo_ext-*.gem &&
47
+ rm mongo_ext-*.gem
48
+ EOS
49
+ end
50
+
48
51
  end
49
52
 
50
53
  task :default => :list
@@ -0,0 +1,59 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $LOAD_PATH[0,0] = File.join(File.dirname(__FILE__), '..', 'lib')
4
+ require 'mongo'
5
+
6
+ include XGen::Mongo::Driver
7
+
8
+ TRIALS = 100000
9
+
10
+ def encode(doc)
11
+ t0 = Time.new
12
+ b = BSON.new
13
+ TRIALS.times { |i|
14
+ b = BSON.new
15
+ b.serialize doc
16
+ }
17
+ print "took: #{Time.now.to_f - t0.to_f}\n"
18
+ return b
19
+ end
20
+
21
+ def decode(bson)
22
+ t0 = Time.new
23
+ doc = nil
24
+ TRIALS.times { |i|
25
+ doc = bson.deserialize
26
+ }
27
+ print "took: #{Time.now.to_f - t0.to_f}\n"
28
+ return doc
29
+ end
30
+
31
+ TEST_CASES = [{},
32
+ {
33
+ "hello" => "world"
34
+ },
35
+ {
36
+ "hello" => "world",
37
+ "mike" => "something",
38
+ "here's" => "another"
39
+ },
40
+ {
41
+ "int" => 200,
42
+ "bool" => true,
43
+ "an int" => 20,
44
+ "a bool" => false
45
+ },
46
+ {
47
+ "this" => 5,
48
+ "is" => {"a" => true},
49
+ "big" => [true, 5.5],
50
+ "object" => nil
51
+ }]
52
+
53
+ TEST_CASES.each { |doc|
54
+ print "case #{doc.inspect}\n"
55
+ print "enc bson\n"
56
+ enc_bson = encode(doc)
57
+ print "dec bson\n"
58
+ raise "FAIL" unless doc == decode(enc_bson)
59
+ }
data/lib/mongo/cursor.rb CHANGED
@@ -18,9 +18,6 @@ require 'mongo/message'
18
18
  require 'mongo/util/byte_buffer'
19
19
  require 'mongo/util/bson'
20
20
 
21
- require 'logger'
22
- LOG = Logger.new('recv_file.log', 'daily')
23
-
24
21
  module XGen
25
22
  module Mongo
26
23
  module Driver
@@ -168,10 +165,7 @@ module XGen
168
165
 
169
166
  def read_response_header
170
167
  header_buf = ByteBuffer.new
171
- read = @db.receive_full(RESPONSE_HEADER_SIZE)
172
- header_buf.put_array(read.unpack("C*"))
173
- LOG.debug "resp head: #{read.inspect}\n"
174
- raise "BAD SIZE" unless read.length == RESPONSE_HEADER_SIZE
168
+ header_buf.put_array(@db.receive_full(RESPONSE_HEADER_SIZE).unpack("C*"))
175
169
  raise "Short read for DB response header; expected #{RESPONSE_HEADER_SIZE} bytes, saw #{header_buf.length}" unless header_buf.length == RESPONSE_HEADER_SIZE
176
170
  header_buf.rewind
177
171
  @result_flags = header_buf.get_int
@@ -206,16 +200,10 @@ module XGen
206
200
 
207
201
  def object_from_stream
208
202
  buf = ByteBuffer.new
209
- read1 = @db.receive_full(4)
210
- buf.put_array(read1.unpack("C*"))
211
- LOG.debug "size: #{read1.inspect}\n"
212
- raise "BAD SIZE" unless read1.length == 4
203
+ buf.put_array(@db.receive_full(4).unpack("C*"))
213
204
  buf.rewind
214
205
  size = buf.get_int
215
- read2 = @db.receive_full(size - 4)
216
- buf.put_array(read2.unpack("C*"), 4)
217
- LOG.debug "body: #{read2.inspect}\n"
218
- raise "BAD SIZE" unless read2.length == size - 4
206
+ buf.put_array(@db.receive_full(size - 4).unpack("C*"), 4)
219
207
  @n_remaining -= 1
220
208
  buf.rewind
221
209
  BSON.new.deserialize(buf)
data/lib/mongo/db.rb CHANGED
@@ -374,6 +374,23 @@ module XGen
374
374
  raise "Error with count command: #{doc.inspect}"
375
375
  end
376
376
 
377
+ # Evaluate a JavaScript expression on MongoDB.
378
+ # +code+ should be a string or Code instance containing a JavaScript
379
+ # expression. Additional arguments will be passed to that expression
380
+ # when it is run on the server.
381
+ def eval(code, *args)
382
+ if not code.is_a? Code
383
+ code = Code.new(code)
384
+ end
385
+
386
+ oh = OrderedHash.new
387
+ oh[:$eval] = code
388
+ oh[:args] = args
389
+ doc = db_command(oh)
390
+ return doc['retval'] if ok?(doc)
391
+ raise "Error with eval command: #{doc.inspect}"
392
+ end
393
+
377
394
  # Drop index +name+ from +collection_name+. Normally called from
378
395
  # Collection#drop_index or Collection#drop_indexes.
379
396
  def drop_index(collection_name, name)
@@ -16,9 +16,6 @@
16
16
 
17
17
  require 'mongo/util/byte_buffer'
18
18
 
19
- require 'logger'
20
- LOG = Logger.new('recv_file.log', 'daily')
21
-
22
19
  module XGen
23
20
  module Mongo
24
21
  module Driver
@@ -33,10 +30,7 @@ module XGen
33
30
 
34
31
  def read_header(db)
35
32
  @buf.rewind
36
- read = db.receive_full(HEADER_SIZE)
37
- @buf.put_array(read.unpack("C*"))
38
- LOG.debug "header: #{read.inspect}\n"
39
- raise "BAD SIZE" unless read.length == HEADER_SIZE
33
+ @buf.put_array(db.receive_full(HEADER_SIZE).unpack("C*"))
40
34
  raise "Short read for DB response header: expected #{HEADER_SIZE} bytes, saw #{@buf.size}" unless @buf.size == HEADER_SIZE
41
35
  @buf.rewind
42
36
  @size = @buf.get_int
@@ -71,7 +71,7 @@ class BSON
71
71
  end
72
72
 
73
73
  begin
74
- require 'mongo/ext/cbson'
74
+ require 'mongo_ext/cbson'
75
75
  def serialize(obj)
76
76
  @buf = ByteBuffer.new(CBson.serialize(obj))
77
77
  end
@@ -129,7 +129,7 @@ class BSON
129
129
  end
130
130
 
131
131
  begin
132
- require 'mongo/ext/cbson'
132
+ require 'mongo_ext/cbson'
133
133
  def deserialize(buf=nil)
134
134
  if buf.is_a? String
135
135
  @buf = ByteBuffer.new(buf) if buf
@@ -245,7 +245,12 @@ class BSON
245
245
  def deserialize_object_data(buf)
246
246
  size = buf.get_int
247
247
  buf.position -= 4
248
- BSON.new().deserialize(buf.get(size))
248
+ object = BSON.new().deserialize(buf.get(size))
249
+ if object.has_key? "$ref"
250
+ DBRef.new(object["$ref"], object["$id"])
251
+ else
252
+ object
253
+ end
249
254
  end
250
255
 
251
256
  def deserialize_array_data(buf)
@@ -324,8 +329,10 @@ class BSON
324
329
  end
325
330
 
326
331
  def serialize_dbref_element(buf, key, val)
327
- serialize_string_element(buf, key, val.namespace, REF)
328
- buf.put_array(val.object_id.to_a)
332
+ oh = OrderedHash.new
333
+ oh['$ref'] = val.namespace
334
+ oh['$id'] = val.object_id
335
+ serialize_object_element(buf, key, oh)
329
336
  end
330
337
 
331
338
  def serialize_binary_element(buf, key, val)
@@ -1,4 +1,7 @@
1
+ # We need to list all of the included files because we aren't allowed to use
2
+ # Dir[...] in the github sandbox.
1
3
  PACKAGE_FILES = ['README.rdoc', 'Rakefile', 'mongo-ruby-driver.gemspec',
4
+ 'bin/bson_benchmark.rb',
2
5
  'bin/mongo_console',
3
6
  'bin/run_test_script',
4
7
  'bin/standard_benchmark',
@@ -45,7 +48,6 @@ PACKAGE_FILES = ['README.rdoc', 'Rakefile', 'mongo-ruby-driver.gemspec',
45
48
  'lib/mongo/util/ordered_hash.rb',
46
49
  'lib/mongo/util/xml_to_ruby.rb',
47
50
  'lib/mongo.rb']
48
-
49
51
  TEST_FILES = ['tests/mongo-qa/_common.rb',
50
52
  'tests/mongo-qa/admin',
51
53
  'tests/mongo-qa/capped',
@@ -53,6 +55,8 @@ TEST_FILES = ['tests/mongo-qa/_common.rb',
53
55
  'tests/mongo-qa/dbs',
54
56
  'tests/mongo-qa/find',
55
57
  'tests/mongo-qa/find1',
58
+ 'tests/mongo-qa/gridfs_in',
59
+ 'tests/mongo-qa/gridfs_out',
56
60
  'tests/mongo-qa/indices',
57
61
  'tests/mongo-qa/remove',
58
62
  'tests/mongo-qa/stress1',
@@ -75,10 +79,10 @@ TEST_FILES = ['tests/mongo-qa/_common.rb',
75
79
 
76
80
  Gem::Specification.new do |s|
77
81
  s.name = 'mongo'
78
- s.version = '0.6.4'
82
+ s.version = '0.6.5'
79
83
  s.platform = Gem::Platform::RUBY
80
- s.summary = 'Simple pure-Ruby driver for the 10gen Mongo DB'
81
- s.description = 'A pure-Ruby driver for the 10gen Mongo DB. For more information about Mongo, see http://www.mongodb.org.'
84
+ s.summary = 'Ruby driver for the 10gen Mongo DB'
85
+ s.description = 'A Ruby driver for the 10gen Mongo DB. For more information about Mongo, see http://www.mongodb.org.'
82
86
 
83
87
  s.require_paths = ['lib']
84
88
 
@@ -89,7 +93,7 @@ Gem::Specification.new do |s|
89
93
  s.rdoc_options = ['--main', 'README.rdoc', '--inline-source']
90
94
  s.extra_rdoc_files = ['README.rdoc']
91
95
 
92
- s.author = 'Jim Menard'
93
- s.email = 'jim@10gen.com'
96
+ s.authors = ['Jim Menard', 'Mike Dirolf']
97
+ s.email = 'mongodb-dev@googlegroups.com'
94
98
  s.homepage = 'http://www.mongodb.org'
95
99
  end
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require File.join(File.dirname(__FILE__), '_common.rb')
4
+
5
+ require 'mongo/gridfs'
6
+ include XGen::Mongo::GridFS
7
+
8
+ db = Mongo.new(DEFAULT_HOST, DEFAULT_PORT).db(DEFAULT_DB)
9
+
10
+ input_file = ARGV[0]
11
+
12
+ File.open(input_file, "r") { |f|
13
+ GridStore.open(db, input_file, "w") { |g|
14
+ g.write(f.read)
15
+ }
16
+ }
@@ -0,0 +1,17 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require File.join(File.dirname(__FILE__), '_common.rb')
4
+
5
+ require 'mongo/gridfs'
6
+ include XGen::Mongo::GridFS
7
+
8
+ db = Mongo.new(DEFAULT_HOST, DEFAULT_PORT).db(DEFAULT_DB)
9
+
10
+ input_file = ARGV[0]
11
+ output_file = ARGV[1]
12
+
13
+ File.open(output_file, "w") { |f|
14
+ GridStore.open(db, input_file, "r") { |g|
15
+ f.write(g.read)
16
+ }
17
+ }
data/tests/test_admin.rb CHANGED
@@ -22,6 +22,7 @@ class AdminTest < Test::Unit::TestCase
22
22
  def teardown
23
23
  @admin.profiling_level = :off
24
24
  @@coll.clear if @@coll
25
+ @@db.error
25
26
  end
26
27
 
27
28
  def test_default_profiling_level
data/tests/test_chunk.rb CHANGED
@@ -24,6 +24,7 @@ class ChunkTest < Test::Unit::TestCase
24
24
  def teardown
25
25
  @@chunks.clear
26
26
  @@files.clear
27
+ @@db.error
27
28
  end
28
29
 
29
30
  def test_pos
data/tests/test_cursor.rb CHANGED
@@ -19,6 +19,7 @@ class CursorTest < Test::Unit::TestCase
19
19
 
20
20
  def teardown
21
21
  @@coll.clear
22
+ @@db.error
22
23
  end
23
24
 
24
25
  def test_explain
@@ -41,55 +42,69 @@ class CursorTest < Test::Unit::TestCase
41
42
  end
42
43
 
43
44
  def test_refill_via_get_more
44
- 1000.times { |i|
45
- @@coll.insert('a' => i)
46
- }
47
-
48
- assert_equal 1001, @@coll.count
49
- count = 0
50
- @@coll.find.each { |obj|
51
- count += obj['a']
52
- }
53
- assert_equal 1001, @@coll.count
54
-
55
- # do the same thing again for debugging
56
- assert_equal 1001, @@coll.count
57
- count2 = 0
58
- @@coll.find.each { |obj|
59
- count2 += obj['a']
60
- }
61
- assert_equal 1001, @@coll.count
62
-
63
- assert_equal count, count2
64
- assert_equal 499501, count
45
+ begin
46
+ assert_equal 1, @@coll.count
47
+ 1000.times { |i|
48
+ assert_equal 1 + i, @@coll.count
49
+ @@coll.insert('a' => i)
50
+ }
51
+
52
+ assert_equal 1001, @@coll.count
53
+ count = 0
54
+ @@coll.find.each { |obj|
55
+ count += obj['a']
56
+ }
57
+ assert_equal 1001, @@coll.count
58
+
59
+ # do the same thing again for debugging
60
+ assert_equal 1001, @@coll.count
61
+ count2 = 0
62
+ @@coll.find.each { |obj|
63
+ count2 += obj['a']
64
+ }
65
+ assert_equal 1001, @@coll.count
66
+
67
+ assert_equal count, count2
68
+ assert_equal 499501, count
69
+ rescue Test::Unit::AssertionFailedError => ex
70
+ p @@db.collection_names
71
+ Process.exit 1
72
+ end
65
73
  end
66
74
 
67
75
  def test_refill_via_get_more_alt_coll
68
- coll = @@db.collection('test-alt-coll')
69
- coll.clear
70
- coll.insert('a' => 1) # collection not created until it's used
71
-
72
- 1000.times { |i|
73
- coll.insert('a' => i)
74
- }
75
-
76
- assert_equal 1001, coll.count
77
- count = 0
78
- coll.find.each { |obj|
79
- count += obj['a']
80
- }
81
- assert_equal 1001, coll.count
82
-
83
- # do the same thing again for debugging
84
- assert_equal 1001, coll.count
85
- count2 = 0
86
- coll.find.each { |obj|
87
- count2 += obj['a']
88
- }
89
- assert_equal 1001, coll.count
90
-
91
- assert_equal count, count2
92
- assert_equal 499501, count
76
+ begin
77
+ coll = @@db.collection('test-alt-coll')
78
+ coll.clear
79
+ coll.insert('a' => 1) # collection not created until it's used
80
+ assert_equal 1, coll.count
81
+
82
+ 1000.times { |i|
83
+ assert_equal 1 + i, coll.count
84
+ coll.insert('a' => i)
85
+ }
86
+
87
+ assert_equal 1001, coll.count
88
+ count = 0
89
+ coll.find.each { |obj|
90
+ count += obj['a']
91
+ }
92
+ assert_equal 1001, coll.count
93
+
94
+ # do the same thing again for debugging
95
+ assert_equal 1001, coll.count
96
+ count2 = 0
97
+ coll.find.each { |obj|
98
+ count2 += obj['a']
99
+ }
100
+ assert_equal 1001, coll.count
101
+
102
+ assert_equal count, count2
103
+ assert_equal 499501, count
104
+ rescue Test::Unit::AssertionFailedError => ex
105
+ p @@db.collection_names
106
+ Process.exit 1
107
+ end
93
108
  end
94
109
 
95
110
  def test_close_after_query_sent
data/tests/test_db.rb CHANGED
@@ -29,6 +29,7 @@ class DBTest < Test::Unit::TestCase
29
29
 
30
30
  def teardown
31
31
  @@users.clear if @@users
32
+ @@db.error
32
33
  end
33
34
 
34
35
  def test_close
data/tests/test_db_api.rb CHANGED
@@ -19,6 +19,7 @@ class DBAPITest < Test::Unit::TestCase
19
19
 
20
20
  def teardown
21
21
  @@coll.clear
22
+ @@db.error
22
23
  end
23
24
 
24
25
  def test_clear
@@ -447,8 +448,28 @@ class DBAPITest < Test::Unit::TestCase
447
448
  @@coll.insert('a' => 3)
448
449
 
449
450
  assert_equal 3, @@coll.count
450
- assert_equal 1, @@coll.find('$where' => Code.new('this.a > 2')).count
451
- assert_equal 2, @@coll.find('$where' => Code.new('this.a > i', {'i' => 1})).count
451
+ assert_equal 1, @@coll.count('$where' => Code.new('this.a > 2'))
452
+ assert_equal 2, @@coll.count('$where' => Code.new('this.a > i', {'i' => 1}))
453
+ end
454
+
455
+ def test_eval
456
+ assert_equal 3, @@db.eval('function (x) {return x;}', 3)
457
+
458
+ assert_equal nil, @@db.eval("function (x) {db.test_eval.save({y:x});}", 5)
459
+ assert_equal 5, @@db.collection('test_eval').find_first['y']
460
+
461
+ assert_equal 5, @@db.eval("function (x, y) {return x + y;}", 2, 3)
462
+ assert_equal 5, @@db.eval("function () {return 5;}")
463
+ assert_equal 5, @@db.eval("2 + 3;")
464
+
465
+ assert_equal 5, @@db.eval(Code.new("2 + 3;"))
466
+ assert_equal nil, @@db.eval(Code.new("return i;"))
467
+ assert_equal 2, @@db.eval(Code.new("return i;", {"i" => 2}))
468
+ assert_equal 5, @@db.eval(Code.new("i + 3;", {"i" => 2}))
469
+
470
+ assert_raise RuntimeError do
471
+ @@db.eval("5 ++ 5;")
472
+ end
452
473
  end
453
474
 
454
475
  def test_hint
@@ -13,5 +13,6 @@ class DBConnectionTest < Test::Unit::TestCase
13
13
  db = Mongo.new(host, port).db('ruby-mongo-demo')
14
14
  coll = db.collection('test')
15
15
  coll.clear
16
+ db.error
16
17
  end
17
18
  end
@@ -22,6 +22,7 @@ class GridStoreTest < Test::Unit::TestCase
22
22
  def teardown
23
23
  @@chunks.clear
24
24
  @@files.clear
25
+ @@db.error
25
26
  end
26
27
 
27
28
  def test_exist
data/tests/test_mongo.rb CHANGED
@@ -13,6 +13,10 @@ class MongoTest < Test::Unit::TestCase
13
13
  @mongo = Mongo.new(@host, @port)
14
14
  end
15
15
 
16
+ def teardown
17
+ @mongo.db('ruby-mongo-test').error
18
+ end
19
+
16
20
  def test_database_info
17
21
  @mongo.drop_database('ruby-mongo-info-test')
18
22
  @mongo.db('ruby-mongo-info-test').collection('info-test').insert('a' => 1)
metadata CHANGED
@@ -1,10 +1,11 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongodb-mongo
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.4
4
+ version: 0.6.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jim Menard
8
+ - Mike Dirolf
8
9
  autorequire:
9
10
  bindir: bin
10
11
  cert_chain: []
@@ -13,8 +14,8 @@ date: 2009-02-25 00:00:00 -08:00
13
14
  default_executable:
14
15
  dependencies: []
15
16
 
16
- description: A pure-Ruby driver for the 10gen Mongo DB. For more information about Mongo, see http://www.mongodb.org.
17
- email: jim@10gen.com
17
+ description: A Ruby driver for the 10gen Mongo DB. For more information about Mongo, see http://www.mongodb.org.
18
+ email: mongodb-dev@googlegroups.com
18
19
  executables: []
19
20
 
20
21
  extensions: []
@@ -25,6 +26,7 @@ files:
25
26
  - README.rdoc
26
27
  - Rakefile
27
28
  - mongo-ruby-driver.gemspec
29
+ - bin/bson_benchmark.rb
28
30
  - bin/mongo_console
29
31
  - bin/run_test_script
30
32
  - bin/standard_benchmark
@@ -98,7 +100,7 @@ rubyforge_project:
98
100
  rubygems_version: 1.2.0
99
101
  signing_key:
100
102
  specification_version: 2
101
- summary: Simple pure-Ruby driver for the 10gen Mongo DB
103
+ summary: Ruby driver for the 10gen Mongo DB
102
104
  test_files:
103
105
  - tests/mongo-qa/_common.rb
104
106
  - tests/mongo-qa/admin
@@ -107,6 +109,8 @@ test_files:
107
109
  - tests/mongo-qa/dbs
108
110
  - tests/mongo-qa/find
109
111
  - tests/mongo-qa/find1
112
+ - tests/mongo-qa/gridfs_in
113
+ - tests/mongo-qa/gridfs_out
110
114
  - tests/mongo-qa/indices
111
115
  - tests/mongo-qa/remove
112
116
  - tests/mongo-qa/stress1