示例#1
0
        public IReadOperation <BsonDocument> ToExplainOperation(ExplainVerbosity verbosity)
        {
            BsonDocument modifiers;

            if (_modifiers == null)
            {
                modifiers = new BsonDocument();
            }
            else
            {
                modifiers = (BsonDocument)_modifiers.DeepClone();
            }
            modifiers["$explain"] = true;
            var operation = new FindOperation <BsonDocument>(_collectionNamespace, BsonDocumentSerializer.Instance, _messageEncoderSettings)
            {
                _allowPartialResults = _allowPartialResults,
                _batchSize           = _batchSize,
                _comment             = _comment,
                _cursorType          = _cursorType,
                _filter          = _filter,
                _limit           = _limit,
                _maxTime         = _maxTime,
                _modifiers       = modifiers,
                _noCursorTimeout = _noCursorTimeout,
                _projection      = _projection,
                _skip            = _skip,
                _sort            = _sort,
            };

            return(new FindExplainOperation(operation));
        }
示例#2
0
        private Task <BsonDocument> ExplainCountAsync(CountModel model, ExplainVerbosity verbosity, TimeSpan?timeout, CancellationToken cancellationToken)
        {
            var countOperation   = CreateCountOperation(model);
            var explainOperation = countOperation.ToExplainOperation(verbosity.ToCore());

            return(ExecuteReadOperation(explainOperation, timeout, cancellationToken));
        }
示例#3
0
 // constructors
 /// <summary>
 /// Initializes a new instance of the <see cref="ExplainOperation"/> class.
 /// </summary>
 /// <param name="databaseNamespace">The database namespace.</param>
 /// <param name="command">The command.</param>
 /// <param name="messageEncoderSettings">The message encoder settings.</param>
 public ExplainOperation(DatabaseNamespace databaseNamespace, BsonDocument command, MessageEncoderSettings messageEncoderSettings)
 {
     _databaseNamespace      = Ensure.IsNotNull(databaseNamespace, nameof(databaseNamespace));
     _command                = Ensure.IsNotNull(command, nameof(command));
     _messageEncoderSettings = Ensure.IsNotNull(messageEncoderSettings, nameof(messageEncoderSettings));
     _verbosity              = ExplainVerbosity.QueryPlanner;
 }
示例#4
0
 // constructors
 /// <summary>
 /// Initializes a new instance of the <see cref="ExplainOperation"/> class.
 /// </summary>
 /// <param name="databaseNamespace">The database namespace.</param>
 /// <param name="explainableOperation">The explainable operation.</param>
 /// <param name="messageEncoderSettings">The message encoder settings.</param>
 public ExplainOperation(DatabaseNamespace databaseNamespace, IExplainableOperation explainableOperation, MessageEncoderSettings messageEncoderSettings)
 {
     _databaseNamespace      = Ensure.IsNotNull(databaseNamespace, nameof(databaseNamespace));
     _explainableOperation   = Ensure.IsNotNull(explainableOperation, nameof(explainableOperation));
     _messageEncoderSettings = Ensure.IsNotNull(messageEncoderSettings, nameof(messageEncoderSettings));
     _verbosity = ExplainVerbosity.QueryPlanner;
 }
示例#5
0
        public void CreateCommand_should_return_expected_result(ExplainVerbosity verbosity, string verbosityString)
        {
            var subject = new ExplainOperation(_databaseNamespace, _explainableOperation, _messageEncoderSettings)
            {
                Verbosity = verbosity
            };

            var expectedResult = new BsonDocument
            {
                { "explain", BsonDocument.Parse("{ find : 'test' }") },
                { "verbosity", verbosityString }
            };

            var endPoint     = new DnsEndPoint("localhost", 27017);
            var serverId     = new ServerId(new ClusterId(), endPoint);
            var connectionId = new ConnectionId(serverId);
            var helloResult  = new HelloResult(new BsonDocument {
                { "ok", 1 }, { "maxMessageSizeBytes", 48000000 }, { "maxWireVersion", WireVersion.Server36 }
            });
            var connectionDescription = new ConnectionDescription(connectionId, helloResult);
            var session = NoCoreSession.Instance;

            var result = subject.CreateExplainCommand(connectionDescription, session);

            result.Should().Be(expectedResult);
        }
        public static Task <BsonDocument> ExplainAggregation <TDocument, TResult>(
            this IMongoCollection <TDocument> collection
            , PipelineDefinition <TDocument, TResult> aggregationPipeline, ExplainVerbosity verbosity)
        {
            IBsonSerializerRegistry     serializerRegistry = collection.Settings.SerializerRegistry;
            IBsonSerializer <TDocument> serializer         = serializerRegistry.GetSerializer <TDocument>();
            MessageEncoderSettings      encoderSettings    = collection.GetMessageEncoderSettings();

            RenderedPipelineDefinition <TResult> renderedDefinition = aggregationPipeline.Render(serializer, serializerRegistry);

            IReadOperation <BsonDocument> explainOperation = new AggregateOperation <TResult>(
                collection.CollectionNamespace,
                renderedDefinition.Documents,
                renderedDefinition.OutputSerializer,
                encoderSettings)
                                                             .ToExplainOperation(verbosity);

            ICluster           cluster = GetCluster(collection);
            ICoreSessionHandle session = NoCoreSession.NewHandle();

            using (IReadBinding binding = new ReadPreferenceBinding(cluster, collection.Settings.ReadPreference, session))
            {
                var cancelToken = new CancellationToken();
                return(explainOperation.ExecuteAsync(binding, cancelToken));
            }
        }
