123456789_123456789_123456789_123456789_123456789_

Class: Mongo::Grid::FSBucket

Relationships & Source Files
Namespace Children
Modules:
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, Forwardable
Inherits: Object
Defined in: lib/mongo/grid/fs_bucket.rb,
lib/mongo/grid/stream.rb,
lib/mongo/grid/stream/read.rb,
lib/mongo/grid/stream/write.rb

Overview

Represents a view of the GridFS in the database.

Since:

  • 2.0.0

Constant Summary

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(database, options = {}) ⇒ FSBucket

Create the GridFS.

Examples:

Create the GridFS.

Grid::FSBucket.new(database)

Parameters:

  • database (Database)

    The database the files reside in.

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

    The GridFS options.

Options Hash (options):

  • :bucket_name (String)

    The prefix for the files and chunks collections.

  • :chunk_size (Integer)

    Override the default chunk size.

  • :fs_name (String)

    The prefix for the files and chunks collections.

  • :read (Hash)

    The read preference options. The hash may have the following items:

    • :mode – read preference specified as a symbol; valid values are :primary, :primary_preferred, :secondary, :secondary_preferred and :nearest.

    • :tag_sets – an array of hashes.

    • :local_threshold.

  • :session (Session)

    The session to use.

  • :write (Hash)

    Deprecated. Equivalent to :write_concern option.

  • :write_concern (Hash)

    The write concern options. Can be :w => Integer|String, :fsync => Boolean, :j => Boolean.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/grid/fs_bucket.rb', line 70

def initialize(database, options = {})
  @database = database
  @options = options.dup
=begin WriteConcern object support
  if @options[:write_concern].is_a?(WriteConcern::Base)
    # Cache the instance so that we do not needlessly reconstruct it.
    @write_concern = @options[:write_concern]
    @options[:write_concern] = @write_concern.options
  end
=end
  @options.freeze
  @chunks_collection = database[chunks_name]
  @files_collection = database[files_name]
end

Instance Attribute Details

#chunks_collectionCollection (readonly)

Returns:

  • (Collection)

    chunks_collection The chunks collection.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/grid/fs_bucket.rb', line 88

attr_reader :chunks_collection

#databaseDatabase (readonly)

Returns:

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/grid/fs_bucket.rb', line 93

attr_reader :database

#files_collectionCollection (readonly)

Returns:

  • (Collection)

    files_collection The files collection.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/grid/fs_bucket.rb', line 98

attr_reader :files_collection

#optionsHash (readonly)

Returns:

  • (Hash)

    options The FSBucket options.

Since:

  • 2.1.0

[ GitHub ]

  
# File 'lib/mongo/grid/fs_bucket.rb', line 103

attr_reader :options

Instance Method Details

#chunks_name (private)

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/grid/fs_bucket.rb', line 513

def chunks_name
  "#{prefix}.#{Grid::File::Chunk::COLLECTION}"
end

#create_index_if_missing!(collection, index_spec, options = {}) (private)

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/grid/fs_bucket.rb', line 543

def create_index_if_missing!(collection, index_spec, options = {})
  indexes_view = collection.indexes
  begin
    if indexes_view.get(index_spec).nil?
      indexes_view.create_one(index_spec, options)
    end
  rescue Mongo::Error::OperationFailure::Family => e
    # proceed with index creation if a NamespaceNotFound error is thrown
    if e.code == 26
      indexes_view.create_one(index_spec, options)
    else
      raise
    end
  end
end

#delete(id, opts = {}) ⇒ Result

Remove a single file, identified by its id from the GridFS.

Examples:

Remove a file from the GridFS.

fs.delete(id)

Parameters:

  • id (BSON::ObjectId, Object)

    The id of the file to remove.

Returns:

  • (Result)

    The result of the remove.

Raises:

Since:

  • 2.1.0

[ GitHub ]

  
# File 'lib/mongo/grid/fs_bucket.rb', line 220

def delete(id, opts = {})
  timeout_holder = CsotTimeoutHolder.new(operation_timeouts: operation_timeouts(opts))
  result = files_collection
    .find({ :_id => id }, @options.merge(timeout_ms: timeout_holder.remaining_timeout_ms))
    .delete_one(timeout_ms: timeout_holder.remaining_timeout_ms)
  chunks_collection
    .find({ :files_id => id }, @options.merge(timeout_ms: timeout_holder.remaining_timeout_ms))
    .delete_many(timeout_ms: timeout_holder.remaining_timeout_ms)
  raise Error::FileNotFound.new(id, :id) if result.n == 0
  result
end

#delete_one(file, opts = {}) ⇒ Result

Remove a single file from the GridFS.

Examples:

Remove a file from the GridFS.

fs.delete_one(file)

Parameters:

