FieldRangeExpression.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. var FieldRangeExpression = module.exports = (function(){
  2. // CONSTRUCTOR
  3. /** Create a field range expression.
  4. | Field ranges are meant to match up with classic Matcher semantics, and
  5. | therefore are conjunctions. For example, these appear in mongo shell
  6. | predicates in one of these forms:
  7. | { a : C } -> (a == C) // degenerate "point" range
  8. | { a : { $lt : C } } -> (a < C) // open range
  9. | { a : { $gt : C1, $lte : C2 } } -> ((a > C1) && (a <= C2)) // closed
  10. | When initially created, a field range only includes one end of the range. Additional points may be added via intersect().
  11. | Note that NE and CMP are not supported.
  12. | @param pathExpr the field path for extracting the field value
  13. | @param cmpOp the comparison operator
  14. | @param value the value to compare against
  15. | @returns the newly created field range expression
  16. **/
  17. var klass = function FieldRangeExpression(pathExpr, cmpOp, value){
  18. if(arguments.length !== 3) throw new Error("args expected: pathExpr, cmpOp, and value");
  19. this.pathExpr = pathExpr;
  20. this.range = new Range({cmpOp:cmpOp, value:value});
  21. }, Expression = require("./Expression"), base = Expression, proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
  22. // DEPENDENCIES
  23. var Value = require("../Value"),
  24. ConstantExpression = require("./ConstantExpression");
  25. // NESTED CLASSES
  26. var Range = (function(){
  27. /** create a new Range; opts is either {cmpOp:..., value:...} or {bottom:..., isBottomOpen:..., top:..., isTopOpen:...} **/
  28. var klass = function Range(opts){
  29. this.isBottomOpen = this.isTopOpen = false;
  30. this.bottom = this.top = undefined;
  31. if(opts.hasOwnProperty("cmpOp") && opts.hasOwnProperty("value")){
  32. switch (opts.cmpOp) {
  33. case Expression.CmpOp.EQ:
  34. this.bottom = this.top = opts.value;
  35. break;
  36. case Expression.CmpOp.GT:
  37. this.isBottomOpen = true;
  38. /* falls through */
  39. case Expression.CmpOp.GTE:
  40. this.isTopOpen = true;
  41. this.bottom = opts.value;
  42. break;
  43. case Expression.CmpOp.LT:
  44. this.isTopOpen = true;
  45. /* falls through */
  46. case Expression.CmpOp.LTE:
  47. this.isBottomOpen = true;
  48. this.top = opts.value;
  49. break;
  50. case Expression.CmpOp.NE:
  51. case Expression.CmpOp.CMP:
  52. throw new Error("CmpOp not allowed: " + opts.cmpOp);
  53. default:
  54. throw new Error("Unexpected CmpOp: " + opts.cmpOp);
  55. }
  56. }else{
  57. this.bottom = opts.bottom;
  58. this.isBottomOpen = opts.isBottomOpen;
  59. this.top = opts.top;
  60. this.isTopOpen = opts.isTopOpen;
  61. }
  62. }, base = Object, proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
  63. // PROTOTYPE MEMBERS
  64. proto.intersect = function intersect(range){
  65. // Find the max of the bottom end of ranges
  66. var maxBottom = range.bottom,
  67. maxBottomOpen = range.isBottomOpen;
  68. if(this.bottom !== undefined){
  69. if(range.bottom === undefined){
  70. maxBottom = this.bottom;
  71. maxBottomOpen = this.isBottomOpen;
  72. }else{
  73. if(Value.compare(this.bottom, range.bottom) === 0){
  74. maxBottomOpen = this.isBottomOpen || range.isBottomOpen;
  75. }else{
  76. maxBottom = this.bottom;
  77. maxBottomOpen = this.isBottomOpen;
  78. }
  79. }
  80. }
  81. // Find the min of the tops of the ranges
  82. var minTop = range.top,
  83. minTopOpen = range.isTopOpen;
  84. if(this.top !== undefined){
  85. if(range.top === undefined){
  86. minTop = this.top;
  87. minTopOpen = this.isTopOpen;
  88. }else{
  89. if(Value.compare(this.top, range.top) === 0){
  90. minTopOpen = this.isTopOpen || range.isTopOpen;
  91. }else{
  92. minTop = this.top;
  93. minTopOpen = this.isTopOpen;
  94. }
  95. }
  96. }
  97. if(Value.compare(maxBottom, minTop) <= 0)
  98. return new Range({bottom:maxBottom, isBottomOpen:maxBottomOpen, top:minTop, isTopOpen:minTopOpen});
  99. return null; // empty intersection
  100. };
  101. proto.contains = function contains(value){
  102. var cmp;
  103. if(this.bottom !== undefined){
  104. cmp = Value.compare(value, this.bottom);
  105. if(cmp < 0) return false;
  106. if(this.isBottomOpen && cmp === 0) return false;
  107. }
  108. if(this.top !== undefined){
  109. cmp = Value.compare(value, this.top);
  110. if(cmp > 0) return false;
  111. if(this.isTopOpen && cmp === 0) return false;
  112. }
  113. return true;
  114. };
  115. return klass;
  116. })();
  117. // PROTOTYPE MEMBERS
  118. proto.evaluate = function evaluate(obj){
  119. if(this.range === undefined) return false;
  120. var value = this.pathExpr.evaluate(obj);
  121. return this.range.contains(value);
  122. };
  123. proto.optimize = function optimize(){
  124. if(this.range === undefined) return new ConstantExpression(false);
  125. if(this.range.bottom === undefined && this.range.top === undefined) return new ConstantExpression(true);
  126. return this;
  127. };
  128. proto.addDependencies = function(deps){
  129. return this.pathExpr.addDependencies(deps);
  130. };
  131. /** Add an intersecting range.
  132. | This can be done any number of times after creation. The range is
  133. | internally optimized for each new addition. If the new intersection
  134. | extends or reduces the values within the range, the internal
  135. | representation is adjusted to reflect that.
  136. | Note that NE and CMP are not supported.
  137. | @param cmpOp the comparison operator
  138. | @param pValue the value to compare against
  139. **/
  140. proto.intersect = function intersect(cmpOp, value){
  141. this.range = this.range.intersect(new Range({cmpOp:cmpOp, value:value}));
  142. };
  143. proto.toJson = function toJson(){
  144. if (this.range === undefined) return false; //nothing will satisfy this predicate
  145. if (this.range.top === undefined && this.range.bottom === undefined) return true; // any value will satisfy this predicate
  146. // FIXME Append constant values using the $const operator. SERVER-6769
  147. var json = {};
  148. if (this.range.top === this.range.bottom) {
  149. json[Expression.CmpOp.EQ] = [this.pathExpr.toJson(), this.range.top];
  150. }else{
  151. var leftOp = {};
  152. if (this.range.bottom !== undefined) {
  153. leftOp[this.range.isBottomOpen ? Expression.CmpOp.GT : Expression.CmpOp.GTE] = [this.pathExpr.toJson(), this.range.bottom];
  154. if (this.range.top === undefined) return leftOp;
  155. }
  156. var rightOp = {};
  157. if(this.range.top !== undefined){
  158. rightOp[this.range.isTopOpen ? Expression.CmpOp.LT : Expression.CmpOp.LTE] = [this.pathExpr.toJson(), this.range.top];
  159. if (this.range.bottom === undefined) return rightOp;
  160. }
  161. json.$and = [leftOp, rightOp];
  162. }
  163. return json;
  164. };
  165. //TODO: proto.addToBson = ...?
  166. //TODO: proto.addToBsonObj = ...?
  167. //TODO: proto.addToBsonArray = ...?
  168. //TODO: proto.toMatcherBson = ...? WILL PROBABLY NEED THESE...
  169. return klass;
  170. })();