animehunter-mongo 0.9
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +311 -0
- data/Rakefile +62 -0
- data/bin/bson_benchmark.rb +59 -0
- data/bin/mongo_console +21 -0
- data/bin/run_test_script +19 -0
- data/bin/standard_benchmark +109 -0
- data/examples/admin.rb +41 -0
- data/examples/benchmarks.rb +42 -0
- data/examples/blog.rb +76 -0
- data/examples/capped.rb +23 -0
- data/examples/cursor.rb +47 -0
- data/examples/gridfs.rb +87 -0
- data/examples/index_test.rb +125 -0
- data/examples/info.rb +30 -0
- data/examples/queries.rb +69 -0
- data/examples/simple.rb +23 -0
- data/examples/strict.rb +34 -0
- data/examples/types.rb +40 -0
- data/lib/mongo.rb +19 -0
- data/lib/mongo/admin.rb +87 -0
- data/lib/mongo/collection.rb +235 -0
- data/lib/mongo/cursor.rb +227 -0
- data/lib/mongo/db.rb +538 -0
- data/lib/mongo/gridfs.rb +16 -0
- data/lib/mongo/gridfs/chunk.rb +96 -0
- data/lib/mongo/gridfs/grid_store.rb +468 -0
- data/lib/mongo/message.rb +20 -0
- data/lib/mongo/message/get_more_message.rb +37 -0
- data/lib/mongo/message/insert_message.rb +35 -0
- data/lib/mongo/message/kill_cursors_message.rb +36 -0
- data/lib/mongo/message/message.rb +84 -0
- data/lib/mongo/message/message_header.rb +50 -0
- data/lib/mongo/message/msg_message.rb +33 -0
- data/lib/mongo/message/opcodes.rb +32 -0
- data/lib/mongo/message/query_message.rb +77 -0
- data/lib/mongo/message/remove_message.rb +36 -0
- data/lib/mongo/message/update_message.rb +37 -0
- data/lib/mongo/mongo.rb +164 -0
- data/lib/mongo/query.rb +119 -0
- data/lib/mongo/types/binary.rb +42 -0
- data/lib/mongo/types/code.rb +34 -0
- data/lib/mongo/types/dbref.rb +37 -0
- data/lib/mongo/types/objectid.rb +137 -0
- data/lib/mongo/types/regexp_of_holding.rb +44 -0
- data/lib/mongo/types/undefined.rb +31 -0
- data/lib/mongo/util/bson.rb +534 -0
- data/lib/mongo/util/byte_buffer.rb +167 -0
- data/lib/mongo/util/ordered_hash.rb +96 -0
- data/lib/mongo/util/xml_to_ruby.rb +107 -0
- data/mongo-ruby-driver.gemspec +99 -0
- data/tests/mongo-qa/_common.rb +8 -0
- data/tests/mongo-qa/admin +26 -0
- data/tests/mongo-qa/capped +22 -0
- data/tests/mongo-qa/count1 +18 -0
- data/tests/mongo-qa/dbs +22 -0
- data/tests/mongo-qa/find +10 -0
- data/tests/mongo-qa/find1 +15 -0
- data/tests/mongo-qa/gridfs_in +16 -0
- data/tests/mongo-qa/gridfs_out +17 -0
- data/tests/mongo-qa/indices +49 -0
- data/tests/mongo-qa/remove +25 -0
- data/tests/mongo-qa/stress1 +35 -0
- data/tests/mongo-qa/test1 +11 -0
- data/tests/mongo-qa/update +18 -0
- data/tests/test_admin.rb +69 -0
- data/tests/test_bson.rb +246 -0
- data/tests/test_byte_buffer.rb +69 -0
- data/tests/test_chunk.rb +84 -0
- data/tests/test_cursor.rb +121 -0
- data/tests/test_db.rb +160 -0
- data/tests/test_db_api.rb +701 -0
- data/tests/test_db_connection.rb +18 -0
- data/tests/test_grid_store.rb +284 -0
- data/tests/test_message.rb +35 -0
- data/tests/test_mongo.rb +78 -0
- data/tests/test_objectid.rb +98 -0
- data/tests/test_ordered_hash.rb +129 -0
- data/tests/test_round_trip.rb +116 -0
- metadata +133 -0
data/bin/run_test_script
ADDED
@@ -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/../tests/mongo-qa/$1 ] ; then
|
9
|
+
exit 0
|
10
|
+
fi
|
11
|
+
|
12
|
+
begintime=`date`
|
13
|
+
ruby $HERE/../tests/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 XGen::Mongo::Driver
|
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'] || XGen::Mongo::Driver::Mongo::DEFAULT_PORT
|
54
|
+
|
55
|
+
connection = Mongo.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_first('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})
|
data/examples/admin.rb
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
$LOAD_PATH[0,0] = File.join(File.dirname(__FILE__), '..', 'lib')
|
2
|
+
require 'mongo'
|
3
|
+
require 'pp'
|
4
|
+
|
5
|
+
include XGen::Mongo::Driver
|
6
|
+
|
7
|
+
host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
|
8
|
+
port = ENV['MONGO_RUBY_DRIVER_PORT'] || XGen::Mongo::Driver::Mongo::DEFAULT_PORT
|
9
|
+
|
10
|
+
puts "Connecting to #{host}:#{port}"
|
11
|
+
db = Mongo.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'] || XGen::Mongo::Driver::Mongo::DEFAULT_PORT
|
8
|
+
|
9
|
+
puts "Connecting to #{host}:#{port}"
|
10
|
+
db = XGen::Mongo::Driver::Mongo.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_first: ") {
|
36
|
+
TEST_COUNT.times {
|
37
|
+
coll.find_first(:number => 0)
|
38
|
+
}
|
39
|
+
}
|
40
|
+
end
|
41
|
+
|
42
|
+
coll.clear
|
data/examples/blog.rb
ADDED
@@ -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 XGen::Mongo::Driver
|
13
|
+
|
14
|
+
host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
|
15
|
+
port = ENV['MONGO_RUBY_DRIVER_PORT'] || XGen::Mongo::Driver::Mongo::DEFAULT_PORT
|
16
|
+
|
17
|
+
puts ">> Connecting to #{host}:#{port}"
|
18
|
+
DB = Mongo.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"
|
data/examples/capped.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
$LOAD_PATH[0,0] = File.join(File.dirname(__FILE__), '..', 'lib')
|
2
|
+
require 'mongo'
|
3
|
+
|
4
|
+
include XGen::Mongo::Driver
|
5
|
+
|
6
|
+
host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
|
7
|
+
port = ENV['MONGO_RUBY_DRIVER_PORT'] || XGen::Mongo::Driver::Mongo::DEFAULT_PORT
|
8
|
+
|
9
|
+
puts "Connecting to #{host}:#{port}"
|
10
|
+
db = Mongo.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
|
data/examples/cursor.rb
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
$LOAD_PATH[0,0] = File.join(File.dirname(__FILE__), '..', 'lib')
|
2
|
+
require 'mongo'
|
3
|
+
require 'pp'
|
4
|
+
|
5
|
+
include XGen::Mongo::Driver
|
6
|
+
|
7
|
+
host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
|
8
|
+
port = ENV['MONGO_RUBY_DRIVER_PORT'] || XGen::Mongo::Driver::Mongo::DEFAULT_PORT
|
9
|
+
|
10
|
+
puts "Connecting to #{host}:#{port}"
|
11
|
+
db = Mongo.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
|
data/examples/gridfs.rb
ADDED
@@ -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 XGen::Mongo::Driver
|
6
|
+
include XGen::Mongo::GridFS
|
7
|
+
|
8
|
+
host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
|
9
|
+
port = ENV['MONGO_RUBY_DRIVER_PORT'] || XGen::Mongo::Driver::Mongo::DEFAULT_PORT
|
10
|
+
|
11
|
+
puts "Connecting to #{host}:#{port}"
|
12
|
+
db = Mongo.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
|
+
|