diff options
author | Eliot Horowitz <eliot@10gen.com> | 2013-05-08 11:56:39 -0400 |
---|---|---|
committer | Eliot Horowitz <eliot@10gen.com> | 2013-05-09 10:16:55 -0400 |
commit | 5638031288f5dc8069dee5bebb02a7698d39ee75 (patch) | |
tree | f7f219e8a27fc4c1edcb5309e38145a1d7622a26 /src | |
parent | b326df258988f9f7d91dcbe77440ab3c365bc3da (diff) | |
download | mongo-5638031288f5dc8069dee5bebb02a7698d39ee75.tar.gz |
SERVER-6400: FieldRef support for sub dottedField
Diffstat (limited to 'src')
-rw-r--r-- | src/mongo/db/field_ref.cpp | 11 | ||||
-rw-r--r-- | src/mongo/db/field_ref.h | 2 | ||||
-rw-r--r-- | src/mongo/db/field_ref_test.cpp | 13 |
3 files changed, 20 insertions, 6 deletions
diff --git a/src/mongo/db/field_ref.cpp b/src/mongo/db/field_ref.cpp index 68ab798b320..bc59e6583f6 100644 --- a/src/mongo/db/field_ref.cpp +++ b/src/mongo/db/field_ref.cpp @@ -101,15 +101,16 @@ namespace mongo { _replacements.clear(); } - std::string FieldRef::dottedField() const { + std::string FieldRef::dottedField( size_t offset ) const { std::string res; - if (_size == 0) { + + if (_size == 0 || offset >= numParts() ) { return res; } - res.append(_fixed[0].rawData(), _fixed[0].size()); - for (size_t i=1; i<_size; i++) { - res.append(1, '.'); + for (size_t i=offset; i<_size; i++) { + if ( i > offset ) + res.append(1, '.'); StringData part = getPart(i); res.append(part.rawData(), part.size()); } diff --git a/src/mongo/db/field_ref.h b/src/mongo/db/field_ref.h index ecc94bce046..e61192d47fc 100644 --- a/src/mongo/db/field_ref.h +++ b/src/mongo/db/field_ref.h @@ -66,7 +66,7 @@ namespace mongo { * Returns a copy of the full dotted field in its current state (i.e., some parts may * have been replaced since the parse() call). */ - std::string dottedField() const; + std::string dottedField( size_t offset = 0 ) const; /** * Resets the internal state. See note in parse() call. diff --git a/src/mongo/db/field_ref_test.cpp b/src/mongo/db/field_ref_test.cpp index 5c2b6dda00e..7dbd7a3359e 100644 --- a/src/mongo/db/field_ref_test.cpp +++ b/src/mongo/db/field_ref_test.cpp @@ -157,4 +157,17 @@ namespace { ASSERT( !a.equalsDottedField( "a.b.c" ) ); } + TEST( DottedField, Simple1 ) { + FieldRef a; + a.parse( "a.b.c.d.e" ); + ASSERT_EQUALS( "a.b.c.d.e", a.dottedField() ); + ASSERT_EQUALS( "a.b.c.d.e", a.dottedField(0) ); + ASSERT_EQUALS( "b.c.d.e", a.dottedField(1) ); + ASSERT_EQUALS( "c.d.e", a.dottedField(2) ); + ASSERT_EQUALS( "d.e", a.dottedField(3) ); + ASSERT_EQUALS( "e", a.dottedField(4) ); + ASSERT_EQUALS( "", a.dottedField(5) ); + ASSERT_EQUALS( "", a.dottedField(6) ); + } + } // namespace mongo |