/
queryIterator.ts
277 lines (260 loc) · 8.44 KB
/
queryIterator.ts
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/// <reference lib="esnext.asynciterable" />
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
import { ClientContext } from "./ClientContext";
import { getPathFromLink, ResourceType, StatusCodes } from "./common";
import {
CosmosHeaders,
DefaultQueryExecutionContext,
ExecutionContext,
FetchFunctionCallback,
getInitialHeader,
mergeHeaders,
PipelinedQueryExecutionContext,
SqlQuerySpec
} from "./queryExecutionContext";
import { Response } from "./request";
import { ErrorResponse, PartitionedQueryExecutionInfo } from "./request/ErrorResponse";
import { FeedOptions } from "./request/FeedOptions";
import { FeedResponse } from "./request/FeedResponse";
/**
* Represents a QueryIterator Object, an implementation of feed or query response that enables
* traversal and iterating over the response
* in the Azure Cosmos DB database service.
*/
export class QueryIterator<T> {
private fetchAllTempResources: T[]; // TODO
private fetchAllLastResHeaders: CosmosHeaders;
private queryExecutionContext: ExecutionContext;
private queryPlanPromise: Promise<Response<PartitionedQueryExecutionInfo>>;
private isInitialized: boolean;
/**
* @hidden
*/
constructor(
private clientContext: ClientContext,
private query: SqlQuerySpec | string,
private options: FeedOptions,
private fetchFunctions: FetchFunctionCallback | FetchFunctionCallback[],
private resourceLink?: string,
private resourceType?: ResourceType
) {
this.query = query;
this.fetchFunctions = fetchFunctions;
this.options = options || {};
this.resourceLink = resourceLink;
this.fetchAllLastResHeaders = getInitialHeader();
this.reset();
this.isInitialized = false;
}
/**
* Gets an async iterator that will yield results until completion.
*
* NOTE: AsyncIterators are a very new feature and you might need to
* use polyfils/etc. in order to use them in your code.
*
* If you're using TypeScript, you can use the following polyfill as long
* as you target ES6 or higher and are running on Node 6 or higher.
*
* ```typescript
* if (!Symbol || !Symbol.asyncIterator) {
* (Symbol as any).asyncIterator = Symbol.for("Symbol.asyncIterator");
* }
* ```
*
* @example Iterate over all databases
* ```typescript
* for await(const {result: db} in client.databases.readAll().getAsyncIterator()) {
* console.log(`Got ${db.id} from AsyncIterator`);
* }
* ```
*/
public async *getAsyncIterator(): AsyncIterable<FeedResponse<T>> {
this.reset();
this.queryPlanPromise = this.fetchQueryPlan();
while (this.queryExecutionContext.hasMoreResults()) {
let response: Response<any>;
try {
response = await this.queryExecutionContext.fetchMore();
} catch (error) {
if (this.needsQueryPlan(error)) {
await this.createPipelinedExecutionContext();
try {
response = await this.queryExecutionContext.fetchMore();
} catch (error) {
this.handleSplitError(error);
}
} else {
throw error;
}
}
const feedResponse = new FeedResponse<T>(
response.result,
response.headers,
this.queryExecutionContext.hasMoreResults()
);
if (response.result !== undefined) {
yield feedResponse;
}
}
}
/**
* Determine if there are still remaining resources to processs based on the value of the continuation token or the\
* elements remaining on the current batch in the QueryIterator.
* @returns {Boolean} true if there is other elements to process in the QueryIterator.
*/
public hasMoreResults(): boolean {
return this.queryExecutionContext.hasMoreResults();
}
/**
* Fetch all pages for the query and return a single FeedResponse.
*/
public async fetchAll(): Promise<FeedResponse<T>> {
this.reset();
this.fetchAllTempResources = [];
let response: FeedResponse<T>;
try {
response = await this.toArrayImplementation();
} catch (error) {
this.handleSplitError(error);
}
return response;
}
/**
* Retrieve the next batch from the feed.
*
* This may or may not fetch more pages from the backend depending on your settings
* and the type of query. Aggregate queries will generally fetch all backend pages
* before returning the first batch of responses.
*/
public async fetchNext(): Promise<FeedResponse<T>> {
this.queryPlanPromise = this.fetchQueryPlan();
if (!this.isInitialized) {
await this.init();
}
let response: Response<any>;
try {
response = await this.queryExecutionContext.fetchMore();
} catch (error) {
if (this.needsQueryPlan(error)) {
await this.createPipelinedExecutionContext();
try {
response = await this.queryExecutionContext.fetchMore();
} catch (error) {
this.handleSplitError(error);
}
} else {
throw error;
}
}
return new FeedResponse<T>(
response.result,
response.headers,
this.queryExecutionContext.hasMoreResults()
);
}
/**
* Reset the QueryIterator to the beginning and clear all the resources inside it
*/
public reset() {
this.queryPlanPromise = undefined;
this.queryExecutionContext = new DefaultQueryExecutionContext(
this.options,
this.fetchFunctions
);
}
private async toArrayImplementation(): Promise<FeedResponse<T>> {
this.queryPlanPromise = this.fetchQueryPlan();
if (!this.isInitialized) {
await this.init();
}
while (this.queryExecutionContext.hasMoreResults()) {
let response: Response<any>;
try {
response = await this.queryExecutionContext.nextItem();
} catch (error) {
if (this.needsQueryPlan(error)) {
await this.createPipelinedExecutionContext();
response = await this.queryExecutionContext.nextItem();
} else {
throw error;
}
}
const { result, headers } = response;
// concatenate the results and fetch more
mergeHeaders(this.fetchAllLastResHeaders, headers);
if (result !== undefined) {
this.fetchAllTempResources.push(result);
}
}
return new FeedResponse(
this.fetchAllTempResources,
this.fetchAllLastResHeaders,
this.queryExecutionContext.hasMoreResults()
);
}
private async createPipelinedExecutionContext() {
const queryPlanResponse = await this.queryPlanPromise;
// We always coerce queryPlanPromise to resolved. So if it errored, we need to manually inspect the resolved value
if (queryPlanResponse instanceof Error) {
throw queryPlanResponse;
}
const queryPlan = queryPlanResponse.result;
const queryInfo = queryPlan.queryInfo;
if (queryInfo.aggregates.length > 0 && queryInfo.hasSelectValue === false) {
throw new Error("Aggregate queries must use the VALUE keyword");
}
this.queryExecutionContext = new PipelinedQueryExecutionContext(
this.clientContext,
this.resourceLink,
this.query,
this.options,
queryPlan
);
}
private async fetchQueryPlan() {
if (!this.queryPlanPromise && this.resourceType === ResourceType.item) {
return this.clientContext
.getQueryPlan(
getPathFromLink(this.resourceLink) + "/docs",
ResourceType.item,
this.resourceLink,
this.query,
this.options
)
.catch((error: any) => error); // Without this catch, node reports an unhandled rejection. So we stash the promise as resolved even if it errored.
}
return this.queryPlanPromise;
}
private needsQueryPlan(error: any): error is ErrorResponse {
return error.code === StatusCodes.BadRequest;
}
private initPromise: Promise<void>;
private async init() {
if (this.isInitialized === true) {
return;
}
if (this.initPromise === undefined) {
this.initPromise = this._init();
}
return this.initPromise;
}
private async _init() {
if (this.options.forceQueryPlan === true && this.resourceType === ResourceType.item) {
await this.createPipelinedExecutionContext();
}
this.isInitialized = true;
}
private handleSplitError(err: any) {
if (err.code === 410) {
const error = new Error(
"Encountered partition split and could not recover. This request is retryable"
) as any;
error.code = 503;
error.originalError = err;
throw error;
} else {
throw err;
}
}
}