Convert Cassandra's UUIDGen class, which generates time-dependent UUID, and parts of the java.util.UUID which I thought we need, to C++. It is possible I missed some needed features of java.util.UUID that we'll need to add later. Also, part of the version-1 UUID is supposed to be node-unique (so that if two nodes happen to boot at the same time and get a UUID at exactly the same time, they still get different UUIDs). Cassandra uses for this a hash function of the IP address, we should use in the future the MAC address (from Seastar's network stack). But currently we just use 0. Left a FIXME to fix that. Signed-off-by: Nadav Har'El <nyh@cloudius-systems.com> [avi: add to ./configure.py]
45 lines
980 B
C++
45 lines
980 B
C++
#pragma once
|
|
|
|
/*
|
|
* Copyright 2015 Cloudius Systems.
|
|
*/
|
|
|
|
// This class is the parts of java.util.UUID that we need
|
|
|
|
#include <stdint.h>
|
|
|
|
namespace utils {
|
|
|
|
class UUID {
|
|
private:
|
|
int64_t most_sig_bits;
|
|
int64_t least_sig_bits;
|
|
public:
|
|
UUID(int64_t most_sig_bits, int64_t least_sig_bits)
|
|
: most_sig_bits(most_sig_bits), least_sig_bits(least_sig_bits) {}
|
|
|
|
int64_t get_most_significant_bits() const {
|
|
return most_sig_bits;
|
|
}
|
|
int64_t get_least_significant_bits() const {
|
|
return least_sig_bits;
|
|
}
|
|
int version() const {
|
|
return (most_sig_bits >> 12) & 0xf;
|
|
}
|
|
|
|
int64_t timestamp() const {
|
|
//if (version() != 1) {
|
|
// throw new UnsupportedOperationException("Not a time-based UUID");
|
|
//}
|
|
assert(version() == 1);
|
|
|
|
return ((most_sig_bits & 0xFFF) << 48) |
|
|
(((most_sig_bits >> 16) & 0xFFFF) << 32) |
|
|
(((uint64_t)most_sig_bits) >> 32);
|
|
|
|
}
|
|
};
|
|
|
|
}
|