From fd43eca5143661b780adde9507bf87b110dac8dc Mon Sep 17 00:00:00 2001 From: Kyle Banker Date: Wed, 7 Apr 2010 15:53:38 -0400 Subject: [PATCH] unique indexes for GridFS chunks collections --- HISTORY | 26 ++++++++++++++++++-------- lib/mongo/gridfs/grid.rb | 2 +- lib/mongo/gridfs/grid_file_system.rb | 4 +++- test/grid_file_system_test.rb | 8 ++++++++ test/grid_test.rb | 4 ++++ 5 files changed, 34 insertions(+), 10 deletions(-) diff --git a/HISTORY b/HISTORY index acf0997..ebee923 100644 --- a/HISTORY +++ b/HISTORY @@ -3,22 +3,32 @@ * Collection#find_and_modify * Collection#stats * DB#stats -* Exception class adjustments: - * Mongo::InvalidObjectID moved to BSON::InvalidObjectID - * Mongo::InvalidDocument moved to BSON::InvalidDocument - * Mongo::InvalidStringEncoding moved to BSON::InvalidStringEncoding - * Mongo::InvalidName replaced by Mongo::InvalidNSName and BSON::InvalidKeyName -* BSON-related code extracted into two separate gems: bson and bson_ext (Chuck Remes). -* Extensions compile on Rubinius (Chuck Remes). * Query :fields options allows for values of 0 to exclude fields (houdini, railsjedi). -* BSON::Binary constructor can now take a string, which will be packed into an array. * GridFS * Option to delete old versions of GridFileSystem entries. * Filename is now optional for Grid#put. * Option to write arbitrary attributes to a file: @grid.put(@data, :favorite_phrase => "blimey!") + * Indexes created on the chunks collection are now unique. If you have an existing chunks collection, + you may want to remove * Removed the following deprecated items: * GridStore class +* BSON-related code extracted into two separate gems: bson and bson_ext (thx to Chuck Remes). + * mongo_ext no longer exists. + * BSON::Binary constructor can now take a string, which will be packed into an array. + * Exception class adjustments: + * Mongo::InvalidObjectID moved to BSON::InvalidObjectID + * Mongo::InvalidDocument moved to BSON::InvalidDocument + * Mongo::InvalidStringEncoding moved to BSON::InvalidStringEncoding + * Mongo::InvalidName replaced by Mongo::InvalidNSName and BSON::InvalidKeyName + * BSON types are now namespaced under the BSON module. These types include: + * Binary + * ObjectID + * Code + * DBRef + * MinKey and MaxKey + * Extensions compile on Rubinius (Chuck Remes). + 0.19.3 2010-4-5 * Minor fix for assert_valid_keys. diff --git a/lib/mongo/gridfs/grid.rb b/lib/mongo/gridfs/grid.rb index cc9750d..7db8e16 100644 --- a/lib/mongo/gridfs/grid.rb +++ b/lib/mongo/gridfs/grid.rb @@ -34,7 +34,7 @@ module Mongo @chunks = @db["#{fs_name}.chunks"] @fs_name = fs_name - @chunks.create_index([['files_id', Mongo::ASCENDING], ['n', Mongo::ASCENDING]]) + @chunks.create_index([['files_id', Mongo::ASCENDING], ['n', Mongo::ASCENDING]], :unique => true) end # Store a file in the file store. diff --git a/lib/mongo/gridfs/grid_file_system.rb b/lib/mongo/gridfs/grid_file_system.rb index 3d37e94..bdf358c 100644 --- a/lib/mongo/gridfs/grid_file_system.rb +++ b/lib/mongo/gridfs/grid_file_system.rb @@ -34,8 +34,10 @@ module Mongo @chunks = @db["#{fs_name}.chunks"] @fs_name = fs_name - @files.create_index([['filename', 1], ['uploadDate', -1]]) @default_query_opts = {:sort => [['filename', 1], ['uploadDate', -1]], :limit => 1} + + @files.create_index([['filename', 1], ['uploadDate', -1]]) + @chunks.create_index([['files_id', Mongo::ASCENDING], ['n', Mongo::ASCENDING]], :unique => true) end # Open a file for reading or writing. Note that the options for this method only apply diff --git a/test/grid_file_system_test.rb b/test/grid_file_system_test.rb index cd30f02..fb2c420 100644 --- a/test/grid_file_system_test.rb +++ b/test/grid_file_system_test.rb @@ -30,6 +30,14 @@ class GridFileSystemTest < Test::Unit::TestCase assert_equal data.length, @chunks_data.length end + should "have a unique index on chunks" do + assert @db['fs.chunks'].index_information['files_id_1_n_1']['unique'] + end + + should "have an index on filename" do + assert @db['fs.files'].index_information['filename_1_uploadDate_-1'] + end + should "return an empty string if length is zero" do data = @grid.open('sample.file', 'r') { |f| f.read(0) } assert_equal '', data diff --git a/test/grid_test.rb b/test/grid_test.rb index 63432e2..e3c7f2c 100644 --- a/test/grid_test.rb +++ b/test/grid_test.rb @@ -27,6 +27,10 @@ class GridTest < Test::Unit::TestCase assert_equal @data, data end + should "have a unique index on chunks" do + assert @chunks.index_information['files_id_1_n_1']['unique'] + end + should "store the filename" do file = @grid.get(@id) assert_equal 'sample', file.filename