-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
BulkWriteResult.cs
317 lines (282 loc) · 10.9 KB
/
BulkWriteResult.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/* Copyright 2010-present MongoDB Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.Collections.Generic;
using System.Linq;
namespace MongoDB.Driver
{
/// <summary>
/// Represents the result of a bulk write operation.
/// </summary>
[Serializable]
public abstract class BulkWriteResult
{
// fields
private readonly int _requestCount;
//constructors
/// <summary>
/// Initializes a new instance of the <see cref="BulkWriteResult"/> class.
/// </summary>
/// <param name="requestCount">The request count.</param>
protected BulkWriteResult(int requestCount)
{
_requestCount = requestCount;
}
// properties
/// <summary>
/// Gets the number of documents that were deleted.
/// </summary>
public abstract long DeletedCount { get; }
/// <summary>
/// Gets the number of documents that were inserted.
/// </summary>
public abstract long InsertedCount { get; }
/// <summary>
/// Gets a value indicating whether the bulk write operation was acknowledged.
/// </summary>
public abstract bool IsAcknowledged { get; }
/// <summary>
/// Gets a value indicating whether the modified count is available.
/// </summary>
/// <remarks>
/// The available modified count.
/// </remarks>
public abstract bool IsModifiedCountAvailable { get; }
/// <summary>
/// Gets the number of documents that were matched.
/// </summary>
public abstract long MatchedCount { get; }
/// <summary>
/// Gets the number of documents that were actually modified during an update.
/// </summary>
public abstract long ModifiedCount { get; }
/// <summary>
/// Gets the request count.
/// </summary>
public int RequestCount
{
get { return _requestCount; }
}
/// <summary>
/// Gets a list with information about each request that resulted in an upsert.
/// </summary>
public abstract IReadOnlyList<BulkWriteUpsert> Upserts { get; }
}
/// <summary>
/// Represents the result of a bulk write operation.
/// </summary>
/// <typeparam name="TDocument">The type of the document.</typeparam>
[Serializable]
public abstract class BulkWriteResult<TDocument> : BulkWriteResult
{
// private fields
private readonly IReadOnlyList<WriteModel<TDocument>> _processedRequests;
// constructors
/// <summary>
/// Initializes a new instance of the <see cref="BulkWriteResult" /> class.
/// </summary>
/// <param name="requestCount">The request count.</param>
/// <param name="processedRequests">The processed requests.</param>
protected BulkWriteResult(
int requestCount,
IReadOnlyList<WriteModel<TDocument>> processedRequests)
: base(requestCount)
{
_processedRequests = processedRequests;
}
// public properties
/// <summary>
/// Gets the processed requests.
/// </summary>
public IReadOnlyList<WriteModel<TDocument>> ProcessedRequests
{
get { return _processedRequests; }
}
// internal static methods
internal static BulkWriteResult<TDocument> FromCore(Core.Operations.BulkWriteOperationResult result)
{
if (result.IsAcknowledged)
{
return new Acknowledged(
result.RequestCount,
result.MatchedCount,
result.DeletedCount,
result.InsertedCount,
result.IsModifiedCountAvailable ? (long?)result.ModifiedCount : null,
result.ProcessedRequests.Select(WriteModel<TDocument>.FromCore).ToArray(),
result.Upserts.Select(BulkWriteUpsert.FromCore));
}
return new Unacknowledged(
result.RequestCount,
result.ProcessedRequests.Select(WriteModel<TDocument>.FromCore).ToArray());
}
internal static BulkWriteResult<TDocument> FromCore(Core.Operations.BulkWriteOperationResult result, IReadOnlyList<WriteModel<TDocument>> requests)
{
if (result.IsAcknowledged)
{
return new Acknowledged(
result.RequestCount,
result.MatchedCount,
result.DeletedCount,
result.InsertedCount,
result.IsModifiedCountAvailable ? (long?)result.ModifiedCount : null,
requests,
result.Upserts.Select(BulkWriteUpsert.FromCore));
}
return new Unacknowledged(
result.RequestCount,
requests);
}
// nested classes
/// <summary>
/// Result from an acknowledged write concern.
/// </summary>
[Serializable]
public class Acknowledged : BulkWriteResult<TDocument>
{
// private fields
private readonly long _deletedCount;
private readonly long _insertedCount;
private readonly long _matchedCount;
private readonly long? _modifiedCount;
private readonly IReadOnlyList<BulkWriteUpsert> _upserts;
// constructors
/// <summary>
/// Initializes a new instance of the <see cref="BulkWriteResult{TDocument}.Acknowledged" /> class.
/// </summary>
/// <param name="requestCount">The request count.</param>
/// <param name="matchedCount">The matched count.</param>
/// <param name="deletedCount">The deleted count.</param>
/// <param name="insertedCount">The inserted count.</param>
/// <param name="modifiedCount">The modified count.</param>
/// <param name="processedRequests">The processed requests.</param>
/// <param name="upserts">The upserts.</param>
public Acknowledged(
int requestCount,
long matchedCount,
long deletedCount,
long insertedCount,
long? modifiedCount,
IReadOnlyList<WriteModel<TDocument>> processedRequests,
IEnumerable<BulkWriteUpsert> upserts)
: base(requestCount, processedRequests)
{
_matchedCount = matchedCount;
_deletedCount = deletedCount;
_insertedCount = insertedCount;
_modifiedCount = modifiedCount;
_upserts = upserts.ToList();
}
// public properties
/// <inheritdoc/>
public override long DeletedCount
{
get { return _deletedCount; }
}
/// <inheritdoc/>
public override long InsertedCount
{
get { return _insertedCount; }
}
/// <inheritdoc/>
public override bool IsAcknowledged
{
get { return true; }
}
/// <inheritdoc/>
public override bool IsModifiedCountAvailable
{
get { return _modifiedCount.HasValue; }
}
/// <inheritdoc/>
public override long MatchedCount
{
get { return _matchedCount; }
}
/// <inheritdoc/>
public override long ModifiedCount
{
get
{
if (!_modifiedCount.HasValue)
{
throw new NotSupportedException("ModifiedCount is not available.");
}
return _modifiedCount.Value;
}
}
/// <inheritdoc/>
public override IReadOnlyList<BulkWriteUpsert> Upserts
{
get { return _upserts; }
}
}
/// <summary>
/// Result from an unacknowledged write concern.
/// </summary>
[Serializable]
public class Unacknowledged : BulkWriteResult<TDocument>
{
// constructors
/// <summary>
/// Initializes a new instance of the <see cref="BulkWriteResult{TDocument}.Unacknowledged"/> class.
/// </summary>
/// <param name="requestCount">The request count.</param>
/// <param name="processedRequests">The processed requests.</param>
public Unacknowledged(
int requestCount,
IReadOnlyList<WriteModel<TDocument>> processedRequests)
: base(requestCount, processedRequests)
{
}
// public properties
/// <inheritdoc/>
public override long DeletedCount
{
get { throw new NotSupportedException("Only acknowledged writes support the DeletedCount property."); }
}
/// <inheritdoc/>
public override long InsertedCount
{
get { throw new NotSupportedException("Only acknowledged writes support the InsertedCount property."); }
}
/// <inheritdoc/>
public override bool IsAcknowledged
{
get { return false; }
}
/// <inheritdoc/>
public override bool IsModifiedCountAvailable
{
get { throw new NotSupportedException("Only acknowledged writes support the IsModifiedCountAvailable property."); }
}
/// <inheritdoc/>
public override long MatchedCount
{
get { throw new NotSupportedException("Only acknowledged writes support the MatchedCount property."); }
}
/// <inheritdoc/>
public override long ModifiedCount
{
get { throw new NotSupportedException("Only acknowledged writes support the ModifiedCount property."); }
}
/// <inheritdoc/>
public override IReadOnlyList<BulkWriteUpsert> Upserts
{
get { throw new NotSupportedException("Only acknowledged writes support the Upserts property."); }
}
}
}
}