123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769 |
- 'use strict';
- const Logger = require('./connection/logger');
- const retrieveBSON = require('./connection/utils').retrieveBSON;
- const MongoError = require('./error').MongoError;
- const MongoNetworkError = require('./error').MongoNetworkError;
- const mongoErrorContextSymbol = require('./error').mongoErrorContextSymbol;
- const f = require('util').format;
- const collationNotSupported = require('./utils').collationNotSupported;
- const wireProtocol = require('./wireprotocol');
- const BSON = retrieveBSON();
- const Long = BSON.Long;
- /**
- * This is a cursor results callback
- *
- * @callback resultCallback
- * @param {error} error An error object. Set to null if no error present
- * @param {object} document
- */
- /**
- * @fileOverview The **Cursor** class is an internal class that embodies a cursor on MongoDB
- * allowing for iteration over the results returned from the underlying query.
- *
- * **CURSORS Cannot directly be instantiated**
- * @example
- * var Server = require('mongodb-core').Server
- * , ReadPreference = require('mongodb-core').ReadPreference
- * , assert = require('assert');
- *
- * var server = new Server({host: 'localhost', port: 27017});
- * // Wait for the connection event
- * server.on('connect', function(server) {
- * assert.equal(null, err);
- *
- * // Execute the write
- * var cursor = _server.cursor('integration_tests.inserts_example4', {
- * find: 'integration_tests.example4'
- * , query: {a:1}
- * }, {
- * readPreference: new ReadPreference('secondary');
- * });
- *
- * // Get the first document
- * cursor.next(function(err, doc) {
- * assert.equal(null, err);
- * server.destroy();
- * });
- * });
- *
- * // Start connecting
- * server.connect();
- */
- /**
- * Creates a new Cursor, not to be used directly
- * @class
- * @param {object} bson An instance of the BSON parser
- * @param {string} ns The MongoDB fully qualified namespace (ex: db1.collection1)
- * @param {{object}|Long} cmd The selector (can be a command or a cursorId)
- * @param {object} [options=null] Optional settings.
- * @param {object} [options.batchSize=1000] Batchsize for the operation
- * @param {array} [options.documents=[]] Initial documents list for cursor
- * @param {object} [options.transforms=null] Transform methods for the cursor results
- * @param {function} [options.transforms.query] Transform the value returned from the initial query
- * @param {function} [options.transforms.doc] Transform each document returned from Cursor.prototype.next
- * @param {object} topology The server topology instance.
- * @param {object} topologyOptions The server topology options.
- * @return {Cursor} A cursor instance
- * @property {number} cursorBatchSize The current cursorBatchSize for the cursor
- * @property {number} cursorLimit The current cursorLimit for the cursor
- * @property {number} cursorSkip The current cursorSkip for the cursor
- */
- var Cursor = function(bson, ns, cmd, options, topology, topologyOptions) {
- options = options || {};
- // Cursor pool
- this.pool = null;
- // Cursor server
- this.server = null;
- // Do we have a not connected handler
- this.disconnectHandler = options.disconnectHandler;
- // Set local values
- this.bson = bson;
- this.ns = ns;
- this.cmd = cmd;
- this.options = options;
- this.topology = topology;
- // All internal state
- this.cursorState = {
- cursorId: null,
- cmd: cmd,
- documents: options.documents || [],
- cursorIndex: 0,
- dead: false,
- killed: false,
- init: false,
- notified: false,
- limit: options.limit || cmd.limit || 0,
- skip: options.skip || cmd.skip || 0,
- batchSize: options.batchSize || cmd.batchSize || 1000,
- currentLimit: 0,
- // Result field name if not a cursor (contains the array of results)
- transforms: options.transforms,
- raw: options.raw || (cmd && cmd.raw)
- };
- if (typeof options.session === 'object') {
- this.cursorState.session = options.session;
- }
- // Add promoteLong to cursor state
- if (typeof topologyOptions.promoteLongs === 'boolean') {
- this.cursorState.promoteLongs = topologyOptions.promoteLongs;
- } else if (typeof options.promoteLongs === 'boolean') {
- this.cursorState.promoteLongs = options.promoteLongs;
- }
- // Add promoteValues to cursor state
- if (typeof topologyOptions.promoteValues === 'boolean') {
- this.cursorState.promoteValues = topologyOptions.promoteValues;
- } else if (typeof options.promoteValues === 'boolean') {
- this.cursorState.promoteValues = options.promoteValues;
- }
- // Add promoteBuffers to cursor state
- if (typeof topologyOptions.promoteBuffers === 'boolean') {
- this.cursorState.promoteBuffers = topologyOptions.promoteBuffers;
- } else if (typeof options.promoteBuffers === 'boolean') {
- this.cursorState.promoteBuffers = options.promoteBuffers;
- }
- if (topologyOptions.reconnect) {
- this.cursorState.reconnect = topologyOptions.reconnect;
- }
- // Logger
- this.logger = Logger('Cursor', topologyOptions);
- //
- // Did we pass in a cursor id
- if (typeof cmd === 'number') {
- this.cursorState.cursorId = Long.fromNumber(cmd);
- this.cursorState.lastCursorId = this.cursorState.cursorId;
- } else if (cmd instanceof Long) {
- this.cursorState.cursorId = cmd;
- this.cursorState.lastCursorId = cmd;
- }
- };
- Cursor.prototype.setCursorBatchSize = function(value) {
- this.cursorState.batchSize = value;
- };
- Cursor.prototype.cursorBatchSize = function() {
- return this.cursorState.batchSize;
- };
- Cursor.prototype.setCursorLimit = function(value) {
- this.cursorState.limit = value;
- };
- Cursor.prototype.cursorLimit = function() {
- return this.cursorState.limit;
- };
- Cursor.prototype.setCursorSkip = function(value) {
- this.cursorState.skip = value;
- };
- Cursor.prototype.cursorSkip = function() {
- return this.cursorState.skip;
- };
- Cursor.prototype._endSession = function(options, callback) {
- if (typeof options === 'function') {
- callback = options;
- options = {};
- }
- options = options || {};
- const session = this.cursorState.session;
- if (session && (options.force || session.owner === this)) {
- this.cursorState.session = undefined;
- session.endSession(callback);
- return true;
- }
- if (callback) {
- callback();
- }
- return false;
- };
- //
- // Handle callback (including any exceptions thrown)
- var handleCallback = function(callback, err, result) {
- try {
- callback(err, result);
- } catch (err) {
- process.nextTick(function() {
- throw err;
- });
- }
- };
- // Internal methods
- Cursor.prototype._getmore = function(callback) {
- if (this.logger.isDebug())
- this.logger.debug(f('schedule getMore call for query [%s]', JSON.stringify(this.query)));
- // Set the current batchSize
- var batchSize = this.cursorState.batchSize;
- if (
- this.cursorState.limit > 0 &&
- this.cursorState.currentLimit + batchSize > this.cursorState.limit
- ) {
- batchSize = this.cursorState.limit - this.cursorState.currentLimit;
- }
- wireProtocol.getMore(this.server, this.ns, this.cursorState, batchSize, this.options, callback);
- };
- /**
- * Clone the cursor
- * @method
- * @return {Cursor}
- */
- Cursor.prototype.clone = function() {
- return this.topology.cursor(this.ns, this.cmd, this.options);
- };
- /**
- * Checks if the cursor is dead
- * @method
- * @return {boolean} A boolean signifying if the cursor is dead or not
- */
- Cursor.prototype.isDead = function() {
- return this.cursorState.dead === true;
- };
- /**
- * Checks if the cursor was killed by the application
- * @method
- * @return {boolean} A boolean signifying if the cursor was killed by the application
- */
- Cursor.prototype.isKilled = function() {
- return this.cursorState.killed === true;
- };
- /**
- * Checks if the cursor notified it's caller about it's death
- * @method
- * @return {boolean} A boolean signifying if the cursor notified the callback
- */
- Cursor.prototype.isNotified = function() {
- return this.cursorState.notified === true;
- };
- /**
- * Returns current buffered documents length
- * @method
- * @return {number} The number of items in the buffered documents
- */
- Cursor.prototype.bufferedCount = function() {
- return this.cursorState.documents.length - this.cursorState.cursorIndex;
- };
- /**
- * Returns current buffered documents
- * @method
- * @return {Array} An array of buffered documents
- */
- Cursor.prototype.readBufferedDocuments = function(number) {
- var unreadDocumentsLength = this.cursorState.documents.length - this.cursorState.cursorIndex;
- var length = number < unreadDocumentsLength ? number : unreadDocumentsLength;
- var elements = this.cursorState.documents.slice(
- this.cursorState.cursorIndex,
- this.cursorState.cursorIndex + length
- );
- // Transform the doc with passed in transformation method if provided
- if (this.cursorState.transforms && typeof this.cursorState.transforms.doc === 'function') {
- // Transform all the elements
- for (var i = 0; i < elements.length; i++) {
- elements[i] = this.cursorState.transforms.doc(elements[i]);
- }
- }
- // Ensure we do not return any more documents than the limit imposed
- // Just return the number of elements up to the limit
- if (
- this.cursorState.limit > 0 &&
- this.cursorState.currentLimit + elements.length > this.cursorState.limit
- ) {
- elements = elements.slice(0, this.cursorState.limit - this.cursorState.currentLimit);
- this.kill();
- }
- // Adjust current limit
- this.cursorState.currentLimit = this.cursorState.currentLimit + elements.length;
- this.cursorState.cursorIndex = this.cursorState.cursorIndex + elements.length;
- // Return elements
- return elements;
- };
- /**
- * Kill the cursor
- * @method
- * @param {resultCallback} callback A callback function
- */
- Cursor.prototype.kill = function(callback) {
- // Set cursor to dead
- this.cursorState.dead = true;
- this.cursorState.killed = true;
- // Remove documents
- this.cursorState.documents = [];
- // If no cursor id just return
- if (
- this.cursorState.cursorId == null ||
- this.cursorState.cursorId.isZero() ||
- this.cursorState.init === false
- ) {
- if (callback) callback(null, null);
- return;
- }
- wireProtocol.killCursors(this.server, this.ns, this.cursorState, callback);
- };
- /**
- * Resets the cursor
- * @method
- * @return {null}
- */
- Cursor.prototype.rewind = function() {
- if (this.cursorState.init) {
- if (!this.cursorState.dead) {
- this.kill();
- }
- this.cursorState.currentLimit = 0;
- this.cursorState.init = false;
- this.cursorState.dead = false;
- this.cursorState.killed = false;
- this.cursorState.notified = false;
- this.cursorState.documents = [];
- this.cursorState.cursorId = null;
- this.cursorState.cursorIndex = 0;
- }
- };
- /**
- * Validate if the pool is dead and return error
- */
- var isConnectionDead = function(self, callback) {
- if (self.pool && self.pool.isDestroyed()) {
- self.cursorState.killed = true;
- const err = new MongoNetworkError(
- f('connection to host %s:%s was destroyed', self.pool.host, self.pool.port)
- );
- _setCursorNotifiedImpl(self, () => callback(err));
- return true;
- }
- return false;
- };
- /**
- * Validate if the cursor is dead but was not explicitly killed by user
- */
- var isCursorDeadButNotkilled = function(self, callback) {
- // Cursor is dead but not marked killed, return null
- if (self.cursorState.dead && !self.cursorState.killed) {
- self.cursorState.killed = true;
- setCursorNotified(self, callback);
- return true;
- }
- return false;
- };
- /**
- * Validate if the cursor is dead and was killed by user
- */
- var isCursorDeadAndKilled = function(self, callback) {
- if (self.cursorState.dead && self.cursorState.killed) {
- handleCallback(callback, new MongoError('cursor is dead'));
- return true;
- }
- return false;
- };
- /**
- * Validate if the cursor was killed by the user
- */
- var isCursorKilled = function(self, callback) {
- if (self.cursorState.killed) {
- setCursorNotified(self, callback);
- return true;
- }
- return false;
- };
- /**
- * Mark cursor as being dead and notified
- */
- var setCursorDeadAndNotified = function(self, callback) {
- self.cursorState.dead = true;
- setCursorNotified(self, callback);
- };
- /**
- * Mark cursor as being notified
- */
- var setCursorNotified = function(self, callback) {
- _setCursorNotifiedImpl(self, () => handleCallback(callback, null, null));
- };
- var _setCursorNotifiedImpl = function(self, callback) {
- self.cursorState.notified = true;
- self.cursorState.documents = [];
- self.cursorState.cursorIndex = 0;
- if (self._endSession) {
- return self._endSession(undefined, () => callback());
- }
- return callback();
- };
- var nextFunction = function(self, callback) {
- // We have notified about it
- if (self.cursorState.notified) {
- return callback(new Error('cursor is exhausted'));
- }
- // Cursor is killed return null
- if (isCursorKilled(self, callback)) return;
- // Cursor is dead but not marked killed, return null
- if (isCursorDeadButNotkilled(self, callback)) return;
- // We have a dead and killed cursor, attempting to call next should error
- if (isCursorDeadAndKilled(self, callback)) return;
- // We have just started the cursor
- if (!self.cursorState.init) {
- return initializeCursor(self, callback);
- }
- if (self.cursorState.limit > 0 && self.cursorState.currentLimit >= self.cursorState.limit) {
- // Ensure we kill the cursor on the server
- self.kill();
- // Set cursor in dead and notified state
- return setCursorDeadAndNotified(self, callback);
- } else if (
- self.cursorState.cursorIndex === self.cursorState.documents.length &&
- !Long.ZERO.equals(self.cursorState.cursorId)
- ) {
- // Ensure an empty cursor state
- self.cursorState.documents = [];
- self.cursorState.cursorIndex = 0;
- // Check if topology is destroyed
- if (self.topology.isDestroyed())
- return callback(
- new MongoNetworkError('connection destroyed, not possible to instantiate cursor')
- );
- // Check if connection is dead and return if not possible to
- // execute a getmore on this connection
- if (isConnectionDead(self, callback)) return;
- // Execute the next get more
- self._getmore(function(err, doc, connection) {
- if (err) {
- if (err instanceof MongoError) {
- err[mongoErrorContextSymbol].isGetMore = true;
- }
- return handleCallback(callback, err);
- }
- if (self.cursorState.cursorId && self.cursorState.cursorId.isZero() && self._endSession) {
- self._endSession();
- }
- // Save the returned connection to ensure all getMore's fire over the same connection
- self.connection = connection;
- // Tailable cursor getMore result, notify owner about it
- // No attempt is made here to retry, this is left to the user of the
- // core module to handle to keep core simple
- if (
- self.cursorState.documents.length === 0 &&
- self.cmd.tailable &&
- Long.ZERO.equals(self.cursorState.cursorId)
- ) {
- // No more documents in the tailed cursor
- return handleCallback(
- callback,
- new MongoError({
- message: 'No more documents in tailed cursor',
- tailable: self.cmd.tailable,
- awaitData: self.cmd.awaitData
- })
- );
- } else if (
- self.cursorState.documents.length === 0 &&
- self.cmd.tailable &&
- !Long.ZERO.equals(self.cursorState.cursorId)
- ) {
- return nextFunction(self, callback);
- }
- if (self.cursorState.limit > 0 && self.cursorState.currentLimit >= self.cursorState.limit) {
- return setCursorDeadAndNotified(self, callback);
- }
- nextFunction(self, callback);
- });
- } else if (
- self.cursorState.documents.length === self.cursorState.cursorIndex &&
- self.cmd.tailable &&
- Long.ZERO.equals(self.cursorState.cursorId)
- ) {
- return handleCallback(
- callback,
- new MongoError({
- message: 'No more documents in tailed cursor',
- tailable: self.cmd.tailable,
- awaitData: self.cmd.awaitData
- })
- );
- } else if (
- self.cursorState.documents.length === self.cursorState.cursorIndex &&
- Long.ZERO.equals(self.cursorState.cursorId)
- ) {
- setCursorDeadAndNotified(self, callback);
- } else {
- if (self.cursorState.limit > 0 && self.cursorState.currentLimit >= self.cursorState.limit) {
- // Ensure we kill the cursor on the server
- self.kill();
- // Set cursor in dead and notified state
- return setCursorDeadAndNotified(self, callback);
- }
- // Increment the current cursor limit
- self.cursorState.currentLimit += 1;
- // Get the document
- var doc = self.cursorState.documents[self.cursorState.cursorIndex++];
- // Doc overflow
- if (!doc || doc.$err) {
- // Ensure we kill the cursor on the server
- self.kill();
- // Set cursor in dead and notified state
- return setCursorDeadAndNotified(self, function() {
- handleCallback(callback, new MongoError(doc ? doc.$err : undefined));
- });
- }
- // Transform the doc with passed in transformation method if provided
- if (self.cursorState.transforms && typeof self.cursorState.transforms.doc === 'function') {
- doc = self.cursorState.transforms.doc(doc);
- }
- // Return the document
- handleCallback(callback, null, doc);
- }
- };
- function initializeCursor(cursor, callback) {
- // Topology is not connected, save the call in the provided store to be
- // Executed at some point when the handler deems it's reconnected
- if (!cursor.topology.isConnected(cursor.options)) {
- // Only need this for single server, because repl sets and mongos
- // will always continue trying to reconnect
- if (cursor.topology._type === 'server' && !cursor.topology.s.options.reconnect) {
- // Reconnect is disabled, so we'll never reconnect
- return callback(new MongoError('no connection available'));
- }
- if (cursor.disconnectHandler != null) {
- if (cursor.topology.isDestroyed()) {
- // Topology was destroyed, so don't try to wait for it to reconnect
- return callback(new MongoError('Topology was destroyed'));
- }
- return cursor.disconnectHandler.addObjectAndMethod(
- 'cursor',
- cursor,
- 'next',
- [callback],
- callback
- );
- }
- }
- // Very explicitly choose what is passed to selectServer
- const serverSelectOptions = {};
- if (cursor.cursorState.session) {
- serverSelectOptions.session = cursor.cursorState.session;
- }
- if (cursor.options.readPreference) {
- serverSelectOptions.readPreference = cursor.options.readPreference;
- }
- return cursor.topology.selectServer(serverSelectOptions, (err, server) => {
- if (err) {
- const disconnectHandler = cursor.disconnectHandler;
- if (disconnectHandler != null) {
- return disconnectHandler.addObjectAndMethod('cursor', cursor, 'next', [callback], callback);
- }
- return callback(err);
- }
- cursor.server = server;
- cursor.cursorState.init = true;
- if (collationNotSupported(cursor.server, cursor.cmd)) {
- return callback(new MongoError(`server ${cursor.server.name} does not support collation`));
- }
- function done() {
- if (
- cursor.cursorState.cursorId &&
- cursor.cursorState.cursorId.isZero() &&
- cursor._endSession
- ) {
- cursor._endSession();
- }
- if (
- cursor.cursorState.documents.length === 0 &&
- cursor.cursorState.cursorId &&
- cursor.cursorState.cursorId.isZero() &&
- !cursor.cmd.tailable &&
- !cursor.cmd.awaitData
- ) {
- return setCursorNotified(cursor, callback);
- }
- nextFunction(cursor, callback);
- }
- // NOTE: this is a special internal method for cloning a cursor, consider removing
- if (cursor.cursorState.cursorId != null) {
- return done();
- }
- const queryCallback = (err, r) => {
- if (err) return callback(err);
- const result = r.message;
- if (result.queryFailure) {
- return callback(new MongoError(result.documents[0]), null);
- }
- // Check if we have a command cursor
- if (
- Array.isArray(result.documents) &&
- result.documents.length === 1 &&
- (!cursor.cmd.find || (cursor.cmd.find && cursor.cmd.virtual === false)) &&
- (typeof result.documents[0].cursor !== 'string' ||
- result.documents[0]['$err'] ||
- result.documents[0]['errmsg'] ||
- Array.isArray(result.documents[0].result))
- ) {
- // We have an error document, return the error
- if (result.documents[0]['$err'] || result.documents[0]['errmsg']) {
- return callback(new MongoError(result.documents[0]), null);
- }
- // We have a cursor document
- if (result.documents[0].cursor != null && typeof result.documents[0].cursor !== 'string') {
- var id = result.documents[0].cursor.id;
- // If we have a namespace change set the new namespace for getmores
- if (result.documents[0].cursor.ns) {
- cursor.ns = result.documents[0].cursor.ns;
- }
- // Promote id to long if needed
- cursor.cursorState.cursorId = typeof id === 'number' ? Long.fromNumber(id) : id;
- cursor.cursorState.lastCursorId = cursor.cursorState.cursorId;
- cursor.cursorState.operationTime = result.documents[0].operationTime;
- // If we have a firstBatch set it
- if (Array.isArray(result.documents[0].cursor.firstBatch)) {
- cursor.cursorState.documents = result.documents[0].cursor.firstBatch; //.reverse();
- }
- // Return after processing command cursor
- return done(result);
- }
- if (Array.isArray(result.documents[0].result)) {
- cursor.cursorState.documents = result.documents[0].result;
- cursor.cursorState.cursorId = Long.ZERO;
- return done(result);
- }
- }
- // Otherwise fall back to regular find path
- const cursorId = result.cursorId || 0;
- cursor.cursorState.cursorId = Long.fromNumber(cursorId);
- cursor.cursorState.documents = result.documents;
- cursor.cursorState.lastCursorId = result.cursorId;
- // Transform the results with passed in transformation method if provided
- if (
- cursor.cursorState.transforms &&
- typeof cursor.cursorState.transforms.query === 'function'
- ) {
- cursor.cursorState.documents = cursor.cursorState.transforms.query(result);
- }
- // Return callback
- done(result);
- };
- if (cursor.logger.isDebug()) {
- cursor.logger.debug(
- `issue initial query [${JSON.stringify(cursor.cmd)}] with flags [${JSON.stringify(
- cursor.query
- )}]`
- );
- }
- if (cursor.cmd.find != null) {
- wireProtocol.query(
- cursor.server,
- cursor.ns,
- cursor.cmd,
- cursor.cursorState,
- cursor.options,
- queryCallback
- );
- return;
- }
- cursor.query = wireProtocol.command(
- cursor.server,
- cursor.ns,
- cursor.cmd,
- cursor.options,
- queryCallback
- );
- });
- }
- /**
- * Retrieve the next document from the cursor
- * @method
- * @param {resultCallback} callback A callback function
- */
- Cursor.prototype.next = function(callback) {
- nextFunction(this, callback);
- };
- module.exports = Cursor;
|