예제 #1
0
        public override FeedIterator GetItemQueryStreamIterator(
            FeedRange feedRange,
            QueryDefinition queryDefinition,
            string continuationToken           = null,
            QueryRequestOptions requestOptions = null)
        {
            FeedRangeInternal feedRangeInternal = feedRange as FeedRangeInternal;

            return(this.GetItemQueryStreamIteratorInternal(
                       sqlQuerySpec: queryDefinition?.ToSqlQuerySpec(),
                       isContinuationExcpected: true,
                       continuationToken: continuationToken,
                       feedRange: feedRangeInternal,
                       requestOptions: requestOptions));
        }
        internal static async Task <T> ProcessResourceOperationAsync <T>(
            RequestInvokerHandler requestHandler,
            string resourceUri,
            ResourceType resourceType,
            OperationType operationType,
            RequestOptions requestOptions,
            ContainerInternal cosmosContainerCore,
            FeedRange feedRange,
            Stream streamPayload,
            Action <RequestMessage> requestEnricher,
            Func <ResponseMessage, T> responseCreator,
            ITrace trace,
            CancellationToken cancellationToken)
        {
            if (requestHandler == null)
            {
                throw new ArgumentException(nameof(requestHandler));
            }

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

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

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

            ResponseMessage response = await requestHandler.SendAsync(
                resourceUri,
                resourceType,
                operationType,
                requestOptions,
                cosmosContainerCore,
                feedRange,
                streamPayload,
                requestEnricher,
                trace,
                cancellationToken);

            return(responseCreator(response));
        }
예제 #3
0
        public override async Task <IEnumerable <string> > GetPartitionKeyRangesAsync(
            FeedRange feedRange,
            CancellationToken cancellationToken = default)
        {
            IRoutingMapProvider routingMapProvider = await this.ClientContext.DocumentClient.GetPartitionKeyRangeCacheAsync();

            string containerRid = await this.GetRIDAsync(cancellationToken);

            PartitionKeyDefinition partitionKeyDefinition = await this.GetPartitionKeyDefinitionAsync(cancellationToken);

            if (!(feedRange is FeedRangeInternal feedTokenInternal))
            {
                throw new ArgumentException(nameof(feedRange), ClientResources.FeedToken_UnrecognizedFeedToken);
            }

            return(await feedTokenInternal.GetPartitionKeyRangesAsync(routingMapProvider, containerRid, partitionKeyDefinition, cancellationToken));
        }
        public override async Task <TryCatch <QueryPage> > ExecuteItemQueryAsync(
            string resourceUri,
            ResourceType resourceType,
            OperationType operationType,
            Guid clientQueryCorrelationId,
            FeedRange feedRange,
            QueryRequestOptions requestOptions,
            SqlQuerySpec sqlQuerySpec,
            string continuationToken,
            bool isContinuationExpected,
            int pageSize,
            ITrace trace,
            CancellationToken cancellationToken)
        {
            requestOptions.MaxItemCount = pageSize;

            ResponseMessage message = await this.clientContext.ProcessResourceOperationStreamAsync(
                resourceUri : resourceUri,
                resourceType : resourceType,
                operationType : operationType,
                requestOptions : requestOptions,
                feedRange : feedRange,
                cosmosContainerCore : this.cosmosContainerCore,
                streamPayload : this.clientContext.SerializerCore.ToStreamSqlQuerySpec(sqlQuerySpec, resourceType),
                requestEnricher : (cosmosRequestMessage) =>
            {
                cosmosRequestMessage.Headers.Add(
                    HttpConstants.HttpHeaders.IsContinuationExpected,
                    isContinuationExpected.ToString());
                QueryRequestOptions.FillContinuationToken(
                    cosmosRequestMessage,
                    continuationToken);
                cosmosRequestMessage.Headers.Add(HttpConstants.HttpHeaders.ContentType, MediaTypes.QueryJson);
                cosmosRequestMessage.Headers.Add(HttpConstants.HttpHeaders.IsQuery, bool.TrueString);
                cosmosRequestMessage.Headers.Add(WFConstants.BackendHeaders.CorrelatedActivityId, clientQueryCorrelationId.ToString());
            },
                trace : trace,
                cancellationToken : cancellationToken);

            return(CosmosQueryClientCore.GetCosmosElementResponse(
                       requestOptions,
                       resourceType,
                       message,
                       trace));
        }
        public override FeedIterator <T> GetItemQueryIterator <T>(
            FeedRange feedRange,
            QueryDefinition queryDefinition,
            string continuationToken           = null,
            QueryRequestOptions requestOptions = null)
        {
            requestOptions = requestOptions ?? new QueryRequestOptions();

            if (!(this.GetItemQueryStreamIterator(
                      feedRange,
                      queryDefinition,
                      continuationToken,
                      requestOptions) is FeedIteratorInternal feedIterator))
            {
                throw new InvalidOperationException($"Expected a FeedIteratorInternal.");
            }

            return(new FeedIteratorCore <T>(
                       feedIterator: feedIterator,
                       responseCreator: this.ClientContext.ResponseFactory.CreateQueryFeedUserTypeResponse <T>));
        }
