public override async Task <TryCatch <QueryPage> > ExecuteItemQueryAsync( string resourceUri, ResourceType resourceType, OperationType operationType, Guid clientQueryCorrelationId, QueryRequestOptions requestOptions, Action <QueryPageDiagnostics> queryPageDiagnostics, SqlQuerySpec sqlQuerySpec, string continuationToken, PartitionKeyRangeIdentity partitionKeyRange, bool isContinuationExpected, int pageSize, CancellationToken cancellationToken) { requestOptions.MaxItemCount = pageSize; ResponseMessage message = await this.clientContext.ProcessResourceOperationStreamAsync( resourceUri : resourceUri, resourceType : resourceType, operationType : operationType, requestOptions : requestOptions, partitionKey : requestOptions.PartitionKey, cosmosContainerCore : this.cosmosContainerCore, streamPayload : this.clientContext.SerializerCore.ToStreamSqlQuerySpec(sqlQuerySpec, resourceType), 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); }, diagnosticsContext : null, cancellationToken : cancellationToken); return(CosmosQueryClientCore.GetCosmosElementResponse( clientQueryCorrelationId, requestOptions, resourceType, message, partitionKeyRange, queryPageDiagnostics)); }
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)); }
private async Task <ResponseMessage> ReadNextInternalAsync( CosmosDiagnosticsContext diagnostics, CancellationToken cancellationToken = default) { cancellationToken.ThrowIfCancellationRequested(); Stream stream = null; OperationType operation = OperationType.ReadFeed; if (this.querySpec != null) { stream = this.clientContext.SerializerCore.ToStreamSqlQuerySpec(this.querySpec, this.resourceType); operation = OperationType.Query; } ResponseMessage responseMessage = await this.clientContext.ProcessResourceOperationStreamAsync( resourceUri : this.resourceLink, resourceType : this.resourceType, operationType : operation, requestOptions : this.requestOptions, cosmosContainerCore : null, partitionKey : this.requestOptions?.PartitionKey, streamPayload : stream, requestEnricher : request => { QueryRequestOptions.FillContinuationToken(request, this.ContinuationToken); if (this.querySpec != null) { request.Headers.Add(HttpConstants.HttpHeaders.ContentType, MediaTypes.QueryJson); request.Headers.Add(HttpConstants.HttpHeaders.IsQuery, bool.TrueString); } }, diagnosticsContext : diagnostics, cancellationToken : cancellationToken); this.ContinuationToken = responseMessage.Headers.ContinuationToken; this.hasMoreResultsInternal = this.ContinuationToken != null && responseMessage.StatusCode != HttpStatusCode.NotModified; if (responseMessage.Content != null) { await CosmosElementSerializer.RewriteStreamAsTextAsync(responseMessage, this.requestOptions); } return(responseMessage); }
internal override async Task <QueryResponse> ExecuteItemQueryAsync( Uri resourceUri, ResourceType resourceType, OperationType operationType, string containerResourceId, QueryRequestOptions requestOptions, SqlQuerySpec sqlQuerySpec, string continuationToken, PartitionKeyRangeIdentity partitionKeyRange, bool isContinuationExpected, int pageSize, CancellationToken cancellationToken) { ResponseMessage 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 : (cosmosRequestMessage) => { this.PopulatePartitionKeyRangeInfo(cosmosRequestMessage, partitionKeyRange); cosmosRequestMessage.Headers.Add( HttpConstants.HttpHeaders.IsContinuationExpected, isContinuationExpected.ToString()); QueryRequestOptions.FillContinuationToken( cosmosRequestMessage, continuationToken); QueryRequestOptions.FillMaxItemCount( cosmosRequestMessage, pageSize); cosmosRequestMessage.Headers.Add(HttpConstants.HttpHeaders.ContentType, MediaTypes.QueryJson); cosmosRequestMessage.Headers.Add(HttpConstants.HttpHeaders.IsQuery, bool.TrueString); }, cancellationToken : cancellationToken); return(this.GetCosmosElementResponse( requestOptions, resourceType, containerResourceId, message)); }
/// <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 <ResponseMessage> ReadNextAsync(CancellationToken cancellationToken = default(CancellationToken)) { Stream stream = null; OperationType operation = OperationType.ReadFeed; if (this.querySpec != null) { // Use property serializer is for internal query operations like throughput // that should not use custom serializer CosmosSerializer serializer = this.usePropertySerializer ? this.clientContext.PropertiesSerializer : this.clientContext.SqlQuerySpecSerializer; stream = serializer.ToStream(this.querySpec); operation = OperationType.Query; } ResponseMessage response = await this.clientContext.ProcessResourceOperationStreamAsync( resourceUri : this.resourceLink, resourceType : this.resourceType, operationType : operation, requestOptions : this.requestOptions, cosmosContainerCore : null, partitionKey : this.requestOptions?.PartitionKey, streamPayload : stream, requestEnricher : request => { QueryRequestOptions.FillContinuationToken(request, this.continuationToken); if (this.querySpec != null) { request.Headers.Add(HttpConstants.HttpHeaders.ContentType, MediaTypes.QueryJson); request.Headers.Add(HttpConstants.HttpHeaders.IsQuery, bool.TrueString); } }, cancellationToken : cancellationToken); this.continuationToken = response.Headers.ContinuationToken; this.hasMoreResultsInternal = GetHasMoreResults(this.continuationToken, response.StatusCode); return(response); }
private Task <CosmosResponseMessage> ContainerStreamFeedRequestExecutorAsync( int?maxItemCount, string continuationToken, RequestOptions requestOptions, object state, CancellationToken cancellationToken) { return(this.ClientContext.ProcessResourceOperationAsStreamAsync( resourceUri: this.database.LinkUri, resourceType: ResourceType.Collection, operationType: OperationType.ReadFeed, cosmosContainerCore: null, partitionKey: null, streamPayload: null, requestOptions: requestOptions, requestEnricher: request => { QueryRequestOptions.FillContinuationToken(request, continuationToken); QueryRequestOptions.FillMaxItemCount(request, maxItemCount); }, cancellationToken: cancellationToken)); }
private Task <FeedResponse <CosmosConflictSettings> > ConflictsFeedRequestExecutorAsync( int?maxItemCount, string continuationToken, RequestOptions options, object state, CancellationToken cancellationToken) { return(this.clientContext.ProcessResourceOperationAsync <FeedResponse <CosmosConflictSettings> >( resourceUri: this.container.LinkUri, resourceType: ResourceType.Conflict, operationType: OperationType.ReadFeed, requestOptions: options, cosmosContainerCore: this.container, partitionKey: null, streamPayload: null, requestEnricher: request => { QueryRequestOptions.FillContinuationToken(request, continuationToken); QueryRequestOptions.FillMaxItemCount(request, maxItemCount); }, responseCreator: response => this.clientContext.ResponseFactory.CreateResultSetQueryResponse <CosmosConflictSettings>(response), cancellationToken: cancellationToken)); }
private Task <ResponseMessage> ItemStreamFeedRequestExecutorAsync( int?maxItemCount, string continuationToken, RequestOptions options, object state, CancellationToken cancellationToken) { return(this.ClientContext.ProcessResourceOperationAsync <ResponseMessage>( resourceUri: this.LinkUri, resourceType: ResourceType.Document, operationType: OperationType.ReadFeed, requestOptions: options, requestEnricher: request => { QueryRequestOptions.FillContinuationToken(request, continuationToken); QueryRequestOptions.FillMaxItemCount(request, maxItemCount); }, responseCreator: response => response, cosmosContainerCore: this, partitionKey: null, streamPayload: null, cancellationToken: cancellationToken)); }
private async Task <ResponseMessage> ReadNextInternalAsync( CosmosDiagnosticsContext diagnostics, CancellationToken cancellationToken = default) { cancellationToken.ThrowIfCancellationRequested(); Stream stream = null; OperationType operation = OperationType.ReadFeed; if (this.querySpec != null) { stream = this.clientContext.SerializerCore.ToStreamSqlQuerySpec(this.querySpec, this.resourceType); operation = OperationType.Query; } ResponseMessage responseMessage = await this.clientContext.ProcessResourceOperationStreamAsync( resourceUri : this.resourceLink, resourceType : this.resourceType, operationType : operation, requestOptions : this.requestOptions, cosmosContainerCore : null, partitionKey : this.requestOptions?.PartitionKey, streamPayload : stream, requestEnricher : request => { QueryRequestOptions.FillContinuationToken(request, this.ContinuationToken); if (this.querySpec != null) { request.Headers.Add(HttpConstants.HttpHeaders.ContentType, MediaTypes.QueryJson); request.Headers.Add(HttpConstants.HttpHeaders.IsQuery, bool.TrueString); } }, diagnosticsContext : diagnostics, cancellationToken : cancellationToken); this.ContinuationToken = responseMessage.Headers.ContinuationToken; this.hasMoreResultsInternal = this.ContinuationToken != null && responseMessage.StatusCode != HttpStatusCode.NotModified; // Rewrite the payload to be in the specified format. // If it's already in the correct format, then the following will be a memcpy. MemoryStream memoryStream; if (responseMessage.Content is MemoryStream responseContentAsMemoryStream) { memoryStream = responseContentAsMemoryStream; } else { memoryStream = new MemoryStream(); await responseMessage.Content.CopyToAsync(memoryStream); } ReadOnlyMemory <byte> buffer; if (memoryStream.TryGetBuffer(out ArraySegment <byte> segment)) { buffer = segment.Array.AsMemory().Slice(start: segment.Offset, length: segment.Count); } else { buffer = memoryStream.ToArray(); } IJsonReader jsonReader = JsonReader.Create(buffer); IJsonWriter jsonWriter; if (this.requestOptions?.CosmosSerializationFormatOptions != null) { jsonWriter = this.requestOptions.CosmosSerializationFormatOptions.CreateCustomWriterCallback(); } else { jsonWriter = NewtonsoftToCosmosDBWriter.CreateTextWriter(); } jsonWriter.WriteAll(jsonReader); ReadOnlyMemory <byte> result = jsonWriter.GetResult(); MemoryStream rewrittenMemoryStream; if (MemoryMarshal.TryGetArray(result, out ArraySegment <byte> rewrittenSegment)) { rewrittenMemoryStream = new MemoryStream(rewrittenSegment.Array, index: rewrittenSegment.Offset, count: rewrittenSegment.Count); } else { rewrittenMemoryStream = new MemoryStream(result.ToArray()); } responseMessage.Content = rewrittenMemoryStream; return(responseMessage); }
private async Task<ResponseMessage> ReadNextInternalAsync( CosmosDiagnosticsContext diagnostics, CancellationToken cancellationToken = default) { cancellationToken.ThrowIfCancellationRequested(); Stream stream = null; OperationType operation = OperationType.ReadFeed; if (this.querySpec != null) { stream = this.clientContext.SerializerCore.ToStreamSqlQuerySpec(this.querySpec, this.resourceType); operation = OperationType.Query; } if (this.feedTokenInternal == null) { TryCatch<FeedTokenInternal> tryCatchFeedTokeninternal = await this.TryInitializeFeedTokenAsync(cancellationToken); if (!tryCatchFeedTokeninternal.Succeeded) { if (tryCatchFeedTokeninternal.Exception.InnerException is CosmosException cosmosException) { return cosmosException.ToCosmosResponseMessage(new RequestMessage(method: null, requestUri: null, diagnosticsContext: diagnostics)); } return CosmosExceptionFactory.CreateInternalServerErrorException( message: tryCatchFeedTokeninternal.Exception.InnerException.Message, innerException: tryCatchFeedTokeninternal.Exception.InnerException, diagnosticsContext: diagnostics).ToCosmosResponseMessage(new RequestMessage(method: null, requestUri: null, diagnosticsContext: diagnostics)); } this.feedTokenInternal = tryCatchFeedTokeninternal.Result; } ResponseMessage response = await this.clientContext.ProcessResourceOperationStreamAsync( resourceUri: this.resourceLink, resourceType: this.resourceType, operationType: operation, requestOptions: this.requestOptions, cosmosContainerCore: null, partitionKey: this.requestOptions?.PartitionKey, streamPayload: stream, requestEnricher: request => { QueryRequestOptions.FillContinuationToken(request, this.ContinuationToken); if (this.querySpec != null) { request.Headers.Add(HttpConstants.HttpHeaders.ContentType, MediaTypes.QueryJson); request.Headers.Add(HttpConstants.HttpHeaders.IsQuery, bool.TrueString); } this.feedTokenInternal?.EnrichRequest(request); }, diagnosticsContext: diagnostics, cancellationToken: cancellationToken); // Retry in case of splits or other scenarios only on partitioned resources if (this.containerCore != null && await this.feedTokenInternal.ShouldRetryAsync(this.containerCore, response, cancellationToken)) { return await this.ReadNextInternalAsync(diagnostics, cancellationToken); } if (response.IsSuccessStatusCode) { this.feedTokenInternal.UpdateContinuation(response.Headers.ContinuationToken); this.ContinuationToken = this.feedTokenInternal.GetContinuation(); this.hasMoreResultsInternal = !this.feedTokenInternal.IsDone; } else { this.hasMoreResultsInternal = false; } return response; }