private Task <BsonDocument> ExplainAggregateAsync <TResult>(AggregateModel <TResult> model, ExplainVerbosity verbosity, TimeSpan?timeout, CancellationToken cancellationToken) { var pipeline = model.Pipeline.Select(x => ConvertToBsonDocument(x)).ToList(); var aggregateOperation = CreateAggregateOperation <TResult>(model, pipeline); var explainOperation = aggregateOperation.ToExplainOperation(verbosity.ToCore()); return(ExecuteReadOperation(explainOperation, timeout, cancellationToken)); }
public async Task AggregateAsync_should_execute_the_AggregateToCollectionOperation_and_the_FindOperation_when_out_is_specified() { var pipeline = new object[] { BsonDocument.Parse("{$match: {x: 2}}"), BsonDocument.Parse("{$out: \"funny\"}") }; var model = new AggregateModel <BsonDocument>(pipeline) { AllowDiskUse = true, BatchSize = 10, MaxTime = TimeSpan.FromSeconds(3), UseCursor = false }; var result = await _subject.AggregateAsync(model, Timeout.InfiniteTimeSpan, CancellationToken.None); _operationExecutor.QueuedCallCount.Should().Be(1); var writeCall = _operationExecutor.GetWriteCall <BsonDocument>(); writeCall.Operation.Should().BeOfType <AggregateToCollectionOperation>(); var writeOperation = (AggregateToCollectionOperation)writeCall.Operation; writeOperation.CollectionNamespace.FullName.Should().Be("foo.bar"); writeOperation.AllowDiskUse.Should().Be(model.AllowDiskUse); writeOperation.MaxTime.Should().Be(model.MaxTime); writeOperation.Pipeline.Should().BeEquivalentTo(pipeline); var fakeCursor = Substitute.For <IAsyncCursor <BsonDocument> >(); _operationExecutor.EnqueueResult(fakeCursor); // this causes execution of the find operation await result.GetAsyncEnumerator().MoveNextAsync(); var call = _operationExecutor.GetReadCall <IAsyncCursor <BsonDocument> >(); call.Operation.Should().BeOfType <FindOperation <BsonDocument> >(); var operation = (FindOperation <BsonDocument>)call.Operation; operation.CollectionNamespace.FullName.Should().Be("foo.funny"); operation.AwaitData.Should().BeTrue(); operation.BatchSize.Should().Be(model.BatchSize); operation.Comment.Should().BeNull(); operation.Criteria.Should().BeNull(); operation.Limit.Should().Be(null); operation.MaxTime.Should().Be(model.MaxTime); operation.Modifiers.Should().BeNull(); operation.NoCursorTimeout.Should().BeFalse(); operation.Partial.Should().BeFalse(); operation.Projection.Should().BeNull(); operation.Skip.Should().Be(null); operation.Sort.Should().BeNull(); operation.Tailable.Should().BeFalse(); }
private AggregateOperation <TResult> CreateAggregateOperation <TResult>(AggregateModel <TResult> model, List <BsonDocument> pipeline) { var resultSerializer = model.ResultSerializer ?? _settings.SerializerRegistry.GetSerializer <TResult>(); return(new AggregateOperation <TResult>( _collectionNamespace, pipeline, resultSerializer, _messageEncoderSettings) { AllowDiskUse = model.AllowDiskUse, BatchSize = model.BatchSize, MaxTime = model.MaxTime, UseCursor = model.UseCursor }); }
// methods public async Task <IAsyncEnumerable <TResult> > AggregateAsync <TResult>(AggregateModel <TResult> model, TimeSpan?timeout, CancellationToken cancellationToken) { Ensure.IsNotNull(model, "model"); var pipeline = model.Pipeline.Select(x => ConvertToBsonDocument(x)).ToList(); var last = pipeline.LastOrDefault(); if (last != null && last.GetElement(0).Name == "$out") { var operation = new AggregateToCollectionOperation( _collectionNamespace, pipeline, _messageEncoderSettings) { AllowDiskUse = model.AllowDiskUse, MaxTime = model.MaxTime }; await ExecuteWriteOperation(operation, timeout, cancellationToken); var outputCollectionName = last.GetElement(0).Value.AsString; var findOperation = new FindOperation <TResult>( new CollectionNamespace(_collectionNamespace.DatabaseNamespace, outputCollectionName), model.ResultSerializer ?? _settings.SerializerRegistry.GetSerializer <TResult>(), _messageEncoderSettings) { BatchSize = model.BatchSize, MaxTime = model.MaxTime }; return(await Task.FromResult <IAsyncEnumerable <TResult> >(new AsyncCursorAsyncEnumerable <TResult>( () => ExecuteReadOperation(findOperation, timeout, cancellationToken), null))); } else { var operation = CreateAggregateOperation <TResult>(model, pipeline); return(await Task.FromResult <IAsyncEnumerable <TResult> >(new AsyncCursorAsyncEnumerable <TResult>( () => ExecuteReadOperation(operation, timeout, cancellationToken), null))); } }
public async Task ExplainAsync_for_aggregate_should_execute_an_AggregateExplainOperation() { var pipeline = new[] { BsonDocument.Parse("{ $skip: 10 }") }; var aggregateModel = new AggregateModel <BsonDocument>(pipeline); var model = new ExplainModel(aggregateModel) { Verbosity = ExplainVerbosity.ExecutionStats }; await _subject.ExplainAsync(model, Timeout.InfiniteTimeSpan, CancellationToken.None); var call = _operationExecutor.GetReadCall <BsonDocument>(); call.Operation.Should().BeOfType <AggregateExplainOperation>(); var operation = (AggregateExplainOperation)call.Operation; operation.CollectionNamespace.FullName.Should().Be("foo.bar"); operation.Pipeline.Should().BeEquivalentTo(pipeline); operation.AllowDiskUse.Should().Be(aggregateModel.AllowDiskUse); operation.MaxTime.Should().Be(aggregateModel.MaxTime); }
public async Task AggregateAsync_should_execute_the_AggregateOperation_when_out_is_not_specified() { var pipeline = new object[] { BsonDocument.Parse("{$match: {x: 2}}") }; var model = new AggregateModel <BsonDocument>(pipeline) { AllowDiskUse = true, BatchSize = 10, MaxTime = TimeSpan.FromSeconds(3), UseCursor = false }; var fakeCursor = NSubstitute.Substitute.For <IAsyncCursor <BsonDocument> >(); _operationExecutor.EnqueueResult(fakeCursor); var result = await _subject.AggregateAsync(model, Timeout.InfiniteTimeSpan, CancellationToken.None); // we haven't executed the operation yet. _operationExecutor.QueuedCallCount.Should().Be(0); // this causes execution of the operation await result.GetAsyncEnumerator().MoveNextAsync(); var call = _operationExecutor.GetReadCall <IAsyncCursor <BsonDocument> >(); call.Operation.Should().BeOfType <AggregateOperation <BsonDocument> >(); var operation = (AggregateOperation <BsonDocument>)call.Operation; operation.CollectionNamespace.FullName.Should().Be("foo.bar"); operation.AllowDiskUse.Should().Be(model.AllowDiskUse); operation.BatchSize.Should().Be(model.BatchSize); operation.MaxTime.Should().Be(model.MaxTime); operation.UseCursor.Should().Be(model.UseCursor); operation.Pipeline.Should().ContainInOrder(pipeline); }