Class: Mongo::GridIO
- Inherits:
-
Object
- Object
- Mongo::GridIO
- Defined in:
- lib/mongo/gridfs/grid_io.rb,
lib/mongo/gridfs/grid_io_fix.rb
Overview
GridIO objects represent files in the GridFS specification. This class manages the reading and writing of file chunks and metadata.
Constant Summary
- 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)
-
- (Object) chunk_size
readonly
Returns the value of attribute chunk_size.
-
- (Object) client_md5
readonly
Returns the value of attribute client_md5.
-
- (Object) content_type
readonly
Returns the value of attribute content_type.
-
- (Object) file_length
readonly
Returns the value of attribute file_length.
-
- (Object) file_position
readonly
Returns the value of attribute file_position.
-
- (Object) filename
readonly
Returns the value of attribute filename.
-
- (Object) files_id
readonly
Returns the value of attribute files_id.
-
- (Object) metadata
readonly
Returns the value of attribute metadata.
-
- (Object) server_md5
readonly
Returns the value of attribute server_md5.
-
- (Object) upload_date
readonly
Returns the value of attribute upload_date.
Instance Method Summary (collapse)
- - (Object) [](key)
- - (Object) []=(key, value)
-
- (BSON::ObjectId) close
Creates or updates the document from the files collection that stores the chunks' metadata.
-
- (Mongo::GridIO) each { ... }
Read a chunk of the data from the file and yield it to the given block.
-
- (Boolean) eof
(also: #eof?)
Return a boolean indicating whether the position pointer is at the end of the file.
-
- (Object) get_md5
This fixes a comparson issue in JRuby 1.9.
-
- (String) getc
Return the next byte from the GridFS file.
-
- (String) gets(separator = "\n", length = nil)
Return the next line from a GridFS file.
-
- (GridIO) initialize(files, chunks, filename, mode, opts = {})
constructor
Create a new GridIO object.
- - (Object) inspect
-
- (String) read(length = nil)
(also: #data)
Read the data from the file.
-
- (Integer) rewind
Rewind the file.
-
- (Integer) seek(pos, whence = IO::SEEK_SET)
Position the file pointer at the provided location.
-
- (Integer) tell
(also: #pos)
The current position of the file.
-
- (Integer) write(io)
Write the given string (binary) data to the file.
Constructor Details
- (GridIO) initialize(files, chunks, filename, mode, opts = {})
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
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/mongo/gridfs/grid_io.rb', line 53 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 @safe = opts.delete(:safe) || false @local_md5 = Digest::MD5.new if @safe @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
- (Object) chunk_size (readonly)
Returns the value of attribute chunk_size
30 31 32 |
# File 'lib/mongo/gridfs/grid_io.rb', line 30 def chunk_size @chunk_size end |
- (Object) client_md5 (readonly)
Returns the value of attribute client_md5
30 31 32 |
# File 'lib/mongo/gridfs/grid_io.rb', line 30 def client_md5 @client_md5 end |
- (Object) content_type (readonly)
Returns the value of attribute content_type
30 31 32 |
# File 'lib/mongo/gridfs/grid_io.rb', line 30 def content_type @content_type end |
- (Object) file_length (readonly)
Returns the value of attribute file_length
30 31 32 |
# File 'lib/mongo/gridfs/grid_io.rb', line 30 def file_length @file_length end |
- (Object) file_position (readonly)
Returns the value of attribute file_position
30 31 32 |
# File 'lib/mongo/gridfs/grid_io.rb', line 30 def file_position @file_position end |
- (Object) filename (readonly)
Returns the value of attribute filename
30 31 32 |
# File 'lib/mongo/gridfs/grid_io.rb', line 30 def filename @filename end |
- (Object) files_id (readonly)
Returns the value of attribute files_id
30 31 32 |
# File 'lib/mongo/gridfs/grid_io.rb', line 30 def files_id @files_id end |
- (Object) metadata (readonly)
Returns the value of attribute metadata
30 31 32 |
# File 'lib/mongo/gridfs/grid_io.rb', line 30 def @metadata end |
- (Object) server_md5 (readonly)
Returns the value of attribute server_md5
30 31 32 |
# File 'lib/mongo/gridfs/grid_io.rb', line 30 def server_md5 @server_md5 end |
- (Object) upload_date (readonly)
Returns the value of attribute upload_date
30 31 32 |
# File 'lib/mongo/gridfs/grid_io.rb', line 30 def upload_date @upload_date end |
Instance Method Details
- (Object) [](key)
74 75 76 |
# File 'lib/mongo/gridfs/grid_io.rb', line 74 def [](key) @custom_attrs[key] || instance_variable_get("@#{key.to_s}") end |
- (Object) []=(key, value)
78 79 80 81 82 83 84 85 |
# File 'lib/mongo/gridfs/grid_io.rb', line 78 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 |
- (BSON::ObjectId) close
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.
266 267 268 269 270 271 272 273 274 275 |
# File 'lib/mongo/gridfs/grid_io.rb', line 266 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 |
- (Mongo::GridIO) each { ... }
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.
286 287 288 289 290 291 292 |
# File 'lib/mongo/gridfs/grid_io.rb', line 286 def each return read_all unless block_given? while chunk = read(chunk_size) yield chunk end self end |
- (Boolean) eof Also known as: eof?
Return a boolean indicating whether the position pointer is at the end of the file.
186 187 188 189 |
# File 'lib/mongo/gridfs/grid_io.rb', line 186 def eof raise GridError, "file not opened for read #{@mode}" unless @mode[0] == ?r @file_position >= @file_length end |
- (Object) get_md5
This fixes a comparson issue in JRuby 1.9
450 451 452 453 454 455 456 457 458 459 460 461 462 463 |
# File 'lib/mongo/gridfs/grid_io.rb', line 450 def get_md5 md5_command = BSON::OrderedHash.new md5_command['filemd5'] = @files_id md5_command['root'] = @fs_name @server_md5 = @files.db.command(md5_command)['md5'] if @safe @client_md5 = @local_md5.hexdigest if @local_md5.to_s != @server_md5.to_s raise GridMD5Failure, "File on server failed MD5 check" end else @server_md5 end end |
- (String) getc
Return the next byte from the GridFS file.
254 255 256 |
# File 'lib/mongo/gridfs/grid_io.rb', line 254 def getc read_length(1) end |
- (String) gets(separator = "\n", length = nil)
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.
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 |
# File 'lib/mongo/gridfs/grid_io.rb', line 205 def gets(separator="\n", length=nil) if separator.nil? read_all elsif separator.is_a?(Integer) read_length(separator) elsif separator.length > 1 result = '' len = 0 match_idx = 0 match_num = separator.length - 1 to_match = separator[match_idx].chr if length matcher = lambda {|idx, num| idx < num && len < length } else matcher = lambda {|idx, num| idx < num} end while matcher.call(match_idx, match_num) && char = getc result << char len += 1 if char == to_match while match_idx < match_num do match_idx += 1 to_match = separator[match_idx].chr char = getc result << char if char != to_match match_idx = 0 to_match = separator[match_idx].chr break end end end end result else result = '' len = 0 while char = getc result << char len += 1 break if char == separator || (length ? len >= length : false) end result end end |
- (Object) inspect
294 295 296 |
# File 'lib/mongo/gridfs/grid_io.rb', line 294 def inspect "#<GridIO _id: #{@files_id}>" end |
- (String) read(length = nil) Also known as: data
Read the data from the file. If a length if specified, will read from the current file position.
94 95 96 97 98 99 100 101 102 103 |
# File 'lib/mongo/gridfs/grid_io.rb', line 94 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 |
- (Integer) rewind
Rewind the file. This is equivalent to seeking to the zeroth position.
177 178 179 180 |
# File 'lib/mongo/gridfs/grid_io.rb', line 177 def rewind raise GridError, "file not opened for read" unless @mode[0] == ?r seek(0) end |
- (Integer) seek(pos, whence = IO::SEEK_SET)
Position the file pointer at the provided location.
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/mongo/gridfs/grid_io.rb', line 145 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 |
- (Integer) tell Also known as: pos
The current position of the file.
169 170 171 |
# File 'lib/mongo/gridfs/grid_io.rb', line 169 def tell @file_position end |
- (Integer) write(io)
Write the given string (binary) data to the file.
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/mongo/gridfs/grid_io.rb', line 113 def write(io) raise GridError, "file not opened for write" unless @mode[0] == ?w if io.is_a? String if @safe @local_md5.update(io) end write_string(io) else length = 0 if @safe 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 |