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
.
Class Method Summary
-
.new(fs, options) ⇒ Write
constructor
Create a stream for writing files to the
::Mongo::Grid::FSBucket
.
Instance Attribute Summary
-
#closed? ⇒ true, false
readonly
Is the stream closed.
- #file_id ⇒ Object readonly
- #filename ⇒ String readonly
- #fs ⇒ FSBucket readonly
- #options ⇒ Hash readonly
Instance Method Summary
-
#abort ⇒ true
Abort the upload by deleting all chunks already inserted.
-
#close ⇒ BSON::ObjectId, Object
Close the write stream.
-
#write(io) ⇒ Stream::Write
Write
to the GridFS bucket from the source stream or a string. -
#write_concern ⇒ Mongo::WriteConcern
Get the write concern used when uploading.
- #chunks_collection private
- #ensure_indexes! private
- #ensure_open! private
- #file_info private
- #files_collection private
- #update_length private
- #with_write_concern(collection) private
Constructor Details
.new(fs, options) ⇒ Write
Create a stream for writing files to the ::Mongo::Grid::FSBucket
.
# File 'lib/mongo/grid/stream/write.rb', line 70
def initialize(fs, ) @fs = fs @length = 0 @n = 0 @file_id = [:file_id] || BSON::ObjectId.new @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: [:timeout_ms], inherited_timeout_ms: fs.database.timeout_ms } ) end
Instance Attribute Details
#closed? ⇒ true
, false
(readonly)
Is the stream closed.
# File 'lib/mongo/grid/stream/write.rb', line 170
def closed? !@open end
#file_id ⇒ Object
(readonly)
# File 'lib/mongo/grid/stream/write.rb', line 36
attr_reader :file_id
#filename ⇒ String
(readonly)
# File 'lib/mongo/grid/stream/write.rb', line 41
attr_reader :filename
#fs ⇒ FSBucket (readonly)
# File 'lib/mongo/grid/stream/write.rb', line 31
attr_reader :fs
#options ⇒ Hash
(readonly)
# File 'lib/mongo/grid/stream/write.rb', line 46
attr_reader :
Instance Method Details
#abort ⇒ true
Abort the upload by deleting all chunks already inserted.
# 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)
# File 'lib/mongo/grid/stream/write.rb', line 192
def chunks_collection with_write_concern(fs.chunks_collection) end
#close ⇒ BSON::ObjectId
, Object
Close the write stream.
# 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)
# File 'lib/mongo/grid/stream/write.rb', line 219
def ensure_indexes! fs.send(:ensure_indexes!, @timeout_holder) end
#ensure_open! (private)
# File 'lib/mongo/grid/stream/write.rb', line 223
def ensure_open! raise Error::ClosedStream.new if closed? end
#file_info (private)
#files_collection (private)
# File 'lib/mongo/grid/stream/write.rb', line 196
def files_collection with_write_concern(fs.files_collection) end
#update_length (private)
# File 'lib/mongo/grid/stream/write.rb', line 210
def update_length file_info.document[:length] = @length end
#with_write_concern(collection) (private)
# File 'lib/mongo/grid/stream/write.rb', line 200
def with_write_concern(collection) if write_concern.nil? || (collection.write_concern && collection.write_concern. == write_concern. ) then collection else collection.with(write: write_concern. ) end end
#write(io) ⇒ Stream::Write
Write
to the GridFS bucket from the source stream or a string.
# 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_concern ⇒ Mongo::WriteConcern
Get the write concern used when uploading.
# 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