Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for keeping a specified amount of documents in GridFS instead of removing all or none #80

Merged
merged 3 commits into from Feb 16, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 11 additions & 3 deletions lib/mongo/gridfs/grid_file_system.rb
Expand Up @@ -71,6 +71,9 @@ def initialize(db, fs_name=Grid::DEFAULT_FS_NAME)
# 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
#
Expand All @@ -97,17 +100,22 @@ def initialize(db, fs_name=Grid::DEFAULT_FS_NAME)
def open(filename, mode, opts={})
opts = opts.dup
opts.merge!(default_grid_io_opts(filename))
del = opts.delete(:delete_old) && 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
begin
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
Expand Down
12 changes: 12 additions & 0 deletions test/grid_file_system_test.rb
Expand Up @@ -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
Expand Down