Skip to content

Commit

Permalink
RUBY-351 Build GridFS indexes when a primary is present.
Browse files Browse the repository at this point in the history
  • Loading branch information
banker committed Nov 15, 2011
1 parent 24b9653 commit 93f228a
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
6 changes: 4 additions & 2 deletions lib/mongo/gridfs/grid.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,10 @@ def initialize(db, fs_name=DEFAULT_FS_NAME)
@chunks = @db["#{fs_name}.chunks"]
@fs_name = fs_name

# Ensure indexes only if not connected to slave.
unless db.connection.slave_ok?
# Create indexes only if we're connected to a primary node.
connection = @db.connection
if (connection.class == Connection && connection.read_primary?) ||
(connection.class == ReplSetConnection && connection.primary)
@chunks.create_index([['files_id', Mongo::ASCENDING], ['n', Mongo::ASCENDING]], :unique => true)
end
end
Expand Down
6 changes: 4 additions & 2 deletions lib/mongo/gridfs/grid_file_system.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,10 @@ def initialize(db, fs_name=Grid::DEFAULT_FS_NAME)

@default_query_opts = {:sort => [['filename', 1], ['uploadDate', -1]], :limit => 1}

# Ensure indexes only if not connected to slave.
unless db.connection.slave_ok?
# Create indexes only if we're connected to a primary node.
connection = @db.connection
if (connection.class == Connection && connection.read_primary?) ||
(connection.class == ReplSetConnection && connection.primary)
@files.create_index([['filename', 1], ['uploadDate', -1]])
@chunks.create_index([['files_id', Mongo::ASCENDING], ['n', Mongo::ASCENDING]], :unique => true)
end
Expand Down

0 comments on commit 93f228a

Please sign in to comment.