Commit 187fa5f1 authored by Axel Auweter's avatar Axel Auweter
Browse files

Double insert performance by reusing the prepared insert statement.

Related to ticket #29.
parent 1c93c881
......@@ -34,6 +34,8 @@ protected:
const CassSchema* schema; /**< The schema object containing the current database schema information */
std::string currentKeyspace; /**< The name of the active keyspace */
const CassPrepared* preparedInsert; /**< The prepared statement for insertions */
/**
* @brief This function validates a name to ensure that
* it only consists of alphanumeric characters.
......@@ -135,6 +137,11 @@ public:
*/
void insert(std::string key, uint64_t ts, uint64_t value);
/**
* @brief Prepare for insertions
*/
void prepareInsert();
/* Class constructor / desctructor */
......
......@@ -263,23 +263,9 @@ void CassandraBackend::insert(std::string key, uint64_t ts, uint64_t value)
#endif
CassError rc = CASS_OK;
CassStatement* statement = NULL;
CassFuture* future = NULL;
const CassPrepared* prepared = NULL;
CassString query = cass_string_init("INSERT INTO dcdb.sensordata (sid, ts, value) VALUES (?, ?, ?);");
CassFuture *future = NULL;
future = cass_session_prepare(session, query);
cass_future_wait(future);
rc = cass_future_error_code(future);
if (rc != CASS_OK) {
printError(future);
} else {
prepared = cass_future_get_prepared(future);
}
cass_future_free(future);
statement = cass_prepared_bind(prepared);
statement = cass_prepared_bind(preparedInsert);
CassBytes sid = cass_bytes_init((cass_byte_t*)(key.c_str()), 16);
......@@ -296,11 +282,35 @@ void CassandraBackend::insert(std::string key, uint64_t ts, uint64_t value)
printError(future);
}
cass_prepared_free(prepared);
cass_future_free(future);
cass_statement_free(statement);
}
/**
* @details
* Since we want high-performance inserts, we prepare the
* insert CQL query in advance and only bind it on the actual
* insert.
*/
void CassandraBackend::prepareInsert()
{
CassError rc = CASS_OK;
CassFuture* future = NULL;
CassString query = cass_string_init("INSERT INTO dcdb.sensordata (sid, ts, value) VALUES (?, ?, ?);");
future = cass_session_prepare(session, query);
cass_future_wait(future);
rc = cass_future_error_code(future);
if (rc != CASS_OK) {
printError(future);
} else {
preparedInsert = cass_future_get_prepared(future);
}
cass_future_free(future);
}
CassandraBackend::CassandraBackend()
{
cluster = nullptr;
......@@ -311,6 +321,8 @@ CassandraBackend::CassandraBackend()
CassandraBackend::~CassandraBackend()
{
/* Clean up... */
if (preparedInsert)
cass_prepared_free(preparedInsert);
if (session)
cass_session_free(session);
if (cluster)
......
......@@ -150,6 +150,9 @@ void SensorDataStoreImpl::init(std::string hostname, int port) {
"sid, ts",
"COMPACT STORAGE");
}
/* Prepare for optimized insertions */
csBackend->prepareInsert();
}
/**
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment