// constructors
 public BulkWriteBatchResult(
     int batchCount,
     IReadOnlyList <WriteRequest> processedRequests,
     IReadOnlyList <WriteRequest> unprocessedRequests,
     long matchedCount,
     long deletedCount,
     long insertedCount,
     long?modifiedCount,
     IReadOnlyList <BulkWriteOperationUpsert> upserts,
     IReadOnlyList <BulkWriteOperationError> writeErrors,
     BulkWriteConcernError writeConcernError,
     IndexMap indexMap)
 {
     _batchCount          = batchCount;
     _matchedCount        = matchedCount;
     _deletedCount        = deletedCount;
     _insertedCount       = insertedCount;
     _modifiedCount       = modifiedCount;
     _indexMap            = indexMap;
     _processedRequests   = processedRequests;
     _writeErrors         = writeErrors;
     _unprocessedRequests = unprocessedRequests;
     _upserts             = upserts;
     _writeConcernError   = writeConcernError;
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="MongoBulkWriteOperationException" /> class.
 /// </summary>
 /// <param name="info">The SerializationInfo.</param>
 /// <param name="context">The StreamingContext.</param>
 public MongoBulkWriteOperationException(SerializationInfo info, StreamingContext context)
     : base(info, context)
 {
     _result = (BulkWriteOperationResult)info.GetValue("_result", typeof(BulkWriteOperationResult));
     _unprocessedRequests = (IReadOnlyList<WriteRequest>)info.GetValue("_unprocessedRequests", typeof(IReadOnlyList<WriteRequest>));
     _writeConcernError = (BulkWriteConcernError)info.GetValue("_writeConcernError", typeof(BulkWriteConcernError));
     _writeErrors = (IReadOnlyList<BulkWriteOperationError>)info.GetValue("_writeErrors", typeof(IReadOnlyList<BulkWriteOperationError>));
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="MongoBulkWriteOperationException" /> class.
 /// </summary>
 /// <param name="info">The SerializationInfo.</param>
 /// <param name="context">The StreamingContext.</param>
 public MongoBulkWriteOperationException(SerializationInfo info, StreamingContext context)
     : base(info, context)
 {
     _result = (BulkWriteOperationResult)info.GetValue("_result", typeof(BulkWriteOperationResult));
     _unprocessedRequests = (IReadOnlyList <WriteRequest>)info.GetValue("_unprocessedRequests", typeof(IReadOnlyList <WriteRequest>));
     _writeConcernError   = (BulkWriteConcernError)info.GetValue("_writeConcernError", typeof(BulkWriteConcernError));
     _writeErrors         = (IReadOnlyList <BulkWriteOperationError>)info.GetValue("_writeErrors", typeof(IReadOnlyList <BulkWriteOperationError>));
 }
 public MongoBulkWriteOperationExceptionTests()
 {
     _connectionId = new ConnectionId(new ServerId(new ClusterId(1), new DnsEndPoint("localhost", 27017)), 2).WithServerValue(3);
     _processedRequests = new List<WriteRequest>();
     _upserts = new List<BulkWriteOperationUpsert>();
     _result = new BulkWriteOperationResult.Acknowledged(1, 2, 3, 4, 5, _processedRequests, _upserts);
     _writeErrors = new List<BulkWriteOperationError>();
     _writeConcernError = new BulkWriteConcernError(1, "message", new BsonDocument("x", 1));
     _unprocessedRequests = new List<WriteRequest>();
 }
 public void OneTimeSetUp()
 {
     _connectionId        = new ConnectionId(new ServerId(new ClusterId(1), new DnsEndPoint("localhost", 27017)), 2).WithServerValue(3);
     _processedRequests   = new List <WriteRequest>();
     _upserts             = new List <BulkWriteOperationUpsert>();
     _result              = new BulkWriteOperationResult.Acknowledged(1, 2, 3, 4, 5, _processedRequests, _upserts);
     _writeErrors         = new List <BulkWriteOperationError>();
     _writeConcernError   = new BulkWriteConcernError(1, "message", new BsonDocument("x", 1));
     _unprocessedRequests = new List <WriteRequest>();
 }
 // constructors
 /// <summary>
 /// Initializes a new instance of the <see cref="BulkWriteOperationException" /> class.
 /// </summary>
 /// <param name="result">The result.</param>
 /// <param name="writeErrors">The write errors.</param>
 /// <param name="unprocessedRequests">The unprocessed requests.</param>
 /// <param name="writeConcernError">The write concern error.</param>
 public BulkWriteOperationException(
     BulkWriteOperationResult result,
     IReadOnlyList <BulkWriteOperationError> writeErrors,
     BulkWriteConcernError writeConcernError,
     IReadOnlyList <WriteRequest> unprocessedRequests)
     : base("A bulk write operation resulted in one or more errors.")
 {
     _result              = result;
     _writeErrors         = writeErrors;
     _writeConcernError   = writeConcernError;
     _unprocessedRequests = unprocessedRequests;
 }
 // constructors
 /// <summary>
 /// Initializes a new instance of the <see cref="BulkWriteOperationException" /> class.
 /// </summary>
 /// <param name="result">The result.</param>
 /// <param name="writeErrors">The write errors.</param>
 /// <param name="unprocessedRequests">The unprocessed requests.</param>
 /// <param name="writeConcernError">The write concern error.</param>
 public BulkWriteOperationException(
     BulkWriteOperationResult result,
     IReadOnlyList<BulkWriteOperationError> writeErrors,
     BulkWriteConcernError writeConcernError,
     IReadOnlyList<WriteRequest> unprocessedRequests)
     : base("A bulk write operation resulted in one or more errors.")
 {
     _result = result;
     _writeErrors = writeErrors;
     _writeConcernError = writeConcernError;
     _unprocessedRequests = unprocessedRequests;
 }
 // constructors
 /// <summary>
 /// Initializes a new instance of the <see cref="MongoBulkWriteOperationException" /> class.
 /// </summary>
 /// <param name="connectionId">The connection identifier.</param>
 /// <param name="result">The result.</param>
 /// <param name="writeErrors">The write errors.</param>
 /// <param name="writeConcernError">The write concern error.</param>
 /// <param name="unprocessedRequests">The unprocessed requests.</param>
 public MongoBulkWriteOperationException(
     ConnectionId connectionId,
     BulkWriteOperationResult result,
     IReadOnlyList <BulkWriteOperationError> writeErrors,
     BulkWriteConcernError writeConcernError,
     IReadOnlyList <WriteRequest> unprocessedRequests)
     : base(connectionId, FormatMessage(writeErrors, writeConcernError))
 {
     _result              = result;
     _writeErrors         = writeErrors;
     _writeConcernError   = writeConcernError;
     _unprocessedRequests = unprocessedRequests;
 }
 // constructors
 /// <summary>
 /// Initializes a new instance of the <see cref="MongoBulkWriteOperationException" /> class.
 /// </summary>
 /// <param name="connectionId">The connection identifier.</param>
 /// <param name="result">The result.</param>
 /// <param name="writeErrors">The write errors.</param>
 /// <param name="writeConcernError">The write concern error.</param>
 /// <param name="unprocessedRequests">The unprocessed requests.</param>
 public MongoBulkWriteOperationException(
     ConnectionId connectionId,
     BulkWriteOperationResult result,
     IReadOnlyList<BulkWriteOperationError> writeErrors,
     BulkWriteConcernError writeConcernError,
     IReadOnlyList<WriteRequest> unprocessedRequests)
     : base(connectionId, FormatMessage(writeErrors, writeConcernError))
 {
     _result = result;
     _writeErrors = writeErrors;
     _writeConcernError = writeConcernError;
     _unprocessedRequests = unprocessedRequests;
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="MongoBulkWriteOperationException" /> class.
 /// </summary>
 /// <param name="info">The SerializationInfo.</param>
 /// <param name="context">The StreamingContext.</param>
 public MongoBulkWriteOperationException(SerializationInfo info, StreamingContext context)
     : base(info, context)
 {
     _result = (BulkWriteOperationResult)info.GetValue("_result", typeof(BulkWriteOperationResult));
     _unprocessedRequests = (IReadOnlyList <WriteRequest>)info.GetValue("_unprocessedRequests", typeof(IReadOnlyList <WriteRequest>));
     _writeConcernError   = (BulkWriteConcernError)info.GetValue("_writeConcernError", typeof(BulkWriteConcernError));
     _writeErrors         = (IReadOnlyList <BulkWriteOperationError>)info.GetValue("_writeErrors", typeof(IReadOnlyList <BulkWriteOperationError>));
     if (_writeConcernError != null)
     {
         foreach (var errorLabel in _writeConcernError.ErrorLabels)
         {
             AddErrorLabel(errorLabel);
         }
     }
 }
        public void Constructor_should_work()
        {
            var processedRequests   = new WriteRequest[0];
            var upserts             = new BulkWriteOperationUpsert[0];
            var result              = new BulkWriteOperationResult.Acknowledged(1, 2, 3, 4, 5, processedRequests, upserts);
            var writeErrors         = new BulkWriteOperationError[0];
            var writeConcernError   = new BulkWriteConcernError(1, "message", new BsonDocument("x", 1));
            var unprocessedRequests = new WriteRequest[0];
            var subject             = new BulkWriteOperationException(result, writeErrors, writeConcernError, unprocessedRequests);

            subject.Result.Should().BeSameAs(result);
            subject.UnprocessedRequests.Should().BeSameAs(unprocessedRequests);
            subject.WriteConcernError.Should().BeSameAs(writeConcernError);
            subject.WriteErrors.Should().BeSameAs(writeErrors);
        }
        public void Constructor_should_work()
        {
            var processedRequests = new WriteRequest[0];
            var upserts = new BulkWriteOperationUpsert[0];
            var result = new BulkWriteOperationResult.Acknowledged(1, 2, 3, 4, 5, processedRequests, upserts);
            var writeErrors = new BulkWriteOperationError[0];
            var writeConcernError = new BulkWriteConcernError(1, "message", new BsonDocument("x", 1));
            var unprocessedRequests = new WriteRequest[0];
            var subject = new BulkWriteOperationException(result, writeErrors, writeConcernError, unprocessedRequests);

            subject.Result.Should().BeSameAs(result);
            subject.UnprocessedRequests.Should().BeSameAs(unprocessedRequests);
            subject.WriteConcernError.Should().BeSameAs(writeConcernError);
            subject.WriteErrors.Should().BeSameAs(writeErrors);
        }
        public static BulkWriteBatchResult Create(
            BulkWriteOperationResult result,
            MongoBulkWriteOperationException exception,
            IndexMap indexMap)
        {
            var  matchedCount  = 0L;
            var  deletedCount  = 0L;
            var  insertedCount = 0L;
            long?modifiedCount = null;
            var  upserts       = __noUpserts;

            if (result.IsAcknowledged)
            {
                matchedCount  = result.MatchedCount;
                deletedCount  = result.DeletedCount;
                insertedCount = result.InsertedCount;
                modifiedCount = result.IsModifiedCountAvailable ? (long?)result.ModifiedCount : null;
                upserts       = result.Upserts;
            }

            var unprocessedRequests = __noWriteRequests;
            var writeErrors         = __noWriteErrors;
            BulkWriteConcernError writeConcernError = null;

            if (exception != null)
            {
                unprocessedRequests = exception.UnprocessedRequests;
                writeErrors         = exception.WriteErrors;
                writeConcernError   = exception.WriteConcernError;
            }

            return(new BulkWriteBatchResult(
                       result.RequestCount,
                       result.ProcessedRequests,
                       unprocessedRequests,
                       matchedCount,
                       deletedCount,
                       insertedCount,
                       modifiedCount,
                       upserts,
                       writeErrors,
                       writeConcernError,
                       indexMap));
        }
 // constructors
 /// <summary>
 /// Initializes a new instance of the <see cref="MongoBulkWriteOperationException" /> class.
 /// </summary>
 /// <param name="connectionId">The connection identifier.</param>
 /// <param name="result">The result.</param>
 /// <param name="writeErrors">The write errors.</param>
 /// <param name="writeConcernError">The write concern error.</param>
 /// <param name="unprocessedRequests">The unprocessed requests.</param>
 public MongoBulkWriteOperationException(
     ConnectionId connectionId,
     BulkWriteOperationResult result,
     IReadOnlyList <BulkWriteOperationError> writeErrors,
     BulkWriteConcernError writeConcernError,
     IReadOnlyList <WriteRequest> unprocessedRequests)
     : base(connectionId, FormatMessage(writeErrors, writeConcernError))
 {
     _result              = result;
     _writeErrors         = writeErrors;
     _writeConcernError   = writeConcernError;
     _unprocessedRequests = unprocessedRequests;
     if (_writeConcernError != null)
     {
         foreach (var errorLabel in _writeConcernError.ErrorLabels)
         {
             AddErrorLabel(errorLabel);
         }
     }
 }
        public void Serialization_should_drop_custom_fields()
        {
            var processedRequests = new WriteRequest[0];
            var upserts = new BulkWriteOperationUpsert[0];
            var result = new BulkWriteOperationResult.Acknowledged(1, 2, 3, 4, 5, processedRequests, upserts);
            var writeErrors = new BulkWriteOperationError[0];
            var writeConcernError = new BulkWriteConcernError(1, "message", new BsonDocument("x", 1));
            var unprocessedRequests = new WriteRequest[0];
            var subject = new BulkWriteOperationException(result, writeErrors, writeConcernError, unprocessedRequests);

            var formatter = new BinaryFormatter();
            using (var stream = new MemoryStream())
            {
                formatter.Serialize(stream, subject);
                stream.Position = 0;
                var rehydrated = (BulkWriteOperationException)formatter.Deserialize(stream);

                rehydrated.Result.Should().BeNull();
                rehydrated.UnprocessedRequests.Should().BeNull();
                rehydrated.WriteConcernError.Should().BeNull();
                rehydrated.WriteErrors.Should().BeNull();
            }
        }
        public void Serialization_should_drop_custom_fields()
        {
            var processedRequests   = new WriteRequest[0];
            var upserts             = new BulkWriteOperationUpsert[0];
            var result              = new BulkWriteOperationResult.Acknowledged(1, 2, 3, 4, 5, processedRequests, upserts);
            var writeErrors         = new BulkWriteOperationError[0];
            var writeConcernError   = new BulkWriteConcernError(1, "message", new BsonDocument("x", 1));
            var unprocessedRequests = new WriteRequest[0];
            var subject             = new BulkWriteOperationException(result, writeErrors, writeConcernError, unprocessedRequests);

            var formatter = new BinaryFormatter();

            using (var stream = new MemoryStream())
            {
                formatter.Serialize(stream, subject);
                stream.Position = 0;
                var rehydrated = (BulkWriteOperationException)formatter.Deserialize(stream);

                rehydrated.Result.Should().BeNull();
                rehydrated.UnprocessedRequests.Should().BeNull();
                rehydrated.WriteConcernError.Should().BeNull();
                rehydrated.WriteErrors.Should().BeNull();
            }
        }
 // internal static methods
 internal static WriteConcernError FromCore(Core.Operations.BulkWriteConcernError error)
 {
     return(error == null ? null : new WriteConcernError(error.Code, error.CodeName, error.Message, error.Details, error.ErrorLabels));
 }
 // constructors
 public BulkWriteBatchResult(
     int batchCount,
     IReadOnlyList<WriteRequest> processedRequests,
     IReadOnlyList<WriteRequest> unprocessedRequests,
     long matchedCount,
     long deletedCount,
     long insertedCount,
     long? modifiedCount,
     IReadOnlyList<BulkWriteOperationUpsert> upserts,
     IReadOnlyList<BulkWriteOperationError> writeErrors,
     BulkWriteConcernError writeConcernError,
     IndexMap indexMap)
 {
     _batchCount = batchCount;
     _matchedCount = matchedCount;
     _deletedCount = deletedCount;
     _insertedCount = insertedCount;
     _modifiedCount = modifiedCount;
     _indexMap = indexMap;
     _processedRequests = processedRequests;
     _writeErrors = writeErrors;
     _unprocessedRequests = unprocessedRequests;
     _upserts = upserts;
     _writeConcernError = writeConcernError;
 }
        public static BulkWriteBatchResult Create(
            WriteRequest request,
            WriteConcernResult writeConcernResult,
            MongoWriteConcernException writeConcernException,
            IndexMap indexMap)
        {
            var       processedRequests   = new[] { request };
            var       unprocessedRequests = __noWriteRequests;
            BsonValue upsertId            = null;
            var       documentsAffected   = 0L;

            if (writeConcernResult != null)
            {
                upsertId          = writeConcernResult.Upserted;
                documentsAffected = writeConcernResult.DocumentsAffected;
                var updateRequest = request as UpdateRequest;

                if (upsertId == null &&
                    documentsAffected == 1 &&
                    updateRequest != null &&
                    updateRequest.IsUpsert &&
                    !writeConcernResult.UpdatedExisting)
                {
                    // Get the _id field first from the Update document
                    // and then from the Query document.
                    upsertId =
                        updateRequest.Update.ToBsonDocument().GetValue("_id", null) ??
                        updateRequest.Filter.ToBsonDocument().GetValue("_id", null);
                }
            }

            var upserts     = (upsertId == null) ? __noUpserts : new[] { new BulkWriteOperationUpsert(0, upsertId) };
            var writeErrors = __noWriteErrors;
            BulkWriteConcernError writeConcernError = null;

            if (writeConcernException != null)
            {
                var getLastErrorResponse = writeConcernResult.Response;
                if (IsGetLasterrorResponseAWriteConcernError(getLastErrorResponse))
                {
                    writeConcernError = CreateWriteConcernErrorFromGetLastErrorResponse(getLastErrorResponse);
                }
                else
                {
                    writeErrors = new[] { CreateWriteErrorFromGetLastErrorResponse(getLastErrorResponse) };
                }
            }

            if (request.RequestType == WriteRequestType.Insert && writeErrors.Count == 0)
            {
                documentsAffected = 1; // note: DocumentsAffected is 0 for inserts
            }

            var  matchedCount  = 0L;
            var  deletedCount  = 0L;
            var  insertedCount = 0L;
            long?modifiedCount = 0L;

            switch (request.RequestType)
            {
            case WriteRequestType.Delete:
                deletedCount = documentsAffected;
                break;

            case WriteRequestType.Insert:
                insertedCount = documentsAffected;
                break;

            case WriteRequestType.Update:
                matchedCount  = documentsAffected - upserts.Count();
                modifiedCount = null;     // getLasterror does not report this value
                break;
            }

            return(new BulkWriteBatchResult(
                       1, // batchCount
                       processedRequests,
                       unprocessedRequests,
                       matchedCount,
                       deletedCount,
                       insertedCount,
                       modifiedCount,
                       upserts,
                       writeErrors,
                       writeConcernError,
                       indexMap));
        }
        private static string FormatMessage(IReadOnlyList <BulkWriteOperationError> writeErrors, BulkWriteConcernError writeConcernError)
        {
            var sb = new StringBuilder("A bulk write operation resulted in one or more errors.");

            if (writeErrors != null)
            {
                foreach (var writeError in writeErrors)
                {
                    sb.AppendLine().Append("  " + writeError.Message);
                }
            }
            if (writeConcernError != null)
            {
                sb.AppendLine().Append("  " + writeConcernError.Message);
            }

            return(sb.ToString());
        }
        private static string FormatMessage(IReadOnlyList<BulkWriteOperationError> writeErrors, BulkWriteConcernError writeConcernError)
        {
            var sb = new StringBuilder("A bulk write operation resulted in one or more errors.");
            if (writeErrors != null)
            {
                foreach (var writeError in writeErrors)
                {
                    sb.AppendLine().Append("  " + writeError.Message);
                }
            }
            if (writeConcernError != null)
            {
                sb.AppendLine().Append("  " + writeConcernError.Message);
            }

            return sb.ToString();
        }