From 5b2c1426333d6e2384437961fda6fc5d2bef6356 Mon Sep 17 00:00:00 2001 From: Michael Fero Date: Wed, 21 Oct 2015 10:46:07 -0400 Subject: [PATCH] Creating utility for contact points/host --- src/cluster.cpp | 21 ++++----------------- src/config.hpp | 2 -- src/session.cpp | 6 +++--- src/testing.cpp | 4 ++-- src/utils.cpp | 12 ++++++++++++ src/utils.hpp | 11 ++++++++--- src/whitelist_policy.hpp | 2 -- test/unit_tests/src/test_load_balancing.cpp | 2 +- 8 files changed, 30 insertions(+), 30 deletions(-) diff --git a/src/cluster.cpp b/src/cluster.cpp index c2c0cda9c..4a5a046b8 100644 --- a/src/cluster.cpp +++ b/src/cluster.cpp @@ -95,15 +95,8 @@ CassError cass_cluster_set_contact_points_n(CassCluster* cluster, if (contact_points_length == 0) { cluster->config().contact_points().clear(); } else { - std::istringstream stream( - std::string(contact_points, contact_points_length)); - while (!stream.eof()) { - std::string contact_point; - std::getline(stream, contact_point, ','); - if (!cass::trim(contact_point).empty()) { - cluster->config().contact_points().push_back(contact_point); - } - } + cass::explode(std::string(contact_points, contact_points_length), + cluster->config().contact_points()); } return CASS_OK; } @@ -307,14 +300,8 @@ void cass_cluster_set_whitelist_routing_hosts_n(CassCluster* cluster, if (hosts_length == 0) { cluster->config().whitelist_hosts().clear(); } else { - std::istringstream stream(std::string(hosts, hosts_length)); - while (!stream.eof()) { - std::string host; - std::getline(stream, host, ','); - if (!cass::trim(host).empty()) { - cluster->config().whitelist_hosts().push_back(host); - } - } + cass::explode(std::string(hosts, hosts_length), + cluster->config().whitelist_hosts()); } } diff --git a/src/config.hpp b/src/config.hpp index ba8d9ced8..ee9678e99 100644 --- a/src/config.hpp +++ b/src/config.hpp @@ -36,8 +36,6 @@ void stderr_log_callback(const CassLogMessage* message, void* data); class Config { public: - typedef std::list ContactPointList; - Config() : port_(9042) , protocol_version_(4) diff --git a/src/session.cpp b/src/session.cpp index 33ef5c1db..9f9a71565 100644 --- a/src/session.cpp +++ b/src/session.cpp @@ -34,7 +34,7 @@ CassSession* cass_session_new() { void cass_session_free(CassSession* session) { // This attempts to close the session because the joining will - // hang indefinately otherwise. This causes minimal delay + // hang indefinitely otherwise. This causes minimal delay // if the session is already closed. cass::SharedRefPtr future(new cass::SessionFuture()); session->close_async(future.get(), true); @@ -408,8 +408,8 @@ void Session::on_event(const SessionEvent& event) { case SessionEvent::CONNECT: { int port = config_.port(); - const Config::ContactPointList& contact_points = config_.contact_points(); - for (Config::ContactPointList::const_iterator it = contact_points.begin(), + const ContactPointList& contact_points = config_.contact_points(); + for (ContactPointList::const_iterator it = contact_points.begin(), end = contact_points.end(); it != end; ++it) { const std::string& seed = *it; diff --git a/src/testing.cpp b/src/testing.cpp index 84a4602fb..4fb93249d 100644 --- a/src/testing.cpp +++ b/src/testing.cpp @@ -46,10 +46,10 @@ int get_port_from_cluster(CassCluster* cluster) { std::string get_contact_points_from_cluster(CassCluster* cluster) { std::string str; - const cass::Config::ContactPointList& contact_points + const ContactPointList& contact_points = cluster->config().contact_points(); - for (cass::Config::ContactPointList::const_iterator it = contact_points.begin(), + for (ContactPointList::const_iterator it = contact_points.begin(), end = contact_points.end(); it != end; ++it) { if (str.size() > 0) { diff --git a/src/utils.cpp b/src/utils.cpp index 3e966fe99..f7815b4a4 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -21,6 +21,7 @@ #include #include #include +#include namespace cass { @@ -65,6 +66,17 @@ std::string opcode_to_string(int opcode) { return ""; } +void explode(const std::string& str, std::vector& vec, const char delimiter /* = ',' */) { + std::istringstream stream(str); + while (!stream.eof()) { + std::string token; + std::getline(stream, token, delimiter); + if (!trim(token).empty()) { + vec.push_back(token); + } + } +} + std::string& trim(std::string& str) { // Trim front str.erase(str.begin(), diff --git a/src/utils.hpp b/src/utils.hpp index 348ccf54a..efed2bd5b 100644 --- a/src/utils.hpp +++ b/src/utils.hpp @@ -23,12 +23,15 @@ #include #include #include +#include namespace cass { class BufferPiece; class Value; +typedef std::vector ContactPointList; + template class IsConvertible { private: @@ -45,9 +48,9 @@ class IsConvertible { static const bool value = sizeof(Helper::test(Helper::check())) == sizeof(Yes); }; -// copy_cast<> prevents incorrect code from being generated when two unrelated +// copy_cast<> prevents incorrect code from being generated when two unrelated // types reference the same memory location and strict aliasing is enabled. -// The type "char*" is an exception and is allowed to alias any other +// The type "char*" is an exception and is allowed to alias any other // pointer type. This allows memcpy() to copy bytes from one type to the other // without violating strict aliasing and usually optimizes away on a modern // compiler (GCC, Clang, and MSVC). @@ -70,9 +73,11 @@ inline size_t next_pow_2(size_t num) { } return next; } - + std::string opcode_to_string(int opcode); +void explode(const std::string& str, std::vector& vec, const char delimiter = ','); + std::string& trim(std::string& str); bool is_valid_cql_id(const std::string& str); diff --git a/src/whitelist_policy.hpp b/src/whitelist_policy.hpp index c7ee7fbbb..8314d75b7 100644 --- a/src/whitelist_policy.hpp +++ b/src/whitelist_policy.hpp @@ -25,8 +25,6 @@ namespace cass { class WhitelistPolicy : public ChainedLoadBalancingPolicy { public: - typedef std::list ContactPointList; - WhitelistPolicy(LoadBalancingPolicy* child_policy, const ContactPointList& hosts) : ChainedLoadBalancingPolicy(child_policy) diff --git a/test/unit_tests/src/test_load_balancing.cpp b/test/unit_tests/src/test_load_balancing.cpp index b75adbe92..0acf9d302 100644 --- a/test/unit_tests/src/test_load_balancing.cpp +++ b/test/unit_tests/src/test_load_balancing.cpp @@ -820,7 +820,7 @@ BOOST_AUTO_TEST_CASE(simple) const int64_t num_hosts = 100; cass::HostMap hosts; populate_hosts(num_hosts, "rack1", LOCAL_DC, &hosts); - cass::WhitelistPolicy::ContactPointList whitelist_hosts; + cass::ContactPointList whitelist_hosts; whitelist_hosts.push_back("37.0.0.0"); whitelist_hosts.push_back("83.0.0.0"); cass::WhitelistPolicy policy(new cass::RoundRobinPolicy(), whitelist_hosts);