From ebfc1ffb203a47f2df8afeeb73c03be120caf59a Mon Sep 17 00:00:00 2001 From: Tomasz Grabiec Date: Thu, 19 Mar 2015 21:29:24 +0100 Subject: [PATCH] query: Add facilities for printing query request --- configure.py | 1 + keys.hh | 15 +++++++++++++++ query.cc | 39 +++++++++++++++++++++++++++++++++++++++ query.hh | 43 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 98 insertions(+) create mode 100644 query.cc diff --git a/configure.py b/configure.py index 7e1eabcf09..4a12a6d52a 100755 --- a/configure.py +++ b/configure.py @@ -305,6 +305,7 @@ urchin_core = (['database.cc', 'dht/i_partitioner.cc', 'dht/murmur3_partitioner.cc', 'unimplemented.cc', + 'query.cc', ] + [Antlr3Grammar('cql3/Cql.g')] + [Thrift('interface/cassandra.thrift', 'Cassandra')] diff --git a/keys.hh b/keys.hh index e57a1bee6d..8ea940b8fa 100644 --- a/keys.hh +++ b/keys.hh @@ -281,3 +281,18 @@ public: return from_exploded(s, prefix.components()); } }; + +static inline +std::ostream& operator<<(std::ostream& out, const partition_key& pk) { + return out << "pk{" << to_hex(pk) << "}"; +} + +static inline +std::ostream& operator<<(std::ostream& out, const clustering_key& ck) { + return out << "ck{" << to_hex(ck) << "}"; +} + +static inline +std::ostream& operator<<(std::ostream& out, const clustering_key_prefix& ckp) { + return out << "ckp{" << to_hex(ckp) << "}"; +} diff --git a/query.cc b/query.cc new file mode 100644 index 0000000000..1108ce7a00 --- /dev/null +++ b/query.cc @@ -0,0 +1,39 @@ +/* + * Copyright (C) 2015 Cloudius Systems, Ltd. + */ + +#include "query.hh" + +namespace query { + +template +sstring join(const std::vector& v, sstring sep) { + std::ostringstream oss; + size_t left = v.size(); + for (auto&& item : v) { + oss << item; + if (--left) { + oss << ", "; + } + } + return oss.str(); +} + +std::ostream& operator<<(std::ostream& out, const partition_slice& ps) { + return out << "{" + << "regular_cols=[" << join(ps.regular_columns, ", ") << "]" + << ", static_cols=[" << join(ps.static_columns, ", ") << "]" + << ", rows=[" << join(ps.row_ranges, ", ") << "]" + << "}"; +} + +std::ostream& operator<<(std::ostream& out, const read_command& r) { + return out << "read_command{" + << "ks=" << r.keyspace + << ", cf=" << r.column_family + << ", pks=[" << join(r.partition_ranges, ", ") << "]" + << ", slice=" << r.slice << "" + << ", limit=" << r.row_limit << "}"; +} + +} diff --git a/query.hh b/query.hh index 942067f77d..b20d96eb5a 100644 --- a/query.hh +++ b/query.hh @@ -73,8 +73,50 @@ public: const T& end_value() const { return _end->value(); } + + const optional& start() const { + return _start; + } + + const optional& end() const { + return _end; + } + + template + friend std::ostream& operator<<(std::ostream& out, const range& r); }; +template +std::ostream& operator<<(std::ostream& out, const range& r) { + if (r.is_singular()) { + return out << "==" << r.start_value(); + } + + if (!r.start()) { + out << "(-inf, "; + } else { + if (r.start()->is_inclusive()) { + out << "["; + } else { + out << "("; + } + out << r.start()->value() << ", "; + } + + if (!r.end()) { + out << "+inf)"; + } else { + out << r.end()->value(); + if (r.end()->is_inclusive()) { + out << "]"; + } else { + out << ")"; + } + } + + return out; +} + using partition_range = range; using clustering_range = range; @@ -142,6 +184,7 @@ public: , slice(std::move(slice)) , row_limit(row_limit) { } + friend std::ostream& operator<<(std::ostream& out, const read_command& r); }; }