PipelineD.js 2.4 KB

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