gridfs docs

This commit is contained in:
Kyle Banker 2010-02-24 15:24:01 -05:00
parent c82b61ffc9
commit 864abe3dce
4 changed files with 84 additions and 113 deletions

View File

@ -70,40 +70,51 @@ Here's how to start MongoDB and run the "simple.rb" example:
See also the test code, especially test/test_db_api.rb. See also the test code, especially test/test_db_api.rb.
= GridStore = GridFS
The GridStore class is a Ruby implementation of MongoDB's GridFS file storage Note: The GridStore class has been deprecated. Use either the Grid or GridFileSystem
system. An instance of GridStore is like an IO object. See the RDocs for classes to take advantage of GridFS.
details, and see examples/gridfs.rb for code that uses many of the GridStore
features (metadata, content type, rewind/seek/tell, etc).
Note that the GridStore class is not automatically required when you require The Ruby driver include two abstractions for storing large files: Grid and GridFileSystem.
'mongo'. You also need to require 'mongo/gridfs' The Grid class is a Ruby implementation of MongoDB's GridFS file storage
specification. GridFileSystem is essentailly the same, but provides a more filesystem-like API
and assumes that filenames are unique.
Example code: An instance of both classes represents an individual file store. See the API reference
for details, and see examples/gridfs.rb for code that uses many of the Grid
features (metadata, content type, seek, tell, etc).
include GridFS Examples:
include Mongo
# Store the text "Hello, world!" in the grid store. # Get a database
GridStore.open(database, 'filename', 'w') do |f| db = Mongo::Connection.new.db('app-db')
f.puts "Hello, world!"
# GridFileSystem. Store the text "Hello, world!" in the fs.
fs = GridFileSystem.new(db)
fs.open('filename', 'w') do |f|
f.write "Hello, world!"
end end
# Output "Hello, world!" # GridFileSystem. Output "Hello, world!"
GridStore.open(database, 'filename', 'r') do |f| fs = GridFileSystem.new(db)
fs.open('filename', 'r') do |f|
puts f.read puts f.read
end end
# Add text to the grid store. # Write a file on disk to the Grid
GridStore.open(database, 'filename', 'w+') do |f| file = File.open('image.jpg')
f.puts "But wait, there's more!" grid = GridFileSystem.new(db)
end id = grid.put(file)
# Retrieve everything, outputting "Hello, world!\nBut wait, there's more!\n" # Retrieve the file
GridStore.open(database, 'filename', 'r') do |f| file = grid.get(id)
puts f.read file.read
end
# Get all the file's metata
file.filename
file.content_type
file.metadata
= Notes = Notes

View File

@ -7,34 +7,39 @@ include GridFS
db = Connection.new['benchmark-gridfs'] db = Connection.new['benchmark-gridfs']
sample_data = File.open(File.join(File.dirname(__FILE__), 'sample_file.pdf'), 'r').read sample_data = File.open(File.join(File.dirname(__FILE__), 'sample_file.pdf'), 'r').read
GridStore.delete(db, 'mongodb.pdf') db['fs.files'].remove
GridStore.delete(db, 'mongodb-new.pdf') db['fs.chunks'].remove
T = 5
length = sample_data.length length = sample_data.length
mb = length / 1048576.0 mb = T * length / 1048576.0
t1 = Time.now
@grid = Grid.new(db) @grid = Grid.new(db)
@id = @grid.put(sample_data, 'mongodb-new.pdf', :safe => true) t1 = Time.now
puts "Write: #{mb / (Time.now - t1)} mb/s" ids = []
T.times do |n|
ids << @grid.put(sample_data, "mongodb-new-#{n}.pdf")
end
puts "Grid Write: #{mb / (Time.now - t1)} mb/s"
t1 = Time.now t1 = Time.now
GridStore.open(db, 'mongodb.pdf', 'w') do |f| T.times do |n|
GridStore.open(db, "mongodb.pdf-#{n}", 'w') do |f|
f.write(sample_data) f.write(sample_data)
end
end end
puts "Write: #{mb / (Time.now - t1)} mb/s" puts "GridStore Write: #{mb / (Time.now - t1)} mb/s"
t1 = Time.now t1 = Time.now
@grid = Grid.new(db) T.times do |n|
data = @grid.get(@id).read data = @grid.get(ids[n]).read
puts "Read new: #{mb / (Time.now - t1)} mb/s" end
file = db['fs.files'].find_one({:filename => 'mongodb-new.pdf'}) puts "Grid Read: #{mb / (Time.now - t1)} mb/s"
t1 = Time.now t1 = Time.now
old_data = GridStore.open(db, 'mongodb.pdf', 'r') do |f| T.times do |n|
old_data = GridStore.open(db, "mongodb.pdf-#{n}", 'r') do |f|
f.read f.read
end
end end
puts "Read: #{mb / (Time.now - t1)} mb/s" puts "GridStore Read: #{mb / (Time.now - t1)} mb/s"
puts sample_data == old_data
puts sample_data == data

