PipelineD.js 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. "use strict";
  2. var PipelineD = module.exports = (function(){
  3. // CONSTRUCTOR
  4. var klass = function PipelineD(){
  5. if(this.constructor == PipelineD) throw new Error("Never create instances of this! Use the static helpers only.");
  6. }, base = Object, proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
  7. // DEPENDENCIES
  8. var DocumentSource = require('./documentSources/DocumentSource'),
  9. CursorDocumentSource = require('./documentSources/CursorDocumentSource'),
  10. Cursor = require('../Cursor');
  11. /**
  12. * Create a Cursor wrapped in a DocumentSourceCursor, which is suitable to be the first source for a pipeline to begin with.
  13. * This source will feed the execution of the pipeline.
  14. *
  15. * //NOTE: DEVIATION FROM THE MONGO: We don't have special optimized cursors; You could support something similar by overriding `Pipeline#run` to call `DocumentSource#coalesce` on the `inputSource` if you really need it.
  16. *
  17. * This method looks for early pipeline stages that can be folded into
  18. * the underlying cursor, and when a cursor can absorb those, they
  19. * are removed from the head of the pipeline. For example, an
  20. * early match can be removed and replaced with a Cursor that will
  21. * do an index scan.
  22. *
  23. * @param pipeline {Pipeline} the logical "this" for this operation
  24. * @param ctx {Object} Context for expressions
  25. * @returns {CursorDocumentSource} the cursor that was created
  26. **/
  27. klass.prepareCursorSource = function prepareCursorSource(pipeline, /*dbName,*/ expCtx){
  28. var sources = pipeline.sourceVector;
  29. //NOTE: SKIPPED: look for initial match
  30. //NOTE: SKIPPED: create a query object
  31. //Look for an initial simple project; we'll avoid constructing Values for fields that won't make it through the projection
  32. var projection = {};
  33. var deps = [];
  34. var status = DocumentSource.GetDepsReturn.SEE_NEXT;
  35. for (var i=0; i < sources.length && status != DocumentSource.GetDepsReturn.EXHAUSTIVE; i++) {
  36. status = sources[i].getDependencies(deps);
  37. }
  38. if (status == DocumentSource.GetDepsReturn.EXHAUSTIVE) {
  39. projection = DocumentSource.depsToProjection(deps);
  40. }
  41. //NOTE: SKIPPED: Look for an initial sort
  42. //NOTE: SKIPPED: Create the sort object
  43. // //get the full "namespace" name
  44. // var fullName = dbName + "." + pipeline.collectionName;
  45. //NOTE: SKIPPED: if(DEV) log messages
  46. //Create the necessary context to use a Cursor
  47. //NOTE: SKIPPED: pSortedCursor bit
  48. //NOTE: SKIPPED: pUnsortedCursor bit
  49. var cursorWithContext = new CursorDocumentSource.CursorWithContext(/*fullName*/);
  50. // Now add the Cursor to cursorWithContext
  51. cursorWithContext._cursor = new Cursor( pipeline.collectionName );
  52. // wrap the cursor with a DocumentSource and return that
  53. var source = new CursorDocumentSource( cursorWithContext, expCtx );
  54. // source.namespace = fullName;
  55. //NOTE: SKIPPED: Note the query and sort
  56. if (Object.keys(projection).length) source.setProjection(projection);
  57. return source;
  58. };
  59. return klass;
  60. })();