Esempio n. 1
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)
            {
                IsOrdered    = _isOrdered,
                WriteConcern = writeConcern
            };

            using (var binding = _collection.Database.Server.GetWriteBinding())
            {
                try
                {
                    var result = operation.Execute(binding, Timeout.InfiniteTimeSpan, CancellationToken.None);
                    return(BulkWriteResult <TDocument> .FromCore(result));
                }
                catch (BulkWriteOperationException ex)
                {
                    throw BulkWriteException <TDocument> .FromCore(ex);
                }
            }
        }
Esempio n. 2
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);
            }
        }