diff options
author | Adam Midvidy <amidvidy@gmail.com> | 2015-10-22 10:32:47 -0400 |
---|---|---|
committer | Adam Midvidy <amidvidy@gmail.com> | 2015-10-30 15:14:06 -0400 |
commit | e62e2e71eff397caf22a0da13ac4669a8546b298 (patch) | |
tree | d170510044130c0c8e34950387b4afac957f097b /src/mongo/bson/bsonobj.h | |
parent | cbc6a790dc4ac537b071626dff77d0073512690c (diff) | |
download | mongo-e62e2e71eff397caf22a0da13ac4669a8546b298.tar.gz |
SERVER-20609 use getFields instead of calling getField 4x
Diffstat (limited to 'src/mongo/bson/bsonobj.h')
-rw-r--r-- | src/mongo/bson/bsonobj.h | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/src/mongo/bson/bsonobj.h b/src/mongo/bson/bsonobj.h index 28f51e55ec3..dd19526d47a 100644 --- a/src/mongo/bson/bsonobj.h +++ b/src/mongo/bson/bsonobj.h @@ -29,6 +29,7 @@ #pragma once +#include <bitset> #include <list> #include <set> #include <string> @@ -244,8 +245,18 @@ public: @param fields if a field is found its element is stored in its corresponding position in this array. if not found the array element is unchanged. */ + void getFields(unsigned n, const char** fieldNames, BSONElement* fields) const; + /** + * Get several fields at once. This is faster than separate getField() calls as the size of + * elements iterated can then be calculated only once each. + */ + template <size_t N> + void getFields(const std::array<StringData, N>& fieldNames, + std::array<BSONElement, N>* fields) const; + + /** Get the field of the specified name. eoo() is true on the returned element if not found. */ @@ -802,4 +813,22 @@ struct DataType::Handler<BSONObj> { return BSONObj(); } }; + +template <size_t N> +inline void BSONObj::getFields(const std::array<StringData, N>& fieldNames, + std::array<BSONElement, N>* fields) const { + std::bitset<N> foundFields; + auto iter = this->begin(); + while (iter.more() && !foundFields.all()) { + auto el = iter.next(); + auto fieldName = el.fieldNameStringData(); + for (std::size_t i = 0; i < N; ++i) { + if (!foundFields.test(i) && (fieldNames[i] == fieldName)) { + (*fields)[i] = std::move(el); + foundFields.set(i); + break; + } + } + } } +} // namespace mongo |