From c70b1d1b30fa05e1a888a2083655b6065f75914b Mon Sep 17 00:00:00 2001 From: VvanGemert Date: Thu, 5 Jan 2012 16:27:05 +0100 Subject: [PATCH 1/3] 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. --- lib/mongo/gridfs/grid_file_system.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/mongo/gridfs/grid_file_system.rb b/lib/mongo/gridfs/grid_file_system.rb index 3addfaa..41a18ca 100644 --- a/lib/mongo/gridfs/grid_file_system.rb +++ b/lib/mongo/gridfs/grid_file_system.rb @@ -97,7 +97,7 @@ module Mongo def open(filename, mode, opts={}) opts = opts.dup 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) return file unless block_given? result = nil @@ -105,9 +105,9 @@ module Mongo result = yield file ensure id = file.close - if del + if versions 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 From 417000da7bd00c83de9bae85b095811c87442be4 Mon Sep 17 00:00:00 2001 From: VvanGemert Date: Tue, 10 Jan 2012 17:37:25 +0100 Subject: [PATCH 2/3] Added back support for :delete_old option and wrote tests for :versions option on GridFilesystem. --- lib/mongo/gridfs/grid_file_system.rb | 7 ++++++- test/grid_file_system_test.rb | 12 ++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/lib/mongo/gridfs/grid_file_system.rb b/lib/mongo/gridfs/grid_file_system.rb index 41a18ca..2cd1ae4 100644 --- a/lib/mongo/gridfs/grid_file_system.rb +++ b/lib/mongo/gridfs/grid_file_system.rb @@ -97,7 +97,12 @@ module Mongo def open(filename, mode, opts={}) opts = opts.dup opts.merge!(default_grid_io_opts(filename)) - versions = opts.delete(:versions) && mode == 'w' + 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 diff --git a/test/grid_file_system_test.rb b/test/grid_file_system_test.rb index bac67f4..0cae6b0 100644 --- a/test/grid_file_system_test.rb +++ b/test/grid_file_system_test.rb @@ -165,6 +165,18 @@ class GridFileSystemTest < Test::Unit::TestCase assert_equal 0, @db['fs.chunks'].find({'files_id' => {'$in' => @ids}}).count end + should "delete all versions which exceed the number of versions to keep specified by the option :versions" do + @versions = 1 + rand(4-1) + @grid.open('sample', 'w', :versions => @versions) do |f| + f.write @new_data + end + @new_ids = @db['fs.files'].find({'filename' => 'sample'}).map {|file| file['_id']} + assert_equal @versions, @new_ids.length + id = @new_ids.first + assert !@ids.include?(id) + assert_equal @versions, @db['fs.files'].find({'filename' => 'sample'}).count + end + should "delete old versions on write with :delete_old is passed in" do @grid.open('sample', 'w', :delete_old => true) do |f| f.write @new_data From 585e6722b4090a52d309ac9c91f49df2cd969f5d Mon Sep 17 00:00:00 2001 From: VvanGemert Date: Tue, 10 Jan 2012 17:52:13 +0100 Subject: [PATCH 3/3] Documented the option :versions in description of method GridFileSystem.open. --- lib/mongo/gridfs/grid_file_system.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/mongo/gridfs/grid_file_system.rb b/lib/mongo/gridfs/grid_file_system.rb index 2cd1ae4..c15c1fe 100644 --- a/lib/mongo/gridfs/grid_file_system.rb +++ b/lib/mongo/gridfs/grid_file_system.rb @@ -71,6 +71,9 @@ module Mongo # GridFileSystem#delete. # @option opts [Boolean] :safe (false) When safe mode is enabled, the chunks sent to the server # will be validated using an md5 hash. If validation fails, an exception will be raised. + # @option opts [Integer] :versions (false) deletes all versions which exceed the number specified to + # retain ordered by uploadDate. This option only works in 'w' mode. Certain precautions must be taken when + # deleting GridFS files. See the notes under GridFileSystem#delete. # # @example #