Skip to content

Commit

Permalink
RUBY-193 don't create gridfs indexes when slave_ok
Browse files Browse the repository at this point in the history
  • Loading branch information
banker committed Oct 21, 2010
1 parent 048e9e6 commit afe8fe3
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 3 deletions.
5 changes: 4 additions & 1 deletion lib/mongo/gridfs/grid.rb
Expand Up @@ -38,7 +38,10 @@ 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]], :unique => true)
# Ensure indexes only if not connected to slave.
unless db.connection.slave_ok?
@chunks.create_index([['files_id', Mongo::ASCENDING], ['n', Mongo::ASCENDING]], :unique => true)
end
end

# Store a file in the file store. This method is designed only for writing new files;
Expand Down
7 changes: 5 additions & 2 deletions lib/mongo/gridfs/grid_file_system.rb
Expand Up @@ -39,8 +39,11 @@ def initialize(db, fs_name=Grid::DEFAULT_FS_NAME)

@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)
# Ensure indexes only if not connected to slave.
unless db.connection.slave_ok?
@files.create_index([['filename', 1], ['uploadDate', -1]])
@chunks.create_index([['files_id', Mongo::ASCENDING], ['n', Mongo::ASCENDING]], :unique => true)
end
end

# Open a file for reading or writing. Note that the options for this method only apply
Expand Down
47 changes: 47 additions & 0 deletions test/unit/grid_test.rb
@@ -0,0 +1,47 @@
require './test/test_helper'

class GridTest < Test::Unit::TestCase

context "GridFS: " do
setup do
@conn = stub()
@db = DB.new("testing", @conn)
@files = mock()
@chunks = mock()

@db.expects(:[]).with('fs.files').returns(@files)
@db.expects(:[]).with('fs.chunks').returns(@chunks)
end

context "Grid classe with standard connections" do
setup do
@conn.expects(:slave_ok?).returns(false)
end

should "create indexes for Grid" do
@chunks.expects(:create_index)
Grid.new(@db)
end

should "create indexes for GridFileSystem" do
@files.expects(:create_index)
@chunks.expects(:create_index)
GridFileSystem.new(@db)
end
end

context "Grid classes with slave connection" do
setup do
@conn.expects(:slave_ok?).returns(true)
end

should "not create indexes for Grid" do
Grid.new(@db)
end

should "not create indexes for GridFileSystem" do
GridFileSystem.new(@db)
end
end
end
end

0 comments on commit afe8fe3

Please sign in to comment.