PipelineD.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. var DocumentSource = require('./documentSources/DocumentSource'),
  8. CursorDocumentSource = require('./documentSources/CursorDocumentSource'),
  9. Cursor = require('../Cursor');
  10. /**
  11. * Create a Cursor wrapped in a DocumentSourceCursor, which is suitable
  12. * to be the first source for a pipeline to begin with. This source
  13. * will feed the execution of the pipeline.
  14. *
  15. * <<Note: Below does not happen in munge>>
  16. * This method looks for early pipeline stages that can be folded into
  17. * the underlying cursor, and when a cursor can absorb those, they
  18. * are removed from the head of the pipeline. For example, an
  19. * early match can be removed and replaced with a Cursor that will
  20. * do an index scan.
  21. *
  22. * @param {Pipeline} pPipeline the logical "this" for this operation
  23. * @param {Array} db the data we are going to be munging
  24. * @returns {CursorDocumentSource} the cursor that was created
  25. **/
  26. klass.prepareCursorSource = function prepareCursorSource(pPipeline, db){
  27. var sources = pPipeline.sourceVector;
  28. //note that this is a deviation from the mongo implementation to facilitate pipeline reuse
  29. sources.forEach(function(source){
  30. source.reset();
  31. source.pSource = null;
  32. });
  33. //TODO: should this go earlier in the execution so that we dont need to do it every time?
  34. var projection = {};
  35. var deps = [];
  36. var status = DocumentSource.GetDepsReturn.SEE_NEXT;
  37. for (var i=0; i < sources.length && status == DocumentSource.GetDepsReturn.SEE_NEXT; i++) {
  38. status = sources[i].getDependencies(deps);
  39. }
  40. if (status == DocumentSource.GetDepsReturn.EXHAUSTIVE) {
  41. projection = DocumentSource.depsToProjection(deps);
  42. }
  43. var cursorWithContext = new CursorDocumentSource.CursorWithContext( );
  44. cursorWithContext._cursor = new Cursor( db );
  45. /* wrap the cursor with a DocumentSource and return that */
  46. var source = new CursorDocumentSource( cursorWithContext );
  47. if (projection && Object.keys(projection).length)
  48. source.setProjection(projection);
  49. return source;
  50. };
  51. return klass;
  52. })();