Returns:

  • (Result)

    The result of the remove.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/grid/fs_bucket.rb', line 204

def delete_one(file, opts = {})
  delete(file.id, opts)
end

#download_to_stream(id, io)

Downloads the contents of the file specified by id and writes them to the destination io object.

Examples:

Download the file and write it to the io object.

fs.download_to_stream(id, io)

Parameters:

  • id (BSON::ObjectId, Object)

    The id of the file to read.

  • io (IO)

    The io object to write to.

Since:

  • 2.1.0

[ GitHub ]

  
# File 'lib/mongo/grid/fs_bucket.rb', line 271

def download_to_stream(id, io)
  open_download_stream(id) do |stream|
    stream.each do |chunk|
      io << chunk
    end
  end
end

#download_to_stream_by_name(filename, io, opts = {})

Downloads the contents of the stored file specified by filename and by the revision in options and writes the contents to the destination io object.

Revision numbers are defined as follows: 0 = the original stored file 1 = the first revision 2 = the second revision etc…-2 = the second most recent revision -1 = the most recent revision

@example Download the original file.

fs.download_to_stream_by_name('some-file.txt', io, revision: 0)

Examples:

Download the most recent revision.

fs.download_to_stream_by_name('some-file.txt', io)

Download the second revision of the stored file.

fs.download_to_stream_by_name('some-file.txt', io, revision: 2)

Parameters:

  • filename (String)

    The file’s name.

  • io (IO)

    The io object to write to.

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

    ::Mongo::Options for the download.

Options Hash (opts):

  • :revision (Integer)

    The revision number of the file to download. Defaults to -1, the most recent version.

Raises:

Since:

  • 2.1.0

[ GitHub ]

  
# File 'lib/mongo/grid/fs_bucket.rb', line 364

def download_to_stream_by_name(filename, io, opts = {})
  download_to_stream(open_download_stream_by_name(filename, opts).file_id, io)
end

#drop(opts = {})

Drop the collections that implement this bucket.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/grid/fs_bucket.rb', line 493

def drop(opts = {})
  context = Operation::Context.new(operation_timeouts: operation_timeouts(opts))
  files_collection.drop(timeout_ms: context.remaining_timeout_ms)
  chunks_collection.drop(timeout_ms: context.remaining_timeout_ms)
end

#ensure_indexes!(timeout_holder = nil) (private)

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/grid/fs_bucket.rb', line 521

def ensure_indexes!(timeout_holder = nil)
  fc_idx = files_collection.find(
    {},
    limit: 1,
    projection: { _id: 1 },
    timeout_ms: timeout_holder&.remaining_timeout_ms
  ).first
  if fc_idx.nil?
    create_index_if_missing!(files_collection, FSBucket::FILES_INDEX)
  end

  cc_idx = chunks_collection.find(
    {},
    limit: 1,
    projection: { _id: 1 },
    timeout_ms: timeout_holder&.remaining_timeout_ms
  ).first
  if cc_idx.nil?
    create_index_if_missing!(chunks_collection, FSBucket::CHUNKS_INDEX, :unique => true)
  end
end

#files_name (private)

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/grid/fs_bucket.rb', line 517

def files_name
  "#{prefix}.#{Grid::File::Info::COLLECTION}"
end

#find(selector = nil, options = {}) ⇒ CollectionView

Find files collection documents matching a given selector.

Examples:

Find files collection documents by a filename.

fs.find(filename: 'file.txt')

Parameters:

  • selector (Hash) (defaults to: nil)

    The selector to use in the find.

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

    The options for the find.

Options Hash (options):

  • :allow_disk_use (true, false)

    Whether the server can write temporary data to disk while executing the find operation.

  • :batch_size (Integer)

    The number of documents returned in each batch of results from MongoDB.

  • :limit (Integer)

    The max number of docs to return from the query.

  • :no_cursor_timeout (true, false)

    The server normally times out idle cursors after an inactivity period (10 minutes) to prevent excess memory use. Set this option to prevent that.

  • :skip (Integer)

    The number of docs to skip before returning results.

  • :sort (Hash)

    The key and direction pairs by which the result set will be sorted.

Returns:

  • (CollectionView)

    The collection view.

Since:

  • 2.1.0

[ GitHub ]

  
# File 'lib/mongo/grid/fs_bucket.rb', line 134

def find(selector = nil, options = {})
  opts = options.merge(read: read_preference) if read_preference
  files_collection.find(selector, opts || options)
end

#find_one(selector = nil) ⇒ Grid::File

Deprecated.

Please use #find instead with a limit of -1. Will be removed in version 3.0.

Find a file in the GridFS.

Examples:

Find a file by its id.

fs.find_one(_id: id)

Find a file by its filename.

fs.find_one(filename: 'test.txt')

