mirror of
https://github.com/mongodb/mongo.git
synced 2024-12-01 09:32:32 +01:00
SERVER-28297 Add IDL to LogicalSessionId class
This commit is contained in:
parent
7ab97aaaf4
commit
8894454fed
@ -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',
|
||||
],
|
||||
|
@ -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
|
||||
|
@ -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.
|
||||
@ -55,13 +63,23 @@ 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.
|
||||
|
43
src/mongo/db/logical_session_id.idl
Normal file
43
src/mongo/db/logical_session_id.idl
Normal file
@ -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
|
@ -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
|
||||
|
@ -81,6 +81,12 @@ StatusWith<UUID> UUID::parse(const std::string& s) {
|
||||
return UUID{std::move(uuid)};
|
||||
}
|
||||
|
||||
UUID UUID::parse(const BSONObj& obj) {
|
||||
auto res = parse(obj.getField("uuid"));
|
||||
invariant(res.isOK());
|
||||
return res.getValue();
|
||||
}
|
||||
|
||||
bool UUID::isUUIDString(const std::string& s) {
|
||||
return std::regex_match(s, uuidRegex);
|
||||
}
|
||||
|
@ -48,7 +48,10 @@ class UUID {
|
||||
using UUIDStorage = std::array<unsigned char, 16>;
|
||||
|
||||
public:
|
||||
UUID() = delete;
|
||||
/**
|
||||
* This constructor exists for IDL only.
|
||||
*/
|
||||
UUID() = default;
|
||||
|
||||
/**
|
||||
* The number of bytes contained in a UUID.
|
||||
@ -72,6 +75,13 @@ public:
|
||||
*/
|
||||
static StatusWith<UUID> parse(BSONElement from);
|
||||
|
||||
/**
|
||||
* Parse a BSON document of the form { uuid: BinData(4, "...") }.
|
||||
*
|
||||
* For IDL.
|
||||
*/
|
||||
static UUID parse(const BSONObj& obj);
|
||||
|
||||
/**
|
||||
* Returns whether this string represents a valid UUID.
|
||||
*/
|
||||
|
Loading…
Reference in New Issue
Block a user