@@ -261,8 +261,6 @@ public void Download(Stream stream, MongoGridFSFileInfo fileInfo)
261261
262262 using ( _database . RequestStart ( _database . Settings . SlaveOk ) )
263263 {
264- EnsureIndexes ( ) ;
265-
266264 string md5Client = null ;
267265 using ( var md5Algorithm = _settings . VerifyMD5 ? MD5 . Create ( ) : null )
268266 {
@@ -325,7 +323,6 @@ public void Download(Stream stream, string remoteFileName)
325323 /// <param name="version">The version to download.</param>
326324 public void Download ( Stream stream , string remoteFileName , int version )
327325 {
328- EnsureIndexes ( ) ;
329326 Download ( stream , Query . EQ ( "filename" , remoteFileName ) , version ) ;
330327 }
331328
@@ -423,25 +420,10 @@ public void EnsureIndexes()
423420 /// <param name="maxFiles">Only create new indexes if there are fewer than this number of GridFS files).</param>
424421 public void EnsureIndexes ( int maxFiles )
425422 {
426- // don't try to create indexes on secondaries
427- var requestConnection = _database . Server . RequestConnection ;
428- if ( requestConnection != null )
429- {
430- // check whether the actual server instance we are using is a primary
431- var serverInstance = requestConnection . ServerInstance ;
432- if ( ! serverInstance . IsPrimary )
433- {
434- return ;
435- }
436- }
437- else
438- {
439- // check whether we are guaranteed to use a primary
440- if ( _database . Settings . SlaveOk )
441- {
442- return ;
443- }
444- }
423+ // EnsureIndexes should only be called for update operations
424+ // read-only operations shouldn't call EnsureIndexes because:
425+ // 1. we might be reading from a secondary
426+ // 2. we might be authenticating as a read-only uaser
445427
446428 // avoid round trip to count files if possible
447429 var indexCache = _database . Server . IndexCache ;
@@ -489,7 +471,6 @@ public bool Exists(IMongoQuery query)
489471 /// <returns>True if the GridFS file exists.</returns>
490472 public bool Exists ( string remoteFileName )
491473 {
492- EnsureIndexes ( ) ;
493474 return Exists ( Query . EQ ( "filename" , remoteFileName ) ) ;
494475 }
495476
@@ -521,7 +502,6 @@ public MongoCursor<MongoGridFSFileInfo> Find(IMongoQuery query)
521502 /// <returns>The matching GridFS files.</returns>
522503 public MongoCursor < MongoGridFSFileInfo > Find ( string remoteFileName )
523504 {
524- EnsureIndexes ( ) ;
525505 return Find ( Query . EQ ( "filename" , remoteFileName ) ) ;
526506 }
527507
@@ -594,7 +574,6 @@ public MongoGridFSFileInfo FindOne(string remoteFileName)
594574 /// <returns>The matching GridFS file.</returns>
595575 public MongoGridFSFileInfo FindOne ( string remoteFileName , int version )
596576 {
597- EnsureIndexes ( ) ;
598577 return FindOne ( Query . EQ ( "filename" , remoteFileName ) , version ) ;
599578 }
600579
0 commit comments