aboutsummaryrefslogtreecommitdiffstats
path: root/util/generic/guid.h
diff options
context:
space:
mode:
authorDevtools Arcadia <arcadia-devtools@yandex-team.ru>2022-02-07 18:08:42 +0300
committerDevtools Arcadia <arcadia-devtools@mous.vla.yp-c.yandex.net>2022-02-07 18:08:42 +0300
commit1110808a9d39d4b808aef724c861a2e1a38d2a69 (patch)
treee26c9fed0de5d9873cce7e00bc214573dc2195b7 /util/generic/guid.h
downloadydb-1110808a9d39d4b808aef724c861a2e1a38d2a69.tar.gz
intermediate changes
ref:cde9a383711a11544ce7e107a78147fb96cc4029
Diffstat (limited to 'util/generic/guid.h')
-rw-r--r--util/generic/guid.h79
1 files changed, 79 insertions, 0 deletions
diff --git a/util/generic/guid.h b/util/generic/guid.h
new file mode 100644
index 0000000000..2bf6c8ad99
--- /dev/null
+++ b/util/generic/guid.h
@@ -0,0 +1,79 @@
+#pragma once
+
+#include "fwd.h"
+
+#include <util/str_stl.h>
+
+/**
+ * UUID generation
+ *
+ * NOTE: It is not a real GUID (RFC 4122), as described in
+ * https://en.wikipedia.org/wiki/Universally_unique_identifier
+ * https://en.wikipedia.org/wiki/Globally_unique_identifier
+ *
+ * See https://clubs.at.yandex-team.ru/stackoverflow/10238/10240
+ * and https://st.yandex-team.ru/IGNIETFERRO-768 for details.
+ */
+struct TGUID {
+ ui32 dw[4] = {};
+
+ constexpr bool IsEmpty() const noexcept {
+ return (dw[0] | dw[1] | dw[2] | dw[3]) == 0;
+ }
+
+ constexpr explicit operator bool() const noexcept {
+ return !IsEmpty();
+ }
+
+ // xxxx-xxxx-xxxx-xxxx
+ TString AsGuidString() const;
+
+ /**
+ * RFC4122 GUID, which described in
+ * https://en.wikipedia.org/wiki/Universally_unique_identifier
+ * xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
+ **/
+ TString AsUuidString() const;
+
+ static TGUID Create();
+
+ /**
+ * Generate time based UUID version 1 RFC4122 GUID
+ * https://datatracker.ietf.org/doc/html/rfc4122#section-4.1
+ **/
+ static TGUID CreateTimebased();
+};
+
+constexpr bool operator==(const TGUID& a, const TGUID& b) noexcept {
+ return a.dw[0] == b.dw[0] && a.dw[1] == b.dw[1] && a.dw[2] == b.dw[2] && a.dw[3] == b.dw[3];
+}
+
+constexpr bool operator!=(const TGUID& a, const TGUID& b) noexcept {
+ return !(a == b);
+}
+
+struct TGUIDHash {
+ constexpr int operator()(const TGUID& a) const noexcept {
+ return a.dw[0] + a.dw[1] + a.dw[2] + a.dw[3];
+ }
+};
+
+template <>
+struct THash<TGUID> {
+ constexpr size_t operator()(const TGUID& g) const noexcept {
+ return (unsigned int)TGUIDHash()(g);
+ }
+};
+
+void CreateGuid(TGUID* res);
+TString GetGuidAsString(const TGUID& g);
+TString CreateGuidAsString();
+TGUID GetGuid(TStringBuf s);
+bool GetGuid(TStringBuf s, TGUID& result);
+
+/**
+* Functions for correct parsing RFC4122 GUID, which described in
+* https://en.wikipedia.org/wiki/Universally_unique_identifier
+**/
+TGUID GetUuid(TStringBuf s);
+bool GetUuid(TStringBuf s, TGUID& result);