Просмотр исходного кода

Refs #3282. Ported ArrayMatchingMatchExpression FTW.

Spencer Rathbun 12 лет назад
Родитель
Сommit
c863643b40
1 измененных файлов с 69 добавлено и 144 удалено
  1. 69 144
      lib/pipeline/matcher/ArrayMatchingMatchExpression.js

+ 69 - 144
lib/pipeline/matcher/ArrayMatchingMatchExpression.js

@@ -1,200 +1,125 @@
-"use strict"
-
-
+"use strict";
 
 // Autogenerated by cport.py on 2013-09-17 14:37
-var ArrayMatchingMatchExpression = module.exports = function (){
-
+var ArrayMatchingMatchExpression = module.exports = function (matchType){
+	this._matchType = matchType;
 }, klass = ArrayMatchingMatchExpression, base =  Object  , proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
 
-
 // File: expression_array.h lines: 55-55
-//         ElementPath _elementPath;
-
 proto._elementPath = undefined;
 
-
 // File: expression_array.h lines: 54-54
-//         StringData _path;
-
 proto._path = undefined;
 
-
 // File: expression_array.h lines: 55-55
-//         ElementPath _elementPath;
-
 proto._elementPath = undefined;
 
-
 // File: expression_array.h lines: 54-54
-//         StringData _path;
-
 proto._path = undefined;
 
-
-
-
-
 /**
- * 
- * This documentation was automatically generated. Please update when you touch this function.
- * @method ArrayMatchingMatchExpression
- * @param
  *
- */
-proto.ArrayMatchingMatchExpression = function ArrayMatchingMatchExpression( /*  MatchType matchType  */ ){
-// File: expression_array.h lines: 36-35
-//         ArrayMatchingMatchExpression( MatchType matchType ) : MatchExpression( matchType ){}
-
-
-
-
-}
-
-
-/**
- * 
- * This documentation was automatically generated. Please update when you touch this function.
+ * Check if the input element is equivalent to us
  * @method equivalent
  * @param
  *
  */
-proto.equivalent = function equivalent( /*  const MatchExpression* other  */ ){
+proto.equivalent = function equivalent(other){
+	// File: expression_array.cpp lines: 63-79
+	if ( this._matchType != other._matchType)
+		return false;
 
-// File: expression_array.cpp lines: 63-79
-//     bool ArrayMatchingMatchExpression::equivalent( const MatchExpression* other ) const {
-//         if ( matchType() != other->matchType() )
-//             return false;
-// 
-//         const ArrayMatchingMatchExpression* realOther =
-//             static_cast<const ArrayMatchingMatchExpression*>( other );
-// 
-//         if ( _path != realOther->_path )
-//             return false;
-// 
-//         if ( numChildren() != realOther->numChildren() )
-//             return false;
-// 
-//         for ( unsigned i = 0; i < numChildren(); i++ )
-//             if ( !getChild(i)->equivalent( realOther->getChild(i) ) )
-//                 return false;
-//         return true;
-//     }
+	var realOther = new ArrayMatchingMatchExpression(other);
 
+	if (this._path != realOther._path)
+		return false;
 
+	if (this.numChildren() != realOther.numChildren())
+		return false;
 
-}
-
+	for (var i = 0; i < this.numChildren(); i++)
+		if (!this.getChild(i).equivalent(realOther.getChild(i)))
+			return false;
+	return true;
+};
 
 /**
- * 
- * This documentation was automatically generated. Please update when you touch this function.
+ *
+ * Initialize the input path as our element path
  * @method initPath
- * @param
+ * @param path
  *
  */
-proto.initPath = function initPath( /*  const StringData& path  */ ){
-
-// File: expression_array.cpp lines: 27-31
-//     Status ArrayMatchingMatchExpression::initPath( const StringData& path ) {
-//         _path = path;
-//         Status s = _elementPath.init( _path );
-//         _elementPath.setTraverseLeafArray( false );
-//         return s;
-//     }
-
-
-
-}
-
+proto.initPath = function initPath(path){
+	// File: expression_array.cpp lines: 27-31
+	this._path = path;
+	var status = this._elementPath.init(this._path);
+	this._elementPath.setTraverseLeafArray(false);
+	return status;
+};
 
 /**
- * 
+ *
  * This documentation was automatically generated. Please update when you touch this function.
  * @method matches
  * @param
  *
  */
-proto.matches = function matches( /*  const MatchableDocument* doc, MatchDetails* details  */ ){
-
-// File: expression_array.cpp lines: 34-53
-//     bool ArrayMatchingMatchExpression::matches( const MatchableDocument* doc, MatchDetails* details ) const {
-// 
-//         boost::scoped_ptr<ElementIterator> cursor( doc->getIterator( _elementPath ) );
-// 
-//         while ( cursor->more() ) {
-//             ElementIterator::Context e = cursor->next();
-//             if ( e.element().type() != Array )
-//                 continue;
-// 
-//             bool amIRoot = e.arrayOffset().eoo();
-// 
-//             if ( !matchesArray( e.element().Obj(), amIRoot ? details : NULL ) )
-//                 continue;
-// 
-//             if ( !amIRoot && details && details->needRecord() && !e.arrayOffset().eoo() ) {
-//                 details->setElemMatchKey( e.arrayOffset().fieldName() );
-//             }
-//             return true;
-//         }
-//         return false;
-//     }
-
-
-
-}
-
+proto.matches = function matches(doc, details){
+	// File: expression_array.cpp lines: 34-53
+	var element;
+	for (var k in doc[this._elementPath]) {
+		element = doc[this._elementPath][k];
+		if (!(element instanceof Array))
+			continue;
+
+		var amIRoot = (element.length === 0);
+
+		if (!this.matchesArray(element, amIRoot ? details : null))
+			continue;
+
+		if (!amIRoot && details && details.needRecord() /* && !e.arrayOffset().eoo() same as amIRoot? */) {
+			details.setElemMatchKey(element.fieldName());
+		}
+		return true;
+	}
+	return false;
+};
 
 /**
- * 
+ *
  * This documentation was automatically generated. Please update when you touch this function.
  * @method matchesSingleElement
  * @param
  *
  */
-proto.matchesSingleElement = function matchesSingleElement( /*  const BSONElement& e  */ ){
-
-// File: expression_array.cpp lines: 56-59
-//     bool ArrayMatchingMatchExpression::matchesSingleElement( const BSONElement& e ) const {
-//         if ( e.type() != Array )
-//             return false;
-//         return matchesArray( e.Obj(), NULL );
-//     }
-
-
-
-}
-
+proto.matchesSingleElement = function matchesSingleElement(element){
+	// File: expression_array.cpp lines: 56-59
+	if (!(element instanceof Array))
+		return false;
+	return this.matchesArray(element, null);
+};
 
 /**
- * 
+ *
  * This documentation was automatically generated. Please update when you touch this function.
  * @method path
  * @param
  *
  */
-proto.path = function path( /*  */ ){
-// File: expression_array.h lines: 52-51
-//         const StringData path() const { return _path; }
-
-
-
-
-}
-
+proto.path = function path(){
+	// File: expression_array.h lines: 52-51
+	return this._path;
+};
 
 /**
- * 
- * This documentation was automatically generated. Please update when you touch this function.
- * @method ~ArrayMatchingMatchExpression
- * @param
+ *
+ * Check if the input array matches
+ * @method path
+ * @param anArray
+ * @param details
  *
  */
-proto.~ArrayMatchingMatchExpression = function ~ArrayMatchingMatchExpression( /*  */ ){
-// File: expression_array.h lines: 37-36
-//         virtual ~ArrayMatchingMatchExpression(){}
-
-
-
-
-}
+proto.matchesArray = function matchesArray(anArray, details){
+	throw new Error("not implemented");
+};