Expression.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. "use strict";
  2. var Expression = module.exports = (function(){
  3. // CONSTRUCTOR
  4. /**
  5. * A base class for all pipeline expressions; Performs common expressions within an Op.
  6. *
  7. * NOTE: An object expression can take any of the following forms:
  8. *
  9. * f0: {f1: ..., f2: ..., f3: ...}
  10. * f0: {$operator:[operand1, operand2, ...]}
  11. *
  12. * @class Expression
  13. * @namespace mungedb.aggregate.pipeline.expressions
  14. * @module mungedb-aggregate
  15. * @constructor
  16. **/
  17. var klass = module.exports = Expression = function Expression(){
  18. if(arguments.length !== 0) throw new Error("zero args expected");
  19. }, base = Object, proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
  20. // DEPENDENCIES
  21. var Document = require("../Document");
  22. // NESTED CLASSES
  23. /**
  24. * Reference to the `munge.pipeline.expressions.Expression.ObjectCtx` class
  25. * @static
  26. * @property ObjectCtx
  27. **/
  28. var ObjectCtx = Expression.ObjectCtx = (function(){
  29. // CONSTRUCTOR
  30. /**
  31. * Utility class for parseObject() below. isDocumentOk indicates that it is OK to use a Document in the current context.
  32. *
  33. * NOTE: deviation from Mongo code: accepts an `Object` of settings rather than a bitmask to help simplify the interface a little bit
  34. *
  35. * @class ObjectCtx
  36. * @namespace mungedb.aggregate.pipeline.expressions.Expression
  37. * @module mungedb-aggregate
  38. * @constructor
  39. * @param opts
  40. * @param [opts.isDocumentOk] {Boolean}
  41. * @param [opts.isTopLevel] {Boolean}
  42. * @param [opts.isInclusionOk] {Boolean}
  43. **/
  44. var klass = function ObjectCtx(opts /*= {isDocumentOk:..., isTopLevel:..., isInclusionOk:...}*/){
  45. if(!(opts instanceof Object && opts.constructor == Object)) throw new Error("opts is required and must be an Object containing named args");
  46. for (var k in opts) { // assign all given opts to self so long as they were part of klass.prototype as undefined properties
  47. if (opts.hasOwnProperty(k) && proto.hasOwnProperty(k) && proto[k] === undefined) this[k] = opts[k];
  48. }
  49. }, base = Object, proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
  50. // PROTOTYPE MEMBERS
  51. proto.isDocumentOk =
  52. proto.isTopLevel =
  53. proto.isInclusionOk = undefined;
  54. return klass;
  55. })();
  56. /**
  57. * Reference to the `munge.pipeline.expressions.Expression.OpDesc` class
  58. * @static
  59. * @property OpDesc
  60. **/
  61. var OpDesc = Expression.OpDesc = (function(){
  62. // CONSTRUCTOR
  63. /**
  64. * Decribes how and when to create an Op instance
  65. *
  66. * @class OpDesc
  67. * @namespace mungedb.aggregate.pipeline.expressions.Expression
  68. * @module mungedb-aggregate
  69. * @constructor
  70. * @param name
  71. * @param factory
  72. * @param flags
  73. * @param argCount
  74. **/
  75. var klass = function OpDesc(name, factory, flags, argCount){
  76. var firstArg = arguments[0];
  77. if (firstArg instanceof Object && firstArg.constructor == Object) { //TODO: using this?
  78. var opts = firstArg;
  79. for (var k in opts) { // assign all given opts to self so long as they were part of klass.prototype as undefined properties
  80. if (opts.hasOwnProperty(k) && proto.hasOwnProperty(k) && proto[k] === undefined) this[k] = opts[k];
  81. }
  82. } else {
  83. this.name = name;
  84. this.factory = factory;
  85. this.flags = flags || 0;
  86. this.argCount = argCount || 0;
  87. }
  88. }, base = Object, proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
  89. // STATIC MEMBERS
  90. klass.FIXED_COUNT = 1;
  91. klass.OBJECT_ARG = 2;
  92. // PROTOTYPE MEMBERS
  93. proto.name =
  94. proto.factory =
  95. proto.flags =
  96. proto.argCount = undefined;
  97. /**
  98. * internal `OpDesc#name` comparer
  99. * @method cmp
  100. * @param that the other `OpDesc` instance
  101. **/
  102. proto.cmp = function cmp(that) {
  103. return this.name < that.name ? -1 : this.name > that.name ? 1 : 0;
  104. };
  105. return klass;
  106. })();
  107. // END OF NESTED CLASSES
  108. /**
  109. * @class Expression
  110. * @namespace mungedb.aggregate.pipeline.expressions
  111. * @module mungedb-aggregate
  112. **/
  113. var kinds = {
  114. UNKNOWN: "UNKNOWN",
  115. OPERATOR: "OPERATOR",
  116. NOT_OPERATOR: "NOT_OPERATOR"
  117. };
  118. // STATIC MEMBERS
  119. /**
  120. * Enumeration of comparison operators. These are shared between a few expression implementations, so they are factored out here.
  121. *
  122. * @static
  123. * @property CmpOp
  124. **/
  125. klass.CmpOp = {
  126. EQ: "$eq", // return true for a == b, false otherwise
  127. NE: "$ne", // return true for a != b, false otherwise
  128. GT: "$gt", // return true for a > b, false otherwise
  129. GTE: "$gte", // return true for a >= b, false otherwise
  130. LT: "$lt", // return true for a < b, false otherwise
  131. LTE: "$lte", // return true for a <= b, false otherwise
  132. CMP: "$cmp" // return -1, 0, 1 for a < b, a == b, a > b
  133. };
  134. // DEPENDENCIES (later in this file as compared to others to ensure that the required statics are setup first)
  135. var FieldPathExpression = require("./FieldPathExpression"),
  136. ObjectExpression = require("./ObjectExpression"),
  137. ConstantExpression = require("./ConstantExpression"),
  138. CompareExpression = require("./CompareExpression");
  139. // DEFERRED DEPENDENCIES
  140. /**
  141. * Expressions, as exposed to users
  142. *
  143. * @static
  144. * @property opMap
  145. **/
  146. process.nextTick(function(){ // Even though `opMap` is deferred, force it to load early rather than later to prevent even *more* potential silliness
  147. Object.defineProperty(klass, "opMap", {value:klass.opMap});
  148. });
  149. Object.defineProperty(klass, "opMap", { //NOTE: deferred requires using a getter to allow circular requires (to maintain the ported API)
  150. configurable: true,
  151. get: function getOpMapOnce() {
  152. return Object.defineProperty(klass, "opMap", {
  153. value: [ //NOTE: rather than OpTable because it gets converted to a dict via OpDesc#name in the Array#reduce() below
  154. new OpDesc("$add", require("./AddExpression"), 0),
  155. new OpDesc("$and", require("./AndExpression"), 0),
  156. new OpDesc("$cmp", CompareExpression.bind(null, Expression.CmpOp.CMP), OpDesc.FIXED_COUNT, 2),
  157. new OpDesc("$cond", require("./CondExpression"), OpDesc.FIXED_COUNT, 3),
  158. // $const handled specially in parseExpression
  159. new OpDesc("$dayOfMonth", require("./DayOfMonthExpression"), OpDesc.FIXED_COUNT, 1),
  160. new OpDesc("$dayOfWeek", require("./DayOfWeekExpression"), OpDesc.FIXED_COUNT, 1),
  161. new OpDesc("$dayOfYear", require("./DayOfYearExpression"), OpDesc.FIXED_COUNT, 1),
  162. new OpDesc("$divide", require("./DivideExpression"), OpDesc.FIXED_COUNT, 2),
  163. new OpDesc("$eq", CompareExpression.bind(null, Expression.CmpOp.EQ), OpDesc.FIXED_COUNT, 2),
  164. new OpDesc("$gt", CompareExpression.bind(null, Expression.CmpOp.GT), OpDesc.FIXED_COUNT, 2),
  165. new OpDesc("$gte", CompareExpression.bind(null, Expression.CmpOp.GTE), OpDesc.FIXED_COUNT, 2),
  166. new OpDesc("$hour", require("./HourExpression"), OpDesc.FIXED_COUNT, 1),
  167. new OpDesc("$ifNull", require("./IfNullExpression"), OpDesc.FIXED_COUNT, 2),
  168. new OpDesc("$indexOf", require("./IndexOfExpression"), OpDesc.FIXED_COUNT, 2),
  169. new OpDesc("$lt", CompareExpression.bind(null, Expression.CmpOp.LT), OpDesc.FIXED_COUNT, 2),
  170. new OpDesc("$lte", CompareExpression.bind(null, Expression.CmpOp.LTE), OpDesc.FIXED_COUNT, 2),
  171. new OpDesc("$minute", require("./MinuteExpression"), OpDesc.FIXED_COUNT, 1),
  172. new OpDesc("$mod", require("./ModExpression"), OpDesc.FIXED_COUNT, 2),
  173. new OpDesc("$month", require("./MonthExpression"), OpDesc.FIXED_COUNT, 1),
  174. new OpDesc("$multiply", require("./MultiplyExpression"), 0),
  175. new OpDesc("$ne", CompareExpression.bind(null, Expression.CmpOp.NE), OpDesc.FIXED_COUNT, 2),
  176. new OpDesc("$not", require("./NotExpression"), OpDesc.FIXED_COUNT, 1),
  177. new OpDesc("$or", require("./OrExpression"), 0),
  178. new OpDesc("$second", require("./SecondExpression"), OpDesc.FIXED_COUNT, 1),
  179. new OpDesc("$strcasecmp", require("./StrcasecmpExpression"), OpDesc.FIXED_COUNT, 2),
  180. new OpDesc("$substr", require("./SubstrExpression"), OpDesc.FIXED_COUNT, 3),
  181. new OpDesc("$subtract", require("./SubtractExpression"), OpDesc.FIXED_COUNT, 2),
  182. new OpDesc("$toLower", require("./ToLowerExpression"), OpDesc.FIXED_COUNT, 1),
  183. new OpDesc("$toUpper", require("./ToUpperExpression"), OpDesc.FIXED_COUNT, 1),
  184. new OpDesc("$week", require("./WeekExpression"), OpDesc.FIXED_COUNT, 1),
  185. new OpDesc("$year", require("./YearExpression"), OpDesc.FIXED_COUNT, 1)
  186. ].reduce(function(r,o){r[o.name]=o; return r;}, {})
  187. }).opMap;
  188. }
  189. });
  190. /**
  191. * Parse an Object. The object could represent a functional expression or a Document expression.
  192. *
  193. * An object expression can take any of the following forms:
  194. *
  195. * f0: {f1: ..., f2: ..., f3: ...}
  196. * f0: {$operator:[operand1, operand2, ...]}
  197. *
  198. * @static
  199. * @method parseObject
  200. * @param obj the element representing the object
  201. * @param ctx a MiniCtx representing the options above
  202. * @returns the parsed Expression
  203. **/
  204. klass.parseObject = function parseObject(obj, ctx){
  205. if(!(ctx instanceof ObjectCtx)) throw new Error("ctx must be ObjectCtx");
  206. var kind = kinds.UNKNOWN,
  207. expr, // the result
  208. exprObj; // the alt result
  209. if (obj === undefined) return new ObjectExpression();
  210. var fieldNames = Object.getOwnPropertyNames(obj);
  211. for (var fc = 0, n = fieldNames.length; fc < n; ++fc) {
  212. var fn = fieldNames[fc];
  213. if (fn[0] === "$") {
  214. if (fc !== 0) throw new Error("the operator must be the only field in a pipeline object (at '" + fn + "'.; code 16410");
  215. if(ctx.isTopLevel) throw new Error("$expressions are not allowed at the top-level of $project; code 16404");
  216. kind = kinds.OPERATOR; //we've determined this "object" is an operator expression
  217. expr = Expression.parseExpression(fn, obj[fn]);
  218. } else {
  219. if (kind === kinds.OPERATOR) throw new Error("this object is already an operator expression, and can't be used as a document expression (at '" + fn + "'.; code 15990");
  220. if (!ctx.isTopLevel && fn.indexOf(".") != -1) throw new Error("dotted field names are only allowed at the top level; code 16405");
  221. if (expr === undefined) { // if it's our first time, create the document expression
  222. if (!ctx.isDocumentOk) throw new Error("document not allowed in this context"); // CW TODO error: document not allowed in this context
  223. expr = exprObj = new ObjectExpression();
  224. kind = kinds.NOT_OPERATOR; //this "object" is not an operator expression
  225. }
  226. var fv = obj[fn];
  227. switch (typeof(fv)) {
  228. case "object":
  229. // it's a nested document
  230. var subCtx = new ObjectCtx({
  231. isDocumentOk: ctx.isDocumentOk,
  232. isInclusionOk: ctx.isInclusionOk
  233. });
  234. exprObj.addField(fn, Expression.parseObject(fv, subCtx));
  235. break;
  236. case "string":
  237. // it's a renamed field // CW TODO could also be a constant
  238. var pathExpr = new FieldPathExpression(Expression.removeFieldPrefix(fv));
  239. exprObj.addField(fn, pathExpr);
  240. break;
  241. case "boolean":
  242. case "number":
  243. // it's an inclusion specification
  244. if (fv) {
  245. if (!ctx.isInclusionOk) throw new Error("field inclusion is not allowed inside of $expressions; code 16420");
  246. exprObj.includePath(fn);
  247. } else {
  248. if (!(ctx.isTopLevel && fn == Document.ID_PROPERTY_NAME)) throw new Error("The top-level " + Document.ID_PROPERTY_NAME + " field is the only field currently supported for exclusion; code 16406");
  249. exprObj.excludeId = true;
  250. }
  251. break;
  252. default:
  253. throw new Error("disallowed field type " + (fv ? fv.constructor.name + ":" : "") + typeof(fv) + " in object expression (at '" + fn + "')");
  254. }
  255. }
  256. }
  257. return expr;
  258. };
  259. /**
  260. * Parse a BSONElement Object which has already been determined to be functional expression.
  261. *
  262. * @static
  263. * @method parseExpression
  264. * @param opName the name of the (prefix) operator
  265. * @param obj the BSONElement to parse
  266. * @returns the parsed Expression
  267. **/
  268. klass.parseExpression = function parseExpression(opName, obj) {
  269. // look for the specified operator
  270. if (opName === "$const") return new ConstantExpression(obj); //TODO: createFromBsonElement was here, not needed since this isn't BSON?
  271. var op = klass.opMap[opName];
  272. if (!(op instanceof OpDesc)) throw new Error("invalid operator " + opName + "; code 15999");
  273. // make the expression node
  274. var IExpression = op.factory, //TODO: should this get renamed from `factory` to `ctor` or something?
  275. expr = new IExpression();
  276. // add the operands to the expression node
  277. if (op.flags & OpDesc.FIXED_COUNT && op.argCount > 1 && !(obj instanceof Array)) throw new Error("the " + op.name + " operator requires an array of " + op.argCount + " operands; code 16019");
  278. var operand; // used below
  279. if (obj.constructor === Object) { // the operator must be unary and accept an object argument
  280. if (!(op.flags & OpDesc.OBJECT_ARG)) throw new Error("the " + op.name + " operator does not accept an object as an operand");
  281. operand = Expression.parseObject(obj, new ObjectCtx({isDocumentOk: 1}));
  282. expr.addOperand(operand);
  283. } else if (obj instanceof Array) { // multiple operands - an n-ary operator
  284. if (op.flags & OpDesc.FIXED_COUNT && op.argCount !== obj.length) throw new Error("the " + op.name + " operator requires " + op.argCount + " operand(s); code 16020");
  285. for (var i = 0, n = obj.length; i < n; ++i) {
  286. operand = Expression.parseOperand(obj[i]);
  287. expr.addOperand(operand);
  288. }
  289. } else { //assume it's an atomic operand
  290. if (op.flags & OpDesc.FIXED_COUNT && op.argCount != 1) throw new Error("the " + op.name + " operator requires an array of " + op.argCount + " operands; code 16022");
  291. operand = Expression.parseOperand(obj);
  292. expr.addOperand(operand);
  293. }
  294. return expr;
  295. };
  296. /**
  297. * Parse a BSONElement which is an operand in an Expression.
  298. *
  299. * @static
  300. * @param pBsonElement the expected operand's BSONElement
  301. * @returns the parsed operand, as an Expression
  302. **/
  303. klass.parseOperand = function parseOperand(obj){
  304. var t = typeof(obj);
  305. if (t === "string" && obj[0] == "$") { //if we got here, this is a field path expression
  306. var path = Expression.removeFieldPrefix(obj);
  307. return new FieldPathExpression(path);
  308. }
  309. else if (t === "object" && obj && obj.constructor === Object) return Expression.parseObject(obj, new ObjectCtx({isDocumentOk: true}));
  310. else return new ConstantExpression(obj);
  311. };
  312. /**
  313. * Produce a field path string with the field prefix removed.
  314. * Throws an error if the field prefix is not present.
  315. *
  316. * @static
  317. * @param prefixedField the prefixed field
  318. * @returns the field path with the prefix removed
  319. **/
  320. klass.removeFieldPrefix = function removeFieldPrefix(prefixedField) {
  321. if (prefixedField.indexOf("\0") != -1) throw new Error("field path must not contain embedded null characters; code 16419");
  322. if (prefixedField[0] !== "$") throw new Error("field path references must be prefixed with a '$' ('" + prefixedField + "'); code 15982");
  323. return prefixedField.substr(1);
  324. };
  325. /**
  326. * returns the signe of a number
  327. *
  328. * @static
  329. * @method signum
  330. * @returns the sign of a number; -1, 1, or 0
  331. **/
  332. klass.signum = function signum(i) {
  333. if (i < 0) return -1;
  334. if (i > 0) return 1;
  335. return 0;
  336. };
  337. // PROTOTYPE MEMBERS
  338. /**
  339. * Evaluate the Expression using the given document as input.
  340. *
  341. * @method evaluate
  342. * @returns the computed value
  343. **/
  344. proto.evaluate = function evaluate(obj) {
  345. throw new Error("WAS NOT IMPLEMENTED BY INHERITOR!");
  346. };
  347. /**
  348. * Optimize the Expression.
  349. *
  350. * This provides an opportunity to do constant folding, or to collapse nested
  351. * operators that have the same precedence, such as $add, $and, or $or.
  352. *
  353. * The Expression should be replaced with the return value, which may or may
  354. * not be the same object. In the case of constant folding, a computed
  355. * expression may be replaced by a constant.
  356. *
  357. * @method optimize
  358. * @returns the optimized Expression
  359. **/
  360. proto.optimize = function optimize() {
  361. throw new Error("WAS NOT IMPLEMENTED BY INHERITOR!");
  362. };
  363. /**
  364. * Add this expression's field dependencies to the set Expressions are trees, so this is often recursive.
  365. *
  366. * Top-level ExpressionObject gets pointer to empty vector.
  367. * If any other Expression is an ancestor, or in other cases where {a:1} inclusion objects aren't allowed, they get NULL.
  368. *
  369. * @method addDependencies
  370. * @param deps output parameter
  371. * @param path path to self if all ancestors are ExpressionObjects.
  372. **/
  373. proto.addDependencies = function addDependencies(deps, path) {
  374. throw new Error("WAS NOT IMPLEMENTED BY INHERITOR!");
  375. };
  376. /**
  377. * simple expressions are just inclusion exclusion as supported by ExpressionObject
  378. * @method getIsSimple
  379. **/
  380. proto.getIsSimple = function getIsSimple() {
  381. return false;
  382. };
  383. proto.toMatcherBson = function toMatcherBson(){
  384. throw new Error("WAS NOT IMPLEMENTED BY INHERITOR!"); //verify(false && "Expression::toMatcherBson()");
  385. };
  386. return klass;
  387. })();