public static async Task <TakeDocumentQueryExecutionComponent> CreateTopDocumentQueryExecutionComponentAsync(
            int topCount,
            string continuationToken,
            Func <string, Task <IDocumentQueryExecutionComponent> > createSourceCallback)
        {
            TopContinuationToken topContinuationToken;

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

            if (topContinuationToken.Top > topCount)
            {
                throw new BadRequestException($"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));
        }
            public static async Task <TryCatch <IDocumentQueryExecutionComponent> > TryCreateTopDocumentQueryExecutionComponentAsync(
                int topCount,
                CosmosElement requestContinuationToken,
                Func <CosmosElement, Task <TryCatch <IDocumentQueryExecutionComponent> > > tryCreateSourceAsync)
            {
                if (topCount < 0)
                {
                    throw new ArgumentException($"{nameof(topCount)}: {topCount} must be a non negative number.");
                }

                if (tryCreateSourceAsync == null)
                {
                    throw new ArgumentNullException(nameof(tryCreateSourceAsync));
                }

                TopContinuationToken topContinuationToken;

                if (requestContinuationToken != null)
                {
                    if (!TopContinuationToken.TryParse(requestContinuationToken.ToString(), out topContinuationToken))
                    {
                        return(TryCatch <IDocumentQueryExecutionComponent> .FromException(
                                   new MalformedContinuationTokenException($"Malformed {nameof(LimitContinuationToken)}: {requestContinuationToken}.")));
                    }
                }
                else
                {
                    topContinuationToken = new TopContinuationToken(topCount, null);
                }

                if (topContinuationToken.Top > topCount)
                {
                    return(TryCatch <IDocumentQueryExecutionComponent> .FromException(
                               new MalformedContinuationTokenException($"{nameof(TopContinuationToken.Top)} in {nameof(TopContinuationToken)}: {requestContinuationToken}: {topContinuationToken.Top} can not be greater than the top count in the query: {topCount}.")));
                }

                CosmosElement sourceToken;

                if (topContinuationToken.SourceToken != null)
                {
                    if (!CosmosElement.TryParse(topContinuationToken.SourceToken, out sourceToken))
                    {
                        return(TryCatch <IDocumentQueryExecutionComponent> .FromException(
                                   new MalformedContinuationTokenException($"{nameof(TopContinuationToken.SourceToken)} in {nameof(TopContinuationToken)}: {requestContinuationToken}: {topContinuationToken.SourceToken} was malformed.")));
                    }
                }
                else
                {
                    sourceToken = null;
                }

                return((await tryCreateSourceAsync(sourceToken))
                       .Try <IDocumentQueryExecutionComponent>((source) => new ClientTakeDocumentQueryExecutionComponent(
                                                                   source,
                                                                   topContinuationToken.Top,
                                                                   TakeEnum.Top)));
            }
        public override async Task <QueryResponseCore> DrainAsync(int maxElements, CancellationToken token)
        {
            token.ThrowIfCancellationRequested();
            QueryResponseCore results = await base.DrainAsync(maxElements, token);

            if (!results.IsSuccess)
            {
                return(results);
            }

            List <CosmosElement> takedDocuments = results.CosmosElements.Take(this.takeCount).ToList();

            this.takeCount -= takedDocuments.Count;
            string updatedContinuationToken = null;

            if (results.DisallowContinuationTokenMessage == null)
            {
                if (!this.IsDone)
                {
                    string sourceContinuation = results.ContinuationToken;
                    TakeContinuationToken takeContinuationToken;
                    switch (this.takeEnum)
                    {
                    case TakeEnum.Limit:
                        takeContinuationToken = new LimitContinuationToken(
                            this.takeCount,
                            sourceContinuation);
                        break;

                    case TakeEnum.Top:
                        takeContinuationToken = new TopContinuationToken(
                            this.takeCount,
                            sourceContinuation);
                        break;

                    default:
                        throw new ArgumentException($"Unknown {nameof(TakeEnum)}: {this.takeEnum}");
                    }

                    updatedContinuationToken = takeContinuationToken.ToString();
                }
            }

            return(QueryResponseCore.CreateSuccess(
                       result: takedDocuments,
                       continuationToken: updatedContinuationToken,
                       disallowContinuationTokenMessage: results.DisallowContinuationTokenMessage,
                       activityId: results.ActivityId,
                       requestCharge: results.RequestCharge,
                       queryMetricsText: results.QueryMetricsText,
                       queryMetrics: results.QueryMetrics,
                       requestStatistics: results.RequestStatistics,
                       responseLengthBytes: results.ResponseLengthBytes));
        }
        public override async Task <FeedResponse <CosmosElement> > DrainAsync(int maxElements, CancellationToken token)
        {
            FeedResponse <CosmosElement> results = await base.DrainAsync(maxElements, token);

            List <CosmosElement> takedDocuments = results.Take(this.takeCount).ToList();

            results = new FeedResponse <CosmosElement>(
                takedDocuments,
                takedDocuments.Count,
                results.Headers,
                results.UseETagAsContinuation,
                results.QueryMetrics,
                results.RequestStatistics,
                results.DisallowContinuationTokenMessage,
                results.ResponseLengthBytes);

            this.takeCount -= takedDocuments.Count;

            if (results.DisallowContinuationTokenMessage == null)
            {
                if (!this.IsDone)
                {
                    string sourceContinuation = results.ResponseContinuation;
                    TakeContinuationToken takeContinuationToken;
                    switch (this.takeEnum)
                    {
                    case TakeEnum.Limit:
                        takeContinuationToken = new LimitContinuationToken(
                            this.takeCount,
                            sourceContinuation);
                        break;

                    case TakeEnum.Top:
                        takeContinuationToken = new TopContinuationToken(
                            this.takeCount,
                            sourceContinuation);
                        break;

                    default:
                        throw new ArgumentException($"Unknown {nameof(TakeEnum)}: {takeEnum}");
                    }

                    results.ResponseContinuation = takeContinuationToken.ToString();
                }
                else
                {
                    results.ResponseContinuation = null;
                }
            }

            return(results);
        }
        public override async Task <QueryResponse> DrainAsync(int maxElements, CancellationToken token)
        {
            token.ThrowIfCancellationRequested();
            QueryResponse results = await base.DrainAsync(maxElements, token);

            if (!results.IsSuccessStatusCode)
            {
                return(results);
            }

            List <CosmosElement> takedDocuments = results.CosmosElements.Take(this.takeCount).ToList();

            this.takeCount -= takedDocuments.Count;
            string updatedContinuationToken = null;

            if (results.QueryHeaders.DisallowContinuationTokenMessage == null)
            {
                if (!this.IsDone)
                {
                    string sourceContinuation = results.Headers.ContinuationToken;
                    TakeContinuationToken takeContinuationToken;
                    switch (this.takeEnum)
                    {
                    case TakeEnum.Limit:
                        takeContinuationToken = new LimitContinuationToken(
                            this.takeCount,
                            sourceContinuation);
                        break;

                    case TakeEnum.Top:
                        takeContinuationToken = new TopContinuationToken(
                            this.takeCount,
                            sourceContinuation);
                        break;

                    default:
                        throw new ArgumentException($"Unknown {nameof(TakeEnum)}: {this.takeEnum}");
                    }

                    updatedContinuationToken = takeContinuationToken.ToString();
                }
            }

            return(QueryResponse.CreateSuccess(
                       takedDocuments,
                       takedDocuments.Count,
                       results.ResponseLengthBytes,
                       results.QueryHeaders.CloneKnownProperties(updatedContinuationToken, results.QueryHeaders.DisallowContinuationTokenMessage),
                       results.queryMetrics));
        }
コード例 #6
0
            public override async Task <QueryResponseCore> DrainAsync(int maxElements, CancellationToken token)
            {
                token.ThrowIfCancellationRequested();
                QueryResponseCore sourcePage = await base.DrainAsync(maxElements, token);

                if (!sourcePage.IsSuccess)
                {
                    return(sourcePage);
                }

                List <CosmosElement> takedDocuments = sourcePage.CosmosElements.Take(this.takeCount).ToList();

                this.takeCount -= takedDocuments.Count;

                string updatedContinuationToken;

                if (!this.IsDone && (sourcePage.DisallowContinuationTokenMessage == null))
                {
                    switch (this.takeEnum)
                    {
                    case TakeEnum.Limit:
                        updatedContinuationToken = new LimitContinuationToken(
                            limit: this.takeCount,
                            sourceToken: sourcePage.ContinuationToken).ToString();
                        break;

                    case TakeEnum.Top:
                        updatedContinuationToken = new TopContinuationToken(
                            top: this.takeCount,
                            sourceToken: sourcePage.ContinuationToken).ToString();
                        break;

                    default:
                        throw new ArgumentOutOfRangeException($"Unknown {nameof(TakeEnum)}: {this.takeEnum}.");
                    }
                }
                else
                {
                    updatedContinuationToken = null;
                }

                return(QueryResponseCore.CreateSuccess(
                           result: takedDocuments,
                           continuationToken: updatedContinuationToken,
                           disallowContinuationTokenMessage: sourcePage.DisallowContinuationTokenMessage,
                           activityId: sourcePage.ActivityId,
                           requestCharge: sourcePage.RequestCharge,
                           diagnostics: sourcePage.Diagnostics,
                           responseLengthBytes: sourcePage.ResponseLengthBytes));
            }
                /// <summary>
                /// Tries to parse out the TopContinuationToken.
                /// </summary>
                /// <param name="value">The value to parse from.</param>
                /// <param name="topContinuationToken">The result of parsing out the token.</param>
                /// <returns>Whether or not the TopContinuationToken was successfully parsed out.</returns>
                public static bool TryParse(string value, out TopContinuationToken topContinuationToken)
                {
                    topContinuationToken = default;
                    if (string.IsNullOrWhiteSpace(value))
                    {
                        return(false);
                    }

                    try
                    {
                        topContinuationToken = JsonConvert.DeserializeObject <TopContinuationToken>(value);
                        return(true);
                    }
                    catch (JsonException)
                    {
                        return(false);
                    }
                }
コード例 #8
0
        public static async Task <TopDocumentQueryExecutionComponent> CreateAsync(
            int topCount,
            string requestContinuation,
            Func <string, Task <IDocumentQueryExecutionComponent> > createSourceCallback)
        {
            TopContinuationToken continuationToken = null;

            try
            {
                if (!string.IsNullOrEmpty(requestContinuation))
                {
                    continuationToken = JsonConvert.DeserializeObject <TopContinuationToken>(requestContinuation);
                }
            }
            catch (JsonException ex)
            {
                DefaultTrace.TraceWarning(string.Format(
                                              CultureInfo.InvariantCulture,
                                              "{0} Invalid continuation token {1} for Top~Component, exception: {2}",
                                              DateTime.UtcNow.ToString("o", CultureInfo.InvariantCulture),
                                              requestContinuation,
                                              ex.Message));

                throw new BadRequestException("Invalid continuation token", ex);
            }

            string sourceContinuation = null;

            if (continuationToken != null)
            {
                if (continuationToken.Top <= 0 || continuationToken.Top > topCount)
                {
                    throw new BadRequestException("Invalid top in continuation token");
                }

                topCount = continuationToken.Top;
                if (continuationToken.SourceToken != null)
                {
                    sourceContinuation = (string)continuationToken.SourceToken.Value;
                }
            }

            return(new TopDocumentQueryExecutionComponent(await createSourceCallback(sourceContinuation), topCount));
        }
        public override bool TryGetContinuationToken(out string state)
        {
            if (!this.IsDone)
            {
                if (this.Source.TryGetContinuationToken(out string sourceState))
                {
                    TakeContinuationToken takeContinuationToken;
                    switch (this.takeEnum)
                    {
                    case TakeEnum.Limit:
                        takeContinuationToken = new LimitContinuationToken(
                            this.takeCount,
                            sourceState);
                        break;

                    case TakeEnum.Top:
                        takeContinuationToken = new TopContinuationToken(
                            this.takeCount,
                            sourceState);
                        break;

                    default:
                        throw new ArgumentException($"Unknown {nameof(TakeEnum)}: {this.takeEnum}");
                    }

                    state = takeContinuationToken.ToString();
                    return(true);
                }
                else
                {
                    state = default(string);
                    return(false);
                }
            }
            else
            {
                state = default(string);
                return(true);
            }
        }
コード例 #10
0
            /// <summary>
            /// Tries to parse out the TopContinuationToken.
            /// </summary>
            /// <param name="value">The value to parse from.</param>
            /// <param name="topContinuationToken">The result of parsing out the token.</param>
            /// <returns>Whether or not the TopContinuationToken was successfully parsed out.</returns>
            public static bool TryParse(string value, out TopContinuationToken topContinuationToken)
            {
                topContinuationToken = default(TopContinuationToken);
                if (string.IsNullOrWhiteSpace(value))
                {
                    return(false);
                }

                try
                {
                    topContinuationToken = JsonConvert.DeserializeObject <TopContinuationToken>(value);
                    return(true);
                }
                catch (JsonException ex)
                {
                    DefaultTrace.TraceWarning(string.Format(
                                                  CultureInfo.InvariantCulture,
                                                  "{0} Invalid continuation token {1} for Top~Component, exception: {2}",
                                                  DateTime.UtcNow.ToString("o", CultureInfo.InvariantCulture),
                                                  value,
                                                  ex.Message));
                    return(false);
                }
            }
            public static TryCatch <IQueryPipelineStage> MonadicCreateTopStage(
                int topCount,
                CosmosElement requestContinuationToken,
                CancellationToken cancellationToken,
                MonadicCreatePipelineStage monadicCreatePipelineStage)
            {
                if (topCount < 0)
                {
                    throw new ArgumentException($"{nameof(topCount)}: {topCount} must be a non negative number.");
                }

                if (monadicCreatePipelineStage == null)
                {
                    throw new ArgumentNullException(nameof(monadicCreatePipelineStage));
                }

                TopContinuationToken topContinuationToken;

                if (requestContinuationToken != null)
                {
                    if (!TopContinuationToken.TryParse(requestContinuationToken.ToString(), out topContinuationToken))
                    {
                        return(TryCatch <IQueryPipelineStage> .FromException(
                                   new MalformedContinuationTokenException(
                                       $"Malformed {nameof(LimitContinuationToken)}: {requestContinuationToken}.")));
                    }
                }
                else
                {
                    topContinuationToken = new TopContinuationToken(topCount, null);
                }

                if (topContinuationToken.Top > topCount)
                {
                    return(TryCatch <IQueryPipelineStage> .FromException(
                               new MalformedContinuationTokenException(
                                   $"{nameof(TopContinuationToken.Top)} in {nameof(TopContinuationToken)}: {requestContinuationToken}: {topContinuationToken.Top} can not be greater than the top count in the query: {topCount}.")));
                }

                CosmosElement sourceToken;

                if (topContinuationToken.SourceToken != null)
                {
                    TryCatch <CosmosElement> tryParse = CosmosElement.Monadic.Parse(topContinuationToken.SourceToken);
                    if (tryParse.Failed)
                    {
                        return(TryCatch <IQueryPipelineStage> .FromException(
                                   new MalformedContinuationTokenException(
                                       message : $"{nameof(TopContinuationToken.SourceToken)} in {nameof(TopContinuationToken)}: {requestContinuationToken}: {topContinuationToken.SourceToken ?? "<null>"} was malformed.",
                                       innerException : tryParse.Exception)));
                    }

                    sourceToken = tryParse.Result;
                }
                else
                {
                    sourceToken = null;
                }

                TryCatch <IQueryPipelineStage> tryCreateSource = monadicCreatePipelineStage(sourceToken, cancellationToken);

                if (tryCreateSource.Failed)
                {
                    return(tryCreateSource);
                }

                IQueryPipelineStage stage = new ClientTakeQueryPipelineStage(
                    tryCreateSource.Result,
                    cancellationToken,
                    topContinuationToken.Top,
                    TakeEnum.Top);

                return(TryCatch <IQueryPipelineStage> .FromResult(stage));
            }