Added support for keeping a limited amount of documents in GridFS and removed the delete_old option.

The delete_old removed all the old files while having a versioning system you might want to keep a limited set of files. This small little patch does that. You can specify a number of versions to keep by using the new versions option.
This commit is contained in:
VvanGemert 2012-01-05 16:27:05 +01:00
parent 63708a5f16
commit c70b1d1b30

View File

@ -97,7 +97,7 @@ module Mongo
def open(filename, mode, opts={}) def open(filename, mode, opts={})
opts = opts.dup opts = opts.dup
opts.merge!(default_grid_io_opts(filename)) opts.merge!(default_grid_io_opts(filename))
del = opts.delete(:delete_old) && mode == 'w' versions = opts.delete(:versions) && mode == 'w'
file = GridIO.new(@files, @chunks, filename, mode, opts) file = GridIO.new(@files, @chunks, filename, mode, opts)
return file unless block_given? return file unless block_given?
result = nil result = nil
@ -105,9 +105,9 @@ module Mongo
result = yield file result = yield file
ensure ensure
id = file.close id = file.close
if del if versions
self.delete do self.delete do
@files.find({'filename' => filename, '_id' => {'$ne' => id}}, :fields => ['_id']) @files.find({'filename' => filename, '_id' => {'$ne' => id}}, :fields => ['_id'], :sort => ['uploadDate', -1], :skip => (versions -1))
end end
end end
end end