public void EnsureCollectionExists(bool async)
        {
            var requests  = new[] { new InsertRequest(new BsonDocument("_id", ObjectId.GenerateNewId())) };
            var operation = new BulkInsertOperation(_collectionNamespace, requests, _messageEncoderSettings);

            ExecuteOperation(operation, async);
        }
Пример #2
0
        public Task EnsureCollectionExistsAsync(IWriteBinding binding)
        {
            var requests  = new[] { new InsertRequest(new BsonDocument("_id", 1)) };
            var operation = new BulkInsertOperation(_collectionNamespace, requests, _messageEncoderSettings);

            return(operation.ExecuteAsync(binding, CancellationToken.None));
        }
Пример #3
0
        protected async Task InsertAsync(IEnumerable <BsonDocument> documents)
        {
            var requests        = documents.Select(d => new InsertRequest(d));
            var insertOperation = new BulkInsertOperation(_collectionNamespace, requests, _messageEncoderSettings);

            await ExecuteOperationAsync(insertOperation);
        }
        // helper methods
        private void EnsureDatabaseExists(IWriteBinding binding)
        {
            var collectionNamespace = new CollectionNamespace(_databaseNamespace, "test");
            var requests            = new[] { new InsertRequest(new BsonDocument()) };
            var insertOperation     = new BulkInsertOperation(collectionNamespace, requests, _messageEncoderSettings);

            insertOperation.Execute(binding);
        }
Пример #5
0
        // helper methods
        private void EnsureDatabaseExists(IWriteBinding binding)
        {
            var collectionNamespace = new CollectionNamespace(_databaseNamespace, "test");
            var requests            = new[] { new InsertRequest(new BsonDocument()) };
            var insertOperation     = new BulkInsertOperation(collectionNamespace, requests, _messageEncoderSettings);

            insertOperation.ExecuteAsync(binding, CancellationToken.None).GetAwaiter().GetResult();
        }
        private void EnsureCollectionExists(CollectionNamespace collectionNamespace, bool async)
        {
            var document        = new BsonDocument("_id", ObjectId.GenerateNewId());
            var insertRequests  = new[] { new InsertRequest(document) };
            var insertOperation = new BulkInsertOperation(collectionNamespace, insertRequests, _messageEncoderSettings);

            ExecuteOperation(insertOperation, async);
        }
        // helper methods
        private void EnsureDatabaseExists(bool async)
        {
            var collectionNamespace = new CollectionNamespace(_databaseNamespace, "test");
            var requests            = new[] { new InsertRequest(new BsonDocument()) };
            var insertOperation     = new BulkInsertOperation(collectionNamespace, requests, _messageEncoderSettings);

            ExecuteOperation(insertOperation, async);
        }
        // private methods
        public void EnsureCollectionExists()
        {
            var document  = new BsonDocument("x", 1);
            var requests  = new[] { new InsertRequest(document) };
            var operation = new BulkInsertOperation(_collectionNamespace, requests, _messageEncoderSettings);

            ExecuteOperation(operation);
        }
        // methods
        public async Task <WriteConcernResult> ExecuteAsync(IChannelHandle channel, CancellationToken cancellationToken)
        {
            Ensure.IsNotNull(channel, "channel");

            var requests = _documentSource.GetRemainingItems().Select(d =>
            {
                if (d == null)
                {
                    throw new ArgumentException("Batch contains one or more null documents.");
                }

                return(new InsertRequest(new BsonDocumentWrapper(d, _serializer)));
            });
            var operation = new BulkInsertOperation(_collectionNamespace, requests, _messageEncoderSettings)
            {
                IsOrdered      = !_continueOnError,
                MaxBatchCount  = _maxBatchCount,
                MaxBatchLength = _maxMessageSize,
                // ReaderSettings = ?
                WriteConcern = _writeConcern,
                // WriteSettings = ?
            };

            BulkWriteOperationResult         bulkWriteResult;
            MongoBulkWriteOperationException bulkWriteException = null;

            try
            {
                bulkWriteResult = await operation.ExecuteAsync(channel, cancellationToken).ConfigureAwait(false);
            }
            catch (MongoBulkWriteOperationException ex)
            {
                bulkWriteResult    = ex.Result;
                bulkWriteException = ex;
            }

            var converter = new BulkWriteOperationResultConverter();

            if (bulkWriteException != null)
            {
                throw converter.ToWriteConcernException(channel.ConnectionDescription.ConnectionId, bulkWriteException);
            }
            else
            {
                if (_writeConcern.IsAcknowledged)
                {
                    return(converter.ToWriteConcernResult(bulkWriteResult));
                }
                else
                {
                    return(null);
                }
            }
        }
        // methods
        public async Task <WriteConcernResult> ExecuteAsync(IConnectionHandle connection, TimeSpan timeout = default(TimeSpan), CancellationToken cancellationToken = default(CancellationToken))
        {
            Ensure.IsNotNull(connection, "connection");

            var requests = _documentSource.GetRemainingItems().Select(d =>
            {
                if (d == null)
                {
                    throw new ArgumentException("Batch contains one or more null documents.");
                }

                return(new InsertRequest(new BsonDocumentWrapper(d, _serializer)));
            });
            var operation = new BulkInsertOperation(_collectionNamespace, requests, _messageEncoderSettings)
            {
                IsOrdered     = !_continueOnError,
                MaxBatchCount = _maxBatchCount ?? 0,
                // ReaderSettings = ?
                WriteConcern = _writeConcern,
                // WriteSettings = ?
            };

            BulkWriteOperationResult    bulkWriteResult;
            BulkWriteOperationException bulkWriteException = null;

            try
            {
                bulkWriteResult = await operation.ExecuteAsync(connection, timeout, cancellationToken);
            }
            catch (BulkWriteOperationException ex)
            {
                bulkWriteResult    = ex.Result;
                bulkWriteException = ex;
            }

            var converter = new BulkWriteOperationResultConverter();

            if (bulkWriteException != null)
            {
                throw converter.ToWriteConcernException(bulkWriteException);
            }
            else
            {
                if (_writeConcern.IsAcknowledged)
                {
                    return(converter.ToWriteConcernResult(bulkWriteResult));
                }
                else
                {
                    return(null);
                }
            }
        }
