MatchExpression.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. "use strict";
  2. /**
  3. * Files: matcher/expression.h/cpp
  4. * Function order follows that in the header file
  5. * @class MatchExpression
  6. * @namespace mungedb-aggregate.pipeline.matcher
  7. * @module mungedb-aggregate
  8. * @constructor
  9. * @param type {String} The type of the match expression
  10. */
  11. var MatchExpression = module.exports = function MatchExpression(type){
  12. this._matchType = type;
  13. }, klass = MatchExpression, base = Object, proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
  14. // DEPENDENCIES
  15. var errors = require("../../Errors.js"),
  16. ErrorCodes = errors.ErrorCodes;
  17. /**
  18. * Return the _matchType property
  19. * @method matchType
  20. */
  21. proto.matchType = function matchType(){
  22. return this._matchType;
  23. };
  24. /**
  25. * Return the number of children we have
  26. * @method numChildren
  27. */
  28. proto.numChildren = function numChildren( ){
  29. return 0;
  30. };
  31. /**
  32. * Get the i-th child.
  33. * @method getChild
  34. */
  35. proto.getChild = function getChild(i) {
  36. return null;
  37. };
  38. /**
  39. * Get all the children of a node.
  40. * @method getChild
  41. */
  42. proto.getChildVector = function getChildVector(i) {
  43. return null;
  44. };
  45. /**
  46. * Get the path of the leaf. Returns StringData() if there is
  47. * no path (node is logical).
  48. * @method path
  49. */
  50. proto.path = function path( ){
  51. return "";
  52. };
  53. /*
  54. * Notes on structure:
  55. * isLogical, isArray, and isLeaf define three partitions of all possible operators.
  56. *
  57. * isLogical can have children and its children can be arbitrary operators.
  58. *
  59. * isArray can have children and its children are predicates over one field.
  60. *
  61. * isLeaf is a predicate over one field.
  62. */
  63. /**
  64. * Is this node a logical operator? All of these inherit from ListOfMatchExpression.
  65. * AND, OR, NOT, NOR.
  66. * @method isLogical
  67. */
  68. proto.isLogical = function isLogical(){
  69. switch( this._matchType ){
  70. case "AND":
  71. case "OR":
  72. case "NOT":
  73. case "NOR":
  74. return true;
  75. default:
  76. return false;
  77. }
  78. return false;
  79. };
  80. /**
  81. * Is this node an array operator? Array operators have multiple clauses but operate on one
  82. * field.
  83. *
  84. * ALL (AllElemMatchOp)
  85. * ELEM_MATCH_VALUE, ELEM_MATCH_OBJECT, SIZE (ArrayMatchingMatchExpression)
  86. * @method isArray
  87. */
  88. proto.isArray = function isArray(){
  89. switch (this._matchType){
  90. case "SIZE":
  91. case "ALL":
  92. case "ELEM_MATCH_VALUE":
  93. case "ELEM_MATCH_OBJECT":
  94. return true;
  95. default:
  96. return false;
  97. }
  98. return false;
  99. };
  100. /**
  101. * Not-internal nodes, predicates over one field. Almost all of these inherit
  102. * from LeafMatchExpression.
  103. *
  104. * Exceptions: WHERE, which doesn't have a field.
  105. * TYPE_OPERATOR, which inherits from MatchExpression due to unique
  106. * array semantics.
  107. * @method isLeaf
  108. */
  109. proto.isLeaf = function isLeaf(){
  110. return !this.isArray() && !this.isLogical();
  111. };
  112. /**
  113. * XXX: document
  114. * @method shallowClone
  115. * @return {MatchExpression}
  116. * @abstract
  117. */
  118. proto.shallowClone = function shallowClone() {
  119. throw new Error("NOT IMPLEMENTED");
  120. };
  121. /**
  122. * XXX document
  123. * @method equivalent
  124. * @return {Boolean}
  125. * @abstract
  126. */
  127. proto.equivalent = function equivalent() {
  128. throw new Error("NOT IMPLEMENTED");
  129. };
  130. //
  131. // Determine if a document satisfies the tree-predicate.
  132. //
  133. /**
  134. * @method matches
  135. * @return {Boolean}
  136. * @abstract
  137. */
  138. proto.matches = function matches(doc, details/* = 0 */) {
  139. throw new Error("NOT IMPLEMENTED");
  140. };
  141. /**
  142. * Wrapper around matches function
  143. * @method matchesJSON
  144. */
  145. proto.matchesJSON = function matchesJSON(doc, details/* = 0 */){
  146. return this.matches(doc, details);
  147. };
  148. /**
  149. * Determines if the element satisfies the tree-predicate.
  150. * Not valid for all expressions (e.g. $where); in those cases, returns false.
  151. * @method matchesSingleElement
  152. */
  153. proto.matchesSingleElement = function matchesSingleElement(doc) {
  154. throw new Error("NOT IMPLEMENTED");
  155. };
  156. /**
  157. * Return the _tagData property
  158. * @method getTag
  159. */
  160. proto.getTag = function getTag(){
  161. return this._tagData;
  162. };
  163. /**
  164. * Set the _tagData property
  165. * @method setTag
  166. * @param data
  167. */
  168. proto.setTag = function setTag(data){
  169. this._tagData = data;
  170. };
  171. proto.resetTag = function resetTag() {
  172. this.setTag(null);
  173. for(var i=0; i<this.numChildren(); i++) {
  174. this.getChild(i).resetTag();
  175. }
  176. };
  177. /**
  178. * Call the debugString method
  179. * @method toString
  180. */
  181. proto.toString = function toString(){
  182. return this.debugString(0);
  183. };
  184. /**
  185. * Debug information
  186. * @method debugString
  187. */
  188. proto.debugString = function debugString(level) {
  189. throw new Error("NOT IMPLEMENTED");
  190. };
  191. /**
  192. * @method _debugAddSpace
  193. * @param level
  194. */
  195. proto._debugAddSpace = function _debugAddSpace(level){
  196. return new Array(level+1).join(" ");
  197. };