diff options
author | Eliot Horowitz <eliot@10gen.com> | 2013-05-08 10:26:44 -0400 |
---|---|---|
committer | Eliot Horowitz <eliot@10gen.com> | 2013-05-09 10:16:55 -0400 |
commit | 3893e7ff97d576652756456fafba2936092e15ce (patch) | |
tree | 124d77e989e9bad3264b4111e0570aead5df23a8 /src | |
parent | f41cc12bf4672528f513fb81f1fab4b4b379ae83 (diff) | |
download | mongo-3893e7ff97d576652756456fafba2936092e15ce.tar.gz |
SERVER-6400 Use an abstraction layer into MatchExpression for different formats
currently BSONObj vs index keys
Diffstat (limited to 'src')
22 files changed, 702 insertions, 484 deletions
diff --git a/src/mongo/SConscript b/src/mongo/SConscript index 3f867df752d..7beaa6b3f5f 100644 --- a/src/mongo/SConscript +++ b/src/mongo/SConscript @@ -120,6 +120,7 @@ env.StaticLibrary('expressions', 'db/matcher/expression_tree.cpp', 'db/matcher/expression_parser.cpp', 'db/matcher/expression_parser_tree.cpp', + 'db/matcher/matchable.cpp', 'db/matcher/match_details.cpp'], LIBDEPS=['bson', '$BUILD_DIR/mongo/db/common', diff --git a/src/mongo/db/matcher/expression.cpp b/src/mongo/db/matcher/expression.cpp index e2372aa14b6..dd1881b0e05 100644 --- a/src/mongo/db/matcher/expression.cpp +++ b/src/mongo/db/matcher/expression.cpp @@ -41,6 +41,12 @@ namespace mongo { debug << " "; } + bool MatchExpression::matchesBSON( const BSONObj& doc, MatchDetails* details ) const { + BSONMatchableDocument mydoc( doc ); + return matches( &mydoc, details ); + } + + void AtomicMatchExpression::debugString( StringBuilder& debug, int level ) const { _debugAddSpace( debug, level ); debug << "$atomic\n"; diff --git a/src/mongo/db/matcher/expression.h b/src/mongo/db/matcher/expression.h index 1a820da4a77..320b3909fd0 100644 --- a/src/mongo/db/matcher/expression.h +++ b/src/mongo/db/matcher/expression.h @@ -21,6 +21,7 @@ #include "mongo/base/disallow_copying.h" #include "mongo/base/status.h" #include "mongo/bson/bsonobj.h" +#include "mongo/db/matcher/matchable.h" #include "mongo/db/matcher/match_details.h" namespace mongo { @@ -58,7 +59,9 @@ namespace mongo { * determins if the doc matches the expression * there could be an expression that looks at fields, or the entire doc */ - virtual bool matches( const BSONObj& doc, MatchDetails* details = 0 ) const = 0; + virtual bool matches( const MatchableDocument* doc, MatchDetails* details = 0 ) const = 0; + + virtual bool matchesBSON( const BSONObj& doc, MatchDetails* details = 0 ) const; /** * does the element match the expression @@ -92,7 +95,7 @@ namespace mongo { public: AtomicMatchExpression() : MatchExpression( SPECIAL, ATOMIC ){} - virtual bool matches( const BSONObj& doc, MatchDetails* details = 0 ) const { + virtual bool matches( const MatchableDocument* doc, MatchDetails* details = 0 ) const { return true; } diff --git a/src/mongo/db/matcher/expression_array.cpp b/src/mongo/db/matcher/expression_array.cpp index f58655044bf..2d469e47b84 100644 --- a/src/mongo/db/matcher/expression_array.cpp +++ b/src/mongo/db/matcher/expression_array.cpp @@ -33,13 +33,13 @@ namespace mongo { return Status::OK(); } - bool AllMatchExpression::matches( const BSONObj& doc, MatchDetails* details ) const { + bool AllMatchExpression::matches( const MatchableDocument* doc, MatchDetails* details ) const { FieldRef path; path.parse(_path); bool traversedArray = false; int32_t idxPath = 0; - BSONElement e = getFieldDottedOrArray( doc, path, &idxPath, &traversedArray ); + BSONElement e = doc->getFieldDottedOrArray( path, &idxPath, &traversedArray ); string rest = pathToString( path, idxPath+1 ); @@ -129,14 +129,14 @@ namespace mongo { // ------- - bool ArrayMatchingMatchExpression::matches( const BSONObj& doc, MatchDetails* details ) const { + bool ArrayMatchingMatchExpression::matches( const MatchableDocument* doc, MatchDetails* details ) const { FieldRef path; path.parse(_path); bool traversedArray = false; int32_t idxPath = 0; - BSONElement e = getFieldDottedOrArray( doc, path, &idxPath, &traversedArray ); + BSONElement e = doc->getFieldDottedOrArray( path, &idxPath, &traversedArray ); string rest = pathToString( path, idxPath+1 ); @@ -214,7 +214,7 @@ namespace mongo { BSONElement inner = i.next(); if ( !inner.isABSONObj() ) continue; - if ( _sub->matches( inner.Obj(), NULL ) ) { + if ( _sub->matchesBSON( inner.Obj(), NULL ) ) { if ( details && details->needRecord() ) { details->setElemMatchKey( inner.fieldName() ); } @@ -306,9 +306,9 @@ namespace mongo { _list.push_back( expr ); } - bool AllElemMatchOp::matches( const BSONObj& doc, MatchDetails* details ) const { + bool AllElemMatchOp::matches( const MatchableDocument* doc, MatchDetails* details ) const { BSONElementSet all; - doc.getFieldsDotted( _path, all, false ); + doc->getFieldsDotted( _path, all, false ); for ( BSONElementSet::const_iterator i = all.begin(); i != all.end(); ++i ) { BSONElement sub = *i; diff --git a/src/mongo/db/matcher/expression_array.h b/src/mongo/db/matcher/expression_array.h index 1d7a2f621d5..5af413d8164 100644 --- a/src/mongo/db/matcher/expression_array.h +++ b/src/mongo/db/matcher/expression_array.h @@ -46,7 +46,7 @@ namespace mongo { Status init( const StringData& path ); ArrayFilterEntries* getArrayFilterEntries() { return &_arrayEntries; } - virtual bool matches( const BSONObj& doc, MatchDetails* details = 0 ) const; + virtual bool matches( const MatchableDocument* doc, MatchDetails* details = 0 ) const; virtual bool matchesSingleElement( const BSONElement& e ) const; @@ -67,7 +67,7 @@ namespace mongo { ArrayMatchingMatchExpression( MatchType matchType ) : MatchExpression( ARRAY, matchType ){} virtual ~ArrayMatchingMatchExpression(){} - virtual bool matches( const BSONObj& doc, MatchDetails* details ) const; + virtual bool matches( const MatchableDocument* doc, MatchDetails* details ) const; /** * @param e - has to be an array. calls matchesArray with e as an array @@ -133,7 +133,7 @@ namespace mongo { Status init( const StringData& path ); void add( const ArrayMatchingMatchExpression* expr ); - virtual bool matches( const BSONObj& doc, MatchDetails* details ) const; + virtual bool matches( const MatchableDocument* doc, MatchDetails* details ) const; /** * @param e has to be an array diff --git a/src/mongo/db/matcher/expression_array_test.cpp b/src/mongo/db/matcher/expression_array_test.cpp index 9b2a0018bb7..a323585f9ec 100644 --- a/src/mongo/db/matcher/expression_array_test.cpp +++ b/src/mongo/db/matcher/expression_array_test.cpp @@ -44,8 +44,8 @@ namespace mongo { AllMatchExpression all; ASSERT( !all.matchesSingleElement( notMatch[ "a" ] ) ); - ASSERT( !all.matches( BSON( "a" << 1 ), NULL ) ); - ASSERT( !all.matches( BSONObj(), NULL ) ); + ASSERT( !all.matchesBSON( BSON( "a" << 1 ), NULL ) ); + ASSERT( !all.matchesBSON( BSONObj(), NULL ) ); } TEST( AllMatchExpression, MatchesElementMultiple ) { @@ -69,8 +69,8 @@ namespace mongo { all.init( "a" ); all.getArrayFilterEntries()->addEquality( operand[0] ); - ASSERT( all.matches( BSON( "a" << 5.0 ), NULL ) ); - ASSERT( !all.matches( BSON( "a" << 4 ), NULL ) ); + ASSERT( all.matchesBSON( BSON( "a" << 5.0 ), NULL ) ); + ASSERT( !all.matchesBSON( BSON( "a" << 4 ), NULL ) ); } TEST( AllMatchExpression, MatchesArrayValue ) { @@ -79,9 +79,9 @@ namespace mongo { all.init( "a" ); all.getArrayFilterEntries()->addEquality( operand[0] ); - ASSERT( all.matches( BSON( "a" << BSON_ARRAY( 5.0 << 6 ) ), NULL ) ); - ASSERT( !all.matches( BSON( "a" << BSON_ARRAY( 6 << 7 ) ), NULL ) ); - ASSERT( !all.matches( BSON( "a" << BSON_ARRAY( BSON_ARRAY( 5 ) ) ), NULL ) ); + ASSERT( all.matchesBSON( BSON( "a" << BSON_ARRAY( 5.0 << 6 ) ), NULL ) ); + ASSERT( !all.matchesBSON( BSON( "a" << BSON_ARRAY( 6 << 7 ) ), NULL ) ); + ASSERT( !all.matchesBSON( BSON( "a" << BSON_ARRAY( BSON_ARRAY( 5 ) ) ), NULL ) ); } TEST( AllMatchExpression, MatchesNonArrayMultiValues ) { @@ -91,12 +91,12 @@ namespace mongo { all.getArrayFilterEntries()->addEquality( operand[0] ); all.getArrayFilterEntries()->addEquality( operand[1] ); - ASSERT( all.matches( BSON( "a" << BSON_ARRAY( BSON( "b" << 5.0 ) << BSON( "b" << 6 ) ) ), + ASSERT( all.matchesBSON( BSON( "a" << BSON_ARRAY( BSON( "b" << 5.0 ) << BSON( "b" << 6 ) ) ), NULL ) ); - ASSERT( all.matches( BSON( "a" << BSON_ARRAY( BSON( "b" << BSON_ARRAY( 5.0 << 7 ) ) << + ASSERT( all.matchesBSON( BSON( "a" << BSON_ARRAY( BSON( "b" << BSON_ARRAY( 5.0 << 7 ) ) << BSON( "b" << BSON_ARRAY( 10 << 6 ) ) ) ), NULL ) ); - ASSERT( !all.matches( BSON( "a" << BSON_ARRAY( BSON( "b" << 5.0 ) << BSON( "c" << 6 ) ) ), + ASSERT( !all.matchesBSON( BSON( "a" << BSON_ARRAY( BSON( "b" << 5.0 ) << BSON( "c" << 6 ) ) ), NULL ) ); } @@ -109,7 +109,7 @@ namespace mongo { all.getArrayFilterEntries()->addEquality( operand[2] ); all.getArrayFilterEntries()->addEquality( operand[3] ); - ASSERT( all.matches( BSON( "a" << BSON_ARRAY( BSON( "b" << BSON_ARRAY( 4 << 5 << 2 ) ) << + ASSERT( all.matchesBSON( BSON( "a" << BSON_ARRAY( BSON( "b" << BSON_ARRAY( 4 << 5 << 2 ) ) << BSON( "b" << 3 ) << BSON( "b" << BSONArray() ) << BSON( "b" << BSON_ARRAY( 1 ) ) ) ), @@ -125,9 +125,9 @@ namespace mongo { ASSERT( all.init( "a" ).isOK() ); all.getArrayFilterEntries()->addEquality( operand[0] ); - ASSERT( all.matches( BSONObj(), NULL ) ); - ASSERT( all.matches( BSON( "a" << BSONNULL ), NULL ) ); - ASSERT( !all.matches( BSON( "a" << 4 ), NULL ) ); + ASSERT( all.matchesBSON( BSONObj(), NULL ) ); + ASSERT( all.matchesBSON( BSON( "a" << BSONNULL ), NULL ) ); + ASSERT( !all.matchesBSON( BSON( "a" << 4 ), NULL ) ); } TEST( AllMatchExpression, MatchesFullArray ) { @@ -138,10 +138,10 @@ namespace mongo { all.getArrayFilterEntries()->addEquality( operand[1] ); // $all does not match full arrays. - ASSERT( !all.matches( BSON( "a" << BSON_ARRAY( 1 << 2 ) ), NULL ) ); - ASSERT( !all.matches( BSON( "a" << BSON_ARRAY( 1 << 2 << 3 ) ), NULL ) ); - ASSERT( !all.matches( BSON( "a" << BSON_ARRAY( 1 ) ), NULL ) ); - ASSERT( !all.matches( BSON( "a" << 1 ), NULL ) ); + ASSERT( !all.matchesBSON( BSON( "a" << BSON_ARRAY( 1 << 2 ) ), NULL ) ); + ASSERT( !all.matchesBSON( BSON( "a" << BSON_ARRAY( 1 << 2 << 3 ) ), NULL ) ); + ASSERT( !all.matchesBSON( BSON( "a" << BSON_ARRAY( 1 ) ), NULL ) ); + ASSERT( !all.matchesBSON( BSON( "a" << 1 ), NULL ) ); } TEST( AllMatchExpression, ElemMatchKey ) { @@ -152,11 +152,11 @@ namespace mongo { MatchDetails details; details.requestElemMatchKey(); - ASSERT( !all.matches( BSON( "a" << 4 ), &details ) ); + ASSERT( !all.matchesBSON( BSON( "a" << 4 ), &details ) ); ASSERT( !details.hasElemMatchKey() ); - ASSERT( all.matches( BSON( "a" << 5 ), &details ) ); + ASSERT( all.matchesBSON( BSON( "a" << 5 ), &details ) ); ASSERT( !details.hasElemMatchKey() ); - ASSERT( all.matches( BSON( "a" << BSON_ARRAY( 1 << 2 << 5 ) ), &details ) ); + ASSERT( all.matchesBSON( BSON( "a" << BSON_ARRAY( 1 << 2 << 5 ) ), &details ) ); // The elemMatchKey feature is not implemented for $all. ASSERT( !details.hasElemMatchKey() ); } @@ -167,9 +167,9 @@ namespace mongo { ASSERT( all.init( "a" ).isOK() ); all.getArrayFilterEntries()->addEquality( operand[0] ); - ASSERT( all.matches( BSON( "a" << MinKey ), NULL ) ); - ASSERT( !all.matches( BSON( "a" << MaxKey ), NULL ) ); - ASSERT( !all.matches( BSON( "a" << 4 ), NULL ) ); + ASSERT( all.matchesBSON( BSON( "a" << MinKey ), NULL ) ); + ASSERT( !all.matchesBSON( BSON( "a" << MaxKey ), NULL ) ); + ASSERT( !all.matchesBSON( BSON( "a" << 4 ), NULL ) ); } TEST( AllMatchExpression, MatchesMaxKey ) { @@ -178,9 +178,9 @@ namespace mongo { ASSERT( all.init( "a" ).isOK() ); all.getArrayFilterEntries()->addEquality( operand[0] ); - ASSERT( all.matches( BSON( "a" << MaxKey ), NULL ) ); - ASSERT( !all.matches( BSON( "a" << MinKey ), NULL ) ); - ASSERT( !all.matches( BSON( "a" << 4 ), NULL ) ); + ASSERT( all.matchesBSON( BSON( "a" << MaxKey ), NULL ) ); + ASSERT( !all.matchesBSON( BSON( "a" << MinKey ), NULL ) ); + ASSERT( !all.matchesBSON( BSON( "a" << 4 ), NULL ) ); } TEST( AllMatchExpression, Equivalent ) { @@ -281,9 +281,9 @@ namespace mongo { ASSERT( op.init( "a", eq.release() ).isOK() ); // Directly nested objects are not matched with $elemMatch. An intervening array is // required. - ASSERT( !op.matches( BSON( "a" << BSON( "b" << 5 ) ), NULL ) ); - ASSERT( !op.matches( BSON( "a" << BSON( "0" << ( BSON( "b" << 5 ) ) ) ), NULL ) ); - ASSERT( !op.matches( BSON( "a" << 4 ), NULL ) ); + ASSERT( !op.matchesBSON( BSON( "a" << BSON( "b" << 5 ) ), NULL ) ); + ASSERT( !op.matchesBSON( BSON( "a" << BSON( "0" << ( BSON( "b" << 5 ) ) ) ), NULL ) ); + ASSERT( !op.matchesBSON( BSON( "a" << 4 ), NULL ) ); } TEST( ElemMatchObjectMatchExpression, MatchesArrayObject ) { @@ -292,10 +292,10 @@ namespace mongo { ASSERT( eq->init( "b", baseOperand[ "b" ] ).isOK() ); ElemMatchObjectMatchExpression op; ASSERT( op.init( "a", eq.release() ).isOK() ); - ASSERT( op.matches( BSON( "a" << BSON_ARRAY( BSON( "b" << 5 ) ) ), NULL ) ); - ASSERT( op.matches( BSON( "a" << BSON_ARRAY( 4 << BSON( "b" << 5 ) ) ), NULL ) ); - ASSERT( op.matches( BSON( "a" << BSON_ARRAY( BSONObj() << BSON( "b" << 5 ) ) ), NULL ) ); - ASSERT( op.matches( BSON( "a" << BSON_ARRAY( BSON( "b" << 6 ) << BSON( "b" << 5 ) ) ), + ASSERT( op.matchesBSON( BSON( "a" << BSON_ARRAY( BSON( "b" << 5 ) ) ), NULL ) ); + ASSERT( op.matchesBSON( BSON( "a" << BSON_ARRAY( 4 << BSON( "b" << 5 ) ) ), NULL ) ); + ASSERT( op.matchesBSON( BSON( "a" << BSON_ARRAY( BSONObj() << BSON( "b" << 5 ) ) ), NULL ) ); + ASSERT( op.matchesBSON( BSON( "a" << BSON_ARRAY( BSON( "b" << 6 ) << BSON( "b" << 5 ) ) ), NULL ) ); } @@ -305,12 +305,12 @@ namespace mongo { ASSERT( eq->init( "c", baseOperand[ "c" ] ).isOK() ); ElemMatchObjectMatchExpression op; ASSERT( op.init( "a.b", eq.release() ).isOK() ); - ASSERT( op.matches( BSON( "a" << + ASSERT( op.matchesBSON( BSON( "a" << BSON_ARRAY( BSON( "b" << BSON_ARRAY( BSON( "c" << 5 ) ) ) ) ), NULL ) ); - ASSERT( op.matches( BSON( "a" << + ASSERT( op.matchesBSON( BSON( "a" << BSON_ARRAY( BSON( "b" << BSON_ARRAY( BSON( "c" << 1 ) ) ) << @@ -328,17 +328,17 @@ namespace mongo { ASSERT( op.init( "a.b", eq.release() ).isOK() ); MatchDetails details; details.requestElemMatchKey(); - ASSERT( !op.matches( BSONObj(), &details ) ); + ASSERT( !op.matchesBSON( BSONObj(), &details ) ); ASSERT( !details.hasElemMatchKey() ); - ASSERT( !op.matches( BSON( "a" << BSON( "b" << BSON_ARRAY( BSON( "c" << 7 ) ) ) ), + ASSERT( !op.matchesBSON( BSON( "a" << BSON( "b" << BSON_ARRAY( BSON( "c" << 7 ) ) ) ), &details ) ); ASSERT( !details.hasElemMatchKey() ); - ASSERT( op.matches( BSON( "a" << BSON( "b" << BSON_ARRAY( 3 << BSON( "c" << 6 ) ) ) ), + ASSERT( op.matchesBSON( BSON( "a" << BSON( "b" << BSON_ARRAY( 3 << BSON( "c" << 6 ) ) ) ), &details ) ); ASSERT( details.hasElemMatchKey() ); // The entry within the $elemMatch array is reported. ASSERT_EQUALS( "1", details.elemMatchKey() ); - ASSERT( op.matches( BSON( "a" << + ASSERT( op.matchesBSON( BSON( "a" << BSON_ARRAY( 1 << 2 << BSON( "b" << BSON_ARRAY( 3 << 5 << @@ -404,8 +404,8 @@ namespace mongo { ASSERT( op.init( "a", gt.release() ).isOK() ); // Directly nested objects are not matched with $elemMatch. An intervening array is // required. - ASSERT( !op.matches( BSON( "a" << 6 ), NULL ) ); - ASSERT( !op.matches( BSON( "a" << BSON( "0" << 6 ) ), NULL ) ); + ASSERT( !op.matchesBSON( BSON( "a" << 6 ), NULL ) ); + ASSERT( !op.matchesBSON( BSON( "a" << BSON( "0" << 6 ) ), NULL ) ); } TEST( ElemMatchValueMatchExpression, MatchesArrayScalar ) { @@ -414,9 +414,9 @@ namespace mongo { ASSERT( gt->init( "", baseOperand[ "$gt" ] ).isOK() ); ElemMatchValueMatchExpression op; ASSERT( op.init( "a", gt.release() ).isOK() ); - ASSERT( op.matches( BSON( "a" << BSON_ARRAY( 6 ) ), NULL ) ); - ASSERT( op.matches( BSON( "a" << BSON_ARRAY( 4 << 6 ) ), NULL ) ); - ASSERT( op.matches( BSON( "a" << BSON_ARRAY( BSONObj() << 7 ) ), NULL ) ); + ASSERT( op.matchesBSON( BSON( "a" << BSON_ARRAY( 6 ) ), NULL ) ); + ASSERT( op.matchesBSON( BSON( "a" << BSON_ARRAY( 4 << 6 ) ), NULL ) ); + ASSERT( op.matchesBSON( BSON( "a" << BSON_ARRAY( BSONObj() << 7 ) ), NULL ) ); } TEST( ElemMatchValueMatchExpression, MatchesMultipleNamedValues ) { @@ -425,8 +425,8 @@ namespace mongo { ASSERT( gt->init( "", baseOperand[ "$gt" ] ).isOK() ); ElemMatchValueMatchExpression op; ASSERT( op.init( "a.b", gt.release() ).isOK() ); - ASSERT( op.matches( BSON( "a" << BSON_ARRAY( BSON( "b" << BSON_ARRAY( 6 ) ) ) ), NULL ) ); - ASSERT( op.matches( BSON( "a" << + ASSERT( op.matchesBSON( BSON( "a" << BSON_ARRAY( BSON( "b" << BSON_ARRAY( 6 ) ) ) ), NULL ) ); + ASSERT( op.matchesBSON( BSON( "a" << BSON_ARRAY( BSON( "b" << BSON_ARRAY( 4 ) ) << BSON( "b" << BSON_ARRAY( 4 << 6 ) ) ) ), NULL ) ); @@ -440,17 +440,17 @@ namespace mongo { ASSERT( op.init( "a.b", gt.release() ).isOK() ); MatchDetails details; details.requestElemMatchKey(); - ASSERT( !op.matches( BSONObj(), &details ) ); + ASSERT( !op.matchesBSON( BSONObj(), &details ) ); ASSERT( !details.hasElemMatchKey() ); - ASSERT( !op.matches( BSON( "a" << BSON( "b" << BSON_ARRAY( 2 ) ) ), + ASSERT( !op.matchesBSON( BSON( "a" << BSON( "b" << BSON_ARRAY( 2 ) ) ), &details ) ); ASSERT( !details.hasElemMatchKey() ); - ASSERT( op.matches( BSON( "a" << BSON( "b" << BSON_ARRAY( 3 << 7 ) ) ), + ASSERT( op.matchesBSON( BSON( "a" << BSON( "b" << BSON_ARRAY( 3 << 7 ) ) ), &details ) ); ASSERT( details.hasElemMatchKey() ); // The entry within the $elemMatch array is reported. ASSERT_EQUALS( "1", details.elemMatchKey() ); - ASSERT( op.matches( BSON( "a" << + ASSERT( op.matchesBSON( BSON( "a" << BSON_ARRAY( 1 << 2 << BSON( "b" << BSON_ARRAY( 3 << 7 ) ) ) ), &details ) ); @@ -567,19 +567,19 @@ namespace mongo { BSONObj nonArray = BSON( "x" << 4 ); - ASSERT( !op.matches( nonArray, NULL ) ); + ASSERT( !op.matchesBSON( nonArray, NULL ) ); BSONObj emptyArray = BSON( "x" << BSONArray() ); - ASSERT( !op.matches( emptyArray, NULL ) ); + ASSERT( !op.matchesBSON( emptyArray, NULL ) ); BSONObj nonNumberArray = BSON( "x" << BSON_ARRAY( "q" ) ); - ASSERT( !op.matches( nonNumberArray, NULL ) ); + ASSERT( !op.matchesBSON( nonNumberArray, NULL ) ); BSONObj singleMatch = BSON( "x" << BSON_ARRAY( 5 ) ); - ASSERT( !op.matches( singleMatch, NULL ) ); + ASSERT( !op.matchesBSON( singleMatch, NULL ) ); BSONObj otherMatch = BSON( "x" << BSON_ARRAY( 105 ) ); - ASSERT( !op.matches( otherMatch, NULL ) ); + ASSERT( !op.matchesBSON( otherMatch, NULL ) ); BSONObj bothMatch = BSON( "x" << BSON_ARRAY( 5 << 105 ) ); - ASSERT( op.matches( bothMatch, NULL ) ); + ASSERT( op.matchesBSON( bothMatch, NULL ) ); BSONObj neitherMatch = BSON( "x" << BSON_ARRAY( 0 << 200 ) ); - ASSERT( !op.matches( neitherMatch, NULL ) ); + ASSERT( !op.matchesBSON( neitherMatch, NULL ) ); } /** @@ -624,9 +624,9 @@ namespace mongo { TEST( SizeMatchExpression, MatchesArray ) { SizeMatchExpression size; ASSERT( size.init( "a", 2 ).isOK() ); - ASSERT( size.matches( BSON( "a" << BSON_ARRAY( 4 << 5.5 ) ), NULL ) ); + ASSERT( size.matchesBSON( BSON( "a" << BSON_ARRAY( 4 << 5.5 ) ), NULL ) ); // Arrays are not unwound to look for matching subarrays. - ASSERT( !size.matches( BSON( "a" << BSON_ARRAY( 4 << 5.5 << BSON_ARRAY( 1 << 2 ) ) ), + ASSERT( !size.matchesBSON( BSON( "a" << BSON_ARRAY( 4 << 5.5 << BSON_ARRAY( 1 << 2 ) ) ), NULL ) ); } @@ -634,7 +634,7 @@ namespace mongo { SizeMatchExpression size; ASSERT( size.init( "a.2", 2 ).isOK() ); // A numerically referenced nested array is matched. - ASSERT( size.matches( BSON( "a" << BSON_ARRAY( 4 << 5.5 << BSON_ARRAY( 1 << 2 ) ) ), + ASSERT( size.matchesBSON( BSON( "a" << BSON_ARRAY( 4 << 5.5 << BSON_ARRAY( 1 << 2 ) ) ), NULL ) ); } @@ -643,11 +643,11 @@ namespace mongo { ASSERT( size.init( "a.b", 3 ).isOK() ); MatchDetails details; details.requestElemMatchKey(); - ASSERT( !size.matches( BSON( "a" << 1 ), &details ) ); + ASSERT( !size.matchesBSON( BSON( "a" << 1 ), &details ) ); ASSERT( !details.hasElemMatchKey() ); - ASSERT( size.matches( BSON( "a" << BSON( "b" << BSON_ARRAY( 1 << 2 << 3 ) ) ), &details ) ); + ASSERT( size.matchesBSON( BSON( "a" << BSON( "b" << BSON_ARRAY( 1 << 2 << 3 ) ) ), &details ) ); ASSERT( !details.hasElemMatchKey() ); - ASSERT( size.matches( BSON( "a" << + ASSERT( size.matchesBSON( BSON( "a" << BSON_ARRAY( 2 << BSON( "b" << BSON_ARRAY( 1 << 2 << 3 ) ) ) ), &details ) ); diff --git a/src/mongo/db/matcher/expression_leaf.cpp b/src/mongo/db/matcher/expression_leaf.cpp index db78d4eaa85..b03f5c9215c 100644 --- a/src/mongo/db/matcher/expression_leaf.cpp +++ b/src/mongo/db/matcher/expression_leaf.cpp @@ -27,7 +27,7 @@ namespace mongo { - bool LeafMatchExpression::matches( const BSONObj& doc, MatchDetails* details ) const { + bool LeafMatchExpression::matches( const MatchableDocument* doc, MatchDetails* details ) const { //log() << "e doc: " << doc << " path: " << _path << std::endl; FieldRef path; @@ -35,7 +35,7 @@ namespace mongo { bool traversedArray = false; int32_t idxPath = 0; - BSONElement e = getFieldDottedOrArray( doc, path, &idxPath, &traversedArray ); + BSONElement e = doc->getFieldDottedOrArray( path, &idxPath, &traversedArray ); string rest = pathToString( path, idxPath+1 ); @@ -321,18 +321,20 @@ namespace mongo { return e.type() == _type; } - bool TypeMatchExpression::matches( const BSONObj& doc, MatchDetails* details ) const { + bool TypeMatchExpression::matches( const MatchableDocument* doc, MatchDetails* details ) const { return _matches( _path, doc, details ); } - bool TypeMatchExpression::_matches( const StringData& path, const BSONObj& doc, MatchDetails* details ) const { + bool TypeMatchExpression::_matches( const StringData& path, + const MatchableDocument* doc, + MatchDetails* details ) const { FieldRef pathRef; pathRef.parse(path); bool traversedArray = false; int32_t idxPath = 0; - BSONElement e = getFieldDottedOrArray( doc, pathRef, &idxPath, &traversedArray ); + BSONElement e = doc->getFieldDottedOrArray( pathRef, &idxPath, &traversedArray ); string rest = pathToString( pathRef, idxPath+1 ); @@ -348,7 +350,8 @@ namespace mongo { found = matchesSingleElement( x ); } else if ( x.isABSONObj() ) { - found = _matches( rest, x.Obj(), details ); + BSONMatchableDocument doc( x.Obj() ); + found = _matches( rest, &doc, details ); } if ( found ) { diff --git a/src/mongo/db/matcher/expression_leaf.h b/src/mongo/db/matcher/expression_leaf.h index 8eee488a02e..fcd3b91782b 100644 --- a/src/mongo/db/matcher/expression_leaf.h +++ b/src/mongo/db/matcher/expression_leaf.h @@ -38,7 +38,7 @@ namespace mongo { virtual LeafMatchExpression* shallowClone() const = 0; - virtual bool matches( const BSONObj& doc, MatchDetails* details = 0 ) const; + virtual bool matches( const MatchableDocument* doc, MatchDetails* details = 0 ) const; virtual bool matchesSingleElement( const BSONElement& e ) const = 0; @@ -225,13 +225,15 @@ namespace mongo { virtual bool matchesSingleElement( const BSONElement& e ) const; - virtual bool matches( const BSONObj& doc, MatchDetails* details = 0 ) const; + virtual bool matches( const MatchableDocument* doc, MatchDetails* details = 0 ) const; virtual void debugString( StringBuilder& debug, int level ) const; virtual bool equivalent( const MatchExpression* other ) const; private: - bool _matches( const StringData& path, const BSONObj& doc, MatchDetails* details = 0 ) const; + bool _matches( const StringData& path, + const MatchableDocument* doc, + MatchDetails* details = 0 ) const; StringData _path; int _type; diff --git a/src/mongo/db/matcher/expression_leaf_test.cpp b/src/mongo/db/matcher/expression_leaf_test.cpp index e102f92f8c5..1e32a6c59d9 100644 --- a/src/mongo/db/matcher/expression_leaf_test.cpp +++ b/src/mongo/db/matcher/expression_leaf_test.cpp @@ -48,51 +48,51 @@ namespace mongo { BSONObj operand = BSON( "a" << 5 ); EqualityMatchExpression eq; eq.init( "a", operand[ "a" ] ); - ASSERT( eq.matches( BSON( "a" << 5.0 ), NULL ) ); - ASSERT( !eq.matches( BSON( "a" << 4 ), NULL ) ); + ASSERT( eq.matchesBSON( BSON( "a" << 5.0 ), NULL ) ); + ASSERT( !eq.matchesBSON( BSON( "a" << 4 ), NULL ) ); } TEST( EqOp, MatchesArrayValue ) { BSONObj operand = BSON( "a" << 5 ); EqualityMatchExpression eq; eq.init( "a", operand[ "a" ] ); - ASSERT( eq.matches( BSON( "a" << BSON_ARRAY( 5.0 << 6 ) ), NULL ) ); - ASSERT( !eq.matches( BSON( "a" << BSON_ARRAY( 6 << 7 ) ), NULL ) ); + ASSERT( eq.matchesBSON( BSON( "a" << BSON_ARRAY( 5.0 << 6 ) ), NULL ) ); + ASSERT( !eq.matchesBSON( BSON( "a" << BSON_ARRAY( 6 << 7 ) ), NULL ) ); } TEST( EqOp, MatchesReferencedObjectValue ) { BSONObj operand = BSON( "a.b" << 5 ); EqualityMatchExpression eq; eq.init( "a.b", operand[ "a.b" ] ); - ASSERT( eq.matches( BSON( "a" << BSON( "b" << 5 ) ), NULL ) ); - ASSERT( eq.matches( BSON( "a" << BSON( "b" << BSON_ARRAY( 5 ) ) ), NULL ) ); - ASSERT( eq.matches( BSON( "a" << BSON_ARRAY( BSON( "b" << 5 ) ) ), NULL ) ); + ASSERT( eq.matchesBSON( BSON( "a" << BSON( "b" << 5 ) ), NULL ) ); + ASSERT( eq.matchesBSON( BSON( "a" << BSON( "b" << BSON_ARRAY( 5 ) ) ), NULL ) ); + ASSERT( eq.matchesBSON( BSON( "a" << BSON_ARRAY( BSON( "b" << 5 ) ) ), NULL ) ); } TEST( EqOp, MatchesReferencedArrayValue ) { BSONObj operand = BSON( "a.0" << 5 ); EqualityMatchExpression eq; eq.init( "a.0", operand[ "a.0" ] ); - ASSERT( eq.matches( BSON( "a" << BSON_ARRAY( 5 ) ), NULL ) ); - ASSERT( !eq.matches( BSON( "a" << BSON_ARRAY( BSON_ARRAY( 5 ) ) ), NULL ) ); + ASSERT( eq.matchesBSON( BSON( "a" << BSON_ARRAY( 5 ) ), NULL ) ); + ASSERT( !eq.matchesBSON( BSON( "a" << BSON_ARRAY( BSON_ARRAY( 5 ) ) ), NULL ) ); } TEST( EqOp, MatchesNull ) { BSONObj operand = BSON( "a" << BSONNULL ); EqualityMatchExpression eq; eq.init( "a", operand[ "a" ] ); - ASSERT( eq.matches( BSONObj(), NULL ) ); - ASSERT( eq.matches( BSON( "a" << BSONNULL ), NULL ) ); - ASSERT( !eq.matches( BSON( "a" << 4 ), NULL ) ); + ASSERT( eq.matchesBSON( BSONObj(), NULL ) ); + ASSERT( eq.matchesBSON( BSON( "a" << BSONNULL ), NULL ) ); + ASSERT( !eq.matchesBSON( BSON( "a" << 4 ), NULL ) ); } TEST( EqOp, MatchesMinKey ) { BSONObj operand = BSON( "a" << MinKey ); EqualityMatchExpression eq; eq.init( "a", operand[ "a" ] ); - ASSERT( eq.matches( BSON( "a" << MinKey ), NULL ) ); - ASSERT( !eq.matches( BSON( "a" << MaxKey ), NULL ) ); - ASSERT( !eq.matches( BSON( "a" << 4 ), NULL ) ); + ASSERT( eq.matchesBSON( BSON( "a" << MinKey ), NULL ) ); + ASSERT( !eq.matchesBSON( BSON( "a" << MaxKey ), NULL ) ); + ASSERT( !eq.matchesBSON( BSON( "a" << 4 ), NULL ) ); } @@ -101,19 +101,19 @@ namespace mongo { BSONObj operand = BSON( "a" << MaxKey ); EqualityMatchExpression eq; ASSERT( eq.init( "a", operand[ "a" ] ).isOK() ); - ASSERT( eq.matches( BSON( "a" << MaxKey ), NULL ) ); - ASSERT( !eq.matches( BSON( "a" << MinKey ), NULL ) ); - ASSERT( !eq.matches( BSON( "a" << 4 ), NULL ) ); + ASSERT( eq.matchesBSON( BSON( "a" << MaxKey ), NULL ) ); + ASSERT( !eq.matchesBSON( BSON( "a" << MinKey ), NULL ) ); + ASSERT( !eq.matchesBSON( BSON( "a" << 4 ), NULL ) ); } TEST( EqOp, MatchesFullArray ) { BSONObj operand = BSON( "a" << BSON_ARRAY( 1 << 2 ) ); EqualityMatchExpression eq; ASSERT( eq.init( "a", operand[ "a" ] ).isOK() ); - ASSERT( eq.matches( BSON( "a" << BSON_ARRAY( 1 << 2 ) ), NULL ) ); - ASSERT( !eq.matches( BSON( "a" << BSON_ARRAY( 1 << 2 << 3 ) ), NULL ) ); - ASSERT( !eq.matches( BSON( "a" << BSON_ARRAY( 1 ) ), NULL ) ); - ASSERT( !eq.matches( BSON( "a" << 1 ), NULL ) ); + ASSERT( eq.matchesBSON( BSON( "a" << BSON_ARRAY( 1 << 2 ) ), NULL ) ); + ASSERT( !eq.matchesBSON( BSON( "a" << BSON_ARRAY( 1 << 2 << 3 ) ), NULL ) ); + ASSERT( !eq.matchesBSON( BSON( "a" << BSON_ARRAY( 1 ) ), NULL ) ); + ASSERT( !eq.matchesBSON( BSON( "a" << 1 ), NULL ) ); } TEST( EqOp, ElemMatchKey ) { @@ -122,11 +122,11 @@ namespace mongo { ASSERT( eq.init( "a", operand[ "a" ] ).isOK() ); MatchDetails details; details.requestElemMatchKey(); - ASSERT( !eq.matches( BSON( "a" << 4 ), &details ) ); + ASSERT( !eq.matchesBSON( BSON( "a" << 4 ), &details ) ); ASSERT( !details.hasElemMatchKey() ); - ASSERT( eq.matches( BSON( "a" << 5 ), &details ) ); + ASSERT( eq.matchesBSON( BSON( "a" << 5 ), &details ) ); ASSERT( !details.hasElemMatchKey() ); - ASSERT( eq.matches( BSON( "a" << BSON_ARRAY( 1 << 2 << 5 ) ), &details ) ); + ASSERT( eq.matchesBSON( BSON( "a" << BSON_ARRAY( 1 << 2 << 5 ) ), &details ) ); ASSERT( details.hasElemMatchKey() ); ASSERT_EQUALS( "2", details.elemMatchKey() ); } @@ -221,16 +221,16 @@ namespace mongo { BSONObj operand = BSON( "$lt" << 5 ); LTMatchExpression lt; ASSERT( lt.init( "a", operand[ "$lt" ] ).isOK() ); - ASSERT( lt.matches( BSON( "a" << 4.5 ), NULL ) ); - ASSERT( !lt.matches( BSON( "a" << 6 ), NULL ) ); + ASSERT( lt.matchesBSON( BSON( "a" << 4.5 ), NULL ) ); + ASSERT( !lt.matchesBSON( BSON( "a" << 6 ), NULL ) ); } TEST( LtOp, MatchesArrayValue ) { BSONObj operand = BSON( "$lt" << 5 ); LTMatchExpression lt; ASSERT( lt.init( "a", operand[ "$lt" ] ).isOK() ); - ASSERT( lt.matches( BSON( "a" << BSON_ARRAY( 6 << 4.5 ) ), NULL ) ); - ASSERT( !lt.matches( BSON( "a" << BSON_ARRAY( 6 << 7 ) ), NULL ) ); + ASSERT( lt.matchesBSON( BSON( "a" << BSON_ARRAY( 6 << 4.5 ) ), NULL ) ); + ASSERT( !lt.matchesBSON( BSON( "a" << BSON_ARRAY( 6 << 7 ) ), NULL ) ); } TEST( LtOp, MatchesWholeArray ) { @@ -238,34 +238,34 @@ namespace mongo { LTMatchExpression lt; ASSERT( lt.init( "a", operand[ "$lt" ] ).isOK() ); // Arrays are not comparable as inequalities. - ASSERT( !lt.matches( BSON( "a" << BSON_ARRAY( 4 ) ), NULL ) ); + ASSERT( !lt.matchesBSON( BSON( "a" << BSON_ARRAY( 4 ) ), NULL ) ); } TEST( LtOp, MatchesNull ) { BSONObj operand = BSON( "$lt" << BSONNULL ); LTMatchExpression lt; ASSERT( lt.init( "a", operand[ "$lt" ] ).isOK() ); - ASSERT( !lt.matches( BSONObj(), NULL ) ); - ASSERT( !lt.matches( BSON( "a" << BSONNULL ), NULL ) ); - ASSERT( !lt.matches( BSON( "a" << 4 ), NULL ) ); + ASSERT( !lt.matchesBSON( BSONObj(), NULL ) ); + ASSERT( !lt.matchesBSON( BSON( "a" << BSONNULL ), NULL ) ); + ASSERT( !lt.matchesBSON( BSON( "a" << 4 ), NULL ) ); } TEST( LtOp, MatchesMinKey ) { BSONObj operand = BSON( "a" << MinKey ); LTMatchExpression lt; ASSERT( lt.init( "a", operand[ "a" ] ).isOK() ); - ASSERT( !lt.matches( BSON( "a" << MinKey ), NULL ) ); - ASSERT( !lt.matches( BSON( "a" << MaxKey ), NULL ) ); - ASSERT( !lt.matches( BSON( "a" << 4 ), NULL ) ); + ASSERT( !lt.matchesBSON( BSON( "a" << MinKey ), NULL ) ); + ASSERT( !lt.matchesBSON( BSON( "a" << MaxKey ), NULL ) ); + ASSERT( !lt.matchesBSON( BSON( "a" << 4 ), NULL ) ); } TEST( LtOp, MatchesMaxKey ) { BSONObj operand = BSON( "a" << MaxKey ); LTMatchExpression lt; ASSERT( lt.init( "a", operand[ "a" ] ).isOK() ); - ASSERT( !lt.matches( BSON( "a" << MaxKey ), NULL ) ); - ASSERT( lt.matches( BSON( "a" << MinKey ), NULL ) ); - ASSERT( lt.matches( BSON( "a" << 4 ), NULL ) ); + ASSERT( !lt.matchesBSON( BSON( "a" << MaxKey ), NULL ) ); + ASSERT( lt.matchesBSON( BSON( "a" << MinKey ), NULL ) ); + ASSERT( lt.matchesBSON( BSON( "a" << 4 ), NULL ) ); } TEST( LtOp, ElemMatchKey ) { @@ -274,11 +274,11 @@ namespace mongo { ASSERT( lt.init( "a", operand[ "$lt" ] ).isOK() ); MatchDetails details; details.requestElemMatchKey(); - ASSERT( !lt.matches( BSON( "a" << 6 ), &details ) ); + ASSERT( !lt.matchesBSON( BSON( "a" << 6 ), &details ) ); ASSERT( !details.hasElemMatchKey() ); - ASSERT( lt.matches( BSON( "a" << 4 ), &details ) ); + ASSERT( lt.matchesBSON( BSON( "a" << 4 ), &details ) ); ASSERT( !details.hasElemMatchKey() ); - ASSERT( lt.matches( BSON( "a" << BSON_ARRAY( 6 << 2 << 5 ) ), &details ) ); + ASSERT( lt.matchesBSON( BSON( "a" << BSON_ARRAY( 6 << 2 << 5 ) ), &details ) ); ASSERT( details.hasElemMatchKey() ); ASSERT_EQUALS( "1", details.elemMatchKey() ); } @@ -357,16 +357,16 @@ namespace mongo { BSONObj operand = BSON( "$lte" << 5 ); LTEMatchExpression lte; ASSERT( lte.init( "a", operand[ "$lte" ] ).isOK() ); - ASSERT( lte.matches( BSON( "a" << 4.5 ), NULL ) ); - ASSERT( !lte.matches( BSON( "a" << 6 ), NULL ) ); + ASSERT( lte.matchesBSON( BSON( "a" << 4.5 ), NULL ) ); + ASSERT( !lte.matchesBSON( BSON( "a" << 6 ), NULL ) ); } TEST( LteOp, MatchesArrayValue ) { BSONObj operand = BSON( "$lte" << 5 ); LTEMatchExpression lte; ASSERT( lte.init( "a", operand[ "$lte" ] ).isOK() ); - ASSERT( lte.matches( BSON( "a" << BSON_ARRAY( 6 << 4.5 ) ), NULL ) ); - ASSERT( !lte.matches( BSON( "a" << BSON_ARRAY( 6 << 7 ) ), NULL ) ); + ASSERT( lte.matchesBSON( BSON( "a" << BSON_ARRAY( 6 << 4.5 ) ), NULL ) ); + ASSERT( !lte.matchesBSON( BSON( "a" << BSON_ARRAY( 6 << 7 ) ), NULL ) ); } TEST( LteOp, MatchesWholeArray ) { @@ -374,34 +374,34 @@ namespace mongo { LTEMatchExpression lte; ASSERT( lte.init( "a", operand[ "$lte" ] ).isOK() ); // Arrays are not comparable as inequalities. - ASSERT( !lte.matches( BSON( "a" << BSON_ARRAY( 4 ) ), NULL ) ); + ASSERT( !lte.matchesBSON( BSON( "a" << BSON_ARRAY( 4 ) ), NULL ) ); } TEST( LteOp, MatchesNull ) { BSONObj operand = BSON( "$lte" << BSONNULL ); LTEMatchExpression lte; ASSERT( lte.init( "a", operand[ "$lte" ] ).isOK() ); - ASSERT( lte.matches( BSONObj(), NULL ) ); - ASSERT( lte.matches( BSON( "a" << BSONNULL ), NULL ) ); - ASSERT( !lte.matches( BSON( "a" << 4 ), NULL ) ); + ASSERT( lte.matchesBSON( BSONObj(), NULL ) ); + ASSERT( lte.matchesBSON( BSON( "a" << BSONNULL ), NULL ) ); + ASSERT( !lte.matchesBSON( BSON( "a" << 4 ), NULL ) ); } TEST( LteOp, MatchesMinKey ) { BSONObj operand = BSON( "a" << MinKey ); LTEMatchExpression lte; ASSERT( lte.init( "a", operand[ "a" ] ).isOK() ); - ASSERT( lte.matches( BSON( "a" << MinKey ), NULL ) ); - ASSERT( !lte.matches( BSON( "a" << MaxKey ), NULL ) ); - ASSERT( !lte.matches( BSON( "a" << 4 ), NULL ) ); + ASSERT( lte.matchesBSON( BSON( "a" << MinKey ), NULL ) ); + ASSERT( !lte.matchesBSON( BSON( "a" << MaxKey ), NULL ) ); + ASSERT( !lte.matchesBSON( BSON( "a" << 4 ), NULL ) ); } TEST( LteOp, MatchesMaxKey ) { BSONObj operand = BSON( "a" << MaxKey ); LTEMatchExpression lte; ASSERT( lte.init( "a", operand[ "a" ] ).isOK() ); - ASSERT( lte.matches( BSON( "a" << MaxKey ), NULL ) ); - ASSERT( lte.matches( BSON( "a" << MinKey ), NULL ) ); - ASSERT( lte.matches( BSON( "a" << 4 ), NULL ) ); + ASSERT( lte.matchesBSON( BSON( "a" << MaxKey ), NULL ) ); + ASSERT( lte.matchesBSON( BSON( "a" << MinKey ), NULL ) ); + ASSERT( lte.matchesBSON( BSON( "a" << 4 ), NULL ) ); } @@ -411,11 +411,11 @@ namespace mongo { ASSERT( lte.init( "a", operand[ "$lte" ] ).isOK() ); MatchDetails details; details.requestElemMatchKey(); - ASSERT( !lte.matches( BSON( "a" << 6 ), &details ) ); + ASSERT( !lte.matchesBSON( BSON( "a" << 6 ), &details ) ); ASSERT( !details.hasElemMatchKey() ); - ASSERT( lte.matches( BSON( "a" << 4 ), &details ) ); + ASSERT( lte.matchesBSON( BSON( "a" << 4 ), &details ) ); ASSERT( !details.hasElemMatchKey() ); - ASSERT( lte.matches( BSON( "a" << BSON_ARRAY( 6 << 2 << 5 ) ), &details ) ); + ASSERT( lte.matchesBSON( BSON( "a" << BSON_ARRAY( 6 << 2 << 5 ) ), &details ) ); ASSERT( details.hasElemMatchKey() ); ASSERT_EQUALS( "1", details.elemMatchKey() ); } @@ -495,16 +495,16 @@ namespace mongo { BSONObj operand = BSON( "$gt" << 5 ); GTMatchExpression gt; ASSERT( gt.init( "a", operand[ "$gt" ] ).isOK() ); - ASSERT( gt.matches( BSON( "a" << 5.5 ), NULL ) ); - ASSERT( !gt.matches( BSON( "a" << 4 ), NULL ) ); + ASSERT( gt.matchesBSON( BSON( "a" << 5.5 ), NULL ) ); + ASSERT( !gt.matchesBSON( BSON( "a" << 4 ), NULL ) ); } TEST( GtOp, MatchesArrayValue ) { BSONObj operand = BSON( "$gt" << 5 ); GTMatchExpression gt; ASSERT( gt.init( "a", operand[ "$gt" ] ).isOK() ); - ASSERT( gt.matches( BSON( "a" << BSON_ARRAY( 3 << 5.5 ) ), NULL ) ); - ASSERT( !gt.matches( BSON( "a" << BSON_ARRAY( 2 << 4 ) ), NULL ) ); + ASSERT( gt.matchesBSON( BSON( "a" << BSON_ARRAY( 3 << 5.5 ) ), NULL ) ); + ASSERT( !gt.matchesBSON( BSON( "a" << BSON_ARRAY( 2 << 4 ) ), NULL ) ); } TEST( GtOp, MatchesWholeArray ) { @@ -512,34 +512,34 @@ namespace mongo { GTMatchExpression gt; ASSERT( gt.init( "a", operand[ "$gt" ] ).isOK() ); // Arrays are not comparable as inequalities. - ASSERT( !gt.matches( BSON( "a" << BSON_ARRAY( 6 ) ), NULL ) ); + ASSERT( !gt.matchesBSON( BSON( "a" << BSON_ARRAY( 6 ) ), NULL ) ); } TEST( GtOp, MatchesNull ) { BSONObj operand = BSON( "$gt" << BSONNULL ); GTMatchExpression gt; ASSERT( gt.init( "a", operand[ "$gt" ] ).isOK() ); - ASSERT( !gt.matches( BSONObj(), NULL ) ); - ASSERT( !gt.matches( BSON( "a" << BSONNULL ), NULL ) ); - ASSERT( !gt.matches( BSON( "a" << 4 ), NULL ) ); + ASSERT( !gt.matchesBSON( BSONObj(), NULL ) ); + ASSERT( !gt.matchesBSON( BSON( "a" << BSONNULL ), NULL ) ); + ASSERT( !gt.matchesBSON( BSON( "a" << 4 ), NULL ) ); } TEST( GtOp, MatchesMinKey ) { BSONObj operand = BSON( "a" << MinKey ); GTMatchExpression gt; ASSERT( gt.init( "a", operand[ "a" ] ).isOK() ); - ASSERT( !gt.matches( BSON( "a" << MinKey ), NULL ) ); - ASSERT( gt.matches( BSON( "a" << MaxKey ), NULL ) ); - ASSERT( gt.matches( BSON( "a" << 4 ), NULL ) ); + ASSERT( !gt.matchesBSON( BSON( "a" << MinKey ), NULL ) ); + ASSERT( gt.matchesBSON( BSON( "a" << MaxKey ), NULL ) ); + ASSERT( gt.matchesBSON( BSON( "a" << 4 ), NULL ) ); } TEST( GtOp, MatchesMaxKey ) { BSONObj operand = BSON( "a" << MaxKey ); GTMatchExpression gt; ASSERT( gt.init( "a", operand[ "a" ] ).isOK() ); - ASSERT( !gt.matches( BSON( "a" << MaxKey ), NULL ) ); - ASSERT( !gt.matches( BSON( "a" << MinKey ), NULL ) ); - ASSERT( !gt.matches( BSON( "a" << 4 ), NULL ) ); + ASSERT( !gt.matchesBSON( BSON( "a" << MaxKey ), NULL ) ); + ASSERT( !gt.matchesBSON( BSON( "a" << MinKey ), NULL ) ); + ASSERT( !gt.matchesBSON( BSON( "a" << 4 ), NULL ) ); } TEST( GtOp, ElemMatchKey ) { @@ -548,11 +548,11 @@ namespace mongo { ASSERT( gt.init( "a", operand[ "$gt" ] ).isOK() ); MatchDetails details; details.requestElemMatchKey(); - ASSERT( !gt.matches( BSON( "a" << 4 ), &details ) ); + ASSERT( !gt.matchesBSON( BSON( "a" << 4 ), &details ) ); ASSERT( !details.hasElemMatchKey() ); - ASSERT( gt.matches( BSON( "a" << 6 ), &details ) ); + ASSERT( gt.matchesBSON( BSON( "a" << 6 ), &details ) ); ASSERT( !details.hasElemMatchKey() ); - ASSERT( gt.matches( BSON( "a" << BSON_ARRAY( 2 << 6 << 5 ) ), &details ) ); + ASSERT( gt.matchesBSON( BSON( "a" << BSON_ARRAY( 2 << 6 << 5 ) ), &details ) ); ASSERT( details.hasElemMatchKey() ); ASSERT_EQUALS( "1", details.elemMatchKey() ); } @@ -632,16 +632,16 @@ namespace mongo { BSONObj operand = BSON( "$gte" << 5 ); GTEMatchExpression gte; ASSERT( gte.init( "a", operand[ "$gte" ] ).isOK() ); - ASSERT( gte.matches( BSON( "a" << 5.5 ), NULL ) ); - ASSERT( !gte.matches( BSON( "a" << 4 ), NULL ) ); + ASSERT( gte.matchesBSON( BSON( "a" << 5.5 ), NULL ) ); + ASSERT( !gte.matchesBSON( BSON( "a" << 4 ), NULL ) ); } TEST( ComparisonMatchExpression, MatchesArrayValue ) { BSONObj operand = BSON( "$gte" << 5 ); GTEMatchExpression gte; ASSERT( gte.init( "a", operand[ "$gte" ] ).isOK() ); - ASSERT( gte.matches( BSON( "a" << BSON_ARRAY( 4 << 5.5 ) ), NULL ) ); - ASSERT( !gte.matches( BSON( "a" << BSON_ARRAY( 1 << 2 ) ), NULL ) ); + ASSERT( gte.matchesBSON( BSON( "a" << BSON_ARRAY( 4 << 5.5 ) ), NULL ) ); + ASSERT( !gte.matchesBSON( BSON( "a" << BSON_ARRAY( 1 << 2 ) ), NULL ) ); } TEST( ComparisonMatchExpression, MatchesWholeArray ) { @@ -649,34 +649,34 @@ namespace mongo { GTEMatchExpression gte; ASSERT( gte.init( "a", operand[ "$gte" ] ).isOK() ); // Arrays are not comparable as inequalities. - ASSERT( !gte.matches( BSON( "a" << BSON_ARRAY( 6 ) ), NULL ) ); + ASSERT( !gte.matchesBSON( BSON( "a" << BSON_ARRAY( 6 ) ), NULL ) ); } TEST( ComparisonMatchExpression, MatchesNull ) { BSONObj operand = BSON( "$gte" << BSONNULL ); GTEMatchExpression gte; ASSERT( gte.init( "a", operand[ "$gte" ] ).isOK() ); - ASSERT( gte.matches( BSONObj(), NULL ) ); - ASSERT( gte.matches( BSON( "a" << BSONNULL ), NULL ) ); - ASSERT( !gte.matches( BSON( "a" << 4 ), NULL ) ); + ASSERT( gte.matchesBSON( BSONObj(), NULL ) ); + ASSERT( gte.matchesBSON( BSON( "a" << BSONNULL ), NULL ) ); + ASSERT( !gte.matchesBSON( BSON( "a" << 4 ), NULL ) ); } TEST( ComparisonMatchExpression, MatchesMinKey ) { BSONObj operand = BSON( "a" << MinKey ); GTEMatchExpression gte; ASSERT( gte.init( "a", operand[ "a" ] ).isOK() ); - ASSERT( gte.matches( BSON( "a" << MinKey ), NULL ) ); - ASSERT( gte.matches( BSON( "a" << MaxKey ), NULL ) ); - ASSERT( gte.matches( BSON( "a" << 4 ), NULL ) ); + ASSERT( gte.matchesBSON( BSON( "a" << MinKey ), NULL ) ); + ASSERT( gte.matchesBSON( BSON( "a" << MaxKey ), NULL ) ); + ASSERT( gte.matchesBSON( BSON( "a" << 4 ), NULL ) ); } TEST( ComparisonMatchExpression, MatchesMaxKey ) { BSONObj operand = BSON( "a" << MaxKey ); GTEMatchExpression gte; ASSERT( gte.init( "a", operand[ "a" ] ).isOK() ); - ASSERT( gte.matches( BSON( "a" << MaxKey ), NULL ) ); - ASSERT( !gte.matches( BSON( "a" << MinKey ), NULL ) ); - ASSERT( !gte.matches( BSON( "a" << 4 ), NULL ) ); + ASSERT( gte.matchesBSON( BSON( "a" << MaxKey ), NULL ) ); + ASSERT( !gte.matchesBSON( BSON( "a" << MinKey ), NULL ) ); + ASSERT( !gte.matchesBSON( BSON( "a" << 4 ), NULL ) ); } TEST( ComparisonMatchExpression, ElemMatchKey ) { @@ -685,11 +685,11 @@ namespace mongo { ASSERT( gte.init( "a", operand[ "$gte" ] ).isOK() ); MatchDetails details; details.requestElemMatchKey(); - ASSERT( !gte.matches( BSON( "a" << 4 ), &details ) ); + ASSERT( !gte.matchesBSON( BSON( "a" << 4 ), &details ) ); ASSERT( !details.hasElemMatchKey() ); - ASSERT( gte.matches( BSON( "a" << 6 ), &details ) ); + ASSERT( gte.matchesBSON( BSON( "a" << 6 ), &details ) ); ASSERT( !details.hasElemMatchKey() ); - ASSERT( gte.matches( BSON( "a" << BSON_ARRAY( 2 << 6 << 5 ) ), &details ) ); + ASSERT( gte.matchesBSON( BSON( "a" << BSON_ARRAY( 2 << 6 << 5 ) ), &details ) ); ASSERT( details.hasElemMatchKey() ); ASSERT_EQUALS( "1", details.elemMatchKey() ); } @@ -765,26 +765,26 @@ namespace mongo { BSONObj operand = BSON( "$ne" << 5 ); NEMatchExpression ne; ASSERT( ne.init( "a", operand[ "$ne" ] ).isOK() ); - ASSERT( ne.matches( BSON( "a" << 4 ), NULL ) ); - ASSERT( !ne.matches( BSON( "a" << 5 ), NULL ) ); + ASSERT( ne.matchesBSON( BSON( "a" << 4 ), NULL ) ); + ASSERT( !ne.matchesBSON( BSON( "a" << 5 ), NULL ) ); } TEST( NeOp, MatchesArrayValue ) { BSONObj operand = BSON( "$ne" << 5 ); NEMatchExpression ne; ASSERT( ne.init( "a", operand[ "$ne" ] ).isOK() ); - ASSERT( ne.matches( BSON( "a" << BSON_ARRAY( 4 << 6 ) ), NULL ) ); - ASSERT( !ne.matches( BSON( "a" << BSON_ARRAY( 4 << 5 ) ), NULL ) ); - ASSERT( !ne.matches( BSON( "a" << BSON_ARRAY( 5 << 5 ) ), NULL ) ); + ASSERT( ne.matchesBSON( BSON( "a" << BSON_ARRAY( 4 << 6 ) ), NULL ) ); + ASSERT( !ne.matchesBSON( BSON( "a" << BSON_ARRAY( 4 << 5 ) ), NULL ) ); + ASSERT( !ne.matchesBSON( BSON( "a" << BSON_ARRAY( 5 << 5 ) ), NULL ) ); } TEST( NeOp, MatchesNull ) { BSONObj operand = BSON( "$ne" << BSONNULL ); NEMatchExpression ne; ASSERT( ne.init( "a", operand[ "$ne" ] ).isOK() ); - ASSERT( ne.matches( BSON( "a" << 4 ), NULL ) ); - ASSERT( !ne.matches( BSONObj(), NULL ) ); - ASSERT( !ne.matches( BSON( "a" << BSONNULL ), NULL ) ); + ASSERT( ne.matchesBSON( BSON( "a" << 4 ), NULL ) ); + ASSERT( !ne.matchesBSON( BSONObj(), NULL ) ); + ASSERT( !ne.matchesBSON( BSON( "a" << BSONNULL ), NULL ) ); } TEST( NeOp, ElemMatchKey ) { @@ -793,9 +793,9 @@ namespace mongo { ASSERT( ne.init( "a", operand[ "$ne" ] ).isOK() ); MatchDetails details; details.requestElemMatchKey(); - ASSERT( !ne.matches( BSON( "a" << BSON_ARRAY( 2 << 6 << 5 ) ), &details ) ); + ASSERT( !ne.matchesBSON( BSON( "a" << BSON_ARRAY( 2 << 6 << 5 ) ), &details ) ); ASSERT( !details.hasElemMatchKey() ); - ASSERT( ne.matches( BSON( "a" << BSON_ARRAY( 2 << 6 ) ), &details ) ); + ASSERT( ne.matchesBSON( BSON( "a" << BSON_ARRAY( 2 << 6 ) ), &details ) ); // The elemMatchKey feature is not implemented for $ne. ASSERT( !details.hasElemMatchKey() ); } @@ -960,22 +960,22 @@ namespace mongo { TEST( RegexMatchExpression, MatchesScalar ) { RegexMatchExpression regex; ASSERT( regex.init( "a", "b", "" ).isOK() ); - ASSERT( regex.matches( BSON( "a" << "b" ), NULL ) ); - ASSERT( !regex.matches( BSON( "a" << "c" ), NULL ) ); + ASSERT( regex.matchesBSON( BSON( "a" << "b" ), NULL ) ); + ASSERT( !regex.matchesBSON( BSON( "a" << "c" ), NULL ) ); } TEST( RegexMatchExpression, MatchesArrayValue ) { RegexMatchExpression regex; ASSERT( regex.init( "a", "b", "" ).isOK() ); - ASSERT( regex.matches( BSON( "a" << BSON_ARRAY( "c" << "b" ) ), NULL ) ); - ASSERT( !regex.matches( BSON( "a" << BSON_ARRAY( "d" << "c" ) ), NULL ) ); + ASSERT( regex.matchesBSON( BSON( "a" << BSON_ARRAY( "c" << "b" ) ), NULL ) ); + ASSERT( !regex.matchesBSON( BSON( "a" << BSON_ARRAY( "d" << "c" ) ), NULL ) ); } TEST( RegexMatchExpression, MatchesNull ) { RegexMatchExpression regex; ASSERT( regex.init( "a", "b", "" ).isOK() ); - ASSERT( !regex.matches( BSONObj(), NULL ) ); - ASSERT( !regex.matches( BSON( "a" << BSONNULL ), NULL ) ); + ASSERT( !regex.matchesBSON( BSONObj(), NULL ) ); + ASSERT( !regex.matchesBSON( BSON( "a" << BSONNULL ), NULL ) ); } TEST( RegexMatchExpression, ElemMatchKey ) { @@ -983,11 +983,11 @@ namespace mongo { ASSERT( regex.init( "a", "b", "" ).isOK() ); MatchDetails details; details.requestElemMatchKey(); - ASSERT( !regex.matches( BSON( "a" << "c" ), &details ) ); + ASSERT( !regex.matchesBSON( BSON( "a" << "c" ), &details ) ); ASSERT( !details.hasElemMatchKey() ); - ASSERT( regex.matches( BSON( "a" << "b" ), &details ) ); + ASSERT( regex.matchesBSON( BSON( "a" << "b" ), &details ) ); ASSERT( !details.hasElemMatchKey() ); - ASSERT( regex.matches( BSON( "a" << BSON_ARRAY( "c" << "b" ) ), &details ) ); + ASSERT( regex.matchesBSON( BSON( "a" << BSON_ARRAY( "c" << "b" ) ), &details ) ); ASSERT( details.hasElemMatchKey() ); ASSERT_EQUALS( "1", details.elemMatchKey() ); } @@ -1070,22 +1070,22 @@ namespace mongo { TEST( ModMatchExpression, MatchesScalar ) { ModMatchExpression mod; ASSERT( mod.init( "a", 5, 2 ).isOK() ); - ASSERT( mod.matches( BSON( "a" << 7.0 ), NULL ) ); - ASSERT( !mod.matches( BSON( "a" << 4 ), NULL ) ); + ASSERT( mod.matchesBSON( BSON( "a" << 7.0 ), NULL ) ); + ASSERT( !mod.matchesBSON( BSON( "a" << 4 ), NULL ) ); } TEST( ModMatchExpression, MatchesArrayValue ) { ModMatchExpression mod; ASSERT( mod.init( "a", 5, 2 ).isOK() ); - ASSERT( mod.matches( BSON( "a" << BSON_ARRAY( 5 << 12LL ) ), NULL ) ); - ASSERT( !mod.matches( BSON( "a" << BSON_ARRAY( 6 << 8 ) ), NULL ) ); + ASSERT( mod.matchesBSON( BSON( "a" << BSON_ARRAY( 5 << 12LL ) ), NULL ) ); + ASSERT( !mod.matchesBSON( BSON( "a" << BSON_ARRAY( 6 << 8 ) ), NULL ) ); } TEST( ModMatchExpression, MatchesNull ) { ModMatchExpression mod; ASSERT( mod.init( "a", 5, 2 ).isOK() ); - ASSERT( !mod.matches( BSONObj(), NULL ) ); - ASSERT( !mod.matches( BSON( "a" << BSONNULL ), NULL ) ); + ASSERT( !mod.matchesBSON( BSONObj(), NULL ) ); + ASSERT( !mod.matchesBSON( BSON( "a" << BSONNULL ), NULL ) ); } TEST( ModMatchExpression, ElemMatchKey ) { @@ -1093,11 +1093,11 @@ namespace mongo { ASSERT( mod.init( "a", 5, 2 ).isOK() ); MatchDetails details; details.requestElemMatchKey(); - ASSERT( !mod.matches( BSON( "a" << 4 ), &details ) ); + ASSERT( !mod.matchesBSON( BSON( "a" << 4 ), &details ) ); ASSERT( !details.hasElemMatchKey() ); - ASSERT( mod.matches( BSON( "a" << 2 ), &details ) ); + ASSERT( mod.matchesBSON( BSON( "a" << 2 ), &details ) ); ASSERT( !details.hasElemMatchKey() ); - ASSERT( mod.matches( BSON( "a" << BSON_ARRAY( 1 << 2 << 5 ) ), &details ) ); + ASSERT( mod.matchesBSON( BSON( "a" << BSON_ARRAY( 1 << 2 << 5 ) ), &details ) ); ASSERT( details.hasElemMatchKey() ); ASSERT_EQUALS( "1", details.elemMatchKey() ); } @@ -1169,23 +1169,23 @@ namespace mongo { TEST( ExistsMatchExpression, MatchesScalar ) { ExistsMatchExpression exists; ASSERT( exists.init( "a", true ).isOK() ); - ASSERT( exists.matches( BSON( "a" << 1 ), NULL ) ); - ASSERT( exists.matches( BSON( "a" << BSONNULL ), NULL ) ); - ASSERT( !exists.matches( BSON( "b" << 1 ), NULL ) ); + ASSERT( exists.matchesBSON( BSON( "a" << 1 ), NULL ) ); + ASSERT( exists.matchesBSON( BSON( "a" << BSONNULL ), NULL ) ); + ASSERT( !exists.matchesBSON( BSON( "b" << 1 ), NULL ) ); } TEST( ExistsMatchExpression, MatchesScalarFalse ) { ExistsMatchExpression exists; ASSERT( exists.init( "a", false ).isOK() ); - ASSERT( !exists.matches( BSON( "a" << 1 ), NULL ) ); - ASSERT( !exists.matches( BSON( "a" << BSONNULL ), NULL ) ); - ASSERT( exists.matches( BSON( "b" << 1 ), NULL ) ); + ASSERT( !exists.matchesBSON( BSON( "a" << 1 ), NULL ) ); + ASSERT( !exists.matchesBSON( BSON( "a" << BSONNULL ), NULL ) ); + ASSERT( exists.matchesBSON( BSON( "b" << 1 ), NULL ) ); } TEST( ExistsMatchExpression, MatchesArray ) { ExistsMatchExpression exists; ASSERT( exists.init( "a", true ).isOK() ); - ASSERT( exists.matches( BSON( "a" << BSON_ARRAY( 4 << 5.5 ) ), NULL ) ); + ASSERT( exists.matchesBSON( BSON( "a" << BSON_ARRAY( 4 << 5.5 ) ), NULL ) ); } TEST( ExistsMatchExpression, ElemMatchKey ) { @@ -1193,11 +1193,11 @@ namespace mongo { ASSERT( exists.init( "a.b", true ).isOK() ); MatchDetails details; details.requestElemMatchKey(); - ASSERT( !exists.matches( BSON( "a" << 1 ), &details ) ); + ASSERT( !exists.matchesBSON( BSON( "a" << 1 ), &details ) ); ASSERT( !details.hasElemMatchKey() ); - ASSERT( exists.matches( BSON( "a" << BSON( "b" << 6 ) ), &details ) ); + ASSERT( exists.matchesBSON( BSON( "a" << BSON( "b" << 6 ) ), &details ) ); ASSERT( !details.hasElemMatchKey() ); - ASSERT( exists.matches( BSON( "a" << BSON_ARRAY( 2 << BSON( "b" << 7 ) ) ), &details ) ); + ASSERT( exists.matchesBSON( BSON( "a" << BSON_ARRAY( 2 << BSON( "b" << 7 ) ) ), &details ) ); ASSERT( details.hasElemMatchKey() ); ASSERT_EQUALS( "1", details.elemMatchKey() ); } @@ -1261,36 +1261,36 @@ namespace mongo { TEST( TypeMatchExpression, MatchesScalar ) { TypeMatchExpression type; ASSERT( type.init( "a", Bool ).isOK() ); - ASSERT( type.matches( BSON( "a" << true ), NULL ) ); - ASSERT( !type.matches( BSON( "a" << 1 ), NULL ) ); + ASSERT( type.matchesBSON( BSON( "a" << true ), NULL ) ); + ASSERT( !type.matchesBSON( BSON( "a" << 1 ), NULL ) ); } TEST( TypeMatchExpression, MatchesArray ) { TypeMatchExpression type; ASSERT( type.init( "a", NumberInt ).isOK() ); - ASSERT( type.matches( BSON( "a" << BSON_ARRAY( 4 ) ), NULL ) ); - ASSERT( type.matches( BSON( "a" << BSON_ARRAY( 4 << "a" ) ), NULL ) ); - ASSERT( type.matches( BSON( "a" << BSON_ARRAY( "a" << 4 ) ), NULL ) ); - ASSERT( !type.matches( BSON( "a" << BSON_ARRAY( "a" ) ), NULL ) ); - ASSERT( !type.matches( BSON( "a" << BSON_ARRAY( BSON_ARRAY( 4 ) ) ), NULL ) ); + ASSERT( type.matchesBSON( BSON( "a" << BSON_ARRAY( 4 ) ), NULL ) ); + ASSERT( type.matchesBSON( BSON( "a" << BSON_ARRAY( 4 << "a" ) ), NULL ) ); + ASSERT( type.matchesBSON( BSON( "a" << BSON_ARRAY( "a" << 4 ) ), NULL ) ); + ASSERT( !type.matchesBSON( BSON( "a" << BSON_ARRAY( "a" ) ), NULL ) ); + ASSERT( !type.matchesBSON( BSON( "a" << BSON_ARRAY( BSON_ARRAY( 4 ) ) ), NULL ) ); } TEST( TypeMatchExpression, MatchesOuterArray ) { TypeMatchExpression type; ASSERT( type.init( "a", Array ).isOK() ); // The outer array is not matched. - ASSERT( !type.matches( BSON( "a" << BSONArray() ), NULL ) ); - ASSERT( !type.matches( BSON( "a" << BSON_ARRAY( 4 << "a" ) ), NULL ) ); - ASSERT( type.matches( BSON( "a" << BSON_ARRAY( BSONArray() << 2 ) ), NULL ) ); - ASSERT( !type.matches( BSON( "a" << "bar" ), NULL ) ); + ASSERT( !type.matchesBSON( BSON( "a" << BSONArray() ), NULL ) ); + ASSERT( !type.matchesBSON( BSON( "a" << BSON_ARRAY( 4 << "a" ) ), NULL ) ); + ASSERT( type.matchesBSON( BSON( "a" << BSON_ARRAY( BSONArray() << 2 ) ), NULL ) ); + ASSERT( !type.matchesBSON( BSON( "a" << "bar" ), NULL ) ); } TEST( TypeMatchExpression, MatchesNull ) { TypeMatchExpression type; ASSERT( type.init( "a", jstNULL ).isOK() ); - ASSERT( type.matches( BSON( "a" << BSONNULL ), NULL ) ); - ASSERT( !type.matches( BSON( "a" << 4 ), NULL ) ); - ASSERT( !type.matches( BSONObj(), NULL ) ); + ASSERT( type.matchesBSON( BSON( "a" << BSONNULL ), NULL ) ); + ASSERT( !type.matchesBSON( BSON( "a" << 4 ), NULL ) ); + ASSERT( !type.matchesBSON( BSONObj(), NULL ) ); } TEST( TypeMatchExpression, ElemMatchKey ) { @@ -1298,14 +1298,14 @@ namespace mongo { ASSERT( type.init( "a.b", String ).isOK() ); MatchDetails details; details.requestElemMatchKey(); - ASSERT( !type.matches( BSON( "a" << 1 ), &details ) ); + ASSERT( !type.matchesBSON( BSON( "a" << 1 ), &details ) ); ASSERT( !details.hasElemMatchKey() ); - ASSERT( type.matches( BSON( "a" << BSON( "b" << "string" ) ), &details ) ); + ASSERT( type.matchesBSON( BSON( "a" << BSON( "b" << "string" ) ), &details ) ); ASSERT( !details.hasElemMatchKey() ); - ASSERT( type.matches( BSON( "a" << BSON( "b" << BSON_ARRAY( "string" ) ) ), &details ) ); + ASSERT( type.matchesBSON( BSON( "a" << BSON( "b" << BSON_ARRAY( "string" ) ) ), &details ) ); ASSERT( details.hasElemMatchKey() ); ASSERT_EQUALS( "0", details.elemMatchKey() ); - ASSERT( type.matches( BSON( "a" << + ASSERT( type.matchesBSON( BSON( "a" << BSON_ARRAY( 2 << BSON( "b" << BSON_ARRAY( "string" ) ) ) ), &details ) ); @@ -1356,8 +1356,8 @@ namespace mongo { BSONObj notMatch = BSON( "a" << 2 ); ASSERT( !in.matchesSingleElement( notMatch[ "a" ] ) ); - ASSERT( !in.matches( BSON( "a" << 1 ), NULL ) ); - ASSERT( !in.matches( BSONObj(), NULL ) ); + ASSERT( !in.matchesBSON( BSON( "a" << 1 ), NULL ) ); + ASSERT( !in.matchesBSON( BSONObj(), NULL ) ); } TEST( InMatchExpression, MatchesElementMultiple ) { @@ -1385,8 +1385,8 @@ namespace mongo { in.init( "a" ); in.getArrayFilterEntries()->addEquality( operand.firstElement() ); - ASSERT( in.matches( BSON( "a" << 5.0 ), NULL ) ); - ASSERT( !in.matches( BSON( "a" << 4 ), NULL ) ); + ASSERT( in.matchesBSON( BSON( "a" << 5.0 ), NULL ) ); + ASSERT( !in.matchesBSON( BSON( "a" << 4 ), NULL ) ); } TEST( InMatchExpression, MatchesArrayValue ) { @@ -1395,9 +1395,9 @@ namespace mongo { in.init( "a" ); in.getArrayFilterEntries()->addEquality( operand.firstElement() ); - ASSERT( in.matches( BSON( "a" << BSON_ARRAY( 5.0 << 6 ) ), NULL ) ); - ASSERT( !in.matches( BSON( "a" << BSON_ARRAY( 6 << 7 ) ), NULL ) ); - ASSERT( !in.matches( BSON( "a" << BSON_ARRAY( BSON_ARRAY( 5 ) ) ), NULL ) ); + ASSERT( in.matchesBSON( BSON( "a" << BSON_ARRAY( 5.0 << 6 ) ), NULL ) ); + ASSERT( !in.matchesBSON( BSON( "a" << BSON_ARRAY( 6 << 7 ) ), NULL ) ); + ASSERT( !in.matchesBSON( BSON( "a" << BSON_ARRAY( BSON_ARRAY( 5 ) ) ), NULL ) ); } TEST( InMatchExpression, MatchesNull ) { @@ -1407,9 +1407,9 @@ namespace mongo { in.init( "a" ); in.getArrayFilterEntries()->addEquality( operand.firstElement() ); - ASSERT( in.matches( BSONObj(), NULL ) ); - ASSERT( in.matches( BSON( "a" << BSONNULL ), NULL ) ); - ASSERT( !in.matches( BSON( "a" << 4 ), NULL ) ); + ASSERT( in.matchesBSON( BSONObj(), NULL ) ); + ASSERT( in.matchesBSON( BSON( "a" << BSONNULL ), NULL ) ); + ASSERT( !in.matchesBSON( BSON( "a" << 4 ), NULL ) ); } TEST( InMatchExpression, MatchesMinKey ) { @@ -1418,9 +1418,9 @@ namespace mongo { in.init( "a" ); in.getArrayFilterEntries()->addEquality( operand.firstElement() ); - ASSERT( in.matches( BSON( "a" << MinKey ), NULL ) ); - ASSERT( !in.matches( BSON( "a" << MaxKey ), NULL ) ); - ASSERT( !in.matches( BSON( "a" << 4 ), NULL ) ); + ASSERT( in.matchesBSON( BSON( "a" << MinKey ), NULL ) ); + ASSERT( !in.matchesBSON( BSON( "a" << MaxKey ), NULL ) ); + ASSERT( !in.matchesBSON( BSON( "a" << 4 ), NULL ) ); } TEST( InMatchExpression, MatchesMaxKey ) { @@ -1429,9 +1429,9 @@ namespace mongo { in.init( "a" ); in.getArrayFilterEntries()->addEquality( operand.firstElement() ); - ASSERT( in.matches( BSON( "a" << MaxKey ), NULL ) ); - ASSERT( !in.matches( BSON( "a" << MinKey ), NULL ) ); - ASSERT( !in.matches( BSON( "a" << 4 ), NULL ) ); + ASSERT( in.matchesBSON( BSON( "a" << MaxKey ), NULL ) ); + ASSERT( !in.matchesBSON( BSON( "a" << MinKey ), NULL ) ); + ASSERT( !in.matchesBSON( BSON( "a" << 4 ), NULL ) ); } TEST( InMatchExpression, MatchesFullArray ) { @@ -1442,10 +1442,10 @@ namespace mongo { in.getArrayFilterEntries()->addEquality( operand[1] ); in.getArrayFilterEntries()->addEquality( operand[2] ); - ASSERT( in.matches( BSON( "a" << BSON_ARRAY( 1 << 2 ) ), NULL ) ); - ASSERT( !in.matches( BSON( "a" << BSON_ARRAY( 1 << 2 << 3 ) ), NULL ) ); - ASSERT( !in.matches( BSON( "a" << BSON_ARRAY( 1 ) ), NULL ) ); - ASSERT( !in.matches( BSON( "a" << 1 ), NULL ) ); + ASSERT( in.matchesBSON( BSON( "a" << BSON_ARRAY( 1 << 2 ) ), NULL ) ); + ASSERT( !in.matchesBSON( BSON( "a" << BSON_ARRAY( 1 << 2 << 3 ) ), NULL ) ); + ASSERT( !in.matchesBSON( BSON( "a" << BSON_ARRAY( 1 ) ), NULL ) ); + ASSERT( !in.matchesBSON( BSON( "a" << 1 ), NULL ) ); } TEST( InMatchExpression, ElemMatchKey ) { @@ -1457,11 +1457,11 @@ namespace mongo { MatchDetails details; details.requestElemMatchKey(); - ASSERT( !in.matches( BSON( "a" << 4 ), &details ) ); + ASSERT( !in.matchesBSON( BSON( "a" << 4 ), &details ) ); ASSERT( !details.hasElemMatchKey() ); - ASSERT( in.matches( BSON( "a" << 5 ), &details ) ); + ASSERT( in.matchesBSON( BSON( "a" << 5 ), &details ) ); ASSERT( !details.hasElemMatchKey() ); - ASSERT( in.matches( BSON( "a" << BSON_ARRAY( 1 << 2 << 5 ) ), &details ) ); + ASSERT( in.matchesBSON( BSON( "a" << BSON_ARRAY( 1 << 2 << 5 ) ), &details ) ); ASSERT( details.hasElemMatchKey() ); ASSERT_EQUALS( "1", details.elemMatchKey() ); } @@ -1573,9 +1573,9 @@ namespace mongo { NinMatchExpression nin; nin.init( "a" ); nin.getArrayFilterEntries()->addEquality( operand.firstElement() ); - ASSERT( nin.matches( BSON( "a" << 4 ), NULL ) ); - ASSERT( !nin.matches( BSON( "a" << 5 ), NULL ) ); - ASSERT( !nin.matches( BSON( "a" << 5.0 ), NULL ) ); + ASSERT( nin.matchesBSON( BSON( "a" << 4 ), NULL ) ); + ASSERT( !nin.matchesBSON( BSON( "a" << 5 ), NULL ) ); + ASSERT( !nin.matchesBSON( BSON( "a" << 5.0 ), NULL ) ); } TEST( NinMatchExpression, MatchesArrayValue ) { @@ -1583,9 +1583,9 @@ namespace mongo { NinMatchExpression nin; nin.init( "a" ); nin.getArrayFilterEntries()->addEquality( operand.firstElement() ); - ASSERT( !nin.matches( BSON( "a" << BSON_ARRAY( 5.0 << 6 ) ), NULL ) ); - ASSERT( nin.matches( BSON( "a" << BSON_ARRAY( 6 << 7 ) ), NULL ) ); - ASSERT( nin.matches( BSON( "a" << BSON_ARRAY( BSON_ARRAY( 5 ) ) ), NULL ) ); + ASSERT( !nin.matchesBSON( BSON( "a" << BSON_ARRAY( 5.0 << 6 ) ), NULL ) ); + ASSERT( nin.matchesBSON( BSON( "a" << BSON_ARRAY( 6 << 7 ) ), NULL ) ); + ASSERT( nin.matchesBSON( BSON( "a" << BSON_ARRAY( BSON_ARRAY( 5 ) ) ), NULL ) ); } TEST( NinMatchExpression, MatchesNull ) { @@ -1595,9 +1595,9 @@ namespace mongo { NinMatchExpression nin; nin.init( "a" ); nin.getArrayFilterEntries()->addEquality( operand.firstElement() ); - ASSERT( !nin.matches( BSONObj(), NULL ) ); - ASSERT( !nin.matches( BSON( "a" << BSONNULL ), NULL ) ); - ASSERT( nin.matches( BSON( "a" << 4 ), NULL ) ); + ASSERT( !nin.matchesBSON( BSONObj(), NULL ) ); + ASSERT( !nin.matchesBSON( BSON( "a" << BSONNULL ), NULL ) ); + ASSERT( nin.matchesBSON( BSON( "a" << 4 ), NULL ) ); } TEST( NinMatchExpression, MatchesFullArray ) { @@ -1607,10 +1607,10 @@ namespace mongo { nin.getArrayFilterEntries()->addEquality( operand[0] ); nin.getArrayFilterEntries()->addEquality( operand[1] ); nin.getArrayFilterEntries()->addEquality( operand[2] ); - ASSERT( !nin.matches( BSON( "a" << BSON_ARRAY( 1 << 2 ) ), NULL ) ); - ASSERT( nin.matches( BSON( "a" << BSON_ARRAY( 1 << 2 << 3 ) ), NULL ) ); - ASSERT( nin.matches( BSON( "a" << BSON_ARRAY( 1 ) ), NULL ) ); - ASSERT( nin.matches( BSON( "a" << 1 ), NULL ) ); + ASSERT( !nin.matchesBSON( BSON( "a" << BSON_ARRAY( 1 << 2 ) ), NULL ) ); + ASSERT( nin.matchesBSON( BSON( "a" << BSON_ARRAY( 1 << 2 << 3 ) ), NULL ) ); + ASSERT( nin.matchesBSON( BSON( "a" << BSON_ARRAY( 1 ) ), NULL ) ); + ASSERT( nin.matchesBSON( BSON( "a" << 1 ), NULL ) ); } TEST( NinMatchExpression, ElemMatchKey ) { @@ -1622,11 +1622,11 @@ namespace mongo { MatchDetails details; details.requestElemMatchKey(); - ASSERT( !nin.matches( BSON( "a" << 2 ), &details ) ); + ASSERT( !nin.matchesBSON( BSON( "a" << 2 ), &details ) ); ASSERT( !details.hasElemMatchKey() ); - ASSERT( nin.matches( BSON( "a" << 3 ), &details ) ); + ASSERT( nin.matchesBSON( BSON( "a" << 3 ), &details ) ); ASSERT( !details.hasElemMatchKey() ); - ASSERT( nin.matches( BSON( "a" << BSON_ARRAY( 1 << 3 << 6 ) ), &details ) ); + ASSERT( nin.matchesBSON( BSON( "a" << BSON_ARRAY( 1 << 3 << 6 ) ), &details ) ); // The elemMatchKey feature is not implemented for $nin. ASSERT( !details.hasElemMatchKey() ); } diff --git a/src/mongo/db/matcher/expression_parser_array_test.cpp b/src/mongo/db/matcher/expression_parser_array_test.cpp index 46f1f158807..a504833762f 100644 --- a/src/mongo/db/matcher/expression_parser_array_test.cpp +++ b/src/mongo/db/matcher/expression_parser_array_test.cpp @@ -32,10 +32,10 @@ namespace mongo { StatusWithMatchExpression result = MatchExpressionParser::parse( query ); ASSERT_TRUE( result.isOK() ); - ASSERT( !result.getValue()->matches( BSON( "x" << 1 ) ) ); - ASSERT( result.getValue()->matches( BSON( "x" << BSON_ARRAY( 1 << 2 ) ) ) ); - ASSERT( !result.getValue()->matches( BSON( "x" << BSON_ARRAY( 1 ) ) ) ); - ASSERT( !result.getValue()->matches( BSON( "x" << BSON_ARRAY( 1 << 2 << 3 ) ) ) ); + ASSERT( !result.getValue()->matchesBSON( BSON( "x" << 1 ) ) ); + ASSERT( result.getValue()->matchesBSON( BSON( "x" << BSON_ARRAY( 1 << 2 ) ) ) ); + ASSERT( !result.getValue()->matchesBSON( BSON( "x" << BSON_ARRAY( 1 ) ) ) ); + ASSERT( !result.getValue()->matchesBSON( BSON( "x" << BSON_ARRAY( 1 << 2 << 3 ) ) ) ); } TEST( MatchExpressionParserArrayTest, SizeAsString ) { @@ -43,10 +43,10 @@ namespace mongo { StatusWithMatchExpression result = MatchExpressionParser::parse( query ); ASSERT_TRUE( result.isOK() ); - ASSERT( !result.getValue()->matches( BSON( "x" << 1 ) ) ); - ASSERT( !result.getValue()->matches( BSON( "x" << BSON_ARRAY( 1 << 2 ) ) ) ); - ASSERT( result.getValue()->matches( BSON( "x" << BSONArray() ) ) ); - ASSERT( !result.getValue()->matches( BSON( "x" << BSON_ARRAY( 1 ) ) ) ); + ASSERT( !result.getValue()->matchesBSON( BSON( "x" << 1 ) ) ); + ASSERT( !result.getValue()->matchesBSON( BSON( "x" << BSON_ARRAY( 1 << 2 ) ) ) ); + ASSERT( result.getValue()->matchesBSON( BSON( "x" << BSONArray() ) ) ); + ASSERT( !result.getValue()->matchesBSON( BSON( "x" << BSON_ARRAY( 1 ) ) ) ); } TEST( MatchExpressionParserArrayTest, SizeWithDouble ) { @@ -54,11 +54,11 @@ namespace mongo { StatusWithMatchExpression result = MatchExpressionParser::parse( query ); ASSERT_TRUE( result.isOK() ); - ASSERT( !result.getValue()->matches( BSON( "x" << 1 ) ) ); - ASSERT( !result.getValue()->matches( BSON( "x" << BSON_ARRAY( 1 << 2 ) ) ) ); - ASSERT( !result.getValue()->matches( BSON( "x" << BSON_ARRAY( 1 ) ) ) ); - ASSERT( !result.getValue()->matches( BSON( "x" << BSONArray() ) ) ); - ASSERT( !result.getValue()->matches( BSON( "x" << BSON_ARRAY( 1 << 2 << 3 ) ) ) ); + ASSERT( !result.getValue()->matchesBSON( BSON( "x" << 1 ) ) ); + ASSERT( !result.getValue()->matchesBSON( BSON( "x" << BSON_ARRAY( 1 << 2 ) ) ) ); + ASSERT( !result.getValue()->matchesBSON( BSON( "x" << BSON_ARRAY( 1 ) ) ) ); + ASSERT( !result.getValue()->matchesBSON( BSON( "x" << BSONArray() ) ) ); + ASSERT( !result.getValue()->matchesBSON( BSON( "x" << BSON_ARRAY( 1 << 2 << 3 ) ) ) ); } TEST( MatchExpressionParserArrayTest, SizeBad ) { @@ -74,10 +74,10 @@ namespace mongo { StatusWithMatchExpression result = MatchExpressionParser::parse( query ); ASSERT_TRUE( result.isOK() ); - ASSERT( !result.getValue()->matches( BSON( "x" << 1 ) ) ); - ASSERT( !result.getValue()->matches( BSON( "x" << BSON_ARRAY( 1 << 2 ) ) ) ); - ASSERT( !result.getValue()->matches( BSON( "x" << BSON_ARRAY( BSON( "x" << 1 ) ) ) ) ); - ASSERT( result.getValue()->matches( BSON( "x" << + ASSERT( !result.getValue()->matchesBSON( BSON( "x" << 1 ) ) ); + ASSERT( !result.getValue()->matchesBSON( BSON( "x" << BSON_ARRAY( 1 << 2 ) ) ) ); + ASSERT( !result.getValue()->matchesBSON( BSON( "x" << BSON_ARRAY( BSON( "x" << 1 ) ) ) ) ); + ASSERT( result.getValue()->matchesBSON( BSON( "x" << BSON_ARRAY( BSON( "x" << 1 << "y" << 2 ) ) ) ) ); } @@ -87,9 +87,9 @@ namespace mongo { StatusWithMatchExpression result = MatchExpressionParser::parse( query ); ASSERT_TRUE( result.isOK() ); - ASSERT( !result.getValue()->matches( BSON( "x" << 1 ) ) ); - ASSERT( !result.getValue()->matches( BSON( "x" << BSON_ARRAY( 4 ) ) ) ); - ASSERT( result.getValue()->matches( BSON( "x" << BSON_ARRAY( 6 ) ) ) ); + ASSERT( !result.getValue()->matchesBSON( BSON( "x" << 1 ) ) ); + ASSERT( !result.getValue()->matchesBSON( BSON( "x" << BSON_ARRAY( 4 ) ) ) ); + ASSERT( result.getValue()->matchesBSON( BSON( "x" << BSON_ARRAY( 6 ) ) ) ); } TEST( MatchExpressionParserArrayTest, All1 ) { @@ -97,12 +97,12 @@ namespace mongo { StatusWithMatchExpression result = MatchExpressionParser::parse( query ); ASSERT_TRUE( result.isOK() ); - ASSERT( !result.getValue()->matches( BSON( "x" << 1 ) ) ); - ASSERT( !result.getValue()->matches( BSON( "x" << BSON_ARRAY( 1 ) ) ) ); - ASSERT( !result.getValue()->matches( BSON( "x" << BSON_ARRAY( 2 ) ) ) ); - ASSERT( result.getValue()->matches( BSON( "x" << BSON_ARRAY( 1 << 2 ) ) ) ); - ASSERT( result.getValue()->matches( BSON( "x" << BSON_ARRAY( 1 << 2 << 3 ) ) ) ); - ASSERT( !result.getValue()->matches( BSON( "x" << BSON_ARRAY( 2 << 3 ) ) ) ); + ASSERT( !result.getValue()->matchesBSON( BSON( "x" << 1 ) ) ); + ASSERT( !result.getValue()->matchesBSON( BSON( "x" << BSON_ARRAY( 1 ) ) ) ); + ASSERT( !result.getValue()->matchesBSON( BSON( "x" << BSON_ARRAY( 2 ) ) ) ); + ASSERT( result.getValue()->matchesBSON( BSON( "x" << BSON_ARRAY( 1 << 2 ) ) ) ); + ASSERT( result.getValue()->matchesBSON( BSON( "x" << BSON_ARRAY( 1 << 2 << 3 ) ) ) ); + ASSERT( !result.getValue()->matchesBSON( BSON( "x" << BSON_ARRAY( 2 << 3 ) ) ) ); } TEST( MatchExpressionParserArrayTest, AllBadArg ) { @@ -169,10 +169,10 @@ namespace mongo { StatusWithMatchExpression result = MatchExpressionParser::parse( query ); ASSERT_TRUE( result.isOK() ); - ASSERT( !result.getValue()->matches( BSON( "x" << 1 ) ) ); - ASSERT( !result.getValue()->matches( BSON( "x" << BSON_ARRAY( 1 << 2 ) ) ) ); - ASSERT( !result.getValue()->matches( BSON( "x" << BSON_ARRAY( BSON( "x" << 1 ) ) ) ) ); - ASSERT( result.getValue()->matches( BSON( "x" << + ASSERT( !result.getValue()->matchesBSON( BSON( "x" << 1 ) ) ); + ASSERT( !result.getValue()->matchesBSON( BSON( "x" << BSON_ARRAY( 1 << 2 ) ) ) ); + ASSERT( !result.getValue()->matchesBSON( BSON( "x" << BSON_ARRAY( BSON( "x" << 1 ) ) ) ) ); + ASSERT( result.getValue()->matchesBSON( BSON( "x" << BSON_ARRAY( BSON( "x" << 1 << "y" << 2 ) ) ) ) ); } diff --git a/src/mongo/db/matcher/expression_parser_leaf_test.cpp b/src/mongo/db/matcher/expression_parser_leaf_test.cpp index c9df6ab2f8c..0948034f400 100644 --- a/src/mongo/db/matcher/expression_parser_leaf_test.cpp +++ b/src/mongo/db/matcher/expression_parser_leaf_test.cpp @@ -32,9 +32,9 @@ namespace mongo { StatusWithMatchExpression result = MatchExpressionParser::parse( query ); ASSERT_TRUE( result.isOK() ); - ASSERT( !result.getValue()->matches( BSON( "x" << 1 ) ) ); - ASSERT( result.getValue()->matches( BSON( "x" << 2 ) ) ); - ASSERT( !result.getValue()->matches( BSON( "x" << 3 ) ) ); + ASSERT( !result.getValue()->matchesBSON( BSON( "x" << 1 ) ) ); + ASSERT( result.getValue()->matchesBSON( BSON( "x" << 2 ) ) ); + ASSERT( !result.getValue()->matchesBSON( BSON( "x" << 3 ) ) ); } TEST( MatchExpressionParserLeafTest, SimpleGT1 ) { @@ -42,8 +42,8 @@ namespace mongo { StatusWithMatchExpression result = MatchExpressionParser::parse( query ); ASSERT_TRUE( result.isOK() ); - ASSERT( !result.getValue()->matches( BSON( "x" << 2 ) ) ); - ASSERT( result.getValue()->matches( BSON( "x" << 3 ) ) ); + ASSERT( !result.getValue()->matchesBSON( BSON( "x" << 2 ) ) ); + ASSERT( result.getValue()->matchesBSON( BSON( "x" << 3 ) ) ); } TEST( MatchExpressionParserLeafTest, SimpleLT1 ) { @@ -51,9 +51,9 @@ namespace mongo { StatusWithMatchExpression result = MatchExpressionParser::parse( query ); ASSERT_TRUE( result.isOK() ); - ASSERT( result.getValue()->matches( BSON( "x" << 1 ) ) ); - ASSERT( !result.getValue()->matches( BSON( "x" << 2 ) ) ); - ASSERT( !result.getValue()->matches( BSON( "x" << 3 ) ) ); + ASSERT( result.getValue()->matchesBSON( BSON( "x" << 1 ) ) ); + ASSERT( !result.getValue()->matchesBSON( BSON( "x" << 2 ) ) ); + ASSERT( !result.getValue()->matchesBSON( BSON( "x" << 3 ) ) ); } TEST( MatchExpressionParserLeafTest, SimpleGTE1 ) { @@ -61,9 +61,9 @@ namespace mongo { StatusWithMatchExpression result = MatchExpressionParser::parse( query ); ASSERT_TRUE( result.isOK() ); - ASSERT( !result.getValue()->matches( BSON( "x" << 1 ) ) ); - ASSERT( result.getValue()->matches( BSON( "x" << 2 ) ) ); - ASSERT( result.getValue()->matches( BSON( "x" << 3 ) ) ); + ASSERT( !result.getValue()->matchesBSON( BSON( "x" << 1 ) ) ); + ASSERT( result.getValue()->matchesBSON( BSON( "x" << 2 ) ) ); + ASSERT( result.getValue()->matchesBSON( BSON( "x" << 3 ) ) ); } TEST( MatchExpressionParserLeafTest, SimpleLTE1 ) { @@ -71,9 +71,9 @@ namespace mongo { StatusWithMatchExpression result = MatchExpressionParser::parse( query ); ASSERT_TRUE( result.isOK() ); - ASSERT( result.getValue()->matches( BSON( "x" << 1 ) ) ); - ASSERT( result.getValue()->matches( BSON( "x" << 2 ) ) ); - ASSERT( !result.getValue()->matches( BSON( "x" << 3 ) ) ); + ASSERT( result.getValue()->matchesBSON( BSON( "x" << 1 ) ) ); + ASSERT( result.getValue()->matchesBSON( BSON( "x" << 2 ) ) ); + ASSERT( !result.getValue()->matchesBSON( BSON( "x" << 3 ) ) ); } TEST( MatchExpressionParserLeafTest, SimpleNE1 ) { @@ -81,9 +81,9 @@ namespace mongo { StatusWithMatchExpression result = MatchExpressionParser::parse( query ); ASSERT_TRUE( result.isOK() ); - ASSERT( result.getValue()->matches( BSON( "x" << 1 ) ) ); - ASSERT( !result.getValue()->matches( BSON( "x" << 2 ) ) ); - ASSERT( result.getValue()->matches( BSON( "x" << 3 ) ) ); + ASSERT( result.getValue()->matchesBSON( BSON( "x" << 1 ) ) ); + ASSERT( !result.getValue()->matchesBSON( BSON( "x" << 2 ) ) ); + ASSERT( result.getValue()->matchesBSON( BSON( "x" << 3 ) ) ); } TEST( MatchExpressionParserLeafTest, SimpleModBad1 ) { @@ -117,9 +117,9 @@ namespace mongo { StatusWithMatchExpression result = MatchExpressionParser::parse( query ); ASSERT_TRUE( result.isOK() ); - ASSERT( result.getValue()->matches( BSON( "x" << 5 ) ) ); - ASSERT( !result.getValue()->matches( BSON( "x" << 4 ) ) ); - ASSERT( result.getValue()->matches( BSON( "x" << 8 ) ) ); + ASSERT( result.getValue()->matchesBSON( BSON( "x" << 5 ) ) ); + ASSERT( !result.getValue()->matchesBSON( BSON( "x" << 4 ) ) ); + ASSERT( result.getValue()->matchesBSON( BSON( "x" << 8 ) ) ); } TEST( MatchExpressionParserLeafTest, SimpleModNotNumber ) { @@ -127,10 +127,10 @@ namespace mongo { StatusWithMatchExpression result = MatchExpressionParser::parse( query ); ASSERT_TRUE( result.isOK() ); - ASSERT( result.getValue()->matches( BSON( "x" << 2 ) ) ); - ASSERT( result.getValue()->matches( BSON( "x" << 4 ) ) ); - ASSERT( !result.getValue()->matches( BSON( "x" << 5 ) ) ); - ASSERT( !result.getValue()->matches( BSON( "x" << "a" ) ) ); + ASSERT( result.getValue()->matchesBSON( BSON( "x" << 2 ) ) ); + ASSERT( result.getValue()->matchesBSON( BSON( "x" << 4 ) ) ); + ASSERT( !result.getValue()->matchesBSON( BSON( "x" << 5 ) ) ); + ASSERT( !result.getValue()->matchesBSON( BSON( "x" << "a" ) ) ); } @@ -139,9 +139,9 @@ namespace mongo { StatusWithMatchExpression result = MatchExpressionParser::parse( query ); ASSERT_TRUE( result.isOK() ); - ASSERT( !result.getValue()->matches( BSON( "x" << 1 ) ) ); - ASSERT( result.getValue()->matches( BSON( "x" << 2 ) ) ); - ASSERT( result.getValue()->matches( BSON( "x" << 3 ) ) ); + ASSERT( !result.getValue()->matchesBSON( BSON( "x" << 1 ) ) ); + ASSERT( result.getValue()->matchesBSON( BSON( "x" << 2 ) ) ); + ASSERT( result.getValue()->matchesBSON( BSON( "x" << 3 ) ) ); } @@ -195,13 +195,13 @@ namespace mongo { BSONObj notMatch = BSON( "a" << "l" ); BSONObj notMatchRegex = BSONObjBuilder().appendRegex( "a", "B", "" ).obj(); - ASSERT( result.getValue()->matches( matchFirst ) ); - ASSERT( result.getValue()->matches( matchFirstRegex ) ); - ASSERT( result.getValue()->matches( matchSecond ) ); - ASSERT( result.getValue()->matches( matchSecondRegex ) ); - ASSERT( result.getValue()->matches( matchThird ) ); - ASSERT( !result.getValue()->matches( notMatch ) ); - ASSERT( !result.getValue()->matches( notMatchRegex ) ); + ASSERT( result.getValue()->matchesBSON( matchFirst ) ); + ASSERT( result.getValue()->matchesBSON( matchFirstRegex ) ); + ASSERT( result.getValue()->matchesBSON( matchSecond ) ); + ASSERT( result.getValue()->matchesBSON( matchSecondRegex ) ); + ASSERT( result.getValue()->matchesBSON( matchThird ) ); + ASSERT( !result.getValue()->matchesBSON( notMatch ) ); + ASSERT( !result.getValue()->matchesBSON( notMatchRegex ) ); } TEST( MatchExpressionParserLeafTest, SimpleNIN1 ) { @@ -209,9 +209,9 @@ namespace mongo { StatusWithMatchExpression result = MatchExpressionParser::parse( query ); ASSERT_TRUE( result.isOK() ); - ASSERT( result.getValue()->matches( BSON( "x" << 1 ) ) ); - ASSERT( !result.getValue()->matches( BSON( "x" << 2 ) ) ); - ASSERT( !result.getValue()->matches( BSON( "x" << 3 ) ) ); + ASSERT( result.getValue()->matchesBSON( BSON( "x" << 1 ) ) ); + ASSERT( !result.getValue()->matchesBSON( BSON( "x" << 2 ) ) ); + ASSERT( !result.getValue()->matchesBSON( BSON( "x" << 3 ) ) ); } TEST( MatchExpressionParserLeafTest, NINNotArray ) { @@ -228,9 +228,9 @@ namespace mongo { StatusWithMatchExpression result = MatchExpressionParser::parse( query ); ASSERT_TRUE( result.isOK() ); - ASSERT( result.getValue()->matches( BSON( "x" << "abc" ) ) ); - ASSERT( result.getValue()->matches( BSON( "x" << "ABC" ) ) ); - ASSERT( !result.getValue()->matches( BSON( "x" << "AC" ) ) ); + ASSERT( result.getValue()->matchesBSON( BSON( "x" << "abc" ) ) ); + ASSERT( result.getValue()->matchesBSON( BSON( "x" << "ABC" ) ) ); + ASSERT( !result.getValue()->matchesBSON( BSON( "x" << "AC" ) ) ); } TEST( MatchExpressionParserLeafTest, Regex2 ) { @@ -238,9 +238,9 @@ namespace mongo { StatusWithMatchExpression result = MatchExpressionParser::parse( query ); ASSERT_TRUE( result.isOK() ); - ASSERT( result.getValue()->matches( BSON( "x" << "abc" ) ) ); - ASSERT( result.getValue()->matches( BSON( "x" << "ABC" ) ) ); - ASSERT( !result.getValue()->matches( BSON( "x" << "AC" ) ) ); + ASSERT( result.getValue()->matchesBSON( BSON( "x" << "abc" ) ) ); + ASSERT( result.getValue()->matchesBSON( BSON( "x" << "ABC" ) ) ); + ASSERT( !result.getValue()->matchesBSON( BSON( "x" << "AC" ) ) ); } TEST( MatchExpressionParserLeafTest, RegexBad ) { @@ -274,8 +274,8 @@ namespace mongo { StatusWithMatchExpression result = MatchExpressionParser::parse( query ); ASSERT_TRUE( result.isOK() ); - ASSERT( result.getValue()->matches( BSON( "x" << "abc" ) ) ); - ASSERT( !result.getValue()->matches( BSON( "y" << "AC" ) ) ); + ASSERT( result.getValue()->matchesBSON( BSON( "x" << "abc" ) ) ); + ASSERT( !result.getValue()->matchesBSON( BSON( "y" << "AC" ) ) ); } TEST( MatchExpressionParserLeafTest, ExistsNO1 ) { @@ -285,8 +285,8 @@ namespace mongo { StatusWithMatchExpression result = MatchExpressionParser::parse( query ); ASSERT_TRUE( result.isOK() ); - ASSERT( !result.getValue()->matches( BSON( "x" << "abc" ) ) ); - ASSERT( result.getValue()->matches( BSON( "y" << "AC" ) ) ); + ASSERT( !result.getValue()->matchesBSON( BSON( "x" << "abc" ) ) ); + ASSERT( result.getValue()->matchesBSON( BSON( "y" << "AC" ) ) ); } TEST( MatchExpressionParserLeafTest, Type1 ) { @@ -294,8 +294,8 @@ namespace mongo { StatusWithMatchExpression result = MatchExpressionParser::parse( query ); ASSERT_TRUE( result.isOK() ); - ASSERT( result.getValue()->matches( BSON( "x" << "abc" ) ) ); - ASSERT( !result.getValue()->matches( BSON( "x" << 5 ) ) ); + ASSERT( result.getValue()->matchesBSON( BSON( "x" << "abc" ) ) ); + ASSERT( !result.getValue()->matchesBSON( BSON( "x" << 5 ) ) ); } TEST( MatchExpressionParserLeafTest, Type2 ) { @@ -303,8 +303,8 @@ namespace mongo { StatusWithMatchExpression result = MatchExpressionParser::parse( query ); ASSERT_TRUE( result.isOK() ); - ASSERT( result.getValue()->matches( BSON( "x" << 5.3 ) ) ); - ASSERT( !result.getValue()->matches( BSON( "x" << 5 ) ) ); + ASSERT( result.getValue()->matchesBSON( BSON( "x" << 5.3 ) ) ); + ASSERT( !result.getValue()->matchesBSON( BSON( "x" << 5 ) ) ); } TEST( MatchExpressionParserLeafTest, TypeDoubleOperator ) { @@ -312,8 +312,8 @@ namespace mongo { StatusWithMatchExpression result = MatchExpressionParser::parse( query ); ASSERT_TRUE( result.isOK() ); - ASSERT( !result.getValue()->matches( BSON( "x" << 5.3 ) ) ); - ASSERT( !result.getValue()->matches( BSON( "x" << 5 ) ) ); + ASSERT( !result.getValue()->matchesBSON( BSON( "x" << 5.3 ) ) ); + ASSERT( !result.getValue()->matchesBSON( BSON( "x" << 5 ) ) ); } TEST( MatchExpressionParserLeafTest, TypeNull ) { @@ -321,11 +321,11 @@ namespace mongo { StatusWithMatchExpression result = MatchExpressionParser::parse( query ); ASSERT_TRUE( result.isOK() ); - ASSERT( !result.getValue()->matches( BSONObj() ) ); - ASSERT( !result.getValue()->matches( BSON( "x" << 5 ) ) ); + ASSERT( !result.getValue()->matchesBSON( BSONObj() ) ); + ASSERT( !result.getValue()->matchesBSON( BSON( "x" << 5 ) ) ); BSONObjBuilder b; b.appendNull( "x" ); - ASSERT( result.getValue()->matches( b.obj() ) ); + ASSERT( result.getValue()->matchesBSON( b.obj() ) ); } TEST( MatchExpressionParserLeafTest, TypeBadType ) { @@ -335,8 +335,8 @@ namespace mongo { StatusWithMatchExpression result = MatchExpressionParser::parse( query ); ASSERT_TRUE( result.isOK() ); - ASSERT( !result.getValue()->matches( BSON( "x" << 5.3 ) ) ); - ASSERT( !result.getValue()->matches( BSON( "x" << 5 ) ) ); + ASSERT( !result.getValue()->matchesBSON( BSON( "x" << 5.3 ) ) ); + ASSERT( !result.getValue()->matchesBSON( BSON( "x" << 5 ) ) ); } TEST( MatchExpressionParserLeafTest, TypeBad ) { diff --git a/src/mongo/db/matcher/expression_parser_test.cpp b/src/mongo/db/matcher/expression_parser_test.cpp index 12d9ce553c2..28e53aef3a7 100644 --- a/src/mongo/db/matcher/expression_parser_test.cpp +++ b/src/mongo/db/matcher/expression_parser_test.cpp @@ -32,8 +32,8 @@ namespace mongo { StatusWithMatchExpression result = MatchExpressionParser::parse( query ); ASSERT_TRUE( result.isOK() ); - ASSERT( result.getValue()->matches( BSON( "x" << 2 ) ) ); - ASSERT( !result.getValue()->matches( BSON( "x" << 3 ) ) ); + ASSERT( result.getValue()->matchesBSON( BSON( "x" << 2 ) ) ); + ASSERT( !result.getValue()->matchesBSON( BSON( "x" << 3 ) ) ); } TEST( MatchExpressionParserTest, Multiple1 ) { @@ -41,11 +41,11 @@ namespace mongo { StatusWithMatchExpression result = MatchExpressionParser::parse( query ); ASSERT_TRUE( result.isOK() ); - ASSERT( result.getValue()->matches( BSON( "x" << 5 << "y" << 7 ) ) ); - ASSERT( result.getValue()->matches( BSON( "x" << 5 << "y" << 6 ) ) ); - ASSERT( !result.getValue()->matches( BSON( "x" << 6 << "y" << 7 ) ) ); - ASSERT( !result.getValue()->matches( BSON( "x" << 5 << "y" << 9 ) ) ); - ASSERT( !result.getValue()->matches( BSON( "x" << 5 << "y" << 4 ) ) ); + ASSERT( result.getValue()->matchesBSON( BSON( "x" << 5 << "y" << 7 ) ) ); + ASSERT( result.getValue()->matchesBSON( BSON( "x" << 5 << "y" << 6 ) ) ); + ASSERT( !result.getValue()->matchesBSON( BSON( "x" << 6 << "y" << 7 ) ) ); + ASSERT( !result.getValue()->matchesBSON( BSON( "x" << 5 << "y" << 9 ) ) ); + ASSERT( !result.getValue()->matchesBSON( BSON( "x" << 5 << "y" << 4 ) ) ); } TEST( AtomicMatchExpressionTest, Simple1 ) { diff --git a/src/mongo/db/matcher/expression_parser_tree_test.cpp b/src/mongo/db/matcher/expression_parser_tree_test.cpp index 65e3e1af055..fc0fe1a53c5 100644 --- a/src/mongo/db/matcher/expression_parser_tree_test.cpp +++ b/src/mongo/db/matcher/expression_parser_tree_test.cpp @@ -33,10 +33,10 @@ namespace mongo { StatusWithMatchExpression result = MatchExpressionParser::parse( query ); ASSERT_TRUE( result.isOK() ); - ASSERT( result.getValue()->matches( BSON( "x" << 1 ) ) ); - ASSERT( result.getValue()->matches( BSON( "y" << 2 ) ) ); - ASSERT( !result.getValue()->matches( BSON( "x" << 3 ) ) ); - ASSERT( !result.getValue()->matches( BSON( "y" << 1 ) ) ); + ASSERT( result.getValue()->matchesBSON( BSON( "x" << 1 ) ) ); + ASSERT( result.getValue()->matchesBSON( BSON( "y" << 2 ) ) ); + ASSERT( !result.getValue()->matchesBSON( BSON( "x" << 3 ) ) ); + ASSERT( !result.getValue()->matchesBSON( BSON( "y" << 1 ) ) ); } TEST( MatchExpressionParserTreeTest, OREmbedded ) { @@ -46,10 +46,10 @@ namespace mongo { StatusWithMatchExpression result = MatchExpressionParser::parse( query2 ); ASSERT_TRUE( result.isOK() ); - ASSERT( result.getValue()->matches( BSON( "x" << 1 ) ) ); - ASSERT( result.getValue()->matches( BSON( "y" << 2 ) ) ); - ASSERT( !result.getValue()->matches( BSON( "x" << 3 ) ) ); - ASSERT( !result.getValue()->matches( BSON( "y" << 1 ) ) ); + ASSERT( result.getValue()->matchesBSON( BSON( "x" << 1 ) ) ); + ASSERT( result.getValue()->matchesBSON( BSON( "y" << 2 ) ) ); + ASSERT( !result.getValue()->matchesBSON( BSON( "x" << 3 ) ) ); + ASSERT( !result.getValue()->matchesBSON( BSON( "y" << 1 ) ) ); } @@ -59,12 +59,12 @@ namespace mongo { StatusWithMatchExpression result = MatchExpressionParser::parse( query ); ASSERT_TRUE( result.isOK() ); - ASSERT( !result.getValue()->matches( BSON( "x" << 1 ) ) ); - ASSERT( !result.getValue()->matches( BSON( "y" << 2 ) ) ); - ASSERT( !result.getValue()->matches( BSON( "x" << 3 ) ) ); - ASSERT( !result.getValue()->matches( BSON( "y" << 1 ) ) ); - ASSERT( result.getValue()->matches( BSON( "x" << 1 << "y" << 2 ) ) ); - ASSERT( !result.getValue()->matches( BSON( "x" << 2 << "y" << 2 ) ) ); + ASSERT( !result.getValue()->matchesBSON( BSON( "x" << 1 ) ) ); + ASSERT( !result.getValue()->matchesBSON( BSON( "y" << 2 ) ) ); + ASSERT( !result.getValue()->matchesBSON( BSON( "x" << 3 ) ) ); + ASSERT( !result.getValue()->matchesBSON( BSON( "y" << 1 ) ) ); + ASSERT( result.getValue()->matchesBSON( BSON( "x" << 1 << "y" << 2 ) ) ); + ASSERT( !result.getValue()->matchesBSON( BSON( "x" << 2 << "y" << 2 ) ) ); } TEST( MatchExpressionParserTreeTest, NOREmbedded ) { @@ -73,10 +73,10 @@ namespace mongo { StatusWithMatchExpression result = MatchExpressionParser::parse( query ); ASSERT_TRUE( result.isOK() ); - ASSERT( !result.getValue()->matches( BSON( "x" << 1 ) ) ); - ASSERT( !result.getValue()->matches( BSON( "y" << 2 ) ) ); - ASSERT( result.getValue()->matches( BSON( "x" << 3 ) ) ); - ASSERT( result.getValue()->matches( BSON( "y" << 1 ) ) ); + ASSERT( !result.getValue()->matchesBSON( BSON( "x" << 1 ) ) ); + ASSERT( !result.getValue()->matchesBSON( BSON( "y" << 2 ) ) ); + ASSERT( result.getValue()->matchesBSON( BSON( "x" << 3 ) ) ); + ASSERT( result.getValue()->matchesBSON( BSON( "y" << 1 ) ) ); } TEST( MatchExpressionParserTreeTest, NOT1 ) { @@ -84,8 +84,8 @@ namespace mongo { StatusWithMatchExpression result = MatchExpressionParser::parse( query ); ASSERT_TRUE( result.isOK() ); - ASSERT( result.getValue()->matches( BSON( "x" << 2 ) ) ); - ASSERT( !result.getValue()->matches( BSON( "x" << 8 ) ) ); + ASSERT( result.getValue()->matchesBSON( BSON( "x" << 2 ) ) ); + ASSERT( !result.getValue()->matchesBSON( BSON( "x" << 8 ) ) ); } TEST( MatchExpressionParserLeafTest, NotRegex1 ) { @@ -95,9 +95,9 @@ namespace mongo { StatusWithMatchExpression result = MatchExpressionParser::parse( query ); ASSERT_TRUE( result.isOK() ); - ASSERT( !result.getValue()->matches( BSON( "x" << "abc" ) ) ); - ASSERT( !result.getValue()->matches( BSON( "x" << "ABC" ) ) ); - ASSERT( result.getValue()->matches( BSON( "x" << "AC" ) ) ); + ASSERT( !result.getValue()->matchesBSON( BSON( "x" << "abc" ) ) ); + ASSERT( !result.getValue()->matchesBSON( BSON( "x" << "ABC" ) ) ); + ASSERT( result.getValue()->matchesBSON( BSON( "x" << "AC" ) ) ); } } diff --git a/src/mongo/db/matcher/expression_test.cpp b/src/mongo/db/matcher/expression_test.cpp index 81ab018fbfb..5fbd80c195f 100644 --- a/src/mongo/db/matcher/expression_test.cpp +++ b/src/mongo/db/matcher/expression_test.cpp @@ -38,16 +38,16 @@ namespace mongo { EqualityMatchExpression e; e.init( "x", temp["x"] ); - ASSERT_TRUE( e.matches( fromjson( "{ x : 5 }" ) ) ); - ASSERT_TRUE( e.matches( fromjson( "{ x : [5] }" ) ) ); - ASSERT_TRUE( e.matches( fromjson( "{ x : [1,5] }" ) ) ); - ASSERT_TRUE( e.matches( fromjson( "{ x : [1,5,2] }" ) ) ); - ASSERT_TRUE( e.matches( fromjson( "{ x : [5,2] }" ) ) ); - - ASSERT_FALSE( e.matches( fromjson( "{ x : null }" ) ) ); - ASSERT_FALSE( e.matches( fromjson( "{ x : 6 }" ) ) ); - ASSERT_FALSE( e.matches( fromjson( "{ x : [4,2] }" ) ) ); - ASSERT_FALSE( e.matches( fromjson( "{ x : [[5]] }" ) ) ); + ASSERT_TRUE( e.matchesBSON( fromjson( "{ x : 5 }" ) ) ); + ASSERT_TRUE( e.matchesBSON( fromjson( "{ x : [5] }" ) ) ); + ASSERT_TRUE( e.matchesBSON( fromjson( "{ x : [1,5] }" ) ) ); + ASSERT_TRUE( e.matchesBSON( fromjson( "{ x : [1,5,2] }" ) ) ); + ASSERT_TRUE( e.matchesBSON( fromjson( "{ x : [5,2] }" ) ) ); + + ASSERT_FALSE( e.matchesBSON( fromjson( "{ x : null }" ) ) ); + ASSERT_FALSE( e.matchesBSON( fromjson( "{ x : 6 }" ) ) ); + ASSERT_FALSE( e.matchesBSON( fromjson( "{ x : [4,2] }" ) ) ); + ASSERT_FALSE( e.matchesBSON( fromjson( "{ x : [[5]] }" ) ) ); } TEST( LeafMatchExpressionTest, Comp1 ) { @@ -56,37 +56,37 @@ namespace mongo { { LTEMatchExpression e; e.init( "x", temp["x"] ); - ASSERT_TRUE( e.matches( fromjson( "{ x : 5 }" ) ) ); - ASSERT_TRUE( e.matches( fromjson( "{ x : 4 }" ) ) ); - ASSERT_FALSE( e.matches( fromjson( "{ x : 6 }" ) ) ); - ASSERT_FALSE( e.matches( fromjson( "{ x : 'eliot' }" ) ) ); + ASSERT_TRUE( e.matchesBSON( fromjson( "{ x : 5 }" ) ) ); + ASSERT_TRUE( e.matchesBSON( fromjson( "{ x : 4 }" ) ) ); + ASSERT_FALSE( e.matchesBSON( fromjson( "{ x : 6 }" ) ) ); + ASSERT_FALSE( e.matchesBSON( fromjson( "{ x : 'eliot' }" ) ) ); } { LTMatchExpression e; e.init( "x", temp["x"] ); - ASSERT_FALSE( e.matches( fromjson( "{ x : 5 }" ) ) ); - ASSERT_TRUE( e.matches( fromjson( "{ x : 4 }" ) ) ); - ASSERT_FALSE( e.matches( fromjson( "{ x : 6 }" ) ) ); - ASSERT_FALSE( e.matches( fromjson( "{ x : 'eliot' }" ) ) ); + ASSERT_FALSE( e.matchesBSON( fromjson( "{ x : 5 }" ) ) ); + ASSERT_TRUE( e.matchesBSON( fromjson( "{ x : 4 }" ) ) ); + ASSERT_FALSE( e.matchesBSON( fromjson( "{ x : 6 }" ) ) ); + ASSERT_FALSE( e.matchesBSON( fromjson( "{ x : 'eliot' }" ) ) ); } { GTEMatchExpression e; e.init( "x", temp["x"] ); - ASSERT_TRUE( e.matches( fromjson( "{ x : 5 }" ) ) ); - ASSERT_FALSE( e.matches( fromjson( "{ x : 4 }" ) ) ); - ASSERT_TRUE( e.matches( fromjson( "{ x : 6 }" ) ) ); - ASSERT_FALSE( e.matches( fromjson( "{ x : 'eliot' }" ) ) ); + ASSERT_TRUE( e.matchesBSON( fromjson( "{ x : 5 }" ) ) ); + ASSERT_FALSE( e.matchesBSON( fromjson( "{ x : 4 }" ) ) ); + ASSERT_TRUE( e.matchesBSON( fromjson( "{ x : 6 }" ) ) ); + ASSERT_FALSE( e.matchesBSON( fromjson( "{ x : 'eliot' }" ) ) ); } { GTMatchExpression e; e.init( "x", temp["x"] ); - ASSERT_FALSE( e.matches( fromjson( "{ x : 5 }" ) ) ); - ASSERT_FALSE( e.matches( fromjson( "{ x : 4 }" ) ) ); - ASSERT_TRUE( e.matches( fromjson( "{ x : 6 }" ) ) ); - ASSERT_FALSE( e.matches( fromjson( "{ x : 'eliot' }" ) ) ); + ASSERT_FALSE( e.matchesBSON( fromjson( "{ x : 5 }" ) ) ); + ASSERT_FALSE( e.matchesBSON( fromjson( "{ x : 4 }" ) ) ); + ASSERT_TRUE( e.matchesBSON( fromjson( "{ x : 6 }" ) ) ); + ASSERT_FALSE( e.matchesBSON( fromjson( "{ x : 'eliot' }" ) ) ); } diff --git a/src/mongo/db/matcher/expression_tree.cpp b/src/mongo/db/matcher/expression_tree.cpp index 94d54ec07ab..7875075e1c1 100644 --- a/src/mongo/db/matcher/expression_tree.cpp +++ b/src/mongo/db/matcher/expression_tree.cpp @@ -61,7 +61,7 @@ namespace mongo { // ----- - bool AndMatchExpression::matches( const BSONObj& doc, MatchDetails* details ) const { + bool AndMatchExpression::matches( const MatchableDocument* doc, MatchDetails* details ) const { for ( size_t i = 0; i < numChildren(); i++ ) { if ( !getChild(i)->matches( doc, details ) ) { if ( details ) @@ -90,7 +90,7 @@ namespace mongo { // ----- - bool OrMatchExpression::matches( const BSONObj& doc, MatchDetails* details ) const { + bool OrMatchExpression::matches( const MatchableDocument* doc, MatchDetails* details ) const { for ( size_t i = 0; i < numChildren(); i++ ) { if ( getChild(i)->matches( doc, NULL ) ) { return true; @@ -117,7 +117,7 @@ namespace mongo { // ---- - bool NorMatchExpression::matches( const BSONObj& doc, MatchDetails* details ) const { + bool NorMatchExpression::matches( const MatchableDocument* doc, MatchDetails* details ) const { for ( size_t i = 0; i < numChildren(); i++ ) { if ( getChild(i)->matches( doc, NULL ) ) { return false; diff --git a/src/mongo/db/matcher/expression_tree.h b/src/mongo/db/matcher/expression_tree.h index 8c826f78769..b2c9f2c252f 100644 --- a/src/mongo/db/matcher/expression_tree.h +++ b/src/mongo/db/matcher/expression_tree.h @@ -61,7 +61,7 @@ namespace mongo { AndMatchExpression() : ListOfMatchExpression( AND ){} virtual ~AndMatchExpression(){} - virtual bool matches( const BSONObj& doc, MatchDetails* details = 0 ) const; + virtual bool matches( const MatchableDocument* doc, MatchDetails* details = 0 ) const; virtual bool matchesSingleElement( const BSONElement& e ) const; virtual void debugString( StringBuilder& debug, int level = 0 ) const; @@ -72,7 +72,7 @@ namespace mongo { OrMatchExpression() : ListOfMatchExpression( OR ){} virtual ~OrMatchExpression(){} - virtual bool matches( const BSONObj& doc, MatchDetails* details = 0 ) const; + virtual bool matches( const MatchableDocument* doc, MatchDetails* details = 0 ) const; virtual bool matchesSingleElement( const BSONElement& e ) const; virtual void debugString( StringBuilder& debug, int level = 0 ) const; @@ -83,7 +83,7 @@ namespace mongo { NorMatchExpression() : ListOfMatchExpression( NOR ){} virtual ~NorMatchExpression(){} - virtual bool matches( const BSONObj& doc, MatchDetails* details = 0 ) const; + virtual bool matches( const MatchableDocument* doc, MatchDetails* details = 0 ) const; virtual bool matchesSingleElement( const BSONElement& e ) const; virtual void debugString( StringBuilder& debug, int level = 0 ) const; @@ -100,7 +100,7 @@ namespace mongo { return Status::OK(); } - virtual bool matches( const BSONObj& doc, MatchDetails* details = 0 ) const { + virtual bool matches( const MatchableDocument* doc, MatchDetails* details = 0 ) const { return !_exp->matches( doc, NULL ); } diff --git a/src/mongo/db/matcher/expression_tree_test.cpp b/src/mongo/db/matcher/expression_tree_test.cpp index e2032da14fd..3b2d57129aa 100644 --- a/src/mongo/db/matcher/expression_tree_test.cpp +++ b/src/mongo/db/matcher/expression_tree_test.cpp @@ -32,8 +32,8 @@ namespace mongo { ASSERT( lt->init( "a", baseOperand[ "$lt" ] ).isOK() ); NotMatchExpression notOp; ASSERT( notOp.init( lt.release() ).isOK() ); - ASSERT( notOp.matches( BSON( "a" << 6 ), NULL ) ); - ASSERT( !notOp.matches( BSON( "a" << 4 ), NULL ) ); + ASSERT( notOp.matchesBSON( BSON( "a" << 6 ), NULL ) ); + ASSERT( !notOp.matchesBSON( BSON( "a" << 4 ), NULL ) ); } TEST( NotMatchExpression, MatchesArray ) { @@ -42,10 +42,10 @@ namespace mongo { ASSERT( lt->init( "a", baseOperand[ "$lt" ] ).isOK() ); NotMatchExpression notOp; ASSERT( notOp.init( lt.release() ).isOK() ); - ASSERT( notOp.matches( BSON( "a" << BSON_ARRAY( 6 ) ), NULL ) ); - ASSERT( !notOp.matches( BSON( "a" << BSON_ARRAY( 4 ) ), NULL ) ); + ASSERT( notOp.matchesBSON( BSON( "a" << BSON_ARRAY( 6 ) ), NULL ) ); + ASSERT( !notOp.matchesBSON( BSON( "a" << BSON_ARRAY( 4 ) ), NULL ) ); // All array elements must match. - ASSERT( !notOp.matches( BSON( "a" << BSON_ARRAY( 4 << 5 << 6 ) ), NULL ) ); + ASSERT( !notOp.matchesBSON( BSON( "a" << BSON_ARRAY( 4 << 5 << 6 ) ), NULL ) ); } TEST( NotMatchExpression, ElemMatchKey ) { @@ -56,11 +56,11 @@ namespace mongo { ASSERT( notOp.init( lt.release() ).isOK() ); MatchDetails details; details.requestElemMatchKey(); - ASSERT( !notOp.matches( BSON( "a" << BSON_ARRAY( 1 ) ), &details ) ); + ASSERT( !notOp.matchesBSON( BSON( "a" << BSON_ARRAY( 1 ) ), &details ) ); ASSERT( !details.hasElemMatchKey() ); - ASSERT( notOp.matches( BSON( "a" << 6 ), &details ) ); + ASSERT( notOp.matchesBSON( BSON( "a" << 6 ), &details ) ); ASSERT( !details.hasElemMatchKey() ); - ASSERT( notOp.matches( BSON( "a" << BSON_ARRAY( 6 ) ), &details ) ); + ASSERT( notOp.matchesBSON( BSON( "a" << BSON_ARRAY( 6 ) ), &details ) ); // elemMatchKey is not implemented for negative match operators. ASSERT( !details.hasElemMatchKey() ); } @@ -96,7 +96,7 @@ namespace mongo { TEST( AndOp, NoClauses ) { AndMatchExpression andMatchExpression; - ASSERT( andMatchExpression.matches( BSONObj(), NULL ) ); + ASSERT( andMatchExpression.matchesBSON( BSONObj(), NULL ) ); } TEST( AndOp, MatchesElementThreeClauses ) { @@ -119,10 +119,10 @@ namespace mongo { andOp.add( sub2.release() ); andOp.add( sub3.release() ); - ASSERT( andOp.matches( match ) ); - ASSERT( !andOp.matches( notMatch1 ) ); - ASSERT( !andOp.matches( notMatch2 ) ); - ASSERT( !andOp.matches( notMatch3 ) ); + ASSERT( andOp.matchesBSON( match ) ); + ASSERT( !andOp.matchesBSON( notMatch1 ) ); + ASSERT( !andOp.matchesBSON( notMatch2 ) ); + ASSERT( !andOp.matchesBSON( notMatch3 ) ); } TEST( AndOp, MatchesSingleClause ) { @@ -133,10 +133,10 @@ namespace mongo { AndMatchExpression andOp; andOp.add( ne.release() ); - ASSERT( andOp.matches( BSON( "a" << 4 ), NULL ) ); - ASSERT( andOp.matches( BSON( "a" << BSON_ARRAY( 4 << 6 ) ), NULL ) ); - ASSERT( !andOp.matches( BSON( "a" << 5 ), NULL ) ); - ASSERT( !andOp.matches( BSON( "a" << BSON_ARRAY( 4 << 5 ) ), NULL ) ); + ASSERT( andOp.matchesBSON( BSON( "a" << 4 ), NULL ) ); + ASSERT( andOp.matchesBSON( BSON( "a" << BSON_ARRAY( 4 << 6 ) ), NULL ) ); + ASSERT( !andOp.matchesBSON( BSON( "a" << 5 ), NULL ) ); + ASSERT( !andOp.matchesBSON( BSON( "a" << BSON_ARRAY( 4 << 5 ) ), NULL ) ); } TEST( AndOp, MatchesThreeClauses ) { @@ -158,11 +158,11 @@ namespace mongo { andOp.add( sub2.release() ); andOp.add( sub3.release() ); - ASSERT( andOp.matches( BSON( "a" << 5 << "b" << 6 ), NULL ) ); - ASSERT( !andOp.matches( BSON( "a" << 5 ), NULL ) ); - ASSERT( !andOp.matches( BSON( "b" << 6 ), NULL ) ); - ASSERT( !andOp.matches( BSON( "a" << 1 << "b" << 6 ), NULL ) ); - ASSERT( !andOp.matches( BSON( "a" << 10 << "b" << 6 ), NULL ) ); + ASSERT( andOp.matchesBSON( BSON( "a" << 5 << "b" << 6 ), NULL ) ); + ASSERT( !andOp.matchesBSON( BSON( "a" << 5 ), NULL ) ); + ASSERT( !andOp.matchesBSON( BSON( "b" << 6 ), NULL ) ); + ASSERT( !andOp.matchesBSON( BSON( "a" << 1 << "b" << 6 ), NULL ) ); + ASSERT( !andOp.matchesBSON( BSON( "a" << 10 << "b" << 6 ), NULL ) ); } TEST( AndOp, ElemMatchKey ) { @@ -181,11 +181,11 @@ namespace mongo { MatchDetails details; details.requestElemMatchKey(); - ASSERT( !andOp.matches( BSON( "a" << BSON_ARRAY( 1 ) ), &details ) ); + ASSERT( !andOp.matchesBSON( BSON( "a" << BSON_ARRAY( 1 ) ), &details ) ); ASSERT( !details.hasElemMatchKey() ); - ASSERT( !andOp.matches( BSON( "b" << BSON_ARRAY( 2 ) ), &details ) ); + ASSERT( !andOp.matchesBSON( BSON( "b" << BSON_ARRAY( 2 ) ), &details ) ); ASSERT( !details.hasElemMatchKey() ); - ASSERT( andOp.matches( BSON( "a" << BSON_ARRAY( 1 ) << "b" << BSON_ARRAY( 1 << 2 ) ), + ASSERT( andOp.matchesBSON( BSON( "a" << BSON_ARRAY( 1 ) << "b" << BSON_ARRAY( 1 << 2 ) ), &details ) ); ASSERT( details.hasElemMatchKey() ); // The elem match key for the second $and clause is recorded. @@ -259,7 +259,7 @@ namespace mongo { TEST( OrOp, NoClauses ) { OrMatchExpression orOp; - ASSERT( !orOp.matches( BSONObj(), NULL ) ); + ASSERT( !orOp.matchesBSON( BSONObj(), NULL ) ); } /* TEST( OrOp, MatchesElementThreeClauses ) { @@ -296,10 +296,10 @@ namespace mongo { OrMatchExpression orOp; orOp.add( ne.release() ); - ASSERT( orOp.matches( BSON( "a" << 4 ), NULL ) ); - ASSERT( orOp.matches( BSON( "a" << BSON_ARRAY( 4 << 6 ) ), NULL ) ); - ASSERT( !orOp.matches( BSON( "a" << 5 ), NULL ) ); - ASSERT( !orOp.matches( BSON( "a" << BSON_ARRAY( 4 << 5 ) ), NULL ) ); + ASSERT( orOp.matchesBSON( BSON( "a" << 4 ), NULL ) ); + ASSERT( orOp.matchesBSON( BSON( "a" << BSON_ARRAY( 4 << 6 ) ), NULL ) ); + ASSERT( !orOp.matchesBSON( BSON( "a" << 5 ), NULL ) ); + ASSERT( !orOp.matchesBSON( BSON( "a" << BSON_ARRAY( 4 << 5 ) ), NULL ) ); } TEST( OrOp, MatchesThreeClauses ) { @@ -318,13 +318,13 @@ namespace mongo { orOp.add( sub2.release() ); orOp.add( sub3.release() ); - ASSERT( orOp.matches( BSON( "a" << -1 ), NULL ) ); - ASSERT( orOp.matches( BSON( "a" << 11 ), NULL ) ); - ASSERT( !orOp.matches( BSON( "a" << 5 ), NULL ) ); - ASSERT( orOp.matches( BSON( "b" << 100 ), NULL ) ); - ASSERT( !orOp.matches( BSON( "b" << 101 ), NULL ) ); - ASSERT( !orOp.matches( BSONObj(), NULL ) ); - ASSERT( orOp.matches( BSON( "a" << 11 << "b" << 100 ), NULL ) ); + ASSERT( orOp.matchesBSON( BSON( "a" << -1 ), NULL ) ); + ASSERT( orOp.matchesBSON( BSON( "a" << 11 ), NULL ) ); + ASSERT( !orOp.matchesBSON( BSON( "a" << 5 ), NULL ) ); + ASSERT( orOp.matchesBSON( BSON( "b" << 100 ), NULL ) ); + ASSERT( !orOp.matchesBSON( BSON( "b" << 101 ), NULL ) ); + ASSERT( !orOp.matchesBSON( BSONObj(), NULL ) ); + ASSERT( orOp.matchesBSON( BSON( "a" << 11 << "b" << 100 ), NULL ) ); } TEST( OrOp, ElemMatchKey ) { @@ -341,12 +341,12 @@ namespace mongo { MatchDetails details; details.requestElemMatchKey(); - ASSERT( !orOp.matches( BSONObj(), &details ) ); + ASSERT( !orOp.matchesBSON( BSONObj(), &details ) ); ASSERT( !details.hasElemMatchKey() ); - ASSERT( !orOp.matches( BSON( "a" << BSON_ARRAY( 10 ) << "b" << BSON_ARRAY( 10 ) ), + ASSERT( !orOp.matchesBSON( BSON( "a" << BSON_ARRAY( 10 ) << "b" << BSON_ARRAY( 10 ) ), &details ) ); ASSERT( !details.hasElemMatchKey() ); - ASSERT( orOp.matches( BSON( "a" << BSON_ARRAY( 1 ) << "b" << BSON_ARRAY( 1 << 2 ) ), + ASSERT( orOp.matchesBSON( BSON( "a" << BSON_ARRAY( 1 ) << "b" << BSON_ARRAY( 1 << 2 ) ), &details ) ); // The elem match key feature is not implemented for $or. ASSERT( !details.hasElemMatchKey() ); @@ -419,7 +419,7 @@ namespace mongo { TEST( NorOp, NoClauses ) { NorMatchExpression norOp; - ASSERT( norOp.matches( BSONObj(), NULL ) ); + ASSERT( norOp.matchesBSON( BSONObj(), NULL ) ); } /* TEST( NorOp, MatchesElementThreeClauses ) { @@ -457,10 +457,10 @@ namespace mongo { NorMatchExpression norOp; norOp.add( ne.release() ); - ASSERT( !norOp.matches( BSON( "a" << 4 ), NULL ) ); - ASSERT( !norOp.matches( BSON( "a" << BSON_ARRAY( 4 << 6 ) ), NULL ) ); - ASSERT( norOp.matches( BSON( "a" << 5 ), NULL ) ); - ASSERT( norOp.matches( BSON( "a" << BSON_ARRAY( 4 << 5 ) ), NULL ) ); + ASSERT( !norOp.matchesBSON( BSON( "a" << 4 ), NULL ) ); + ASSERT( !norOp.matchesBSON( BSON( "a" << BSON_ARRAY( 4 << 6 ) ), NULL ) ); + ASSERT( norOp.matchesBSON( BSON( "a" << 5 ), NULL ) ); + ASSERT( norOp.matchesBSON( BSON( "a" << BSON_ARRAY( 4 << 5 ) ), NULL ) ); } TEST( NorOp, MatchesThreeClauses ) { @@ -480,13 +480,13 @@ namespace mongo { norOp.add( sub2.release() ); norOp.add( sub3.release() ); - ASSERT( !norOp.matches( BSON( "a" << -1 ), NULL ) ); - ASSERT( !norOp.matches( BSON( "a" << 11 ), NULL ) ); - ASSERT( norOp.matches( BSON( "a" << 5 ), NULL ) ); - ASSERT( !norOp.matches( BSON( "b" << 100 ), NULL ) ); - ASSERT( norOp.matches( BSON( "b" << 101 ), NULL ) ); - ASSERT( norOp.matches( BSONObj(), NULL ) ); - ASSERT( !norOp.matches( BSON( "a" << 11 << "b" << 100 ), NULL ) ); + ASSERT( !norOp.matchesBSON( BSON( "a" << -1 ), NULL ) ); + ASSERT( !norOp.matchesBSON( BSON( "a" << 11 ), NULL ) ); + ASSERT( norOp.matchesBSON( BSON( "a" << 5 ), NULL ) ); + ASSERT( !norOp.matchesBSON( BSON( "b" << 100 ), NULL ) ); + ASSERT( norOp.matchesBSON( BSON( "b" << 101 ), NULL ) ); + ASSERT( norOp.matchesBSON( BSONObj(), NULL ) ); + ASSERT( !norOp.matchesBSON( BSON( "a" << 11 << "b" << 100 ), NULL ) ); } TEST( NorOp, ElemMatchKey ) { @@ -503,12 +503,12 @@ namespace mongo { MatchDetails details; details.requestElemMatchKey(); - ASSERT( !norOp.matches( BSON( "a" << 1 ), &details ) ); + ASSERT( !norOp.matchesBSON( BSON( "a" << 1 ), &details ) ); ASSERT( !details.hasElemMatchKey() ); - ASSERT( !norOp.matches( BSON( "a" << BSON_ARRAY( 1 ) << "b" << BSON_ARRAY( 10 ) ), + ASSERT( !norOp.matchesBSON( BSON( "a" << BSON_ARRAY( 1 ) << "b" << BSON_ARRAY( 10 ) ), &details ) ); ASSERT( !details.hasElemMatchKey() ); - ASSERT( norOp.matches( BSON( "a" << BSON_ARRAY( 3 ) << "b" << BSON_ARRAY( 4 ) ), + ASSERT( norOp.matchesBSON( BSON( "a" << BSON_ARRAY( 3 ) << "b" << BSON_ARRAY( 4 ) ), &details ) ); // The elem match key feature is not implemented for $nor. ASSERT( !details.hasElemMatchKey() ); diff --git a/src/mongo/db/matcher/expression_where.cpp b/src/mongo/db/matcher/expression_where.cpp index 1bfc74107af..532f403a2ce 100644 --- a/src/mongo/db/matcher/expression_where.cpp +++ b/src/mongo/db/matcher/expression_where.cpp @@ -34,7 +34,7 @@ namespace mongo { Status init( const StringData& ns, const StringData& theCode, const BSONObj& scope ); - virtual bool matches( const BSONObj& doc, MatchDetails* details = 0 ) const; + virtual bool matches( const MatchableDocument* doc, MatchDetails* details = 0 ) const; virtual bool matchesSingleElement( const BSONElement& e ) const { return false; @@ -76,8 +76,9 @@ namespace mongo { return Status::OK(); } - bool WhereMatchExpression::matches( const BSONObj& obj, MatchDetails* details ) const { + bool WhereMatchExpression::matches( const MatchableDocument* doc, MatchDetails* details ) const { verify( _func ); + BSONObj obj = doc->toBSON(); if ( ! _userScope.isEmpty() ) { _scope->init( &_userScope ); diff --git a/src/mongo/db/matcher/matchable.cpp b/src/mongo/db/matcher/matchable.cpp new file mode 100644 index 00000000000..53636c8da72 --- /dev/null +++ b/src/mongo/db/matcher/matchable.cpp @@ -0,0 +1,49 @@ +// matchable.h + +/** + * Copyright (C) 2013 10gen Inc. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#include "mongo/pch.h" +#include "mongo/db/jsobj.h" +#include "mongo/db/matcher/expression_internal.h" +#include "mongo/db/matcher/matchable.h" + +namespace mongo { + + MatchableDocument::~MatchableDocument(){ + } + + BSONMatchableDocument::BSONMatchableDocument( const BSONObj& obj ) + : _obj( obj ) { + } + + BSONMatchableDocument::~BSONMatchableDocument() { + } + + + BSONElement BSONMatchableDocument::getFieldDottedOrArray( const FieldRef& path, + int32_t* idxPath, + bool* inArray ) const { + return mongo::getFieldDottedOrArray( _obj, path, idxPath, inArray ); + } + + void BSONMatchableDocument::getFieldsDotted( const StringData& name, + BSONElementSet &ret, + bool expandLastArray ) const { + return _obj.getFieldsDotted( name, ret, expandLastArray ); + } + +} diff --git a/src/mongo/db/matcher/matchable.h b/src/mongo/db/matcher/matchable.h new file mode 100644 index 00000000000..c01f827d8a1 --- /dev/null +++ b/src/mongo/db/matcher/matchable.h @@ -0,0 +1,60 @@ +// matchable.h + +/** + * Copyright (C) 2013 10gen Inc. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#pragma once + +#include "mongo/bson/bsonobj.h" +#include "mongo/db/field_ref.h" + +namespace mongo { + + class MatchableDocument { + public: + virtual ~MatchableDocument(); + + virtual BSONObj toBSON() const = 0; + + virtual BSONElement getFieldDottedOrArray( const FieldRef& path, + int32_t* idxPath, + bool* inArray ) const = 0; + + virtual void getFieldsDotted( const StringData& name, + BSONElementSet &ret, + bool expandLastArray = true ) const = 0; + + }; + + class BSONMatchableDocument : public MatchableDocument { + public: + BSONMatchableDocument( const BSONObj& obj ); + virtual ~BSONMatchableDocument(); + + virtual BSONObj toBSON() const { return _obj; } + + virtual BSONElement getFieldDottedOrArray( const FieldRef& path, + int32_t* idxPath, + bool* inArray ) const; + + virtual void getFieldsDotted( const StringData& name, + BSONElementSet &ret, + bool expandLastArray = true ) const; + + private: + BSONObj _obj; + }; +} diff --git a/src/mongo/db/matcher/matcher.cpp b/src/mongo/db/matcher/matcher.cpp index 2779fe2def2..9e081b39031 100644 --- a/src/mongo/db/matcher/matcher.cpp +++ b/src/mongo/db/matcher/matcher.cpp @@ -25,6 +25,87 @@ namespace mongo { + class IndexKeyMatchableDocument : public MatchableDocument { + public: + IndexKeyMatchableDocument( const BSONObj& pattern, + const BSONObj& doc ) + : _pattern( pattern ), _doc( doc ) { + } + + BSONObj toBSON() const { + // TODO: this isn't quite correct because of dots + // don't think it'll ever be called though + return _doc.replaceFieldNames( _pattern ); + } + + virtual BSONElement getFieldDottedOrArray( const FieldRef& path, + int32_t* idxPath, + bool* inArray ) const; + + virtual void getFieldsDotted( const StringData& name, + BSONElementSet &ret, + bool expandLastArray = true ) const; + + private: + + BSONElement _getElement( const FieldRef& path ) const; + + BSONObj _pattern; + BSONObj _doc; + + }; + + BSONElement IndexKeyMatchableDocument::_getElement( const FieldRef& path ) const { + BSONObjIterator patternIterator( _pattern ); + BSONObjIterator docIterator( _pattern ); + + while ( patternIterator.more() ) { + BSONElement patternElement = patternIterator.next(); + verify( docIterator.more() ); + BSONElement docElement = docIterator.next(); + + if ( path.equalsDottedField( patternElement.fieldName() ) ) { + return docElement; + } + } + + return BSONElement(); + } + + + BSONElement IndexKeyMatchableDocument::getFieldDottedOrArray( const FieldRef& path, + int32_t* idxPath, + bool* inArray ) const { + BSONElement res = _getElement( path ); + if ( !res.eoo() ) { + *idxPath = path.numParts() - 1; + *inArray = false; + } + + return res; + } + + void IndexKeyMatchableDocument::getFieldsDotted( const StringData& name, + BSONElementSet &ret, + bool expandLastArray ) const { + BSONObjIterator patternIterator( _pattern ); + BSONObjIterator docIterator( _pattern ); + + while ( patternIterator.more() ) { + BSONElement patternElement = patternIterator.next(); + verify( docIterator.more() ); + BSONElement docElement = docIterator.next(); + + if ( name == patternElement.fieldName() ) { + ret.insert( docElement ); + } + } + + } + + + // ----------------- + Matcher2::Matcher2( const BSONObj& pattern, bool nested ) : _pattern( pattern ) { @@ -36,7 +117,8 @@ namespace mongo { _expression.reset( result.getValue() ); } - Matcher2::Matcher2( const Matcher2 &docMatcher, const BSONObj &constrainIndexKey ) { + Matcher2::Matcher2( const Matcher2 &docMatcher, const BSONObj &constrainIndexKey ) + : _indexKey( constrainIndexKey ) { MatchExpression* indexExpression = spliceForIndex( constrainIndexKey, docMatcher._expression.get() ); @@ -47,7 +129,15 @@ namespace mongo { bool Matcher2::matches(const BSONObj& doc, MatchDetails* details ) const { if ( !_expression ) return true; - return _expression->matches( doc, details ); + + if ( _indexKey.isEmpty() ) + return _expression->matchesBSON( doc, details ); + + if ( !doc.isEmpty() && doc.firstElement().fieldName()[0] ) + return _expression->matchesBSON( doc, details ); + + IndexKeyMatchableDocument mydoc( _indexKey, doc ); + return _expression->matches( &mydoc, details ); } diff --git a/src/mongo/db/matcher/matcher.h b/src/mongo/db/matcher/matcher.h index 6013b8540db..fb6e6b7c32b 100644 --- a/src/mongo/db/matcher/matcher.h +++ b/src/mongo/db/matcher/matcher.h @@ -67,7 +67,10 @@ namespace mongo { const MatchExpression* full ); private: - const BSONObj _pattern; // this is owned by who created us + BSONObj _pattern; + + BSONObj _indexKey; + boost::scoped_ptr<MatchExpression> _expression; static MatchExpression* _spliceForIndex( const set<std::string>& keys, |