ProjectDocumentSource.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. "use strict";
  2. var Expression = require("../expressions/Expression"),
  3. ObjectExpression = require("../expressions/ObjectExpression"),
  4. Variables = require("../expressions/Variables"),
  5. VariablesIdGenerator = require("../expressions/VariablesIdGenerator"),
  6. VariablesParseState = require("../expressions/VariablesParseState");
  7. /**
  8. * A base class for filter document sources
  9. * @class ProjectDocumentSource
  10. * @namespace mungedb-aggregate.pipeline.documentSources
  11. * @module mungedb-aggregate
  12. * @constructor
  13. * @param [ctx] {ExpressionContext}
  14. **/
  15. var ProjectDocumentSource = module.exports = function ProjectDocumentSource(ctx, exprObj){
  16. if (arguments.length > 2) throw new Error("up to two args expected");
  17. base.call(this, ctx);
  18. this.OE = ObjectExpression.create();
  19. this._raw = undefined;
  20. this._variables = undefined;
  21. }, klass = ProjectDocumentSource, base = require("./DocumentSource"), proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}}); //jshint ignore:line
  22. klass.projectName = "$project";
  23. /**
  24. * Returns the name of project
  25. * @return {string} the name of project
  26. **/
  27. proto.getSourceName = function getSourceName() {
  28. return klass.projectName;
  29. };
  30. proto.getNext = function getNext() {
  31. if (this.expCtx && this.expCtx.checkForInterrupt) this.expCtx.checkForInterrupt();
  32. var input = this.source.getNext();
  33. if (!input)
  34. return null;
  35. // create the result document
  36. //var sizeHint = 0
  37. var out = {};
  38. //Document.copyMetaDataFrom(out);
  39. /**
  40. * Use the ExpressionObject to create the base result.
  41. *
  42. * If we're excluding fields at the top level, leave out the _id if
  43. * it is found, because we took care of it above.
  44. */
  45. this._variables.setRoot(input);
  46. this.OE.addToDocument(out, input, this._variables);
  47. this._variables.clearRoot();
  48. //NOTE: DEVIATION: skipped debug bit here
  49. return out;
  50. };
  51. /**
  52. * Optimizes the internal ObjectExpression
  53. * @return
  54. **/
  55. proto.optimize = function optimize() {
  56. this.OE = this.OE.optimize();
  57. };
  58. proto.serialize = function serialize(explain) {
  59. var out = {};
  60. out[this.getSourceName()] = this.OE.serialize(explain);
  61. return out;
  62. };
  63. /**
  64. * Builds a new ProjectDocumentSource from an object
  65. * @method createFromJson
  66. * @return {ProjectDocmentSource} a ProjectDocumentSource instance
  67. **/
  68. klass.createFromJson = function(elem, expCtx) {
  69. if (!(elem instanceof Object) || elem.constructor !== Object)
  70. throw new Error("Error 15969. Specification must be an object but was " + typeof elem);
  71. var objectContext = new Expression.ObjectCtx({
  72. isDocumentOk: true,
  73. isTopLevel: true,
  74. isInclusionOk: true
  75. });
  76. var idGenerator = new VariablesIdGenerator(),
  77. vps = new VariablesParseState(idGenerator),
  78. parsed = Expression.parseObject(elem, objectContext, vps),
  79. exprObj = parsed;
  80. if (!exprObj instanceof ObjectExpression) throw new Error("16402, parseObject() returned wrong type of Expression");
  81. //if (!exprObj.getFieldCount() ) throw new Error("uassert 16403: $project requires at least one output field");
  82. var project = new ProjectDocumentSource(expCtx, exprObj);
  83. project._variables = new Variables(idGenerator.getIdCount());
  84. var projectObj = elem;
  85. project._raw = projectObj;
  86. project.OE = exprObj;
  87. return project;
  88. };
  89. /**
  90. * Adds dependencies to the contained ObjectExpression
  91. * @param {deps} An object that is treated as a set of strings
  92. * @return A string that is part of the GetDepsReturn enum
  93. **/
  94. proto.getDependencies = function getDependencies(deps) {
  95. var path = [];
  96. this.OE.addDependencies(deps, path);
  97. return base.GetDepsReturn.EXHAUSTIVE_FIELDS;
  98. };
  99. /**
  100. * Returns the object that was used to construct the ProjectDocumentSource
  101. * @return {object} the object that was used to construct the ProjectDocumentSource
  102. **/
  103. proto.getRaw = function getRaw() {
  104. return this._raw;
  105. };