BulkWriteResult.cs 11 KB

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