Commit 11a582ec authored by Axel Auweter's avatar Axel Auweter
Browse files

First working implementation of the Slurm Plugin. Runs on top

of the new super-simple C API in DCDBLib.
parent 09063423
......@@ -11,6 +11,9 @@
* DCDBLib library using C instead of C++ bindings.
*/
#include <stdint.h>
#include <time.h>
#ifndef DCDB_C_API_H
#define DCDB_C_API_H
......@@ -18,7 +21,27 @@
extern "C" {
#endif
void dcdb_test_c_api();
typedef enum {
DCDB_C_OK,
DCDB_C_CONNERR,
DCDB_C_SENSORNOTFOUND,
DCDB_C_UNKNOWN
} DCDB_C_RESULT;
typedef uint32_t DCDB_C_OPTIONS;
#define DCDB_C_LOCALTIME 0x1
DCDB_C_RESULT dcdbQuerySum(
int64_t* result,
const char* hostname,
uint16_t port,
const char* sensorPublicName,
time_t start,
time_t end,
DCDB_C_OPTIONS options
);
#ifdef __cplusplus
}
......
......@@ -13,6 +13,7 @@
*/
#include <cstdint>
#include <ctime>
#include <string>
#include <stdexcept>
......@@ -57,6 +58,11 @@ public:
*/
DCDBTimeStamp(std::string ts);
/**
* @brief Time_t constructor. Initializes the object from a time_t struct.
*/
DCDBTimeStamp(time_t ts);
/**
* @brief Standard destructor.
*/
......
......@@ -6,10 +6,87 @@
*/
#include <cstdio>
#include <iostream>
#include <list>
#include "c_api.h"
void dcdb_test_c_api()
#include "sensorid.h"
#include "timestamp.h"
#include "connection.h"
#include "sensorconfig.h"
#include "sensordatastore.h"
DCDB_C_RESULT dcdbQuerySum(
int64_t* result,
const char* hostname,
uint16_t port,
const char* sensorPublicName,
time_t start,
time_t end,
DCDB_C_OPTIONS options)
{
printf("Hello from your DCDB C API.\n");
DCDBTimeStamp startTs(start), endTs(end);
*result = 0;
/* Convert start and end to UTC if they're specified in local time. */
if(options & DCDB_C_LOCALTIME) {
startTs.convertFromLocal();
endTs.convertFromLocal();
}
/* Create a new connection to the database */
DCDBConnection* connection;
connection = new DCDBConnection();
connection->setHostname(hostname);
connection->setPort(port);
if (!connection->connect()) {
delete connection;
return DCDB_C_CONNERR;
}
/* Initialize the SensorConfig interface */
SensorConfig sensorConfig(connection);
SensorDataStore sensorDataStore(connection);
/* Look up the pattern for the sensor's public name */
std::string pattern;
switch (sensorConfig.getSensorPattern(pattern, sensorPublicName)) {
case SC_OK:
break;
case SC_INVALIDSESSION:
delete connection;
return DCDB_C_CONNERR;
case SC_UNKNOWNSENSOR:
delete connection;
return DCDB_C_SENSORNOTFOUND;
default:
delete connection;
return DCDB_C_UNKNOWN;
}
/* Expand the pattern into a list of existing sensors in the time range */
std::list<SensorId> sensorIds;
switch (sensorConfig.getSensorListForPattern(sensorIds, pattern, startTs, endTs)) {
case SC_OK:
break;
case SC_INVALIDPATTERN:
delete connection;
return DCDB_C_SENSORNOTFOUND;
default:
delete connection;
return DCDB_C_UNKNOWN;
}
/* Iterate over the expanded list of sensorIds and sum up the results */
for (std::list<SensorId>::iterator sit = sensorIds.begin(); sit != sensorIds.end(); sit++) {
int64_t tmp_result = 0;
sensorDataStore.querySum(tmp_result, *sit, start, end);
*result += tmp_result;
}
/* Clean up */
connection->disconnect();
delete connection;
return DCDB_C_OK;
}
......@@ -310,6 +310,8 @@ SCError SensorConfigImpl::getSensorPattern(std::string& pattern, std::string pub
rc = cass_future_error_code(future);
if (rc != CASS_OK) {
connection->printError(future);
cass_future_free(future);
return SC_UNKNOWNERROR;
} else {
prepared = cass_future_get_prepared(future);
}
......@@ -352,8 +354,8 @@ SCError SensorConfigImpl::getSensorPattern(std::string& pattern, std::string pub
SCError SensorConfigImpl::getSensorListForPattern(std::list<SensorId>& sensorIds, std::string pattern)
{
/* Tiny hack to call the long version of this function */
DCDBTimeStamp start(0x0);
DCDBTimeStamp end(0x260DD31906D70000);
DCDBTimeStamp start((uint64_t)0x0);
DCDBTimeStamp end((uint64_t)0x260DD31906D70000);
return getSensorListForPattern(sensorIds, pattern, start, end);
}
......
......@@ -176,6 +176,8 @@ void SensorDataStoreImpl::query(std::list<SensorDataStoreReading>& result, Senso
std::string key = sid.serialize();
std::cout << "Query: " << query << std::endl << "sid: " << key << " ts1: " << start.getRaw() << " ts2: " << end.getRaw() << std::endl;
statement = cass_prepared_bind(prepared);
cass_statement_bind_bytes(statement, 0, (const cass_byte_t*)(key.c_str()), 16);
cass_statement_bind_int64(statement, 1, start.getRaw());
......
......@@ -74,6 +74,18 @@ DCDBTimeStamp::DCDBTimeStamp(std::string ts)
guessFromString(ts);
}
/**
* This constructor sets the from a time_t struct.
*/
DCDBTimeStamp::DCDBTimeStamp(time_t ts)
{
boost::posix_time::ptime epoch(boost::gregorian::date(1970,1,1));
boost::posix_time::ptime t = boost::posix_time::from_time_t(ts);
boost::posix_time::time_duration diff = t - epoch;
raw = diff.total_nanoseconds();
}
/**
* Currently, the destructor doesn't need to do anything.
*/
......
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