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

Inherits:
Object
  • Object
show all
Defined in:
lib/mongo/grid/stream/write.rb

Overview

A stream that writes files to the FSBucket.

Since:

  • 2.1.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fs, options) ⇒ Write

Create a stream for writing files to the 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



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/mongo/grid/stream/write.rb', line 67

def initialize(fs, options)
  @fs = fs
  @length = 0
  @n = 0
  @file_id = options[:file_id] || BSON::ObjectId.new
  @options = options.dup
  # 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
  @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

#file_idObject (readonly)

Returns file_id The id of the file being uploaded.

Returns:

  • (Object)

    file_id The id of the file being uploaded.

Since:

  • 2.1.0



33
34
35
# File 'lib/mongo/grid/stream/write.rb', line 33

def file_id
  @file_id
end

#filenameString (readonly)

Returns filename The name of the file being uploaded.

Returns:

  • (String)

    filename The name of the file being uploaded.

Since:

  • 2.1.0



38
39
40
# File 'lib/mongo/grid/stream/write.rb', line 38

def filename
  @filename
end

#fsFSBucket (readonly)

Returns fs The fs bucket to which this stream writes.

Returns:

  • (FSBucket)

    fs The fs bucket to which this stream writes.

Since:

  • 2.1.0



28
29
30
# File 'lib/mongo/grid/stream/write.rb', line 28

def fs
  @fs
end

#optionsHash (readonly)

Returns options The write stream options.

Returns:

  • (Hash)

    options The write stream options.

Since:

  • 2.1.0



43
44
45
# File 'lib/mongo/grid/stream/write.rb', line 43

def options
  @options
end

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



178
179
180
181
182
183
184
# File 'lib/mongo/grid/stream/write.rb', line 178

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

#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



131
132
133
134
135
136
137
138
139
140
# File 'lib/mongo/grid/stream/write.rb', line 131

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

#closed?true, false

Is the stream closed.

Examples:

Is the stream closed.

stream.closed?

Returns:

  • (true, false)

    Whether the stream is closed.

Since:

  • 2.1.0



166
167
168
# File 'lib/mongo/grid/stream/write.rb', line 166

def closed?
  !@open
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



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

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



150
151
152
153
154
155
156
# File 'lib/mongo/grid/stream/write.rb', line 150

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