summaryrefslogtreecommitdiff
path: root/src/mongo/db/tenant_id.h
diff options
context:
space:
mode:
authorSophia Tan <sophia_tll@hotmail.com>2022-01-20 19:18:49 +0000
committerEvergreen Agent <no-reply@evergreen.mongodb.com>2022-01-20 21:36:30 +0000
commit0f90f936d206032693578db712376489619c0d62 (patch)
tree8b6f973a9dcfeff7d39ffddb2441e43d3873a43f /src/mongo/db/tenant_id.h
parent08aa07493315218bb47578ec77c94f2d728fb347 (diff)
downloadmongo-0f90f936d206032693578db712376489619c0d62.tar.gz
SERVER-62441 Define a new type mongo::TenantId for tenant id
Diffstat (limited to 'src/mongo/db/tenant_id.h')
-rw-r--r--src/mongo/db/tenant_id.h152
1 files changed, 152 insertions, 0 deletions
diff --git a/src/mongo/db/tenant_id.h b/src/mongo/db/tenant_id.h
new file mode 100644
index 00000000000..7af08a45d95
--- /dev/null
+++ b/src/mongo/db/tenant_id.h
@@ -0,0 +1,152 @@
+/**
+ * Copyright (C) 2022-present MongoDB, Inc.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the Server Side Public License, version 1,
+ * as published by MongoDB, Inc.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * Server Side Public License for more details.
+ *
+ * You should have received a copy of the Server Side Public License
+ * along with this program. If not, see
+ * <http://www.mongodb.com/licensing/server-side-public-license>.
+ *
+ * As a special exception, the copyright holders give permission to link the
+ * code of portions of this program with the OpenSSL library under certain
+ * conditions as described in each individual source file and distribute
+ * linked combinations including the program with the OpenSSL library. You
+ * must comply with the Server Side Public License in all respects for
+ * all of the code used other than as permitted herein. If you modify file(s)
+ * with this exception, you may extend this exception to your version of the
+ * file(s), but you are not obligated to do so. If you do not wish to do so,
+ * delete this exception statement from your version. If you delete this
+ * exception statement from all source files in the program, then also delete
+ * it in the license file.
+ */
+
+#pragma once
+
+#include <ostream>
+#include <string>
+
+#include "mongo/base/string_data.h"
+#include "mongo/bson/bsonelement.h"
+#include "mongo/bson/bsonmisc.h"
+#include "mongo/bson/bsonobjbuilder.h"
+#include "mongo/bson/oid.h"
+#include "mongo/bson/util/builder.h"
+
+namespace mongo {
+
+/**
+ * Representation of a tenant identifier.
+ */
+class TenantId {
+public:
+ /**
+ * kSystemTenantId must be unique across all possible tenant IDs.
+ * Since the first four bytes of an OID are a unix epoch timestamp,
+ * we can simply select a value prior to the inception of MongoDB,
+ * and be guaranteed to never have a collision with a value
+ * produced by OID::gen().
+ */
+ static const TenantId kSystemTenantId;
+
+ explicit TenantId(const OID& oid) : _oid(oid), _idStr(oid.toString()) {}
+
+ TenantId() = delete;
+
+ const std::string& toString() const {
+ return _idStr;
+ }
+
+ /**
+ * Returns -1, 0, or 1 if 'this' is less, equal, or greater than 'other' in
+ * lexicographical order.
+ */
+ int compare(const TenantId& other) const {
+ return _oid.compare(other._oid);
+ }
+
+ std::size_t hash() const {
+ return OID::Hasher()(_oid);
+ }
+
+ /**
+ * Functor compatible with std::hash for std::unordered_{map,set}
+ */
+ struct Hasher {
+ std::size_t operator()(const TenantId& tenantId) const {
+ return tenantId.hash();
+ }
+ };
+
+ /**
+ * Hash function compatible with absl::Hash for absl::unordered_{map,set}
+ */
+ template <typename H>
+ friend H AbslHashValue(H h, const TenantId& tenantId) {
+ return H::combine(std::move(h), tenantId.hash());
+ }
+
+ /**
+ * Parse tenant id from BSON. The function is used by IDL parsers.
+ */
+ static TenantId parseFromBSON(const BSONElement& elem);
+
+ /**
+ * Serialize tenant id to BSON. These functions are used by IDL parsers.
+ */
+ void serializeToBSON(StringData fieldName, BSONObjBuilder* builder) const;
+ void serializeToBSON(BSONArrayBuilder* builder) const;
+
+private:
+ OID _oid;
+ std::string _idStr;
+};
+
+inline bool operator==(const TenantId& lhs, const TenantId& rhs) {
+ return lhs.compare(rhs) == 0;
+}
+
+inline bool operator!=(const TenantId& lhs, const TenantId& rhs) {
+ return !(lhs == rhs);
+}
+
+inline bool operator<(const TenantId& lhs, const TenantId& rhs) {
+ return lhs.compare(rhs) < 0;
+}
+
+inline bool operator>(const TenantId& lhs, const TenantId& rhs) {
+ return rhs < lhs;
+}
+
+inline bool operator<=(const TenantId& lhs, const TenantId& rhs) {
+ return !(lhs > rhs);
+}
+
+inline bool operator>=(const TenantId& lhs, const TenantId& rhs) {
+ return !(lhs < rhs);
+}
+
+inline std::ostream& operator<<(std::ostream& os, const TenantId& tenantId) {
+ return os << tenantId.toString();
+}
+
+template <typename Allocator>
+StringBuilderImpl<Allocator>& operator<<(StringBuilderImpl<Allocator>& stream,
+ const TenantId& tenantId) {
+ return stream << tenantId.toString();
+}
+
+/**
+ * Supports use of TenantId with the BSON macro:
+ * BSON("tenant" << tenantId)
+ */
+template <>
+BSONObjBuilder& BSONObjBuilderValueStream::operator<<<TenantId>(TenantId value);
+
+} // namespace mongo