Skip to content

Commit

Permalink
Creating utility for contact points/host
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Fero committed Oct 21, 2015
1 parent 1bb05bf commit 5b2c142
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 30 deletions.
21 changes: 4 additions & 17 deletions src/cluster.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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());
}
}

Expand Down
2 changes: 0 additions & 2 deletions src/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ void stderr_log_callback(const CassLogMessage* message, void* data);

class Config {
public:
typedef std::list<std::string> ContactPointList;

Config()
: port_(9042)
, protocol_version_(4)
Expand Down
6 changes: 3 additions & 3 deletions src/session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<cass::Future> future(new cass::SessionFuture());
session->close_async(future.get(), true);
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/testing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
12 changes: 12 additions & 0 deletions src/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <algorithm>
#include <assert.h>
#include <functional>
#include <sstream>

namespace cass {

Expand Down Expand Up @@ -65,6 +66,17 @@ std::string opcode_to_string(int opcode) {
return "";
}

void explode(const std::string& str, std::vector<std::string>& 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(),
Expand Down
11 changes: 8 additions & 3 deletions src/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,15 @@
#include <stddef.h>
#include <string>
#include <string.h>
#include <vector>

namespace cass {

class BufferPiece;
class Value;

typedef std::vector<std::string> ContactPointList;

template<class From, class To>
class IsConvertible {
private:
Expand All @@ -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).
Expand All @@ -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<std::string>& vec, const char delimiter = ',');

std::string& trim(std::string& str);

bool is_valid_cql_id(const std::string& str);
Expand Down
2 changes: 0 additions & 2 deletions src/whitelist_policy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ namespace cass {

class WhitelistPolicy : public ChainedLoadBalancingPolicy {
public:
typedef std::list<std::string> ContactPointList;

WhitelistPolicy(LoadBalancingPolicy* child_policy,
const ContactPointList& hosts)
: ChainedLoadBalancingPolicy(child_policy)
Expand Down
2 changes: 1 addition & 1 deletion test/unit_tests/src/test_load_balancing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 5b2c142

Please sign in to comment.