载入中...
搜索中...
未找到
Identity.cpp
浏览该文件的文档.
1#include "common/Identity.h"
2#include "common/UuidV7.h"
3
4#include <utility>
5
6namespace eve {
7
8namespace {
9
10[[nodiscard]] bool isLogicalName(std::string_view value) noexcept {
11 if (value.empty()) return false;
12 for (const char character : value) {
13 const bool alpha = (character >= 'a' && character <= 'z') || (character >= 'A' && character <= 'Z');
14 const bool digit = character >= '0' && character <= '9';
15 if (!(alpha || digit || character == '_' || character == '-' || character == '.')) {
16 return false;
17 }
18 }
19 const char first = value.front();
20 return (first >= 'a' && first <= 'z') || (first >= 'A' && first <= 'Z') || (first >= '0' && first <= '9');
21}
22
23[[nodiscard]] bool isLogicalNamespace(std::string_view value) noexcept {
24 if (value.empty()) return false;
25 for (const char character : value) {
26 const bool lower = character >= 'a' && character <= 'z';
27 const bool digit = character >= '0' && character <= '9';
28 if (!(lower || digit || character == '_' || character == '-' || character == '.')) {
29 return false;
30 }
31 }
32 return value.front() >= 'a' && value.front() <= 'z';
33}
34
35} // namespace
36
37std::optional<LogicalId> LogicalId::parse(std::string_view text) {
38 const auto separator = text.find(':');
39 if (separator == std::string_view::npos || text.find(':', separator + 1) != std::string_view::npos) {
40 return std::nullopt;
41 }
42
43 const auto namespaceName = text.substr(0, separator);
44 const auto name = text.substr(separator + 1);
45 if (!isLogicalNamespace(namespaceName) || !isLogicalName(name)) return std::nullopt;
46 return LogicalId(std::string(text));
47}
48
49std::optional<LogicalId> LogicalId::fromParts(std::string_view namespaceName, std::string_view name) {
50 if (!isLogicalNamespace(namespaceName) || !isLogicalName(name)) return std::nullopt;
51 std::string value;
52 value.reserve(namespaceName.size() + name.size() + 1);
53 value.append(namespaceName);
54 value.push_back(':');
55 value.append(name);
56 return LogicalId(std::move(value));
57}
58
59std::string_view LogicalId::namespaceName() const noexcept {
60 const auto separator = value_.find(':');
61 return separator == std::string::npos ? std::string_view{} : std::string_view(value_).substr(0, separator);
62}
63
64std::string_view LogicalId::name() const noexcept {
65 const auto separator = value_.find(':');
66 return separator == std::string::npos ? std::string_view{} : std::string_view(value_).substr(separator + 1);
67}
68
69std::uint64_t LogicalId::hash() const noexcept {
70 std::uint64_t result = 14695981039346656037ull;
71 for (const auto character : value_) {
72 result ^= static_cast<std::uint8_t>(character);
73 result *= 1099511628211ull;
74 }
75 return result;
76}
77
79 : entropy_(std::move(entropy)), clock_(std::move(clock)) {}
80
81std::optional<PersistentId> UuidV7Generator::generate() const {
82 const auto timestamp = clock_ ? clock_() : std::chrono::system_clock::now();
83 return generate(timestamp);
84}
85
86std::optional<PersistentId> UuidV7Generator::generate(std::chrono::system_clock::time_point timestamp) const {
87 return generateUuidV7(timestamp, entropy_);
88}
89
90std::optional<PersistentId> generateUuidV7(std::chrono::system_clock::time_point timestamp,
91 const UuidEntropySource& entropy) {
92 if (!entropy) return std::nullopt;
93
94 if (timestamp.time_since_epoch() < std::chrono::system_clock::duration::zero()) return std::nullopt;
95 const auto milliseconds =
96 std::chrono::duration_cast<std::chrono::milliseconds>(timestamp.time_since_epoch()).count();
97 constexpr std::uint64_t maxTimestamp = (std::uint64_t{1} << 48u) - 1u;
98 if (milliseconds < 0 || static_cast<std::uint64_t>(milliseconds) > maxTimestamp) {
99 return std::nullopt;
100 }
101
103 if (!entropy(std::span<std::uint8_t>(bytes).subspan(6))) return std::nullopt;
104
105 auto timestampValue = static_cast<std::uint64_t>(milliseconds);
106 for (std::size_t index = 6; index > 0; --index) {
107 bytes[index - 1] = static_cast<std::uint8_t>(timestampValue & 0xffu);
108 timestampValue >>= 8u;
109 }
110
111 bytes[6] = static_cast<std::uint8_t>(0x70u | (bytes[6] & 0x0fu));
112 bytes[8] = static_cast<std::uint8_t>(0x80u | (bytes[8] & 0x3fu));
113 return PersistentId(bytes);
114}
115
116// ---- Tag-free UUID core ---------------------------------------------------
117//
118// One copy of each algorithm, shared by every eve::detail::Id128<Tag>
119// instantiation. The tagged methods are inline one-line forwards, so adding a
120// domain tag no longer duplicates the parser, the formatter or the derivation.
121
122namespace detail {
123
124namespace {
125
126[[nodiscard]] constexpr std::optional<std::uint8_t> hexValue(char value) noexcept {
127 if (value >= '0' && value <= '9') return static_cast<std::uint8_t>(value - '0');
128 if (value >= 'a' && value <= 'f') return static_cast<std::uint8_t>(value - 'a' + 10);
129 if (value >= 'A' && value <= 'F') return static_cast<std::uint8_t>(value - 'A' + 10);
130 return std::nullopt;
131}
132
133} // namespace
134
135std::optional<UuidBytes> parseUuidText(std::string_view text) noexcept {
136 if (text.size() != 36 || text[8] != '-' || text[13] != '-' || text[18] != '-' || text[23] != '-') {
137 return std::nullopt;
138 }
139
141 std::size_t byteIndex = 0;
142 for (std::size_t i = 0; i < text.size();) {
143 if (text[i] == '-') {
144 ++i;
145 continue;
146 }
147
148 if (i + 1 >= text.size()) return std::nullopt;
149 const auto high = hexValue(text[i]);
150 const auto low = hexValue(text[i + 1]);
151 if (!high || !low || byteIndex >= bytes.size()) {
152 return std::nullopt;
153 }
154 bytes[byteIndex++] = static_cast<std::uint8_t>((*high << 4u) | *low);
155 i += 2;
156 }
157 if (byteIndex != bytes.size()) return std::nullopt;
158 return bytes;
159}
160
161std::string formatUuidBytes(const UuidBytes& bytes) {
162 static constexpr char digits[] = "0123456789abcdef";
163 std::string result;
164 result.reserve(36);
165 for (std::size_t i = 0; i < bytes.size(); ++i) {
166 if (i == 4 || i == 6 || i == 8 || i == 10) result.push_back('-');
167 result.push_back(digits[(bytes[i] >> 4u) & 0x0fu]);
168 result.push_back(digits[bytes[i] & 0x0fu]);
169 }
170 return result;
171}
172
173UuidBytes childUuidBytes(const UuidBytes& parent, std::string_view role) noexcept {
175 std::uint64_t hash = 14695981039346656037ull;
176 for (const unsigned char byte : role) {
177 hash ^= byte;
178 hash *= 1099511628211ull;
179 }
180 for (std::size_t i = 0; i < sizeof(hash); ++i) {
181 result[8 + i] ^= static_cast<std::uint8_t>((hash >> (i * 8u)) & 0xffu);
182 }
183 // Preserve the UUID variant while retaining the parent's namespace.
184 result[8] = static_cast<std::uint8_t>((result[8] & 0x3fu) | 0x80u);
185 return result;
186}
187
188void UuidIdState::assign(std::string_view text) {
189 if (text.empty()) return;
190 if (const auto parsed = parseUuidText(text)) {
191 uuid = *parsed;
192 spelling = formatUuidBytes(*parsed);
193 canonical = true;
194 return;
195 }
196 spelling = text;
198 canonical = false;
199}
200
201UuidBytes UuidIdState::projectLegacy(std::string_view text) noexcept {
202 std::uint64_t first = 14695981039346656037ull;
203 std::uint64_t second = 1099511628211ull;
204 for (const unsigned char byte : text) {
205 first ^= byte;
206 first *= 1099511628211ull;
207 second ^= static_cast<std::uint8_t>(byte + 0x9du);
208 second *= 14029467366897019727ull;
209 }
210
212 for (std::size_t i = 0; i < sizeof(first); ++i) {
213 bytes[i] = static_cast<std::uint8_t>((first >> (i * 8u)) & 0xffu);
214 bytes[8 + i] = static_cast<std::uint8_t>((second >> (i * 8u)) & 0xffu);
215 }
216 // Mark the deterministic projection as UUIDv5-shaped and RFC variant.
217 bytes[6] = static_cast<std::uint8_t>((bytes[6] & 0x0fu) | 0x50u);
218 bytes[8] = static_cast<std::uint8_t>((bytes[8] & 0x3fu) | 0x80u);
219 return bytes;
220}
221
222} // namespace detail
223
224} // namespace eve
std::string name
Definition ECS.cpp:154
std::int32_t second
std::int32_t first
Strong identity values shared by persistence, content and logical-name APIs.
const ColourScope * parent
Poco::JSON::Object::Ptr result
std::uint64_t bytes
double & milliseconds
Definition OnnxGpgpu.cpp:19
Object::Ptr value
std::string hash
int detail
std::string text
uint32_t index
UUIDv7 generation kept separate from lightweight identity values.
static std::optional< LogicalId > fromParts(std::string_view namespaceName, std::string_view name)
Builds a logical ID from separate namespace and name components.
Definition Identity.cpp:49
static std::optional< LogicalId > parse(std::string_view text)
Parses a scoped logical name.
Definition Identity.cpp:37
std::uint64_t hash() const noexcept
Returns a stable non-cryptographic hash for hash tables.
Definition Identity.cpp:69
std::string_view name() const noexcept
Returns the name component as a view into this object.
Definition Identity.cpp:64
std::string_view namespaceName() const noexcept
Returns the namespace component as a view into this object.
Definition Identity.cpp:59
LogicalId()=default
Constructs an empty, invalid logical ID.
std::optional< PersistentId > generate() const
Generates a UUIDv7 using the injected clock.
Definition Identity.cpp:81
UuidV7Generator(UuidEntropySource entropy, UuidClock clock={})
Creates a generator with an injected entropy source and optional clock.
Definition Identity.cpp:78
std::array< std::uint8_t, 16 > Bytes
Definition Identity.h:113
void assign(std::string_view text)
Assigns from canonical UUID text or from a legacy spelling.
Definition Identity.cpp:188
static UuidBytes projectLegacy(std::string_view text) noexcept
Builds the deterministic UUID projection of a legacy spelling.
Definition Identity.cpp:201
std::array< std::uint8_t, 16 > UuidBytes
Byte representation shared by every tagged 128-bit identity.
Definition Identity.h:53
std::string formatUuidBytes(const UuidBytes &bytes)
Formats UUID bytes as lower-case canonical 8-4-4-4-12 text.
Definition Identity.cpp:161
UuidBytes childUuidBytes(const UuidBytes &parent, std::string_view role) noexcept
Derives a deterministic child UUID from a parent and a role.
Definition Identity.cpp:173
std::optional< UuidBytes > parseUuidText(std::string_view text) noexcept
Parses canonical UUID text without knowing the domain tag.
Definition Identity.cpp:135
Build metadata (engine git commit, build time, third-party version).
Definition Build.cpp:16
StrongUuid< detail::PersistentIdTag > PersistentId
Stable instance identity for persistence, networking and process boundaries.
Definition Identity.h:253
std::function< std::chrono::system_clock::time_point()> UuidClock
Clock callback used by the injectable UUIDv7 generator.
Definition UuidV7.h:29
std::function< bool(std::span< std::uint8_t > bytes)> UuidEntropySource
Entropy callback used by UUIDv7 generation.
Definition UuidV7.h:26
std::optional< PersistentId > generateUuidV7(std::chrono::system_clock::time_point timestamp, const UuidEntropySource &entropy)
Constructs a UUIDv7 without owning a clock or generator object.
Definition Identity.cpp:90