Expression.js 15 KB

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