From d22098f810037a27a8bdb978b46c8adb2f3a39e7 Mon Sep 17 00:00:00 2001 From: Eliot Horowitz Date: Mon, 2 Nov 2009 10:51:42 -0500 Subject: [PATCH] get rid of deep in Matcher --- db/matcher.cpp | 38 +++++++++++++++----------------------- db/matcher.h | 14 ++++++-------- db/query.cpp | 17 ++++++----------- 3 files changed, 27 insertions(+), 42 deletions(-) diff --git a/db/matcher.cpp b/db/matcher.cpp index c1df954786b..a2d376aedef 100644 --- a/db/matcher.cpp +++ b/db/matcher.cpp @@ -93,9 +93,9 @@ namespace mongo { ); } - bool KeyValJSMatcher::matches(const BSONObj &key, const DiskLoc &recLoc, bool *deep) { + bool KeyValJSMatcher::matches(const BSONObj &key, const DiskLoc &recLoc ) { if ( _keyMatcher.keyMatch() ) { - if ( !_keyMatcher.matches(key, deep) ) { + if ( !_keyMatcher.matches(key) ) { return false; } } @@ -104,7 +104,7 @@ namespace mongo { return true; } - return _recordMatcher.matches(recLoc.rec(), deep); + return _recordMatcher.matches(recLoc.rec()); } @@ -268,7 +268,7 @@ namespace mongo { constrainIndexKey_ = constrainIndexKey; } - inline int JSMatcher::valuesMatch(const BSONElement& l, const BSONElement& r, int op, const BasicMatcher& bm, bool *deep) { + inline int JSMatcher::valuesMatch(const BSONElement& l, const BSONElement& r, int op, const BasicMatcher& bm) { assert( op != BSONObj::NE && op != BSONObj::NIN ); if ( op == 0 ) @@ -314,8 +314,8 @@ namespace mongo { return (op & z); } - int JSMatcher::matchesNe(const char *fieldName, const BSONElement &toMatch, const BSONObj &obj, const BasicMatcher& bm, bool *deep) { - int ret = matchesDotted( fieldName, toMatch, obj, BSONObj::Equality, bm, deep ); + int JSMatcher::matchesNe(const char *fieldName, const BSONElement &toMatch, const BSONObj &obj, const BasicMatcher& bm ) { + int ret = matchesDotted( fieldName, toMatch, obj, BSONObj::Equality, bm ); if ( bm.toMatch.type() != jstNULL ) return ( ret <= 0 ) ? 1 : 0; else @@ -334,7 +334,6 @@ namespace mongo { toMatch - element we want to match. obj - database object to check against compareOp - Equality, LT, GT, etc. - deep - out param. set to true/false if we scanned an array isArr - Special forms: @@ -348,7 +347,7 @@ namespace mongo { 0 missing element 1 match */ - int JSMatcher::matchesDotted(const char *fieldName, const BSONElement& toMatch, const BSONObj& obj, int compareOp, const BasicMatcher& bm , bool *deep, bool isArr) { + int JSMatcher::matchesDotted(const char *fieldName, const BSONElement& toMatch, const BSONObj& obj, int compareOp, const BasicMatcher& bm , bool isArr) { if ( compareOp == BSONObj::opALL ) { if ( bm.myset->size() == 0 ) @@ -371,10 +370,10 @@ namespace mongo { } if ( compareOp == BSONObj::NE ) - return matchesNe( fieldName, toMatch, obj, bm, deep ); + return matchesNe( fieldName, toMatch, obj, bm ); if ( compareOp == BSONObj::NIN ) { for( set::const_iterator i = bm.myset->begin(); i != bm.myset->end(); ++i ) { - int ret = matchesNe( fieldName, *i, obj, bm, deep ); + int ret = matchesNe( fieldName, *i, obj, bm ); if ( ret != 1 ) return ret; } @@ -394,9 +393,8 @@ namespace mongo { BSONElement z = ai.next(); if ( z.type() == Object ) { BSONObj eo = z.embeddedObject(); - int cmp = matchesDotted(fieldName, toMatch, eo, compareOp, bm, deep, false); + int cmp = matchesDotted(fieldName, toMatch, eo, compareOp, bm, false); if ( cmp > 0 ) { - if ( deep ) *deep = true; return 1; } else if ( cmp < 0 ) { found = true; @@ -416,7 +414,7 @@ namespace mongo { return retMissing( bm ); BSONObj eo = se.embeddedObject(); - return matchesDotted(p+1, toMatch, eo, compareOp, bm, deep, se.type() == Array); + return matchesDotted(p+1, toMatch, eo, compareOp, bm, se.type() == Array); } else { e = obj.getField(fieldName); } @@ -425,15 +423,13 @@ namespace mongo { if ( compareOp == BSONObj::opEXISTS ) { return ( e.eoo() ^ toMatch.boolean() ) ? 1 : -1; } else if ( ( e.type() != Array || indexed || compareOp == BSONObj::opSIZE ) && - valuesMatch(e, toMatch, compareOp, bm, deep) ) { + valuesMatch(e, toMatch, compareOp, bm ) ) { return 1; } else if ( e.type() == Array && compareOp != BSONObj::opSIZE ) { BSONObjIterator ai(e.embeddedObject()); while ( ai.moreWithEOO() ) { BSONElement z = ai.next(); if ( valuesMatch( z, toMatch, compareOp, bm) ) { - if ( deep ) - *deep = true; return 1; } } @@ -468,12 +464,8 @@ namespace mongo { } /* See if an object matches the query. - deep - return true when means we looked into arrays for a match */ - bool JSMatcher::matches(const BSONObj& jsobj, bool *deep) { - if ( deep ) - *deep = false; - + bool JSMatcher::matches(const BSONObj& jsobj ) { /* assuming there is usually only one thing to match. if more this could be slow sometimes. */ @@ -482,7 +474,7 @@ namespace mongo { BasicMatcher& bm = basics[i]; BSONElement& m = bm.toMatch; // -1=mismatch. 0=missing element. 1=match - int cmp = matchesDotted(m.fieldName(), m, jsobj, bm.compareOp, bm, deep); + int cmp = matchesDotted(m.fieldName(), m, jsobj, bm.compareOp, bm ); if ( cmp < 0 ) return false; if ( cmp == 0 ) { @@ -505,7 +497,7 @@ namespace mongo { if ( !e.eoo() ) s.insert( e ); } else { - jsobj.getFieldsDotted( rm.fieldName, s, deep ); + jsobj.getFieldsDotted( rm.fieldName, s ); } bool match = false; for( BSONElementSet::const_iterator i = s.begin(); i != s.end(); ++i ) diff --git a/db/matcher.h b/db/matcher.h index 1cc7e3cbe43..faccfd20168 100644 --- a/db/matcher.h +++ b/db/matcher.h @@ -115,12 +115,12 @@ namespace mongo { int matchesDotted( const char *fieldName, const BSONElement& toMatch, const BSONObj& obj, - int compareOp, const BasicMatcher& bm, bool *deep, bool isArr = false); + int compareOp, const BasicMatcher& bm, bool isArr = false); int matchesNe( const char *fieldName, const BSONElement &toMatch, const BSONObj &obj, - const BasicMatcher&bm, bool *deep); + const BasicMatcher&bm); public: static int opDirection(int op) { @@ -133,9 +133,7 @@ namespace mongo { ~JSMatcher(); - /* deep - means we looked into arrays for a match - */ - bool matches(const BSONObj& j, bool *deep = 0); + bool matches(const BSONObj& j); bool keyMatch() const { return !all && !haveSize && !hasArray; } private: @@ -146,7 +144,7 @@ namespace mongo { basics.push_back( BasicMatcher( e , c ) ); } - int valuesMatch(const BSONElement& l, const BSONElement& r, int op, const BasicMatcher& bm , bool *deep=0); + int valuesMatch(const BSONElement& l, const BSONElement& r, int op, const BasicMatcher& bm); Where *where; // set if query uses $where BSONObj jsobj; // the query pattern. e.g., { name: "joe" } @@ -171,8 +169,8 @@ namespace mongo { class KeyValJSMatcher : boost::noncopyable { public: KeyValJSMatcher(const BSONObj &pattern, const BSONObj &indexKeyPattern); - bool matches(const BSONObj &j, bool *deep = 0); - bool matches(const BSONObj &key, const DiskLoc &recLoc, bool *deep = 0); + bool matches(const BSONObj &j); + bool matches(const BSONObj &key, const DiskLoc &recLoc); bool needRecord(){ return _needRecord; } private: JSMatcher _keyMatcher; diff --git a/db/query.cpp b/db/query.cpp index 2c539941da0..35810c63aed 100644 --- a/db/query.cpp +++ b/db/query.cpp @@ -69,8 +69,7 @@ namespace mongo { DiskLoc rloc = c_->currLoc(); - bool deep; - if ( matcher_->matches(c_->currKey(), rloc, &deep) ) { + if ( matcher_->matches(c_->currKey(), rloc ) ) { if ( !c_->getsetdup(rloc) ) ++count_; } @@ -138,8 +137,7 @@ namespace mongo { do { DiskLoc rloc = c->currLoc(); - bool deep; - if ( !matcher.matches(c->currKey(), rloc, &deep) ) { + if ( !matcher.matches(c->currKey(), rloc ) ) { c->advance(); // advance must be after noMoreMatches() because it uses currKey() } else { @@ -285,11 +283,10 @@ namespace mongo { cc = 0; break; } - bool deep; - if ( !cc->matcher->matches(c->currKey(), c->currLoc(), &deep) ) { + if ( !cc->matcher->matches(c->currKey(), c->currLoc() ) ) { } else { - //out() << "matches " << c->currLoc().toString() << ' ' << deep << '\n'; + //out() << "matches " << c->currLoc().toString() << '\n'; if( c->getsetdup(c->currLoc()) ) { //out() << " but it's a dup \n"; } @@ -375,8 +372,7 @@ namespace mongo { ++count_; } } else { - bool deep; - if ( !matcher_->matches(c_->currKey(), c_->currLoc(), &deep) ) { + if ( !matcher_->matches(c_->currKey(), c_->currLoc() ) ) { } else if( !c_->getsetdup(c_->currLoc()) ) { ++count_; @@ -513,8 +509,7 @@ namespace mongo { } nscanned_++; - bool deep; - if ( !matcher_->matches(c_->currKey(), c_->currLoc(), &deep) ) { + if ( !matcher_->matches(c_->currKey(), c_->currLoc() ) ) { ; } else {