1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
module;
// Might as well since we're using OpenSSL
#include <openssl/err.h>
#include <openssl/rand.h>
module routemon:trace$impl;
import :trace;
namespace routemon::trace {
uuid7::uuid7()
{
namespace chrono = std::chrono;
auto const unix_time_ms_signed = static_cast<std::int64_t>(
chrono::duration_cast<chrono::milliseconds>(
chrono::system_clock::now().time_since_epoch())
.count());
if (unix_time_ms_signed < 0)
throw std::runtime_error{"system time before UNIX epoch"};
auto const unix_time_ms = static_cast<std::uint64_t>(unix_time_ms_signed);
if (std::countl_zero(unix_time_ms) < 16)
throw std::runtime_error{"system time too great"};
auto rand = std::array<unsigned char, 10>{};
int s = RAND_bytes(rand.data(), static_cast<int>(rand.size()));
if (s != 1)
{
unsigned long e = ERR_get_error();
throw std::runtime_error{std::format(
"failed to generate UUID(v7): {} ({}, code {})",
ERR_reason_error_string(e), ERR_lib_error_string(e), e)};
}
auto version = std::uint64_t{0b0111};
auto variant = std::uint64_t{0b10};
hi_ |= unix_time_ms << 16;
hi_ |= version << 12;
hi_ |= std::uint64_t{rand[0]} << 4;
hi_ |= std::uint64_t{rand[1]};
lo_ |= variant << 62;
lo_ |= std::uint64_t{rand[2]} << 54;
lo_ |= std::uint64_t{rand[3]} << 48;
lo_ |= std::uint64_t{rand[4]} << 40;
lo_ |= std::uint64_t{rand[5]} << 32;
lo_ |= std::uint64_t{rand[6]} << 24;
lo_ |= std::uint64_t{rand[7]} << 16;
lo_ |= std::uint64_t{rand[8]} << 8;
lo_ |= std::uint64_t{rand[9]};
}
auto uuid7::format(std::array<char, 37>& target) -> void
{
auto p____hi_hi = (hi_ & 0xffff'ffff'0000'0000) >> 32;
auto p_hi_lo_hi = (hi_ & 0x0000'0000'ffff'0000) >> 16;
auto p_lo_lo_hi = (hi_ & 0x0000'0000'0000'ffff) >> 0;
auto p____hi_lo = (lo_ & 0xffff'0000'0000'0000) >> 48;
auto p____lo_lo = (lo_ & 0x0000'ffff'ffff'ffff) >> 0;
std::format_to(
target.begin(), "{:0>8x}-{:0>4x}-{:0>4x}-{:0>4x}-{:0>12x}", p____hi_hi,
p_hi_lo_hi, p_lo_lo_hi, p____hi_lo, p____lo_lo);
target.back() = '\0';
}
id::id() { uuid7{}.format(chars_); }
auto id::as_string() const -> util::zstring_view
{
return util::zstring_view{chars_.data(), chars_.size() - 1};
}
} // namespace routemon::trace
|