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 ⇒ Hash
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.
50 51 52 53 54 55 |
# File 'lib/mongo/grid/stream/read.rb', line 50 def initialize(fs, ) @fs = fs @options = .dup @file_id = @options.delete(:file_id) @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 @options end |
Instance Method Details
#close ⇒ BSON::ObjectId, Object
Close the read stream.
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.
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.
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 = Grid::File::Chunk.assemble([ chunk ]) yield data length_read += data.size end if block_given? view.to_enum end |
#file_info ⇒ Hash
Get the files collection file information document for the file being read.
149 150 151 152 153 154 |
# File 'lib/mongo/grid/stream/read.rb', line 149 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 |
#read ⇒ String
Read all file data.
94 95 96 |
# File 'lib/mongo/grid/stream/read.rb', line 94 def read to_a.join end |
#read_preference ⇒ Mongo::ServerSelector
Get the read preference used when streaming.
135 136 137 138 139 |
# File 'lib/mongo/grid/stream/read.rb', line 135 def read_preference @read_preference ||= @options[:read] ? ServerSelector.get(Options::Redacted.new((@options[:read] || {}).merge(fs.))) : fs.read_preference end |