From e9cc24bc1880a93a8a73c72587b9c97c595f661e Mon Sep 17 00:00:00 2001 From: Benny Halevy Date: Thu, 4 Aug 2022 12:30:17 +0300 Subject: [PATCH] counters: base counter_id on utils::tagged_uuid Use the common base class for uuid-based types. tagged_uuid::to_uuid defined here for backward compatibility, but it will be renamed in the next patch to uuid(). Signed-off-by: Benny Halevy --- counters.cc | 4 ---- counters.hh | 32 ++------------------------------ utils/UUID.hh | 14 ++++++++++++++ 3 files changed, 16 insertions(+), 34 deletions(-) diff --git a/counters.cc b/counters.cc index 517263e055..8b3133adb5 100644 --- a/counters.cc +++ b/counters.cc @@ -12,10 +12,6 @@ #include -std::ostream& operator<<(std::ostream& os, const counter_id& id) { - return os << id.to_uuid(); -} - std::ostream& operator<<(std::ostream& os, counter_shard_view csv) { return os << "{global_shard id: " << csv.id() << " value: " << csv.value() << " clock: " << csv.logical_clock() << "}"; diff --git a/counters.hh b/counters.hh index d90e741495..c9af595333 100644 --- a/counters.hh +++ b/counters.hh @@ -18,45 +18,17 @@ class mutation; class atomic_cell_or_collection; -class counter_id { - int64_t _least_significant; - int64_t _most_significant; +class counter_id : public utils::tagged_uuid { public: - static_assert(std::is_same().get_least_significant_bits()), int64_t>::value - && std::is_same().get_most_significant_bits()), int64_t>::value, - "utils::UUID is expected to work with two signed 64-bit integers"); - counter_id() = default; - explicit counter_id(utils::UUID uuid) noexcept - : _least_significant(uuid.get_least_significant_bits()) - , _most_significant(uuid.get_most_significant_bits()) - { } + explicit counter_id(utils::UUID uuid) noexcept : utils::tagged_uuid(uuid) {} - utils::UUID to_uuid() const { - return utils::UUID(_most_significant, _least_significant); - } - - bool operator<(const counter_id& other) const { - return to_uuid() < other.to_uuid(); - } - bool operator>(const counter_id& other) const { - return other.to_uuid() < to_uuid(); - } - bool operator==(const counter_id& other) const { - return to_uuid() == other.to_uuid(); - } - bool operator!=(const counter_id& other) const { - return !(*this == other); - } -public: // For tests. static counter_id generate_random() { return counter_id(utils::make_random_uuid()); } }; -std::ostream& operator<<(std::ostream& os, const counter_id& id); - template class basic_counter_shard_view { enum class offset : unsigned { diff --git a/utils/UUID.hh b/utils/UUID.hh index 67c8607dff..08b81f418d 100644 --- a/utils/UUID.hh +++ b/utils/UUID.hh @@ -217,6 +217,15 @@ struct tagged_uuid { bool operator<(const tagged_uuid& o) const noexcept { return id < o.id; } + bool operator>(const tagged_uuid& o) const noexcept { + return id > o.id; + } + bool operator<=(const tagged_uuid& o) const noexcept { + return id <= o.id; + } + bool operator>=(const tagged_uuid& o) const noexcept { + return id >= o.id; + } explicit operator bool() const noexcept { // The default constructor sets the id to nil, which is // guaranteed to not match any valid id. @@ -225,6 +234,11 @@ struct tagged_uuid { static tagged_uuid create_random_id() noexcept { return tagged_uuid{utils::make_random_uuid()}; } explicit tagged_uuid(const utils::UUID& uuid) noexcept : id(uuid) {} tagged_uuid() = default; + + // FIXME: rename to uuid() + const utils::UUID& to_uuid() const noexcept { + return id; + } }; } // namespace utils