Skip to content

Commit 7662b6e

Browse files
author
rstam
committed
CSHARP-924: Use nullable long for ModifiedCount internally.
1 parent f525dfe commit 7662b6e

File tree

7 files changed

+37
-47
lines changed

7 files changed

+37
-47
lines changed

MongoDB.Driver/AcknowledgedBulkWriteResult.cs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@ internal class AcknowledgedBulkWriteResult : BulkWriteResult
3030
private readonly long _deletedCount;
3131
private readonly long _insertedCount;
3232
private readonly long _matchedCount;
33-
private readonly long _modifiedCount;
34-
private readonly bool _isModifiedCountAvailable;
33+
private readonly long? _modifiedCount;
3534
private readonly ReadOnlyCollection<BulkWriteUpsert> _upserts;
3635

3736
// constructors
@@ -43,16 +42,14 @@ internal class AcknowledgedBulkWriteResult : BulkWriteResult
4342
/// <param name="deletedCount">The deleted count.</param>
4443
/// <param name="insertedCount">The inserted count.</param>
4544
/// <param name="modifiedCount">The modified count.</param>
46-
/// <param name="isModifiedCountAvailable">Whether the modified count is available.</param>
4745
/// <param name="processedRequests">The processed requests.</param>
4846
/// <param name="upserts">The upserts.</param>
4947
public AcknowledgedBulkWriteResult(
5048
int requestCount,
5149
long matchedCount,
5250
long deletedCount,
5351
long insertedCount,
54-
long modifiedCount,
55-
bool isModifiedCountAvailable,
52+
long? modifiedCount,
5653
IEnumerable<WriteRequest> processedRequests,
5754
IEnumerable<BulkWriteUpsert> upserts)
5855
: base(requestCount, processedRequests)
@@ -61,7 +58,6 @@ public AcknowledgedBulkWriteResult(
6158
_deletedCount = deletedCount;
6259
_insertedCount = insertedCount;
6360
_modifiedCount = modifiedCount;
64-
_isModifiedCountAvailable = isModifiedCountAvailable;
6561
_upserts = new ReadOnlyCollection<BulkWriteUpsert>(upserts.ToList());
6662
}
6763

@@ -91,12 +87,15 @@ public override long InsertedCount
9187
/// <summary>
9288
/// Gets a value indicating whether the modified count is available.
9389
/// </summary>
90+
/// <remarks>
91+
/// The modified count is only available when all servers have been upgraded to 2.6 or above.
92+
/// </remarks>
9493
/// <value>
9594
/// <c>true</c> if the modified count is available; otherwise, <c>false</c>.
9695
/// </value>
9796
public override bool IsModifiedCountAvailable
9897
{
99-
get { return _isModifiedCountAvailable; }
98+
get { return _modifiedCount.HasValue; }
10099
}
101100

102101
/// <summary>
@@ -120,11 +119,11 @@ public override long ModifiedCount
120119
{
121120
get
122121
{
123-
if (!_isModifiedCountAvailable)
122+
if (!_modifiedCount.HasValue)
124123
{
125124
throw new NotSupportedException("ModifiedCount is not available.");
126125
}
127-
return _modifiedCount;
126+
return _modifiedCount.Value;
128127
}
129128
}
130129

