diff --git a/keys.hh b/keys.hh index 4ccab53e92..3c36749401 100644 --- a/keys.hh +++ b/keys.hh @@ -109,6 +109,16 @@ public: operator bytes_view() const { return _bytes; } + + // begin() and end() return iterators over components of this tuple. The iterator yields a bytes_view to the component. + auto begin(const schema& s) const { + return get_tuple_type(s)->begin(_bytes); + } + + // See begin() + auto end(const schema& s) const { + return get_tuple_type(s)->end(_bytes); + } }; template @@ -264,8 +274,13 @@ public: public: using tuple = lw_shared_ptr>; - static partition_key from_bytes(bytes b) { return partition_key(std::move(b)); } - static auto get_tuple_type(const schema& s) { return s.partition_key_type; } + static partition_key from_bytes(bytes b) { + return partition_key(std::move(b)); + } + + static const tuple& get_tuple_type(const schema& s) { + return s.partition_key_type; + } }; class exploded_clustering_prefix { @@ -294,8 +309,13 @@ public: public: using tuple = lw_shared_ptr>; - static clustering_key from_bytes(bytes b) { return clustering_key(std::move(b)); } - static auto get_tuple_type(const schema& s) { return s.clustering_key_type; } + static clustering_key from_bytes(bytes b) { + return clustering_key(std::move(b)); + } + + static const tuple& get_tuple_type(const schema& s) { + return s.clustering_key_type; + } static clustering_key from_clustering_prefix(const schema& s, const exploded_clustering_prefix& prefix) { assert(prefix.is_full(s)); @@ -308,8 +328,13 @@ class clustering_key_prefix : public prefix_tuple_wrapper>; - static clustering_key_prefix from_bytes(bytes b) { return clustering_key_prefix(std::move(b)); } - static auto get_tuple_type(const schema& s) { return s.clustering_key_prefix_type; } + static clustering_key_prefix from_bytes(bytes b) { + return clustering_key_prefix(std::move(b)); + } + + static const tuple& get_tuple_type(const schema& s) { + return s.clustering_key_prefix_type; + } static clustering_key_prefix from_clustering_prefix(const schema& s, const exploded_clustering_prefix& prefix) { return from_exploded(s, prefix.components()); diff --git a/tests/urchin/keys_test.cc b/tests/urchin/keys_test.cc index d76ec3c945..2dc706efbc 100644 --- a/tests/urchin/keys_test.cc +++ b/tests/urchin/keys_test.cc @@ -25,3 +25,32 @@ BOOST_AUTO_TEST_CASE(test_key_is_prefixed_by) { BOOST_REQUIRE(!key.is_prefixed_by(s, clustering_key_prefix::from_exploded(s, {bytes("abc")}))); BOOST_REQUIRE(!key.is_prefixed_by(s, clustering_key_prefix::from_exploded(s, {bytes("ab")}))); } + +BOOST_AUTO_TEST_CASE(test_key_component_iterator) { + schema s({}, "", "", + { + {"c1", bytes_type} + }, { + {"c2", bytes_type}, {"c3", bytes_type}, {"c4", bytes_type} + }, + {}, {}, utf8_type); + + auto key = clustering_key::from_exploded(s, {bytes("a"), bytes("b"), bytes("c")}); + + auto i = key.begin(s); + auto end = key.end(s); + + BOOST_REQUIRE(i != end); + BOOST_REQUIRE(*i == bytes_view(bytes("a"))); + ++i; + + BOOST_REQUIRE(i != end); + BOOST_REQUIRE(*i == bytes_view(bytes("b"))); + ++i; + + BOOST_REQUIRE(i != end); + BOOST_REQUIRE(*i == bytes_view(bytes("c"))); + ++i; + + BOOST_REQUIRE(i == end); +}