Skip to content

Commit 5328807

Browse files
committed
minor: code review cleanup
1 parent db69b64 commit 5328807

File tree

2 files changed

+29
-25
lines changed

2 files changed

+29
-25
lines changed

Driver/Core/MongoCursorEnumerator.cs

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -35,20 +35,21 @@ namespace MongoDB.Driver
3535
public class MongoCursorEnumerator<TDocument> : IEnumerator<TDocument>
3636
{
3737
// private fields
38+
private readonly MongoCursor<TDocument> _cursor;
39+
private readonly QueryFlags _queryFlags;
40+
private readonly ReadPreference _readPreference;
41+
3842
private bool _disposed = false;
3943
private bool _started = false;
4044
private bool _done = false;
41-
private MongoCursor<TDocument> _cursor;
4245
private MongoServerInstance _serverInstance; // set when first request is sent to server instance
4346
private int _count;
4447
private int _positiveLimit;
4548
private MongoReplyMessage<TDocument> _reply;
4649
private int _replyIndex;
4750
private ResponseFlags _responseFlags;
4851
private long _openCursorId;
49-
private ReadPreference _readPreference;
50-
private QueryFlags _queryFlags;
51-
52+
5253
// constructors
5354
/// <summary>
5455
/// Initializes a new instance of the MongoCursorEnumerator class.
@@ -58,6 +59,22 @@ public MongoCursorEnumerator(MongoCursor<TDocument> cursor)
5859
{
5960
_cursor = cursor;
6061
_positiveLimit = cursor.Limit >= 0 ? cursor.Limit : -cursor.Limit;
62+
_readPreference = _cursor.ReadPreference;
63+
_queryFlags = _cursor.Flags;
64+
65+
if (_readPreference.ReadPreferenceMode != ReadPreferenceMode.Primary && _cursor.Collection.Name == "$cmd")
66+
{
67+
var queryDocument = _cursor.Query.ToBsonDocument();
68+
var isSecondaryOk = MongoDefaults.CanCommandBeSentToSecondary(queryDocument);
69+
if (!isSecondaryOk)
70+
{
71+
// if the command can't be sent to a secondary, then we use primary here
72+
// regardless of the user's choice.
73+
_readPreference = ReadPreference.Primary;
74+
// remove the slaveOk bit from the flags
75+
_queryFlags &= ~QueryFlags.SlaveOk;
76+
}
77+
}
6178
}
6279

6380
// public properties
@@ -208,22 +225,6 @@ private MongoConnection AcquireConnection()
208225
{
209226
if (_serverInstance == null)
210227
{
211-
_readPreference = _cursor.ReadPreference;
212-
_queryFlags = _cursor.Flags;
213-
if (_readPreference.ReadPreferenceMode != ReadPreferenceMode.Primary && _cursor.Collection.Name == "$cmd")
214-
{
215-
var queryDocument = _cursor.Query.ToBsonDocument();
216-
var isSecondaryOk = MongoDefaults.CanCommandBeSentToSecondary(queryDocument);
217-
if (!isSecondaryOk)
218-
{
219-
// if the command can't be sent to a secondary, then we use primary here
220-
// regardless of the user's choice.
221-
_readPreference = ReadPreference.Primary;
222-
// remove the slaveOk bit from the flags
223-
_queryFlags &= ~QueryFlags.SlaveOk;
224-
}
225-
}
226-
227228
// first time we need a connection let Server.AcquireConnection pick the server instance
228229
var connection = _cursor.Server.AcquireConnection(_cursor.Database, _readPreference);
229230
_serverInstance = connection.ServerInstance;
@@ -349,8 +350,8 @@ private void KillCursor()
349350
private IMongoQuery WrapQuery()
350351
{
351352
BsonDocument formattedReadPreference = null;
352-
if (_serverInstance.InstanceType == MongoServerInstanceType.ShardRouter
353-
&& _readPreference.ReadPreferenceMode != ReadPreferenceMode.Primary)
353+
if (_serverInstance.InstanceType == MongoServerInstanceType.ShardRouter &&
354+
_readPreference.ReadPreferenceMode != ReadPreferenceMode.Primary)
354355
{
355356
BsonArray tagSetsArray = null;
356357
if (_readPreference.TagSets != null)

Driver/MongoDefaults.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -272,10 +272,13 @@ private static bool CanCommandBeSendToSecondaryDefault(BsonDocument document)
272272
return true;
273273
}
274274

275-
BsonValue outValue;
276-
if (document.TryGetValue("out", out outValue) && outValue.IsBsonDocument)
275+
if (commandName.Equals("mapreduce", StringComparison.InvariantCultureIgnoreCase))
277276
{
278-
return outValue.AsBsonDocument.Contains("inline");
277+
BsonValue outValue;
278+
if (document.TryGetValue("out", out outValue) && outValue.IsBsonDocument)
279+
{
280+
return outValue.AsBsonDocument.Contains("inline");
281+
}
279282
}
280283

281284
return false;

0 commit comments

Comments
 (0)