public override 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());
            }
        }
Esempio n. 2
0
        private BulkWriteResult <TDocument> ExecuteHelper(WriteConcern writeConcern)
        {
            if (_hasBeenExecuted)
            {
                throw new InvalidOperationException("The bulk write operation has already been executed.");
            }
            _hasBeenExecuted = true;

            var collectionSettings     = _collection.Settings;
            var messageEncoderSettings = _collection.GetMessageEncoderSettings();

            IEnumerable <WriteRequest> requests = _requests;

            if (_collection.Settings.AssignIdOnInsert)
            {
                requests = _requests.Select(x =>
                {
                    var insertRequest = x as InsertRequest;
                    if (insertRequest != null)
                    {
                        object document            = insertRequest.Document;
                        IBsonSerializer serializer = BsonDocumentSerializer.Instance;
                        var wrapped = insertRequest.Document as BsonDocumentWrapper;
                        while (wrapped != null)
                        {
                            document   = wrapped.Wrapped;
                            serializer = wrapped.Serializer;
                            wrapped    = document as BsonDocumentWrapper;
                        }

                        _collection.AssignId(document, serializer);
                    }

                    return(x);
                });
            }

            var operation = new BulkMixedWriteOperation(new CollectionNamespace(_collection.Database.Name, _collection.Name), requests, messageEncoderSettings)
            {
                BypassDocumentValidation = _bypassDocumentValidation,
                IsOrdered    = _isOrdered,
                WriteConcern = writeConcern
            };

            using (var binding = _collection.Database.Server.GetWriteBinding())
            {
                try
                {
                    var result = operation.Execute(binding, CancellationToken.None);
                    return(BulkWriteResult <TDocument> .FromCore(result));
                }
                catch (MongoBulkWriteOperationException ex)
                {
                    throw MongoBulkWriteException <TDocument> .FromCore(ex);
                }
            }
        }
Esempio n. 3
0
        public override BulkWriteResult <TDocument> BulkWrite(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 = ExecuteWriteOperation(operation, cancellationToken);
                return(BulkWriteResult <TDocument> .FromCore(result, requests));
            }
            catch (MongoBulkWriteOperationException ex)
            {
                throw MongoBulkWriteException <TDocument> .FromCore(ex, requests.ToList());
            }
        }
Esempio n. 4
0
        public async Task <BulkWriteResult <TDocument> > BulkWriteAsync(BulkWriteModel <TDocument> model, TimeSpan?timeout, CancellationToken cancellationToken)
        {
            Ensure.IsNotNull(model, "model");

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

            try
            {
                var result = await ExecuteWriteOperation(operation, timeout, cancellationToken);

                return(BulkWriteResult <TDocument> .FromCore(result, model.Requests));
            }
            catch (BulkWriteOperationException ex)
            {
                throw BulkWriteException <TDocument> .FromCore(ex, model.Requests);
            }
        }