Class: Mongo::GridFileSystem
- Includes:
- Mongo::GridExt::InstanceMethods
- Defined in:
- lib/mongo/gridfs/grid_file_system.rb
Overview
A file store built on the GridFS specification featuring an API and behavior similar to that of a traditional file system.
Instance Method Summary collapse
-
#delete(filename = nil) { ... } ⇒ Boolean
(also: #unlink)
Delete the file with the given filename.
-
#initialize(db, fs_name = Grid::DEFAULT_FS_NAME) ⇒ GridFileSystem
constructor
Initialize a new GridFileSystem instance, consisting of a MongoDB database and a filesystem prefix if not using the default.
-
#open(filename, mode, opts = {}) ⇒ Object
Open a file for reading or writing.
Methods included from Mongo::GridExt::InstanceMethods
Constructor Details
#initialize(db, fs_name = Grid::DEFAULT_FS_NAME) ⇒ GridFileSystem
Initialize a new GridFileSystem instance, consisting of a MongoDB database and a filesystem prefix if not using the default.
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/mongo/gridfs/grid_file_system.rb', line 32 def initialize(db, fs_name=Grid::DEFAULT_FS_NAME) raise MongoArgumentError, "db must be a Mongo::DB." unless db.is_a?(Mongo::DB) @db = db @files = @db["#{fs_name}.files"] @chunks = @db["#{fs_name}.chunks"] @fs_name = fs_name @default_query_opts = {:sort => [['filename', 1], ['uploadDate', -1]], :limit => 1} # Create indexes only if we're connected to a primary node. connection = @db.connection if (connection.class == MongoClient && connection.read_primary?) || (connection.class == MongoReplicaSetClient && connection.primary) @files.create_index([['filename', 1], ['uploadDate', -1]]) @chunks.create_index([['files_id', Mongo::ASCENDING], ['n', Mongo::ASCENDING]], :unique => true) end end |
Instance Method Details
#delete(filename = nil) { ... } ⇒ Boolean Also known as: unlink
Delete the file with the given filename. Note that this will delete all versions of the file.
Be careful with this. Deleting a GridFS file can result in read errors if another process is attempting to read a file while it’s being deleted. While the odds for this kind of race condition are small, it’s important to be aware of.
140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/mongo/gridfs/grid_file_system.rb', line 140 def delete(filename=nil) if block_given? files = yield else files = @files.find({'filename' => filename}, :fields => ['_id']) end files.each do |file| @files.remove({'_id' => file['_id']}) @chunks.remove({'files_id' => file['_id']}) end end |
#open(filename, mode, opts = {}) ⇒ Object
Open a file for reading or writing. Note that the options for this method only apply when opening in ‘w’ mode.
Note that arbitrary metadata attributes can be saved to the file by passing them is as options.
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/mongo/gridfs/grid_file_system.rb', line 103 def open(filename, mode, opts={}) opts = opts.dup opts.merge!(default_grid_io_opts(filename)) if mode == 'w' versions = opts.delete(:versions) if opts.delete(:delete_old) || (versions && versions < 1) versions = 1 end end file = GridIO.new(@files, @chunks, filename, mode, opts) return file unless block_given? result = nil begin result = yield file ensure id = file.close if versions self.delete do @files.find({'filename' => filename, '_id' => {'$ne' => id}}, :fields => ['_id'], :sort => ['uploadDate', -1], :skip => (versions - 1)) end end end result end |