View File

@ -1,10 +1,10 @@
$:.unshift(File.join(File.dirname(__FILE__), '..', 'lib')) $:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
def assert
raise "Failed!" unless yield
end
require 'mongo' require 'mongo'
require 'mongo/gridfs'
include Mongo include Mongo
include GridFS
host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost' host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
port = ENV['MONGO_RUBY_DRIVER_PORT'] || Connection::DEFAULT_PORT port = ENV['MONGO_RUBY_DRIVER_PORT'] || Connection::DEFAULT_PORT
@ -12,77 +12,32 @@ port = ENV['MONGO_RUBY_DRIVER_PORT'] || Connection::DEFAULT_PORT
puts "Connecting to #{host}:#{port}" puts "Connecting to #{host}:#{port}"
db = Connection.new(host, port).db('ruby-mongo-examples') db = Connection.new(host, port).db('ruby-mongo-examples')
def dump(db, fname) data = "hello, world!"
GridStore.open(db, fname, 'r') { |f| puts f.read }
end
# Write a new file grid = Grid.new(db)
GridStore.open(db, 'foobar', 'w') { |f| f.write("hello, world!") }
# Write a new file. data can be a string or an io object responding to #read.
id = grid.put(data, 'hello.txt')
# Read it and print out the contents # Read it and print out the contents
dump(db, 'foobar') file = grid.get(id)
puts file.read
# Append more data # Delete the file
GridStore.open(db, 'foobar', 'w+') { |f| f.write("\n"); f.puts "line two" } grid.delete(id)
dump(db, 'foobar')
# Overwrite begin
GridStore.open(db, 'foobar', 'w') { |f| f.puts "hello, sailor!" } grid.get(id)
dump(db, 'foobar') rescue => e
assert {e.class == Mongo::GridError}
# File existence tests end
puts "File 'foobar' exists: #{GridStore.exist?(db, 'foobar')}"
puts "File 'does-not-exist' exists: #{GridStore.exist?(db, 'does-not-exist')}"
# Read with offset (uses seek)
puts GridStore.read(db, 'foobar', 6, 7)
# Rewind/seek/tell
GridStore.open(db, 'foobar', 'w') { |f|
f.write "hello, world!"
f.rewind
f.write "xyzzz"
puts f.tell # => 5
f.seek(4)
f.write('y')
}
dump(db, 'foobar') # => 'xyzzy'
# Unlink (delete)
GridStore.unlink(db, 'foobar')
puts "File 'foobar' exists after delete: #{GridStore.exist?(db, 'foobar')}"
# Metadata # Metadata
GridStore.open(db, 'foobar', 'w') { |f| f.write("hello, world!") } id = grid.put(data, 'hello.txt', :content_type => 'text/plain', :metadata => {'name' => 'hello'})
GridStore.open(db, 'foobar', 'r') { |f| file = grid.get(id)
puts f.content_type
puts f.upload_date
puts f.chunk_size
puts f.metadata.inspect
}
# Add some metadata; change content type
GridStore.open(db, 'foobar', 'w+') { |f|
f.content_type = 'text/xml'
f.metadata = {'a' => 1}
}
# Print it
GridStore.open(db, 'foobar', 'r') { |f|
puts f.content_type
puts f.upload_date
puts f.chunk_size
puts f.metadata.inspect
}
# You can also set metadata when initially writing the file. Setting :root
# means that the file and its chunks are stored in a different root
# collection: instead of gridfs.files and gridfs.chunks, here we use
# my_files.files and my_files.chunks.
GridStore.open(db, 'foobar', 'w',
:content_type => 'text/plain',
:metadata => {'a' => 1},
:chunk_size => 1024 * 4,
:root => 'my_files') { |f|
f.puts 'hello, world'
}
p file.content_type
p file.metadata.inspect
p file.chunk_size
p file.file_length
p file.data

View File

@ -29,7 +29,7 @@ module Mongo
DEFAULT_CONTENT_TYPE = 'binary/octet-stream' DEFAULT_CONTENT_TYPE = 'binary/octet-stream'
attr_reader :content_type, :chunk_size, :upload_date, :files_id, :filename, attr_reader :content_type, :chunk_size, :upload_date, :files_id, :filename,
:metadata, :server_md5, :client_md5 :metadata, :server_md5, :client_md5, :file_length
# Create a new GridIO object. Note that most users will not need to use this class directly; # Create a new GridIO object. Note that most users will not need to use this class directly;
# the Grid and GridFileSystem classes will instantiate this class # the Grid and GridFileSystem classes will instantiate this class