internal FeedResponse <T> CreateResultSetQueryResponse <T>(
     CosmosResponseMessage cosmosResponseMessage)
 {
     return(FeedIteratorCore <T> .CreateCosmosQueryResponse(
                cosmosResponseMessage,
                this.cosmosSerializer));
 }
        internal override async Task <QueryResponse> ExecuteItemQueryAsync(
            Uri resourceUri,
            ResourceType resourceType,
            OperationType operationType,
            string containerResourceId,
            QueryRequestOptions requestOptions,
            SqlQuerySpec sqlQuerySpec,
            Action <CosmosRequestMessage> requestEnricher,
            CancellationToken cancellationToken)
        {
            CosmosResponseMessage message = await this.clientContext.ProcessResourceOperationStreamAsync(
                resourceUri : resourceUri,
                resourceType : resourceType,
                operationType : operationType,
                requestOptions : requestOptions,
                partitionKey : requestOptions.PartitionKey,
                cosmosContainerCore : this.cosmosContainerCore,
                streamPayload : this.clientContext.CosmosSerializer.ToStream <SqlQuerySpec>(sqlQuerySpec),
                requestEnricher : requestEnricher,
                cancellationToken : cancellationToken);

            return(this.GetCosmosElementResponse(
                       requestOptions,
                       resourceType,
                       containerResourceId,
                       message));
        }
 internal CosmosItemResponse <T> CreateItemResponse <T>(
     CosmosResponseMessage cosmosResponseMessage)
 {
     return(CosmosItemResponse <T> .CreateResponse <T>(
                cosmosResponseMessage,
                this.jsonSerializer));
 }
 internal async Task <T> ProcessMessageAsync <T>(Task <CosmosResponseMessage> cosmosResponseTask, Func <CosmosResponseMessage, T> createResponse)
 {
     using (CosmosResponseMessage message = await cosmosResponseTask)
     {
         return(createResponse(message));
     }
 }
 internal CosmosQueryResponse <T> CreateChangeFeedQueryResponse <T>(
     CosmosResponseMessage cosmosResponseMessage)
 {
     return(ChangeFeedResultSetIterator <T> .CreateCosmosQueryFeedResponse <T>(
                cosmosResponseMessage,
                this.jsonSerializer));
 }