Parameters:

  • selector (Hash) (defaults to: nil)

    The selector.

Returns:

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/grid/fs_bucket.rb', line 155

def find_one(selector = nil)
  file_info = files_collection.find(selector).first
  return nil unless file_info
  chunks = chunks_collection.find(:files_id => file_info[:_id]).sort(:n => 1)
  Grid::File.new(chunks.to_a, Options::Mapper.transform(file_info, Grid::File::Info::MAPPINGS.invert))
end

#insert_one(file) ⇒ BSON::ObjectId

Deprecated.

Please use #upload_from_stream or #open_upload_stream instead. Will be removed in version 3.0.

Insert a single file into the GridFS.

Examples:

Insert a single file.

fs.insert_one(file)

Parameters:

Returns:

  • (BSON::ObjectId)

    The file id.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/grid/fs_bucket.rb', line 175

def insert_one(file)
  @indexes ||= ensure_indexes!
  chunks_collection.insert_many(file.chunks)
  files_collection.insert_one(file.info)
  file.id
end

#open_download_stream(id, options = nil) {|The| ... } ⇒ Stream::Read

Opens a stream from which a file can be downloaded, specified by id.

Examples:

Open a stream from which a file can be downloaded.

fs.open_download_stream(id)

Parameters:

  • id (BSON::ObjectId, Object)

    The id of the file to read.

  • options (Hash) (defaults to: nil)

    The options.

Options Hash (options):

  • :file_info_doc (BSON::Document)

    For internal driver use only. A BSON document to use as file information.

Yield Parameters:

  • The (Hash)

    read stream.

Returns:

Since:

  • 2.1.0

[ GitHub ]

  
# File 'lib/mongo/grid/fs_bucket.rb', line 248

def open_download_stream(id, options = nil)
  options = Utils.shallow_symbolize_keys(options || {})
  read_stream(id, **options).tap do |stream|
    if block_given?
      begin
        yield stream
      ensure
        stream.close
      end
    end
  end
end

#open_download_stream_by_name(filename, opts = {}) {|The| ... } ⇒ Stream::Read

Opens a stream from which the application can read the contents of the stored file specified by filename and the revision in options.

Revision numbers are defined as follows: 0 = the original stored file 1 = the first revision 2 = the second revision etc…-2 = the second most recent revision -1 = the most recent revision

@example Open a stream to download the original file.

fs.open_download_stream_by_name('some-file.txt', revision: 0)

Examples:

Open a stream to download the most recent revision.

fs.open_download_stream_by_name('some-file.txt')

Open a stream to download the second revision of the stored file.

fs.open_download_stream_by_name('some-file.txt', revision: 2)

Parameters:

  • filename (String)

    The file’s name.

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

    ::Mongo::Options for the download.

Options Hash (opts):

  • :revision (Integer)

    The revision number of the file to download. Defaults to -1, the most recent version.

Yield Parameters:

  • The (Hash)

    read stream.

Returns:

Raises:

Since:

  • 2.1.0

[ GitHub ]

  
# File 'lib/mongo/grid/fs_bucket.rb', line 313

def open_download_stream_by_name(filename, opts = {}, &block)
  revision = opts.fetch(:revision, -1)
  if revision < 0
    skip = revision.abs - 1
    sort = { 'uploadDate' => Mongo::Index::DESCENDING }
  else
    skip = revision
    sort = { 'uploadDate' => Mongo::Index::ASCENDING }
  end
  file_info_doc = files_collection.find({ filename: filename} ,
                                     sort: sort,
                                     skip: skip,
                                     limit: -1).first
  unless file_info_doc
    raise Error::FileNotFound.new(filename, :filename) unless opts[:revision]
    raise Error::InvalidFileRevision.new(filename, opts[:revision])
  end
  open_download_stream(file_info_doc[:_id], file_info_doc: file_info_doc, &block)
end

#open_upload_stream(filename, opts = {}) {|The| ... } ⇒ Stream::Write

Opens an upload stream to GridFS to which the contents of a file or blob can be written.

Parameters:

  • filename (String)

    The name of the file in GridFS.

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

    The options for the write stream.

  • options (Hash)

    a customizable set of options

Options Hash (opts):

  • :file_id (Object)

    An optional unique file id. A BSON::ObjectId is automatically generated if a file id is not provided.

  • :chunk_size (Integer)

    Override the default chunk size.

  • :metadata (Hash)

    User data for the ‘metadata’ field of the files collection document.

  • :content_type (String)

    The content type of the file. Deprecated, please use the metadata document instead.

  • :aliases (Array<String>)

    A list of aliases. Deprecated, please use the metadata document instead.

Yield Parameters:

  • The (Hash)

    write stream.

Returns:

Since:

  • 2.1.0

