Pipeline.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. "use strict";
  2. var async = require("async"),
  3. DepsTracker = require("./DepsTracker"),
  4. documentSources = require("./documentSources/"),
  5. DocumentSource = documentSources.DocumentSource,
  6. LimitDocumentSource = documentSources.LimitDocumentSource,
  7. MatchDocumentSource = documentSources.MatchDocumentSource,
  8. ProjectDocumentSource = documentSources.ProjectDocumentSource,
  9. SkipDocumentSource = documentSources.SkipDocumentSource,
  10. UnwindDocumentSource = documentSources.UnwindDocumentSource,
  11. GroupDocumentSource = documentSources.GroupDocumentSource,
  12. OutDocumentSource = documentSources.OutDocumentSource,
  13. GeoNearDocumentSource = documentSources.GeoNearDocumentSource,
  14. RedactDocumentSource = documentSources.RedactDocumentSource,
  15. SortDocumentSource = documentSources.SortDocumentSource;
  16. /**
  17. * mongodb "commands" (sent via db.$cmd.findOne(...)) subclass to make a command. define a singleton object for it.
  18. * @class Pipeline
  19. * @namespace mungedb-aggregate.pipeline
  20. * @module mungedb-aggregate
  21. * @constructor
  22. **/
  23. var Pipeline = module.exports = function Pipeline(theCtx){
  24. this.sources = null;
  25. this.explain = false;
  26. this.splitMongodPipeline = false;
  27. this.ctx = theCtx;
  28. }, klass = Pipeline, proto = klass.prototype;
  29. klass.COMMAND_NAME = "aggregate";
  30. klass.PIPELINE_NAME = "pipeline";
  31. klass.EXPLAIN_NAME = "explain";
  32. klass.FROM_ROUTER_NAME = "fromRouter";
  33. klass.SERVER_PIPELINE_NAME = "serverPipeline";
  34. klass.MONGOS_PIPELINE_NAME = "mongosPipeline";
  35. klass.BATCH_SIZE_NAME = "batchSize";
  36. klass.stageDesc = {};//attaching this to the class for test cases
  37. klass.stageDesc[GeoNearDocumentSource.geoNearName] = GeoNearDocumentSource.createFromJson;
  38. klass.stageDesc[GroupDocumentSource.groupName] = GroupDocumentSource.createFromJson;
  39. klass.stageDesc[LimitDocumentSource.limitName] = LimitDocumentSource.createFromJson;
  40. klass.stageDesc[MatchDocumentSource.matchName] = MatchDocumentSource.createFromJson;
  41. klass.stageDesc[OutDocumentSource.outName] = OutDocumentSource.createFromJson;
  42. klass.stageDesc[ProjectDocumentSource.projectName] = ProjectDocumentSource.createFromJson;
  43. klass.stageDesc[RedactDocumentSource.redactName] = ProjectDocumentSource.createFromJson;
  44. klass.stageDesc[SkipDocumentSource.skipName] = SkipDocumentSource.createFromJson;
  45. klass.stageDesc[SortDocumentSource.sortName] = SortDocumentSource.createFromJson;
  46. klass.stageDesc[UnwindDocumentSource.unwindName] = UnwindDocumentSource.createFromJson;
  47. klass.nStageDesc = Object.keys(klass.stageDesc).length;
  48. klass.optimizations = {};
  49. klass.optimizations.local = {};
  50. klass.optimizations.sharded = {};
  51. /**
  52. * Moves $match before $sort when they are placed next to one another
  53. * @static
  54. * @method moveMatchBeforeSort
  55. * @param pipelineInst An instance of a Pipeline
  56. */
  57. klass.optimizations.local.moveMatchBeforeSort = function moveMatchBeforeSort(pipelineInst) {
  58. var sources = pipelineInst.sources;
  59. for(var srcn = sources.length, srci = 1; srci < srcn; ++srci) {
  60. var source = sources[srci];
  61. if(source.constructor === MatchDocumentSource) {
  62. var previous = sources[srci - 1];
  63. if(previous && previous.constructor === SortDocumentSource) { //Added check that previous exists
  64. /* swap this item with the previous */
  65. sources[srci] = previous;
  66. sources[srci-1] = source;
  67. }
  68. }
  69. }
  70. };
  71. /**
  72. * Moves $limit before $skip when they are placed next to one another
  73. * @static
  74. * @method moveLimitBeforeSkip
  75. * @param pipelineInst An instance of a Pipeline
  76. */
  77. klass.optimizations.local.moveLimitBeforeSkip = function moveLimitBeforeSkip(pipelineInst) {
  78. var sources = pipelineInst.sources;
  79. if(sources.length === 0) return;
  80. for(var i = sources.length - 1; i >= 1 /* not looking at 0 */; i--) {
  81. var limit = sources[i].constructor === LimitDocumentSource ? sources[i] : undefined,
  82. skip = sources[i-1].constructor === SkipDocumentSource ? sources[i-1] : undefined;
  83. if(limit && skip) {
  84. limit.setLimit(limit.getLimit() + skip.getSkip());
  85. sources[i-1] = limit;
  86. sources[i] = skip;
  87. // Start at back again. This is needed to handle cases with more than 1 $limit
  88. // (S means skip, L means limit)
  89. //
  90. // These two would work without second pass (assuming back to front ordering)
  91. // SL -> LS
  92. // SSL -> LSS
  93. //
  94. // The following cases need a second pass to handle the second limit
  95. // SLL -> LLS
  96. // SSLL -> LLSS
  97. // SLSL -> LLSS
  98. i = sources.length; // decremented before next pass
  99. }
  100. }
  101. };
  102. /**
  103. * Attempts to coalesce every pipeline stage into the previous pipeline stage, starting after the first
  104. * @static
  105. * @method coalesceAdjacent
  106. * @param pipelineInst An instance of a Pipeline
  107. */
  108. klass.optimizations.local.coalesceAdjacent = function coalesceAdjacent(pipelineInst) {
  109. var sources = pipelineInst.sources;
  110. if(sources.length === 0) return;
  111. // move all sources to a temporary list
  112. var moveSrc = sources.pop(),
  113. tempSources = [];
  114. while(moveSrc) {
  115. tempSources.unshift(moveSrc);
  116. moveSrc = sources.pop();
  117. }
  118. // move the first one to the final list
  119. sources.push(tempSources[0]);
  120. // run through the sources, coalescing them or keeping them
  121. for(var tempn = tempSources.length, tempi = 1; tempi < tempn; ++tempi) {
  122. // If we can't coalesce the source with the last, then move it
  123. // to the final list, and make it the new last. (If we succeeded,
  124. // then we're still on the same last, and there's no need to move
  125. // or do anything with the source -- the destruction of tempSources
  126. // will take care of the rest.)
  127. var lastSource = sources[sources.length-1],
  128. tempSrc = tempSources[tempi];
  129. if(!(lastSource && tempSrc)) {
  130. throw new Error("Must have a last and current source"); // verify(lastSource && tempSrc);
  131. }
  132. if(!lastSource.coalesce(tempSrc)) sources.push(tempSrc);
  133. }
  134. };
  135. /**
  136. * Iterates over sources in the pipelineInst, optimizing each
  137. * @static
  138. * @method optimizeEachDocumentSource
  139. * @param pipelineInst An instance of a Pipeline
  140. */
  141. klass.optimizations.local.optimizeEachDocumentSource = function optimizeEachDocumentSource(pipelineInst) {
  142. var sources = pipelineInst.sources;
  143. for(var srci = 0, srcn = sources.length; srci < srcn; ++srci) {
  144. sources[srci].optimize();
  145. }
  146. };
  147. /**
  148. * Auto-places a $match before a $redact when the $redact is the first item in a pipeline
  149. * @static
  150. * @method duplicateMatchBeforeInitalRedact
  151. * @param pipelineInst An instance of a Pipeline
  152. */
  153. klass.optimizations.local.duplicateMatchBeforeInitalRedact = function duplicateMatchBeforeInitalRedact(pipelineInst) {
  154. var sources = pipelineInst.sources;
  155. if(sources.length >= 2 && sources[0].constructor === RedactDocumentSource) {
  156. if(sources[1].constructor === MatchDocumentSource) {
  157. var match = sources[1],
  158. redactSafePortion = match.redactSafePortion();
  159. if(Object.keys(redactSafePortion).length > 0) {
  160. sources.shift(MatchDocumentSource.createFromJson(redactSafePortion, pipelineInst.ctx));
  161. }
  162. }
  163. }
  164. };
  165. //SKIPPED: addRequiredPrivileges
  166. /**
  167. * Perform optimizations for a pipeline through sharding
  168. * @method splitForSharded
  169. */
  170. proto.splitForSharded = function splitForSharded() {
  171. var shardPipeline = new Pipeline({});
  172. shardPipeline.explain = this.explain;
  173. klass.optimizations.sharded.findSplitPoint(shardPipeline, this);
  174. klass.optimizations.sharded.moveFinalUnwindFromShardsToMerger(shardPipeline, this);
  175. //klass.optimizations.sharded.limitFieldsSentFromShardsToMerger(shardPipeline, this);
  176. return shardPipeline;
  177. };
  178. /**
  179. * Split the source into Merge sources and Shard sources
  180. * @static
  181. * @method findSplitPoint
  182. * @param shardPipe Shard sources
  183. * @param mergePipe Merge sources
  184. */
  185. klass.optimizations.sharded.findSplitPoint = function findSplitPoint(shardPipe, mergePipe) {
  186. while(mergePipe.sources.length > 0) {
  187. var current = mergePipe.sources[0];
  188. mergePipe.sources.splice(0, 1);
  189. if (current.isSplittable && current.isSplittable()) {
  190. var shardSource = current.getShardSource(),
  191. mergeSource = current.getMergeSource();
  192. //if (typeof shardSource != "undefined") { shardPipe.sources.push(shardSource); } //push_back
  193. if (shardSource) { shardPipe.sources.push(shardSource); } //push_back
  194. //if (typeof mergeSource != "undefined") { mergePipe.sources.unshift(mergeSource); } //push_front
  195. if (mergeSource) { mergePipe.sources.unshift(mergeSource); } //push_front
  196. break;
  197. }
  198. else {
  199. if (!shardPipe.sources) { shardPipe.sources = []; }
  200. shardPipe.sources.push(current);
  201. }
  202. }
  203. };
  204. /**
  205. * Optimize pipeline through moving unwind to the end
  206. * @static
  207. * @method moveFinalUnwindFromShardsToMerger
  208. * @param shardPipe shard sources
  209. * @param mergePipe merge sources
  210. */
  211. klass.optimizations.sharded.moveFinalUnwindFromShardsToMerger = function moveFinalUnwindFromShardsToMerger(shardPipe, mergePipe) {
  212. if (true) {
  213. while (shardPipe.sources !== null &&
  214. shardPipe.sources.length > 0 &&
  215. shardPipe.sources[shardPipe.sources.length - 1] instanceof UnwindDocumentSource) {
  216. mergePipe.sources.unshift(shardPipe.sources.pop());
  217. }
  218. }
  219. };
  220. //SKIPPED: optimizations.sharded.limitFieldsSentFromShardsToMerger. Somehow what this produces is not handled by Expression.js (err 16404)
  221. /**
  222. * Create an `Array` of `DocumentSource`s from the given JSON pipeline
  223. * // NOTE: DEVIATION FROM MONGO: split out into a separate function to better allow extensions (was in parseCommand)
  224. * @static
  225. * @method parseDocumentSources
  226. * @param pipeline {Array} The JSON pipeline
  227. * @returns {Array} The parsed `DocumentSource`s
  228. */
  229. klass.parseDocumentSources = function parseDocumentSources(pipeline, ctx){
  230. var sources = [];
  231. for (var nSteps = pipeline.length, iStep = 0; iStep < nSteps; ++iStep) {
  232. // pull out the pipeline element as an object
  233. var pipeElement = pipeline[iStep];
  234. if (!(pipeElement instanceof Object)) throw new Error("pipeline element " + iStep + " is not an object; code 15942");
  235. var obj = pipeElement;
  236. // Parse a pipeline stage from 'obj'.
  237. if (Object.keys(obj).length !== 1)
  238. throw new Error("A pipeline stage specification object must contain exactly one field; code 16435");
  239. var stageName = Object.keys(obj)[0],
  240. stageSpec = obj[stageName];
  241. // Create a DocumentSource pipeline stage from 'stageSpec'.
  242. var desc = klass.stageDesc[stageName];
  243. if (!desc) throw new Error("Unrecognized pipeline stage name: '" + stageName + "'; uassert code 16436");
  244. // Parse the stage
  245. var stage = desc(stageSpec, ctx);
  246. if (!stage) throw new Error("Stage must not be undefined!"); // verify(stage)
  247. sources.push(stage);
  248. if(stage.constructor === OutDocumentSource && iStep !== nSteps - 1) {
  249. throw new Error("$out can only be the final stage in the pipeline; code 16991");
  250. }
  251. }
  252. return sources;
  253. };
  254. /**
  255. * Create a pipeline from the command.
  256. * @static
  257. * @method parseCommand
  258. * @param cmdObj {Object} The command object sent from the client
  259. * @param cmdObj.aggregate {Array} the thing to aggregate against // NOTE: DEVIATION FROM MONGO: not a collection name
  260. * @param cmdObj.pipeline {Object} the JSON pipeline of `DocumentSource` specs
  261. * @param cmdObj.explain {Boolean} should explain?
  262. * @param cmdObj.fromRouter {Boolean} is from router?
  263. * @param cmdObj.splitMongodPipeline {Boolean} should split?
  264. * @param ctx {Object} Not used yet in mungedb-aggregate
  265. * @returns {Array} the pipeline, if created, otherwise a NULL reference
  266. */
  267. klass.parseCommand = function parseCommand(cmdObj, ctx){
  268. var pipelineNamespace = require("./"),
  269. Pipeline = pipelineNamespace.Pipeline, // using require in case Pipeline gets replaced with an extension
  270. pipelineInst = new Pipeline(ctx);
  271. //gather the specification for the aggregation
  272. var pipeline;
  273. for (var fieldName in cmdObj) { //jshint ignore:line
  274. var cmdElement = cmdObj[fieldName];
  275. if (fieldName[0] === "$")
  276. continue;
  277. else if (fieldName === "cursor")
  278. continue;
  279. else if (fieldName === klass.COMMAND_NAME)
  280. continue; //look for the aggregation command
  281. else if (fieldName === klass.BATCH_SIZE_NAME)
  282. continue;
  283. else if (fieldName === klass.PIPELINE_NAME)
  284. pipeline = cmdElement; //check for the pipeline of JSON doc srcs
  285. else if (fieldName === klass.EXPLAIN_NAME)
  286. pipelineInst.explain = cmdElement; //check for explain option
  287. else if (fieldName === klass.FROM_ROUTER_NAME)
  288. ctx.inShard = cmdElement; //if the request came from the router, we're in a shard
  289. else if (fieldName === "allowDiskUsage") {
  290. if (typeof cmdElement !== "boolean")
  291. throw new Error("allowDiskUsage must be a bool, not a " + typeof allowDiskUsage + "; uassert code 16949");
  292. } else
  293. throw new Error("unrecognized field " + JSON.stringify(fieldName));
  294. }
  295. /**
  296. * If we get here, we've harvested the fields we expect for a pipeline
  297. * Set up the specified document source pipeline.
  298. */
  299. // NOTE: DEVIATION FROM MONGO: split this into a separate function to simplify facilitate extensions (now in parseDocumentSources)
  300. pipelineInst.sources = Pipeline.parseDocumentSources(pipeline, ctx);
  301. klass.optimizations.local.moveMatchBeforeSort(pipelineInst);
  302. klass.optimizations.local.moveLimitBeforeSkip(pipelineInst);
  303. klass.optimizations.local.coalesceAdjacent(pipelineInst);
  304. klass.optimizations.local.optimizeEachDocumentSource(pipelineInst);
  305. klass.optimizations.local.duplicateMatchBeforeInitalRedact(pipelineInst);
  306. return pipelineInst;
  307. };
  308. /**
  309. * Gets the initial $match query when $match is the first pipeline stage
  310. * @method run
  311. * @param inputSource {DocumentSource} The input document source for the pipeline
  312. * @param [callback] {Function} Optional callback function if using async extensions
  313. * @return {Object} An empty object or the match spec
  314. */
  315. proto.getInitialQuery = function getInitialQuery() {
  316. var sources = this.sources;
  317. if(sources.length === 0) {
  318. return {};
  319. }
  320. /* look for an initial $match */
  321. var match = sources[0].constructor === MatchDocumentSource ? sources[0] : undefined;
  322. if(!match) return {};
  323. return match.getQuery();
  324. };
  325. /**
  326. * Creates the JSON representation of the pipeline
  327. * @method run
  328. * @param inputSource {DocumentSource} The input document source for the pipeline
  329. * @param [callback] {Function} Optional callback function if using async extensions
  330. * @return {Object} An empty object or the match spec
  331. */
  332. proto.serialize = function serialize() {
  333. var serialized = {},
  334. array = [];
  335. // create an array out of the pipeline operations
  336. if (this.sources) {
  337. for (var i = 0; i < this.sources.length; i++) {
  338. //this.sources.forEach(function(source) {
  339. this.sources[i].serializeToArray(array);
  340. }
  341. }
  342. serialized[klass.COMMAND_NAME] = this.ctx && this.ctx.ns && this.ctx.ns.coll ? this.ctx.ns.coll : "";
  343. serialized[klass.PIPELINE_NAME] = array;
  344. if(this.explain) serialized[klass.EXPLAIN_NAME] = this.explain;
  345. return serialized;
  346. };
  347. /**
  348. * Points each source at its previous source
  349. * @method stitch
  350. */
  351. proto.stitch = function stitch() {
  352. if(this.sources.length <= 0) throw new Error("should not have an empty pipeline; massert code 16600");
  353. /* chain together the sources we found */
  354. var prevSource = this.sources[0];
  355. for(var srci = 1, srcn = this.sources.length; srci < srcn; srci++) {
  356. var tempSource = this.sources[srci];
  357. tempSource.setSource(prevSource);
  358. prevSource = tempSource;
  359. }
  360. };
  361. /**
  362. * Run the pipeline
  363. * @method run
  364. * @param callback {Function} gets called once for each document result from the pipeline
  365. */
  366. proto.run = function run(callback) {
  367. // should not get here in the explain case
  368. if(this.explain) throw new Error("Should not be running a pipeline in explain mode!");
  369. var doc = null,
  370. error = null,
  371. finalSource = this._getFinalSource();
  372. async.doWhilst(
  373. function iterator(next){
  374. return finalSource.getNext(function (err, obj){
  375. callback(err, obj);
  376. doc = obj;
  377. error = err;
  378. next();
  379. });
  380. },
  381. function test(){
  382. return doc !== null && !error;
  383. },
  384. function done(err){
  385. //nothing to do here
  386. });
  387. };
  388. /**
  389. * Get the last document source in the pipeline
  390. * @method _getFinalSource
  391. * @return {Object} The DocumentSource at the end of the pipeline
  392. * @private
  393. */
  394. proto._getFinalSource = function _getFinalSource() {
  395. return this.sources[this.sources.length - 1];
  396. };
  397. /**
  398. * Get the pipeline explanation
  399. * @method writeExplainOps
  400. * @return {Array} An array of source explanations
  401. */
  402. proto.writeExplainOps = function writeExplainOps() {
  403. var array = [];
  404. this.sources.forEach(function(source) {
  405. source.serializeToArray(array, /*explain=*/true);
  406. });
  407. return array;
  408. };
  409. /**
  410. * Set the source of documents for the pipeline
  411. * @method addInitialSource
  412. * @param source {DocumentSource}
  413. */
  414. proto.addInitialSource = function addInitialSource(source) {
  415. this.sources.unshift(source);
  416. };
  417. //SKIPPED: canRunInMongos
  418. //Note: Deviation from Mongo: Mongo 2.6.5 passes a param to getDependencies
  419. // to calculate TextScore. mungedb-aggregate doesn't do this, so no param is needed.
  420. proto.getDependencies = function getDependencies () {
  421. var deps = new DepsTracker(),
  422. knowAllFields = false;
  423. //NOTE: Deviation from Mongo -- We aren't using Meta and textscore
  424. for (var i=0; i < this.sources.length && !knowAllFields; i++) {
  425. var localDeps = new DepsTracker(),
  426. status = this.sources[i].getDependencies(localDeps);
  427. if (status === DocumentSource.GetDepsReturn.NOT_SUPPORTED) {
  428. // Assume this stage needs everything. We may still know something about our
  429. // dependencies if an earlier stage returned either EXHAUSTIVE_FIELDS or
  430. // EXHAUSTIVE_META.
  431. break;
  432. }
  433. if (!knowAllFields) {
  434. for (var key in localDeps.fields) //jshint ignore:line
  435. deps.fields[key] = localDeps.fields[key];
  436. if (localDeps.needWholeDocument)
  437. deps.needWholeDocument = true;
  438. knowAllFields = status & DocumentSource.GetDepsReturn.EXHAUSTIVE_FIELDS;
  439. }
  440. }
  441. if (!knowAllFields)
  442. deps.needWholeDocument = true; // don't know all fields we need
  443. return deps;
  444. };