Skip to content

Commit 864abe3

Browse files
committed
gridfs docs
1 parent c82b61f commit 864abe3

File tree

4 files changed

+83
-112
lines changed

4 files changed

+83
-112
lines changed

README.rdoc

+33-22
Original file line numberDiff line numberDiff line change
@@ -70,40 +70,51 @@ Here's how to start MongoDB and run the "simple.rb" example:
7070

7171
See also the test code, especially test/test_db_api.rb.
7272

73-
= GridStore
73+
= GridFS
7474

75-
The GridStore class is a Ruby implementation of MongoDB's GridFS file storage
76-
system. An instance of GridStore is like an IO object. See the RDocs for
77-
details, and see examples/gridfs.rb for code that uses many of the GridStore
78-
features (metadata, content type, rewind/seek/tell, etc).
75+
Note: The GridStore class has been deprecated. Use either the Grid or GridFileSystem
76+
classes to take advantage of GridFS.
7977

80-
Note that the GridStore class is not automatically required when you require
81-
'mongo'. You also need to require 'mongo/gridfs'
78+
The Ruby driver include two abstractions for storing large files: Grid and GridFileSystem.
79+
The Grid class is a Ruby implementation of MongoDB's GridFS file storage
80+
specification. GridFileSystem is essentailly the same, but provides a more filesystem-like API
81+
and assumes that filenames are unique.
8282

83-
Example code:
83+
An instance of both classes represents an individual file store. See the API reference
84+
for details, and see examples/gridfs.rb for code that uses many of the Grid
85+
features (metadata, content type, seek, tell, etc).
8486

85-
include GridFS
87+
Examples:
88+
include Mongo
89+
90+
# Get a database
91+
db = Mongo::Connection.new.db('app-db')
8692

87-
# Store the text "Hello, world!" in the grid store.
88-
GridStore.open(database, 'filename', 'w') do |f|
89-
f.puts "Hello, world!"
93+
# GridFileSystem. Store the text "Hello, world!" in the fs.
94+
fs = GridFileSystem.new(db)
95+
fs.open('filename', 'w') do |f|
96+
f.write "Hello, world!"
9097
end
9198

92-
# Output "Hello, world!"
93-
GridStore.open(database, 'filename', 'r') do |f|
99+
# GridFileSystem. Output "Hello, world!"
100+
fs = GridFileSystem.new(db)
101+
fs.open('filename', 'r') do |f|
94102
puts f.read
95103
end
96104

97-
# Add text to the grid store.
98-
GridStore.open(database, 'filename', 'w+') do |f|
99-
f.puts "But wait, there's more!"
100-
end
105+
# Write a file on disk to the Grid
106+
file = File.open('image.jpg')
107+
grid = GridFileSystem.new(db)
108+
id = grid.put(file)
101109

102-
# Retrieve everything, outputting "Hello, world!\nBut wait, there's more!\n"
103-
GridStore.open(database, 'filename', 'r') do |f|
104-
puts f.read
105-
end
110+
# Retrieve the file
111+
file = grid.get(id)
112+
file.read
106113

114+
# Get all the file's metata
115+
file.filename
116+
file.content_type
117+
file.metadata
107118

108119
= Notes
109120

bin/gridstore_benchmark

+24-19
Original file line numberDiff line numberDiff line change
@@ -7,34 +7,39 @@ include GridFS
77

88
db = Connection.new['benchmark-gridfs']
99
sample_data = File.open(File.join(File.dirname(__FILE__), 'sample_file.pdf'), 'r').read
10-
GridStore.delete(db, 'mongodb.pdf')
11-
GridStore.delete(db, 'mongodb-new.pdf')
10+
db['fs.files'].remove
11+
db['fs.chunks'].remove
1212

13+
T = 5
1314
length = sample_data.length
14-
mb = length / 1048576.0
15+
mb = T * length / 1048576.0
1516

16-
t1 = Time.now
1717
@grid = Grid.new(db)
18-
@id = @grid.put(sample_data, 'mongodb-new.pdf', :safe => true)
19-
puts "Write: #{mb / (Time.now - t1)} mb/s"
20-
2118
t1 = Time.now
22-
GridStore.open(db, 'mongodb.pdf', 'w') do |f|
23-
f.write(sample_data)
19+
ids = []
20+
T.times do |n|
21+
ids << @grid.put(sample_data, "mongodb-new-#{n}.pdf")
2422
end
25-
puts "Write: #{mb / (Time.now - t1)} mb/s"
23+
puts "Grid Write: #{mb / (Time.now - t1)} mb/s"
2624

2725
t1 = Time.now
28-
@grid = Grid.new(db)
29-
data = @grid.get(@id).read
30-
puts "Read new: #{mb / (Time.now - t1)} mb/s"
31-
file = db['fs.files'].find_one({:filename => 'mongodb-new.pdf'})
26+
T.times do |n|
27+
GridStore.open(db, "mongodb.pdf-#{n}", 'w') do |f|
28+
f.write(sample_data)
29+
end
30+
end
31+
puts "GridStore Write: #{mb / (Time.now - t1)} mb/s"
3232

3333
t1 = Time.now
34-
old_data = GridStore.open(db, 'mongodb.pdf', 'r') do |f|
35-
f.read
34+
T.times do |n|
35+
data = @grid.get(ids[n]).read
3636
end
37-
puts "Read: #{mb / (Time.now - t1)} mb/s"
37+
puts "Grid Read: #{mb / (Time.now - t1)} mb/s"
3838

