From c31befb4c2870bc0e615ab09645ace281906cea5 Mon Sep 17 00:00:00 2001 From: Geert Bosch Date: Tue, 28 Mar 2017 17:30:05 -0400 Subject: SERVER-28532 Implement UUID::appendToBuilder --- src/mongo/util/uuid.cpp | 8 +++++--- src/mongo/util/uuid.h | 10 ++++++++-- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/mongo/util/uuid.cpp b/src/mongo/util/uuid.cpp index ca9f527a2a5..e47889a6244 100644 --- a/src/mongo/util/uuid.cpp +++ b/src/mongo/util/uuid.cpp @@ -50,8 +50,6 @@ std::regex uuidRegex("[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4 } // namespace -const int UUID::kNumBytes = 16; - StatusWith UUID::parse(BSONElement from) { try { return UUID{from.uuid()}; @@ -111,9 +109,13 @@ UUID UUID::gen() { return UUID{randomBytes}; } +void UUID::appendToBuilder(BSONObjBuilder* builder, StringData name) const { + builder->appendBinData(name, sizeof(UUIDStorage), BinDataType::newUUID, &_uuid); +} + BSONObj UUID::toBSON() const { BSONObjBuilder builder; - builder.appendBinData("uuid", sizeof(UUIDStorage), BinDataType::newUUID, &_uuid); + appendToBuilder(&builder, "uuid"); return builder.obj(); } diff --git a/src/mongo/util/uuid.h b/src/mongo/util/uuid.h index 5127932dd6a..22adb92ba01 100644 --- a/src/mongo/util/uuid.h +++ b/src/mongo/util/uuid.h @@ -44,13 +44,15 @@ namespace mongo { * a secure random number generator. */ class UUID { + using UUIDStorage = std::array; + public: UUID() = delete; /** * The number of bytes contained in a UUID. */ - static const int kNumBytes; + static constexpr int kNumBytes = sizeof(UUIDStorage); /** * Generate a new random v4 UUID per RFC 4122. @@ -74,6 +76,11 @@ public: */ static bool isUUIDString(const std::string& s); + /** + * Append to builder as BinData(4, "...") element with the given name. + */ + void appendToBuilder(BSONObjBuilder* builder, StringData name) const; + /** * Return a BSON object of the form { uuid: BinData(4, "...") }. */ @@ -109,7 +116,6 @@ public: }; private: - using UUIDStorage = std::array; UUID(const UUIDStorage& uuid) : _uuid(uuid) {} UUIDStorage _uuid; // UUID in network byte order -- cgit v1.2.1