PipelineD.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. "use strict";
  2. var CursorDocumentSource = require("./documentSources/CursorDocumentSource"),
  3. SortDocumentSource = require("./documentSources/SortDocumentSource"),
  4. getRunner = require("../query").getRunner;
  5. /**
  6. * Pipeline helper for reading data
  7. * @class PipelineD
  8. * @namespace mungedb-aggregate.pipeline
  9. * @module mungedb-aggregate
  10. * @constructor
  11. **/
  12. var PipelineD = module.exports = function PipelineD(){
  13. throw new Error("Never create instances of this! Use the static helpers only.");
  14. }, klass = PipelineD;
  15. /**
  16. * Create a Cursor wrapped in a DocumentSourceCursor, which is suitable to be the first source for a pipeline to begin with.
  17. * This source will feed the execution of the pipeline.
  18. *
  19. * //NOTE: Not doing anything here, as we don't use any of these cursor source features
  20. * //NOTE: DEVIATION FROM THE MONGO: we don't have special optimized cursors but could do it if needed
  21. *
  22. * This method looks for early pipeline stages that can be folded into
  23. * the underlying cursor, and when a cursor can absorb those, they
  24. * are removed from the head of the pipeline. For example, an
  25. * early match can be removed and replaced with a Cursor that will
  26. * do an index scan.
  27. *
  28. * @param pipeline {Pipeline} the logical "this" for this operation
  29. * @param ctx {Object} Context for expressions
  30. * @returns {CursorDocumentSource} the cursor that was created
  31. **/
  32. klass.prepareCursorSource = function prepareCursorSource(pipeline, expCtx){
  33. // We will be modifying the source vector as we go
  34. var sources = pipeline.sources;
  35. // Inject a MongodImplementation to sources that need them.
  36. // NOTE: SKIPPED
  37. // Don't modify the pipeline if we got a DocumentSourceMergeCursor
  38. // NOTE: SKIPPED
  39. // Look for an initial match. This works whether we got an initial query or not.
  40. // If not, it results in a "{}" query, which will be what we want in that case.
  41. var queryObj = pipeline.getInitialQuery(),
  42. match;
  43. if (queryObj !== undefined && queryObj !== null && queryObj.constructor === Object && Object.keys(queryObj).length > 0) {
  44. // This will get built in to the Cursor we'll create, so
  45. // remove the match from the pipeline
  46. match = sources.shift();
  47. }
  48. // Find the set of fields in the source documents depended on by this pipeline.
  49. var deps = pipeline.getDependencies(queryObj);
  50. // Passing query an empty projection since it is faster to use ParsedDeps::extractFields().
  51. // This will need to change to support covering indexes (SERVER-12015). There is an
  52. // exception for textScore since that can only be retrieved by a query projection.
  53. var projectionForQuery = deps.needTextScore ? deps.toProjection() : {};
  54. /*
  55. Look for an initial sort; we'll try to add this to the
  56. Cursor we create. If we're successful in doing that (further down),
  57. we'll remove the $sort from the pipeline, because the documents
  58. will already come sorted in the specified order as a result of the
  59. index scan.
  60. */
  61. var sortStage,
  62. sortObj,
  63. sortInRunner = false;
  64. if (sources.length) {
  65. sortStage = sources[0] instanceof SortDocumentSource ? sources[0] : undefined;
  66. if (sortStage) {
  67. // build the sort key
  68. sortObj = sortStage.serializeSortKey(/*explain*/false);
  69. sortInRunner = true;
  70. }
  71. }
  72. //munge deviation: the runner is (usually) not actually handling the initial query, so we need to add it back to the pipeline
  73. if (match){
  74. sources.unshift(match);
  75. }
  76. // Create the Runner.
  77. // NOTE: the logic here is simplified for munge
  78. var runner = expCtx.ns instanceof CursorDocumentSource ?
  79. expCtx.ns._runner : getRunner(expCtx.ns, queryObj, sortObj, projectionForQuery, sources);
  80. // DocumentSourceCursor expects a yielding Runner that has had its state saved.
  81. //runner.setYieldPolicy(Runner.RunnerState.YIELD_AUTO); //Skipped as we don't really support yielding yet
  82. runner.saveState();
  83. // Put the Runner into a DocumentSourceCursor and add it to the front of the pipeline.
  84. var source = expCtx.ns instanceof CursorDocumentSource ? expCtx.ns : new CursorDocumentSource("", runner, expCtx);
  85. // Note the query, sort, and projection for explain.
  86. source.setQuery(queryObj);
  87. if (sortInRunner)
  88. source.setSort(sortObj);
  89. source.setProjection(deps.toProjection(), deps.toParsedDeps());
  90. while (sources.length && source.coalesce(sources[0])) {
  91. sources.shift();
  92. }
  93. pipeline.addInitialSource(source);
  94. return runner;
  95. };