Skip to content

Commit 2af0be2

Browse files
author
rstam
committed
Fixed CSHARP-504. Support read-only GridFS operations when authenticated as a read-only user.
1 parent 038ec79 commit 2af0be2

File tree

3 files changed

+10
-27
lines changed

3 files changed

+10
-27
lines changed

Driver/GridFS/MongoGridFS.cs

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Driver/GridFS/MongoGridFSFileInfo.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,6 @@ public void Refresh()
470470
}
471471
else
472472
{
473-
_gridFS.EnsureIndexes();
474473
cursor = _gridFS.Files.Find(Query.EQ("filename", _name)).SetSortOrder(SortBy.Descending("uploadDate"));
475474
}
476475
var fileInfo = cursor.SetLimit(1).FirstOrDefault();

Driver/GridFS/MongoGridFSStream.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,6 @@ public MongoGridFSStream(MongoGridFSFileInfo fileInfo, FileMode mode, FileAccess
142142
message = string.Format("Invalid FileMode {0}.", mode);
143143
throw new ArgumentException(message, "mode");
144144
}
145-
_gridFS.EnsureIndexes();
146145
}
147146

148147
// public properties
@@ -554,12 +553,16 @@ private void LoadChunkNoData(long chunkIndex)
554553

555554
private void OpenAppend()
556555
{
556+
_gridFS.EnsureIndexes();
557+
557558
_length = _fileInfo.Length;
558559
_position = _fileInfo.Length;
559560
}
560561

561562
private void OpenCreate()
562563
{
564+
_gridFS.EnsureIndexes();
565+
563566
_fileIsDirty = true;
564567
if (_fileInfo.Id == null)
565568
{
@@ -591,6 +594,8 @@ private void OpenExisting()
591594

592595
private void OpenTruncate()
593596
{
597+
_gridFS.EnsureIndexes();
598+
594599
_fileIsDirty = true;
595600
// existing chunks will be overwritten as needed and extra chunks will be removed on Close
596601
_length = 0;

0 commit comments

Comments
 (0)