/* 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.Collections.ObjectModel; using System.Linq; #if NET452 using System.Runtime.Serialization; #endif using MongoDB.Bson; namespace MongoDB.Driver { /// /// Represents the result of a bulk write operation. /// #if NET452 [Serializable] #endif public abstract class BulkWriteResult { // fields private readonly int _requestCount; //constructors /// /// Initializes a new instance of the class. /// /// The request count. protected BulkWriteResult(int requestCount) { _requestCount = requestCount; } // properties /// /// Gets the number of documents that were deleted. /// public abstract long DeletedCount { get; } /// /// Gets the number of documents that were inserted. /// public abstract long InsertedCount { get; } /// /// Gets a value indicating whether the bulk write operation was acknowledged. /// public abstract bool IsAcknowledged { get; } /// /// Gets a value indicating whether the modified count is available. /// /// /// The modified count is only available when all servers have been upgraded to 2.6 or above. /// public abstract bool IsModifiedCountAvailable { get; } /// /// Gets the number of documents that were matched. /// public abstract long MatchedCount { get; } /// /// Gets the number of documents that were actually modified during an update. /// public abstract long ModifiedCount { get; } /// /// Gets the request count. /// public int RequestCount { get { return _requestCount; } } /// /// Gets a list with information about each request that resulted in an upsert. /// public abstract IReadOnlyList Upserts { get; } } /// /// Represents the result of a bulk write operation. /// /// The type of the document. #if NET452 [Serializable] #endif public abstract class BulkWriteResult : BulkWriteResult { // private fields private readonly IReadOnlyList> _processedRequests; // constructors /// /// Initializes a new instance of the class. /// /// The request count. /// The processed requests. protected BulkWriteResult( int requestCount, IEnumerable> processedRequests) : base(requestCount) { _processedRequests = processedRequests.ToList(); } // public properties /// /// Gets the processed requests. /// public IReadOnlyList> ProcessedRequests { get { return _processedRequests; } } // internal static methods internal static BulkWriteResult 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(r => WriteModel.FromCore(r)), result.Upserts.Select(u => BulkWriteUpsert.FromCore(u))); } return new Unacknowledged( result.RequestCount, result.ProcessedRequests.Select(r => WriteModel.FromCore(r))); } internal static BulkWriteResult FromCore(Core.Operations.BulkWriteOperationResult result, IEnumerable> 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(u => BulkWriteUpsert.FromCore(u))); } return new Unacknowledged( result.RequestCount, requests); } // nested classes /// /// Result from an acknowledged write concern. /// #if NET452 [Serializable] #endif public class Acknowledged : BulkWriteResult { // private fields private readonly long _deletedCount; private readonly long _insertedCount; private readonly long _matchedCount; private readonly long? _modifiedCount; private readonly IReadOnlyList _upserts; // constructors /// /// Initializes a new instance of the class. /// /// The request count. /// The matched count. /// The deleted count. /// The inserted count. /// The modified count. /// The processed requests. /// The upserts. public Acknowledged( int requestCount, long matchedCount, long deletedCount, long insertedCount, long? modifiedCount, IEnumerable> processedRequests, IEnumerable upserts) : base(requestCount, processedRequests) { _matchedCount = matchedCount; _deletedCount = deletedCount; _insertedCount = insertedCount; _modifiedCount = modifiedCount; _upserts = upserts.ToList(); } // public properties /// public override long DeletedCount { get { return _deletedCount; } } /// public override long InsertedCount { get { return _insertedCount; } } /// public override bool IsAcknowledged { get { return true; } } /// public override bool IsModifiedCountAvailable { get { return _modifiedCount.HasValue; } } /// public override long MatchedCount { get { return _matchedCount; } } /// public override long ModifiedCount { get { if (!_modifiedCount.HasValue) { throw new NotSupportedException("ModifiedCount is not available."); } return _modifiedCount.Value; } } /// public override IReadOnlyList Upserts { get { return _upserts; } } } /// /// Result from an unacknowledged write concern. /// #if NET452 [Serializable] #endif public class Unacknowledged : BulkWriteResult { // constructors /// /// Initializes a new instance of the class. /// /// The request count. /// The processed requests. public Unacknowledged( int requestCount, IEnumerable> processedRequests) : base(requestCount, processedRequests) { } // public properties /// public override long DeletedCount { get { throw new NotSupportedException("Only acknowledged writes support the DeletedCount property."); } } /// public override long InsertedCount { get { throw new NotSupportedException("Only acknowledged writes support the InsertedCount property."); } } /// public override bool IsAcknowledged { get { return false; } } /// public override bool IsModifiedCountAvailable { get { throw new NotSupportedException("Only acknowledged writes support the IsModifiedCountAvailable property."); } } /// public override long MatchedCount { get { throw new NotSupportedException("Only acknowledged writes support the MatchedCount property."); } } /// public override long ModifiedCount { get { throw new NotSupportedException("Only acknowledged writes support the ModifiedCount property."); } } /// public override IReadOnlyList Upserts { get { throw new NotSupportedException("Only acknowledged writes support the Upserts property."); } } } } }