Exemple #1
0
 public void OneTimeSetUp()
 {
     _connectionId = new ConnectionId(new ServerId(new ClusterId(1), new DnsEndPoint("localhost", 27017)), 2);
     var processedRequests = new[] { new InsertOneModel<BsonDocument>(new BsonDocument("b", 1)) };
     var upserts = new BulkWriteUpsert[0];
     _bulkWriteResult = new BulkWriteResult<BsonDocument>.Acknowledged(1, 1, 0, 0, 0, processedRequests, upserts);
     _writeConcernError = new WriteConcernError(11, "funny", new BsonDocument("c", 1));
     _writeErrors = new[] { new BulkWriteError(10, ServerErrorCategory.Uncategorized, 1, "blah", new BsonDocument("a", 1)) };
     _unprocessedRequests = new[] { new InsertOneModel<BsonDocument>(new BsonDocument("a", 1)) };
 }
        public void Constructor_should_work()
        {
            var processedRequests   = new WriteRequest[0];
            var upserts             = new BulkWriteUpsert[0];
            var result              = new AcknowledgedBulkWriteOperationResult(1, 2, 3, 4, 5, processedRequests, upserts);
            var writeErrors         = new BulkWriteError[0];
            var writeConcernError   = new WriteConcernError(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 BulkWriteUpsert[0];
            var result = new AcknowledgedBulkWriteOperationResult(1, 2, 3, 4, 5, processedRequests, upserts);
            var writeErrors = new BulkWriteError[0];
            var writeConcernError = new WriteConcernError(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);
        }
Exemple #4
0
        private static IReadOnlyList <BulkWriteUpsert> CreateUpserts(BsonDocument writeCommandResponse)
        {
            var upserts = new List <BulkWriteUpsert>();

            if (writeCommandResponse.Contains("upserted"))
            {
                foreach (BsonDocument value in writeCommandResponse["upserted"].AsBsonArray)
                {
                    var index  = value["index"].ToInt32();
                    var id     = value["_id"];
                    var upsert = new BulkWriteUpsert(index, id);
                    upserts.Add(upsert);
                }
            }

            return(upserts);
        }
        public void Serialization_should_drop_custom_fields()
        {
            var processedRequests = new WriteRequest[0];
            var upserts = new BulkWriteUpsert[0];
            var result = new AcknowledgedBulkWriteOperationResult(1, 2, 3, 4, 5, processedRequests, upserts);
            var writeErrors = new BulkWriteError[0];
            var writeConcernError = new WriteConcernError(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 BulkWriteUpsert[0];
            var result              = new AcknowledgedBulkWriteOperationResult(1, 2, 3, 4, 5, processedRequests, upserts);
            var writeErrors         = new BulkWriteError[0];
            var writeConcernError   = new WriteConcernError(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();
            }
        }
        private static IReadOnlyList<BulkWriteUpsert> CreateUpserts(BsonDocument writeCommandResponse)
        {
            var upserts = new List<BulkWriteUpsert>();

            if (writeCommandResponse.Contains("upserted"))
            {
                foreach (BsonDocument value in writeCommandResponse["upserted"].AsBsonArray)
                {
                    var index = value["index"].ToInt32();
                    var id = value["_id"];
                    var upsert = new BulkWriteUpsert(index, id);
                    upserts.Add(upsert);
                }
            }

            return upserts;
        }
        // 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;
            BulkWriteUpsert 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));
        }
 public static bool Equals(BulkWriteUpsert x, BulkWriteUpsert y)
 {
     return
         object.Equals(x.Id, x.Id) &&
         x.Index == y.Index;
 }
Exemple #10
0
 // internal static methods
 internal static BulkWriteUpsert FromCore(Core.Operations.BulkWriteUpsert upsert)
 {
     return(new BulkWriteUpsert(upsert.Index, upsert.Id));
 }