Files
scylladb/utils/uuid.cc
Avi Kivity fcb8d040e8 treewide: use Software Package Data Exchange (SPDX) license identifiers
Instead of lengthy blurbs, switch to single-line, machine-readable
standardized (https://spdx.dev) license identifiers. The Linux kernel
switched long ago, so there is strong precedent.

Three cases are handled: AGPL-only, Apache-only, and dual licensed.
For the latter case, I chose (AGPL-3.0-or-later and Apache-2.0),
reasoning that our changes are extensive enough to apply our license.

The changes we applied mechanically with a script, except to
licenses/README.md.

Closes #9937
2022-01-18 12:15:18 +01:00

55 lines
1.5 KiB
C++

/*
* Copyright (C) 2015-present ScyllaDB
*/
/*
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
#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 <seastar/core/sstring.hh>
#include "utils/serialization.hh"
#include "marshal_exception.hh"
namespace utils {
UUID
make_random_uuid() {
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) {
return out << uuid.to_sstring();
}
UUID::UUID(sstring_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(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;
this->most_sig_bits = std::stoull(most, nullptr, base);
this->least_sig_bits = std::stoull(least, nullptr, base);
}
}