示例#1
0
        private async Task <(IList <IDocumentQueryExecutionComponent> components, QueryResponseCore response)> GetAllExecutionComponents()
        {
            (Func <CosmosElement, Task <TryCatch <IDocumentQueryExecutionComponent> > > func, QueryResponseCore response) = this.SetupBaseContextToVerifyFailureScenario();

            List <IDocumentQueryExecutionComponent> components = new List <IDocumentQueryExecutionComponent>();
            List <AggregateOperator> operators = new List <AggregateOperator>()
            {
                AggregateOperator.Average,
                AggregateOperator.Count,
                AggregateOperator.Max,
                AggregateOperator.Min,
                AggregateOperator.Sum
            };

            components.Add((await AggregateDocumentQueryExecutionComponent.TryCreateAsync(
                                ExecutionEnvironment.Client,
                                operators.ToArray(),
                                new Dictionary <string, AggregateOperator?>()
            {
                { "test", AggregateOperator.Count }
            },
                                new List <string>()
            {
                "test"
            },
                                false,
                                null,
                                func)).Result);

            components.Add((await DistinctDocumentQueryExecutionComponent.TryCreateAsync(
                                ExecutionEnvironment.Client,
                                null,
                                func,
                                DistinctQueryType.Ordered)).Result);

            components.Add((await SkipDocumentQueryExecutionComponent.TryCreateAsync(
                                ExecutionEnvironment.Client,
                                5,
                                null,
                                func)).Result);

            components.Add((await TakeDocumentQueryExecutionComponent.TryCreateLimitDocumentQueryExecutionComponentAsync(
                                ExecutionEnvironment.Client,
                                5,
                                null,
                                func)).Result);

            components.Add((await TakeDocumentQueryExecutionComponent.TryCreateTopDocumentQueryExecutionComponentAsync(
                                ExecutionEnvironment.Client,
                                5,
                                null,
                                func)).Result);

            return(components, response);
        }
        public static async Task <TryCatch <CosmosQueryExecutionContext> > TryCreateAsync(
            ExecutionEnvironment executionEnvironment,
            CosmosQueryContext queryContext,
            CosmosCrossPartitionQueryExecutionContext.CrossPartitionInitParams initParams,
            string requestContinuationToken,
            CancellationToken cancellationToken)
        {
            if (queryContext == null)
            {
                throw new ArgumentNullException(nameof(initParams));
            }

            cancellationToken.ThrowIfCancellationRequested();

            QueryInfo queryInfo = initParams.PartitionedQueryExecutionInfo.QueryInfo;

            int initialPageSize = initParams.InitialPageSize;

            if (queryInfo.HasGroupBy)
            {
                // The query will block until all groupings are gathered so we might as well speed up the process.
                initParams = new CosmosCrossPartitionQueryExecutionContext.CrossPartitionInitParams(
                    sqlQuerySpec: initParams.SqlQuerySpec,
                    collectionRid: initParams.CollectionRid,
                    partitionedQueryExecutionInfo: initParams.PartitionedQueryExecutionInfo,
                    partitionKeyRanges: initParams.PartitionKeyRanges,
                    initialPageSize: int.MaxValue,
                    maxConcurrency: initParams.MaxConcurrency,
                    maxItemCount: int.MaxValue,
                    maxBufferedItemCount: initParams.MaxBufferedItemCount);
            }

            async Task <TryCatch <IDocumentQueryExecutionComponent> > tryCreateOrderByComponentAsync(string continuationToken)
            {
                return((await CosmosOrderByItemQueryExecutionContext.TryCreateAsync(
                            queryContext,
                            initParams,
                            continuationToken,
                            cancellationToken)).Try <IDocumentQueryExecutionComponent>(component => component));
            }

            async Task <TryCatch <IDocumentQueryExecutionComponent> > tryCreateParallelComponentAsync(string continuationToken)
            {
                return((await CosmosParallelItemQueryExecutionContext.TryCreateAsync(
                            queryContext,
                            initParams,
                            continuationToken,
                            cancellationToken)).Try <IDocumentQueryExecutionComponent>(component => component));
            }

            Func <string, Task <TryCatch <IDocumentQueryExecutionComponent> > > tryCreatePipelineAsync;

            if (queryInfo.HasOrderBy)
            {
                tryCreatePipelineAsync = tryCreateOrderByComponentAsync;
            }
            else
            {
                tryCreatePipelineAsync = tryCreateParallelComponentAsync;
            }

            if (queryInfo.HasAggregates && !queryInfo.HasGroupBy)
            {
                Func <string, Task <TryCatch <IDocumentQueryExecutionComponent> > > tryCreateSourceAsync = tryCreatePipelineAsync;
                tryCreatePipelineAsync = async(continuationToken) =>
                {
                    return(await AggregateDocumentQueryExecutionComponent.TryCreateAsync(
                               executionEnvironment,
                               queryInfo.Aggregates,
                               queryInfo.GroupByAliasToAggregateType,
                               queryInfo.GroupByAliases,
                               queryInfo.HasSelectValue,
                               continuationToken,
                               tryCreateSourceAsync));
                };
            }

            if (queryInfo.HasDistinct)
            {
                Func <string, Task <TryCatch <IDocumentQueryExecutionComponent> > > tryCreateSourceAsync = tryCreatePipelineAsync;
                tryCreatePipelineAsync = async(continuationToken) =>
                {
                    return(await DistinctDocumentQueryExecutionComponent.TryCreateAsync(
                               executionEnvironment,
                               continuationToken,
                               tryCreateSourceAsync,
                               queryInfo.DistinctType));
                };
            }

            if (queryInfo.HasGroupBy)
            {
                Func <string, Task <TryCatch <IDocumentQueryExecutionComponent> > > tryCreateSourceAsync = tryCreatePipelineAsync;
                tryCreatePipelineAsync = async(continuationToken) =>
                {
                    return(await GroupByDocumentQueryExecutionComponent.TryCreateAsync(
                               executionEnvironment,
                               continuationToken,
                               tryCreateSourceAsync,
                               queryInfo.GroupByAliasToAggregateType,
                               queryInfo.GroupByAliases,
                               queryInfo.HasSelectValue));
                };
            }

            if (queryInfo.HasOffset)
            {
                Func <string, Task <TryCatch <IDocumentQueryExecutionComponent> > > tryCreateSourceAsync = tryCreatePipelineAsync;
                tryCreatePipelineAsync = async(continuationToken) =>
                {
                    return(await SkipDocumentQueryExecutionComponent.TryCreateAsync(
                               queryInfo.Offset.Value,
                               continuationToken,
                               tryCreateSourceAsync));
                };
            }

            if (queryInfo.HasLimit)
            {
                Func <string, Task <TryCatch <IDocumentQueryExecutionComponent> > > tryCreateSourceAsync = tryCreatePipelineAsync;
                tryCreatePipelineAsync = async(continuationToken) =>
                {
                    return(await TakeDocumentQueryExecutionComponent.TryCreateLimitDocumentQueryExecutionComponentAsync(
                               queryInfo.Limit.Value,
                               continuationToken,
                               tryCreateSourceAsync));
                };
            }

            if (queryInfo.HasTop)
            {
                Func <string, Task <TryCatch <IDocumentQueryExecutionComponent> > > tryCreateSourceAsync = tryCreatePipelineAsync;
                tryCreatePipelineAsync = async(continuationToken) =>
                {
                    return(await TakeDocumentQueryExecutionComponent.TryCreateTopDocumentQueryExecutionComponentAsync(
                               queryInfo.Top.Value,
                               continuationToken,
                               tryCreateSourceAsync));
                };
            }

            return((await tryCreatePipelineAsync(requestContinuationToken))
                   .Try <CosmosQueryExecutionContext>((source) => new PipelinedDocumentQueryExecutionContext(source, initialPageSize)));
        }