Skip to content

Commit

Permalink
Added support for insert int64_t types for columns with LongType comp…
Browse files Browse the repository at this point in the history
…arators.
  • Loading branch information
posulliv committed Feb 28, 2011
1 parent cd46b6f commit cbc0e61
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 29 deletions.
31 changes: 12 additions & 19 deletions libcassandra/cassandra.cc
Expand Up @@ -8,10 +8,12 @@
*/

#include <time.h>
#include <netinet/in.h>

#include <string>
#include <set>
#include <sstream>
#include <iostream>

#include "libgenthrift/Cassandra.h"

Expand All @@ -26,16 +28,6 @@ using namespace std;
using namespace org::apache::cassandra;
using namespace libcassandra;

/* utility functions */

template<class T>
inline string toString(const T &tt)
{
stringstream ss;
ss << tt;
return ss.str();
}


Cassandra::Cassandra()
:
Expand Down Expand Up @@ -167,6 +159,16 @@ void Cassandra::insertColumn(const string& key,
}


void Cassandra::insertColumn(const string& key,
const string& column_family,
const string& column_name,
const int64_t value)
{
//int64_t store_value= htonll(value);
insertColumn(key, column_family, "", column_name, serializeLong(value), ConsistencyLevel::QUORUM);
}


void Cassandra::remove(const string &key,
const ColumnPath &col_path,
ConsistencyLevel::type level)
Expand Down Expand Up @@ -598,15 +600,6 @@ int Cassandra::getPort() const
}


string Cassandra::buildKeyspaceMapName(string keyspace, int level)
{
keyspace.append("[");
keyspace.append(toString(level));
keyspace.append("]");
return keyspace;
}


bool Cassandra::findKeyspace(const string& name)
{
for (vector<KeyspaceDefinition>::iterator it= key_spaces.begin();
Expand Down
18 changes: 13 additions & 5 deletions libcassandra/cassandra.h
Expand Up @@ -134,6 +134,19 @@ class Cassandra
const std::string& column_name,
const std::string& value);

/**
* Insert a column, directly in a columnfamily
*
* @param[in] key the column key
* @param[in] column_family the column family
* @param[in] column_name the column name
* @param[in] value the column value
*/
void insertColumn(const std::string& key,
const std::string& column_family,
const std::string& column_name,
const int64_t value);

/**
* Removes all the columns that match the given column path
*
Expand Down Expand Up @@ -397,11 +410,6 @@ class Cassandra

private:

/**
* Creates a unique map name for the keyspace and its consistency level
*/
std::string buildKeyspaceMapName(std::string keyspace, int level);

/**
* Finds the given keyspace in the list of keyspace definitions
* @return true if found; false otherwise
Expand Down
51 changes: 51 additions & 0 deletions libcassandra/indexed_slices_query.cc
Expand Up @@ -48,6 +48,17 @@ void IndexedSlicesQuery::addEqualsExpression(const string& column, const string&
}



void IndexedSlicesQuery::addEqualsExpression(const string& column, const int64_t value)
{
IndexExpression new_expr;
new_expr.column_name.assign(column);
new_expr.value.assign(serializeLong(value));
new_expr.op= IndexOperator::EQ;
index_clause.expressions.push_back(new_expr);
}


void IndexedSlicesQuery::addGtExpression(const string& column, const string& value)
{
IndexExpression new_expr;
Expand All @@ -58,6 +69,16 @@ void IndexedSlicesQuery::addGtExpression(const string& column, const string& val
}


void IndexedSlicesQuery::addGtExpression(const string& column, const int64_t value)
{
IndexExpression new_expr;
new_expr.column_name.assign(column);
new_expr.value.assign(serializeLong(value));
new_expr.op= IndexOperator::GT;
index_clause.expressions.push_back(new_expr);
}


void IndexedSlicesQuery::addGtEqualsExpression(const string& column, const string& value)
{
IndexExpression new_expr;
Expand All @@ -68,6 +89,16 @@ void IndexedSlicesQuery::addGtEqualsExpression(const string& column, const strin
}


void IndexedSlicesQuery::addGtEqualsExpression(const string& column, const int64_t value)
{
IndexExpression new_expr;
new_expr.column_name.assign(column);
new_expr.value.assign(serializeLong(value));
new_expr.op= IndexOperator::GTE;
index_clause.expressions.push_back(new_expr);
}


void IndexedSlicesQuery::addLtExpression(const string& column, const string& value)
{
IndexExpression new_expr;
Expand All @@ -78,6 +109,16 @@ void IndexedSlicesQuery::addLtExpression(const string& column, const string& val
}


void IndexedSlicesQuery::addLtExpression(const string& column, const int64_t value)
{
IndexExpression new_expr;
new_expr.column_name.assign(column);
new_expr.value.assign(serializeLong(value));
new_expr.op= IndexOperator::LT;
index_clause.expressions.push_back(new_expr);
}


void IndexedSlicesQuery::addLtEqualsExpression(const string& column, const string& value)
{
IndexExpression new_expr;
Expand All @@ -88,6 +129,16 @@ void IndexedSlicesQuery::addLtEqualsExpression(const string& column, const strin
}


void IndexedSlicesQuery::addLtEqualsExpression(const string& column, const int64_t value)
{
IndexExpression new_expr;
new_expr.column_name.assign(column);
new_expr.value.assign(serializeLong(value));
new_expr.op= IndexOperator::LT;
index_clause.expressions.push_back(new_expr);
}


void IndexedSlicesQuery::addIndexExpression(const string& column,
const string& value,
IndexOperator::type op_type)
Expand Down
10 changes: 10 additions & 0 deletions libcassandra/indexed_slices_query.h
Expand Up @@ -33,14 +33,24 @@ class IndexedSlicesQuery

void addEqualsExpression(const std::string& column, const std::string& value);

void addEqualsExpression(const std::string& column, const int64_t value);

void addGtExpression(const std::string& column, const std::string& value);

void addGtExpression(const std::string& column, const int64_t value);

void addGtEqualsExpression(const std::string& column, const std::string& value);

void addGtEqualsExpression(const std::string& column, const int64_t value);

void addLtExpression(const std::string& column, const std::string& value);

void addLtExpression(const std::string& column, const int64_t value);

void addLtEqualsExpression(const std::string& column, const std::string& value);

void addLtEqualsExpression(const std::string& column, const int64_t value);

void addIndexExpression(const std::string& column,
const std::string& value,
org::apache::cassandra::IndexOperator::type op_type);
Expand Down
15 changes: 15 additions & 0 deletions libcassandra/util_functions.cc
Expand Up @@ -244,4 +244,19 @@ int64_t createTimestamp()
return (int64_t) tv.tv_sec * 1000000 + (int64_t) tv.tv_usec;
}


string serializeLong(int64_t t)
{
unsigned char raw_array[8];
raw_array[0]= (t >> 56) & 0xff;
raw_array[1]= (t >> 48) & 0xff;
raw_array[2]= (t >> 40) & 0xff;
raw_array[3]= (t >> 32) & 0xff;
raw_array[4]= (t >> 24) & 0xff;
raw_array[5]= (t >> 16) & 0xff;
raw_array[6]= (t >> 8) & 0xff;
raw_array[7]= t & 0xff;
return string(reinterpret_cast<const char *>(raw_array), 8);
}

} /* end namespace libcassandra */
10 changes: 9 additions & 1 deletion libcassandra/util_functions.h
Expand Up @@ -84,12 +84,20 @@ getColumnList(std::vector<org::apache::cassandra::ColumnOrSuperColumn>& cols);
std::vector<org::apache::cassandra::SuperColumn>
getSuperColumnList(std::vector<org::apache::cassandra::ColumnOrSuperColumn>& cols);


/**
* @return a timestamp in micro-seconds
*/
int64_t createTimestamp();

/**
* Convert given 64 bit integer to big-endian
* format and place these raw bytes in a std::string
* This is the format thrift expects for a LongType
* @param[in] t integer to work with
* @return a std::string representing the input in big-endian format
*/
std::string serializeLong(int64_t t);

} /* end namespace libcassandra */

