MatchExpression.js 4.3 KB

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