Ejemplo n.º 1
0
 /// <inheritdoc />
 public virtual BulkWriteResult <TDocument> BulkWrite(IEnumerable <WriteModel <TDocument> > requests, BulkWriteOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
 {
     throw new NotImplementedException();
 }
        public async Task BulkWriteAsync_should_execute_the_BulkMixedWriteOperation(bool isOrdered)
        {
            var requests = new WriteModel <BsonDocument>[]
            {
                new InsertOneModel <BsonDocument>(new BsonDocument("_id", 1).Add("a", 1)),
                new DeleteManyModel <BsonDocument>(new BsonDocument("b", 1)),
                new DeleteOneModel <BsonDocument>(new BsonDocument("c", 1)),
                new ReplaceOneModel <BsonDocument>(new BsonDocument("d", 1), new BsonDocument("e", 1)),
                new ReplaceOneModel <BsonDocument>(new BsonDocument("f", 1), new BsonDocument("g", 1))
                {
                    IsUpsert = true
                },
                new UpdateManyModel <BsonDocument>(new BsonDocument("h", 1), new BsonDocument("$set", new BsonDocument("i", 1))),
                new UpdateManyModel <BsonDocument>(new BsonDocument("j", 1), new BsonDocument("$set", new BsonDocument("k", 1)))
                {
                    IsUpsert = true
                },
                new UpdateOneModel <BsonDocument>(new BsonDocument("l", 1), new BsonDocument("$set", new BsonDocument("m", 1))),
                new UpdateOneModel <BsonDocument>(new BsonDocument("n", 1), new BsonDocument("$set", new BsonDocument("o", 1)))
                {
                    IsUpsert = true
                },
            };
            var bulkOptions = new BulkWriteOptions
            {
                IsOrdered = isOrdered
            };

            var operationResult = new BulkWriteOperationResult.Unacknowledged(9, new[] { new InsertRequest(new BsonDocument("b", 1)) });

            _operationExecutor.EnqueueResult <BulkWriteOperationResult>(operationResult);

            var result = await _subject.BulkWriteAsync(requests, bulkOptions, CancellationToken.None);

            var call = _operationExecutor.GetWriteCall <BulkWriteOperationResult>();

            call.Operation.Should().BeOfType <BulkMixedWriteOperation>();
            var operation = (BulkMixedWriteOperation)call.Operation;

            // I know, this is a lot of stuff in one test :(
            operation.CollectionNamespace.FullName.Should().Be("foo.bar");
            operation.IsOrdered.Should().Be(isOrdered);
            operation.Requests.Count().Should().Be(9);
            var convertedRequests = operation.Requests.ToList();

            // InsertOneModel
            convertedRequests[0].Should().BeOfType <InsertRequest>();
            convertedRequests[0].CorrelationId.Should().Be(0);
            var convertedRequest0 = (InsertRequest)convertedRequests[0];

            convertedRequest0.Document.Should().Be("{_id:1, a:1}");

            // RemoveManyModel
            convertedRequests[1].Should().BeOfType <DeleteRequest>();
            convertedRequests[1].CorrelationId.Should().Be(1);
            var convertedRequest1 = (DeleteRequest)convertedRequests[1];

            convertedRequest1.Filter.Should().Be("{b:1}");
            convertedRequest1.Limit.Should().Be(0);

            // RemoveOneModel
            convertedRequests[2].Should().BeOfType <DeleteRequest>();
            convertedRequests[2].CorrelationId.Should().Be(2);
            var convertedRequest2 = (DeleteRequest)convertedRequests[2];

            convertedRequest2.Filter.Should().Be("{c:1}");
            convertedRequest2.Limit.Should().Be(1);

            // ReplaceOneModel
            convertedRequests[3].Should().BeOfType <UpdateRequest>();
            convertedRequests[3].CorrelationId.Should().Be(3);
            var convertedRequest3 = (UpdateRequest)convertedRequests[3];

            convertedRequest3.Filter.Should().Be("{d:1}");
            convertedRequest3.Update.Should().Be("{e:1}");
            convertedRequest3.UpdateType.Should().Be(UpdateType.Replacement);
            convertedRequest3.IsMulti.Should().BeFalse();
            convertedRequest3.IsUpsert.Should().BeFalse();

            // ReplaceOneModel with upsert
            convertedRequests[4].Should().BeOfType <UpdateRequest>();
            convertedRequests[4].CorrelationId.Should().Be(4);
            var convertedRequest4 = (UpdateRequest)convertedRequests[4];

            convertedRequest4.Filter.Should().Be("{f:1}");
            convertedRequest4.Update.Should().Be("{g:1}");
            convertedRequest4.UpdateType.Should().Be(UpdateType.Replacement);
            convertedRequest4.IsMulti.Should().BeFalse();
            convertedRequest4.IsUpsert.Should().BeTrue();

            // UpdateManyModel
            convertedRequests[5].Should().BeOfType <UpdateRequest>();
            convertedRequests[5].CorrelationId.Should().Be(5);
            var convertedRequest5 = (UpdateRequest)convertedRequests[5];

            convertedRequest5.Filter.Should().Be("{h:1}");
            convertedRequest5.Update.Should().Be("{$set:{i:1}}");
            convertedRequest5.UpdateType.Should().Be(UpdateType.Update);
            convertedRequest5.IsMulti.Should().BeTrue();
            convertedRequest5.IsUpsert.Should().BeFalse();

            // UpdateManyModel with upsert
            convertedRequests[6].Should().BeOfType <UpdateRequest>();
            convertedRequests[6].CorrelationId.Should().Be(6);
            var convertedRequest6 = (UpdateRequest)convertedRequests[6];

            convertedRequest6.Filter.Should().Be("{j:1}");
            convertedRequest6.Update.Should().Be("{$set:{k:1}}");
            convertedRequest6.UpdateType.Should().Be(UpdateType.Update);
            convertedRequest6.IsMulti.Should().BeTrue();
            convertedRequest6.IsUpsert.Should().BeTrue();

            // UpdateOneModel
            convertedRequests[7].Should().BeOfType <UpdateRequest>();
            convertedRequests[7].CorrelationId.Should().Be(7);
            var convertedRequest7 = (UpdateRequest)convertedRequests[7];

            convertedRequest7.Filter.Should().Be("{l:1}");
            convertedRequest7.Update.Should().Be("{$set:{m:1}}");
            convertedRequest7.UpdateType.Should().Be(UpdateType.Update);
            convertedRequest7.IsMulti.Should().BeFalse();
            convertedRequest7.IsUpsert.Should().BeFalse();

            // UpdateOneModel with upsert
            convertedRequests[8].Should().BeOfType <UpdateRequest>();
            convertedRequests[8].CorrelationId.Should().Be(8);
            var convertedRequest8 = (UpdateRequest)convertedRequests[8];

            convertedRequest8.Filter.Should().Be("{n:1}");
            convertedRequest8.Update.Should().Be("{$set:{o:1}}");
            convertedRequest8.UpdateType.Should().Be(UpdateType.Update);
            convertedRequest8.IsMulti.Should().BeFalse();
            convertedRequest8.IsUpsert.Should().BeTrue();

            // Result
            result.Should().NotBeNull();
            result.IsAcknowledged.Should().BeFalse();
            result.RequestCount.Should().Be(9);
            result.ProcessedRequests.Should().BeEquivalentTo(requests);
            for (int i = 0; i < requests.Length; i++)
            {
                result.ProcessedRequests[i].Should().BeSameAs(requests[i]);
            }
        }
 /// <inheritdoc />
 public abstract Task <BulkWriteResult <TDocument> > BulkWriteAsync(IEnumerable <WriteModel <TDocument> > requests, BulkWriteOptions options = null, CancellationToken cancellationToken = default(CancellationToken));
Ejemplo n.º 4
0
 private BulkMixedWriteOperation CreateBulkWriteOperation(IEnumerable <WriteModel <TDocument> > requests, BulkWriteOptions options)
 {
     return(new BulkMixedWriteOperation(
                _collectionNamespace,
                requests.Select(ConvertWriteModelToWriteRequest),
                _messageEncoderSettings)
     {
         BypassDocumentValidation = options.BypassDocumentValidation,
         IsOrdered = options.IsOrdered,
         WriteConcern = _settings.WriteConcern
     });
 }
Ejemplo n.º 5
0
        public override async Task <BulkWriteResult <TDocument> > BulkWriteAsync(IEnumerable <WriteModel <TDocument> > requests, BulkWriteOptions options, CancellationToken cancellationToken)
        {
            Ensure.IsNotNull(requests, nameof(requests));
            if (!requests.Any())
            {
                throw new ArgumentException("Must contain at least 1 request.", "requests");
            }
            options = options ?? new BulkWriteOptions();

            var operation = CreateBulkWriteOperation(requests, options);

            try
            {
                var result = await ExecuteWriteOperationAsync(operation, cancellationToken).ConfigureAwait(false);

                return(BulkWriteResult <TDocument> .FromCore(result, requests));
            }
            catch (MongoBulkWriteOperationException ex)
            {
                throw MongoBulkWriteException <TDocument> .FromCore(ex, requests.ToList());
            }
        }
Ejemplo n.º 6
0
        public async Task <BulkWriteResult <TDocument> > BulkWriteAsync(IEnumerable <WriteModel <TDocument> > requests, BulkWriteOptions options, CancellationToken cancellationToken)
        {
            Ensure.IsNotNull(requests, "requests");
            if (!requests.Any())
            {
                throw new ArgumentException("Must contain at least 1 request.", "requests");
            }

            options = options ?? new BulkWriteOptions();

            var operation = new BulkMixedWriteOperation(
                _collectionNamespace,
                requests.Select(ConvertWriteModelToWriteRequest),
                _messageEncoderSettings)
            {
                IsOrdered    = options.IsOrdered,
                WriteConcern = _settings.WriteConcern
            };

            try
            {
                var result = await ExecuteWriteOperation(operation, cancellationToken).ConfigureAwait(false);

                return(BulkWriteResult <TDocument> .FromCore(result, requests));
            }
            catch (MongoBulkWriteOperationException ex)
            {
                throw MongoBulkWriteException <TDocument> .FromCore(ex, requests.ToList());
            }
        }
 public override Task <BulkWriteResult <TDocument> > BulkWriteAsync(IClientSessionHandle session, IEnumerable <WriteModel <TDocument> > requests, BulkWriteOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
 {
     return(_wrappedCollection.BulkWriteAsync(session, CombineModelFilters(requests), options, cancellationToken));
 }
 public override BulkWriteResult <TDocument> BulkWrite(IEnumerable <WriteModel <TDocument> > requests, BulkWriteOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
 {
     return(_wrappedCollection.BulkWrite(CombineModelFilters(requests), options, cancellationToken));
 }