Example #6
0
 public Task <ShouldRetryResult> ShouldRetryAsync(
     CosmosResponseMessage cosmosResponseMessage,
     CancellationToken cancellationToken)
 {
     // Only used for collection cache whcih doesn't participate in pipeline
     throw new NotImplementedException();
 }
        internal override async Task <PartitionedQueryExecutionInfo> ExecuteQueryPlanRequestAsync(
            Uri resourceUri,
            ResourceType resourceType,
            OperationType operationType,
            SqlQuerySpec sqlQuerySpec,
            Action <CosmosRequestMessage> requestEnricher,
            CancellationToken cancellationToken)
        {
            PartitionedQueryExecutionInfo partitionedQueryExecutionInfo;

            using (CosmosResponseMessage message = await this.clientContext.ProcessResourceOperationStreamAsync(
                       resourceUri: resourceUri,
                       resourceType: resourceType,
                       operationType: operationType,
                       requestOptions: null,
                       partitionKey: null,
                       cosmosContainerCore: this.cosmosContainerCore,
                       streamPayload: this.clientContext.CosmosSerializer.ToStream <SqlQuerySpec>(sqlQuerySpec),
                       requestEnricher: requestEnricher,
                       cancellationToken: cancellationToken))
            {
                // Syntax exception are argument exceptions and thrown to the user.
                message.EnsureSuccessStatusCode();
                partitionedQueryExecutionInfo = this.clientContext.CosmosSerializer.FromStream <PartitionedQueryExecutionInfo>(message.Content);
            }

            return(partitionedQueryExecutionInfo);
        }
 internal CosmosQueryResponse <T> CreateResultSetQueryResponse <T>(
     CosmosResponseMessage cosmosResponseMessage)
 {
     return(CosmosDefaultResultSetIterator <T> .CreateCosmosQueryResponse(
                cosmosResponseMessage,
                this.jsonSerializer));
 }
        /// <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 async Task <CosmosResponseMessage> FetchNextSetAsync(CancellationToken cancellationToken = default(CancellationToken))
        {
            CosmosResponseMessage response = await this.nextResultSetDelegate(this.MaxItemCount, this.continuationToken, this.queryOptions, this.state, cancellationToken);

            this.continuationToken = response.Headers.Continuation;
            this.HasMoreResults    = GetHasMoreResults(this.continuationToken, response.StatusCode);
            return(response);
        }
 public static Task <CosmosResponseMessage> ReturnSuccess()
 {
     return(Task.Factory.StartNew(
                () => {
         CosmosResponseMessage responseMessage = new CosmosResponseMessage(HttpStatusCode.OK);
         responseMessage.Content = new MemoryStream(Encoding.UTF8.GetBytes(@"{ ""Documents"": [{ ""id"": ""Test""}]}"));
         return responseMessage;
     }));
 }
 /// <summary>
 /// Should the caller retry the operation.
 /// </summary>
 /// <param name="cosmosResponseMessage"><see cref="CosmosResponseMessage"/> in return of the request</param>
 /// <param name="cancellationToken"></param>
 /// <returns>True indicates caller should retry, False otherwise</returns>
 public async Task <ShouldRetryResult> ShouldRetryAsync(
     CosmosResponseMessage cosmosResponseMessage,
     CancellationToken cancellationToken)
 {
     return(await this.ShouldRetryAsyncInternal(cosmosResponseMessage?.StatusCode,
                                                cosmosResponseMessage?.Headers.SubStatusCode,
                                                cancellationToken,
                                                continueIfNotHandled : null));
 }
 /// <summary>
 /// Should the caller retry the operation.
 /// </summary>
 /// <param name="cosmosResponseMessage"><see cref="CosmosResponseMessage"/> in return of the request</param>
 /// <param name="cancellationToken"></param>
 /// <returns>True indicates caller should retry, False otherwise</returns>
 public Task <ShouldRetryResult> ShouldRetryAsync(
     CosmosResponseMessage cosmosResponseMessage,
     CancellationToken cancellationToken)
 {
     return(this.ShouldRetryAsyncInternal(cosmosResponseMessage?.StatusCode,
                                          cosmosResponseMessage?.Headers.SubStatusCode,
                                          cosmosResponseMessage?.GetResourceAddress(),
                                          () => this.nextRetryPolicy.ShouldRetryAsync(cosmosResponseMessage, cancellationToken)));
 }
 internal CosmosUserDefinedFunctionResponse CreateUserDefinedFunctionResponse(
     CosmosResponseMessage cosmosResponseMessage,
     CosmosUserDefinedFunction userDefinedFunction)
 {
     return(CosmosUserDefinedFunctionResponse.CreateResponse(
                cosmosResponseMessage,
                this.jsonSerializer,
                userDefinedFunction));
 }
 internal CosmosTriggerResponse CreateTriggerResponse(
     CosmosResponseMessage cosmosResponseMessage,
     CosmosTrigger trigger)
 {
     return(CosmosTriggerResponse.CreateResponse(
                cosmosResponseMessage,
                this.jsonSerializer,
                trigger));
 }
 internal CosmosStoredProcedureResponse CreateStoredProcedureResponse(
     CosmosResponseMessage cosmosResponseMessage,
     CosmosStoredProcedure storedProcedure)
 {
     return(CosmosStoredProcedureResponse.CreateResponse(
                cosmosResponseMessage,
                this.jsonSerializer,
                storedProcedure));
 }
 internal CosmosDatabaseResponse CreateDatabaseResponse(
     CosmosResponseMessage cosmosResponseMessage,
     CosmosDatabase database)
 {
     return(CosmosDatabaseResponse.CreateResponse(
                cosmosResponseMessage,
                this.jsonSerializer,
                database));
 }
 internal CosmosContainerResponse CreateContainerResponse(
     CosmosResponseMessage cosmosResponseMessage,
     CosmosContainer container)
 {
     return(CosmosContainerResponse.CreateResponse(
                cosmosResponseMessage,
                this.jsonSerializer,
                container));
 }
