Class: Mongo::GridIO
- Includes:
- WriteConcern
- Defined in:
- lib/mongo/gridfs/grid_io.rb
Overview
GridIO objects represent files in the GridFS specification. This class manages the reading and writing of file chunks and metadata.
Constant Summary collapse
- DEFAULT_CHUNK_SIZE =
256 * 1024
- DEFAULT_CONTENT_TYPE =
'binary/octet-stream'- PROTECTED_ATTRS =
[:files_id, :file_length, :client_md5, :server_md5]
Instance Attribute Summary collapse
-
#chunk_size ⇒ Object
readonly
Returns the value of attribute chunk_size.
-
#client_md5 ⇒ Object
readonly
Returns the value of attribute client_md5.
-
#content_type ⇒ Object
readonly
Returns the value of attribute content_type.
-
#file_length ⇒ Object
readonly
Returns the value of attribute file_length.
-
#file_position ⇒ Object
readonly
Returns the value of attribute file_position.
-
#filename ⇒ Object
readonly
Returns the value of attribute filename.
-
#files_id ⇒ Object
readonly
Returns the value of attribute files_id.
-
#metadata ⇒ Object
readonly
Returns the value of attribute metadata.
-
#server_md5 ⇒ Object
readonly
Returns the value of attribute server_md5.
-
#upload_date ⇒ Object
readonly
Returns the value of attribute upload_date.
Attributes included from WriteConcern
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
-
#close ⇒ BSON::ObjectId
Creates or updates the document from the files collection that stores the chunks’ metadata.
-
#each { ... } ⇒ Mongo::GridIO
Read a chunk of the data from the file and yield it to the given block.
-
#eof ⇒ Boolean
(also: #eof?)
Return a boolean indicating whether the position pointer is at the end of the file.
-
#getc ⇒ String
Return the next byte from the GridFS file.
-
#gets(separator = "\n", length = nil) ⇒ String
Return the next line from a GridFS file.
-
#initialize(files, chunks, filename, mode, opts = {}) ⇒ GridIO
constructor
Create a new GridIO object.
- #inspect ⇒ Object
-
#read(length = nil) ⇒ String
(also: #data)
Read the data from the file.
-
#rewind ⇒ Integer
Rewind the file.
-
#seek(pos, whence = IO::SEEK_SET) ⇒ Integer
Position the file pointer at the provided location.
-
#tell ⇒ Integer
(also: #pos)
The current position of the file.
-
#write(io) ⇒ Integer
Write the given string (binary) data to the file.
Methods included from WriteConcern
#get_write_concern, gle?, #write_concern_from_legacy
Constructor Details
#initialize(files, chunks, filename, mode, opts = {}) ⇒ GridIO
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
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/mongo/gridfs/grid_io.rb', line 40 def initialize(files, chunks, filename, mode, opts={}) @files = files @chunks = chunks @filename = filename @mode = mode opts = opts.dup @query = opts.delete(:query) || {} @query_opts = opts.delete(:query_opts) || {} @fs_name = opts.delete(:fs_name) || Grid::DEFAULT_FS_NAME @write_concern = get_write_concern(opts) @local_md5 = Digest::MD5.new if Mongo::WriteConcern.gle?(@write_concern) @custom_attrs = {} case @mode when 'r' then init_read when 'w' then init_write(opts) else raise GridError, "Invalid file mode #{@mode}. Mode should be 'r' or 'w'." end end |
Instance Attribute Details
#chunk_size ⇒ Object (readonly)
Returns the value of attribute chunk_size.
14 15 16 |
# File 'lib/mongo/gridfs/grid_io.rb', line 14 def chunk_size @chunk_size end |
#client_md5 ⇒ Object (readonly)
Returns the value of attribute client_md5.
14 15 16 |
# File 'lib/mongo/gridfs/grid_io.rb', line 14 def client_md5 @client_md5 end |
#content_type ⇒ Object (readonly)
Returns the value of attribute content_type.
14 15 16 |
# File 'lib/mongo/gridfs/grid_io.rb', line 14 def content_type @content_type end |
#file_length ⇒ Object (readonly)
Returns the value of attribute file_length.
14 15 16 |
# File 'lib/mongo/gridfs/grid_io.rb', line 14 def file_length @file_length end |
#file_position ⇒ Object (readonly)
Returns the value of attribute file_position.
14 15 16 |
# File 'lib/mongo/gridfs/grid_io.rb', line 14 def file_position @file_position end |
#filename ⇒ Object (readonly)
Returns the value of attribute filename.
14 15 16 |
# File 'lib/mongo/gridfs/grid_io.rb', line 14 def filename @filename end |
#files_id ⇒ Object (readonly)
Returns the value of attribute files_id.
14 15 16 |
# File 'lib/mongo/gridfs/grid_io.rb', line 14 def files_id @files_id end |
#metadata ⇒ Object (readonly)
Returns the value of attribute metadata.
14 15 16 |
# File 'lib/mongo/gridfs/grid_io.rb', line 14 def @metadata end |
#server_md5 ⇒ Object (readonly)
Returns the value of attribute server_md5.
14 15 16 |
# File 'lib/mongo/gridfs/grid_io.rb', line 14 def server_md5 @server_md5 end |
#upload_date ⇒ Object (readonly)
Returns the value of attribute upload_date.
14 15 16 |
# File 'lib/mongo/gridfs/grid_io.rb', line 14 def upload_date @upload_date end |
Instance Method Details
#[](key) ⇒ Object
61 62 63 |
# File 'lib/mongo/gridfs/grid_io.rb', line 61 def [](key) @custom_attrs[key] || instance_variable_get("@#{key.to_s}") end |
#[]=(key, value) ⇒ Object
65 66 67 68 69 70 71 72 |
# File 'lib/mongo/gridfs/grid_io.rb', line 65 def []=(key, value) if PROTECTED_ATTRS.include?(key.to_sym) warn "Attempting to overwrite protected value." return nil else @custom_attrs[key] = value end end |
#close ⇒ BSON::ObjectId
Creates or updates the document from the files collection that stores the chunks’ metadata. The file becomes available only after this method has been called.
This method will be invoked automatically when on GridIO#open is passed a block. Otherwise, it must be called manually.
219 220 221 222 223 224 225 226 227 228 |
# File 'lib/mongo/gridfs/grid_io.rb', line 219 def close if @mode[0] == ?w if @current_chunk['n'].zero? && @chunk_position.zero? warn "Warning: Storing a file with zero length." end @upload_date = Time.now.utc id = @files.insert(to_mongo_object) end id end |
#each { ... } ⇒ Mongo::GridIO
Read a chunk of the data from the file and yield it to the given block.
Note that this method reads from the current file position.
239 240 241 242 243 244 245 246 |
# File 'lib/mongo/gridfs/grid_io.rb', line 239 def each return read_all unless block_given? while chunk = read(chunk_size) yield chunk break if chunk.empty? end self end |
#eof ⇒ Boolean Also known as: eof?
Return a boolean indicating whether the position pointer is at the end of the file.
173 174 175 176 |
# File 'lib/mongo/gridfs/grid_io.rb', line 173 def eof raise GridError, "file not opened for read #{@mode}" unless @mode[0] == ?r @file_position >= @file_length end |
#getc ⇒ String
Return the next byte from the GridFS file.
207 208 209 |
# File 'lib/mongo/gridfs/grid_io.rb', line 207 def getc read_length(1) end |
#gets(separator = "\n", length = nil) ⇒ String
Return the next line from a GridFS file. This probably makes sense only if you’re storing plain text. This method has a somewhat tricky API, which it inherits from Ruby’s StringIO#gets.
192 193 194 195 196 197 198 199 200 201 202 |
# File 'lib/mongo/gridfs/grid_io.rb', line 192 def gets(separator="\n", length=nil) if separator.nil? read_all elsif separator.is_a?(Integer) read_length(separator) elsif separator.length > 1 read_to_string(separator, length) else read_to_character(separator, length) end end |
#inspect ⇒ Object
248 249 250 |
# File 'lib/mongo/gridfs/grid_io.rb', line 248 def inspect "#<GridIO _id: #{@files_id}>" end |
#read(length = nil) ⇒ String Also known as: data
Read the data from the file. If a length if specified, will read from the current file position.
81 82 83 84 85 86 87 88 89 90 |
# File 'lib/mongo/gridfs/grid_io.rb', line 81 def read(length=nil) return '' if @file_length.zero? if length == 0 return '' elsif length.nil? && @file_position.zero? read_all else read_length(length) end end |
#rewind ⇒ Integer
Rewind the file. This is equivalent to seeking to the zeroth position.
164 165 166 167 |
# File 'lib/mongo/gridfs/grid_io.rb', line 164 def rewind raise GridError, "file not opened for read" unless @mode[0] == ?r seek(0) end |
#seek(pos, whence = IO::SEEK_SET) ⇒ Integer
Position the file pointer at the provided location.
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/mongo/gridfs/grid_io.rb', line 132 def seek(pos, whence=IO::SEEK_SET) raise GridError, "Seek is only allowed in read mode." unless @mode == 'r' target_pos = case whence when IO::SEEK_CUR @file_position + pos when IO::SEEK_END @file_length + pos when IO::SEEK_SET pos end new_chunk_number = (target_pos / @chunk_size).to_i if new_chunk_number != @current_chunk['n'] save_chunk(@current_chunk) if @mode[0] == ?w @current_chunk = get_chunk(new_chunk_number) end @file_position = target_pos @chunk_position = @file_position % @chunk_size @file_position end |
#tell ⇒ Integer Also known as: pos
The current position of the file.
156 157 158 |
# File 'lib/mongo/gridfs/grid_io.rb', line 156 def tell @file_position end |
#write(io) ⇒ Integer
Write the given string (binary) data to the file.
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/mongo/gridfs/grid_io.rb', line 100 def write(io) raise GridError, "file not opened for write" unless @mode[0] == ?w if io.is_a? String if Mongo::WriteConcern.gle?(@write_concern) @local_md5.update(io) end write_string(io) else length = 0 if Mongo::WriteConcern.gle?(@write_concern) while(string = io.read(@chunk_size)) @local_md5.update(string) length += write_string(string) end else while(string = io.read(@chunk_size)) length += write_string(string) end end length end end |