Exemple #1
0
        // static
        internal static WriteException FromBulkWriteException(BulkWriteException bulkException)
        {
            var writeConcernError = bulkException.WriteConcernError;
            var writeError        = bulkException.WriteErrors.Count > 0
                ? bulkException.WriteErrors[0]
                : null;

            return(new WriteException(writeError, writeConcernError, bulkException));
        }
        // static
        internal static WriteException FromBulkWriteException(BulkWriteException bulkException)
        {
            var writeConcernError = bulkException.WriteConcernError;
            var writeError = bulkException.WriteErrors.Count > 0
                ? bulkException.WriteErrors[0]
                : null;

            return new WriteException(writeError, writeConcernError, bulkException);
        }
Exemple #3
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);
                }
            }
        }
Exemple #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);
            }
        }
 private void WriteErrors(BulkWriteException exception)
 {
     exception.WriteErrors
         .ToList()
         .ForEach(e => Console.WriteLine(e.Message));
 }