forked from scylladb/scylladb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuuid.cc
72 lines (63 loc) · 2.01 KB
/
uuid.cc
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
/*
* Copyright (C) 2015-present ScyllaDB
*/
/*
* SPDX-License-Identifier: LicenseRef-ScyllaDB-Source-Available-1.0
*/
#include "UUID.hh"
#include <seastar/net/byteorder.hh>
#include <random>
#include <boost/iterator/function_input_iterator.hpp>
#include <boost/algorithm/string.hpp>
#include <string>
#include <fmt/ostream.h>
#include <seastar/core/format.hh>
#include <seastar/core/sstring.hh>
#include "marshal_exception.hh"
namespace utils {
UUID
make_random_uuid() noexcept {
static thread_local std::mt19937_64 engine(std::random_device().operator()());
static thread_local std::uniform_int_distribution<uint64_t> dist;
uint64_t msb, lsb;
msb = dist(engine);
lsb = dist(engine);
msb &= ~uint64_t(0x0f << 12);
msb |= 0x4 << 12; // version 4
lsb &= ~(uint64_t(0x3) << 62);
lsb |= uint64_t(0x2) << 62; // IETF variant
return UUID(msb, lsb);
}
std::ostream& operator<<(std::ostream& out, const UUID& uuid) {
fmt::print(out, "{}", uuid);
return out;
}
UUID::UUID(std::string_view uuid) {
sstring uuid_string(uuid.begin(), uuid.end());
boost::erase_all(uuid_string, "-");
auto size = uuid_string.size() / 2;
if (size != 16) {
throw marshal_exception(seastar::format("UUID string size mismatch: '{}'", uuid));
}
sstring most = sstring(uuid_string.begin(), uuid_string.begin() + size);
sstring least = sstring(uuid_string.begin() + size, uuid_string.end());
int base = 16;
try {
std::size_t pos = 0;
this->most_sig_bits = std::stoull(most, &pos, base);
if (pos != most.size()) {
throw std::invalid_argument("");
}
this->least_sig_bits = std::stoull(least, &pos, base);
if (pos != least.size()) {
throw std::invalid_argument("");
}
} catch (const std::logic_error&) {
throw marshal_exception(seastar::format("invalid UUID: '{}'", uuid));
}
}
uint32_t uuid_xor_to_uint32(const UUID& uuid) {
size_t h = std::hash<utils::UUID>{}(uuid);
return uint32_t(h);
}
}