Skip to content

Commit f525dfe

Browse files
author
rstam
committed
CSHARP-924: Add IsModifiedCountAvailable and change ModifiedCount to throw when modified count is not available.
1 parent e69dd07 commit f525dfe

File tree

8 files changed

+251
-97
lines changed

8 files changed

+251
-97
lines changed

MongoDB.Driver/AcknowledgedBulkWriteResult.cs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ internal class AcknowledgedBulkWriteResult : BulkWriteResult
3131
private readonly long _insertedCount;
3232
private readonly long _matchedCount;
3333
private readonly long _modifiedCount;
34+
private readonly bool _isModifiedCountAvailable;
3435
private readonly ReadOnlyCollection<BulkWriteUpsert> _upserts;
3536

3637
// constructors
@@ -42,6 +43,7 @@ internal class AcknowledgedBulkWriteResult : BulkWriteResult
4243
/// <param name="deletedCount">The deleted count.</param>
4344
/// <param name="insertedCount">The inserted count.</param>
4445
/// <param name="modifiedCount">The modified count.</param>
46+
/// <param name="isModifiedCountAvailable">Whether the modified count is available.</param>
4547
/// <param name="processedRequests">The processed requests.</param>
4648
/// <param name="upserts">The upserts.</param>
4749
public AcknowledgedBulkWriteResult(
@@ -50,6 +52,7 @@ public AcknowledgedBulkWriteResult(
5052
long deletedCount,
5153
long insertedCount,
5254
long modifiedCount,
55+
bool isModifiedCountAvailable,
5356
IEnumerable<WriteRequest> processedRequests,
5457
IEnumerable<BulkWriteUpsert> upserts)
5558
: base(requestCount, processedRequests)
@@ -58,6 +61,7 @@ public AcknowledgedBulkWriteResult(
5861
_deletedCount = deletedCount;
5962
_insertedCount = insertedCount;
6063
_modifiedCount = modifiedCount;
64+
_isModifiedCountAvailable = isModifiedCountAvailable;
6165
_upserts = new ReadOnlyCollection<BulkWriteUpsert>(upserts.ToList());
6266
}
6367

@@ -84,6 +88,17 @@ public override long InsertedCount
8488
get { return _insertedCount; }
8589
}
8690

91+
/// <summary>
92+
/// Gets a value indicating whether the modified count is available.
93+
/// </summary>
94+
/// <value>
95+
/// <c>true</c> if the modified count is available; otherwise, <c>false</c>.
96+
/// </value>
97+
public override bool IsModifiedCountAvailable
98+
{
99+
get { return _isModifiedCountAvailable; }
100+
}
101+
87102
/// <summary>
88103
/// Gets the number of documents that were matched.
89104
/// </summary>
@@ -97,14 +112,20 @@ public override long MatchedCount
97112

98113
/// <summary>
99114
/// Gets the number of documents that were actually modified during an update.
100-
/// When connected to server versions before 2.6 ModifiedCount will equal MatchedCount.
101115
/// </summary>
102116
/// <value>
103117
/// The number of document that were actually modified during an update.
104118
/// </value>
105119
public override long ModifiedCount
106120
{
107-
get { return _modifiedCount; }
121+
get
122+
{
123+
if (!_isModifiedCountAvailable)
124+
{
125+
throw new NotSupportedException("ModifiedCount is not available.");
126+
}
127+
return _modifiedCount;
128+
}
108129
}
109130

110131
/// <summary>

