UpdateResult.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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.Linq;
  18. using System.Text;
  19. using MongoDB.Bson;
  20. namespace MongoDB.Driver
  21. {
  22. /// <summary>
  23. /// The result of an update operation.
  24. /// </summary>
  25. public abstract class UpdateResult
  26. {
  27. // static
  28. internal static UpdateResult FromCore(BulkWriteResult result)
  29. {
  30. if (result.IsAcknowledged)
  31. {
  32. var upsert = result.Upserts.Count == 1
  33. ? result.Upserts[0].Id
  34. : null;
  35. var modifiedCount = result.IsModifiedCountAvailable
  36. ? (long?)result.ModifiedCount
  37. : null;
  38. return new Acknowledged(result.MatchedCount, modifiedCount, upsert);
  39. }
  40. return Unacknowledged.Instance;
  41. }
  42. // properties
  43. /// <summary>
  44. /// Gets a value indicating whether the result is acknowledged.
  45. /// </summary>
  46. public abstract bool IsAcknowledged { get; }
  47. /// <summary>
  48. /// Gets a value indicating whether the modified count is available.
  49. /// </summary>
  50. /// <remarks>
  51. /// The modified count is only available when all servers have been upgraded to 2.6 or above.
  52. /// </remarks>
  53. public abstract bool IsModifiedCountAvailable { get; }
  54. /// <summary>
  55. /// Gets the matched count. If IsAcknowledged is false, this will throw an exception.
  56. /// </summary>
  57. public abstract long MatchedCount { get; }
  58. /// <summary>
  59. /// Gets the modified count. If IsAcknowledged is false, this will throw an exception.
  60. /// </summary>
  61. public abstract long ModifiedCount { get; }
  62. /// <summary>
  63. /// Gets the upserted id, if one exists. If IsAcknowledged is false, this will throw an exception.
  64. /// </summary>
  65. public abstract BsonValue UpsertedId { get; }
  66. // constructors
  67. /// <summary>
  68. /// Initializes a new instance of the <see cref="UpdateResult"/> class.
  69. /// </summary>
  70. protected UpdateResult()
  71. {
  72. }
  73. // nested classes
  74. /// <summary>
  75. /// The result of an acknowledged update operation.
  76. /// </summary>
  77. public class Acknowledged : UpdateResult
  78. {
  79. private readonly long _matchedCount;
  80. private readonly long? _modifiedCount;
  81. private readonly BsonValue _upsertedId;
  82. /// <summary>
  83. /// Initializes a new instance of the class.
  84. /// </summary>
  85. /// <param name="matchedCount">The matched count.</param>
  86. /// <param name="modifiedCount">The modified count.</param>
  87. /// <param name="upsertedId">The upserted id.</param>
  88. public Acknowledged(long matchedCount, long? modifiedCount, BsonValue upsertedId)
  89. {
  90. _matchedCount = matchedCount;
  91. _modifiedCount = modifiedCount;
  92. _upsertedId = upsertedId;
  93. }
  94. /// <inheritdoc/>
  95. public override bool IsAcknowledged
  96. {
  97. get { return true; }
  98. }
  99. /// <inheritdoc/>
  100. public override bool IsModifiedCountAvailable
  101. {
  102. get { return _modifiedCount.HasValue; }
  103. }
  104. /// <inheritdoc/>
  105. public override long MatchedCount
  106. {
  107. get { return _matchedCount; }
  108. }
  109. /// <inheritdoc/>
  110. public override long ModifiedCount
  111. {
  112. get
  113. {
  114. if (!_modifiedCount.HasValue)
  115. {
  116. throw new NotSupportedException("ModifiedCount is not available.");
  117. }
  118. return _modifiedCount.Value;
  119. }
  120. }
  121. /// <inheritdoc/>
  122. public override BsonValue UpsertedId
  123. {
  124. get { return _upsertedId; }
  125. }
  126. }
  127. /// <summary>
  128. /// The result of an acknowledged update operation.
  129. /// </summary>
  130. public class Unacknowledged : UpdateResult
  131. {
  132. private static Unacknowledged __instance = new Unacknowledged();
  133. /// <summary>
  134. /// Gets the instance.
  135. /// </summary>
  136. public static Unacknowledged Instance
  137. {
  138. get { return __instance; }
  139. }
  140. private Unacknowledged()
  141. {
  142. }
  143. /// <inheritdoc/>
  144. public override bool IsAcknowledged
  145. {
  146. get { return false; }
  147. }
  148. /// <inheritdoc/>
  149. public override bool IsModifiedCountAvailable
  150. {
  151. get { return false; }
  152. }
  153. /// <inheritdoc/>
  154. public override long MatchedCount
  155. {
  156. get { throw new NotSupportedException("Only acknowledged writes support the MatchedCount property."); }
  157. }
  158. /// <inheritdoc/>
  159. public override long ModifiedCount
  160. {
  161. get { throw new NotSupportedException("Only acknowledged writes support the ModifiedCount property."); }
  162. }
  163. /// <inheritdoc/>
  164. public override BsonValue UpsertedId
  165. {
  166. get { throw new NotSupportedException("Only acknowledged writes support the UpsertedId property."); }
  167. }
  168. }
  169. }
  170. }