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

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
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.

Since:

  • 2.1.0



50
51
52
53
54
55
# File 'lib/mongo/grid/stream/read.rb', line 50

def initialize(fs, options)
  @fs = fs
  @options = options.dup
  @file_id = @options.delete(:file_id)
  @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



39
40
41
# File 'lib/mongo/grid/stream/read.rb', line 39

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



29
30
31
# File 'lib/mongo/grid/stream/read.rb', line 29

def fs
  @fs
end

#optionsHash (readonly)

Returns options The stream options.

Returns:

  • (Hash)

    options The stream options.

Since:

  • 2.1.0



34
35
36
# File 'lib/mongo/grid/stream/read.rb', line 34

def options
  @options
end

Instance Method Details

#closeBSON::ObjectId, Object

Close the read stream.

Examples:

Close the stream.

stream.close

Returns:

  • (BSON::ObjectId, Object)

    The file id.

Raises:

Since:

  • 2.1.0



108
109
110
111
112
113
# File 'lib/mongo/grid/stream/read.rb', line 108

def close
  ensure_open!
  view.close_query
  @open = false
  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



123
124
125
# File 'lib/mongo/grid/stream/read.rb', line 123

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



71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/mongo/grid/stream/read.rb', line 71

def each
  ensure_readable!
  num_chunks = (file_info.length + file_info.chunk_size - 1) / file_info.chunk_size
  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
    length_read += data.size
  end if block_given?
  view.to_enum
end

#file_infoHash

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

Examples:

Get the file info document.

stream.file_info

Returns:

  • (Hash)

    The file info document.

Since:

  • 2.1.0



147
148
149
150
151
152
# File 'lib/mongo/grid/stream/read.rb', line 147

def file_info
  doc = fs.files_collection.find(_id: file_id).first
  if doc
    @file_info ||= File::Info.new(Options::Mapper.transform(doc, File::Info::MAPPINGS.invert))
  end
end

#readString

Read all file data.

Examples:

Read the file data.

stream.read

Returns:

  • (String)

    The file data.

Raises:

Since:

  • 2.1.0



94
95
96
# File 'lib/mongo/grid/stream/read.rb', line 94

def read
  to_a.join
end

#read_preferenceMongo::ServerSelector

Get the read preference used when streaming.

Examples:

Get the read preference.

stream.read_preference

Returns:

Since:

  • 2.1.0



135
136
137
# File 'lib/mongo/grid/stream/read.rb', line 135

def read_preference
  @read_preference ||= ServerSelector.get(@options[:read] || fs.read_preference)
end