Add UUID::to_sstring() method, analogous to the Java UUID.toString(), and I verified that it generates the same output as the original Java method. Signed-off-by: Nadav Har'El <nyh@cloudius-systems.com> [avi: make it build, using sprint() instead of sprintf()]
62 lines
1.6 KiB
C++
62 lines
1.6 KiB
C++
#pragma once
|
|
|
|
/*
|
|
* Copyright 2015 Cloudius Systems.
|
|
*/
|
|
|
|
// This class is the parts of java.util.UUID that we need
|
|
|
|
#include <stdint.h>
|
|
#include <cassert>
|
|
|
|
#include "core/sstring.hh"
|
|
#include "core/print.hh"
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
// This matches Java's UUID.toString() actual implementation. Note that
|
|
// that method's documentation suggest something completely different!
|
|
sstring to_sstring() const {
|
|
return sprint("%08x-%04x-%04x-%04x-%012x",
|
|
((uint64_t)most_sig_bits >> 32),
|
|
((uint64_t)most_sig_bits >> 16 & 0xffff),
|
|
((uint64_t)most_sig_bits & 0xffff),
|
|
((uint64_t)least_sig_bits >> 48 & 0xffff),
|
|
((uint64_t)least_sig_bits & 0xffffffffffffLL));
|
|
}
|
|
};
|
|
|
|
UUID make_random_uuid();
|
|
|
|
}
|