MongoDB.Driver/BulkWriteResult.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ protected BulkWriteResult(
7272
/// <summary>
7373
/// Gets a value indicating whether the modified count is available.
7474
/// </summary>
75+
/// <remarks>
76+
/// The modified count is only available when all servers have been upgraded to 2.6 or above.
77+
/// </remarks>
7578
/// <value>
7679
/// <c>true</c> if the modified count is available; otherwise, <c>false</c>.
7780
/// </value>

MongoDB.Driver/Operations/BulkWriteBatchResult.cs

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@ internal class BulkWriteBatchResult
3838
private readonly IndexMap _indexMap;
3939
private readonly long _insertedCount;
4040
private readonly long _matchedCount;
41-
private readonly long _modifiedCount;
42-
private readonly bool _isModifiedCountAvailable;
41+
private readonly long? _modifiedCount;
4342
private readonly Batch<WriteRequest> _nextBatch;
4443
private readonly ReadOnlyCollection<WriteRequest> _processedRequests;
4544
private readonly ReadOnlyCollection<WriteRequest> _unprocessedRequests;
@@ -55,8 +54,7 @@ public BulkWriteBatchResult(
5554
long matchedCount,
5655
long deletedCount,
5756
long insertedCount,
58-
long modifiedCount,
59-
bool isModifiedCountAvailable,
57+
long? modifiedCount,
6058
IEnumerable<BulkWriteUpsert> upserts,
6159
IEnumerable<BulkWriteError> writeErrors,
6260
WriteConcernError writeConcernError,
@@ -68,7 +66,6 @@ public BulkWriteBatchResult(
6866
_deletedCount = deletedCount;
6967
_insertedCount = insertedCount;
7068
_modifiedCount = modifiedCount;
71-
_isModifiedCountAvailable = isModifiedCountAvailable;
7269
_indexMap = indexMap;
7370
_nextBatch = nextBatch;
7471
_processedRequests = ToReadOnlyCollection(processedRequests);
@@ -109,17 +106,12 @@ public long InsertedCount
109106
get { return _insertedCount; }
110107
}
111108

112-
public bool IsModifiedCountAvailable
113-
{
114-
get { return _isModifiedCountAvailable; }
115-
}
116-
117109
public long MatchedCount
118110
{
119111
get { return _matchedCount; }
120112
}
121113

122-
public long ModifiedCount
114+
public long? ModifiedCount
123115
{
124116
get { return _modifiedCount; }
125117
}
@@ -163,19 +155,14 @@ public static BulkWriteBatchResult Create(
163155
var matchedCount = 0L;
164156
var deletedCount = 0L;
165157
var insertedCount = 0L;
166-
var modifiedCount = 0L;
167-
var isModifiedCountAvailable = false;
158+
long? modifiedCount = null;
168159
var upserts = Enumerable.Empty<BulkWriteUpsert>();
169160
if (result.IsAcknowledged)
170161
{
171162
matchedCount = result.MatchedCount;
172163
deletedCount = result.DeletedCount;
173164
insertedCount = result.InsertedCount;
174-
if (result.IsModifiedCountAvailable)
175-
{
176-
modifiedCount = result.ModifiedCount;
177-
isModifiedCountAvailable = true;
178-
}
165+
modifiedCount = result.IsModifiedCountAvailable ? (long?)result.ModifiedCount : null;
179166
upserts = result.Upserts;
180167
}
181168

@@ -197,7 +184,6 @@ public static BulkWriteBatchResult Create(
197184
deletedCount,
198185
insertedCount,
199186
modifiedCount,
200-
isModifiedCountAvailable,
201187
upserts,
202188
writeErrors,
203189
writeConcernError,
@@ -222,8 +208,7 @@ public static BulkWriteBatchResult Create(
222208
var matchedCount = 0L;
223209
var deletedCount = 0L;
224210
var insertedCount = 0L;
225-
var modifiedCount = 0L;
226-
var isModifiedCountAvailable = true;
211+
long? modifiedCount = 0L;
227212
var firstRequest = requests.FirstOrDefault();
228213
if (firstRequest != null)
229214
{
@@ -244,7 +229,7 @@ public static BulkWriteBatchResult Create(
244229
}
245230
else
246231
{
247-
isModifiedCountAvailable = false;
232+
modifiedCount = null;
248233
}
249234
break;
250235
}
@@ -258,7 +243,6 @@ public static BulkWriteBatchResult Create(
258243
deletedCount,
259244
insertedCount,
260245
modifiedCount,
261-
isModifiedCountAvailable,
262246
upserts,
263247
writeErrors,
264248
writeConcernError,
@@ -302,8 +286,7 @@ public static BulkWriteBatchResult Create(
302286
var matchedCount = 0L;
303287
var deletedCount = 0L;
304288
var insertedCount = 0L;
305-
var modifiedCount = 0L;
306-
var isModifiedCountAvailable = true;
289+
long? modifiedCount = 0L;
307290
switch (request.RequestType)
308291
{
309292
case WriteRequestType.Delete:
@@ -314,7 +297,7 @@ public static BulkWriteBatchResult Create(
314297
break;
315298
case WriteRequestType.Update:
316299
matchedCount = documentsAffected - upserts.Count();
317-
isModifiedCountAvailable = false; // getLasterror does not report this value
300+
modifiedCount = null; // getLasterror does not report this value
318301
break;
319302
}
320303

@@ -326,7 +309,6 @@ public static BulkWriteBatchResult Create(
326309
deletedCount,
327310
insertedCount,
328311
modifiedCount,
329-
isModifiedCountAvailable,
330312
upserts,
331313
writeErrors,
332314
writeConcernError,

MongoDB.Driver/Operations/BulkWriteBatchResultCombiner.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -60,19 +60,21 @@ private long CombineInsertedCount()
6060
return _batchResults.Sum(r => r.InsertedCount);
6161
}
6262

63-
private bool CombineIsModifiedCountAvailable()
64-
{
65-
return _batchResults.All(r => r.IsModifiedCountAvailable);
66-
}
67-
6863
private long CombineMatchedCount()
6964
{
7065
return _batchResults.Sum(r => r.MatchedCount);
7166
}
7267

73-
private long CombineModifiedCount()
68+
private long? CombineModifiedCount()
7469
{
75-
return _batchResults.Sum(r => r.ModifiedCount);
70+
if (_batchResults.All(r => r.ModifiedCount.HasValue))
71+
{
72+
return _batchResults.Sum(r => r.ModifiedCount.Value);
73+
}
74+
else
75+
{
76+
return null;
77+
}
7678
}
7779

7880
private IEnumerable<WriteRequest> CombineProcessedRequests()
@@ -125,8 +127,7 @@ private BulkWriteResult CreateBulkWriteResult()
125127
var matchedCount = CombineMatchedCount();
126128
var deletedCount = CombineDeletedCount();
127129
var insertedCount = CombineInsertedCount();
128-
var isModifiedCountAvailable = CombineIsModifiedCountAvailable();
129-
var modifiedCount = isModifiedCountAvailable ? CombineModifiedCount() : 0;
130+
var modifiedCount = CombineModifiedCount();
130131
var upserts = CombineUpserts();
131132

132133
return new AcknowledgedBulkWriteResult(
@@ -135,7 +136,6 @@ private BulkWriteResult CreateBulkWriteResult()
135136
deletedCount,
136137
insertedCount,
137138
modifiedCount,
138-
isModifiedCountAvailable,
139139
processedRequests,
140140
upserts);
141141
}

MongoDB.Driver/UnacknowledgedBulkWriteResult.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ public override long InsertedCount
6767
/// <summary>
6868
/// Gets a value indicating whether the modified count is available.
6969
/// </summary>
70+
/// <remarks>
71+
/// The modified count is only available when all servers have been upgraded to 2.6 or above.
72+
/// </remarks>
7073
/// <value>
7174
/// <c>true</c> if the modified count is available; otherwise, <c>false</c>.
7275
/// </value>

MongoDB.DriverUnitTests/MongoCollectionTests.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,10 +339,12 @@ public void TestBulkWriteCounts()
339339
Assert.AreEqual(1, result.InsertedCount);
340340
if (serverInstance.Supports(FeatureId.WriteCommands))
341341
{
342+
Assert.AreEqual(true, result.IsModifiedCountAvailable);
342343
Assert.AreEqual(1, result.ModifiedCount);
343344
}
344345
else
345346
{
347+
Assert.AreEqual(false, result.IsModifiedCountAvailable);
346348
Assert.Throws<NotSupportedException>(() => { var _ = result.ModifiedCount; });
347349
}
348350
Assert.AreEqual(3, result.RequestCount);
@@ -376,10 +378,12 @@ public void TestBulkWriteCountsWithUpsert()
376378
Assert.AreEqual(0, result.InsertedCount);
377379
if (serverInstance.Supports(FeatureId.WriteCommands))
378380
{
381+
Assert.AreEqual(true, result.IsModifiedCountAvailable);
379382
Assert.AreEqual(1, result.ModifiedCount);
380383
}
381384
else
382385
{
386+
Assert.AreEqual(false, result.IsModifiedCountAvailable);
383387
Assert.Throws<NotSupportedException>(() => { var _ = result.ModifiedCount; });
384388
}
385389
Assert.AreEqual(3, result.RequestCount);

MongoDB.DriverUnitTests/Operations/BulkWriteBatchResultCombinerTests.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,6 @@ private BulkWriteBatchResult CreateBatchResult(
157157
deletedCount ?? 0,
158158
insertedCount ?? 0,
159159
modifiedCount ?? 0,
160-
true, // isModifiedCountAvailable
161160
upserts ?? Enumerable.Empty<BulkWriteUpsert>(),
162161
writeErrors ?? Enumerable.Empty<BulkWriteError>(),
163162
writeConcernError,

0 commit comments

Comments
 (0)