123456789_123456789_123456789_123456789_123456789_

Class: Mongo::Grid::FSBucket::Stream::Write

Relationships & Source Files
Inherits: Object
Defined in: lib/mongo/grid/stream/write.rb

Overview

A stream that writes files to the ::Mongo::Grid::FSBucket.

Since:

  • 2.1.0

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(fs, options) ⇒ Write

Create a stream for writing files to the ::Mongo::Grid::FSBucket.

Examples:

Create the stream.

Stream::Write.new(fs, options)

Parameters:

  • fs (FSBucket)

    The GridFS bucket object.

  • options (Hash)

    The write stream options.

  • opts (Hash)

    a customizable set of options

Options Hash (options):

  • :file_id (Object)

    The file id. An ObjectId is generated if the file id is not provided.

  • :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.1.0

[ GitHub ]

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

def initialize(fs, options)
  @fs = fs
  @length = 0
  @n = 0
  @file_id = options[:file_id] || BSON::ObjectId.new
  @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
  @filename = @options[:filename]
  @open = true
  @timeout_holder = CsotTimeoutHolder.new(
    operation_timeouts: {
      operation_timeout_ms: options[:timeout_ms],
      inherited_timeout_ms: fs.database.timeout_ms
    }
  )
end

Instance Attribute Details

#closed?true, false (readonly)

Is the stream closed.

Examples:

Is the stream closed.

stream.closed?

Returns:

  • (true, false)

    Whether the stream is closed.

Since:

  • 2.1.0

[ GitHub ]

  
# File 'lib/mongo/grid/stream/write.rb', line 170

def closed?
  !@open
end

#file_idObject (readonly)

Returns:

  • (Object)

    file_id The id of the file being uploaded.

Since:

  • 2.1.0

[ GitHub ]

  
# File 'lib/mongo/grid/stream/write.rb', line 36

attr_reader :file_id

#filenameString (readonly)

Returns:

  • (String)

    filename The name of the file being uploaded.

Since:

  • 2.1.0

[ GitHub ]

  
# File 'lib/mongo/grid/stream/write.rb', line 41

attr_reader :filename

#fsFSBucket (readonly)

Returns:

  • (FSBucket)

    fs The fs bucket to which this stream writes.

Since:

  • 2.1.0

[ GitHub ]

  
# File 'lib/mongo/grid/stream/write.rb', line 31

attr_reader :fs

#optionsHash (readonly)

Returns:

  • (Hash)

    options The write stream options.

Since:

  • 2.1.0

[ GitHub ]

  
# File 'lib/mongo/grid/stream/write.rb', line 46

attr_reader :options

Instance Method Details

#aborttrue

Abort the upload by deleting all chunks already inserted.

Examples:

Abort the write operation.

stream.abort

Returns:

  • (true)

    True if the operation was aborted and the stream is closed.

Since:

  • 2.1.0

[ GitHub ]

  
# File 'lib/mongo/grid/stream/write.rb', line 182

def abort
  fs.chunks_collection.find(
    { :files_id => file_id },
    @options.merge(timeout_ms: @timeout_holder.remaining_timeout_ms!)
  ).delete_many
  (@open = false) || true
end

#chunks_collection (private)

Since:

  • 2.1.0

[ GitHub ]

  
# File 'lib/mongo/grid/stream/write.rb', line 192

def chunks_collection
  with_write_concern(fs.chunks_collection)
end

#closeBSON::ObjectId, Object

Close the write stream.

Examples:

Close the stream.

stream.close

Returns:

  • (BSON::ObjectId, Object)

    The file id.

Raises:

Since:

  • 2.1.0

[ GitHub ]

  
# File 'lib/mongo/grid/stream/write.rb', line 135

def close
  ensure_open!
  update_length
  files_collection.insert_one(
    file_info,
    @options.merge(timeout_ms: @timeout_holder.remaining_timeout_ms!)
  )
  @open = false
  file_id
end

#ensure_indexes! (private)

Since:

  • 2.1.0

[ GitHub ]

  
# File 'lib/mongo/grid/stream/write.rb', line 219

def ensure_indexes!
  fs.send(:ensure_indexes!, @timeout_holder)
end

#ensure_open! (private)

Raises:

Since:

  • 2.1.0

[ GitHub ]

  
# File 'lib/mongo/grid/stream/write.rb', line 223

def ensure_open!
  raise Error::ClosedStream.new if closed?
end

#file_info (private)

Since:

  • 2.1.0

[ GitHub ]

  
# File 'lib/mongo/grid/stream/write.rb', line 214

def file_info
  doc = { length: @length, _id: file_id, filename: filename }
  @file_info ||= File::Info.new(options.merge(doc))
end

#files_collection (private)

Since:

  • 2.1.0

[ GitHub ]

  
# File 'lib/mongo/grid/stream/write.rb', line 196

def files_collection
  with_write_concern(fs.files_collection)
end

#update_length (private)

Since:

  • 2.1.0

[ GitHub ]

  
# File 'lib/mongo/grid/stream/write.rb', line 210

def update_length
  file_info.document[:length] = @length
end

#with_write_concern(collection) (private)

Since:

  • 2.1.0

[ GitHub ]

  
# File 'lib/mongo/grid/stream/write.rb', line 200

def with_write_concern(collection)
  if write_concern.nil? || (collection.write_concern &&
    collection.write_concern.options == write_concern.options)
  then
    collection
  else
    collection.with(write: write_concern.options)
  end
end

#write(io) ⇒ Stream::Write

Write to the GridFS bucket from the source stream or a string.

Examples:

Write to GridFS.

stream.write(io)

Parameters:

  • io (String | IO)

    The string or IO object to upload from.

Returns:

Since:

  • 2.1.0

[ GitHub ]

  
# File 'lib/mongo/grid/stream/write.rb', line 104

def write(io)
  ensure_open!
  @indexes ||= ensure_indexes!
  @length += if io.respond_to?(:bytesize)
    # String objects
    io.bytesize
  else
    # IO objects
    io.size
  end
  chunks = File::Chunk.split(io, file_info, @n)
  @n += chunks.size
  unless chunks.empty?
    chunks_collection.insert_many(
      chunks,
      timeout_ms: @timeout_holder.remaining_timeout_ms!
    )
  end
  self
end

#write_concernMongo::WriteConcern

Get the write concern used when uploading.

Examples:

Get the write concern.

stream.write_concern

Returns:

Since:

  • 2.1.0

[ GitHub ]

  
# File 'lib/mongo/grid/stream/write.rb', line 154

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