MatchDocumentSource.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. "use strict";
  2. var async = require("async"),
  3. matcher = require("../../matcher/Matcher2.js");
  4. /**
  5. * A match document source built off of DocumentSource
  6. *
  7. * NOTE: THIS IS A DEVIATION FROM THE MONGO IMPLEMENTATION.
  8. * TODO: internally uses `sift` to fake it, which has bugs, so we need to reimplement this by porting the MongoDB implementation
  9. *
  10. * @class MatchDocumentSource
  11. * @namespace mungedb-aggregate.pipeline.documentSources
  12. * @module mungedb-aggregate
  13. * @constructor
  14. * @param {Object} query the match query to use
  15. * @param [ctx] {ExpressionContext}
  16. **/
  17. var MatchDocumentSource = module.exports = function MatchDocumentSource(query, ctx){
  18. if (arguments.length > 2) throw new Error("up to two args expected");
  19. if (!query) throw new Error("arg `query` is required");
  20. base.call(this, ctx);
  21. this.query = query; // save the query, so we can check it for deps later. THIS IS A DEVIATION FROM THE MONGO IMPLEMENTATION
  22. this.matcher = new matcher(query);
  23. // not supporting currently $text operator
  24. // set _isTextQuery to false.
  25. // TODO: update after we implement $text.
  26. if (klass.isTextQuery(query)) throw new Error("$text pipeline operation not supported");
  27. this._isTextQuery = false;
  28. }, klass = MatchDocumentSource, base = require("./DocumentSource"), proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}}); //jshint ignore:line
  29. klass.matchName = "$match";
  30. proto.getSourceName = function getSourceName(){
  31. return klass.matchName;
  32. };
  33. proto.getNext = function getNext(callback) {
  34. if (!callback) throw new Error(this.getSourceName() + " #getNext() requires callback");
  35. if (this.expCtx.checkForInterrupt && this.expCtx.checkForInterrupt() === false) {
  36. return callback(new Error("Interrupted"));
  37. }
  38. var self = this,
  39. next,
  40. test = function test(doc) {
  41. return self.matcher.matches(doc);
  42. },
  43. makeReturn = function makeReturn(doc) {
  44. if(doc !== null && test(doc)) { // Passes the match criteria
  45. return doc;
  46. } else if(doc === null){ // Got EOF
  47. return doc;
  48. }
  49. return undefined; // Didn't match, but not EOF
  50. };
  51. async.doUntil(
  52. function(cb) {
  53. self.source.getNext(function(err, doc) {
  54. if(err) return cb(err);
  55. try {
  56. if (makeReturn(doc) !== undefined) {
  57. next = doc;
  58. }
  59. } catch (ex) {
  60. return cb(ex);
  61. }
  62. return cb();
  63. });
  64. },
  65. function() {
  66. var foundDoc = (next === null || next !== undefined);
  67. return foundDoc; //keep going until doc is found
  68. },
  69. function(err) {
  70. return callback(err, next);
  71. }
  72. );
  73. return next;
  74. };
  75. proto.coalesce = function coalesce(nextSource) {
  76. if (!(nextSource instanceof MatchDocumentSource))
  77. return false;
  78. this.matcher = new matcher({"$and": [this.getQuery(), nextSource.getQuery()]});
  79. return true;
  80. };
  81. proto.serialize = function(explain) {
  82. var out = {};
  83. out[this.getSourceName()] = this.getQuery();
  84. return out;
  85. };
  86. klass.uassertNoDisallowedClauses = function uassertNoDisallowedClauses(query) {
  87. for(var key in query){
  88. if(query.hasOwnProperty(key)){
  89. // can't use the Matcher API because this would segfault the constructor
  90. if (key === "$where") throw new Error("code 16395; $where is not allowed inside of a $match aggregation expression");
  91. // geo breaks if it is not the first portion of the pipeline
  92. if (key === "$near") throw new Error("code 16424; $near is not allowed inside of a $match aggregation expression");
  93. if (key === "$within") throw new Error("code 16425; $within is not allowed inside of a $match aggregation expression");
  94. if (key === "$nearSphere") throw new Error("code 16426; $nearSphere is not allowed inside of a $match aggregation expression");
  95. if (query[key] instanceof Object && query[key].constructor === Object) this.uassertNoDisallowedClauses(query[key]);
  96. }
  97. }
  98. };
  99. klass.createFromJson = function createFromJson(jsonElement, ctx) {
  100. if (!(jsonElement instanceof Object) || jsonElement.constructor !== Object)
  101. throw new Error("code 15959 ; the match filter must be an expression in an object");
  102. klass.uassertNoDisallowedClauses(jsonElement);
  103. var matcher = new MatchDocumentSource(jsonElement, ctx);
  104. return matcher;
  105. };
  106. proto.isTextQuery = function isTextQuery() {
  107. return this._isTextQuery;
  108. };
  109. klass.isTextQuery = function isTextQuery(query) {
  110. for (var key in query) { //jshint ignore:line
  111. var fieldName = key;
  112. if (fieldName === "$text") return true;
  113. if (query[key] instanceof Object && query[key].constructor === Object && this.isTextQuery(query[key])) {
  114. return true;
  115. }
  116. }
  117. return false;
  118. };
  119. klass.setSource = function setSource (source) {
  120. this.setSource(source);
  121. };
  122. proto.getQuery = function getQuery() {
  123. return this.matcher._pattern;
  124. };
  125. /** Returns the portion of the match that can safely be promoted to before a $redact.
  126. * If this returns an empty BSONObj, no part of this match may safely be promoted.
  127. *
  128. * To be safe to promote, removing a field from a document to be matched must not cause
  129. * that document to be accepted when it would otherwise be rejected. As an example,
  130. * {name: {$ne: "bob smith"}} accepts documents without a name field, which means that
  131. * running this filter before a redact that would remove the name field would leak
  132. * information. On the other hand, {age: {$gt:5}} is ok because it doesn't accept documents
  133. * that have had their age field removed.
  134. */
  135. proto.redactSafePortion = function redactSafePortion() {
  136. // This block contains the functions that make up the implementation of
  137. // DocumentSourceMatch::redactSafePortion(). They will only be called after
  138. // the Match expression has been successfully parsed so they can assume that
  139. // input is well formed.
  140. var isAllDigits = function(n) {
  141. return !isNaN(n);
  142. };
  143. var isFieldnameRedactSafe = function isFieldnameRedactSafe(field) {
  144. var dotPos = field.indexOf(".");
  145. if (dotPos === -1)
  146. return !isAllDigits(field);
  147. var part = field.slice(0, dotPos),
  148. rest = field.slice(dotPos+1, field.length);
  149. return !isAllDigits(part) && isFieldnameRedactSafe(rest);
  150. };
  151. // Returns the redact-safe portion of an "inner" match expression. This is the layer like
  152. // {$gt: 5} which does not include the field name. Returns an empty document if none of the
  153. // expression can safely be promoted in front of a $redact.
  154. var redactSavePortionDollarOps = function redactSafePortionDollarOps(expr) { //jshint maxcomplexity:23
  155. var output = {},
  156. elem, i, j;
  157. var keys = Object.keys(expr);
  158. for (i = 0; i < keys.length; i++) {
  159. var field = keys[i],
  160. value = expr[field];
  161. if (field[0] !== "$")
  162. continue;
  163. // Ripped the case apart and did not implement this painful thing:
  164. // https://github.com/mongodb/mongo/blob/r2.5.4/src/mongo/db/jsobj.cpp#L286
  165. // Somebody should be taken to task for that work of art.
  166. if (field === "$type" || field === "$regex" || field === "$options" || field === "$mod") {
  167. output[field] = value;
  168. } else if (field === "$lte" || field === "$gte" || field === "$lt" || field === "$gt") {
  169. if (isTypeRedactSafeInComparison(field))
  170. output[field] = value;
  171. } else if (field === "$in") {
  172. // TODO: value/elem/field/etc may be mixed up and wrong here
  173. var allOk = true;
  174. for (j = 0; j < Object.keys(value).length; j++) {
  175. elem = Object.keys(value)[j];
  176. if (!isTypeRedactSafeInComparison(value[elem])) {
  177. allOk = false;
  178. break;
  179. }
  180. }
  181. if (allOk) {
  182. output[field] = value;
  183. }
  184. break;
  185. } else if (field === "$all") {
  186. // TODO: value/elem/field/etc may be mixed up and wrong here
  187. var matches = [];
  188. for (j = 0; j < value.length; j++) {
  189. elem = Object.keys(value)[j];
  190. if (isTypeRedactSafeInComparison(value[elem]))
  191. matches.push(value[elem]);
  192. }
  193. if (matches.length)
  194. output[field] = matches;
  195. } else if (field === "$elemMatch") {
  196. var subIn = value,
  197. subOut;
  198. if (subIn[0] === "$")
  199. subOut = redactSafePortionDollarOps(subIn);
  200. else
  201. subOut = redactSafePortionTopLevel(subIn);
  202. if (subOut && Object.keys(subOut).length)
  203. output[field] = subOut;
  204. break;
  205. } else {
  206. // never allowed:
  207. // equality, maxDist, near, ne, opSize, nin, exists, within, geoIntersects
  208. continue;
  209. }
  210. }
  211. return output;
  212. };
  213. var isTypeRedactSafeInComparison = function isTypeRedactSafeInComparison(type) {
  214. if (type instanceof Array || (type instanceof Object && type.constructor === Object) || type === null || type === undefined)
  215. return false;
  216. return true;
  217. };
  218. // Returns the redact-safe portion of an "outer" match expression. This is the layer like
  219. // {fieldName: {...}} which does include the field name. Returns an empty document if none of
  220. // the expression can safely be promoted in front of a $redact.
  221. var redactSafePortionTopLevel = function(topQuery) { //jshint maxcomplexity:18
  222. var output = {},
  223. okClauses = [],
  224. keys = topQuery ? Object.keys(topQuery) : [],
  225. j, elm, clause;
  226. for (var i = 0; i < keys.length; i++) {
  227. var field = keys[i],
  228. value = topQuery[field];
  229. if (field.length && field[0] === "$") {
  230. if (field === "$or") {
  231. okClauses = [];
  232. for (j = 0; j < Object.keys(value).length; j++) {
  233. elm = value[Object.keys(value)[j]];
  234. clause = redactSafePortionTopLevel(elm);
  235. if (!clause || Object.keys(clause).length === 0) {
  236. okClauses = [];
  237. break;
  238. }
  239. okClauses.push(clause);
  240. }
  241. if (okClauses && okClauses.length) {
  242. output.$or = okClauses;
  243. }
  244. } else if (field === "$and") {
  245. okClauses = [];
  246. for (j = 0; j < Object.keys(value).length; j++) {
  247. elm = value[Object.keys(value)[j]];
  248. clause = redactSafePortionTopLevel(elm);
  249. if (clause && Object.keys(clause).length)
  250. okClauses.push(clause);
  251. }
  252. if (okClauses.length)
  253. output.$and = okClauses;
  254. }
  255. continue;
  256. }
  257. if (!isFieldnameRedactSafe(field))
  258. continue;
  259. if (value instanceof Array || !value) {
  260. continue;
  261. } else if (value instanceof Object && value.constructor === Object) {
  262. // subobjects (not regex etc)
  263. var sub = redactSavePortionDollarOps(value);
  264. if (sub && Object.keys(sub).length)
  265. output[field] = sub;
  266. break;
  267. } else {
  268. output[field] = value;
  269. }
  270. }
  271. return output;
  272. };
  273. return redactSafePortionTopLevel(this.getQuery());
  274. };