diff --git a/src/common/id/CMakeLists.txt b/src/common/id/CMakeLists.txt index e60a8a76ebe..cd04ca131fa 100644 --- a/src/common/id/CMakeLists.txt +++ b/src/common/id/CMakeLists.txt @@ -8,4 +8,9 @@ nebula_add_library( Snowflake.cpp ) +nebula_add_library( + uuid_obj OBJECT + UUID.cpp +) + nebula_add_subdirectory(test) diff --git a/src/common/id/UUID.cpp b/src/common/id/UUID.cpp new file mode 100644 index 00000000000..01212aa587a --- /dev/null +++ b/src/common/id/UUID.cpp @@ -0,0 +1,20 @@ +/* Copyright (c) 2021 vesoft inc. All rights reserved. + * + * This source code is licensed under Apache 2.0 License. + */ + +#include "common/id/UUID.h" + +namespace nebula { + +boost::uuids::uuid UUIDV4::getId() { + boost::uuids::uuid uuid = boost::uuids::random_generator()(); + return uuid; +} + +std::string UUIDV4::getIdStr() { + boost::uuids::uuid uuid = getId(); + return boost::uuids::to_string(uuid); +} + +} // namespace nebula diff --git a/src/common/id/UUID.h b/src/common/id/UUID.h new file mode 100644 index 00000000000..e7dfcc96e46 --- /dev/null +++ b/src/common/id/UUID.h @@ -0,0 +1,28 @@ +/* Copyright (c) 2021 vesoft inc. All rights reserved. + * + * This source code is licensed under Apache 2.0 License. + */ + +#include +#include +#include + +/* + * UUID v1: time-based and mac-based + * UUID v2: DCE security + * UUID v3: name-based MD5 + * UUID v4: random + * UUID v5: name-based SHA-1 + * boost implements uuid v4 and v5(is deprecated due to security concerns) + */ +namespace nebula { + +// the probability to find a duplicate within version-4 UUIDs is one in a billion. +// the computed is in: https://en.wikipedia.org/wiki/Universally_unique_identifier +class UUIDV4 { + public: + static boost::uuids::uuid getId(); + + static std::string getIdStr(); +}; +} // namespace nebula