Browse Source

Refs #3359. Updates to Matcher

Spencer Rathbun 12 years ago
parent
commit
fcbc76c6c9

+ 2 - 2
lib/pipeline/documentSources/MatchDocumentSource.js

@@ -19,7 +19,7 @@ var MatchDocumentSource = module.exports = function MatchDocumentSource(query, c
 	if (!query) throw new Error("arg `query` is required");
 	base.call(this, ctx);
 	this.query = query; // save the query, so we can check it for deps later. THIS IS A DEVIATION FROM THE MONGO IMPLEMENTATION
-	this.matcher = matcher(query);
+	this.matcher = new matcher(query);
 }, klass = MatchDocumentSource, base = require('./FilterBaseDocumentSource'), proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
 
 klass.matchName = "$match";
@@ -39,7 +39,7 @@ proto.getSourceName = function getSourceName(){
  * @param	{Boolean}	explain	create explain output
  **/
 proto.sourceToJson = function sourceToJson(builder, explain) {
-	builder[this.getSourceName()] = this.matcher.query;
+	builder[this.getSourceName()] = this.matcher._pattern;
 };
 
 /**

+ 17 - 14
lib/pipeline/matcher/MatchExpression.js

@@ -7,6 +7,9 @@ var MatchExpression = module.exports = function MatchExpression( type ){
 	this._matchType = type;
 }, klass = MatchExpression, base =  Object, proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
 
+// DEPENDENCIES
+var errors = require("../../Errors.js"),
+	ErrorCodes = errors.ErrorCodes;
 
 // File: expression.h lines: 172-172
 //         MatchType _matchType;
@@ -20,7 +23,7 @@ proto._matchType = undefined;
 proto._tagData = undefined;
 
 /**
- * 
+ *
  * This documentation was automatically generated. Please update when you touch this function.
  * @method _debugAddSpace
  * @param
@@ -33,7 +36,7 @@ proto._debugAddSpace = function _debugAddSpace( level ){ //  StringBuilder& debu
 
 
 /**
- * 
+ *
  * This documentation was automatically generated. Please update when you touch this function.
  * @method getChild
  * @param
@@ -47,7 +50,7 @@ proto.getChild = function getChild( ) {//  size_t i
 
 
 /**
- * 
+ *
  * This documentation was automatically generated. Please update when you touch this function.
  * @method getTag
  * @param
@@ -60,7 +63,7 @@ proto.getTag = function getTag( ){
 
 
 /**
- * 
+ *
  * This documentation was automatically generated. Please update when you touch this function.
  * @method isArray
  * @param
@@ -85,7 +88,7 @@ proto.isArray = function isArray( /*  */ ){
 
 
 /**
- * 
+ *
  * This documentation was automatically generated. Please update when you touch this function.
  * @method isLeaf
  * @param
@@ -98,7 +101,7 @@ proto.isLeaf = function isLeaf( /*  */ ){
 
 
 /**
- * 
+ *
  * This documentation was automatically generated. Please update when you touch this function.
  * @method isLogical
  * @param
@@ -120,7 +123,7 @@ proto.isLogical = function isLogical( /*  */ ){
 
 
 /**
- * 
+ *
  * This documentation was automatically generated. Please update when you touch this function.
  * @method matchType
  * @param
@@ -133,25 +136,25 @@ proto.matchType = function matchType( /*  */ ){
 
 
 /**
- * 
+ *
  * This documentation was automatically generated. Please update when you touch this function.
  * @method matchesBSON
  * @param
  *
  */
-proto.matchesBSON = function matchesBSON( doc,detail ){ //  const BSONObj& doc, MatchDetails* details
+proto.matchesBSON = function matchesBSON(doc, details){ //  const BSONObj& doc, MatchDetails* details
 
 // File: expression.cpp lines: 42-44
 //     bool MatchExpression::matchesBSON( const BSONObj& doc, MatchDetails* details ) const {
 //         BSONMatchableDocument mydoc( doc );
 //         return matches( &mydoc, details );
 //     }
-	return matches(new MatchableDocument(doc),details);
+	return this.matches(doc, details);
 };
 
 
 /**
- * 
+ *
  * This documentation was automatically generated. Please update when you touch this function.
  * @method numChildren
  * @param
@@ -164,7 +167,7 @@ proto.numChildren = function numChildren( /*  */ ){
 
 
 /**
- * 
+ *
  * This documentation was automatically generated. Please update when you touch this function.
  * @method path
  * @param
@@ -178,7 +181,7 @@ proto.path = function path( /*  */ ){
 
 
 /**
- * 
+ *
  * This documentation was automatically generated. Please update when you touch this function.
  * @method setTag
  * @param
@@ -192,7 +195,7 @@ proto.setTag = function setTag( /* TagData* data */ ){
 
 
 /**
- * 
+ *
  * This documentation was automatically generated. Please update when you touch this function.
  * @method toString
  * @param

+ 18 - 18
lib/pipeline/matcher/Matcher2.js

@@ -1,18 +1,28 @@
 "use strict";
 
 // Autogenerated by cport.py on 2013-09-17 14:37
-var Matcher2 = module.exports = function (docMatcher, constrainIndexKey){
+var Matcher2 = module.exports = function Matcher2(pattern, nested){
+	// File: matcher.cpp lines: 83-92
+	this._pattern = pattern;
+	var result = this.parser.parse(pattern);
+	if (result.code != ErrorCodes.OK)
+		return {code:16810, description:"bad query: " + result};
+	this._expression = result.result;
+
 	// File: matcher.cpp lines: 94-102
-	var indexExpression = this.spliceForIndex(constrainIndexKey, docMatcher._expression.get(), this._spliceInfo);
+	/*
+	var indexExpression = this.spliceForIndex(constrainIndexKey, docMatcher, this._spliceInfo);
 	if (indexExpression) {
-		this._expression.reset(indexExpression);
+		this._expression = indexExpression;
 	}
-}, klass = Matcher2, base =  Object  , proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
+	*/
+}, klass = Matcher2, base = Object, proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
 
 // DEPENDENCIES
 var errors = require("../../Errors.js"),
 	ErrorCodes = errors.ErrorCodes,
 	MatchExpression = require("./MatchExpression.js"),
+	MatchExpressionParser = require("./MatchExpressionParser.js"),
 	FalseMatchExpression = require("./FalseMatchExpression.js"),
 	ComparisonMatchExpression = require("./ComparisonMatchExpression.js"),
 	InMatchExpression = require("./InMatchExpression.js"),
@@ -34,17 +44,7 @@ proto._pattern = undefined;
 // File: matcher.h lines: 84-84
 proto._spliceInfo = undefined;
 
-// File: matcher.h lines: 82-82
-proto._expression = undefined;
-
-// File: matcher.h lines: 80-80
-proto._indexKey = undefined;
-
-// File: matcher.h lines: 79-79
-proto._pattern = undefined;
-
-// File: matcher.h lines: 84-84
-proto._spliceInfo = undefined;
+proto.parser = new MatchExpressionParser();
 
 /**
  *
@@ -56,7 +56,7 @@ proto._spliceInfo = undefined;
 proto._spliceForIndex = function _spliceForIndex(keys, full, spliceInfo){
 	// File: matcher.cpp lines: 236-380
 	var dup, i, obj, lme;
-	switch (full.matchType()) {
+	switch (full) {
 		case MatchExpression.ALWAYS_FALSE:
 			return new FalseMatchExpression();
 
@@ -273,10 +273,10 @@ proto.matches = function matches(doc, details){
 	if (!this._expression)
 		return true;
 
-	if (this._indexKey.isEmpty())
+	if (this._indexKey == {})
 		return this._expression.matchesBSON(doc, details);
 
-	if (!doc.isEmpty() && doc.firstElement().fieldName()[0])
+	if ((doc != {}) && (Object.keys(doc)[0]))
 		return this._expression.matchesBSON(doc, details);
 
 	var mydoc = new IndexKeyMatchableDocument(this._indexKey, doc);