39-
puts sample_data == old_data
40-
puts sample_data == data
39+
t1 = Time.now
40+
T.times do |n|
41+
old_data = GridStore.open(db, "mongodb.pdf-#{n}", 'r') do |f|
42+
f.read
43+
end
44+
end
45+
puts "GridStore Read: #{mb / (Time.now - t1)} mb/s"

examples/gridfs.rb

+25-70
Original file line numberDiff line numberDiff line change
@@ -1,88 +1,43 @@
11
$:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2+
def assert
3+
raise "Failed!" unless yield
4+
end
25

36
require 'mongo'
4-
require 'mongo/gridfs'
5-
67
include Mongo
7-
include GridFS
88

99
host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
1010
port = ENV['MONGO_RUBY_DRIVER_PORT'] || Connection::DEFAULT_PORT
1111

1212
puts "Connecting to #{host}:#{port}"
1313
db = Connection.new(host, port).db('ruby-mongo-examples')
1414

15-
def dump(db, fname)
16-
GridStore.open(db, fname, 'r') { |f| puts f.read }
17-
end
18-
19-
# Write a new file
20-
GridStore.open(db, 'foobar', 'w') { |f| f.write("hello, world!") }
21-
22-
# Read it and print out the contents
23-
dump(db, 'foobar')
24-
25-
# Append more data
26-
GridStore.open(db, 'foobar', 'w+') { |f| f.write("\n"); f.puts "line two" }
27-
dump(db, 'foobar')
15+
data = "hello, world!"
2816

29-
# Overwrite
30-
GridStore.open(db, 'foobar', 'w') { |f| f.puts "hello, sailor!" }
31-
dump(db, 'foobar')
17+
grid = Grid.new(db)
3218

33-
# File existence tests
34-
puts "File 'foobar' exists: #{GridStore.exist?(db, 'foobar')}"
35-
puts "File 'does-not-exist' exists: #{GridStore.exist?(db, 'does-not-exist')}"
19+
# Write a new file. data can be a string or an io object responding to #read.
20+
id = grid.put(data, 'hello.txt')
3621

37-
# Read with offset (uses seek)
38-
puts GridStore.read(db, 'foobar', 6, 7)
22+
# Read it and print out the contents
23+
file = grid.get(id)
24+
puts file.read
3925

40-
# Rewind/seek/tell
41-
GridStore.open(db, 'foobar', 'w') { |f|
42-
f.write "hello, world!"
43-
f.rewind
44-
f.write "xyzzz"
45-
puts f.tell # => 5
46-
f.seek(4)
47-
f.write('y')
48-
}
49-
dump(db, 'foobar') # => 'xyzzy'
26+
# Delete the file
27+
grid.delete(id)
5028

51-
# Unlink (delete)
52-
GridStore.unlink(db, 'foobar')
53-
puts "File 'foobar' exists after delete: #{GridStore.exist?(db, 'foobar')}"
29+
begin
30+
grid.get(id)
31+
rescue => e
32+
assert {e.class == Mongo::GridError}
33+
end
5434

5535
# Metadata
56-
GridStore.open(db, 'foobar', 'w') { |f| f.write("hello, world!") }
57-
GridStore.open(db, 'foobar', 'r') { |f|
58-
puts f.content_type
59-
puts f.upload_date
60-
puts f.chunk_size
61-
puts f.metadata.inspect
62-
}
63-
64-
# Add some metadata; change content type
65-
GridStore.open(db, 'foobar', 'w+') { |f|
66-
f.content_type = 'text/xml'
67-
f.metadata = {'a' => 1}
68-
}
69-
# Print it
70-
GridStore.open(db, 'foobar', 'r') { |f|
71-
puts f.content_type
72-
puts f.upload_date
73-
puts f.chunk_size
74-
puts f.metadata.inspect
75-
}
76-
77-
# You can also set metadata when initially writing the file. Setting :root
78-
# means that the file and its chunks are stored in a different root
79-
# collection: instead of gridfs.files and gridfs.chunks, here we use
80-
# my_files.files and my_files.chunks.
81-
GridStore.open(db, 'foobar', 'w',
82-
:content_type => 'text/plain',
83-
:metadata => {'a' => 1},
84-
:chunk_size => 1024 * 4,
85-
:root => 'my_files') { |f|
86-
f.puts 'hello, world'
87-
}
88-
36+
id = grid.put(data, 'hello.txt', :content_type => 'text/plain', :metadata => {'name' => 'hello'})
37+
file = grid.get(id)
38+
39+
p file.content_type
40+
p file.metadata.inspect
41+
p file.chunk_size
42+
p file.file_length
43+
p file.data

lib/mongo/gridfs/grid_io.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class GridIO
2929
DEFAULT_CONTENT_TYPE = 'binary/octet-stream'
3030

3131
attr_reader :content_type, :chunk_size, :upload_date, :files_id, :filename,
32-
:metadata, :server_md5, :client_md5
32+
:metadata, :server_md5, :client_md5, :file_length
3333

3434
# Create a new GridIO object. Note that most users will not need to use this class directly;
3535
# the Grid and GridFileSystem classes will instantiate this class

0 commit comments

Comments
 (0)