示例#7
0
 public IReadOperation <BsonDocument> ToExplainOperation(ExplainVerbosity verbosity)
 {
     return(new AggregateExplainOperation(_collectionNamespace, _pipeline, _messageEncoderSettings)
     {
         AllowDiskUse = _allowDiskUse,
         MaxTime = _maxTime
     });
 }
示例#8
0
 /// <summary>
 /// Returns an explain operation for this count operation.
 /// </summary>
 /// <param name="verbosity">The verbosity.</param>
 /// <returns>An explain operation.</returns>
 public IReadOperation <BsonDocument> ToExplainOperation(ExplainVerbosity verbosity)
 {
     return(new ExplainOperation(
                _collectionNamespace.DatabaseNamespace,
                CreateCommand(),
                _messageEncoderSettings)
     {
         Verbosity = verbosity
     });
 }
        public void CreateCommand_should_return_expected_result(ExplainVerbosity verbosity, string verbosityString)
        {
            var subject = new ExplainOperation(_databaseNamespace, _command, _messageEncoderSettings)
            {
                Verbosity = verbosity
            };

            var expectedResult = new BsonDocument
            {
                { "explain", _command },
                { "verbosity", verbosityString }
            };

            var result = subject.CreateCommand();

            result.Should().Be(expectedResult);
        }
示例#10
0
        public void CreateCommand_should_return_expected_result(ExplainVerbosity verbosity, string verbosityString)
        {
            var subject = new ExplainOperation(_databaseNamespace, _command, _messageEncoderSettings)
            {
                Verbosity = verbosity
            };

            var expectedResult = new BsonDocument
            {
                { "explain", _command },
                { "verbosity", verbosityString }
            };

            var result = subject.CreateCommand();

            result.Should().Be(expectedResult);
        }
        public static Core.Operations.ExplainVerbosity ToCore(this ExplainVerbosity verbosity)
        {
            switch (verbosity)
            {
            case ExplainVerbosity.AllPlansExecution:
                return(Core.Operations.ExplainVerbosity.AllPlansExecution);

            case ExplainVerbosity.ExecutionStats:
                return(Core.Operations.ExplainVerbosity.ExecutionStats);

            case ExplainVerbosity.QueryPlanner:
                return(Core.Operations.ExplainVerbosity.QueryPlanner);

            default:
                var message = string.Format("Unsupported ExplainVerbosity: {0}", verbosity.ToString());
                throw new InvalidOperationException(message);
            }
        }
示例#12
0
        // private methods
        private static string ConvertVerbosityToString(ExplainVerbosity verbosity)
        {
            switch (verbosity)
            {
            case ExplainVerbosity.AllPlansExecution:
                return("allPlansExecution");

            case ExplainVerbosity.ExecutionStats:
                return("executionStats");

            case ExplainVerbosity.QueryPlanner:
                return("queryPlanner");

            default:
                var message = string.Format("Unsupported explain verbosity: {0}.", verbosity.ToString());
                throw new InvalidOperationException(message);
            }
        }
示例#13
0
        private Task <BsonDocument> ExplainFindAsync <TResult>(FindModel <TResult> model, ExplainVerbosity verbosity, TimeSpan?timeout, CancellationToken cancellationToken)
        {
            var findOperation    = CreateFindOperation <TResult>(model);
            var explainOperation = findOperation.ToExplainOperation(verbosity.ToCore());

            return(ExecuteReadOperation(explainOperation, timeout, cancellationToken));
        }
示例#14
0
        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));
        }
 /// <summary>
 /// Creates an explain operation for this cursor.
 /// </summary>
 /// <param name="verbosity">The requested <see cref="ExplainVerbosity"/>.</param>
 /// <returns>An explain operation.</returns>
 protected abstract ExplainOperation CreateExplainOperation(ExplainVerbosity verbosity);