internal async Task <CosmosQueryResponse <T> > NextResultSetAsync <T>(
            int?maxItemCount,
            string continuationToken,
            CosmosRequestOptions options,
            object state,
            CancellationToken cancellationToken)
        {
            IDocumentQueryExecutionContext documentQueryExecution = (IDocumentQueryExecutionContext)state;

            try
            {
                FeedResponse <CosmosElement> feedResponse = await documentQueryExecution.ExecuteNextAsync(cancellationToken);

                return(CosmosQueryResponse <T> .CreateResponse <T>(
                           feedResponse : feedResponse,
                           jsonSerializer : this.clientContext.JsonSerializer,
                           hasMoreResults : !documentQueryExecution.IsDone,
                           resourceType : ResourceType.Document));
            }
            catch (DocumentClientException exception)
            {
                throw new CosmosException(
                          message: exception.Message,
                          statusCode: exception.StatusCode.HasValue ? exception.StatusCode.Value : HttpStatusCode.InternalServerError,
                          subStatusCode: (int)exception.GetSubStatus(),
                          activityId: exception.ActivityId,
                          requestCharge: exception.RequestCharge);
            }
        }
Ejemplo n.º 2
0
        internal async Task <CosmosQueryResponse <T> > NextResultSetAsync <T>(
            int?maxItemCount,
            string continuationToken,
            CosmosRequestOptions options,
            object state,
            CancellationToken cancellationToken)
        {
            CosmosQueryRequestOptions cosmosQueryRequestOptions = options as CosmosQueryRequestOptions ?? new CosmosQueryRequestOptions();
            FeedOptions feedOptions = cosmosQueryRequestOptions.ToFeedOptions();

            feedOptions.RequestContinuation = continuationToken;
            feedOptions.MaxItemCount        = maxItemCount;

            IDocumentQuery <T> documentClientResult = this.client.DocumentClient.CreateDocumentQuery <T>(
                collectionLink: this.container.LinkUri.OriginalString,
                feedOptions: feedOptions,
                querySpec: state as SqlQuerySpec).AsDocumentQuery();

            try
            {
                FeedResponse <T> feedResponse = await documentClientResult.ExecuteNextAsync <T>(cancellationToken);

                return(CosmosQueryResponse <T> .CreateResponse <T>(feedResponse, feedResponse.ResponseContinuation, documentClientResult.HasMoreResults));
            }
            catch (DocumentClientException exception)
            {
                throw new CosmosException(
                          message: exception.Message,
                          statusCode: exception.StatusCode.HasValue ? exception.StatusCode.Value : HttpStatusCode.InternalServerError,
                          subStatusCode: (int)exception.GetSubStatus(),
                          activityId: exception.ActivityId,
                          requestCharge: exception.RequestCharge);
            }
        }
Ejemplo n.º 3
0
        internal static CosmosQueryResponse <TInput> CreateResponse <TInput>(
            IEnumerable <TInput> resources,
            string continuationToken,
            bool hasMoreResults)
        {
            CosmosQueryResponse <TInput> queryResponse = new CosmosQueryResponse <TInput>();

            queryResponse.SetProperties(resources, continuationToken, hasMoreResults);
            return(queryResponse);
        }
 /// <summary>
 /// Get the next set of results from the cosmos service
 /// </summary>
 /// <param name="cancellationToken">(Optional) <see cref="CancellationToken"/> representing request cancellation.</param>
 /// <returns>A query response from cosmos service</returns>
 public override Task <CosmosQueryResponse <T> > FetchNextSetAsync(CancellationToken cancellationToken = default(CancellationToken))
 {
     return(this.nextResultSetDelegate(this.MaxItemCount, this.continuationToken, this.queryOptions, this.state, cancellationToken)
            .ContinueWith(task =>
     {
         CosmosQueryResponse <T> response = task.Result;
         this.continuationToken = response.ContinuationToken;
         this.HasMoreResults = response.GetHasMoreResults();
         return response;
     }, cancellationToken));
 }
        /// <summary>
        /// Get the next set of results from the cosmos service
        /// </summary>
        /// <param name="cancellationToken">(Optional) <see cref="CancellationToken"/> representing request cancellation.</param>
        /// <returns>A query response from cosmos service</returns>
        public override Task <CosmosQueryResponse> FetchNextSetAsync(CancellationToken cancellationToken = default(CancellationToken))
        {
            cancellationToken.ThrowIfCancellationRequested();

            return(this.nextResultSetDelegate(this.continuationToken, this.state, cancellationToken)
                   .ContinueWith(task =>
            {
                CosmosQueryResponse response = task.Result;
                this.continuationToken = response.ContinuationToken;
                this.HasMoreResults = response.GetHasMoreResults();
                return response;
            }, cancellationToken));
        }
