2009-02-15 13:24:14 +00:00
|
|
|
# --
|
|
|
|
# Copyright (C) 2008-2009 10gen Inc.
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
# ++
|
|
|
|
|
2009-01-29 16:23:50 +00:00
|
|
|
require 'mongo/types/objectid'
|
|
|
|
require 'mongo/util/ordered_hash'
|
|
|
|
require 'mongo/gridfs/chunk'
|
|
|
|
|
|
|
|
module XGen
|
|
|
|
module Mongo
|
|
|
|
module GridFS
|
|
|
|
|
|
|
|
# GridStore is an IO-like object that provides input and output for
|
|
|
|
# streams of data to Mongo. See Mongo's documentation about GridFS for
|
|
|
|
# storage implementation details.
|
|
|
|
#
|
|
|
|
# Example code:
|
|
|
|
#
|
2009-01-29 16:31:45 +00:00
|
|
|
# require 'mongo/gridfs'
|
2009-01-29 16:23:50 +00:00
|
|
|
# GridStore.open(database, 'filename', 'w') { |f|
|
|
|
|
# f.puts "Hello, world!"
|
|
|
|
# }
|
|
|
|
# GridStore.open(database, 'filename, 'r') { |f|
|
|
|
|
# puts f.read # => Hello, world!\n
|
|
|
|
# }
|
2009-01-29 16:31:45 +00:00
|
|
|
# GridStore.open(database, 'filename', 'w+') { |f|
|
2009-01-29 16:23:50 +00:00
|
|
|
# f.puts "But wait, there's more!"
|
|
|
|
# }
|
|
|
|
# GridStore.open(database, 'filename, 'r') { |f|
|
|
|
|
# puts f.read # => Hello, world!\nBut wait, there's more!\n
|
|
|
|
# }
|
|
|
|
class GridStore
|
|
|
|
|
2009-01-30 19:54:58 +00:00
|
|
|
DEFAULT_ROOT_COLLECTION = 'gridfs'
|
|
|
|
DEFAULT_CONTENT_TYPE = 'text/plain'
|
|
|
|
|
2009-01-29 16:23:50 +00:00
|
|
|
include Enumerable
|
|
|
|
|
|
|
|
attr_accessor :filename
|
|
|
|
|
|
|
|
# Array of strings; may be +nil+
|
|
|
|
attr_accessor :aliases
|
|
|
|
|
2009-01-30 19:54:58 +00:00
|
|
|
# Default is DEFAULT_CONTENT_TYPE
|
2009-01-29 16:23:50 +00:00
|
|
|
attr_accessor :content_type
|
|
|
|
|
2009-01-30 21:44:29 +00:00
|
|
|
attr_accessor :metadata
|
|
|
|
|
|
|
|
attr_reader :files_id
|
2009-01-30 19:54:58 +00:00
|
|
|
|
|
|
|
# Time that the file was first saved.
|
|
|
|
attr_reader :upload_date
|
2009-01-29 16:23:50 +00:00
|
|
|
|
|
|
|
attr_reader :chunk_size
|
|
|
|
|
|
|
|
attr_accessor :lineno
|
|
|
|
|
|
|
|
class << self
|
|
|
|
|
2009-01-30 21:44:29 +00:00
|
|
|
def exist?(db, name, root_collection=DEFAULT_ROOT_COLLECTION)
|
|
|
|
db.collection("#{root_collection}.files").find({'filename' => name}).next_object != nil
|
2009-01-29 16:23:50 +00:00
|
|
|
end
|
|
|
|
|
2009-01-30 19:54:58 +00:00
|
|
|
def open(db, name, mode, options={})
|
|
|
|
gs = self.new(db, name, mode, options)
|
2009-01-29 16:23:50 +00:00
|
|
|
result = nil
|
|
|
|
begin
|
|
|
|
result = yield gs if block_given?
|
|
|
|
ensure
|
|
|
|
gs.close
|
|
|
|
end
|
|
|
|
result
|
|
|
|
end
|
|
|
|
|
|
|
|
def read(db, name, length=nil, offset=nil)
|
|
|
|
GridStore.open(db, name, 'r') { |gs|
|
|
|
|
gs.seek(offset) if offset
|
|
|
|
gs.read(length)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def readlines(db, name, separator=$/)
|
|
|
|
GridStore.open(db, name, 'r') { |gs|
|
|
|
|
gs.readlines(separator)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def unlink(db, *names)
|
|
|
|
names.each { |name|
|
|
|
|
gs = GridStore.new(db, name)
|
|
|
|
gs.send(:delete_chunks)
|
2009-01-30 21:44:29 +00:00
|
|
|
gs.collection.remove('_id' => gs.files_id)
|
2009-01-29 16:23:50 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
alias_method :delete, :unlink
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
#---
|
|
|
|
# ================================================================
|
|
|
|
#+++
|
|
|
|
|
|
|
|
# Mode may only be 'r', 'w', or 'w+'.
|
2009-01-30 19:54:58 +00:00
|
|
|
#
|
2009-01-30 21:44:29 +00:00
|
|
|
# Options. Descriptions start with a list of the modes for which that
|
|
|
|
# option is legitimate.
|
|
|
|
#
|
|
|
|
# :root :: (r, w, w+) Name of root collection to use, instead of
|
|
|
|
# DEFAULT_ROOT_COLLECTION.
|
|
|
|
#
|
|
|
|
# :metadata:: (w, w+) A hash containing any data you want persisted as
|
|
|
|
# this file's metadata. See also metadata=
|
2009-01-30 19:54:58 +00:00
|
|
|
#
|
2009-01-30 21:44:29 +00:00
|
|
|
# :chunk_size :: (w) Sets chunk size for files opened for writing
|
|
|
|
# See also chunk_size= which may only be called before
|
|
|
|
# any data is written.
|
2009-01-30 19:54:58 +00:00
|
|
|
#
|
2009-01-30 21:44:29 +00:00
|
|
|
# :content_type :: (w) Default value is DEFAULT_CONTENT_TYPE. See
|
|
|
|
# also #content_type=
|
2009-01-30 19:54:58 +00:00
|
|
|
def initialize(db, name, mode='r', options={})
|
2009-01-29 16:23:50 +00:00
|
|
|
@db, @filename, @mode = db, name, mode
|
2009-01-30 21:44:29 +00:00
|
|
|
@root = options[:root] || DEFAULT_ROOT_COLLECTION
|
2009-01-29 16:23:50 +00:00
|
|
|
|
2009-01-30 21:44:29 +00:00
|
|
|
doc = collection.find({'filename' => @filename}).next_object
|
2009-01-29 16:23:50 +00:00
|
|
|
if doc
|
2009-01-30 21:44:29 +00:00
|
|
|
@files_id = doc['_id']
|
2009-01-29 16:23:50 +00:00
|
|
|
@content_type = doc['contentType']
|
|
|
|
@chunk_size = doc['chunkSize']
|
|
|
|
@upload_date = doc['uploadDate']
|
|
|
|
@aliases = doc['aliases']
|
|
|
|
@length = doc['length']
|
2009-01-30 21:44:29 +00:00
|
|
|
@metadata = doc['metadata']
|
2009-01-29 16:23:50 +00:00
|
|
|
else
|
2009-01-30 21:44:29 +00:00
|
|
|
@files_id = XGen::Mongo::Driver::ObjectID.new
|
2009-01-30 19:54:58 +00:00
|
|
|
@content_type = DEFAULT_CONTENT_TYPE
|
2009-01-30 21:44:29 +00:00
|
|
|
@chunk_size = Chunk::DEFAULT_CHUNK_SIZE
|
2009-01-29 16:23:50 +00:00
|
|
|
@length = 0
|
|
|
|
end
|
|
|
|
|
|
|
|
case mode
|
|
|
|
when 'r'
|
2009-01-30 21:44:29 +00:00
|
|
|
@curr_chunk = nth_chunk(0)
|
|
|
|
@position = 0
|
2009-01-29 16:23:50 +00:00
|
|
|
when 'w'
|
2009-01-30 21:44:29 +00:00
|
|
|
chunk_collection.create_index("chunk_index", ['files_id', 'n'])
|
2009-01-29 16:23:50 +00:00
|
|
|
delete_chunks
|
2009-01-30 21:44:29 +00:00
|
|
|
@curr_chunk = Chunk.new(self, 'n' => 0)
|
2009-01-30 19:54:58 +00:00
|
|
|
@content_type = options[:content_type] if options[:content_type]
|
|
|
|
@chunk_size = options[:chunk_size] if options[:chunk_size]
|
2009-01-30 21:44:29 +00:00
|
|
|
@metadata = options[:metadata] if options[:metadata]
|
|
|
|
@position = 0
|
2009-01-29 16:23:50 +00:00
|
|
|
when 'w+'
|
2009-01-30 21:44:29 +00:00
|
|
|
chunk_collection.create_index("chunk_index", ['files_id', 'n'])
|
|
|
|
@curr_chunk = nth_chunk(last_chunk_number) || Chunk.new(self, 'n' => 0) # might be empty
|
2009-01-29 16:23:50 +00:00
|
|
|
@curr_chunk.pos = @curr_chunk.data.length if @curr_chunk
|
2009-01-30 21:44:29 +00:00
|
|
|
@metadata = options[:metadata] if options[:metadata]
|
|
|
|
@position = @length
|
|
|
|
else
|
|
|
|
raise "error: illegal mode #{mode}"
|
2009-01-29 16:23:50 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
@lineno = 0
|
|
|
|
@pushback_byte = nil
|
|
|
|
end
|
|
|
|
|
2009-01-30 21:44:29 +00:00
|
|
|
def collection
|
|
|
|
@db.collection("#{@root}.files")
|
|
|
|
end
|
|
|
|
|
|
|
|
# Returns collection used for storing chunks. Depends on value of
|
|
|
|
# @root.
|
|
|
|
def chunk_collection
|
|
|
|
@db.collection("#{@root}.chunks")
|
|
|
|
end
|
|
|
|
|
2009-01-29 16:23:50 +00:00
|
|
|
# Change chunk size. Can only change if the file is opened for write
|
2009-01-30 21:44:29 +00:00
|
|
|
# and no data has yet been written.
|
2009-01-29 16:23:50 +00:00
|
|
|
def chunk_size=(size)
|
2009-01-30 21:44:29 +00:00
|
|
|
unless @mode[0] == ?w && @position == 0 && @upload_date == nil
|
2009-01-29 16:23:50 +00:00
|
|
|
raise "error: can only change chunk size if open for write and no data written."
|
|
|
|
end
|
|
|
|
@chunk_size = size
|
|
|
|
end
|
|
|
|
|
|
|
|
#---
|
|
|
|
# ================ reading ================
|
|
|
|
#+++
|
|
|
|
|
|
|
|
def getc
|
|
|
|
if @pushback_byte
|
|
|
|
byte = @pushback_byte
|
|
|
|
@pushback_byte = nil
|
2009-01-30 21:44:29 +00:00
|
|
|
@position += 1
|
2009-01-29 16:23:50 +00:00
|
|
|
byte
|
|
|
|
elsif eof?
|
|
|
|
nil
|
|
|
|
else
|
|
|
|
if @curr_chunk.eof?
|
2009-01-30 21:44:29 +00:00
|
|
|
@curr_chunk = nth_chunk(@curr_chunk.chunk_number + 1)
|
2009-01-29 16:23:50 +00:00
|
|
|
end
|
2009-01-30 21:44:29 +00:00
|
|
|
@position += 1
|
2009-01-29 16:23:50 +00:00
|
|
|
@curr_chunk.getc
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def gets(separator=$/)
|
|
|
|
str = ''
|
2009-02-10 16:41:36 +00:00
|
|
|
byte = self.getc
|
2009-01-29 16:23:50 +00:00
|
|
|
return nil if byte == nil # EOF
|
|
|
|
while byte != nil
|
|
|
|
s = byte.chr
|
|
|
|
str << s
|
|
|
|
break if s == separator
|
2009-02-10 16:41:36 +00:00
|
|
|
byte = self.getc
|
2009-01-29 16:23:50 +00:00
|
|
|
end
|
|
|
|
@lineno += 1
|
|
|
|
str
|
|
|
|
end
|
|
|
|
|
|
|
|
def read(len=nil, buf=nil)
|
|
|
|
buf ||= ''
|
2009-02-10 16:41:36 +00:00
|
|
|
byte = self.getc
|
2009-01-29 16:23:50 +00:00
|
|
|
while byte != nil && (len == nil || len > 0)
|
|
|
|
buf << byte.chr
|
|
|
|
len -= 1 if len
|
2009-02-10 16:41:36 +00:00
|
|
|
byte = self.getc if (len == nil || len > 0)
|
2009-01-29 16:23:50 +00:00
|
|
|
end
|
|
|
|
buf
|
|
|
|
end
|
|
|
|
|
|
|
|
def readchar
|
2009-02-10 16:41:36 +00:00
|
|
|
byte = self.getc
|
2009-01-29 16:23:50 +00:00
|
|
|
raise EOFError.new if byte == nil
|
|
|
|
byte
|
|
|
|
end
|
|
|
|
|
|
|
|
def readline(separator=$/)
|
|
|
|
line = gets
|
|
|
|
raise EOFError.new if line == nil
|
|
|
|
line
|
|
|
|
end
|
|
|
|
|
|
|
|
def readlines(separator=$/)
|
|
|
|
read.split(separator).collect { |line| "#{line}#{separator}" }
|
|
|
|
end
|
|
|
|
|
|
|
|
def each
|
|
|
|
line = gets
|
|
|
|
while line
|
|
|
|
yield line
|
|
|
|
line = gets
|
|
|
|
end
|
|
|
|
end
|
|
|
|
alias_method :each_line, :each
|
|
|
|
|
|
|
|
def each_byte
|
2009-02-10 16:41:36 +00:00
|
|
|
byte = self.getc
|
2009-01-29 16:23:50 +00:00
|
|
|
while byte
|
|
|
|
yield byte
|
2009-02-10 16:41:36 +00:00
|
|
|
byte = self.getc
|
2009-01-29 16:23:50 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def ungetc(byte)
|
|
|
|
@pushback_byte = byte
|
2009-01-30 21:44:29 +00:00
|
|
|
@position -= 1
|
2009-01-29 16:23:50 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
#---
|
|
|
|
# ================ writing ================
|
|
|
|
#+++
|
|
|
|
|
|
|
|
def putc(byte)
|
2009-01-30 21:44:29 +00:00
|
|
|
if @curr_chunk.pos == @chunk_size
|
|
|
|
prev_chunk_number = @curr_chunk.chunk_number
|
|
|
|
@curr_chunk.save
|
|
|
|
@curr_chunk = Chunk.new(self, 'n' => prev_chunk_number + 1)
|
2009-01-29 16:23:50 +00:00
|
|
|
end
|
2009-01-30 21:44:29 +00:00
|
|
|
@position += 1
|
2009-01-29 16:23:50 +00:00
|
|
|
@curr_chunk.putc(byte)
|
|
|
|
end
|
|
|
|
|
|
|
|
def print(*objs)
|
|
|
|
objs = [$_] if objs == nil || objs.empty?
|
|
|
|
objs.each { |obj|
|
|
|
|
str = obj.to_s
|
2009-02-10 16:41:36 +00:00
|
|
|
str.each_byte { |byte| self.putc(byte) }
|
2009-01-29 16:23:50 +00:00
|
|
|
}
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def puts(*objs)
|
|
|
|
if objs == nil || objs.empty?
|
2009-02-10 16:41:36 +00:00
|
|
|
self.putc(10)
|
2009-01-29 16:23:50 +00:00
|
|
|
else
|
|
|
|
print(*objs.collect{ |obj|
|
|
|
|
str = obj.to_s
|
|
|
|
str << "\n" unless str =~ /\n$/
|
|
|
|
str
|
|
|
|
})
|
|
|
|
end
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def <<(obj)
|
|
|
|
write(obj.to_s)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Writes +string+ as bytes and returns the number of bytes written.
|
|
|
|
def write(string)
|
|
|
|
raise "#@filename not opened for write" unless @mode[0] == ?w
|
|
|
|
count = 0
|
|
|
|
string.each_byte { |byte|
|
2009-02-10 16:41:36 +00:00
|
|
|
self.putc byte
|
2009-01-29 16:23:50 +00:00
|
|
|
count += 1
|
|
|
|
}
|
|
|
|
count
|
|
|
|
end
|
|
|
|
|
|
|
|
# A no-op.
|
|
|
|
def flush
|
|
|
|
end
|
|
|
|
|
|
|
|
#---
|
|
|
|
# ================ status ================
|
|
|
|
#+++
|
|
|
|
|
|
|
|
def eof
|
|
|
|
raise IOError.new("stream not open for reading") unless @mode[0] == ?r
|
2009-01-30 21:44:29 +00:00
|
|
|
@position >= @length
|
2009-01-29 16:23:50 +00:00
|
|
|
end
|
|
|
|
alias_method :eof?, :eof
|
|
|
|
|
|
|
|
#---
|
|
|
|
# ================ positioning ================
|
|
|
|
#+++
|
|
|
|
|
|
|
|
def rewind
|
2009-01-30 21:44:29 +00:00
|
|
|
if @curr_chunk.chunk_number != 0
|
|
|
|
if @mode[0] == ?w
|
|
|
|
delete_chunks
|
|
|
|
@curr_chunk = Chunk.new(self, 'n' => 0)
|
|
|
|
else
|
|
|
|
@curr_chunk == nth_chunk(0)
|
|
|
|
end
|
2009-01-29 16:23:50 +00:00
|
|
|
end
|
|
|
|
@curr_chunk.pos = 0
|
|
|
|
@lineno = 0
|
2009-01-30 21:44:29 +00:00
|
|
|
@position = 0
|
2009-01-29 16:23:50 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def seek(pos, whence=IO::SEEK_SET)
|
2009-01-30 21:44:29 +00:00
|
|
|
target_pos = case whence
|
|
|
|
when IO::SEEK_CUR
|
|
|
|
@position + pos
|
|
|
|
when IO::SEEK_END
|
2009-02-02 21:45:36 +00:00
|
|
|
@length + pos
|
2009-01-30 21:44:29 +00:00
|
|
|
when IO::SEEK_SET
|
|
|
|
pos
|
|
|
|
end
|
2009-01-29 16:23:50 +00:00
|
|
|
|
2009-01-30 21:44:29 +00:00
|
|
|
new_chunk_number = (target_pos / @chunk_size).to_i
|
|
|
|
if new_chunk_number != @curr_chunk.chunk_number
|
|
|
|
@curr_chunk.save if @mode[0] == ?w
|
|
|
|
@curr_chunk = nth_chunk(new_chunk_number)
|
|
|
|
end
|
|
|
|
@position = target_pos
|
|
|
|
@curr_chunk.pos = @position % @chunk_size
|
|
|
|
0
|
2009-01-29 16:23:50 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def tell
|
2009-01-30 21:44:29 +00:00
|
|
|
@position
|
|
|
|
end
|
2009-01-29 16:23:50 +00:00
|
|
|
|
|
|
|
#---
|
|
|
|
# ================ closing ================
|
|
|
|
#+++
|
|
|
|
|
|
|
|
def close
|
|
|
|
if @mode[0] == ?w
|
|
|
|
if @curr_chunk
|
|
|
|
@curr_chunk.truncate
|
2009-01-30 21:44:29 +00:00
|
|
|
@curr_chunk.save if @curr_chunk.pos > 0
|
2009-01-29 16:23:50 +00:00
|
|
|
end
|
2009-01-30 21:44:29 +00:00
|
|
|
files = collection
|
|
|
|
if @upload_date
|
|
|
|
files.remove('_id' => @files_id)
|
2009-01-29 16:23:50 +00:00
|
|
|
else
|
2009-01-30 19:54:58 +00:00
|
|
|
@upload_date = Time.now
|
2009-01-29 16:23:50 +00:00
|
|
|
end
|
|
|
|
files.insert(to_mongo_object)
|
|
|
|
end
|
|
|
|
@db = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def closed?
|
|
|
|
@db == nil
|
|
|
|
end
|
|
|
|
|
|
|
|
#---
|
|
|
|
# ================ protected ================
|
|
|
|
#+++
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
def to_mongo_object
|
|
|
|
h = OrderedHash.new
|
2009-01-30 21:44:29 +00:00
|
|
|
h['_id'] = @files_id
|
2009-01-29 16:23:50 +00:00
|
|
|
h['filename'] = @filename
|
|
|
|
h['contentType'] = @content_type
|
2009-01-30 21:44:29 +00:00
|
|
|
h['length'] = @curr_chunk ? @curr_chunk.chunk_number * @chunk_size + @curr_chunk.pos : 0
|
2009-01-29 16:23:50 +00:00
|
|
|
h['chunkSize'] = @chunk_size
|
|
|
|
h['uploadDate'] = @upload_date
|
|
|
|
h['aliases'] = @aliases
|
2009-01-30 21:44:29 +00:00
|
|
|
h['metadata'] = @metadata
|
2009-01-29 16:23:50 +00:00
|
|
|
h
|
|
|
|
end
|
|
|
|
|
2009-01-30 21:44:29 +00:00
|
|
|
def delete_chunks
|
|
|
|
chunk_collection.remove({'files_id' => @files_id}) if @files_id
|
|
|
|
@curr_chunk = nil
|
2009-01-29 16:23:50 +00:00
|
|
|
end
|
|
|
|
|
2009-01-30 21:44:29 +00:00
|
|
|
def nth_chunk(n)
|
|
|
|
mongo_chunk = chunk_collection.find({'files_id' => @files_id, 'n' => n}).next_object
|
|
|
|
Chunk.new(self, mongo_chunk || {})
|
2009-01-29 16:23:50 +00:00
|
|
|
end
|
|
|
|
|
2009-01-30 21:44:29 +00:00
|
|
|
def last_chunk_number
|
|
|
|
(@length / @chunk_size).to_i
|
2009-01-29 16:23:50 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|