Class Mongo::GridIO
In: lib/mongo/gridfs/grid_io.rb
Parent: Object

GridIO objects represent files in the GridFS specification. This class manages the reading and writing of file chunks and metadata.

Methods

[]   []=   close   data   each   eof   eof?   getc   gets   inspect   new   pos   read   rewind   seek   tell   write  

Included Modules

Mongo::WriteConcern

Constants

DEFAULT_CHUNK_SIZE = 256 * 1024
DEFAULT_CONTENT_TYPE = 'binary/octet-stream'
PROTECTED_ATTRS = [:files_id, :file_length, :client_md5, :server_md5]

Attributes

chunk_size  [R] 
client_md5  [R] 
content_type  [R] 
file_length  [R] 
file_position  [R] 
filename  [R] 
files_id  [R] 
metadata  [R] 
server_md5  [R] 
upload_date  [R] 

Public Class methods

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

@param [Mongo::Collection] files a collection for storing file metadata. @param [Mongo::Collection] chunks a collection for storing file chunks. @param [String] filename the name of the file to open or write. @param [String] mode ‘r’ or ‘w’ or reading or creating a file.

@option opts [Hash] :query a query selector used when opening the file in ‘r’ mode. @option opts [Hash] :query_opts any query options to be used when opening the file in ‘r’ mode. @option opts [String] :fs_name the file system prefix. @option opts [Integer] (262144) :chunk_size size of file chunks in bytes. @option opts [Hash] :metadata ({}) any additional data to store with the file. @option opts [ObjectId] :_id (ObjectId) a unique id for

  the file to be use in lieu of an automatically generated one.

@option opts [String] :content_type (‘binary/octet-stream’) If no content type is specified,

  the content type will may be inferred from the filename extension if the mime-types gem can be
  loaded. Otherwise, the content type 'binary/octet-stream' will be used.

@option opts [String, Integer, Symbol] :w (1) Set the write concern

  Notes on write concern:
    When :w > 0, the chunks sent to the server
    will be validated using an md5 hash. If validation fails, an exception will be raised.

Public Instance methods

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.

@return [BSON::ObjectId]

data(length=nil)

Alias for read

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.

@yield Yields on chunk per iteration as defined by this file‘s

  chunk size.

@return [Mongo::GridIO] self

Return a boolean indicating whether the position pointer is at the end of the file.

@return [Boolean]

eof?()

Alias for eof

Return the next byte from the GridFS file.

@return [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.

@param [String, Integer] separator or length. If a separator,

  read up to the separator. If a length, read the +length+ number
  of bytes. If nil, read the entire file.

@param [Integer] length If a separator is provided, then

  read until either finding the separator or
  passing over the +length+ number of bytes.

@return [String]

pos()

Alias for tell

Read the data from the file. If a length if specified, will read from the current file position.

@param [Integer] length

@return [String]

  the data in the file

Rewind the file. This is equivalent to seeking to the zeroth position.

@return [Integer] the position of the file after rewinding (always zero).

Position the file pointer at the provided location.

@param [Integer] pos

  the number of bytes to advance the file pointer. this can be a negative
  number.

@param [Integer] whence

  one of IO::SEEK_CUR, IO::SEEK_END, or IO::SEEK_SET

@return [Integer] the new file position

The current position of the file.

@return [Integer]

Write the given string (binary) data to the file.

@param [String] string

  the data to write

@return [Integer]

  the number of bytes written.

[Validate]