Skip to content

Commit

Permalink
unique indexes for GridFS chunks collections
Browse files Browse the repository at this point in the history
  • Loading branch information
banker committed Apr 7, 2010
1 parent cd32dab commit fd43eca
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 10 deletions.
26 changes: 18 additions & 8 deletions HISTORY
Expand Up @@ -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.

Expand Down
2 changes: 1 addition & 1 deletion lib/mongo/gridfs/grid.rb
Expand Up @@ -34,7 +34,7 @@ def initialize(db, fs_name=DEFAULT_FS_NAME)
@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.
Expand Down
4 changes: 3 additions & 1 deletion lib/mongo/gridfs/grid_file_system.rb
Expand Up @@ -34,8 +34,10 @@ def initialize(db, fs_name=Grid::DEFAULT_FS_NAME)
@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
Expand Down
8 changes: 8 additions & 0 deletions test/grid_file_system_test.rb
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions test/grid_test.rb
Expand Up @@ -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
Expand Down

0 comments on commit fd43eca

Please sign in to comment.