Class: Mongo::GridIO

Inherits:
Object show all
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'

Instance Attribute Summary collapse

Instance Method Summary collapse

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

Parameters:

  • files (Mongo::Collection)

    a collection for storing file metadata.

  • chunks (Mongo::Collection)

    a collection for storing file chunks.

  • filename (String)

    the name of the file to open or write.

  • mode (String)

    ‘r’ or ‘w’ or reading or creating a file.

  • opts (Hash) (defaults to: {})

    a customizable set of options

Options Hash (opts):

  • :query (Hash)

    a query selector used when opening the file in ‘r’ mode.

  • :query_opts (Hash)

    any query options to be used when opening the file in ‘r’ mode.

  • :fs_name (String)

    the file system prefix.



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/mongo/gridfs/grid_io.rb', line 54

def initialize(files, chunks, filename, mode, opts={})
  @files      = files
  @chunks     = chunks
  @filename   = filename
  @mode       = mode
  @query      = opts[:query] || {}
  @query_opts = opts[:query_opts] || {}
  @fs_name    = opts[:fs_name] || Grid::DEFAULT_FS_NAME
  @safe       = opts[:safe] || false
  @local_md5  = Digest::MD5.new if @safe

  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_sizeObject (readonly)

Returns the value of attribute chunk_size.



31
32
33
# File 'lib/mongo/gridfs/grid_io.rb', line 31

def chunk_size
  @chunk_size
end

#client_md5Object (readonly)

Returns the value of attribute client_md5.



31
32
33
# File 'lib/mongo/gridfs/grid_io.rb', line 31

def client_md5
  @client_md5
end

#content_typeObject (readonly)

Returns the value of attribute content_type.



31
32
33
# File 'lib/mongo/gridfs/grid_io.rb', line 31

def content_type
  @content_type
end

#file_lengthObject (readonly)

Returns the value of attribute file_length.



31
32
33
# File 'lib/mongo/gridfs/grid_io.rb', line 31

def file_length
  @file_length
end

#filenameObject (readonly)

Returns the value of attribute filename.



31
32
33
# File 'lib/mongo/gridfs/grid_io.rb', line 31

def filename
  @filename
end

#files_idObject (readonly)

Returns the value of attribute files_id.



31
32
33
# File 'lib/mongo/gridfs/grid_io.rb', line 31

def files_id
  @files_id
end

#metadataObject (readonly)

Returns the value of attribute metadata.



31
32
33
# File 'lib/mongo/gridfs/grid_io.rb', line 31

def 
  @metadata
end

#server_md5Object (readonly)

Returns the value of attribute server_md5.



31
32
33
# File 'lib/mongo/gridfs/grid_io.rb', line 31

def server_md5
  @server_md5
end

#upload_dateObject (readonly)

Returns the value of attribute upload_date.



31
32
33
# File 'lib/mongo/gridfs/grid_io.rb', line 31

def upload_date
  @upload_date
end

Instance Method Details

#closeTrue

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.

Returns:

  • (True)


166
167
168
169
170
171
172
# File 'lib/mongo/gridfs/grid_io.rb', line 166

def close
  if @mode[0] == ?w
    @upload_date = Time.now.utc
    @files.insert(to_mongo_object)
  end
  true
end

#inspectObject



174
175
176
# File 'lib/mongo/gridfs/grid_io.rb', line 174

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.

Parameters:

  • length (Integer) (defaults to: nil)

Returns:

  • (String)

    the data in the file



80
81
82
83
84
85
86
87
88
# File 'lib/mongo/gridfs/grid_io.rb', line 80

def read(length=nil)
  if length == 0
    return ''
  elsif length.nil? && @file_position.zero?
    read_all
  else
    read_length(length)
  end
end

#seek(pos, whence = IO::SEEK_SET) ⇒ Integer

Position the file pointer at the provided location.

Parameters:

  • pos (Integer)

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

  • whence (Integer) (defaults to: IO::SEEK_SET)

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

Returns:

  • (Integer)

    the new file position

Raises:



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/mongo/gridfs/grid_io.rb', line 130

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

#tellInteger

The current position of the file.

Returns:

  • (Integer)


154
155
156
# File 'lib/mongo/gridfs/grid_io.rb', line 154

def tell
  @file_position
end

#write(io) ⇒ Integer

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

Parameters:

  • string (String)

    the data to write

Returns:

  • (Integer)

    the number of bytes written.

Raises:



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/mongo/gridfs/grid_io.rb', line 98

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