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 <nyh@scylladb.com>
Message-Id: <20180521174220.13262-1-nyh@scylladb.com>
This commit is contained in:
Nadav Har'El
2018-05-21 20:42:20 +03:00
committed by Tomasz Grabiec
parent 300af65555
commit 433fc6c36e

34
keys.hh
View File

@@ -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 <typename TopLevel, typename TopLevelView>
@@ -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 <typename TopLevel, typename TopLevelView, typename FullTopLevel>
@@ -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<clustering_key_prefix> {
void operator()(Hasher& h, const clustering_key_prefix& ck, const schema& s) const {
appending_hash<clustering_key_prefix_view>()(h, ck.view(), s);
}
};
};