public static async Task <TakeDocumentQueryExecutionComponent> CreateTopDocumentQueryExecutionComponentAsync(
            CosmosQueryClient queryClient,
            int topCount,
            string continuationToken,
            Func <string, Task <IDocumentQueryExecutionComponent> > createSourceCallback)
        {
            if (queryClient == null)
            {
                throw new ArgumentNullException(nameof(queryClient));
            }

            TopContinuationToken topContinuationToken;

            if (continuationToken != null)
            {
                topContinuationToken = TopContinuationToken.Parse(queryClient, continuationToken);
            }
            else
            {
                topContinuationToken = new TopContinuationToken(topCount, null);
            }

            if (topContinuationToken.Top > topCount)
            {
                throw queryClient.CreateBadRequestException($"top count in continuation token: {topContinuationToken.Top} can not be greater than the top count in the query: {topCount}.");
            }

            return(new TakeDocumentQueryExecutionComponent(
                       await createSourceCallback(topContinuationToken.SourceToken),
                       topContinuationToken.Top,
                       TakeEnum.Top));
        }
            /// <summary>
            /// Creates an DistinctDocumentQueryExecutionComponent
            /// </summary>
            /// <param name="queryClient">The query client</param>
            /// <param name="requestContinuation">The continuation token.</param>
            /// <param name="createSourceCallback">The callback to create the source to drain from.</param>
            /// <param name="distinctQueryType">The type of distinct query.</param>
            /// <returns>A task to await on and in return </returns>
            public static async Task <DistinctDocumentQueryExecutionComponent> CreateAsync(
                CosmosQueryClient queryClient,
                string requestContinuation,
                Func <string, Task <IDocumentQueryExecutionComponent> > createSourceCallback,
                DistinctQueryType distinctQueryType)
            {
                DistinctContinuationToken distinctContinuationToken;

                if (requestContinuation != null)
                {
                    if (!DistinctContinuationToken.TryParse(requestContinuation, out distinctContinuationToken))
                    {
                        throw queryClient.CreateBadRequestException($"Invalid {nameof(DistinctContinuationToken)}: {requestContinuation}");
                    }
                }
                else
                {
                    distinctContinuationToken = new DistinctContinuationToken(sourceToken: null, distinctMapToken: null);
                }

                DistinctMap distinctMap = DistinctMap.Create(distinctQueryType, distinctContinuationToken.DistinctMapToken);
                IDocumentQueryExecutionComponent source = await createSourceCallback(distinctContinuationToken.SourceToken);

                return(new ComputeDistinctDocumentQueryExecutionComponent(
                           distinctQueryType,
                           distinctMap,
                           source));
            }
        public static async Task <TakeDocumentQueryExecutionComponent> CreateLimitDocumentQueryExecutionComponentAsync(
            CosmosQueryClient queryClient,
            int limitCount,
            string continuationToken,
            Func <string, Task <IDocumentQueryExecutionComponent> > createSourceCallback)
        {
            if (queryClient == null)
            {
                throw new ArgumentNullException(nameof(queryClient));
            }

            LimitContinuationToken limitContinuationToken;

            if (continuationToken != null)
            {
                limitContinuationToken = LimitContinuationToken.Parse(queryClient, continuationToken);
            }
            else
            {
                limitContinuationToken = new LimitContinuationToken(limitCount, null);
            }

            if (limitContinuationToken.Limit > limitCount)
            {
                throw queryClient.CreateBadRequestException($"limit count in continuation token: {limitContinuationToken.Limit} can not be greater than the limit count in the query: {limitCount}.");
            }

            return(new TakeDocumentQueryExecutionComponent(
                       await createSourceCallback(limitContinuationToken.SourceToken),
                       limitContinuationToken.Limit,
                       TakeEnum.Limit));
        }
            public static GroupingTable CreateFromContinuationToken(CosmosQueryClient cosmosQueryClient,
                                                                    IReadOnlyDictionary <string, AggregateOperator?> groupByAliasToAggregateType,
                                                                    IReadOnlyList <string> orderedAliases,
                                                                    bool hasSelectValue,
                                                                    string groupingTableContinuationToken)
            {
                GroupingTable groupingTable = new GroupingTable(
                    cosmosQueryClient,
                    groupByAliasToAggregateType,
                    orderedAliases,
                    hasSelectValue);

                if (groupingTableContinuationToken != null)
                {
                    if (!CosmosElement.TryParse(
                            groupingTableContinuationToken,
                            out CosmosObject parsedGroupingTableContinuations))
                    {
                        throw cosmosQueryClient.CreateBadRequestException($"Invalid GroupingTableContinuationToken");
                    }

                    foreach (KeyValuePair <string, CosmosElement> kvp in parsedGroupingTableContinuations)
                    {
                        string        key   = kvp.Key;
                        CosmosElement value = kvp.Value;

                        UInt128 groupByKey = UInt128.Parse(key);

                        if (!(value is CosmosString singleGroupAggregatorContinuationToken))
                        {
                            throw cosmosQueryClient.CreateBadRequestException($"Invalid GroupingTableContinuationToken");
                        }

                        SingleGroupAggregator singleGroupAggregator = SingleGroupAggregator.Create(
                            cosmosQueryClient,
                            EmptyAggregateOperators,
                            groupByAliasToAggregateType,
                            orderedAliases,
                            hasSelectValue,
                            singleGroupAggregatorContinuationToken.Value);

                        groupingTable.table[groupByKey] = singleGroupAggregator;
                    }
                }

                return(groupingTable);
            }
            /// <summary>
            /// Parses the TopContinuationToken from it's string form.
            /// </summary>
            /// <param name="queryClient">The query client</param>
            /// <param name="value">The string form to parse from.</param>
            /// <returns>The parsed TopContinuationToken.</returns>
            public static TopContinuationToken Parse(CosmosQueryClient queryClient, string value)
            {
                TopContinuationToken result;

                if (!TryParse(value, out result))
                {
                    throw queryClient.CreateBadRequestException($"Invalid TopContinuationToken: {value}");
                }
                else
                {
                    return(result);
                }
            }
            public static async Task <IDocumentQueryExecutionComponent> CreateAsync(
                CosmosQueryClient cosmosQueryClient,
                string requestContinuation,
                Func <string, Task <IDocumentQueryExecutionComponent> > createSourceCallback,
                IReadOnlyDictionary <string, AggregateOperator?> groupByAliasToAggregateType,
                IReadOnlyList <string> orderedAliases,
                bool hasSelectValue)
            {
                GroupByContinuationToken groupByContinuationToken;

                if (requestContinuation != null)
                {
                    if (!GroupByContinuationToken.TryParse(requestContinuation, out groupByContinuationToken))
                    {
                        throw cosmosQueryClient.CreateBadRequestException(
                                  $"Invalid {nameof(GroupByContinuationToken)}: '{requestContinuation}'");
                    }
                }
                else
                {
                    groupByContinuationToken = new GroupByContinuationToken(
                        groupingTableContinuationToken: null,
                        sourceContinuationToken: null);
                }

                IDocumentQueryExecutionComponent source;

                if (groupByContinuationToken.SourceContinuationToken == ComputeGroupByDocumentQueryExecutionComponent.DoneReadingGroupingsContinuationToken)
                {
                    source = DoneDocumentQueryExecutionComponent.Value;
                }
                else
                {
                    source = await createSourceCallback(groupByContinuationToken.SourceContinuationToken);
                }

                GroupingTable groupingTable = GroupingTable.CreateFromContinuationToken(
                    cosmosQueryClient,
                    groupByAliasToAggregateType,
                    orderedAliases,
                    hasSelectValue,
                    groupByContinuationToken.GroupingTableContinuationToken);

                return(new ComputeGroupByDocumentQueryExecutionComponent(
                           source,
                           groupingTable));
            }
