mongo 0.1.0 → 0.15

Sign up to get free protection for your applications and to get access to all the features.
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
@@ -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 Mongo
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
+ }
@@ -7,14 +7,14 @@ require 'irb'
7
7
  $LOAD_PATH[0,0] = File.join(File.dirname(__FILE__), '..', 'lib')
8
8
  require 'mongo'
9
9
 
10
- include XGen::Mongo::Driver
10
+ include Mongo
11
11
 
12
12
  host = org_argv[0] || ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
13
- port = org_argv[1] || ENV['MONGO_RUBY_DRIVER_PORT'] || XGen::Mongo::Driver::Mongo::DEFAULT_PORT
13
+ port = org_argv[1] || ENV['MONGO_RUBY_DRIVER_PORT'] || Connection::DEFAULT_PORT
14
14
  dbnm = org_argv[2] || ENV['MONGO_RUBY_DRIVER_DB'] || 'ruby-mongo-console'
15
15
 
16
16
  puts "Connecting to #{host}:#{port} (CONN) on with database #{dbnm} (DB)"
17
- CONN = Mongo.new(host, port)
17
+ CONN = Connection.new(host, port)
18
18
  DB = CONN.db(dbnm)
19
19
 
20
20
  puts "Starting IRB session..."
@@ -0,0 +1,19 @@
1
+ #!/bin/bash
2
+ # usage: run_test_script test_name output_file
3
+ #
4
+ # See http://mongodb.onconfluence.com/display/DOCS/Using+the+Framework+(for+Driver+Developers)
5
+
6
+ HERE=`dirname $0`
7
+
8
+ if [ ! -f $HERE/../test/mongo-qa/$1 ] ; then
9
+ exit 0
10
+ fi
11
+
12
+ begintime=`date`
13
+ ruby $HERE/../test/mongo-qa/$1 $3 $4 >> $2
14
+ exitval=$?
15
+ endtime=`date`
16
+
17
+ echo "begintime:$begintime" >> $2
18
+ echo "endtime:$endtime" >> $2
19
+ echo "exit_code:$exitval" >> $2
@@ -0,0 +1,109 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # Note: Ruby 1.9 is faster than 1.8, as expected.
4
+
5
+ $LOAD_PATH[0,0] = File.join(File.dirname(__FILE__), '..', 'lib')
6
+ require 'mongo'
7
+
8
+ include Mongo
9
+
10
+ TRIALS = 2
11
+ PER_TRIAL = 5000
12
+ BATCH_SIZE = 100
13
+
14
+ SMALL = {}
15
+ MEDIUM = {
16
+ 'integer' => 5,
17
+ 'number' => 5.05,
18
+ 'boolean' => false,
19
+ 'array' => ['test', 'benchmark']
20
+ }
21
+ LARGE = {
22
+ 'base_url' => 'http://www.example.com/test-me',
23
+ 'total_word_count' => 6743,
24
+ 'access_time' => Time.now,
25
+ 'meta_tags' => {
26
+ 'description' => 'i am a long description string',
27
+ 'author' => 'Holly Man',
28
+ 'dynamically_created_meta_tag' => 'who know\n what'
29
+ },
30
+ 'page_structure' => {
31
+ 'counted_tags' => 3450,
32
+ 'no_of_js_attached' => 10,
33
+ 'no_of_images' => 6
34
+ },
35
+ 'harvested_words' => ['10gen','web','open','source','application','paas',
36
+ 'platform-as-a-service','technology','helps',
37
+ 'developers','focus','building','mongodb','mongo'] * 20
38
+ }
39
+
40
+ def report(str, t)
41
+ printf("%s%d\n", str.ljust(60, '.'), PER_TRIAL / t)
42
+ end
43
+
44
+ def benchmark(str, proc, n, db, coll_name, object, setup=nil)
45
+ coll = db.collection(coll_name)
46
+ setup.call(coll, object) if setup
47
+ t0 = Time.new
48
+ n.times { |i| proc.call(coll, object, i) }
49
+ report(str, Time.new.to_f - t0.to_f)
50
+ end
51
+
52
+ host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
53
+ port = ENV['MONGO_RUBY_DRIVER_PORT'] || Connection::DEFAULT_PORT
54
+
55
+ connection = Connection.new(host, port)
56
+ connection.drop_database("benchmark")
57
+ db = connection.db('benchmark')
58
+
59
+ insert = Proc.new { |coll, object, i|
60
+ object['x'] = i
61
+ coll.insert(object)
62
+ }
63
+ benchmark('insert (small, no index)', insert, PER_TRIAL, db, 'small_none', SMALL)
64
+ benchmark('insert (medium, no index)', insert, PER_TRIAL, db, 'medium_none', MEDIUM)
65
+ benchmark('insert (large, no index)', insert, PER_TRIAL, db, 'large_none', LARGE)
66
+
67
+ index_on_x = Proc.new { |coll, object|
68
+ coll.create_index('x')
69
+ }
70
+ benchmark('insert (small, indexed)', insert, PER_TRIAL, db, 'small_index', SMALL, index_on_x)
71
+ benchmark('insert (medium, indexed)', insert, PER_TRIAL, db, 'medium_index', MEDIUM, index_on_x)
72
+ benchmark('insert (large, indexed)', insert, PER_TRIAL, db, 'large_index', LARGE, index_on_x)
73
+
74
+ insert_batch = Proc.new { |coll, object, i|
75
+ object['x'] = i
76
+ coll.insert([object] * BATCH_SIZE)
77
+ }
78
+ benchmark('batch insert (small, no index)', insert_batch, PER_TRIAL/BATCH_SIZE, db, 'small_bulk', SMALL)
79
+ benchmark('batch insert (medium, no index)', insert_batch, PER_TRIAL/BATCH_SIZE, db, 'medium_bulk', MEDIUM)
80
+ benchmark('batch insert (large, no index)', insert_batch, PER_TRIAL/BATCH_SIZE, db, 'large_bulk', LARGE)
81
+
82
+ find_one = Proc.new { |coll, x, i|
83
+ coll.find_one('x' => x)
84
+ }
85
+ benchmark('find_one (small, no index)', find_one, PER_TRIAL, db, 'small_none', PER_TRIAL / 2)
86
+ benchmark('find_one (medium, no index)', find_one, PER_TRIAL, db, 'medium_none', PER_TRIAL / 2)
87
+ benchmark('find_one (large, no index)', find_one, PER_TRIAL, db, 'large_none', PER_TRIAL / 2)
88
+
89
+ benchmark('find_one (small, indexed)', find_one, PER_TRIAL, db, 'small_index', PER_TRIAL / 2)
90
+ benchmark('find_one (medium, indexed)', find_one, PER_TRIAL, db, 'medium_index', PER_TRIAL / 2)
91
+ benchmark('find_one (large, indexed)', find_one, PER_TRIAL, db, 'large_index', PER_TRIAL / 2)
92
+
93
+ find = Proc.new { |coll, x, i|
94
+ coll.find('x' => x).each {}
95
+ }
96
+ benchmark('find (small, no index)', find, PER_TRIAL, db, 'small_none', PER_TRIAL / 2)
97
+ benchmark('find (medium, no index)', find, PER_TRIAL, db, 'medium_none', PER_TRIAL / 2)
98
+ benchmark('find (large, no index)', find, PER_TRIAL, db, 'large_none', PER_TRIAL / 2)
99
+
100
+ benchmark('find (small, indexed)', find, PER_TRIAL, db, 'small_index', PER_TRIAL / 2)
101
+ benchmark('find (medium, indexed)', find, PER_TRIAL, db, 'medium_index', PER_TRIAL / 2)
102
+ benchmark('find (large, indexed)', find, PER_TRIAL, db, 'large_index', PER_TRIAL / 2)
103
+
104
+ benchmark('find range (small, indexed)', find, PER_TRIAL, db, 'small_index',
105
+ {"$gt" => PER_TRIAL / 2, "$lt" => PER_TRIAL / 2 + BATCH_SIZE})
106
+ benchmark('find range (medium, indexed)', find, PER_TRIAL, db, 'medium_index',
107
+ {"$gt" => PER_TRIAL / 2, "$lt" => PER_TRIAL / 2 + BATCH_SIZE})
108
+ benchmark('find range (large, indexed)', find, PER_TRIAL, db, 'large_index',
109
+ {"$gt" => PER_TRIAL / 2, "$lt" => PER_TRIAL / 2 + BATCH_SIZE})
@@ -0,0 +1,41 @@
1
+ $LOAD_PATH[0,0] = File.join(File.dirname(__FILE__), '..', 'lib')
2
+ require 'mongo'
3
+ require 'pp'
4
+
5
+ include Mongo
6
+
7
+ host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
8
+ port = ENV['MONGO_RUBY_DRIVER_PORT'] || Connection::DEFAULT_PORT
9
+
10
+ puts "Connecting to #{host}:#{port}"
11
+ db = Connection.new(host, port).db('ruby-mongo-examples')
12
+ coll = db.create_collection('test')
13
+
14
+ # Erase all records from collection, if any
15
+ coll.clear
16
+
17
+ admin = db.admin
18
+
19
+ # Profiling level set/get
20
+ p admin.profiling_level
21
+
22
+ # Start profiling everything
23
+ admin.profiling_level = :all
24
+
25
+ # Read records, creating a profiling event
26
+ coll.find().to_a
27
+
28
+ # Stop profiling
29
+ admin.profiling_level = :off
30
+
31
+ # Print all profiling info
32
+ pp admin.profiling_info
33
+
34
+ # Validate returns a hash if all is well or raises an exception if there is a
35
+ # problem.
36
+ info = admin.validate_collection(coll.name)
37
+ puts "valid = #{info['ok']}"
38
+ puts info['result']
39
+
40
+ # Destroy the collection
41
+ coll.drop
@@ -0,0 +1,42 @@
1
+ require "benchmark"
2
+
3
+ $LOAD_PATH[0,0] = File.join(File.dirname(__FILE__), '..', 'lib')
4
+ require 'mongo'
5
+
6
+ host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
7
+ port = ENV['MONGO_RUBY_DRIVER_PORT'] || Mongo::Connection::DEFAULT_PORT
8
+
9
+ puts "Connecting to #{host}:#{port}"
10
+ db = Mongo::Connection.new(host, port).db('ruby-mongo-examples')
11
+ coll = db.collection('test')
12
+ coll.clear
13
+
14
+ OBJS_COUNT = 100
15
+ TEST_COUNT = 100
16
+
17
+ puts "Generating benchmark data"
18
+ msgs = %w{hola hello aloha ciao}
19
+ arr = (0..OBJS_COUNT).collect {|x| { :number => x, :rndm => (rand(5)+1), :msg => msgs[rand(4)] }}
20
+
21
+ puts "Running benchmark"
22
+ Benchmark.bmbm do |results|
23
+ results.report("single object inserts: ") {
24
+ TEST_COUNT.times {
25
+ coll.clear
26
+ arr.each {|x| coll.insert(x)}
27
+ }
28
+ }
29
+ results.report("multiple object insert: ") {
30
+ TEST_COUNT.times {
31
+ coll.clear
32
+ coll.insert(arr)
33
+ }
34
+ }
35
+ results.report("find_one: ") {
36
+ TEST_COUNT.times {
37
+ coll.find_one(:number => 0)
38
+ }
39
+ }
40
+ end
41
+
42
+ coll.clear
@@ -0,0 +1,76 @@
1
+ require "rubygems"
2
+
3
+ class Exception
4
+ def errmsg
5
+ "%s: %s\n%s" % [self.class, message, (backtrace || []).join("\n") << "\n"]
6
+ end
7
+ end
8
+
9
+ $LOAD_PATH[0,0] = File.join(File.dirname(__FILE__), '..', 'lib')
10
+ require 'mongo'
11
+
12
+ include Mongo
13
+
14
+ host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
15
+ port = ENV['MONGO_RUBY_DRIVER_PORT'] || Connection::DEFAULT_PORT
16
+
17
+ puts ">> Connecting to #{host}:#{port}"
18
+ DB = Connection.new(host, port).db('ruby-mongo-blog')
19
+
20
+ LINE_SIZE = 120
21
+ puts "=" * LINE_SIZE
22
+ puts "Adding authors"
23
+ authors = DB.collection "authors"
24
+ authors.clear
25
+ authors.create_index "meta", '_id' => 1, 'name' => 1, 'age' => 1
26
+ puts "-" * LINE_SIZE
27
+ shaksp = authors << { :name => "William Shakespeare", :email => "william@shakespeare.com", :age => 587 }
28
+ puts "shaksp : #{shaksp.inspect}"
29
+ borges = authors << { :name => "Jorge Luis Borges", :email => "jorge@borges.com", :age => 123 }
30
+ puts "borges : #{borges.inspect}"
31
+ puts "-" * LINE_SIZE
32
+ puts "authors ordered by age ascending"
33
+ puts "-" * LINE_SIZE
34
+ authors.find({}, :sort => [{'age' => 1}]).each {|x| puts "%-25.25s : %-25.25s : %3i" % [x['name'], x['email'], x['age']]}
35
+
36
+ puts "=" * LINE_SIZE
37
+ puts "Adding users"
38
+ users = DB.collection "users"
39
+ users.clear
40
+ # users.create_index "meta", :_id => 1, :login => 1, :name => 1
41
+ puts "-" * LINE_SIZE
42
+ jdoe = users << { :login => "jdoe", :name => "John Doe", :email => "john@doe.com" }
43
+ puts "jdoe : #{jdoe.inspect}"
44
+ lsmt = users << { :login => "lsmith", :name => "Lucy Smith", :email => "lucy@smith.com" }
45
+ puts "lsmt : #{lsmt.inspect}"
46
+ puts "-" * LINE_SIZE
47
+ puts "users ordered by login ascending"
48
+ puts "-" * LINE_SIZE
49
+ users.find({}, :sort => [{'login' => 1}]).each {|x| puts "%-10.10s : %-25.25s : %-25.25s" % [x['login'], x['name'], x['email']]}
50
+
51
+ puts "=" * LINE_SIZE
52
+ puts "Adding articles"
53
+ articles = DB.collection "articles"
54
+ articles.clear
55
+ # articles.create_index "meta", :_id => 1, :author_id => 1, :title => 1
56
+ puts "-" * LINE_SIZE
57
+ begin
58
+ art1 = articles << { :title => "Caminando por Buenos Aires", :body => "Las callecitas de Buenos Aires tienen ese no se que...", :author_id => borges["_id"].to_s }
59
+ puts "art1 : #{art1.inspect}"
60
+ rescue => e
61
+ puts "Error: #{e.errmsg}"
62
+ end
63
+ begin
64
+ art2 = articles << { :title => "I must have seen thy face before", :body => "Thine eyes call me in a new way", :author_id => shaksp["_id"].to_s, :comments => [ { :user_id => jdoe["_id"].to_s, :body => "great article!" } ] }
65
+ puts "art2 : #{art2.inspect}"
66
+ rescue => e
67
+ puts "Error: #{e.errmsg}"
68
+ end
69
+ puts "-" * LINE_SIZE
70
+ puts "articles ordered by title ascending"
71
+ puts "-" * LINE_SIZE
72
+ articles.find({}, :sort => [{'title' => 1}]).each {|x| puts "%-25.25s : %-25.25s" % [x['title'], x['author_id']]}
73
+
74
+ puts ">> Closing connection"
75
+ DB.close
76
+ puts "closed"
@@ -0,0 +1,23 @@
1
+ $LOAD_PATH[0,0] = File.join(File.dirname(__FILE__), '..', 'lib')
2
+ require 'mongo'
3
+
4
+ include Mongo
5
+
6
+ host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
7
+ port = ENV['MONGO_RUBY_DRIVER_PORT'] || Connection::DEFAULT_PORT
8
+
9
+ puts "Connecting to #{host}:#{port}"
10
+ db = Connection.new(host, port).db('ruby-mongo-examples')
11
+ db.drop_collection('test')
12
+
13
+ # A capped collection has a max size and optionally a max number of records.
14
+ # Old records get pushed out by new ones once the size or max num records is
15
+ # reached.
16
+ coll = db.create_collection('test', :capped => true, :size => 1024, :max => 12)
17
+
18
+ 100.times { |i| coll.insert('a' => i+1) }
19
+
20
+ # We will only see the last 12 records
21
+ coll.find().each { |row| p row }
22
+
23
+ coll.drop
@@ -0,0 +1,47 @@
1
+ $LOAD_PATH[0,0] = File.join(File.dirname(__FILE__), '..', 'lib')
2
+ require 'mongo'
3
+ require 'pp'
4
+
5
+ include Mongo
6
+
7
+ host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
8
+ port = ENV['MONGO_RUBY_DRIVER_PORT'] || Connection::DEFAULT_PORT
9
+
10
+ puts "Connecting to #{host}:#{port}"
11
+ db = Connection.new(host, port).db('ruby-mongo-examples')
12
+ coll = db.collection('test')
13
+
14
+ # Erase all records from collection, if any
15
+ coll.clear
16
+
17
+ # Insert 3 records
18
+ 3.times { |i| coll.insert({'a' => i+1}) }
19
+
20
+ # Cursors don't run their queries until you actually attempt to retrieve data
21
+ # from them.
22
+
23
+ # Find returns a Cursor, which is Enumerable. You can iterate:
24
+ coll.find().each { |row| pp row }
25
+
26
+ # You can turn it into an array
27
+ array = coll.find().to_a
28
+
29
+ # You can iterate after turning it into an array (the cursor will iterate over
30
+ # the copy of the array that it saves internally.)
31
+ cursor = coll.find()
32
+ array = cursor.to_a
33
+ cursor.each { |row| pp row }
34
+
35
+ # You can get the next object
36
+ first_object = coll.find().next_object
37
+
38
+ # next_object returns nil if there are no more objects that match
39
+ cursor = coll.find()
40
+ obj = cursor.next_object
41
+ while obj
42
+ pp obj
43
+ obj = cursor.next_object
44
+ end
45
+
46
+ # Destroy the collection
47
+ coll.drop
@@ -0,0 +1,87 @@
1
+ $LOAD_PATH[0,0] = File.join(File.dirname(__FILE__), '..', 'lib')
2
+ require 'mongo'
3
+ require 'mongo/gridfs'
4
+
5
+ include Mongo
6
+ include GridFS
7
+
8
+ host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
9
+ port = ENV['MONGO_RUBY_DRIVER_PORT'] || Connection::DEFAULT_PORT
10
+
11
+ puts "Connecting to #{host}:#{port}"
12
+ db = Connection.new(host, port).db('ruby-mongo-examples')
13
+
14
+ def dump(db, fname)
15
+ GridStore.open(db, fname, 'r') { |f| puts f.read }
16
+ end
17
+
18
+ # Write a new file
19
+ GridStore.open(db, 'foobar', 'w') { |f| f.write("hello, world!") }
20
+
21
+ # Read it and print out the contents
22
+ dump(db, 'foobar') # defined above
23
+
24
+ # Append more data
25
+ GridStore.open(db, 'foobar', 'w+') { |f| f.write("\n"); f.puts "line two" }
26
+ dump(db, 'foobar')
27
+
28
+ # Overwrite
29
+ GridStore.open(db, 'foobar', 'w') { |f| f.puts "hello, sailor!" }
30
+ dump(db, 'foobar')
31
+
32
+ # File existence tests
33
+ puts "File 'foobar' exists: #{GridStore.exist?(db, 'foobar')}"
34
+ puts "File 'does-not-exist' exists: #{GridStore.exist?(db, 'does-not-exist')}"
35
+
36
+ # Read with offset (uses seek)
37
+ puts GridStore.read(db, 'foobar', 6, 7)
38
+
39
+ # Rewind/seek/tell
40
+ GridStore.open(db, 'foobar', 'w') { |f|
41
+ f.write "hello, world!"
42
+ f.rewind
43
+ f.write "xyzzz"
44
+ puts f.tell # => 5
45
+ f.seek(4)
46
+ f.write('y')
47
+ }
48
+ dump(db, 'foobar') # => 'xyzzy'
49
+
50
+ # Unlink (delete)
51
+ GridStore.unlink(db, 'foobar')
52
+ puts "File 'foobar' exists after delete: #{GridStore.exist?(db, 'foobar')}"
53
+
54
+ # Metadata
55
+ GridStore.open(db, 'foobar', 'w') { |f| f.write("hello, world!") }
56
+ GridStore.open(db, 'foobar', 'r') { |f|
57
+ puts f.content_type
58
+ puts f.upload_date
59
+ puts f.chunk_size
60
+ puts f.metadata.inspect
61
+ }
62
+
63
+ # Add some metadata; change content type
64
+ GridStore.open(db, 'foobar', 'w+') { |f|
65
+ f.content_type = 'text/xml'
66
+ f.metadata = {'a' => 1}
67
+ }
68
+ # Print it
69
+ GridStore.open(db, 'foobar', 'r') { |f|
70
+ puts f.content_type
71
+ puts f.upload_date
72
+ puts f.chunk_size
73
+ puts f.metadata.inspect
74
+ }
75
+
76
+ # You can also set metadata when initially writing the file. Setting :root
77
+ # means that the file and its chunks are stored in a different root
78
+ # collection: instead of gridfs.files and gridfs.chunks, here we use
79
+ # my_files.files and my_files.chunks.
80
+ GridStore.open(db, 'foobar', 'w',
81
+ :content_type => 'text/plain',
82
+ :metadata => {'a' => 1},
83
+ :chunk_size => 1024 * 4,
84
+ :root => 'my_files') { |f|
85
+ f.puts 'hello, world'
86
+ }
87
+