Browse Source

Refs #3424. Converted TypeMatchExpression to use elementPath match method.

Spencer Rathbun 12 years ago
parent
commit
7665698e79
1 changed files with 12 additions and 47 deletions
  1. 12 47
      lib/pipeline/matcher/TypeMatchExpression.js

+ 12 - 47
lib/pipeline/matcher/TypeMatchExpression.js

@@ -92,56 +92,21 @@ proto.init = function init( path,type ) { //  const StringData& path, int type
  * @param
  *
  */
+proto.matches = function matches(doc,details) {
+	// File: expression_leaf.cpp lines: 318-332
+	var self = this,
+		checker = function(element) {
+			if (!this.matchesSingleElement(element, details))
+				return false;
 
-proto.matches = function matches( doc,details ) { //  const MatchableDocument* doc, MatchDetails* details
-// File: expression_leaf.cpp lines: 318-332
-	return this._matches(doc,this._path,details);
-};
+			//var amIRoot = (element.length === 0);
 
-proto._matches = function _matches(doc,path,details) {
-	var element, k, item,
-		curr = doc;
-	for (k = 0; k < path.length; k++) {
-		item = curr[path[k]];
-		if (item instanceof Object && item.constructor === Object) {
-			curr = item;
-			continue;
-		} else if (item instanceof Object && item.constructor === Array) {
-			if (k == path.length-1) {
-				curr = item;
-				break; // this is the end of the path, so check this array
-			} else if (!(isNaN(parseInt(path[k+1], 10)))) {
-				curr = item;
-				continue; // the *next* path section is an item in the array so we don't check this whole array
-			}
-			// otherwise, check each item in the array against the rest of the path
-			for(var ii = 0, il = item.length; ii < il; ii++){
-				var subitem = item[ii];
-				if (subitem.constructor !== Object) continue;	// can't look for a subfield in a non-object value.
-				if (this._matches(subitem, path.slice(k), null)) { // check the item against the rest of the path
-					if (details && details.needRecord())
-						details.setElemMatchKey(ii.toString());
-					return true;
-				}
+			if (details && details.needRecord()) {
+				details.setElemMatchKey(element);
 			}
-			return false; // checked all items in the array and found no matches
-		}
-	}
-
-	// we got the whole path, now check it
-	element = item;
-
-	//var amIRoot = (element.length === 0);
-
-	if (!this.matchesSingleElement(element, details))
-		return false;
-
-	/*
-	if (!amIRoot && details && details.needRecord() {
-		details.setElemMatchKey(element);
-	}
-	*/
-	return true;
+			return true;
+		};
+	return this._elementPath._matches(doc, details, checker);
 };