MongoDB.Driver/BulkWriteResult.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,14 @@ protected BulkWriteResult(
6969
/// </value>
7070
public abstract bool IsAcknowledged { get; }
7171

72+
/// <summary>
73+
/// Gets a value indicating whether the modified count is available.
74+
/// </summary>
75+
/// <value>
76+
/// <c>true</c> if the modified count is available; otherwise, <c>false</c>.
77+
/// </value>
78+
public abstract bool IsModifiedCountAvailable { get; }
79+
7280
/// <summary>
7381
/// Gets the number of documents that were matched.
7482
/// </summary>
@@ -79,7 +87,6 @@ protected BulkWriteResult(
7987

8088
/// <summary>
8189
/// Gets the number of documents that were actually modified during an update.
82-
/// When connected to server versions before 2.6 ModifiedCount will equal MatchedCount.
8390
/// </summary>
8491
/// <value>
8592
/// The number of document that were actually modified during an update.

MongoDB.Driver/Operations/BulkWriteBatchResult.cs

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ internal class BulkWriteBatchResult
3939
private readonly long _insertedCount;
4040
private readonly long _matchedCount;
4141
private readonly long _modifiedCount;
42+
private readonly bool _isModifiedCountAvailable;
4243
private readonly Batch<WriteRequest> _nextBatch;
4344
private readonly ReadOnlyCollection<WriteRequest> _processedRequests;
4445
private readonly ReadOnlyCollection<WriteRequest> _unprocessedRequests;
@@ -55,6 +56,7 @@ public BulkWriteBatchResult(
5556
long deletedCount,
5657
long insertedCount,
5758
long modifiedCount,
59+
bool isModifiedCountAvailable,
5860
IEnumerable<BulkWriteUpsert> upserts,
5961
IEnumerable<BulkWriteError> writeErrors,
6062
WriteConcernError writeConcernError,
@@ -66,6 +68,7 @@ public BulkWriteBatchResult(
6668
_deletedCount = deletedCount;
6769
_insertedCount = insertedCount;
6870
_modifiedCount = modifiedCount;
71+
_isModifiedCountAvailable = isModifiedCountAvailable;
6972
_indexMap = indexMap;
7073
_nextBatch = nextBatch;
7174
_processedRequests = ToReadOnlyCollection(processedRequests);
@@ -106,6 +109,11 @@ public long InsertedCount
106109
get { return _insertedCount; }
107110
}
108111

112+
public bool IsModifiedCountAvailable
113+
{
114+
get { return _isModifiedCountAvailable; }
115+
}
116+
109117
public long MatchedCount
110118
{
111119
get { return _matchedCount; }
@@ -156,13 +164,18 @@ public static BulkWriteBatchResult Create(
156164
var deletedCount = 0L;
157165
var insertedCount = 0L;
158166
var modifiedCount = 0L;
167+
var isModifiedCountAvailable = false;
159168
var upserts = Enumerable.Empty<BulkWriteUpsert>();
160169
if (result.IsAcknowledged)
161170
{
162171
matchedCount = result.MatchedCount;
163172
deletedCount = result.DeletedCount;
164173
insertedCount = result.InsertedCount;
165-
modifiedCount = result.ModifiedCount;
174+
if (result.IsModifiedCountAvailable)
175+
{
176+
modifiedCount = result.ModifiedCount;
177+
isModifiedCountAvailable = true;
178+
}
166179
upserts = result.Upserts;
167180
}
168181

@@ -184,6 +197,7 @@ public static BulkWriteBatchResult Create(
184197
deletedCount,
185198
insertedCount,
186199
modifiedCount,
200+
isModifiedCountAvailable,
187201
upserts,
188202
writeErrors,
189203
writeConcernError,
@@ -209,6 +223,7 @@ public static BulkWriteBatchResult Create(
209223
var deletedCount = 0L;
210224
var insertedCount = 0L;
211225
var modifiedCount = 0L;
226+
var isModifiedCountAvailable = true;
212227
var firstRequest = requests.FirstOrDefault();
213228
if (firstRequest != null)
214229
{
@@ -222,7 +237,15 @@ public static BulkWriteBatchResult Create(
222237
break;
223238
case WriteRequestType.Update:
224239
matchedCount = n - upserts.Count();
225-
modifiedCount = writeCommandResponse.GetValue("nModified", 0).ToInt64();
240+
BsonValue nModified;
241+
if (writeCommandResponse.TryGetValue("nModified", out nModified))
242+
{
243+
modifiedCount = nModified.ToInt64();
244+
}
245+
else
246+
{
247+
isModifiedCountAvailable = false;
248+
}
226249
break;
227250
}
228251
}
@@ -235,6 +258,7 @@ public static BulkWriteBatchResult Create(
235258
deletedCount,
236259
insertedCount,
237260
modifiedCount,
261+
isModifiedCountAvailable,
238262
upserts,
239263
writeErrors,
240264
writeConcernError,
@@ -279,6 +303,7 @@ public static BulkWriteBatchResult Create(
279303
var deletedCount = 0L;
280304
var insertedCount = 0L;
281305
var modifiedCount = 0L;
306+
var isModifiedCountAvailable = true;
282307
switch (request.RequestType)
283308
{
284309
case WriteRequestType.Delete:
@@ -289,7 +314,7 @@ public static BulkWriteBatchResult Create(
289314
break;
290315
case WriteRequestType.Update:
291316
matchedCount = documentsAffected - upserts.Count();
292-
modifiedCount = 0; // getLasterror does not report this value
317+
isModifiedCountAvailable = false; // getLasterror does not report this value
293318
break;
294319
}
295320

@@ -301,6 +326,7 @@ public static BulkWriteBatchResult Create(
301326
deletedCount,
302327
insertedCount,
303328
modifiedCount,
329+
isModifiedCountAvailable,
304330
upserts,
305331
writeErrors,
306332
writeConcernError,

MongoDB.Driver/Operations/BulkWriteBatchResultCombiner.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ 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+
6368
private long CombineMatchedCount()
6469
{
6570
return _batchResults.Sum(r => r.MatchedCount);
@@ -120,7 +125,8 @@ private BulkWriteResult CreateBulkWriteResult()
120125
var matchedCount = CombineMatchedCount();
121126
var deletedCount = CombineDeletedCount();
122127
var insertedCount = CombineInsertedCount();
123-
var modifiedCount = CombineModifiedCount();
128+
var isModifiedCountAvailable = CombineIsModifiedCountAvailable();
129+
var modifiedCount = isModifiedCountAvailable ? CombineModifiedCount() : 0;
124130
var upserts = CombineUpserts();
125131

126132
return new AcknowledgedBulkWriteResult(
@@ -129,6 +135,7 @@ private BulkWriteResult CreateBulkWriteResult()
129135
deletedCount,
130136
insertedCount,
131137
modifiedCount,
138+
isModifiedCountAvailable,
132139
processedRequests,
133140
upserts);
134141
}

MongoDB.Driver/UnacknowledgedBulkWriteResult.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,18 @@ public override long InsertedCount
6464
get { throw new NotSupportedException("Only acknowledged writes support the InsertedCount property."); }
6565
}
6666

67+
/// <summary>
68+
/// Gets a value indicating whether the modified count is available.
69+
/// </summary>
70+
/// <value>
71+
/// <c>true</c> if the modified count is available; otherwise, <c>false</c>.
72+
/// </value>
73+
/// <exception cref="System.NotImplementedException"></exception>
74+
public override bool IsModifiedCountAvailable
75+
{
76+
get { throw new NotSupportedException("Only acknowledged writes support the IsModifiedCountAvailable property."); }
77+
}
78+
6779
/// <summary>
6880
/// Gets the number of documents that were matched.
6981
/// </summary>
@@ -79,7 +91,6 @@ public override long MatchedCount
7991

8092
/// <summary>
8193
/// Gets the number of documents that were actually modified during an update.
82-
/// When connected to server versions before 2.6 ModifiedCount will equal MatchedCount.
8394
/// </summary>
8495
/// <value>
8596
/// The number of document that were actually modified during an update.

MongoDB.DriverUnitTests/MongoCollectionTests.cs

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -335,15 +335,16 @@ public void TestBulkWriteCounts()
335335
}
336336
});
337337

338-
var expectedModifiedCount = 1;
339-
if (!serverInstance.Supports(FeatureId.WriteCommands))
340-
{
341-
expectedModifiedCount = 0;
342-
}
343-
344338
Assert.AreEqual(1, result.DeletedCount);
345339
Assert.AreEqual(1, result.InsertedCount);
346-
Assert.AreEqual(expectedModifiedCount, result.ModifiedCount);
340+
if (serverInstance.Supports(FeatureId.WriteCommands))
341+
{
342+
Assert.AreEqual(1, result.ModifiedCount);
343+
}
344+
else
345+
{
346+
Assert.Throws<NotSupportedException>(() => { var _ = result.ModifiedCount; });
347+
}
347348
Assert.AreEqual(3, result.RequestCount);
348349
Assert.AreEqual(1, result.MatchedCount);
349350
}
@@ -371,15 +372,16 @@ public void TestBulkWriteCountsWithUpsert()
371372
}
372373
});
373374

374-
var expectedModifiedCount = 1;
375-
if (!serverInstance.Supports(FeatureId.WriteCommands))
376-
{
377-
expectedModifiedCount = 0;
378-
}
379-
380375
Assert.AreEqual(0, result.DeletedCount);
381376
Assert.AreEqual(0, result.InsertedCount);
382-
Assert.AreEqual(expectedModifiedCount, result.ModifiedCount);
377+
if (serverInstance.Supports(FeatureId.WriteCommands))
378+
{
379+
Assert.AreEqual(1, result.ModifiedCount);
380+
}
381+
else
382+
{
383+
Assert.Throws<NotSupportedException>(() => { var _ = result.ModifiedCount; });
384+
}
383385
Assert.AreEqual(3, result.RequestCount);
384386
Assert.AreEqual(2, result.MatchedCount);
385387
Assert.AreEqual(1, result.Upserts.Count);

MongoDB.DriverUnitTests/Operations/BulkWriteBatchResultCombinerTests.cs

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

0 commit comments

Comments
 (0)