예제 #6
0
 public abstract FeedIterator <T> GetItemQueryIterator <T>(
     FeedRange feedRange,
     QueryDefinition queryDefinition,
     string continuationToken           = null,
     QueryRequestOptions requestOptions = null);
예제 #7
0
 public abstract FeedIterator GetItemQueryStreamIterator(
     FeedRange feedRange,
     QueryDefinition queryDefinition,
     string continuationToken,
     QueryRequestOptions requestOptions = null);
예제 #8
0
 public abstract Task <IEnumerable <string> > GetPartitionKeyRangesAsync(
     FeedRange feedRange,
     CancellationToken cancellationToken = default(CancellationToken));
예제 #9
0
        private static TryCatch <QueryPage> GetCosmosElementResponse(
            Guid clientQueryCorrelationId,
            QueryRequestOptions requestOptions,
            ResourceType resourceType,
            ResponseMessage cosmosResponseMessage,
            FeedRange feedRange,
            Action <QueryPageDiagnostics> queryPageDiagnostics,
            ITrace trace)
        {
            using (ITrace getCosmosElementResponse = trace.StartChild("Get Cosmos Element Response", TraceComponent.Json, Tracing.TraceLevel.Info))
            {
                using (cosmosResponseMessage)
                {
                    QueryPageDiagnostics queryPage = new QueryPageDiagnostics(
                        clientQueryCorrelationId: clientQueryCorrelationId,
                        partitionKeyRangeId: feedRange is FeedRangePartitionKey feedRangePartitionKey && feedRangePartitionKey.PartitionKey.IsNone ? "None" : feedRange.ToJsonString(),
                        queryMetricText: cosmosResponseMessage.Headers.QueryMetricsText,
                        indexUtilizationText: cosmosResponseMessage.Headers[HttpConstants.HttpHeaders.IndexUtilization],
                        diagnosticsContext: cosmosResponseMessage.DiagnosticsContext);
                    queryPageDiagnostics(queryPage);

                    if (
                        cosmosResponseMessage.Headers.QueryMetricsText != null &&
                        BackendMetricsParser.TryParse(cosmosResponseMessage.Headers.QueryMetricsText, out BackendMetrics backendMetrics))
                    {
                        QueryMetricsTraceDatum datum = new QueryMetricsTraceDatum(
                            new QueryMetrics(backendMetrics, IndexUtilizationInfo.Empty, ClientSideMetrics.Empty));
                        trace.AddDatum("Query Metrics", datum);
                    }

                    if (!cosmosResponseMessage.IsSuccessStatusCode)
                    {
                        CosmosException exception;
                        if (cosmosResponseMessage.CosmosException != null)
                        {
                            exception = cosmosResponseMessage.CosmosException;
                        }
                        else
                        {
                            exception = new CosmosException(
                                cosmosResponseMessage.ErrorMessage,
                                cosmosResponseMessage.StatusCode,
                                (int)cosmosResponseMessage.Headers.SubStatusCode,
                                cosmosResponseMessage.Headers.ActivityId,
                                cosmosResponseMessage.Headers.RequestCharge);
                        }

                        return(TryCatch <QueryPage> .FromException(exception));
                    }

                    if (!(cosmosResponseMessage.Content is MemoryStream memoryStream))
                    {
                        memoryStream = new MemoryStream();
                        cosmosResponseMessage.Content.CopyTo(memoryStream);
                    }

                    long        responseLengthBytes = memoryStream.Length;
                    CosmosArray documents           = CosmosQueryClientCore.ParseElementsFromRestStream(
                        memoryStream,
                        resourceType,
                        requestOptions.CosmosSerializationFormatOptions);

                    CosmosQueryExecutionInfo cosmosQueryExecutionInfo;
                    if (cosmosResponseMessage.Headers.TryGetValue(QueryExecutionInfoHeader, out string queryExecutionInfoString))
                    {
                        cosmosQueryExecutionInfo = JsonConvert.DeserializeObject <CosmosQueryExecutionInfo>(queryExecutionInfoString);
                    }
                    else
                    {
                        cosmosQueryExecutionInfo = default;
                    }

                    QueryState queryState;
                    if (cosmosResponseMessage.Headers.ContinuationToken != null)
                    {
                        queryState = new QueryState(CosmosString.Create(cosmosResponseMessage.Headers.ContinuationToken));
                    }
                    else
                    {
                        queryState = default;
                    }

                    QueryPage response = new QueryPage(
                        documents,
                        cosmosResponseMessage.Headers.RequestCharge,
                        cosmosResponseMessage.Headers.ActivityId,
                        responseLengthBytes,
                        cosmosQueryExecutionInfo,
                        disallowContinuationTokenMessage: null,
                        queryState);

                    return(TryCatch <QueryPage> .FromResult(response));
                }
            }
        }
