Class: Mongo::Grid::FSBucket::Stream::Read

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
build/ruby-driver-v2.18/lib/mongo/grid/stream/read.rb

Overview

A stream that reads files from the FSBucket.

Since:

  • 2.1.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fs, options) ⇒ Read

Create a stream for reading files from the FSBucket.

Examples:

Create the stream.

Stream::Read.new(fs, options)

Parameters:

  • fs (FSBucket)

    The GridFS bucket object.

  • options (Hash)

    The read stream options.

Options Hash (options):

  • :file_info_doc (BSON::Document)

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

Since:

  • 2.1.0



56
57
58
59
60
61
62
# File 'build/ruby-driver-v2.18/lib/mongo/grid/stream/read.rb', line 56

def initialize(fs, options)
  @fs = fs
  @options = options.dup
  @file_id = @options.delete(:file_id)
  @options.freeze
  @open = true
end

Instance Attribute Details

#file_idBSON::ObjectId, Object (readonly)

Returns file_id The id of the file being read.

Returns:

  • (BSON::ObjectId, Object)

    file_id The id of the file being read.

Since:

  • 2.1.0



42
43
44
# File 'build/ruby-driver-v2.18/lib/mongo/grid/stream/read.rb', line 42

def file_id
  @file_id
end

#fsFSBucket (readonly)

Returns fs The fs bucket from which this stream reads.

Returns:

  • (FSBucket)

    fs The fs bucket from which this stream reads.

Since:

  • 2.1.0



32
33
34
# File 'build/ruby-driver-v2.18/lib/mongo/grid/stream/read.rb', line 32

def fs
  @fs
end

#optionsHash (readonly)

Returns options The stream options.

Returns:

  • (Hash)

    options The stream options.

Since:

  • 2.1.0



37
38
39
# File 'build/ruby-driver-v2.18/lib/mongo/grid/stream/read.rb', line 37

def options
  @options
end

Instance Method Details

#closeBSON::ObjectId, Object

Close the read stream.

If the stream is already closed, this method does nothing.

Examples:

Close the stream.

stream.close

Returns:

  • (BSON::ObjectId, Object)

    The file id.

Since:

  • 2.1.0



125
126
127
128
129
130
131
# File 'build/ruby-driver-v2.18/lib/mongo/grid/stream/read.rb', line 125

def close
  if @open
    view.close_query
    @open = false
  end
  file_id
end

#closed?true, false

Is the stream closed.

Examples:

Is the stream closd.

stream.closed?

Returns:

  • (true, false)

    Whether the stream is closed.

Since:

  • 2.1.0



141
142
143
# File 'build/ruby-driver-v2.18/lib/mongo/grid/stream/read.rb', line 141

def closed?
  !@open
end

#each {|Each| ... } ⇒ Enumerator

Iterate through chunk data streamed from the FSBucket.

Examples:

Iterate through the chunk data.

stream.each do |data|
  buffer << data
end

Yield Parameters:

  • Each (Hash)

    chunk of file data.

Returns:

  • (Enumerator)

    The enumerator.

Raises:

Since:

  • 2.1.0



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'build/ruby-driver-v2.18/lib/mongo/grid/stream/read.rb', line 78

def each
  ensure_readable!
  info = file_info
  num_chunks = (info.length + info.chunk_size - 1) / info.chunk_size
  num_read = 0
  if block_given?
    view.each_with_index.reduce(0) do |length_read, (doc, index)|
      chunk = Grid::File::Chunk.new(doc)
      validate!(index, num_chunks, chunk, length_read)
      data = chunk.data.data
      yield data
      num_read += 1
      length_read += data.size
    end.tap do
      if num_read < num_chunks
        raise Error::MissingFileChunk.new(num_chunks, num_read)
      end
    end
  else
    view.to_enum
  end
end

#file_infoFile::Info

Note:

The file information is cached in the stream. Subsequent calls to file_info will return the same information that the first call returned, and will not query the database again.

Get the files collection file information document for the file being read.

Returns:

Since:

  • 2.1.0



179
180
181
182
183
184
185
186
187
188
# File 'build/ruby-driver-v2.18/lib/mongo/grid/stream/read.rb', line 179

def file_info
  @file_info ||= begin
    doc = options[:file_info_doc] || fs.files_collection.find(_id: file_id).first
    if doc
      File::Info.new(Options::Mapper.transform(doc, File::Info::MAPPINGS.invert))
    else
      nil
    end
  end
end

#readString

Read all file data.

Examples:

Read the file data.

stream.read

Returns:

  • (String)

    The file data.

Raises:

Since:

  • 2.1.0



111
112
113
# File 'build/ruby-driver-v2.18/lib/mongo/grid/stream/read.rb', line 111

def read
  to_a.join
end

#read_preferenceBSON::Document

Note:

This method always returns a BSON::Document instance, even though the 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.1.0



158
159
160
161
162
163
164
165
166
167
# File 'build/ruby-driver-v2.18/lib/mongo/grid/stream/read.rb', line 158

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