CursorDocumentSource.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. "use strict";
  2. var async = require("async"),
  3. Runner = require("../../query/Runner"),
  4. DocumentSource = require("./DocumentSource"),
  5. LimitDocumentSource = require("./LimitDocumentSource");
  6. /**
  7. * Constructs and returns Documents from the BSONObj objects produced by a supplied Runner.
  8. * An object of this type may only be used by one thread, see SERVER-6123.
  9. *
  10. * This is usually put at the beginning of a chain of document sources
  11. * in order to fetch data from the database.
  12. *
  13. * @class CursorDocumentSource
  14. * @namespace mungedb-aggregate.pipeline.documentSources
  15. * @module mungedb-aggregate
  16. * @constructor
  17. * @param ns {String}
  18. * @param runner {Runner}
  19. * @param expCtx {Object}
  20. */
  21. var CursorDocumentSource = module.exports = CursorDocumentSource = function CursorDocumentSource(ns, runner, expCtx){
  22. base.call(this, expCtx);
  23. this._docsAddedToBatches = 0; // for _limit enforcement
  24. this._ns = ns;
  25. this._runner = runner;
  26. this._currentBatch = [];
  27. this._currentBatchIndex = 0; //NOTE: DEVIATION FROM MONGO: they do not track index
  28. // BSONObj members must outlive _projection and cursor.
  29. this._query = undefined;
  30. this._sort = undefined;
  31. this._projection = undefined;
  32. this._dependencies = undefined;
  33. this._limit = undefined;
  34. this._firstRun = true; //NOTE: DEVIATION FROM MONGO: to ensure that the callstack does not get too large doing things syncronously
  35. }, klass = CursorDocumentSource, base = DocumentSource, proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}}); //jshint ignore:line
  36. proto.getSourceName = function getSourceName() {
  37. return "$cursor";
  38. };
  39. proto.getNext = function getNext(callback) {
  40. if (this.expCtx && this.expCtx.checkForInterrupt && this.expCtx.checkForInterrupt()) {
  41. return callback(new Error("Interrupted"));
  42. }
  43. var self = this;
  44. if (self._currentBatchIndex >= self._currentBatch.length) {
  45. self._currentBatchIndex = 0;
  46. self._currentBatch = [];
  47. return self._loadBatch(function(err){
  48. if (err) return callback(err);
  49. if (self._currentBatch.length === 0) // exhausted the cursor
  50. return callback(null, null);
  51. var out = self._currentBatch[self._currentBatchIndex];
  52. self._currentBatchIndex++;
  53. return callback(null, out);
  54. });
  55. }
  56. return callback(null, self._currentBatch[self._currentBatchIndex++]);
  57. };
  58. proto.dispose = function dispose() {
  59. // Can't call in to Runner or ClientCursor registries from this function since it will be
  60. // called when an agg cursor is killed which would cause a deadlock.
  61. this._runner = undefined;
  62. this._currentBatch = [];
  63. };
  64. proto._loadBatch = function _loadBatch(callback) {
  65. if (!this._runner) {
  66. this.dispose();
  67. return callback();
  68. }
  69. this._runner.restoreState();
  70. var self = this,
  71. whileShouldBreak = false, // mimic while loop break in async land
  72. whileShouldReturn = false; // mimic while loop return in async land
  73. return async.whilst(
  74. function test(){
  75. return !whileShouldBreak && !whileShouldReturn;
  76. },
  77. function(next) {
  78. return self._runner.getNext(function(err, obj, state){
  79. if (err) return next(err);
  80. //NOTE: DEVIATION FROM MONGO: they check state in the loop condition we check it inside (due to async stuff)
  81. if (state !== Runner.RunnerState.RUNNER_ADVANCED) return whileShouldBreak = true, next();
  82. if (self._dependencies) {
  83. self._currentBatch.push(self._dependencies.extractFields(obj));
  84. } else {
  85. self._currentBatch.push(obj);
  86. }
  87. if (self._limit) {
  88. if (++self._docsAddedToBatches === self._limit.getLimit()) {
  89. return whileShouldBreak = true, next();
  90. }
  91. if (self._docsAddedToBatches > self._limit.getLimit()) return next(new Error("Assertion failure: end of limit"));
  92. }
  93. var memUsageDocs = self._currentBatch.length;
  94. if (memUsageDocs >= klass.MaxDocumentsToReturnToClientAtOnce) {
  95. // End self batch and prepare Runner for yielding.
  96. self._runner.saveState();
  97. return whileShouldReturn = true, next();
  98. }
  99. return next();
  100. });
  101. },
  102. function(err){
  103. if (whileShouldReturn){
  104. return setImmediate(function() {
  105. callback(err);
  106. });
  107. }
  108. // If we got here, there won't be any more documents, so destroy the runner. Can't use
  109. // dispose since we want to keep the _currentBatch.
  110. self._runner = undefined;
  111. //NOTE: DEVIATION FROM MONGO: to ensure that the callstack does not get too large if the Runner does things syncronously
  112. if (self._firstRun || !self._currentBatch.length){
  113. self._firstRun = false;
  114. callback(err);
  115. } else {
  116. return setImmediate(function(){
  117. callback(err);
  118. });
  119. }
  120. }
  121. );
  122. };
  123. proto.setSource = function setSource(theSource) {
  124. // this doesn't take a source
  125. throw new Error("Assertion error: this doesnt take a source");
  126. };
  127. /**
  128. * returns -1 for no limit
  129. * @method getLimit
  130. */
  131. proto.getLimit = function getLimit() {
  132. return this._limit ? this._limit.getLimit() : -1;
  133. };
  134. proto.coalesce = function coalesce(nextSource) {
  135. // Note: Currently we assume the $limit is logically after any $sort or
  136. // $match. If we ever pull in $match or $sort using this method, we
  137. // will need to keep track of the order of the sub-stages.
  138. if (!this._limit) {
  139. this._limit = nextSource instanceof LimitDocumentSource ? nextSource : undefined;
  140. return Boolean(this._limit); // false if next is not a $limit
  141. } else {
  142. return this._limit.coalesce(nextSource);
  143. }
  144. return false;
  145. };
  146. function extractInfo(o) { //NOTE: DEVIATION FROM MONGO: skipping a lot of explain for now
  147. return o;
  148. }
  149. proto.serialize = function serialize(explain) { //NOTE: DEVIATION FROM MONGO: parts of this not implemented, may want later
  150. // we never parse a documentSourceCursor, so we only serialize for explain
  151. if (!explain)
  152. return null;
  153. var plan;
  154. // explainStatus = {code:ErrorCodes.INTERNAL_ERROR, description:""};
  155. //NOTE: DEVIATION FROM MONGO: our `Runner#getInfo()` API is a little broken
  156. //TODO: fix our `Runner#getInfo()` API to match their API
  157. {
  158. if (!this._runner)
  159. throw new Error("No _runner. Were we disposed before explained?; massert code 17392");
  160. this._runner.restoreState();
  161. var explainRaw = {};
  162. explainRaw = this._runner.getInfo(explain, null);
  163. if (explainRaw) //TODO: use this instead: if (explainStatus.code === ErrorCodes.OK)
  164. plan = explainRaw;
  165. this._runner.saveState();
  166. }
  167. var out = {};
  168. out.query = this._query;
  169. if (this._sort && Object.keys(this._sort).length > 0)
  170. out.sort = this._sort;
  171. if (this._limit)
  172. out.limit = this._limit.getLimit();
  173. if (this._projection && Object.keys(this._projection).length > 0)
  174. out.fields = this._projection;
  175. if (true) { //TODO: use this instead: if (explainStatus.code === ErrorCodes.OK) {
  176. out.plan = extractInfo(plan);
  177. } else {
  178. out.planError = "ERROR EXPLAINING PLAN"; //TODO: use this instead: explainStatus
  179. }
  180. var doc = {};
  181. doc[this.getSourceName()] = out;
  182. return doc;
  183. };
  184. /**
  185. * Create a document source based on a passed-in Runner.
  186. *
  187. * This is usually put at the beginning of a chain of document sources
  188. * in order to fetch data from the database.
  189. *
  190. * @method create
  191. * @static
  192. * @param ns {String}
  193. * @param runner {Runner}
  194. * @param expCtx {Object}
  195. */
  196. klass.create = function create(ns, runner, expCtx) {
  197. return new CursorDocumentSource(ns, runner, expCtx);
  198. };
  199. /**
  200. * Informs this object of projection and dependency information.
  201. *
  202. * @method setProjection
  203. * @param projection A projection specification describing the fields needed by the rest of
  204. * the pipeline.
  205. * @param deps The output of DepsTracker::toParsedDeps
  206. */
  207. proto.setProjection = function setProjection(projection, deps) {
  208. this._projection = projection;
  209. this._dependencies = deps;
  210. };
  211. proto.isValidInitialSource = function(){
  212. return true;
  213. };
  214. /**
  215. * Record the query that was specified for the cursor this wraps, if
  216. * any.
  217. *
  218. * This should be captured after any optimizations are applied to
  219. * the pipeline so that it reflects what is really used.
  220. *
  221. * This gets used for explain output.
  222. *
  223. * @method setQuery
  224. * @param {Object} pBsonObj the query to record
  225. */
  226. proto.setQuery = function setQuery(query) {
  227. this._query = query;
  228. };
  229. /**
  230. * Record the sort that was specified for the cursor this wraps, if
  231. * any.
  232. *
  233. * This should be captured after any optimizations are applied to
  234. * the pipeline so that it reflects what is really used.
  235. *
  236. * This gets used for explain output.
  237. *
  238. * @method setSort
  239. * @param {Object} pBsonObj the sort to record
  240. */
  241. proto.setSort = function setSort(sort) {
  242. this._sort = sort;
  243. };
  244. klass.MaxDocumentsToReturnToClientAtOnce = 150; //NOTE: DEVIATION FROM MONGO: put this here and using MaxDocuments instead of MaxBytes