Class: Mongo::Grid::FSBucket::Stream::Read
- Inherits:
-
Object
- Object
- Mongo::Grid::FSBucket::Stream::Read
- Includes:
- Enumerable
- Defined in:
- lib/mongo/grid/stream/read.rb
Overview
A stream that reads files from the FSBucket.
Instance Attribute Summary collapse
-
#file_id ⇒ BSON::ObjectId, Object
readonly
File_id The id of the file being read.
-
#fs ⇒ FSBucket
readonly
Fs The fs bucket from which this stream reads.
-
#options ⇒ Hash
readonly
Options The stream options.
Instance Method Summary collapse
-
#close ⇒ BSON::ObjectId, Object
Close the read stream.
-
#closed? ⇒ true, false
Is the stream closed.
-
#each {|Each| ... } ⇒ Enumerator
Iterate through chunk data streamed from the FSBucket.
-
#file_info ⇒ File::Info
Get the files collection file information document for the file being read.
-
#initialize(fs, options) ⇒ Read
constructor
Create a stream for reading files from the FSBucket.
-
#read ⇒ String
Read all file data.
-
#read_preference ⇒ BSON::Document
Get the read preference.
Constructor Details
#initialize(fs, options) ⇒ Read
Create a stream for reading files from the FSBucket.
54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/mongo/grid/stream/read.rb', line 54 def initialize(fs, ) @fs = fs @options = .dup @file_id = @options.delete(:file_id) @options.freeze @open = true @timeout_holder = CsotTimeoutHolder.new( operation_timeouts: { operation_timeout_ms: [:timeout_ms], inherited_timeout_ms: fs.database.timeout_ms } ) end |
Instance Attribute Details
#file_id ⇒ BSON::ObjectId, Object (readonly)
Returns file_id The id of the file being read.
40 41 42 |
# File 'lib/mongo/grid/stream/read.rb', line 40 def file_id @file_id end |
#fs ⇒ FSBucket (readonly)
Returns fs The fs bucket from which this stream reads.
30 31 32 |
# File 'lib/mongo/grid/stream/read.rb', line 30 def fs @fs end |
#options ⇒ Hash (readonly)
Returns options The stream options.
35 36 37 |
# File 'lib/mongo/grid/stream/read.rb', line 35 def @options end |
Instance Method Details
#close ⇒ BSON::ObjectId, Object
Close the read stream.
If the stream is already closed, this method does nothing.
127 128 129 130 131 132 133 |
# File 'lib/mongo/grid/stream/read.rb', line 127 def close if @open view.close_query @open = false end file_id end |
#closed? ⇒ true, false
Is the stream closed.
143 144 145 |
# File 'lib/mongo/grid/stream/read.rb', line 143 def closed? !@open end |
#each {|Each| ... } ⇒ Enumerator
Iterate through chunk data streamed from the FSBucket.
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/mongo/grid/stream/read.rb', line 82 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 raise Error::MissingFileChunk.new(num_chunks, num_read) if num_read < num_chunks end else view.to_enum end end |
#file_info ⇒ File::Info
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.
181 182 183 184 185 186 187 188 189 190 |
# File 'lib/mongo/grid/stream/read.rb', line 181 def file_info @file_info ||= begin doc = [:file_info_doc] || fs.files_collection.find( { _id: file_id }, { timeout_ms: @timeout_holder.remaining_timeout_ms! } ).first File::Info.new(Options::Mapper.transform(doc, File::Info::MAPPINGS.invert)) if doc end end |
#read ⇒ String
Read all file data.
113 114 115 |
# File 'lib/mongo/grid/stream/read.rb', line 113 def read to_a.join end |
#read_preference ⇒ BSON::Document
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.
160 161 162 163 164 165 166 167 168 169 |
# File 'lib/mongo/grid/stream/read.rb', line 160 def read_preference @read_preference ||= begin pref = [:read] || fs.read_preference if pref.is_a?(BSON::Document) pref else BSON::Document.new(pref) end end end |