From 0c8ac1a52dcf0a93e6e1ab4d701215b2beb6cff6 Mon Sep 17 00:00:00 2001 From: Eliot Horowitz Date: Thu, 28 Feb 2013 16:43:45 -0500 Subject: SERVER-8758 - IndexSet fix for ab vs a --- src/mongo/db/index_set.cpp | 16 ++++++++++++++-- src/mongo/db/index_set.h | 3 +++ src/mongo/db/index_set_test.cpp | 7 +++++++ 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/src/mongo/db/index_set.cpp b/src/mongo/db/index_set.cpp index 4d7cb62a759..65f74ed54ea 100644 --- a/src/mongo/db/index_set.cpp +++ b/src/mongo/db/index_set.cpp @@ -47,16 +47,28 @@ namespace mongo { StringData idx( *i ); - if ( use.startsWith( idx ) ) + if ( _startsWith( use, idx ) ) return true; - if ( idx.startsWith( use ) ) + if ( _startsWith( idx, use ) ) return true; } return false; } + bool IndexPathSet::_startsWith( const StringData& a, const StringData& b ) const { + if ( !a.startsWith( b ) ) + return false; + + // make sure there is a dot or EOL right after + + if ( a.size() == b.size() ) + return true; + + return a[b.size()] == '.'; + } + bool getCanonicalIndexField( const StringData& fullName, string* out ) { // check if fieldName contains ".$" or ".###" substrings (#=digit) and skip them // however do not skip the first field even if it meets these criteria diff --git a/src/mongo/db/index_set.h b/src/mongo/db/index_set.h index 24717750e9a..1ff99780f07 100644 --- a/src/mongo/db/index_set.h +++ b/src/mongo/db/index_set.h @@ -39,6 +39,9 @@ namespace mongo { bool mightBeIndexed( const StringData& path ) const; private: + + bool _startsWith( const StringData& a, const StringData& b ) const; + std::set _canonical; }; diff --git a/src/mongo/db/index_set_test.cpp b/src/mongo/db/index_set_test.cpp index aac5b43550b..28a1d9fe68e 100644 --- a/src/mongo/db/index_set_test.cpp +++ b/src/mongo/db/index_set_test.cpp @@ -33,6 +33,13 @@ namespace mongo { ASSERT_FALSE( a.mightBeIndexed( "a.c" ) ); } + TEST( IndexPathSetTest, Simple2 ) { + IndexPathSet a; + a.addPath( "ab" ); + ASSERT_FALSE( a.mightBeIndexed( "a" ) ); + } + + TEST( IndexPathSetTest, getCanonicalIndexField1 ) { string x; -- cgit v1.2.1