From 5ef11d113a3ae98acb566736198622403c0615c7 Mon Sep 17 00:00:00 2001 From: Tomasz Grabiec Date: Thu, 16 Apr 2015 13:15:07 +0200 Subject: [PATCH 1/2] types: Improve code readability --- keys.hh | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/keys.hh b/keys.hh index 4ccab53e92..292bfbd673 100644 --- a/keys.hh +++ b/keys.hh @@ -264,8 +264,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 +299,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 +318,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()); From bacede04b2cc752fedd479eb7fda106bdf6b3342 Mon Sep 17 00:00:00 2001 From: Tomasz Grabiec Date: Thu, 16 Apr 2015 13:15:30 +0200 Subject: [PATCH 2/2] types: Expose component iterators in tuple_wrapper This automatically exposes them in partition_key and clustering_key too. The iterators return bytes_view to components. For example: schema s; partition_key k; for (bytes_view component : boost::make_iterator_range(key.begin(s), key.end(s))) { // ... } --- keys.hh | 10 ++++++++++ tests/urchin/keys_test.cc | 29 +++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/keys.hh b/keys.hh index 292bfbd673..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 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); +}