SetIsSubsetExpression.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. "use strict";
  2. /**
  3. * A $setissubset pipeline expression.
  4. * @see evaluateInternal
  5. * @class SetIsSubsetExpression
  6. * @namespace mungedb-aggregate.pipeline.expressions
  7. * @module mungedb-aggregate
  8. * @constructor
  9. **/
  10. var SetIsSubsetExpression = module.exports = function SetIsSubsetExpression() {
  11. this.nargs = 2;
  12. if (arguments.length !== 2) throw new Error("two args expected");
  13. base.call(this);
  14. }, klass = SetIsSubsetExpression,
  15. base = require("./NaryExpression"),
  16. proto = klass.prototype = Object.create(base.prototype, {
  17. constructor: {
  18. value: klass
  19. }
  20. });
  21. // DEPENDENCIES
  22. var Value = require("../Value"),
  23. Expression = require("./Expression");
  24. // PROTOTYPE MEMBERS
  25. proto.getOpName = function getOpName() {
  26. return "$setissubset";
  27. };
  28. proto.optimize = function optimize(cachedRhsSet, operands) {
  29. // This optimize needs to be done, eventually
  30. // // perfore basic optimizations
  31. // intrusive_ptr<Expression> optimized = ExpressionNary::optimize();
  32. // // if ExpressionNary::optimize() created a new value, return it directly
  33. // if (optimized.get() != this)
  34. // return optimized;
  35. // if (ExpressionConstant* ec = dynamic_cast<ExpressionConstant*>(vpOperand[1].get())) {
  36. // const Value rhs = ec->getValue();
  37. // uassert(17311, str::stream() << "both operands of $setIsSubset must be arrays. Second "
  38. // << "argument is of type: " << typeName(rhs.getType()),
  39. // rhs.getType() == Array);
  40. // return new Optimized(arrayToSet(rhs), vpOperand);
  41. // }
  42. // return optimized;
  43. };
  44. /**
  45. * Takes 2 arrays. Assigns the second array to the first array.
  46. * @method evaluateInternal
  47. **/
  48. proto.evaluateInternal = function evaluateInternal(vars) {
  49. var array1 = this.operands[0].evaluateInternal(vars),
  50. array2 = this.operands[1].evaluateInternal(vars);
  51. if (array1 instanceof Array) throw new Error(this.getOpName() + ": object 1 must be an array");
  52. if (array2 instanceof Array) throw new Error(this.getOpName() + ": object 2 must be an array");
  53. var sizeOfArray1 = array1.length;
  54. var sizeOfArray2 = array2.length;
  55. var outerLoop = 0;
  56. var innerLoop = 0;
  57. for (outerLoop = 0; outerLoop < sizeOfArray1; outerLoop++) {
  58. for (innerLoop = 0; innerLoop < sizeOfArray2; innerLoop++) {
  59. if (array2[outerLoop] == array1[innerLoop])
  60. break;
  61. }
  62. /* If the above inner loop was not broken at all then
  63. array2[i] is not present in array1[] */
  64. if (innerLoop == sizeOfArray2)
  65. return false;
  66. }
  67. /* If we reach here then all elements of array2[]
  68. are present in array1[] */
  69. return true;
  70. };
  71. /** Register Expression */
  72. Expression.registerExpression("$setissubset", base.parse(SetIsSubsetExpression));