Example #18
0
 /// <summary>
 /// Should the caller retry the operation.
 /// </summary>
 /// <param name="cosmosResponseMessage"><see cref="CosmosResponseMessage"/> in return of the request</param>
 /// <param name="cancellationToken"></param>
 /// <returns>True indicates caller should retry, False otherwise</returns>
 public async Task <ShouldRetryResult> ShouldRetryAsync(
     CosmosResponseMessage cosmosResponseMessage,
     CancellationToken cancellationToken)
 {
     return(await this.ShouldRetryAsyncInternal(cosmosResponseMessage?.StatusCode,
                                                cosmosResponseMessage?.Headers.SubStatusCode,
                                                cancellationToken,
                                                () => this.nextRetryPolicy?.ShouldRetryAsync(cosmosResponseMessage, cancellationToken)));
 }
 public Task <ShouldRetryResult> ShouldRetryAsync(
     CosmosResponseMessage cosmosResponseMessage,
     CancellationToken cancellationToken)
 {
     return(this.ShouldRetryInternalAsync(
                cosmosResponseMessage?.StatusCode,
                cosmosResponseMessage?.Headers.SubStatusCode,
                this.retryPolicy.ShouldRetryAsync(cosmosResponseMessage, cancellationToken),
                cancellationToken));
 }
 /// <summary>
 /// Create the cosmos item response.
 /// Creates the response object, deserializes the
 /// HTTP content stream, and disposes of the HttpResponseMessage
 /// </summary>
 /// <param name="cosmosResponseMessage"><see cref="CosmosResponseMessage"/> from the Cosmos DB service</param>
 /// <param name="jsonSerializer">The cosmos JSON serializer</param>
 internal static CosmosItemResponse <CustomResponseType> CreateResponse <CustomResponseType>(
     CosmosResponseMessage cosmosResponseMessage,
     CosmosJsonSerializer jsonSerializer)
 {
     return(CosmosResponse <CosmosContainerSettings>
            .InitResponse <CosmosItemResponse <CustomResponseType>, CustomResponseType>(
                (httpResponse) => new CosmosItemResponse <CustomResponseType>(cosmosResponseMessage),
                jsonSerializer,
                cosmosResponseMessage));
 }
 internal Task <T> MessageHelper <T>(Task <CosmosResponseMessage> cosmosResponseTask, Func <CosmosResponseMessage, T> createResponse)
 {
     return(cosmosResponseTask.ContinueWith((action) =>
     {
         using (CosmosResponseMessage message = action.Result)
         {
             return createResponse(message);
         }
     }));
 }
 /// <summary>
 /// Create the cosmos database response.
 /// Creates the response object, deserializes the
 /// http content stream, and disposes of the HttpResponseMessage
 /// </summary>
 /// <param name="cosmosResponseMessage"><see cref="CosmosResponseMessage"/> from the Cosmos DB service</param>
 /// <param name="jsonSerializer">The cosmos json serializer</param>
 /// <param name="database">The cosmos database</param>
 internal static CosmosDatabaseResponse CreateResponse(
     CosmosResponseMessage cosmosResponseMessage,
     CosmosJsonSerializer jsonSerializer,
     CosmosDatabase database)
 {
     return(CosmosResponse <CosmosDatabaseSettings>
            .InitResponse <CosmosDatabaseResponse, CosmosDatabaseSettings>(
                (httpResponse) => new CosmosDatabaseResponse(cosmosResponseMessage, database),
                jsonSerializer,
                cosmosResponseMessage));
 }
