Exemplo n.º 1
0
        private QueryResponseCore GetCosmosElementResponse(
            QueryRequestOptions requestOptions,
            ResourceType resourceType,
            ResponseMessage cosmosResponseMessage,
            PartitionKeyRangeIdentity partitionKeyRangeIdentity,
            SchedulingStopwatch schedulingStopwatch)
        {
            using (cosmosResponseMessage)
            {
                QueryPageDiagnostics diagnostics = new QueryPageDiagnostics(
                    partitionKeyRangeId: partitionKeyRangeIdentity.PartitionKeyRangeId,
                    queryMetricText: cosmosResponseMessage.Headers.QueryMetricsText,
                    indexUtilizationText: cosmosResponseMessage.Headers[HttpConstants.HttpHeaders.IndexUtilization],
                    requestDiagnostics: cosmosResponseMessage.Diagnostics,
                    schedulingStopwatch: schedulingStopwatch);

                IReadOnlyCollection <QueryPageDiagnostics> pageDiagnostics = new List <QueryPageDiagnostics>()
                {
                    diagnostics
                };
                if (!cosmosResponseMessage.IsSuccessStatusCode)
                {
                    return(QueryResponseCore.CreateFailure(
                               statusCode: cosmosResponseMessage.StatusCode,
                               subStatusCodes: cosmosResponseMessage.Headers.SubStatusCode,
                               errorMessage: cosmosResponseMessage.ErrorMessage,
                               requestCharge: cosmosResponseMessage.Headers.RequestCharge,
                               activityId: cosmosResponseMessage.Headers.ActivityId,
                               diagnostics: pageDiagnostics));
                }

                MemoryStream memoryStream = cosmosResponseMessage.Content as MemoryStream;
                if (memoryStream == null)
                {
                    memoryStream = new MemoryStream();
                    cosmosResponseMessage.Content.CopyTo(memoryStream);
                }

                long        responseLengthBytes = memoryStream.Length;
                CosmosArray cosmosArray         = CosmosElementSerializer.ToCosmosElements(
                    memoryStream,
                    resourceType,
                    requestOptions.CosmosSerializationFormatOptions);

                int itemCount = cosmosArray.Count;
                return(QueryResponseCore.CreateSuccess(
                           result: cosmosArray,
                           requestCharge: cosmosResponseMessage.Headers.RequestCharge,
                           activityId: cosmosResponseMessage.Headers.ActivityId,
                           diagnostics: pageDiagnostics,
                           responseLengthBytes: cosmosResponseMessage.Headers.ContentLengthAsLong,
                           disallowContinuationTokenMessage: null,
                           continuationToken: cosmosResponseMessage.Headers.ContinuationToken));
            }
        }
Exemplo n.º 2
0
        internal override async Task <QueryResponseCore> ExecuteItemQueryAsync <RequestOptionType>(
            Uri resourceUri,
            ResourceType resourceType,
            OperationType operationType,
            RequestOptionType requestOptions,
            SqlQuerySpec sqlQuerySpec,
            string continuationToken,
            PartitionKeyRangeIdentity partitionKeyRange,
            bool isContinuationExpected,
            int pageSize,
            SchedulingStopwatch schedulingStopwatch,
            CancellationToken cancellationToken)
        {
            QueryRequestOptions queryRequestOptions = requestOptions as QueryRequestOptions;

            if (queryRequestOptions == null)
            {
                throw new InvalidOperationException($"CosmosQueryClientCore.ExecuteItemQueryAsync only supports RequestOptionType of QueryRequestOptions");
            }

            queryRequestOptions.MaxItemCount = pageSize;

            schedulingStopwatch.Start();
            ResponseMessage message = await this.clientContext.ProcessResourceOperationStreamAsync(
                resourceUri : resourceUri,
                resourceType : resourceType,
                operationType : operationType,
                requestOptions : queryRequestOptions,
                partitionKey : queryRequestOptions.PartitionKey,
                cosmosContainerCore : this.cosmosContainerCore,
                streamPayload : this.clientContext.SqlQuerySpecSerializer.ToStream(sqlQuerySpec),
                requestEnricher : (cosmosRequestMessage) =>
            {
                this.PopulatePartitionKeyRangeInfo(cosmosRequestMessage, partitionKeyRange);
                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);
            },
                cancellationToken : cancellationToken);

            schedulingStopwatch.Stop();

            return(this.GetCosmosElementResponse(
                       queryRequestOptions,
                       resourceType,
                       message,
                       partitionKeyRange,
                       schedulingStopwatch));
        }