summaryrefslogtreecommitdiff
path: root/src/mongo/db
diff options
context:
space:
mode:
authorSamantha Ritter <samantha.ritter@10gen.com>2017-05-04 11:27:52 -0400
committersamantharitter <samantha.ritter@10gen.com>2017-05-05 11:53:30 -0400
commit8894454fedc4faa9b41858ea5349ae2181239258 (patch)
tree4c4295beced74344bb68f2d8de7a784ae36dfb0f /src/mongo/db
parent7ab97aaaf42b91736afd61ed7bfe684d393c89c2 (diff)
downloadmongo-8894454fedc4faa9b41858ea5349ae2181239258.tar.gz
SERVER-28297 Add IDL to LogicalSessionId class
Diffstat (limited to 'src/mongo/db')
-rw-r--r--src/mongo/db/SConscript2
-rw-r--r--src/mongo/db/logical_session_id.cpp29
-rw-r--r--src/mongo/db/logical_session_id.h31
-rw-r--r--src/mongo/db/logical_session_id.idl43
-rw-r--r--src/mongo/db/logical_session_id_test.cpp27
5 files changed, 125 insertions, 7 deletions
diff --git a/src/mongo/db/SConscript b/src/mongo/db/SConscript
index daf2022a7ef..c171f4ffd87 100644
--- a/src/mongo/db/SConscript
+++ b/src/mongo/db/SConscript
@@ -890,8 +890,10 @@ env.Library(
target='logical_session_id',
source=[
'logical_session_id.cpp',
+ env.Idlc('logical_session_id.idl')[0],
],
LIBDEPS=[
+ '$BUILD_DIR/mongo/idl/idl_parser',
'$BUILD_DIR/mongo/util/uuid',
'$BUILD_DIR/mongo/base',
],
diff --git a/src/mongo/db/logical_session_id.cpp b/src/mongo/db/logical_session_id.cpp
index 53450eab7e4..c26b8753b90 100644
--- a/src/mongo/db/logical_session_id.cpp
+++ b/src/mongo/db/logical_session_id.cpp
@@ -30,11 +30,23 @@
#include "mongo/db/logical_session_id.h"
+#include "mongo/bson/bsonobjbuilder.h"
+#include "mongo/db/logical_session_id_gen.h"
#include "mongo/util/assert_util.h"
namespace mongo {
-LogicalSessionId::LogicalSessionId(UUID id) : _id(id) {}
+LogicalSessionId::LogicalSessionId() {
+ setId(UUID::gen());
+}
+
+LogicalSessionId::LogicalSessionId(UUID id) {
+ setId(std::move(id));
+}
+
+LogicalSessionId LogicalSessionId::gen() {
+ return {UUID::gen()};
+}
StatusWith<LogicalSessionId> LogicalSessionId::parse(const TxnId& txnId) {
// TODO: the TxnId class is not yet implemented.
@@ -50,8 +62,21 @@ StatusWith<LogicalSessionId> LogicalSessionId::parse(const std::string& s) {
return LogicalSessionId{std::move(res.getValue())};
}
+LogicalSessionId LogicalSessionId::parse(const BSONObj& doc) {
+ IDLParserErrorContext ctx("logical session id");
+ LogicalSessionId lsid;
+ lsid.parseProtected(ctx, doc);
+ return lsid;
+}
+
+BSONObj LogicalSessionId::toBSON() const {
+ BSONObjBuilder builder;
+ serialize(&builder);
+ return builder.obj();
+}
+
std::string LogicalSessionId::toString() const {
- return _id.toString();
+ return getId().toString();
}
} // namespace mongo
diff --git a/src/mongo/db/logical_session_id.h b/src/mongo/db/logical_session_id.h
index 20e6d8e9547..50697ee4e0b 100644
--- a/src/mongo/db/logical_session_id.h
+++ b/src/mongo/db/logical_session_id.h
@@ -31,18 +31,26 @@
#include <string>
#include "mongo/base/status_with.h"
+#include "mongo/db/logical_session_id_gen.h"
#include "mongo/util/uuid.h"
namespace mongo {
+class BSONObjBuilder;
class TxnId;
/**
* A 128-bit identifier for a logical session.
*/
-class LogicalSessionId {
+class LogicalSessionId : public Logical_session_id {
public:
- LogicalSessionId() = delete;
+ friend class Logical_session_id;
+ friend class Logical_session_record;
+
+ /**
+ * Create and return a new LogicalSessionId with a random UUID.
+ */
+ static LogicalSessionId gen();
/**
* Construct a new LogicalSessionId out of a txnId received with an operation.
@@ -56,12 +64,22 @@ public:
static StatusWith<LogicalSessionId> parse(const std::string& s);
/**
+ * Constructs a new LogicalSessionId out of a BSONObj. For IDL.
+ */
+ static LogicalSessionId parse(const BSONObj& doc);
+
+ /**
* Returns a string representation of this session id.
*/
std::string toString() const;
+ /**
+ * Serialize this object to BSON.
+ */
+ BSONObj toBSON() const;
+
inline bool operator==(const LogicalSessionId& rhs) const {
- return _id == rhs._id;
+ return getId() == rhs.getId();
}
inline bool operator!=(const LogicalSessionId& rhs) const {
@@ -75,7 +93,7 @@ public:
*/
struct Hash {
std::size_t operator()(const LogicalSessionId& lsid) const {
- return _hasher(lsid._id);
+ return _hasher(lsid.getId());
}
private:
@@ -84,7 +102,10 @@ public:
private:
- UUID _id;
+ /**
+ * This constructor exists for IDL only.
+ */
+ LogicalSessionId();
/**
* Construct a LogicalSessionId from a UUID.
diff --git a/src/mongo/db/logical_session_id.idl b/src/mongo/db/logical_session_id.idl
new file mode 100644
index 00000000000..6e04d948ca7
--- /dev/null
+++ b/src/mongo/db/logical_session_id.idl
@@ -0,0 +1,43 @@
+# Copyright (C) 2017 MongoDB Inc.
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License, version 3,
+# as published by the Free Software Foundation.
+#
+# 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
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+#
+
+# This IDL file describes the BSON format for a LogicalSessionId, and
+# handles the serialization to and deserialization from its BSON representation
+# for that class.
+
+global:
+ cpp_namespace: "mongo"
+ cpp_includes:
+ - "mongo/util/uuid.h"
+
+imports:
+ - "mongo/idl/basic_types.idl"
+
+types:
+
+ UUIDIDL:
+ description: "IDL representation of the UUID type"
+ bson_serialization_type: object
+ cpp_type: "mongo::UUID"
+ deserializer: "mongo::UUID::parse"
+ serializer: "mongo::UUID::toBSON"
+
+structs:
+
+ logical_session_id:
+ description: "A struct representing a LogicalSessionId"
+ strict: true
+ fields:
+ id: UUIDIDL
diff --git a/src/mongo/db/logical_session_id_test.cpp b/src/mongo/db/logical_session_id_test.cpp
index 059a18fe8b3..13f3375ab33 100644
--- a/src/mongo/db/logical_session_id_test.cpp
+++ b/src/mongo/db/logical_session_id_test.cpp
@@ -30,6 +30,10 @@
#include <string>
+#include "mongo/bson/bsonmisc.h"
+#include "mongo/bson/bsonobj.h"
+#include "mongo/bson/bsonobjbuilder.h"
+#include "mongo/bson/bsontypes.h"
#include "mongo/db/logical_session_id.h"
#include "mongo/unittest/unittest.h"
#include "mongo/util/uuid.h"
@@ -53,5 +57,28 @@ TEST(LogicalSessionIdTest, ToAndFromStringTest) {
ASSERT(!res.isOK());
}
+TEST(LogicalSessionIdTest, FromBSONTest) {
+ auto uuid = UUID::gen();
+ auto bson = BSON("id" << uuid.toBSON());
+
+ auto lsid = LogicalSessionId::parse(bson);
+ ASSERT_EQUALS(lsid.toString(), uuid.toString());
+
+ // Dump back to BSON, make sure we get the same thing
+ auto bsonDump = lsid.toBSON();
+ ASSERT_EQ(bsonDump.woCompare(bson), 0);
+
+ // Try parsing mal-formatted bson objs
+ ASSERT_THROWS(LogicalSessionId::parse(BSON("hi"
+ << "there")),
+ UserException);
+
+ // TODO: use these and add more once there is bindata
+ ASSERT_THROWS(LogicalSessionId::parse(BSON("id"
+ << "not a session id!")),
+ UserException);
+ ASSERT_THROWS(LogicalSessionId::parse(BSON("id" << 14)), UserException);
+}
+
} // namespace
} // namespace mongo