|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339 |
- 'use strict';
-
- const inherits = require('util').inherits;
- const ReadPreference = require('mongodb-core').ReadPreference;
- const MongoError = require('mongodb-core').MongoError;
- const Readable = require('stream').Readable;
- const CoreCursor = require('./cursor');
- const SUPPORTS = require('./utils').SUPPORTS;
-
-
-
-
-
-
- var CommandCursor = function(bson, ns, cmd, options, topology, topologyOptions) {
- CoreCursor.apply(this, Array.prototype.slice.call(arguments, 0));
- var state = CommandCursor.INIT;
- var streamOptions = {};
-
-
- var maxTimeMS = null;
-
-
- var promiseLibrary = options.promiseLibrary || Promise;
-
-
- Readable.call(this, { objectMode: true });
-
-
- this.s = {
-
- maxTimeMS: maxTimeMS,
-
- state: state,
-
- streamOptions: streamOptions,
-
- bson: bson,
-
- ns: ns,
-
- cmd: cmd,
-
- options: options,
-
- topology: topology,
-
- topologyOptions: topologyOptions,
-
- promiseLibrary: promiseLibrary,
-
- session: options.session
- };
- };
-
-
-
-
-
-
-
-
-
-
- inherits(CommandCursor, Readable);
-
-
- var methodsToInherit = [
- '_next',
- 'next',
- 'hasNext',
- 'each',
- 'forEach',
- 'toArray',
- 'rewind',
- 'bufferedCount',
- 'readBufferedDocuments',
- 'close',
- 'isClosed',
- 'kill',
- 'setCursorBatchSize',
- '_find',
- '_getmore',
- '_killcursor',
- 'isDead',
- 'explain',
- 'isNotified',
- 'isKilled',
- '_endSession',
- '_initImplicitSession'
- ];
-
-
- for (var i = 0; i < methodsToInherit.length; i++) {
- CommandCursor.prototype[methodsToInherit[i]] = CoreCursor.prototype[methodsToInherit[i]];
- }
-
- if (SUPPORTS.ASYNC_ITERATOR) {
- CommandCursor.prototype[Symbol.asyncIterator] = require('./async/async_iterator').asyncIterator;
- }
-
-
- CommandCursor.prototype.setReadPreference = function(readPreference) {
- if (this.s.state === CommandCursor.CLOSED || this.isDead()) {
- throw MongoError.create({ message: 'Cursor is closed', driver: true });
- }
-
- if (this.s.state !== CommandCursor.INIT) {
- throw MongoError.create({
- message: 'cannot change cursor readPreference after cursor has been accessed',
- driver: true
- });
- }
-
- if (readPreference instanceof ReadPreference) {
- this.s.options.readPreference = readPreference;
- } else if (typeof readPreference === 'string') {
- this.s.options.readPreference = new ReadPreference(readPreference);
- } else {
- throw new TypeError('Invalid read preference: ' + readPreference);
- }
-
- return this;
- };
-
-
- CommandCursor.prototype.batchSize = function(value) {
- if (this.s.state === CommandCursor.CLOSED || this.isDead())
- throw MongoError.create({ message: 'Cursor is closed', driver: true });
- if (typeof value !== 'number')
- throw MongoError.create({ message: 'batchSize requires an integer', driver: true });
- if (this.s.cmd.cursor) this.s.cmd.cursor.batchSize = value;
- this.setCursorBatchSize(value);
- return this;
- };
-
-
- CommandCursor.prototype.maxTimeMS = function(value) {
- if (this.s.topology.lastIsMaster().minWireVersion > 2) {
- this.s.cmd.maxTimeMS = value;
- }
- return this;
- };
-
-
- CommandCursor.prototype.getLogger = function() {
- return this.logger;
- };
-
- CommandCursor.prototype.get = CommandCursor.prototype.toArray;
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- CommandCursor.INIT = 0;
- CommandCursor.OPEN = 1;
- CommandCursor.CLOSED = 2;
-
- module.exports = CommandCursor;
|