Esempio n. 7
0
            public static async Task <IDocumentQueryExecutionComponent> CreateAsync(
                CosmosQueryClient queryClient,
                AggregateOperator[] aggregates,
                IReadOnlyDictionary <string, AggregateOperator?> aliasToAggregateType,
                IReadOnlyList <string> orderedAliases,
                bool hasSelectValue,
                string requestContinuation,
                Func <string, Task <IDocumentQueryExecutionComponent> > createSourceCallback)
            {
                string sourceContinuationToken;
                string singleGroupAggregatorContinuationToken;

                if (requestContinuation != null)
                {
                    if (!AggregateContinuationToken.TryParse(requestContinuation, out AggregateContinuationToken aggregateContinuationToken))
                    {
                        throw queryClient.CreateBadRequestException($"Malfomed {nameof(AggregateContinuationToken)}: '{requestContinuation}'");
                    }

                    sourceContinuationToken = aggregateContinuationToken.SourceContinuationToken;
                    singleGroupAggregatorContinuationToken = aggregateContinuationToken.SingleGroupAggregatorContinuationToken;
                }
                else
                {
                    sourceContinuationToken = null;
                    singleGroupAggregatorContinuationToken = null;
                }

                IDocumentQueryExecutionComponent source = await createSourceCallback(sourceContinuationToken);

                SingleGroupAggregator singleGroupAggregator = SingleGroupAggregator.Create(
                    queryClient,
                    aggregates,
                    aliasToAggregateType,
                    orderedAliases,
                    hasSelectValue,
                    singleGroupAggregatorContinuationToken);

                return(new ComputeAggregateDocumentQueryExecutionComponent(
                           source,
                           singleGroupAggregator,
                           hasSelectValue));
            }
Esempio n. 8
0
        /// <summary>
        /// Creates an DistinctDocumentQueryExecutionComponent
        /// </summary>
        /// <param name="queryClient">The query client</param>
        /// <param name="requestContinuation">The continuation token.</param>
        /// <param name="createSourceCallback">The callback to create the source to drain from.</param>
        /// <param name="distinctQueryType">The type of distinct query.</param>
        /// <returns>A task to await on and in return </returns>
        public static async Task <DistinctDocumentQueryExecutionComponent> CreateAsync(
            CosmosQueryClient queryClient,
            string requestContinuation,
            Func <string, Task <IDocumentQueryExecutionComponent> > createSourceCallback,
            DistinctQueryType distinctQueryType)
        {
            DistinctContinuationToken distinctContinuationToken = new DistinctContinuationToken(null, null);

            if (requestContinuation != null)
            {
                distinctContinuationToken = DistinctContinuationToken.Parse(queryClient, requestContinuation);
                if (distinctQueryType != DistinctQueryType.Ordered && distinctContinuationToken.LastHash != null)
                {
                    throw queryClient.CreateBadRequestException($"DistinctContinuationToken is malformed: {distinctContinuationToken}. DistinctContinuationToken can not have a 'lastHash', when the query type is not ordered (ex SELECT DISTINCT VALUE c.blah FROM c ORDER BY c.blah).");
                }
            }

            return(new DistinctDocumentQueryExecutionComponent(
                       queryClient,
                       distinctQueryType,
                       distinctContinuationToken.LastHash,
                       await createSourceCallback(distinctContinuationToken.SourceToken)));
        }