-
Notifications
You must be signed in to change notification settings - Fork 493
/
QueryIterator.cs
132 lines (121 loc) · 6.47 KB
/
QueryIterator.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------
namespace Microsoft.Azure.Cosmos.Query
{
using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Azure.Cosmos.Query.Core;
using Microsoft.Azure.Cosmos.Query.Core.ExecutionContext;
using Microsoft.Azure.Cosmos.Query.Core.Monads;
using Microsoft.Azure.Cosmos.Query.Core.QueryClient;
using Microsoft.Azure.Cosmos.Query.Core.QueryPlan;
internal sealed class QueryIterator : FeedIteratorInternal
{
private readonly CosmosQueryContext cosmosQueryContext;
private readonly CosmosQueryExecutionContext cosmosQueryExecutionContext;
private readonly CosmosSerializationFormatOptions cosmosSerializationFormatOptions;
private QueryIterator(
CosmosQueryContext cosmosQueryContext,
CosmosQueryExecutionContext cosmosQueryExecutionContext,
CosmosSerializationFormatOptions cosmosSerializationFormatOptions)
{
this.cosmosQueryContext = cosmosQueryContext ?? throw new ArgumentNullException(nameof(cosmosQueryContext));
this.cosmosQueryExecutionContext = cosmosQueryExecutionContext ?? throw new ArgumentNullException(nameof(cosmosQueryExecutionContext));
this.cosmosSerializationFormatOptions = cosmosSerializationFormatOptions;
}
public static QueryIterator Create(
CosmosQueryClient client,
SqlQuerySpec sqlQuerySpec,
string continuationToken,
QueryRequestOptions queryRequestOptions,
Uri resourceLink,
bool isContinuationExpected,
bool allowNonValueAggregateQuery,
PartitionedQueryExecutionInfo partitionedQueryExecutionInfo)
{
if (queryRequestOptions == null)
{
queryRequestOptions = new QueryRequestOptions();
}
CosmosQueryContext cosmosQueryContext = new CosmosQueryContextCore(
client: client,
queryRequestOptions: queryRequestOptions,
resourceTypeEnum: Documents.ResourceType.Document,
operationType: Documents.OperationType.Query,
resourceType: typeof(QueryResponseCore),
resourceLink: resourceLink,
isContinuationExpected: isContinuationExpected,
allowNonValueAggregateQuery: allowNonValueAggregateQuery,
correlatedActivityId: Guid.NewGuid());
CosmosQueryExecutionContextFactory.InputParameters inputParameters = new CosmosQueryExecutionContextFactory.InputParameters(
sqlQuerySpec: sqlQuerySpec,
initialUserContinuationToken: continuationToken,
maxConcurrency: queryRequestOptions.MaxConcurrency,
maxItemCount: queryRequestOptions.MaxItemCount,
maxBufferedItemCount: queryRequestOptions.MaxBufferedItemCount,
partitionKey: queryRequestOptions.PartitionKey,
properties: queryRequestOptions.Properties,
partitionedQueryExecutionInfo: partitionedQueryExecutionInfo,
executionEnvironment: queryRequestOptions.ExecutionEnvironment,
testInjections: queryRequestOptions.TestSettings);
return new QueryIterator(
cosmosQueryContext,
CosmosQueryExecutionContextFactory.Create(cosmosQueryContext, inputParameters),
queryRequestOptions.CosmosSerializationFormatOptions);
}
public override bool HasMoreResults => !this.cosmosQueryExecutionContext.IsDone;
public override async Task<ResponseMessage> ReadNextAsync(CancellationToken cancellationToken = default)
{
// This catches exception thrown by the pipeline and converts it to QueryResponse
QueryResponseCore responseCore = await this.cosmosQueryExecutionContext.ExecuteNextAsync(cancellationToken);
CosmosQueryContext cosmosQueryContext = this.cosmosQueryContext;
QueryAggregateDiagnostics diagnostics = new QueryAggregateDiagnostics(responseCore.Diagnostics);
QueryResponse queryResponse;
if (responseCore.IsSuccess)
{
queryResponse = QueryResponse.CreateSuccess(
result: responseCore.CosmosElements,
count: responseCore.CosmosElements.Count,
responseLengthBytes: responseCore.ResponseLengthBytes,
diagnostics: diagnostics,
serializationOptions: this.cosmosSerializationFormatOptions,
responseHeaders: new CosmosQueryResponseMessageHeaders(
responseCore.ContinuationToken,
responseCore.DisallowContinuationTokenMessage,
cosmosQueryContext.ResourceTypeEnum,
cosmosQueryContext.ContainerResourceId)
{
RequestCharge = responseCore.RequestCharge,
ActivityId = responseCore.ActivityId,
SubStatusCode = responseCore.SubStatusCode ?? Documents.SubStatusCodes.Unknown
});
}
else
{
queryResponse = QueryResponse.CreateFailure(
statusCode: responseCore.StatusCode,
error: null,
errorMessage: responseCore.ErrorMessage,
requestMessage: null,
diagnostics: diagnostics,
responseHeaders: new CosmosQueryResponseMessageHeaders(
responseCore.ContinuationToken,
responseCore.DisallowContinuationTokenMessage,
cosmosQueryContext.ResourceTypeEnum,
cosmosQueryContext.ContainerResourceId)
{
RequestCharge = responseCore.RequestCharge,
ActivityId = responseCore.ActivityId,
SubStatusCode = responseCore.SubStatusCode ?? Documents.SubStatusCodes.Unknown
});
}
return queryResponse;
}
public override bool TryGetContinuationToken(out string continuationToken)
{
return this.cosmosQueryExecutionContext.TryGetContinuationToken(out continuationToken);
}
}
}