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 ⇒ Mongo::ServerSelector
Get the read preference used when streaming.
Constructor Details
#initialize(fs, options) ⇒ Read
Create a stream for reading files from the FSBucket.
53 54 55 56 57 58 59 |
# File 'lib/mongo/grid/stream/read.rb', line 53 def initialize(fs, ) @fs = fs = .dup @file_id = .delete(:file_id) .freeze @open = true end |
Instance Attribute Details
#file_id ⇒ BSON::ObjectId, Object (readonly)
Returns file_id The id of the file being read.
39 40 41 |
# File 'lib/mongo/grid/stream/read.rb', line 39 def file_id @file_id end |
#fs ⇒ FSBucket (readonly)
Returns fs The fs bucket from which this stream reads.
29 30 31 |
# File 'lib/mongo/grid/stream/read.rb', line 29 def fs @fs end |
#options ⇒ Hash (readonly)
Returns options The stream options.
34 35 36 |
# File 'lib/mongo/grid/stream/read.rb', line 34 def end |
Instance Method Details
#close ⇒ BSON::ObjectId, Object
Close the read stream.
If the stream is already closed, this method does nothing.
116 117 118 119 120 121 122 |
# File 'lib/mongo/grid/stream/read.rb', line 116 def close if @open view.close_query @open = false end file_id end |
#closed? ⇒ true, false
Is the stream closed.
132 133 134 |
# File 'lib/mongo/grid/stream/read.rb', line 132 def closed? !@open end |
#each {|Each| ... } ⇒ Enumerator
Iterate through chunk data streamed from the FSBucket.
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/mongo/grid/stream/read.rb', line 75 def each ensure_readable! info = file_info num_chunks = (info.length + info.chunk_size - 1) / info.chunk_size 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 length_read += data.size 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.
158 159 160 161 162 163 164 165 166 167 |
# File 'lib/mongo/grid/stream/read.rb', line 158 def file_info @file_info ||= begin doc = [: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 |
#read ⇒ String
Read all file data.
102 103 104 |
# File 'lib/mongo/grid/stream/read.rb', line 102 def read to_a.join end |
#read_preference ⇒ Mongo::ServerSelector
Get the read preference used when streaming.
144 145 146 |
# File 'lib/mongo/grid/stream/read.rb', line 144 def read_preference @read_preference ||= [:read] || fs.read_preference end |