Debian Codegraph @

src/ulid_transform/ulid_struct.hh

raw symbols SCIP
1 #ifndef ULID_STRUCT_HH
2 #define ULID_STRUCT_HH
3
4 #include <array>
5 #include <chrono>
6 #include <cstdlib>
7 #include <ctime>
8 #include <functional>
9 #include <random>
10 #include <thread>
11 #include <vector>
12
13 #include "splitmix64.hh"
14 #include "ulid_base32.hh"
15
16 namespace ulid {
17
18 /**
19 * ULID is a 16 byte Universally Unique Lexicographically Sortable Identifier
20 * */
21 struct ULID {
22 uint8_t data[16];
23 };
24
25 /**
26 * EncodeTimestamp will encode the int64_t timestamp to the passed ulid
27 * */
28 inline void EncodeTimestamp(int64_t timestamp, ULID& ulid)
29 {
30 ulid.data[0] = static_cast<uint8_t>(timestamp >> 40);
31 ulid.data[1] = static_cast<uint8_t>(timestamp >> 32);
32 ulid.data[2] = static_cast<uint8_t>(timestamp >> 24);
33 ulid.data[3] = static_cast<uint8_t>(timestamp >> 16);
34 ulid.data[4] = static_cast<uint8_t>(timestamp >> 8);
35 ulid.data[5] = static_cast<uint8_t>(timestamp);
36 }
37
38 /**
39 * EncodeTime will encode the time point to the passed ulid
40 * */
41 inline void EncodeTime(std::chrono::time_point<std::chrono::system_clock> time_point, ULID& ulid)
42 {
43 auto time_ms = std::chrono::time_point_cast<std::chrono::milliseconds>(time_point);
44 int64_t timestamp = time_ms.time_since_epoch().count();
45 EncodeTimestamp(timestamp, ulid);
46 }
47
48 /**
49 * EncodeTimeSystemClockNow will encode a ULID using the time obtained using
50 * std::chrono::system_clock::now() by taking the timestamp in milliseconds.
51 * */
52 inline void EncodeTimeSystemClockNow(ULID& ulid)
53 {
54 EncodeTime(std::chrono::system_clock::now(), ulid);
55 }
56
57 /**
58 * EncodeEntropyFast will encode using SplitMix64
59 * with only 2 generated values.
60 * */
61 inline void EncodeEntropyFast(ULID& ulid)
62 {
63 static thread_local SplitMix64 gen([]() {
64 // Use multiple entropy sources for seeding
65 uint64_t seed = static_cast<uint64_t>(std::chrono::high_resolution_clock::now().time_since_epoch().count());
66 seed ^= static_cast<uint64_t>(std::random_device { }()) << 32;
67 seed ^= static_cast<uint64_t>(std::random_device { }());
68 return seed;
69 }());
70 uint64_t high = gen();
71 uint64_t low = gen();
72 ulid.data[6] = (high >> 56) & 0xFF;
73 ulid.data[7] = (high >> 48) & 0xFF;
74 ulid.data[8] = (high >> 40) & 0xFF;
75 ulid.data[9] = (high >> 32) & 0xFF;
76 ulid.data[10] = (high >> 24) & 0xFF;
77 ulid.data[11] = (high >> 16) & 0xFF;
78 ulid.data[12] = (high >> 8) & 0xFF;
79 ulid.data[13] = high & 0xFF;
80 ulid.data[14] = (low >> 8) & 0xFF;
81 ulid.data[15] = low & 0xFF;
82 }
83
84 /**
85 * MarshalTo will marshal a ULID to the passed character array.
86 *
87 * Implementation taken directly from oklog/ulid
88 * (https://sourcegraph.com/github.com/oklog/ulid@0774f81f6e44af5ce5e91c8d7d76cf710e889ebb/-/blob/ulid.go#L162-190)
89 *
90 * timestamp:<br>
91 * dst[0]: first 3 bits of data[0]<br>
92 * dst[1]: last 5 bits of data[0]<br>
93 * dst[2]: first 5 bits of data[1]<br>
94 * dst[3]: last 3 bits of data[1] + first 2 bits of data[2]<br>
95 * dst[4]: bits 3-7 of data[2]<br>
96 * dst[5]: last bit of data[2] + first 4 bits of data[3]<br>
97 * dst[6]: last 4 bits of data[3] + first bit of data[4]<br>
98 * dst[7]: bits 2-6 of data[4]<br>
99 * dst[8]: last 2 bits of data[4] + first 3 bits of data[5]<br>
100 * dst[9]: last 5 bits of data[5]<br>
101 *
102 * entropy:
103 * follows similarly, except now all components are set to 5 bits.
104 * */
105 inline void MarshalTo(const ULID& ulid, char dst[26])
106 {
107 EncodeBase32From(ulid.data, dst);
108 }
109
110 /**
111 * MarshalBinaryTo will Marshal a ULID to the passed byte array
112 * */
113 inline void MarshalBinaryTo(const ULID& ulid, uint8_t dst[16])
114 {
115 // timestamp
116 dst[0] = ulid.data[0];
117 dst[1] = ulid.data[1];
118 dst[2] = ulid.data[2];
119 dst[3] = ulid.data[3];
120 dst[4] = ulid.data[4];
121 dst[5] = ulid.data[5];
122
123 // entropy
124 dst[6] = ulid.data[6];
125 dst[7] = ulid.data[7];
126 dst[8] = ulid.data[8];
127 dst[9] = ulid.data[9];
128 dst[10] = ulid.data[10];
129 dst[11] = ulid.data[11];
130 dst[12] = ulid.data[12];
131 dst[13] = ulid.data[13];
132 dst[14] = ulid.data[14];
133 dst[15] = ulid.data[15];
134 }
135
136 /**
137 * UnmarshalFrom will unmarshal a ULID from the passed character array.
138 * */
139 inline void UnmarshalFrom(const char str[26], ULID& ulid)
140 {
141 DecodeBase32To(str, ulid.data);
142 }
143
144 /**
145 * UnmarshalBinaryFrom will unmarshal a ULID from the passed byte array.
146 * */
147 inline void UnmarshalBinaryFrom(const uint8_t b[16], ULID& ulid)
148 {
149 // timestamp
150 ulid.data[0] = b[0];
151 ulid.data[1] = b[1];
152 ulid.data[2] = b[2];
153 ulid.data[3] = b[3];
154 ulid.data[4] = b[4];
155 ulid.data[5] = b[5];
156
157 // entropy
158 ulid.data[6] = b[6];
159 ulid.data[7] = b[7];
160 ulid.data[8] = b[8];
161 ulid.data[9] = b[9];
162 ulid.data[10] = b[10];
163 ulid.data[11] = b[11];
164 ulid.data[12] = b[12];
165 ulid.data[13] = b[13];
166 ulid.data[14] = b[14];
167 ulid.data[15] = b[15];
168 }
169
170 }; // namespace ulid
171
172 #endif // ULID_STRUCT_HH