[ GitHub ]

  
# File 'lib/mongo/grid/fs_bucket.rb', line 394

def open_upload_stream(filename, opts = {})
  opts = Utils.shallow_symbolize_keys(opts)
  write_stream(filename, **opts).tap do |stream|
    if block_given?
      begin
        yield stream
      ensure
        stream.close
      end
    end
  end
end

#operation_timeouts(opts = {}) ⇒ Hash (private)

This method is for internal use only.

Returns:

  • (Hash)

    timeout_ms value set on the operation level (if any), and/or timeout_ms that is set on collection/database/client level (if any).

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/grid/fs_bucket.rb', line 563

def operation_timeouts(opts = {})
  # TODO: We should re-evaluate if we need two timeouts separately.
  {}.tap do |result|
    if opts[:timeout_ms].nil?
      result[:inherited_timeout_ms] = database.timeout_ms
    else
      result[:operation_timeout_ms] = opts[:timeout_ms]
    end
  end
end

#prefixString

Get the prefix for the GridFS

Examples:

Get the prefix.

fs.prefix

Returns:

  • (String)

    The GridFS prefix.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/grid/fs_bucket.rb', line 190

def prefix
  @options[:fs_name] || @options[:bucket_name] || DEFAULT_ROOT
end

#read_preferenceBSON::Document

Note:

This method always returns a BSON::Document instance, even though the FSBucket constructor specifies the type of :read as a Hash, not as a BSON::Document.

Get the read preference.

Returns:

  • (BSON::Document)

    The read preference. The document may have the following fields:

    • :mode – read preference specified as a symbol; valid values are :primary, :primary_preferred, :secondary, :secondary_preferred and :nearest.

    • :tag_sets – an array of hashes.

    • :local_threshold.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/grid/fs_bucket.rb', line 465

def read_preference
  @read_preference ||= begin
    pref = options[:read] || database.read_preference
    if BSON::Document === pref
      pref
    else
      BSON::Document.new(pref)
    end
  end
end

#read_stream(id, **opts) (private)

Parameters:

  • opts (Hash)

    The options.

Options Hash (**opts):

  • :file_info_doc (BSON::Document)

    For internal driver use only. A BSON document to use as file information.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/grid/fs_bucket.rb', line 505

def read_stream(id, **opts)
  Stream.get(self, Stream::READ_MODE, { file_id: id }.update(options).update(opts))
end

#upload_from_stream(filename, io, opts = {}) ⇒ BSON::ObjectId

Uploads a user file to a GridFS bucket. Reads the contents of the user file from the source stream and uploads it as chunks in the chunks collection. After all the chunks have been uploaded, it creates a files collection document for the filename in the files collection.

Examples:

Upload a file to the GridFS bucket.

fs.upload_from_stream('a-file.txt', file)

Parameters:

  • filename (String)

    The filename of the file to upload.

  • io (IO)

    The source io stream to upload from.

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

    The options for the write stream.

  • options (Hash)

    a customizable set of options

Options Hash (opts):

  • :file_id (Object)

    An optional unique file id. An ObjectId is generated otherwise.

  • :chunk_size (Integer)

    Override the default chunk size.

  • :metadata (Hash)

    User data for the ‘metadata’ field of the files collection document.

  • :content_type (String)

    The content type of the file. Deprecated, please use the metadata document instead.

  • :aliases (Array<String>)

    A list of aliases. Deprecated, please use the metadata document instead.

Returns:

  • (BSON::ObjectId)

    The ObjectId file id.

Since:

  • 2.1.0

[ GitHub ]

  
# File 'lib/mongo/grid/fs_bucket.rb', line 435

def upload_from_stream(filename, io, opts = {})
  open_upload_stream(filename, opts) do |stream|
    begin
      stream.write(io)
    # IOError and SystemCallError are for errors reading the io.
    # Error::SocketError and Error::SocketTimeoutError are for
    # writing to MongoDB.
    rescue IOError, SystemCallError, Error::SocketError, Error::SocketTimeoutError
      begin
        stream.abort
      rescue Error::OperationFailure
      end
      raise
    end
  end.file_id
end

#write_concernMongo::WriteConcern

Get the write concern.

Examples:

Get the write concern.

stream.write_concern

Returns:

Since:

  • 2.1.0

[ GitHub ]

  
# File 'lib/mongo/grid/fs_bucket.rb', line 484

def write_concern
  @write_concern ||= if wco = @options[:write_concern] || @options[:write]
    WriteConcern.get(wco)
  else
    database.write_concern
  end
end

#write_stream(filename, **opts) (private)

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/grid/fs_bucket.rb', line 509

def write_stream(filename, **opts)
  Stream.get(self, Stream::WRITE_MODE, { filename: filename }.update(options).update(opts))
end