command_cursor.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. "use strict";
  2. var inherits = require('util').inherits
  3. , f = require('util').format
  4. , toError = require('./utils').toError
  5. , getSingleProperty = require('./utils').getSingleProperty
  6. , formattedOrderClause = require('./utils').formattedOrderClause
  7. , handleCallback = require('./utils').handleCallback
  8. , Logger = require('mongodb-core').Logger
  9. , EventEmitter = require('events').EventEmitter
  10. , ReadPreference = require('./read_preference')
  11. , MongoError = require('mongodb-core').MongoError
  12. , Readable = require('stream').Readable || require('readable-stream').Readable
  13. // , CoreCursor = require('mongodb-core').Cursor
  14. , CoreCursor = require('./cursor')
  15. , Query = require('mongodb-core').Query
  16. , CoreReadPreference = require('mongodb-core').ReadPreference;
  17. /**
  18. * @fileOverview The **CommandCursor** class is an internal class that embodies a
  19. * generalized cursor based on a MongoDB command allowing for iteration over the
  20. * results returned. It supports one by one document iteration, conversion to an
  21. * array or can be iterated as a Node 0.10.X or higher stream
  22. *
  23. * **CommandCursor Cannot directly be instantiated**
  24. * @example
  25. * var MongoClient = require('mongodb').MongoClient,
  26. * test = require('assert');
  27. * // Connection url
  28. * var url = 'mongodb://localhost:27017/test';
  29. * // Connect using MongoClient
  30. * MongoClient.connect(url, function(err, db) {
  31. * // Create a collection we want to drop later
  32. * var col = db.collection('listCollectionsExample1');
  33. * // Insert a bunch of documents
  34. * col.insert([{a:1, b:1}
  35. * , {a:2, b:2}, {a:3, b:3}
  36. * , {a:4, b:4}], {w:1}, function(err, result) {
  37. * test.equal(null, err);
  38. *
  39. * // List the database collections available
  40. * db.listCollections().toArray(function(err, items) {
  41. * test.equal(null, err);
  42. * db.close();
  43. * });
  44. * });
  45. * });
  46. */
  47. /**
  48. * Namespace provided by the browser.
  49. * @external Readable
  50. */
  51. /**
  52. * Creates a new Command Cursor instance (INTERNAL TYPE, do not instantiate directly)
  53. * @class CommandCursor
  54. * @extends external:Readable
  55. * @fires CommandCursor#data
  56. * @fires CommandCursor#end
  57. * @fires CommandCursor#close
  58. * @fires CommandCursor#readable
  59. * @return {CommandCursor} an CommandCursor instance.
  60. */
  61. var CommandCursor = function(bson, ns, cmd, options, topology, topologyOptions) {
  62. CoreCursor.apply(this, Array.prototype.slice.call(arguments, 0));
  63. var self = this;
  64. var state = CommandCursor.INIT;
  65. var streamOptions = {};
  66. // MaxTimeMS
  67. var maxTimeMS = null;
  68. // Set up
  69. Readable.call(this, {objectMode: true});
  70. // Internal state
  71. this.s = {
  72. // MaxTimeMS
  73. maxTimeMS: maxTimeMS
  74. // State
  75. , state: state
  76. // Stream options
  77. , streamOptions: streamOptions
  78. // BSON
  79. , bson: bson
  80. // Namespae
  81. , ns: ns
  82. // Command
  83. , cmd: cmd
  84. // Options
  85. , options: options
  86. // Topology
  87. , topology: topology
  88. // Topology Options
  89. , topologyOptions: topologyOptions
  90. }
  91. }
  92. /**
  93. * CommandCursor stream data event, fired for each document in the cursor.
  94. *
  95. * @event CommandCursor#data
  96. * @type {object}
  97. */
  98. /**
  99. * CommandCursor stream end event
  100. *
  101. * @event CommandCursor#end
  102. * @type {null}
  103. */
  104. /**
  105. * CommandCursor stream close event
  106. *
  107. * @event CommandCursor#close
  108. * @type {null}
  109. */
  110. /**
  111. * CommandCursor stream readable event
  112. *
  113. * @event CommandCursor#readable
  114. * @type {null}
  115. */
  116. // Inherit from Readable
  117. inherits(CommandCursor, Readable);
  118. // Set the methods to inherit from prototype
  119. var methodsToInherit = ['_next', 'next', 'each', 'forEach', 'toArray'
  120. , 'rewind', 'bufferedCount', 'readBufferedDocuments', 'close', 'isClosed', 'kill'];
  121. // Only inherit the types we need
  122. for(var i = 0; i < methodsToInherit.length; i++) {
  123. CommandCursor.prototype[methodsToInherit[i]] = CoreCursor.prototype[methodsToInherit[i]];
  124. }
  125. /**
  126. * Set the batch size for the cursor.
  127. * @method
  128. * @param {number} value The batchSize for the cursor.
  129. * @throws {MongoError}
  130. * @return {CommandCursor}
  131. */
  132. CommandCursor.prototype.batchSize = function(value) {
  133. if(this.s.state == CommandCursor.CLOSED || this.isDead()) throw new MongoError("Cursor is closed");
  134. if(typeof value != 'number') throw new MongoError("batchSize requires an integer");
  135. if(this.s.cmd.cursor) this.s.cmd.cursor.batchSize = value;
  136. this.setCursorBatchSize(value);
  137. return this;
  138. }
  139. /**
  140. * Add a maxTimeMS stage to the aggregation pipeline
  141. * @method
  142. * @param {number} value The state maxTimeMS value.
  143. * @return {CommandCursor}
  144. */
  145. CommandCursor.prototype.maxTimeMS = function(value) {
  146. if(this.s.topology.lastIsMaster().minWireVersion > 2) {
  147. this.s.cmd.maxTimeMS = value;
  148. }
  149. return this;
  150. }
  151. CommandCursor.prototype.get = CommandCursor.prototype.toArray;
  152. /**
  153. * Get the next available document from the cursor, returns null if no more documents are available.
  154. * @function CommandCursor.prototype.next
  155. * @param {CommandCursor~resultCallback} callback The result callback.
  156. * @throws {MongoError}
  157. * @return {null}
  158. */
  159. /**
  160. * Set the new batchSize of the cursor
  161. * @function CommandCursor.prototype.setBatchSize
  162. * @param {number} value The new batchSize for the cursor
  163. * @return {null}
  164. */
  165. /**
  166. * Get the batchSize of the cursor
  167. * @function CommandCursor.prototype.batchSize
  168. * @param {number} value The current batchSize for the cursor
  169. * @return {null}
  170. */
  171. /**
  172. * The callback format for results
  173. * @callback CommandCursor~toArrayResultCallback
  174. * @param {MongoError} error An error instance representing the error during the execution.
  175. * @param {object[]} documents All the documents the satisfy the cursor.
  176. */
  177. /**
  178. * Returns an array of documents. The caller is responsible for making sure that there
  179. * is enough memory to store the results. Note that the array only contain partial
  180. * results when this cursor had been previouly accessed.
  181. * @method CommandCursor.prototype.toArray
  182. * @param {CommandCursor~toArrayResultCallback} callback The result callback.
  183. * @throws {MongoError}
  184. * @return {null}
  185. */
  186. /**
  187. * The callback format for results
  188. * @callback CommandCursor~resultCallback
  189. * @param {MongoError} error An error instance representing the error during the execution.
  190. * @param {(object|null)} result The result object if the command was executed successfully.
  191. */
  192. /**
  193. * Iterates over all the documents for this cursor. As with **{cursor.toArray}**,
  194. * not all of the elements will be iterated if this cursor had been previouly accessed.
  195. * In that case, **{cursor.rewind}** can be used to reset the cursor. However, unlike
  196. * **{cursor.toArray}**, the cursor will only hold a maximum of batch size elements
  197. * at any given time if batch size is specified. Otherwise, the caller is responsible
  198. * for making sure that the entire result can fit the memory.
  199. * @method CommandCursor.prototype.each
  200. * @param {CommandCursor~resultCallback} callback The result callback.
  201. * @throws {MongoError}
  202. * @return {null}
  203. */
  204. /**
  205. * Close the cursor, sending a KillCursor command and emitting close.
  206. * @method CommandCursor.prototype.close
  207. * @param {CommandCursor~resultCallback} [callback] The result callback.
  208. * @return {null}
  209. */
  210. /**
  211. * Is the cursor closed
  212. * @method CommandCursor.prototype.isClosed
  213. * @return {boolean}
  214. */
  215. /**
  216. * Clone the cursor
  217. * @function CommandCursor.prototype.clone
  218. * @return {CommandCursor}
  219. */
  220. /**
  221. * Resets the cursor
  222. * @function CommandCursor.prototype.rewind
  223. * @return {CommandCursor}
  224. */
  225. /**
  226. * The callback format for the forEach iterator method
  227. * @callback CommandCursor~iteratorCallback
  228. * @param {Object} doc An emitted document for the iterator
  229. */
  230. /**
  231. * The callback error format for the forEach iterator method
  232. * @callback CommandCursor~endCallback
  233. * @param {MongoError} error An error instance representing the error during the execution.
  234. */
  235. /*
  236. * Iterates over all the documents for this cursor using the iterator, callback pattern.
  237. * @method CommandCursor.prototype.forEach
  238. * @param {CommandCursor~iteratorCallback} iterator The iteration callback.
  239. * @param {CommandCursor~endCallback} callback The end callback.
  240. * @throws {MongoError}
  241. * @return {null}
  242. */
  243. CommandCursor.INIT = 0;
  244. CommandCursor.OPEN = 1;
  245. CommandCursor.CLOSED = 2;
  246. module.exports = CommandCursor;