Example #23
0
 /// <summary>
 /// Create the cosmos user defined function response.
 /// Creates the response object, deserializes the
 /// HTTP content stream, and disposes of the HttpResponseMessage
 /// </summary>
 /// <param name="cosmosResponseMessage"><see cref="CosmosResponseMessage"/> from the Cosmos DB service</param>
 /// <param name="jsonSerializer">The cosmos json serializer</param>
 /// <param name="userDefinedFunction">The cosmos user defined function</param>
 internal static CosmosUserDefinedFunctionResponse CreateResponse(
     CosmosResponseMessage cosmosResponseMessage,
     CosmosJsonSerializer jsonSerializer,
     CosmosUserDefinedFunction userDefinedFunction)
 {
     return(CosmosResponse <CosmosUserDefinedFunctionSettings>
            .InitResponse <CosmosUserDefinedFunctionResponse, CosmosUserDefinedFunctionSettings>(
                (httpResponse) => new CosmosUserDefinedFunctionResponse(cosmosResponseMessage, userDefinedFunction),
                jsonSerializer,
                cosmosResponseMessage));
 }
 /// <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 <CosmosResponseMessage> FetchNextSetAsync(CancellationToken cancellationToken = default(CancellationToken))
 {
     return(this.nextResultSetDelegate(this.MaxItemCount, this.continuationToken, this.queryOptions, this.state, cancellationToken)
            .ContinueWith(task =>
     {
         CosmosResponseMessage response = task.Result;
         this.continuationToken = response.Headers.ETag;
         this.HasMoreResults = ChangeFeedResultSetStreamIterator.GetHasMoreResults(this.continuationToken, response.StatusCode);
         return response;
     }, cancellationToken));
 }
 /// <summary>
 /// Create the cosmos stored procedure response.
 /// Creates the response object, deserializes the
 /// http content stream, and disposes of the HttpResponseMessage
 /// </summary>
 /// <param name="cosmosResponseMessage"><see cref="CosmosResponseMessage"/> from the Cosmos DB service</param>
 /// <param name="jsonSerializer">The cosmos json serializer</param>
 /// <param name="storedProcedure">The cosmos stored procedure</param>
 internal static CosmosStoredProcedureResponse CreateResponse(
     CosmosResponseMessage cosmosResponseMessage,
     CosmosJsonSerializer jsonSerializer,
     CosmosStoredProcedure storedProcedure)
 {
     return(CosmosResponse <CosmosStoredProcedureSettings>
            .InitResponse <CosmosStoredProcedureResponse, CosmosStoredProcedureSettings>(
                (httpResponse) => new CosmosStoredProcedureResponse(httpResponse, storedProcedure),
                jsonSerializer,
                cosmosResponseMessage));
 }
        /// <summary>
        /// Should the caller retry the operation.
        /// </summary>
        /// <param name="cosmosResponseMessage"><see cref="CosmosResponseMessage"/> in return of the request</param>
        /// <param name="cancellationToken"></param>
        /// <returns>True indicates caller should retry, False otherwise</returns>
        public Task <ShouldRetryResult> ShouldRetryAsync(
            CosmosResponseMessage cosmosResponseMessage,
            CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            this.retryContext = null;

            return(this.ShouldRetryInternalAsync(cosmosResponseMessage?.StatusCode,
                                                 cosmosResponseMessage?.Headers.SubStatusCode,
                                                 () => this.throttlingRetry.ShouldRetryAsync(cosmosResponseMessage, cancellationToken)));
        }
 /// <summary>
 /// Create the cosmos trigger response.
 /// Creates the response object, deserializes the
 /// http content stream, and disposes of the HttpResponseMessage
 /// </summary>
 /// <param name="cosmosResponseMessage"><see cref="CosmosResponseMessage"/> from the Cosmos DB service</param>
 /// <param name="jsonSerializer">The cosmos json serializer</param>
 /// <param name="trigger">The cosmos trigger</param>
 internal static CosmosTriggerResponse CreateResponse(
     CosmosResponseMessage cosmosResponseMessage,
     CosmosJsonSerializer jsonSerializer,
     CosmosTrigger trigger)
 {
     return(CosmosResponse <CosmosTriggerSettings>
            .InitResponse <CosmosTriggerResponse, CosmosTriggerSettings>(
                (httpResponse) => new CosmosTriggerResponse(cosmosResponseMessage, trigger),
                jsonSerializer,
                cosmosResponseMessage));
 }
        /// <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 async Task <CosmosResponseMessage> FetchNextSetAsync(CancellationToken cancellationToken = default(CancellationToken))
        {
            cancellationToken.ThrowIfCancellationRequested();

            if (this.compositeContinuationToken == null)
            {
                PartitionKeyRangeCache pkRangeCache = await this.cosmosContainer.Client.DocumentClient.GetPartitionKeyRangeCacheAsync();

                this.containerRid = await this.cosmosContainer.GetRID(cancellationToken);

                this.compositeContinuationToken = await StandByFeedContinuationToken.CreateAsync(this.containerRid, this.continuationToken, pkRangeCache.TryGetOverlappingRangesAsync);
            }

            (CompositeContinuationToken currentRangeToken, string rangeId) = await this.compositeContinuationToken.GetCurrentTokenAsync();

            this.partitionKeyRangeId = rangeId;
            this.continuationToken   = currentRangeToken.Token;

            CosmosResponseMessage response = await this.NextResultSetDelegate(this.continuationToken, this.partitionKeyRangeId, this.maxItemCount, this.changeFeedOptions, cancellationToken);

            if (await this.ShouldRetryFailureAsync(response, cancellationToken))
            {
                cancellationToken.ThrowIfCancellationRequested();

                (CompositeContinuationToken currentRangeTokenForRetry, string rangeIdForRetry) = await this.compositeContinuationToken.GetCurrentTokenAsync();

                currentRangeToken        = currentRangeTokenForRetry;
                this.partitionKeyRangeId = rangeIdForRetry;
                this.continuationToken   = currentRangeToken.Token;
                response = await this.NextResultSetDelegate(this.continuationToken, this.partitionKeyRangeId, this.maxItemCount, this.changeFeedOptions, cancellationToken);
            }

            // Change Feed read uses Etag for continuation
            string responseContinuationToken = response.Headers.ETag;
            bool   hasMoreResults            = response.StatusCode != HttpStatusCode.NotModified;

            if (!hasMoreResults)
            {
                // Current Range is done, push it to the end
                this.compositeContinuationToken.MoveToNextToken();
            }
            else if (response.IsSuccessStatusCode)
            {
                currentRangeToken.Token = responseContinuationToken;
            }

            // Send to the user the composite state for all ranges
            response.Headers.Continuation = this.compositeContinuationToken.ToString();
            return(response);
        }
 /// <summary>
 /// Create the cosmos response using the custom JSON parser.
 /// This factory exist to ensure that the HttpResponseMessage is
 /// correctly disposed of, and to handle the async logic required to
 /// read the stream and to convert it to an object.
 /// </summary>
 internal static CosmosResponseType InitResponse <CosmosResponseType, CosmosSettingsType>(
     Func <CosmosResponseMessage, CosmosResponseType> createCosmosResponse,
     CosmosJsonSerializer jsonSerializer,
     CosmosResponseMessage cosmosResponseMessage,
     CosmosIdentifier cosmosSetResource = null)
     where CosmosResponseType : CosmosResponse <CosmosSettingsType>
 {
     using (cosmosResponseMessage)
     {
         CosmosResponseType cosmosResponse = createCosmosResponse(cosmosResponseMessage);
         cosmosResponse.InitializeResource(cosmosResponseMessage, jsonSerializer);
         return(cosmosResponse);
     }
 }
        public static Task <CosmosResponseMessage> ReturnStatusCode(HttpStatusCode statusCode, SubStatusCodes subStatusCode = SubStatusCodes.Unknown)
        {
            return(Task.Factory.StartNew(
                       () =>
            {
                CosmosResponseMessage httpResponse = new CosmosResponseMessage(statusCode);
                if (subStatusCode != SubStatusCodes.Unknown)
                {
                    httpResponse.Headers.Add(WFConstants.BackendHeaders.SubStatus, ((uint)subStatusCode).ToString(CultureInfo.InvariantCulture));
                }

                return httpResponse;
            }));
        }