Class: Mongo::Grid::FSBucket
- Inherits:
-
Object
- Object
- Mongo::Grid::FSBucket
- Extended by:
- Forwardable
- Defined in:
- lib/mongo/grid/fs_bucket.rb,
lib/mongo/grid/stream.rb,
lib/mongo/grid/stream/read.rb,
lib/mongo/grid/stream/write.rb
Overview
Represents a view of the GridFS in the database.
Defined Under Namespace
Modules: Stream
Constant Summary collapse
- DEFAULT_ROOT =
The default root prefix.
'fs'- CHUNKS_INDEX =
The specification for the chunks collection index.
{ files_id: 1, n: 1 }.freeze
- FILES_INDEX =
The specification for the files collection index.
{ filename: 1, uploadDate: 1 }.freeze
Instance Attribute Summary collapse
-
#chunks_collection ⇒ Collection
readonly
Chunks_collection The chunks collection.
-
#database ⇒ Database
readonly
Database The database.
-
#files_collection ⇒ Collection
readonly
Files_collection The files collection.
-
#options ⇒ Hash
readonly
Options The FSBucket options.
Instance Method Summary collapse
-
#delete(id, opts = {}) ⇒ Result
Remove a single file, identified by its id from the GridFS.
-
#delete_one(file, opts = {}) ⇒ Result
Remove a single file from the GridFS.
-
#download_to_stream(id, io) ⇒ Object
Downloads the contents of the file specified by id and writes them to the destination io object.
-
#download_to_stream_by_name(filename, io, opts = {}) ⇒ Object
Downloads the contents of the stored file specified by filename and by the revision in options and writes the contents to the destination io object.
-
#drop(opts = {}) ⇒ Object
Drop the collections that implement this bucket.
-
#find(selector = nil, options = {}) ⇒ CollectionView
Find files collection documents matching a given selector.
-
#find_one(selector = nil) ⇒ Grid::File
deprecated
Deprecated.
Please use #find instead with a limit of -1. Will be removed in version 3.0.
-
#initialize(database, options = {}) ⇒ FSBucket
constructor
Create the GridFS.
-
#insert_one(file) ⇒ BSON::ObjectId
deprecated
Deprecated.
Please use #upload_from_stream or #open_upload_stream instead. Will be removed in version 3.0.
-
#open_download_stream(id, options = nil) {|The| ... } ⇒ Stream::Read
Opens a stream from which a file can be downloaded, specified by id.
-
#open_download_stream_by_name(filename, opts = {}) {|The| ... } ⇒ Stream::Read
Opens a stream from which the application can read the contents of the stored file specified by filename and the revision in options.
-
#open_upload_stream(filename, opts = {}) {|The| ... } ⇒ Stream::Write
Opens an upload stream to GridFS to which the contents of a file or blob can be written.
-
#prefix ⇒ String
Get the prefix for the GridFS.
-
#read_preference ⇒ BSON::Document
Get the read preference.
-
#upload_from_stream(filename, io, opts = {}) ⇒ BSON::ObjectId
Uploads a user file to a GridFS bucket.
-
#write_concern ⇒ Mongo::WriteConcern
Get the write concern.
Constructor Details
#initialize(database, options = {}) ⇒ FSBucket
Create the GridFS.
68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/mongo/grid/fs_bucket.rb', line 68 def initialize(database, = {}) @database = database @options = .dup # WriteConcern object support # if @options[:write_concern].is_a?(WriteConcern::Base) # # Cache the instance so that we do not needlessly reconstruct it. # @write_concern = @options[:write_concern] # @options[:write_concern] = @write_concern.options # end @options.freeze @chunks_collection = database[chunks_name] @files_collection = database[files_name] end |
Instance Attribute Details
#chunks_collection ⇒ Collection (readonly)
Returns chunks_collection The chunks collection.
85 86 87 |
# File 'lib/mongo/grid/fs_bucket.rb', line 85 def chunks_collection @chunks_collection end |
#database ⇒ Database (readonly)
Returns database The database.
90 91 92 |
# File 'lib/mongo/grid/fs_bucket.rb', line 90 def database @database end |
#files_collection ⇒ Collection (readonly)
Returns files_collection The files collection.
95 96 97 |
# File 'lib/mongo/grid/fs_bucket.rb', line 95 def files_collection @files_collection end |
#options ⇒ Hash (readonly)
Returns options The FSBucket options.
100 101 102 |
# File 'lib/mongo/grid/fs_bucket.rb', line 100 def @options end |
Instance Method Details
#delete(id, opts = {}) ⇒ Result
Remove a single file, identified by its id from the GridFS.
218 219 220 221 222 223 224 225 226 227 228 229 |
# File 'lib/mongo/grid/fs_bucket.rb', line 218 def delete(id, opts = {}) timeout_holder = CsotTimeoutHolder.new(operation_timeouts: operation_timeouts(opts)) result = files_collection .find({ _id: id }, @options.merge(timeout_ms: timeout_holder.remaining_timeout_ms)) .delete_one(timeout_ms: timeout_holder.remaining_timeout_ms) chunks_collection .find({ files_id: id }, @options.merge(timeout_ms: timeout_holder.remaining_timeout_ms)) .delete_many(timeout_ms: timeout_holder.remaining_timeout_ms) raise Error::FileNotFound.new(id, :id) if result.n == 0 result end |
#delete_one(file, opts = {}) ⇒ Result
Remove a single file from the GridFS.
202 203 204 |
# File 'lib/mongo/grid/fs_bucket.rb', line 202 def delete_one(file, opts = {}) delete(file.id, opts) end |
#download_to_stream(id, io) ⇒ Object
Downloads the contents of the file specified by id and writes them to the destination io object.
270 271 272 273 274 275 276 |
# File 'lib/mongo/grid/fs_bucket.rb', line 270 def download_to_stream(id, io) open_download_stream(id) do |stream| stream.each do |chunk| io << chunk end end end |
#download_to_stream_by_name(filename, io, opts = {}) ⇒ Object
Downloads the contents of the stored file specified by filename and by the revision in options and writes the contents to the destination io object.
Revision numbers are defined as follows: 0 = the original stored file 1 = the first revision 2 = the second revision etc…-2 = the second most recent revision -1 = the most recent revision
# @example Download the original file.
fs.download_to_stream_by_name('some-file.txt', io, revision: 0)
364 365 366 |
# File 'lib/mongo/grid/fs_bucket.rb', line 364 def download_to_stream_by_name(filename, io, opts = {}) download_to_stream(open_download_stream_by_name(filename, opts).file_id, io) end |
#drop(opts = {}) ⇒ Object
Drop the collections that implement this bucket.
491 492 493 494 495 |
# File 'lib/mongo/grid/fs_bucket.rb', line 491 def drop(opts = {}) context = Operation::Context.new(operation_timeouts: operation_timeouts(opts)) files_collection.drop(timeout_ms: context.remaining_timeout_ms) chunks_collection.drop(timeout_ms: context.remaining_timeout_ms) end |
#find(selector = nil, options = {}) ⇒ CollectionView
Find files collection documents matching a given selector.
131 132 133 134 |
# File 'lib/mongo/grid/fs_bucket.rb', line 131 def find(selector = nil, = {}) opts = .merge(read: read_preference) if read_preference files_collection.find(selector, opts || ) end |
#find_one(selector = nil) ⇒ Grid::File
Please use #find instead with a limit of -1. Will be removed in version 3.0.
Find a file in the GridFS.
152 153 154 155 156 157 158 |
# File 'lib/mongo/grid/fs_bucket.rb', line 152 def find_one(selector = nil) file_info = files_collection.find(selector).first return nil unless file_info chunks = chunks_collection.find(files_id: file_info[:_id]).sort(n: 1) Grid::File.new(chunks.to_a, Options::Mapper.transform(file_info, Grid::File::Info::MAPPINGS.invert)) end |
#insert_one(file) ⇒ BSON::ObjectId
Please use #upload_from_stream or #open_upload_stream instead. Will be removed in version 3.0.
Insert a single file into the GridFS.
173 174 175 176 177 178 |
# File 'lib/mongo/grid/fs_bucket.rb', line 173 def insert_one(file) @indexes ||= ensure_indexes! chunks_collection.insert_many(file.chunks) files_collection.insert_one(file.info) file.id end |
#open_download_stream(id, options = nil) {|The| ... } ⇒ Stream::Read
Opens a stream from which a file can be downloaded, specified by id.
247 248 249 250 251 252 253 254 255 256 257 258 |
# File 'lib/mongo/grid/fs_bucket.rb', line 247 def open_download_stream(id, = nil) = Utils.shallow_symbolize_keys( || {}) read_stream(id, **).tap do |stream| if block_given? begin yield stream ensure stream.close end end end end |
#open_download_stream_by_name(filename, opts = {}) {|The| ... } ⇒ Stream::Read
Opens a stream from which the application can read the contents of the stored file specified by filename and the revision in options.
Revision numbers are defined as follows: 0 = the original stored file 1 = the first revision 2 = the second revision etc…-2 = the second most recent revision -1 = the most recent revision
# @example Open a stream to download the original file.
fs.open_download_stream_by_name('some-file.txt', revision: 0)
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 |
# File 'lib/mongo/grid/fs_bucket.rb', line 312 def open_download_stream_by_name(filename, opts = {}, &block) revision = opts.fetch(:revision, -1) if revision < 0 skip = revision.abs - 1 sort = { 'uploadDate' => Mongo::Index::DESCENDING } else skip = revision sort = { 'uploadDate' => Mongo::Index::ASCENDING } end file_info_doc = files_collection.find({ filename: filename }, sort: sort, skip: skip, limit: -1).first unless file_info_doc raise Error::FileNotFound.new(filename, :filename) unless opts[:revision] raise Error::InvalidFileRevision.new(filename, opts[:revision]) end open_download_stream(file_info_doc[:_id], file_info_doc: file_info_doc, &block) end |
#open_upload_stream(filename, opts = {}) {|The| ... } ⇒ Stream::Write
Opens an upload stream to GridFS to which the contents of a file or blob can be written.
394 395 396 397 398 399 400 401 402 403 404 405 |
# File 'lib/mongo/grid/fs_bucket.rb', line 394 def open_upload_stream(filename, opts = {}) opts = Utils.shallow_symbolize_keys(opts) write_stream(filename, **opts).tap do |stream| if block_given? begin yield stream ensure stream.close end end end end |
#prefix ⇒ String
Get the prefix for the GridFS
188 189 190 |
# File 'lib/mongo/grid/fs_bucket.rb', line 188 def prefix @options[:fs_name] || @options[:bucket_name] || DEFAULT_ROOT end |
#read_preference ⇒ BSON::Document
This method always returns a BSON::Document instance, even though the FSBucket constructor specifies the type of :read as a Hash, not as a BSON::Document.
Get the read preference.
463 464 465 466 467 468 469 470 471 472 |
# File 'lib/mongo/grid/fs_bucket.rb', line 463 def read_preference @read_preference ||= begin pref = [:read] || database.read_preference if pref.is_a?(BSON::Document) pref else BSON::Document.new(pref) end end end |
#upload_from_stream(filename, io, opts = {}) ⇒ BSON::ObjectId
Uploads a user file to a GridFS bucket. Reads the contents of the user file from the source stream and uploads it as chunks in the chunks collection. After all the chunks have been uploaded, it creates a files collection document for the filename in the files collection.
435 436 437 438 439 440 441 442 443 444 445 446 447 448 |
# File 'lib/mongo/grid/fs_bucket.rb', line 435 def upload_from_stream(filename, io, opts = {}) open_upload_stream(filename, opts) do |stream| stream.write(io) # IOError and SystemCallError are for errors reading the io. # Error::SocketError and Error::SocketTimeoutError are for # writing to MongoDB. rescue IOError, SystemCallError, Error::SocketError, Error::SocketTimeoutError begin stream.abort rescue Error::OperationFailure end raise end.file_id end |
#write_concern ⇒ Mongo::WriteConcern
Get the write concern.
482 483 484 485 486 487 488 |
# File 'lib/mongo/grid/fs_bucket.rb', line 482 def write_concern @write_concern ||= if wco = @options[:write_concern] || @options[:write] WriteConcern.get(wco) else database.write_concern end end |