diff options
author | Eliot Horowitz <eliot@10gen.com> | 2013-05-08 10:22:47 -0400 |
---|---|---|
committer | Eliot Horowitz <eliot@10gen.com> | 2013-05-09 10:16:55 -0400 |
commit | f41cc12bf4672528f513fb81f1fab4b4b379ae83 (patch) | |
tree | 706cde2ccbcbbda3d895fd4bca46cc019f50607d | |
parent | 0078fb7d2bc7cb352df42524399276fa51b7b110 (diff) | |
download | mongo-f41cc12bf4672528f513fb81f1fab4b4b379ae83.tar.gz |
SERVER-6400 FieldRef has a equalsDottedField to compare it to a full string
-rw-r--r-- | src/mongo/db/field_ref.cpp | 29 | ||||
-rw-r--r-- | src/mongo/db/field_ref.h | 5 | ||||
-rw-r--r-- | src/mongo/db/field_ref_test.cpp | 18 |
3 files changed, 52 insertions, 0 deletions
diff --git a/src/mongo/db/field_ref.cpp b/src/mongo/db/field_ref.cpp index c3dbc47ed5d..68ab798b320 100644 --- a/src/mongo/db/field_ref.cpp +++ b/src/mongo/db/field_ref.cpp @@ -16,6 +16,7 @@ #include "mongo/db/field_ref.h" +#include "mongo/util/log.h" #include "mongo/util/assert_util.h" namespace mongo { @@ -115,6 +116,34 @@ namespace mongo { return res; } + bool FieldRef::equalsDottedField( const StringData& other ) const { + StringData rest = other; + + + for ( size_t i = 0; i < _size; i++ ) { + + StringData part = getPart( i ); + + if ( !rest.startsWith( part ) ) + return false; + + if ( i == _size - 1 ) + return rest.size() == part.size(); + + // make sure next thing is a dot + if ( rest.size() == part.size() ) + return false; + + if ( rest[part.size()] != '.' ) + return false; + + rest = rest.substr( part.size() + 1 ); + } + + return false; + } + + size_t FieldRef::numReplaced() const { size_t res = 0; for (size_t i = 0; i < _replacements.size(); i++) { diff --git a/src/mongo/db/field_ref.h b/src/mongo/db/field_ref.h index ea7b7dab34d..ecc94bce046 100644 --- a/src/mongo/db/field_ref.h +++ b/src/mongo/db/field_ref.h @@ -88,6 +88,11 @@ namespace mongo { */ size_t numReplaced() const; + /** + * compares the full dotted path represented by this FieldRef to other + */ + bool equalsDottedField( const StringData& other ) const; + private: // Dotted fields are most often not longer than three parts. We use a mixed structure // here that will not require any extra memory allocation when that is the case. And diff --git a/src/mongo/db/field_ref_test.cpp b/src/mongo/db/field_ref_test.cpp index cdf4aa7ad92..5c2b6dda00e 100644 --- a/src/mongo/db/field_ref_test.cpp +++ b/src/mongo/db/field_ref_test.cpp @@ -139,4 +139,22 @@ namespace { ASSERT_EQUALS(fieldRef.numReplaced(), 1U); } + TEST(Equality, Simple1 ) { + FieldRef a; + a.parse( "a.b" ); + ASSERT( a.equalsDottedField( "a.b" ) ); + ASSERT( !a.equalsDottedField( "a" ) ); + ASSERT( !a.equalsDottedField( "b" ) ); + ASSERT( !a.equalsDottedField( "a.b.c" ) ); + } + + TEST(Equality, Simple2 ) { + FieldRef a; + a.parse( "a" ); + ASSERT( !a.equalsDottedField( "a.b" ) ); + ASSERT( a.equalsDottedField( "a" ) ); + ASSERT( !a.equalsDottedField( "b" ) ); + ASSERT( !a.equalsDottedField( "a.b.c" ) ); + } + } // namespace mongo |