#endif /* __LIBCASSANDRA_UTIL_FUNCTIONS_H */
8 changes: 4 additions & 4 deletions tests/cassandra_client_test.cc
Expand Up @@ -250,21 +250,21 @@ TEST_F(ClientTest, SecondaryIndexes)
c->setKeyspace(ks_def.getName());
c->createColumnFamily(cf_def);
c->insertColumn("bsanderson", cf_def.getName(), "full_name", "Brandon Sanderson");
c->insertColumn("bsanderson", cf_def.getName(), "birth_date", "00001975");
c->insertColumn("bsanderson", cf_def.getName(), "birth_date", 1975);
c->insertColumn("bsanderson", cf_def.getName(), "state", "UT");
c->insertColumn("prothfuss", cf_def.getName(), "full_name", "Patrick Rothfuss");
c->insertColumn("prothfuss", cf_def.getName(), "birth_date", "00001973");
c->insertColumn("prothfuss", cf_def.getName(), "birth_date", 1973);
c->insertColumn("prothfuss", cf_def.getName(), "state", "WI");
c->insertColumn("htayler", cf_def.getName(), "full_name", "Howard Tayler");
c->insertColumn("htayler", cf_def.getName(), "birth_date", "00001968");
c->insertColumn("htayler", cf_def.getName(), "birth_date", 1968);
c->insertColumn("htayler", cf_def.getName(), "state", "UT");
IndexedSlicesQuery query;
vector<string> column_names;
column_names.push_back("full_name");
column_names.push_back("birth_date");
column_names.push_back("state");
query.setColumns(column_names);
query.addGtExpression("birth_date", "00001970");
query.addGtExpression("birth_date", 1970);
query.addEqualsExpression("state", "UT");
query.setColumnFamily("users");
map<string, map<string, string> > res= c->getIndexedSlices(query);
Expand Down

0 comments on commit cbc0e61

Please sign in to comment.