Пример #11
0
        private void CreateIndexAndInsertData(CollectionNamespace collectionNamespace)
        {
            var createIndexRequests  = new[] { new CreateIndexRequest(new BsonDocument("x", 1)) };
            var createIndexOperation = new CreateIndexesOperation(collectionNamespace, createIndexRequests, _messageEncoderSettings);

            ExecuteOperation(createIndexOperation);

            var insertRequests  = new[] { new InsertRequest(new BsonDocument("x", 1)) };
            var insertOperation = new BulkInsertOperation(collectionNamespace, insertRequests, _messageEncoderSettings);

            ExecuteOperation(insertOperation);
        }
        private Task <BulkWriteOperationResult> ExecuteInsertsAsync(IChannelHandle channel, IEnumerable <InsertRequest> requests, CancellationToken cancellationToken)
        {
            var operation = new BulkInsertOperation(_collectionNamespace, requests, _messageEncoderSettings)
            {
                MaxBatchCount          = _maxBatchCount,
                MaxBatchLength         = _maxBatchLength,
                IsOrdered              = _isOrdered,
                MessageEncoderSettings = _messageEncoderSettings,
                WriteConcern           = _writeConcern
            };

            return(operation.ExecuteAsync(channel, cancellationToken));
        }
Пример #13
0
        private Task <BulkWriteResult> ExecuteInsertsAsync(IConnectionHandle connection, IEnumerable <InsertRequest> requests, TimeSpan timeout, CancellationToken cancellationToken)
        {
            var operation = new BulkInsertOperation(_databaseName, _collectionName, requests)
            {
                AssignId          = _assignId,
                CheckElementNames = _checkElementNames,
                MaxBatchCount     = _maxBatchCount,
                MaxBatchLength    = _maxBatchLength,
                IsOrdered         = _isOrdered,
                ReaderSettings    = _readerSettings,
                WriteConcern      = _writeConcern,
                WriterSettings    = _writerSettings
            };

            return(operation.ExecuteAsync(connection, timeout, cancellationToken));
        }
Пример #14
0
        // helper methods
        private void EnsureTestData()
        {
            if (_testDataHasBeenCreated)
            {
                return;
            }

            using (var binding = SuiteConfiguration.GetReadWriteBinding())
            {
                var dropCollectionOperation = new DropCollectionOperation(_collectionNamespace, _messageEncoderSettings);
                dropCollectionOperation.Execute(binding);

                var requests = Enumerable.Range(1, 5)
                               .Select(id => new BsonDocument("_id", id))
                               .Select(document => new InsertRequest(document));
                var insertOperation = new BulkInsertOperation(_collectionNamespace, requests, _messageEncoderSettings);
                insertOperation.Execute(binding);
            }

            _testDataHasBeenCreated = true;
        }
 public Task EnsureCollectionExistsAsync(IWriteBinding binding)
 {
     var requests = new[] { new InsertRequest(new BsonDocument("_id", 1)) };
     var operation = new BulkInsertOperation(_collectionNamespace, requests, _messageEncoderSettings);
     return operation.ExecuteAsync(binding, CancellationToken.None);
 }
 // private methods
 public void EnsureCollectionExists()
 {
     var document = new BsonDocument("x", 1);
     var requests = new[] { new InsertRequest(document) };
     var operation = new BulkInsertOperation(_collectionNamespace, requests, _messageEncoderSettings);
     ExecuteOperation(operation);
 }
 public void EnsureCollectionExists(bool async)
 {
     var requests = new[] { new InsertRequest(new BsonDocument("_id", ObjectId.GenerateNewId())) };
     var operation = new BulkInsertOperation(_collectionNamespace, requests, _messageEncoderSettings);
     ExecuteOperation(operation, async);
 }
 private void EnsureCollectionExists(CollectionNamespace collectionNamespace, bool async)
 {
     var document = new BsonDocument("_id", ObjectId.GenerateNewId());
     var insertRequests = new[] { new InsertRequest(document) };
     var insertOperation = new BulkInsertOperation(collectionNamespace, insertRequests, _messageEncoderSettings);
     ExecuteOperation(insertOperation, async);
 }
 // helper methods
 private void EnsureDatabaseExists(IWriteBinding binding)
 {
     var collectionNamespace = new CollectionNamespace(_databaseNamespace, "test");
     var requests = new[] { new InsertRequest(new BsonDocument()) };
     var insertOperation = new BulkInsertOperation(collectionNamespace, requests, _messageEncoderSettings);
     insertOperation.ExecuteAsync(binding, CancellationToken.None).GetAwaiter().GetResult();
 }
 // helper methods
 private void EnsureDatabaseExists(bool async)
 {
     var collectionNamespace = new CollectionNamespace(_databaseNamespace, "test");
     var requests = new[] { new InsertRequest(new BsonDocument()) };
     var insertOperation = new BulkInsertOperation(collectionNamespace, requests, _messageEncoderSettings);
     ExecuteOperation(insertOperation, async);
 }
        private void CreateIndexAndInsertData(CollectionNamespace collectionNamespace)
        {
            var createIndexRequests = new[] { new CreateIndexRequest(new BsonDocument("x", 1)) };
            var createIndexOperation = new CreateIndexesOperation(collectionNamespace, createIndexRequests, _messageEncoderSettings);
            ExecuteOperation(createIndexOperation);

            var insertRequests = new[] { new InsertRequest(new BsonDocument("x", 1)) };
            var insertOperation = new BulkInsertOperation(collectionNamespace, insertRequests, _messageEncoderSettings);
            ExecuteOperation(insertOperation);
        }