Ejemplo n.º 6
0
        internal static CosmosQueryResponse <TInput> CreateResponse <TInput>(
            Stream stream,
            CosmosJsonSerializer jsonSerializer,
            string continuationToken,
            bool hasMoreResults)
        {
            using (stream)
            {
                CosmosQueryResponse <TInput> queryResponse = new CosmosQueryResponse <TInput>()
                {
                    ContinuationToken = continuationToken,
                    HasMoreResults    = hasMoreResults
                };

                queryResponse.InitializeResource(stream, jsonSerializer);
                return(queryResponse);
            }
        }
Ejemplo n.º 7
0
        internal static CosmosQueryResponse <T> CreateCosmosQueryResponse(
            CosmosResponseMessage cosmosResponseMessage,
            CosmosJsonSerializer jsonSerializer)
        {
            using (cosmosResponseMessage)
            {
                // Throw the exception if the query failed.
                cosmosResponseMessage.EnsureSuccessStatusCode();

                string continuationToken = CosmosFeedResultSetIteratorCore.GetContinuationToken(cosmosResponseMessage);
                bool   hasMoreResults    = CosmosFeedResultSetIteratorCore.GetHasMoreResults(continuationToken, cosmosResponseMessage.StatusCode);

                return(CosmosQueryResponse <T> .CreateResponse <T>(
                           stream : cosmosResponseMessage.Content,
                           jsonSerializer : jsonSerializer,
                           continuationToken : continuationToken,
                           hasMoreResults : hasMoreResults));
            }
        }
        private async Task <CosmosQueryResponse> QueryRequestExecutor(
            string continuationToken,
            CosmosRequestOptions requestOptions,
            object state,
            CancellationToken cancellationToken)
        {
            IDocumentQueryExecutionContext documentQueryExecution = (IDocumentQueryExecutionContext)state;
            CosmosQueryRequestOptions      queryRequestOptions    = (CosmosQueryRequestOptions)requestOptions;

            // DEVNOTE: Remove try catch once query pipeline is converted to exceptionless
            try
            {
                FeedResponse <CosmosElement> feedResponse = await documentQueryExecution.ExecuteNextAsync(cancellationToken);

                return(CosmosQueryResponse.CreateResponse(
                           feedResponse: feedResponse,
                           cosmosSerializationOptions: queryRequestOptions.CosmosSerializationOptions));
            }
            catch (DocumentClientException exception)
            {
                return(new CosmosQueryResponse(
                           errorMessage: exception.Message,
                           httpStatusCode: exception.StatusCode.HasValue ? exception.StatusCode.Value : HttpStatusCode.InternalServerError,
                           retryAfter: exception.RetryAfter));
            }
            catch (AggregateException ae)
            {
                DocumentClientException exception = ae.InnerException as DocumentClientException;
                if (exception == null)
                {
                    throw;
                }

                return(new CosmosQueryResponse(
                           errorMessage: exception.Message,
                           httpStatusCode: exception.StatusCode.HasValue ? exception.StatusCode.Value : HttpStatusCode.InternalServerError,
                           retryAfter: exception.RetryAfter));
            }
        }
        internal static CosmosQueryResponse <TInput> CreateCosmosQueryFeedResponse <TInput>(
            CosmosResponseMessage cosmosResponseMessage,
            CosmosJsonSerializer jsonSerializer)
        {
            using (cosmosResponseMessage)
            {
                // Throw the exception if the query failed: EnsureSuccessStatusCode only
                // validates 200-299. 304 is valid for Changefeed so do not throw on that.
                if (cosmosResponseMessage.StatusCode != HttpStatusCode.NotModified)
                {
                    cosmosResponseMessage.EnsureSuccessStatusCode();
                }

                string continuationToken = cosmosResponseMessage.Headers.ETag;
                bool   hasMoreResults    = ChangeFeedResultSetStreamIterator.GetHasMoreResults(continuationToken, cosmosResponseMessage.StatusCode);

                return(CosmosQueryResponse <TInput> .CreateResponse <TInput>(
                           stream : cosmosResponseMessage.Content,
                           jsonSerializer : jsonSerializer,
                           continuationToken : continuationToken,
                           hasMoreResults : hasMoreResults));
            }
        }