[QUEUED scylla next] uuid: change comparators to std::strong_ordering

0 views
Skip to first unread message

Commit Bot

unread,
Jul 29, 2021, 3:44:21 PMJul 29
to scylla...@googlegroups.com, Avi Kivity
From: Avi Kivity <a...@scylladb.com>
Committer: Avi Kivity <a...@scylladb.com>
Branch: next

uuid: change comparators to std::strong_ordering

Ref #1449.

---
diff --git a/test/boost/UUID_test.cc b/test/boost/UUID_test.cc
--- a/test/boost/UUID_test.cc
+++ b/test/boost/UUID_test.cc
@@ -97,24 +97,24 @@ BOOST_AUTO_TEST_CASE(test_get_time_uuid) {
BOOST_CHECK(unix_timestamp == millis);
}

-int timeuuid_legacy_tri_compare(bytes_view o1, bytes_view o2) {
- auto compare_pos = [&] (unsigned pos, int mask, int ifequal) {
- int d = (o1[pos] & mask) - (o2[pos] & mask);
- return d ? d : ifequal;
+std::strong_ordering timeuuid_legacy_tri_compare(bytes_view o1, bytes_view o2) {
+ auto compare_pos = [&] (unsigned pos, int mask, std::strong_ordering ifequal) {
+ auto d = (o1[pos] & mask) <=> (o2[pos] & mask);
+ return d != 0 ? d : ifequal;
};
- int res = compare_pos(6, 0xf,
+ auto res = compare_pos(6, 0xf,
compare_pos(7, 0xff,
compare_pos(4, 0xff,
compare_pos(5, 0xff,
compare_pos(0, 0xff,
compare_pos(1, 0xff,
compare_pos(2, 0xff,
- compare_pos(3, 0xff, 0))))))));
+ compare_pos(3, 0xff, std::strong_ordering::equal))))))));
if (res == 0) {
res = lexicographical_tri_compare(o1.begin(), o1.end(), o2.begin(), o2.end(),
- [] (const int8_t& a, const int8_t& b) { return a - b; });
+ [] (const int8_t& a, const int8_t& b) { return a <=> b; });
}
- return res < 0 ? -1 : res > 0;
+ return res;
}

BOOST_AUTO_TEST_CASE(test_timeuuid_msb_is_monotonic) {
diff --git a/utils/UUID.hh b/utils/UUID.hh
--- a/utils/UUID.hh
+++ b/utils/UUID.hh
@@ -27,6 +27,7 @@
#include <cassert>
#include <array>
#include <iosfwd>
+#include <compare>

#include <seastar/core/sstring.hh>
#include <seastar/core/print.hh>
@@ -141,8 +142,8 @@ public:

UUID make_random_uuid();

-inline int uint64_t_tri_compare(uint64_t a, uint64_t b) {
- return a < b ? -1 : a > b;
+inline std::strong_ordering uint64_t_tri_compare(uint64_t a, uint64_t b) {
+ return a <=> b;
}

// Read 8 most significant bytes of timeuuid from serialized bytes
@@ -178,11 +179,11 @@ inline uint64_t uuid_read_lsb(const int8_t *b) {
// To avoid breaking ordering in existing sstables, Scylla preserves
// Cassandra compare order.
//
-inline int timeuuid_tri_compare(bytes_view o1, bytes_view o2) {
+inline std::strong_ordering timeuuid_tri_compare(bytes_view o1, bytes_view o2) {
auto timeuuid_read_lsb = [](bytes_view o) -> uint64_t {
return uuid_read_lsb(o.begin()) ^ 0x8080808080808080;
};
- int res = uint64_t_tri_compare(timeuuid_read_msb(o1.begin()), timeuuid_read_msb(o2.begin()));
+ auto res = uint64_t_tri_compare(timeuuid_read_msb(o1.begin()), timeuuid_read_msb(o2.begin()));
if (res == 0) {
res = uint64_t_tri_compare(timeuuid_read_lsb(o1), timeuuid_read_lsb(o2));
}
@@ -196,8 +197,8 @@ inline int timeuuid_tri_compare(bytes_view o1, bytes_view o2) {
// which is both faster and monotonic, so should be preferred
// to @timeuuid_tri_compare() used for all new features.
//
-inline int uuid_tri_compare_timeuuid(bytes_view o1, bytes_view o2) {
- int res = uint64_t_tri_compare(timeuuid_read_msb(o1.begin()), timeuuid_read_msb(o2.begin()));
+inline std::strong_ordering uuid_tri_compare_timeuuid(bytes_view o1, bytes_view o2) {
+ auto res = uint64_t_tri_compare(timeuuid_read_msb(o1.begin()), timeuuid_read_msb(o2.begin()));
if (res == 0) {
res = uint64_t_tri_compare(uuid_read_lsb(o1.begin()), uuid_read_lsb(o2.begin()));
}

Commit Bot

unread,
Jul 29, 2021, 9:40:11 PMJul 29
to scylla...@googlegroups.com, Avi Kivity
From: Avi Kivity <a...@scylladb.com>
Committer: Avi Kivity <a...@scylladb.com>
Branch: master
Reply all
Reply to author
Forward
0 new messages