summaryrefslogtreecommitdiff
path: root/src/mongo
diff options
context:
space:
mode:
Diffstat (limited to 'src/mongo')
-rw-r--r--src/mongo/db/repl/rollback_fix_up_info_descriptions_test.cpp14
-rw-r--r--src/mongo/db/repl/rollback_fix_up_info_test.cpp133
-rw-r--r--src/mongo/util/uuid.cpp7
-rw-r--r--src/mongo/util/uuid.h8
-rw-r--r--src/mongo/util/uuid_test.cpp11
5 files changed, 85 insertions, 88 deletions
diff --git a/src/mongo/db/repl/rollback_fix_up_info_descriptions_test.cpp b/src/mongo/db/repl/rollback_fix_up_info_descriptions_test.cpp
index 5433e640c47..76e2b18472a 100644
--- a/src/mongo/db/repl/rollback_fix_up_info_descriptions_test.cpp
+++ b/src/mongo/db/repl/rollback_fix_up_info_descriptions_test.cpp
@@ -52,8 +52,7 @@ TEST(RollbackFixUpInfoDescriptionsTest, SingleDocumentDescriptionToBson) {
"mydb");
auto expectedDocument = BSON(
- "_id" << BSON("collectionUuid" << collectionUuid.toBSON().firstElement() << "documentId"
- << docId.firstElement())
+ "_id" << BSON("collectionUuid" << collectionUuid << "documentId" << docId.firstElement())
<< "operationType"
<< "insert"
<< "db"
@@ -70,8 +69,7 @@ TEST(RollbackFixUpInfoDescriptionsTest, CollectionUuidDescriptionToBson) {
RollbackFixUpInfo::CollectionUuidDescription description(collectionUuid, nss);
- auto expectedDocument =
- BSON("_id" << collectionUuid.toBSON().firstElement() << "ns" << nss.ns());
+ auto expectedDocument = BSON("_id" << collectionUuid << "ns" << nss.ns());
ASSERT_BSONOBJ_EQ(expectedDocument, description.toBSON());
}
@@ -82,7 +80,7 @@ TEST(RollbackFixUpInfoDescriptionsTest, CollectionUuidDescriptionWithEmptyNamesp
RollbackFixUpInfo::CollectionUuidDescription description(collectionUuid, emptyNss);
- auto expectedDocument = BSON("_id" << collectionUuid.toBSON().firstElement() << "ns"
+ auto expectedDocument = BSON("_id" << collectionUuid << "ns"
<< "");
ASSERT_BSONOBJ_EQ(expectedDocument, description.toBSON());
@@ -99,8 +97,7 @@ TEST(RollbackFixUpInfoDescriptionsTest, CollectionOptionsDescriptionToBson) {
RollbackFixUpInfo::CollectionOptionsDescription description(collectionUuid, options.toBSON());
- auto expectedDocument =
- BSON("_id" << collectionUuid.toBSON().firstElement() << "options" << options.toBSON());
+ auto expectedDocument = BSON("_id" << collectionUuid << "options" << options.toBSON());
ASSERT_BSONOBJ_EQ(expectedDocument, description.toBSON());
}
@@ -142,8 +139,7 @@ TEST(RollbackFixUpInfoDescriptionsTest, IndexDescriptionToBson) {
collectionUuid, indexName, RollbackFixUpInfo::IndexOpType::kDrop, infoObj);
auto expectedDocument =
- BSON("_id" << BSON("collectionUuid" << collectionUuid.toBSON().firstElement() << "indexName"
- << indexName)
+ BSON("_id" << BSON("collectionUuid" << collectionUuid << "indexName" << indexName)
<< "operationType"
<< "drop"
<< "infoObj"
diff --git a/src/mongo/db/repl/rollback_fix_up_info_test.cpp b/src/mongo/db/repl/rollback_fix_up_info_test.cpp
index ec4c8039cd9..a2e964f090f 100644
--- a/src/mongo/db/repl/rollback_fix_up_info_test.cpp
+++ b/src/mongo/db/repl/rollback_fix_up_info_test.cpp
@@ -160,7 +160,7 @@ TEST_F(RollbackFixUpInfoTest,
<< "ns"
<< "test.t"
<< "ui"
- << UUID::gen().toBSON().firstElement()
+ << UUID::gen()
<< "o"
<< BSON("_id"
<< "mydocid"
@@ -180,15 +180,14 @@ TEST_F(RollbackFixUpInfoTest,
RollbackFixUpInfo::SingleDocumentOpType::kInsert,
nss.db().toString()));
- auto expectedDocument = BSON(
- "_id" << BSON("collectionUuid" << collectionUuid.toBSON().firstElement() << "documentId"
- << docId)
- << "operationType"
- << "insert"
- << "db"
- << "test"
- << "documentToRestore"
- << BSONNULL);
+ auto expectedDocument =
+ BSON("_id" << BSON("collectionUuid" << collectionUuid << "documentId" << docId)
+ << "operationType"
+ << "insert"
+ << "db"
+ << "test"
+ << "documentToRestore"
+ << BSONNULL);
_assertDocumentsInCollectionEquals(
opCtx.get(), RollbackFixUpInfo::kRollbackDocsNamespace, {expectedDocument});
}
@@ -269,7 +268,7 @@ TEST_F(RollbackFixUpInfoTest,
<< "ns"
<< "test.t"
<< "ui"
- << UUID::gen().toBSON().firstElement()
+ << UUID::gen()
<< "o"
<< BSON("_id"
<< "mydocid"));
@@ -287,15 +286,14 @@ TEST_F(RollbackFixUpInfoTest,
RollbackFixUpInfo::SingleDocumentOpType::kDelete,
nss.db().toString()));
- auto expectedDocument = BSON(
- "_id" << BSON("collectionUuid" << collectionUuid.toBSON().firstElement() << "documentId"
- << docId)
- << "operationType"
- << "delete"
- << "db"
- << "test"
- << "documentToRestore"
- << BSONNULL);
+ auto expectedDocument =
+ BSON("_id" << BSON("collectionUuid" << collectionUuid << "documentId" << docId)
+ << "operationType"
+ << "delete"
+ << "db"
+ << "test"
+ << "documentToRestore"
+ << BSONNULL);
_assertDocumentsInCollectionEquals(
opCtx.get(), RollbackFixUpInfo::kRollbackDocsNamespace, {expectedDocument});
@@ -308,7 +306,7 @@ TEST_F(RollbackFixUpInfoTest,
<< "ns"
<< "test.t"
<< "ui"
- << UUID::gen().toBSON().firstElement()
+ << UUID::gen()
<< "o2"
<< BSON("_id"
<< "mydocid")
@@ -328,15 +326,14 @@ TEST_F(RollbackFixUpInfoTest,
RollbackFixUpInfo::SingleDocumentOpType::kUpdate,
nss.db().toString()));
- auto expectedDocument = BSON(
- "_id" << BSON("collectionUuid" << collectionUuid.toBSON().firstElement() << "documentId"
- << docId)
- << "operationType"
- << "update"
- << "db"
- << "test"
- << "documentToRestore"
- << BSONNULL);
+ auto expectedDocument =
+ BSON("_id" << BSON("collectionUuid" << collectionUuid << "documentId" << docId)
+ << "operationType"
+ << "update"
+ << "db"
+ << "test"
+ << "documentToRestore"
+ << BSONNULL);
_assertDocumentsInCollectionEquals(
opCtx.get(), RollbackFixUpInfo::kRollbackDocsNamespace, {expectedDocument});
@@ -432,7 +429,7 @@ TEST_F(
<< "ns"
<< "mydb.$cmd"
<< "ui"
- << UUID::gen().toBSON().firstElement()
+ << UUID::gen()
<< "o"
<< BSON("create"
<< "mynewcoll"
@@ -526,7 +523,7 @@ TEST_F(
ASSERT_OK(rollbackFixUpInfo.processCreateCollectionOplogEntry(opCtx.get(), collectionUuid));
- auto expectedDocument = BSON("_id" << collectionUuid.toBSON().firstElement() << "ns"
+ auto expectedDocument = BSON("_id" << collectionUuid << "ns"
<< "");
// Finally, process create collection oplog entry.
@@ -556,7 +553,7 @@ TEST_F(RollbackFixUpInfoTest,
<< "ns"
<< "mydb.$cmd"
<< "ui"
- << UUID::gen().toBSON().firstElement()
+ << UUID::gen()
<< "o"
<< BSON("drop"
<< "mydroppedcoll"));
@@ -571,8 +568,7 @@ TEST_F(RollbackFixUpInfoTest,
RollbackFixUpInfo rollbackFixUpInfo(_storageInterface.get());
ASSERT_OK(rollbackFixUpInfo.processDropCollectionOplogEntry(opCtx.get(), collectionUuid, nss));
- auto expectedDocument =
- BSON("_id" << collectionUuid.toBSON().firstElement() << "ns" << nss.ns());
+ auto expectedDocument = BSON("_id" << collectionUuid << "ns" << nss.ns());
_assertDocumentsInCollectionEquals(
opCtx.get(), RollbackFixUpInfo::kRollbackCollectionUuidNamespace, {expectedDocument});
@@ -586,7 +582,7 @@ TEST_F(
<< "ns"
<< "mydb.$cmd"
<< "ui"
- << UUID::gen().toBSON().firstElement()
+ << UUID::gen()
<< "o"
<< BSON("renameCollection"
<< "mydb.prevCollName"
@@ -607,8 +603,7 @@ TEST_F(
ASSERT_OK(rollbackFixUpInfo.processRenameCollectionOplogEntry(
opCtx.get(), collectionUuid, sourceNss, boost::none));
- auto expectedDocument =
- BSON("_id" << collectionUuid.toBSON().firstElement() << "ns" << sourceNss.ns());
+ auto expectedDocument = BSON("_id" << collectionUuid << "ns" << sourceNss.ns());
_assertDocumentsInCollectionEquals(
opCtx.get(), RollbackFixUpInfo::kRollbackCollectionUuidNamespace, {expectedDocument});
@@ -622,7 +617,7 @@ TEST_F(
<< "ns"
<< "mydb.$cmd"
<< "ui"
- << UUID::gen().toBSON().firstElement()
+ << UUID::gen()
<< "o"
<< BSON("renameCollection"
<< "mydb.prevCollName"
@@ -631,7 +626,7 @@ TEST_F(
<< "stayTemp"
<< false
<< "dropTarget"
- << UUID::gen().toBSON().firstElement()));
+ << UUID::gen()));
auto collectionUuid = unittest::assertGet(UUID::parse(operation["ui"]));
NamespaceString sourceNss(operation["o"].Obj().firstElement().String());
NamespaceString targetNss(operation["o"].Obj()["to"].String());
@@ -645,10 +640,8 @@ TEST_F(
ASSERT_OK(rollbackFixUpInfo.processRenameCollectionOplogEntry(
opCtx.get(), collectionUuid, sourceNss, std::make_pair(droppedCollectionUuid, targetNss)));
- auto expectedDocument1 =
- BSON("_id" << collectionUuid.toBSON().firstElement() << "ns" << sourceNss.ns());
- auto expectedDocument2 =
- BSON("_id" << droppedCollectionUuid.toBSON().firstElement() << "ns" << targetNss.ns());
+ auto expectedDocument1 = BSON("_id" << collectionUuid << "ns" << sourceNss.ns());
+ auto expectedDocument2 = BSON("_id" << droppedCollectionUuid << "ns" << targetNss.ns());
_assertDocumentsInCollectionEquals(opCtx.get(),
RollbackFixUpInfo::kRollbackCollectionUuidNamespace,
@@ -663,7 +656,7 @@ TEST_F(RollbackFixUpInfoTest,
<< "ns"
<< "mydb.$cmd"
<< "ui"
- << UUID::gen().toBSON().firstElement()
+ << UUID::gen()
<< "o"
<< BSON("collMod"
<< "mycoll"
@@ -688,8 +681,7 @@ TEST_F(RollbackFixUpInfoTest,
RollbackFixUpInfo rollbackFixUpInfo(_storageInterface.get());
ASSERT_OK(rollbackFixUpInfo.processCollModOplogEntry(opCtx.get(), collectionUuid, optionsObj));
- auto expectedDocument =
- BSON("_id" << collectionUuid.toBSON().firstElement() << "options" << optionsObj);
+ auto expectedDocument = BSON("_id" << collectionUuid << "options" << optionsObj);
_assertDocumentsInCollectionEquals(
opCtx.get(), RollbackFixUpInfo::kRollbackCollectionOptionsNamespace, {expectedDocument});
@@ -703,7 +695,7 @@ TEST_F(RollbackFixUpInfoTest,
<< "ns"
<< "mydb.$cmd"
<< "ui"
- << UUID::gen().toBSON().firstElement()
+ << UUID::gen()
<< "o"
<< BSON("createIndex" << 1 << "v" << 2 << "key" << BSON("b" << 1) << "name"
<< "b_1"
@@ -722,8 +714,7 @@ TEST_F(RollbackFixUpInfoTest,
rollbackFixUpInfo.processCreateIndexOplogEntry(opCtx.get(), collectionUuid, indexName));
auto expectedDocument =
- BSON("_id" << BSON("collectionUuid" << collectionUuid.toBSON().firstElement() << "indexName"
- << indexName)
+ BSON("_id" << BSON("collectionUuid" << collectionUuid << "indexName" << indexName)
<< "operationType"
<< "create"
<< "infoObj"
@@ -756,9 +747,7 @@ TEST_F(RollbackFixUpInfoTest,
_assertDocumentsInCollectionEquals(
opCtx.get(),
RollbackFixUpInfo::kRollbackIndexNamespace,
- {BSON("_id" << BSON("collectionUuid" << collectionUuid.toBSON().firstElement()
- << "indexName"
- << indexName)
+ {BSON("_id" << BSON("collectionUuid" << collectionUuid << "indexName" << indexName)
<< "operationType"
<< "drop"
<< "infoObj"
@@ -792,9 +781,7 @@ TEST_F(RollbackFixUpInfoTest,
_assertDocumentsInCollectionEquals(
opCtx.get(),
RollbackFixUpInfo::kRollbackIndexNamespace,
- {BSON("_id" << BSON("collectionUuid" << collectionUuid.toBSON().firstElement()
- << "indexName"
- << indexName)
+ {BSON("_id" << BSON("collectionUuid" << collectionUuid << "indexName" << indexName)
<< "operationType"
<< "updateTTL"
<< "infoObj"
@@ -807,9 +794,7 @@ TEST_F(RollbackFixUpInfoTest,
_assertDocumentsInCollectionEquals(
opCtx.get(),
RollbackFixUpInfo::kRollbackIndexNamespace,
- {BSON("_id" << BSON("collectionUuid" << collectionUuid.toBSON().firstElement()
- << "indexName"
- << indexName)
+ {BSON("_id" << BSON("collectionUuid" << collectionUuid << "indexName" << indexName)
<< "operationType"
<< "create"
<< "infoObj"
@@ -827,8 +812,7 @@ TEST_F(
RollbackFixUpInfo rollbackFixUpInfo(_storageInterface.get());
auto malformedDoc =
- BSON("_id" << BSON("collectionUuid" << collectionUuid.toBSON().firstElement() << "indexName"
- << indexName)
+ BSON("_id" << BSON("collectionUuid" << collectionUuid << "indexName" << indexName)
<< "operationType"
<< "unknownIndexOpType"
<< "infoObj"
@@ -846,8 +830,7 @@ TEST_F(
rollbackFixUpInfo.processCreateIndexOplogEntry(opCtx.get(), collectionUuid, indexName));
auto expectedDocument =
- BSON("_id" << BSON("collectionUuid" << collectionUuid.toBSON().firstElement() << "indexName"
- << indexName)
+ BSON("_id" << BSON("collectionUuid" << collectionUuid << "indexName" << indexName)
<< "operationType"
<< "create"
<< "infoObj"
@@ -865,7 +848,7 @@ TEST_F(
<< "ns"
<< "mydb.$cmd"
<< "ui"
- << UUID::gen().toBSON().firstElement()
+ << UUID::gen()
<< "o"
<< BSON("collMod"
<< "mycoll"
@@ -890,8 +873,7 @@ TEST_F(
opCtx.get(), collectionUuid, indexName, expireAfterSeconds));
auto expectedDocument =
- BSON("_id" << BSON("collectionUuid" << collectionUuid.toBSON().firstElement() << "indexName"
- << indexName)
+ BSON("_id" << BSON("collectionUuid" << collectionUuid << "indexName" << indexName)
<< "operationType"
<< "updateTTL"
<< "infoObj"
@@ -942,8 +924,7 @@ TEST_F(
auto expectedInfoObj = bob.obj();
auto expectedDocument =
- BSON("_id" << BSON("collectionUuid" << collectionUuid.toBSON().firstElement() << "indexName"
- << indexName)
+ BSON("_id" << BSON("collectionUuid" << collectionUuid << "indexName" << indexName)
<< "operationType"
<< "drop"
<< "infoObj"
@@ -976,8 +957,7 @@ TEST_F(
opCtx.get(), collectionUuid, indexName, Seconds(60)));
auto expectedDocument =
- BSON("_id" << BSON("collectionUuid" << collectionUuid.toBSON().firstElement() << "indexName"
- << indexName)
+ BSON("_id" << BSON("collectionUuid" << collectionUuid << "indexName" << indexName)
<< "operationType"
<< "updateTTL"
<< "infoObj"
@@ -994,7 +974,7 @@ TEST_F(RollbackFixUpInfoTest,
<< "ns"
<< "mydb.$cmd"
<< "ui"
- << UUID::gen().toBSON().firstElement()
+ << UUID::gen()
<< "o"
<< BSON("dropIndexes"
<< "mycoll"
@@ -1019,8 +999,7 @@ TEST_F(RollbackFixUpInfoTest,
opCtx.get(), collectionUuid, indexName, infoObj));
auto expectedDocument =
- BSON("_id" << BSON("collectionUuid" << collectionUuid.toBSON().firstElement() << "indexName"
- << indexName)
+ BSON("_id" << BSON("collectionUuid" << collectionUuid << "indexName" << indexName)
<< "operationType"
<< "drop"
<< "infoObj"
@@ -1053,9 +1032,7 @@ TEST_F(RollbackFixUpInfoTest,
_assertDocumentsInCollectionEquals(
opCtx.get(),
RollbackFixUpInfo::kRollbackIndexNamespace,
- {BSON("_id" << BSON("collectionUuid" << collectionUuid.toBSON().firstElement()
- << "indexName"
- << indexName)
+ {BSON("_id" << BSON("collectionUuid" << collectionUuid << "indexName" << indexName)
<< "operationType"
<< "create"
<< "infoObj"
@@ -1070,9 +1047,7 @@ TEST_F(RollbackFixUpInfoTest,
_assertDocumentsInCollectionEquals(
opCtx.get(),
RollbackFixUpInfo::kRollbackIndexNamespace,
- {BSON("_id" << BSON("collectionUuid" << collectionUuid.toBSON().firstElement()
- << "indexName"
- << indexName)
+ {BSON("_id" << BSON("collectionUuid" << collectionUuid << "indexName" << indexName)
<< "operationType"
<< "drop"
<< "infoObj"
diff --git a/src/mongo/util/uuid.cpp b/src/mongo/util/uuid.cpp
index e47889a6244..8d7326fd76b 100644
--- a/src/mongo/util/uuid.cpp
+++ b/src/mongo/util/uuid.cpp
@@ -136,4 +136,11 @@ std::string UUID::toString() const {
return ss.str();
}
+template <>
+BSONObjBuilder& BSONObjBuilderValueStream::operator<<<UUID>(UUID value) {
+ value.appendToBuilder(_builder, _fieldName);
+ _fieldName = StringData();
+ return *_builder;
+}
+
} // namespace mongo
diff --git a/src/mongo/util/uuid.h b/src/mongo/util/uuid.h
index 22adb92ba01..d2073ec2a4b 100644
--- a/src/mongo/util/uuid.h
+++ b/src/mongo/util/uuid.h
@@ -35,6 +35,7 @@
#include "mongo/base/status_with.h"
#include "mongo/bson/bsonelement.h"
+#include "mongo/bson/bsonmisc.h"
#include "mongo/bson/bsonobj.h"
namespace mongo {
@@ -129,4 +130,11 @@ inline StringBuilder& operator<<(StringBuilder& s, const UUID& uuid) {
return (s << uuid.toString());
}
+/**
+ * Supports use of UUID with the BSON macro:
+ * BSON("uuid" << uuid) -> { uuid: BinData(4, "...") }
+ */
+template <>
+BSONObjBuilder& BSONObjBuilderValueStream::operator<<<UUID>(UUID value);
+
} // namespace mongo
diff --git a/src/mongo/util/uuid_test.cpp b/src/mongo/util/uuid_test.cpp
index e321f840e5e..b0a5d12dada 100644
--- a/src/mongo/util/uuid_test.cpp
+++ b/src/mongo/util/uuid_test.cpp
@@ -170,5 +170,16 @@ TEST(UUIDTest, toAndFromBSONTest) {
ASSERT_EQUALS(ErrorCodes::InvalidUUID, UUID::parse(bson4.getField("uuid")));
}
+TEST(UUIDTest, toBSONUsingBSONMacroTest) {
+ auto uuid = UUID::gen();
+ auto bson = BSON("myuuid" << uuid);
+
+ BSONObjBuilder bob;
+ uuid.appendToBuilder(&bob, "myuuid");
+ auto expectedBson = bob.obj();
+
+ ASSERT_BSONOBJ_EQ(expectedBson, bson);
+}
+
} // namespace
} // namespace mongo