/// <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>));
 }
 protected override void When()
 {
     var subject = new BulkMixedWriteOperation(CollectionNamespace, _requests, MessageEncoderSettings)
     {
         MaxBatchCount = _maxBatchCount,
     };
     _result = ExecuteOperationAsync(subject).GetAwaiter().GetResult();
 }
 /// <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<BulkWriteError> writeErrors,
     WriteConcernError 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 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);
         }
     }
 }
Exemplo n.º 13
0
        private WriteConcernResult CreateResultOrThrow(IChannelHandle channel, BulkWriteOperationResult result, MongoBulkWriteOperationException exception)
        {
            var converter = new BulkWriteOperationResultConverter();

            if (exception != null)
            {
                throw converter.ToWriteConcernException(channel.ConnectionDescription.ConnectionId, exception);
            }
            else
            {
                if (_writeConcern.IsAcknowledged)
                {
                    return(converter.ToWriteConcernResult(result));
                }
                else
                {
                    return(null);
                }
            }
        }
 public WriteConcernResult ToWriteConcernResult(BulkWriteOperationResult bulkWriteResult)
 {
     return ToWriteConcernResult(bulkWriteResult, null);
 }
        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);
        }
 private WriteConcernResult CreateResultOrThrow(IChannelHandle channel, BulkWriteOperationResult result, MongoBulkWriteOperationException exception)
 {
     var converter = new BulkWriteOperationResultConverter();
     if (exception != null)
     {
         throw converter.ToWriteConcernException(channel.ConnectionDescription.ConnectionId, exception);
     }
     else
     {
         if (_writeConcern.IsAcknowledged)
         {
             return converter.ToWriteConcernResult(result);
         }
         else
         {
             return null;
         }
     }
 }
Exemplo n.º 17
0
 public WriteConcernResult ToWriteConcernResult(BulkWriteOperationResult bulkWriteResult)
 {
     return(ToWriteConcernResult(bulkWriteResult, null));
 }
Exemplo n.º 18
0
        // private methods
        private WriteConcernResult ToWriteConcernResult(BulkWriteOperationResult bulkWriteResult, MongoBulkWriteOperationException bulkWriteException)
        {
            if (!bulkWriteResult.IsAcknowledged)
            {
                return(null);
            }

            // don't include InsertedCount in getLastErrorResponse
            var documentsAffectedCount =
                bulkWriteResult.DeletedCount +
                bulkWriteResult.MatchedCount +
                bulkWriteResult.Upserts.Count;

            var isUpdate = bulkWriteResult.ProcessedRequests.Any(r => r.RequestType == WriteRequestType.Update);

            var updatedExisting             = false;
            BulkWriteOperationUpsert upsert = null;

            if (isUpdate)
            {
                upsert          = bulkWriteResult.Upserts.LastOrDefault();
                updatedExisting = documentsAffectedCount > 0 && upsert == null;
            }

            var          code    = 0;
            string       message = null;
            BsonDocument details = null;

            if (bulkWriteException != null)
            {
                var lastWriteError    = bulkWriteException.WriteErrors.LastOrDefault();
                var writeConcernError = bulkWriteException.WriteConcernError;

                code = 8; // UnknownError
                if (lastWriteError != null)
                {
                    code    = lastWriteError.Code;
                    message = lastWriteError.Message;
                    details = lastWriteError.Details;
                }
                else if (writeConcernError != null)
                {
                    code    = writeConcernError.Code;
                    message = writeConcernError.Message;
                    details = writeConcernError.Details;
                }
            }

            var getLastErrorResponse = new BsonDocument
            {
                { "ok", 1 },
                { "code", code, code != 0 },
                { "err", message, message != null },
                { "n", documentsAffectedCount },
                { "updatedExisting", updatedExisting, isUpdate },
                { "upserted", () => upsert.Id, isUpdate&& upsert != null },
            };

            if (details != null)
            {
                getLastErrorResponse.Merge(details, false); // don't overwrite existing elements
            }

            return(new WriteConcernResult(getLastErrorResponse));
        }
        // private methods
        private WriteConcernResult ToWriteConcernResult(BulkWriteOperationResult bulkWriteResult, BulkWriteOperationException bulkWriteException)
        {
            if (!bulkWriteResult.IsAcknowledged)
            {
                return null;
            }

            // don't include InsertedCount in getLastErrorResponse
            var documentsAffectedCount =
                bulkWriteResult.DeletedCount +
                bulkWriteResult.MatchedCount +
                bulkWriteResult.Upserts.Count;

            var isUpdate = bulkWriteResult.ProcessedRequests.Any(r => r.RequestType == WriteRequestType.Update);

            var updatedExisting = false;
            BulkWriteOperationUpsert upsert = null;
            if (isUpdate)
            {
                upsert = bulkWriteResult.Upserts.LastOrDefault();
                updatedExisting = documentsAffectedCount > 0 && upsert == null;
            }

            var code = 0;
            string message = null;
            BsonDocument details = null;
            if (bulkWriteException != null)
            {
                var lastWriteError = bulkWriteException.WriteErrors.LastOrDefault();
                var writeConcernError = bulkWriteException.WriteConcernError;

                code = 8; // UnknownError
                if (lastWriteError != null)
                {
                    code = lastWriteError.Code;
                    message = lastWriteError.Message;
                    details = lastWriteError.Details;
                }
                else if (writeConcernError != null)
                {
                    code = writeConcernError.Code;
                    message = writeConcernError.Message;
                    details = writeConcernError.Details;
                }
            }

            var getLastErrorResponse = new BsonDocument
            {
                { "ok", 1 },
                { "code", code, code != 0 },
                { "err", message, message != null },
                { "n", documentsAffectedCount },
                { "updatedExisting", updatedExisting, isUpdate },
                { "upserted", () => upsert.Id, isUpdate && upsert != null },
            };
            if (details != null)
            {
                getLastErrorResponse.Merge(details, false); // don't overwrite existing elements
            }

            return new WriteConcernResult(getLastErrorResponse);
        }