-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathabstract_cursor.ts
800 lines (676 loc) · 22 KB
/
abstract_cursor.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
import { Callback, maybePromise, MongoDBNamespace, ns } from '../utils';
import { Long, Document, BSONSerializeOptions, pluckBSONSerializeOptions } from '../bson';
import { ClientSession } from '../sessions';
import { MongoError } from '../error';
import { ReadPreference, ReadPreferenceLike } from '../read_preference';
import type { Server } from '../sdam/server';
import type { Topology } from '../sdam/topology';
import { Readable, Transform } from 'stream';
import { EventEmitter } from 'events';
import type { ExecutionResult } from '../operations/execute_operation';
import { ReadConcern, ReadConcernLike } from '../read_concern';
const kId = Symbol('id');
const kDocuments = Symbol('documents');
const kServer = Symbol('server');
const kNamespace = Symbol('namespace');
const kTopology = Symbol('topology');
const kSession = Symbol('session');
const kOptions = Symbol('options');
const kTransform = Symbol('transform');
const kInitialized = Symbol('initialized');
const kClosed = Symbol('closed');
const kKilled = Symbol('killed');
/** @public */
export const CURSOR_FLAGS = [
'tailable',
'oplogReplay',
'noCursorTimeout',
'awaitData',
'exhaust',
'partial'
] as const;
/** @public */
export interface CursorCloseOptions {
/** Bypass calling killCursors when closing the cursor. */
skipKillCursors?: boolean;
}
/** @public */
export interface CursorStreamOptions {
/** A transformation method applied to each document emitted by the stream */
transform?(doc: Document): Document;
}
/** @public */
export type CursorFlag = typeof CURSOR_FLAGS[number];
/** @public */
export interface AbstractCursorOptions extends BSONSerializeOptions {
session?: ClientSession;
readPreference?: ReadPreferenceLike;
readConcern?: ReadConcernLike;
batchSize?: number;
maxTimeMS?: number;
comment?: Document | string;
tailable?: boolean;
awaitData?: boolean;
noCursorTimeout?: boolean;
}
/** @internal */
export type InternalAbstractCursorOptions = Omit<AbstractCursorOptions, 'readPreference'> & {
// resolved
readPreference: ReadPreference;
readConcern?: ReadConcern;
// cursor flags, some are deprecated
oplogReplay?: boolean;
exhaust?: boolean;
partial?: boolean;
};
/** @public */
export abstract class AbstractCursor extends EventEmitter {
/** @internal */
[kId]?: Long;
/** @internal */
[kSession]?: ClientSession;
/** @internal */
[kServer]?: Server;
/** @internal */
[kNamespace]: MongoDBNamespace;
/** @internal */
[kDocuments]: Document[];
/** @internal */
[kTopology]: Topology;
/** @internal */
[kTransform]?: (doc: Document) => Document;
/** @internal */
[kInitialized]: boolean;
/** @internal */
[kClosed]: boolean;
/** @internal */
[kKilled]: boolean;
/** @internal */
[kOptions]: InternalAbstractCursorOptions;
/** @event */
static readonly CLOSE = 'close' as const;
constructor(
topology: Topology,
namespace: MongoDBNamespace,
options: AbstractCursorOptions = {}
) {
super();
this[kTopology] = topology;
this[kNamespace] = namespace;
this[kDocuments] = []; // TODO: https://github.com/microsoft/TypeScript/issues/36230
this[kInitialized] = false;
this[kClosed] = false;
this[kKilled] = false;
this[kOptions] = {
readPreference:
options.readPreference && options.readPreference instanceof ReadPreference
? options.readPreference
: ReadPreference.primary,
...pluckBSONSerializeOptions(options)
};
const readConcern = ReadConcern.fromOptions(options);
if (readConcern) {
this[kOptions].readConcern = readConcern;
}
if (typeof options.batchSize === 'number') {
this[kOptions].batchSize = options.batchSize;
}
if (typeof options.comment !== 'undefined') {
this[kOptions].comment = options.comment;
}
if (typeof options.maxTimeMS === 'number') {
this[kOptions].maxTimeMS = options.maxTimeMS;
}
if (options.session instanceof ClientSession) {
this[kSession] = options.session;
}
}
get id(): Long | undefined {
return this[kId];
}
get topology(): Topology {
return this[kTopology];
}
get server(): Server | undefined {
return this[kServer];
}
get namespace(): MongoDBNamespace {
return this[kNamespace];
}
get readPreference(): ReadPreference {
return this[kOptions].readPreference;
}
get readConcern(): ReadConcern | undefined {
return this[kOptions].readConcern;
}
get session(): ClientSession | undefined {
return this[kSession];
}
/** @internal */
get cursorOptions(): InternalAbstractCursorOptions {
return this[kOptions];
}
get closed(): boolean {
return this[kClosed];
}
get killed(): boolean {
return this[kKilled];
}
/** Returns current buffered documents length */
bufferedCount(): number {
return this[kDocuments].length;
}
/** Returns current buffered documents */
readBufferedDocuments(number?: number): Document[] {
return this[kDocuments].splice(0, number ?? this[kDocuments].length);
}
[Symbol.asyncIterator](): AsyncIterator<Document | null> {
return {
next: () => this.next().then(value => ({ value, done: value === null }))
};
}
stream(options?: CursorStreamOptions): Readable {
if (options?.transform) {
const transform = options.transform;
const readable = makeCursorStream(this);
return readable.pipe(
new Transform({
objectMode: true,
highWaterMark: 1,
transform(chunk, _, callback) {
try {
const transformed = transform(chunk);
callback(undefined, transformed);
} catch (err) {
callback(err);
}
}
})
);
}
return makeCursorStream(this);
}
hasNext(): Promise<boolean>;
hasNext(callback: Callback<boolean>): void;
hasNext(callback?: Callback<boolean>): Promise<boolean> | void {
return maybePromise(callback, done => {
if (this[kId] === Long.ZERO) {
return done(undefined, false);
}
if (this[kDocuments].length) {
return done(undefined, true);
}
next(this, true, (err, doc) => {
if (err) return done(err);
if (doc) {
this[kDocuments].unshift(doc);
done(undefined, true);
return;
}
done(undefined, false);
});
});
}
/** Get the next available document from the cursor, returns null if no more documents are available. */
next(): Promise<Document | null>;
next(callback: Callback<Document | null>): void;
next(callback?: Callback<Document | null>): Promise<Document | null> | void {
return maybePromise(callback, done => {
if (this[kId] === Long.ZERO) {
return done(new MongoError('Cursor is exhausted'));
}
next(this, true, done);
});
}
/**
* Try to get the next available document from the cursor or `null` if an empty batch is returned
*/
tryNext(): Promise<Document | null>;
tryNext(callback: Callback<Document | null>): void;
tryNext(callback?: Callback<Document | null>): Promise<Document | null> | void {
return maybePromise(callback, done => {
if (this[kId] === Long.ZERO) {
return done(new MongoError('Cursor is exhausted'));
}
next(this, false, done);
});
}
/**
* Iterates over all the documents for this cursor using the iterator, callback pattern.
*
* @param iterator - The iteration callback.
* @param callback - The end callback.
*/
forEach(iterator: (doc: Document) => boolean | void): Promise<void>;
forEach(iterator: (doc: Document) => boolean | void, callback: Callback<void>): void;
forEach(
iterator: (doc: Document) => boolean | void,
callback?: Callback<void>
): Promise<void> | void {
if (typeof iterator !== 'function') {
throw new TypeError('Missing required parameter `iterator`');
}
return maybePromise(callback, done => {
const transform = this[kTransform];
const fetchDocs = () => {
next(this, true, (err, doc) => {
if (err || doc == null) return done(err);
if (doc == null) return done();
// NOTE: no need to transform because `next` will do this automatically
let result = iterator(doc);
if (result === false) return done();
// these do need to be transformed since they are copying the rest of the batch
const internalDocs = this[kDocuments].splice(0, this[kDocuments].length);
if (internalDocs) {
for (let i = 0; i < internalDocs.length; ++i) {
result = iterator(transform ? transform(internalDocs[i]) : internalDocs[i]);
if (result === false) return done();
}
}
fetchDocs();
});
};
fetchDocs();
});
}
close(): void;
close(callback: Callback): void;
close(options: CursorCloseOptions): Promise<void>;
close(options: CursorCloseOptions, callback: Callback): void;
close(options?: CursorCloseOptions | Callback, callback?: Callback): Promise<void> | void {
if (typeof options === 'function') (callback = options), (options = {});
options = options ?? {};
const needsToEmitClosed = !this[kClosed];
this[kClosed] = true;
return maybePromise(callback, done => {
const cursorId = this[kId];
const cursorNs = this[kNamespace];
const server = this[kServer];
const session = this[kSession];
if (cursorId == null || server == null || cursorId.isZero() || cursorNs == null) {
if (needsToEmitClosed) {
this[kId] = Long.ZERO;
this.emit(AbstractCursor.CLOSE);
}
if (session && session.owner === this) {
return session.endSession(done);
}
return done();
}
this[kKilled] = true;
server.killCursors(
cursorNs,
[cursorId],
{ ...pluckBSONSerializeOptions(this[kOptions]), session },
() => {
if (session && session.owner === this) {
return session.endSession(() => {
this.emit(AbstractCursor.CLOSE);
done();
});
}
this.emit(AbstractCursor.CLOSE);
done();
}
);
});
}
/**
* Returns an array of documents. The caller is responsible for making sure that there
* is enough memory to store the results. Note that the array only contains partial
* results when this cursor had been previously accessed. In that case,
* cursor.rewind() can be used to reset the cursor.
*
* @param callback - The result callback.
*/
toArray(): Promise<Document[]>;
toArray(callback: Callback<Document[]>): void;
toArray(callback?: Callback<Document[]>): Promise<Document[]> | void {
return maybePromise(callback, done => {
const docs: Document[] = [];
const transform = this[kTransform];
const fetchDocs = () => {
// NOTE: if we add a `nextBatch` then we should use it here
next(this, true, (err, doc) => {
if (err) return done(err);
if (doc == null) return done(undefined, docs);
// NOTE: no need to transform because `next` will do this automatically
docs.push(doc);
// these do need to be transformed since they are copying the rest of the batch
const internalDocs = transform
? this[kDocuments].splice(0, this[kDocuments].length).map(transform)
: this[kDocuments].splice(0, this[kDocuments].length);
if (internalDocs) {
docs.push(...internalDocs);
}
fetchDocs();
});
};
fetchDocs();
});
}
/**
* Add a cursor flag to the cursor
*
* @param flag - The flag to set, must be one of following ['tailable', 'oplogReplay', 'noCursorTimeout', 'awaitData', 'partial' -.
* @param value - The flag boolean value.
*/
addCursorFlag(flag: CursorFlag, value: boolean): this {
assertUninitialized(this);
if (!CURSOR_FLAGS.includes(flag)) {
throw new MongoError(`flag ${flag} is not one of ${CURSOR_FLAGS}`);
}
if (typeof value !== 'boolean') {
throw new MongoError(`flag ${flag} must be a boolean value`);
}
this[kOptions][flag] = value;
return this;
}
/**
* Map all documents using the provided function
*
* @param transform - The mapping transformation method.
*/
map(transform: (doc: Document) => Document): this {
assertUninitialized(this);
const oldTransform = this[kTransform];
if (oldTransform) {
this[kTransform] = doc => {
return transform(oldTransform(doc));
};
} else {
this[kTransform] = transform;
}
return this;
}
/**
* Set the ReadPreference for the cursor.
*
* @param readPreference - The new read preference for the cursor.
*/
withReadPreference(readPreference: ReadPreferenceLike): this {
assertUninitialized(this);
if (readPreference instanceof ReadPreference) {
this[kOptions].readPreference = readPreference;
} else if (typeof readPreference === 'string') {
this[kOptions].readPreference = ReadPreference.fromString(readPreference);
} else {
throw new TypeError('Invalid read preference: ' + readPreference);
}
return this;
}
/**
* Set the ReadPreference for the cursor.
*
* @param readPreference - The new read preference for the cursor.
*/
withReadConcern(readConcern: ReadConcernLike): this {
assertUninitialized(this);
const resolvedReadConcern = ReadConcern.fromOptions({ readConcern });
if (resolvedReadConcern) {
this[kOptions].readConcern = resolvedReadConcern;
}
return this;
}
/**
* Set a maxTimeMS on the cursor query, allowing for hard timeout limits on queries (Only supported on MongoDB 2.6 or higher)
*
* @param value - Number of milliseconds to wait before aborting the query.
*/
maxTimeMS(value: number): this {
assertUninitialized(this);
if (typeof value !== 'number') {
throw new TypeError('maxTimeMS must be a number');
}
this[kOptions].maxTimeMS = value;
return this;
}
/**
* Set the batch size for the cursor.
*
* @param value - The number of documents to return per batch. See {@link https://docs.mongodb.com/manual/reference/command/find/|find command documentation}.
*/
batchSize(value: number): this {
assertUninitialized(this);
if (this[kOptions].tailable) {
throw new MongoError('Tailable cursors do not support batchSize');
}
if (typeof value !== 'number') {
throw new TypeError('batchSize requires an integer');
}
this[kOptions].batchSize = value;
return this;
}
/**
* Rewind this cursor to its uninitialized state. Any options that are present on the cursor will
* remain in effect. Iterating this cursor will cause new queries to be sent to the server, even
* if the resultant data has already been retrieved by this cursor.
*/
rewind(): void {
if (!this[kInitialized]) {
return;
}
this[kId] = undefined;
this[kDocuments] = [];
this[kClosed] = false;
this[kKilled] = false;
this[kInitialized] = false;
const session = this[kSession];
if (session) {
// We only want to end this session if we created it, and it hasn't ended yet
if (session.explicit === false && !session.hasEnded) {
session.endSession();
}
this[kSession] = undefined;
}
}
/**
* Returns a new uninitialized copy of this cursor, with options matching those that have been set on the current instance
*/
abstract clone(): AbstractCursor;
/** @internal */
abstract _initialize(
session: ClientSession | undefined,
callback: Callback<ExecutionResult>
): void;
/** @internal */
_getMore(batchSize: number, callback: Callback<Document>): void {
const cursorId = this[kId];
const cursorNs = this[kNamespace];
const server = this[kServer];
if (cursorId == null) {
callback(new MongoError('Unable to iterate cursor with no id'));
return;
}
if (server == null) {
callback(new MongoError('Unable to iterate cursor without selected server'));
return;
}
server.getMore(
cursorNs,
cursorId,
{
...this[kOptions],
session: this[kSession],
batchSize
},
callback
);
}
}
function nextDocument(cursor: AbstractCursor): Document | null | undefined {
if (cursor[kDocuments] == null || !cursor[kDocuments].length) {
return null;
}
const doc = cursor[kDocuments].shift();
if (doc) {
const transform = cursor[kTransform];
if (transform) {
return transform(doc);
}
return doc;
}
return null;
}
function next(
cursor: AbstractCursor,
blocking: boolean,
callback: Callback<Document | null>
): void {
const cursorId = cursor[kId];
if (cursor.closed) {
return callback(undefined, null);
}
if (cursor[kDocuments] && cursor[kDocuments].length) {
callback(undefined, nextDocument(cursor));
return;
}
if (cursorId == null) {
// All cursors must operate within a session, one must be made implicitly if not explicitly provided
if (cursor[kSession] == null && cursor[kTopology].hasSessionSupport()) {
cursor[kSession] = cursor[kTopology].startSession({ owner: cursor, explicit: false });
}
cursor._initialize(cursor[kSession], (err, state) => {
if (state) {
const response = state.response;
cursor[kServer] = state.server;
cursor[kSession] = state.session;
if (response.cursor) {
cursor[kId] =
typeof response.cursor.id === 'number'
? Long.fromNumber(response.cursor.id)
: response.cursor.id;
if (response.cursor.ns) {
cursor[kNamespace] = ns(response.cursor.ns);
}
cursor[kDocuments] = response.cursor.firstBatch;
} else {
// NOTE: This is for support of older servers (<3.2) which do not use commands
cursor[kId] =
typeof response.cursorId === 'number'
? Long.fromNumber(response.cursorId)
: response.cursorId;
cursor[kDocuments] = response.documents;
}
// When server responses return without a cursor document, we close this cursor
// and return the raw server response. This is often the case for explain commands
// for example
if (cursor[kId] == null) {
cursor[kId] = Long.ZERO;
cursor[kDocuments] = [state.response];
}
}
// the cursor is now initialized, even if an error occurred or it is dead
cursor[kInitialized] = true;
if (err || cursorIsDead(cursor)) {
return cleanupCursor(cursor, () => callback(err, nextDocument(cursor)));
}
next(cursor, blocking, callback);
});
return;
}
if (cursorIsDead(cursor)) {
return cleanupCursor(cursor, () => callback(undefined, null));
}
// otherwise need to call getMore
const batchSize = cursor[kOptions].batchSize || 1000;
cursor._getMore(batchSize, (err, response) => {
if (response) {
const cursorId =
typeof response.cursor.id === 'number'
? Long.fromNumber(response.cursor.id)
: response.cursor.id;
cursor[kDocuments] = response.cursor.nextBatch;
cursor[kId] = cursorId;
}
if (err || cursorIsDead(cursor)) {
return cleanupCursor(cursor, () => callback(err, nextDocument(cursor)));
}
if (cursor[kDocuments].length === 0 && blocking === false) {
return callback(undefined, null);
}
next(cursor, blocking, callback);
});
}
function cursorIsDead(cursor: AbstractCursor): boolean {
const cursorId = cursor[kId];
return !!cursorId && cursorId.isZero();
}
function cleanupCursor(cursor: AbstractCursor, callback: Callback): void {
if (cursor[kDocuments].length === 0) {
cursor[kClosed] = true;
cursor.emit(AbstractCursor.CLOSE);
}
const session = cursor[kSession];
if (session && session.owner === cursor) {
session.endSession(callback);
} else {
callback();
}
}
/** @internal */
export function assertUninitialized(cursor: AbstractCursor): void {
if (cursor[kInitialized]) {
throw new MongoError('Cursor is already initialized');
}
}
function makeCursorStream(cursor: AbstractCursor) {
const readable = new Readable({
objectMode: true,
highWaterMark: 1
});
let initialized = false;
let reading = false;
let needToClose = true; // NOTE: we must close the cursor if we never read from it, use `_construct` in future node versions
readable._read = function () {
if (initialized === false) {
needToClose = false;
initialized = true;
}
if (!reading) {
reading = true;
readNext();
}
};
readable._destroy = function (error, cb) {
if (needToClose) {
cursor.close(err => process.nextTick(cb, err || error));
} else {
cb(error);
}
};
function readNext() {
needToClose = false;
next(cursor, true, (err, result) => {
needToClose = err ? !cursor.closed : result !== null;
if (err) {
// NOTE: This is questionable, but we have a test backing the behavior. It seems the
// desired behavior is that a stream ends cleanly when a user explicitly closes
// a client during iteration. Alternatively, we could do the "right" thing and
// propagate the error message by removing this special case.
if (err.message.match(/server is closed/)) {
cursor.close();
return readable.push(null);
}
// NOTE: This is also perhaps questionable. The rationale here is that these errors tend
// to be "operation interrupted", where a cursor has been closed but there is an
// active getMore in-flight.
if (cursor.killed) {
return readable.push(null);
}
return readable.destroy(err);
}
if (result === null) {
readable.push(null);
} else if (readable.destroyed) {
cursor.close();
} else {
if (readable.push(result)) {
return readNext();
}
reading = false;
}
});
}
return readable;
}