예제 #10
0
 public abstract FeedIterator <T> GetChangeFeedIterator <T>(
     FeedRange feedRange,
     ChangeFeedRequestOptions changeFeedRequestOptions = null);
예제 #11
0
 public abstract FeedIterator GetChangeFeedStreamIterator(
     FeedRange feedRange,
     ChangeFeedRequestOptions changeFeedRequestOptions = null);
예제 #12
0
 public override Task <IEnumerable <string> > GetPartitionKeyRangesAsync(
     FeedRange feedRange,
     CancellationToken cancellationToken = default(CancellationToken))
 {
     return(TaskHelper.RunInlineIfNeededAsync(() => this.container.GetPartitionKeyRangesAsync(feedRange, cancellationToken)));
 }
예제 #13
0
 public override FeedIterator <T> GetChangeFeedIterator <T>(
     FeedRange feedRange,
     ChangeFeedRequestOptions changeFeedRequestOptions = null)
 {
     return(this.container.GetChangeFeedIterator <T>(feedRange, changeFeedRequestOptions));
 }
예제 #14
0
 /// <summary>
 /// Initializes an instance of the <see cref="ChangeFeedStartFrom"/> class.
 /// </summary>
 internal ChangeFeedStartFrom(FeedRange feedRange)
 {
     // Internal so people can't derive from this type.
     this.FeedRange = feedRange;
 }
예제 #15
0
 public override FeedIterator GetChangeFeedStreamIterator(
     FeedRange feedRange,
     ChangeFeedRequestOptions changeFeedRequestOptions = null)
 {
     return(base.GetChangeFeedStreamIterator(feedRange, changeFeedRequestOptions));
 }