summaryrefslogtreecommitdiff
path: root/src/mongo/util
diff options
context:
space:
mode:
authormathisbessamdb <mathis.bessa@mongodb.com>2023-05-01 20:06:00 +0000
committerEvergreen Agent <no-reply@evergreen.mongodb.com>2023-05-10 20:07:40 +0000
commit1da4e2258b20629c277ce78b9c1776b28717d2ec (patch)
tree2c05c63d558c3069af63ffc0f6f2af232dd9beea /src/mongo/util
parentad8a74f5782fb8b3f24f74305c44da725732616a (diff)
downloadmongo-1da4e2258b20629c277ce78b9c1776b28717d2ec.tar.gz
SERVER-74491 Make DatabaseName::toString private
Diffstat (limited to 'src/mongo/util')
-rw-r--r--src/mongo/util/database_name_util.cpp14
-rw-r--r--src/mongo/util/database_name_util.h13
-rw-r--r--src/mongo/util/database_name_util_test.cpp56
3 files changed, 74 insertions, 9 deletions
diff --git a/src/mongo/util/database_name_util.cpp b/src/mongo/util/database_name_util.cpp
index bc2b999cc5e..a79255578eb 100644
--- a/src/mongo/util/database_name_util.cpp
+++ b/src/mongo/util/database_name_util.cpp
@@ -60,6 +60,11 @@ std::string DatabaseNameUtil::serializeForStorage(const DatabaseName& dbName,
return dbName.toStringWithTenantId();
}
+std::string DatabaseNameUtil::serializeForCatalog(const DatabaseName& dbName,
+ const SerializationContext& context) {
+ return dbName.toStringWithTenantId();
+}
+
std::string DatabaseNameUtil::serializeForCommands(const DatabaseName& dbName,
const SerializationContext& context) {
// tenantId came from either a $tenant field or security token
@@ -88,7 +93,6 @@ std::string DatabaseNameUtil::serializeForCommands(const DatabaseName& dbName,
}
}
-
DatabaseName DatabaseNameUtil::parseDbNameFromStringExpectTenantIdInMultitenancyMode(
StringData dbName) {
if (!gMultitenancySupport) {
@@ -196,4 +200,12 @@ DatabaseName DatabaseNameUtil::deserializeForCommands(boost::optional<TenantId>
return dbName;
}
+DatabaseName DatabaseNameUtil::deserializeForCatalog(StringData db,
+ const SerializationContext& context) {
+ // TenantId always prefix in the passed `db` for durable catalog. This method below checks for
+ // multitenancy and will either return a DatabaseName with (tenantId, nonPrefixedDb) or
+ // (none, prefixedDb).
+ return DatabaseNameUtil::parseDbNameFromStringExpectTenantIdInMultitenancyMode(db);
+}
+
} // namespace mongo
diff --git a/src/mongo/util/database_name_util.h b/src/mongo/util/database_name_util.h
index 352f24a9dd8..d58d6f02c8c 100644
--- a/src/mongo/util/database_name_util.h
+++ b/src/mongo/util/database_name_util.h
@@ -67,6 +67,13 @@ public:
const DatabaseName& dbName, const SerializationContext& context = SerializationContext());
/**
+ * To be used only for durable catalog. We always include the tenantId as prefixed in a
+ * databasename for the catalog.
+ */
+ static std::string serializeForCatalog(
+ const DatabaseName& dbName, const SerializationContext& context = SerializationContext());
+
+ /**
* Deserializes StringData dbName to a DatabaseName object.
*
* If multitenancySupport is enabled and featureFlagRequireTenantID is enabled, then a
@@ -105,6 +112,12 @@ public:
StringData db,
const SerializationContext& context = SerializationContext());
+ /**
+ * To be used only by the storage catalog.
+ */
+ static DatabaseName deserializeForCatalog(
+ StringData db, const SerializationContext& context = SerializationContext());
+
private:
static DatabaseName parseDbNameFromStringExpectTenantIdInMultitenancyMode(StringData dbName);
};
diff --git a/src/mongo/util/database_name_util_test.cpp b/src/mongo/util/database_name_util_test.cpp
index d573ee33434..1c292a9c2e5 100644
--- a/src/mongo/util/database_name_util_test.cpp
+++ b/src/mongo/util/database_name_util_test.cpp
@@ -273,6 +273,46 @@ TEST(DatabaseNameUtilTest, SerializeExpectPrefixTrue_CommandReply) {
}
}
+TEST(DatabaseNameUtilTest, Serialize_StorageCatalog) {
+ TenantId tenantId(OID::gen());
+ const std::string dbnString = "foo";
+ const std::string dbnPrefixString = str::stream() << tenantId.toString() << "_" << dbnString;
+
+ {
+ RAIIServerParameterControllerForTest multitenanyController("multitenancySupport", false);
+ { // No prefix, no tenantId.
+ // request --> { ns: database.coll }
+ auto dbName = DatabaseNameUtil::deserializeForCatalog(dbnString);
+ ASSERT_EQ(dbName.tenantId(), boost::none);
+ ASSERT_EQ(DatabaseNameUtil::serializeForCatalog(dbName), dbnString);
+ }
+
+ { // Has prefix, no tenantId. Storage catalog always returns prefixed dbname.
+ // request --> { ns: tenantId_database.coll }
+ auto dbName = DatabaseNameUtil::deserializeForCatalog(dbnPrefixString);
+ ASSERT_EQ(dbName.tenantId(), boost::none);
+ ASSERT_EQ(DatabaseNameUtil::serializeForCatalog(dbName), dbnPrefixString);
+ }
+ }
+
+ {
+ RAIIServerParameterControllerForTest multitenanyController("multitenancySupport", true);
+ { // No prefix, no tenantId.
+ // request --> { ns: database.coll }
+ auto dbName = DatabaseNameUtil::deserializeForCatalog(dbnString);
+ ASSERT_EQ(dbName.tenantId(), boost::none);
+ ASSERT_EQ(DatabaseNameUtil::serializeForCatalog(dbName), dbnString);
+ }
+
+ { // Has prefix, no tenantId. Storage catalog always returns prefixed dbname.
+ // request --> { ns: tenantId_database.coll }
+ auto dbName = DatabaseNameUtil::deserializeForCatalog(dbnPrefixString);
+ ASSERT_EQ(dbName.tenantId(), tenantId);
+ ASSERT_EQ(DatabaseNameUtil::serializeForCatalog(dbName), dbnPrefixString);
+ }
+ }
+}
+
TEST(DatabaseNameUtilTest, DeserializeMissingExpectPrefix_CommandRequest) {
RAIIServerParameterControllerForTest multitenanyController("multitenancySupport", true);
TenantId tenantId(OID::gen());
@@ -299,7 +339,7 @@ TEST(DatabaseNameUtilTest, DeserializeMissingExpectPrefix_CommandRequest) {
auto dbName =
DatabaseNameUtil::deserializeForCommands(boost::none, dbnPrefixString, ctxt_noTenantId);
ASSERT_EQ(dbName.tenantId(), tenantId);
- ASSERT_EQ(dbName.toString(), dbnString);
+ ASSERT_EQ(dbName.toString_forTest(), dbnString);
}
{ // No prefix, has tenantId.
@@ -307,7 +347,7 @@ TEST(DatabaseNameUtilTest, DeserializeMissingExpectPrefix_CommandRequest) {
auto dbName =
DatabaseNameUtil::deserializeForCommands(tenantId, dbnString, ctxt_withTenantId);
ASSERT_EQ(dbName.tenantId(), tenantId);
- ASSERT_EQ(dbName.toString(), dbnString);
+ ASSERT_EQ(dbName.toString_forTest(), dbnString);
}
{ // Has prefix, has tenantId. *** we shouldn't see this from Atlas Proxy
@@ -315,7 +355,7 @@ TEST(DatabaseNameUtilTest, DeserializeMissingExpectPrefix_CommandRequest) {
auto dbName =
DatabaseNameUtil::deserializeForCommands(tenantId, dbnPrefixString, ctxt_withTenantId);
ASSERT_EQ(dbName.tenantId(), tenantId);
- ASSERT_EQ(dbName.toString(), dbnPrefixString);
+ ASSERT_EQ(dbName.toString_forTest(), dbnPrefixString);
}
}
@@ -350,7 +390,7 @@ TEST(DatabaseNameUtilTest, DeserializeExpectPrefixFalse_CommandRequest) {
// can't expect dbName.toString == dbnPrefixString as we will still attempt to parse the
// prefix as usual.
ASSERT_EQ(dbName.tenantId(), tenantId);
- ASSERT_EQ(dbName.toString(), dbnString);
+ ASSERT_EQ(dbName.toString_forTest(), dbnString);
}
{ // No prefix, has tenantId.
@@ -358,7 +398,7 @@ TEST(DatabaseNameUtilTest, DeserializeExpectPrefixFalse_CommandRequest) {
auto dbName =
DatabaseNameUtil::deserializeForCommands(tenantId, dbnString, ctxt_withTenantId);
ASSERT_EQ(dbName.tenantId(), tenantId);
- ASSERT_EQ(dbName.toString(), dbnString);
+ ASSERT_EQ(dbName.toString_forTest(), dbnString);
}
{ // Has prefix, has tenantId. *** we shouldn't see this from Atlas Proxy
@@ -366,7 +406,7 @@ TEST(DatabaseNameUtilTest, DeserializeExpectPrefixFalse_CommandRequest) {
auto dbName =
DatabaseNameUtil::deserializeForCommands(tenantId, dbnPrefixString, ctxt_withTenantId);
ASSERT_EQ(dbName.tenantId(), tenantId);
- ASSERT_EQ(dbName.toString(), dbnPrefixString);
+ ASSERT_EQ(dbName.toString_forTest(), dbnPrefixString);
}
}
@@ -398,7 +438,7 @@ TEST(DatabaseNameUtilTest, DeserializeExpectPrefixTrue_CommandRequest) {
auto dbName =
DatabaseNameUtil::deserializeForCommands(boost::none, dbnPrefixString, ctxt_noTenantId);
ASSERT_EQ(dbName.tenantId(), tenantId);
- ASSERT_EQ(dbName.toString(), dbnString);
+ ASSERT_EQ(dbName.toString_forTest(), dbnString);
}
{ // No prefix, has tenantId. *** we shouldn't see this from Atlas Proxy
@@ -414,7 +454,7 @@ TEST(DatabaseNameUtilTest, DeserializeExpectPrefixTrue_CommandRequest) {
auto dbName =
DatabaseNameUtil::deserializeForCommands(tenantId, dbnPrefixString, ctxt_withTenantId);
ASSERT_EQ(dbName.tenantId(), tenantId);
- ASSERT_EQ(dbName.toString(), dbnString);
+ ASSERT_EQ(dbName.toString_forTest(), dbnString);
}
}