From 433fc6c36e4cd6b190c47d4be7149cd3f588da2b Mon Sep 17 00:00:00 2001 From: Nadav Har'El Date: Mon, 21 May 2018 20:42:20 +0300 Subject: [PATCH] keys.hh: simplify empty clustering-key check The exploded_clustering_prefix type has a convenient is_empty() method and an even more convenient "operator bool" shortcut. Unfortunately, the other clustering prefix types (clustering_key_prefix, clustering_key_prefix_view) have, for historic reasons, an is_empty method which takes a schema parameter. That also means they can't have an "operator bool" shortcut. But checking if a prefix doesn't really need the schema - all we need to check is whether the byte representation is empty. The result is simpler and more efficient code, and easier to use. It is also more consistent - all clustering-key-related types will have an "operator bool" instead of just some of them. To avoid massive code changes, we leave a is_empty(schema) variant, which simply calls is_empty(). There's already precedent for that - various methods which have a variant taking schema (and ignoring it) and one taking nothing. Signed-off-by: Nadav Har'El Message-Id: <20180521174220.13262-1-nyh@scylladb.com> --- keys.hh | 34 ++++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/keys.hh b/keys.hh index 9fce736d59..dd97d42aa4 100644 --- a/keys.hh +++ b/keys.hh @@ -146,6 +146,19 @@ public: auto components(const schema& s) const { return components(); } + + bool is_empty() const { + return _bytes.empty(); + } + + explicit operator bool() const { + return !is_empty(); + } + + // For backward compatibility with existing code. + bool is_empty(const schema& s) const { + return is_empty(); + } }; template @@ -304,8 +317,17 @@ public: return get_compound_type(s)->end(_bytes); } + bool is_empty() const { + return _bytes.empty(); + } + + explicit operator bool() const { + return !is_empty(); + } + + // For backward compatibility with existing code. bool is_empty(const schema& s) const { - return begin(s) == end(s); + return is_empty(); } // Returns a range of bytes_view @@ -520,10 +542,6 @@ public: bool is_full(const schema& s) const { return TopLevel::get_compound_type(s)->is_full(base::_bytes); } - - bool is_empty(const schema& s) const { - return TopLevel::get_compound_type(s)->is_empty(base::_bytes); - } }; template @@ -542,10 +560,6 @@ public: return TopLevel::get_compound_type(s)->is_full(base::_bytes); } - bool is_empty(const schema& s) const { - return TopLevel::get_compound_type(s)->is_empty(base::_bytes); - } - // Can be called only if is_full() FullTopLevel to_full(const schema& s) const { return FullTopLevel::from_exploded(s, base::explode(s)); @@ -803,4 +817,4 @@ struct appending_hash { void operator()(Hasher& h, const clustering_key_prefix& ck, const schema& s) const { appending_hash()(h, ck.view(), s); } -}; \ No newline at end of file +};