summaryrefslogtreecommitdiff
path: root/datastore-leveldb/src
diff options
context:
space:
mode:
Diffstat (limited to 'datastore-leveldb/src')
-rw-r--r--datastore-leveldb/src/db.cpp20
-rw-r--r--datastore-leveldb/src/db.h5
-rw-r--r--datastore-leveldb/src/server.cpp3
-rw-r--r--datastore-leveldb/src/web.cpp44
-rw-r--r--datastore-leveldb/src/web.h3
5 files changed, 55 insertions, 20 deletions
diff --git a/datastore-leveldb/src/db.cpp b/datastore-leveldb/src/db.cpp
index 84e30f3..58889dd 100644
--- a/datastore-leveldb/src/db.cpp
+++ b/datastore-leveldb/src/db.cpp
@@ -1,9 +1,10 @@
#include "db.h"
+#include <iomanip>
#include <iostream>
#include <mutex>
-#include <sstream>
#include <unordered_map>
+#include <sstream>
static std::unordered_map<std::string,leveldb::DB*> dbs;
static std::mutex getDBmutex;
@@ -20,6 +21,13 @@ static bool sensor_name_is_sane(std::string& name) {
return true;
}
+std::string make_key(const uint64_t timestamp) {
+ std::stringstream key;
+ key << "ts-";
+ key << std::setfill('0') << std::setw(20) << timestamp;
+ return key.str();
+}
+
leveldb::DB *getDB(std::string& name) {
getDBmutex.lock();
if (dbs.find(name) == dbs.end()) {
@@ -30,7 +38,7 @@ leveldb::DB *getDB(std::string& name) {
leveldb::DB *db;
leveldb::Options options;
options.create_if_missing = true;
- leveldb::Status status = leveldb::DB::Open(options, "/tmp/testdb."+name, &db);
+ leveldb::Status status = leveldb::DB::Open(options, "data/"+name, &db);
if (not status.ok()) {
std::cout << status.ToString() << std::endl;
getDBmutex.unlock();
@@ -46,6 +54,14 @@ leveldb::DB *getDB(std::string& name) {
}
+bool db_insert(std::string& name, const uint64_t timestamp, std::string& value) {
+ leveldb::DB *db = getDB(name);
+ if (db == nullptr) return false;
+
+ auto status = db->Put(leveldb::WriteOptions(), make_key(timestamp), value);
+ return status.ok();
+}
+
void closeDB() {
std::cout << "Close Databases: ";
auto it = dbs.begin();
diff --git a/datastore-leveldb/src/db.h b/datastore-leveldb/src/db.h
index c7ed25b..9c4c9fb 100644
--- a/datastore-leveldb/src/db.h
+++ b/datastore-leveldb/src/db.h
@@ -5,6 +5,11 @@
leveldb::DB *getDB(std::string& name);
+bool db_insert(std::string& name, const uint64_t timestamp, std::string& value);
+
void closeDB();
+
+std::string make_key(const uint64_t timestamp);
+
#endif /*HAVE_DB_H*/
diff --git a/datastore-leveldb/src/server.cpp b/datastore-leveldb/src/server.cpp
index 3000a25..4776f33 100644
--- a/datastore-leveldb/src/server.cpp
+++ b/datastore-leveldb/src/server.cpp
@@ -57,6 +57,9 @@ int main(int argc, char **argv) {
web_handle_api_value_R,
web_handle_api_value));
web_handler.push_front(std::make_pair(
+ web_handle_api_value_timestamp_R,
+ web_handle_api_value_timestamp));
+ web_handler.push_front(std::make_pair(
web_handle_api_range_R,
web_handle_api_range));
web_handler.push_front(std::make_pair(
diff --git a/datastore-leveldb/src/web.cpp b/datastore-leveldb/src/web.cpp
index efff6b9..125a4ad 100644
--- a/datastore-leveldb/src/web.cpp
+++ b/datastore-leveldb/src/web.cpp
@@ -1,17 +1,14 @@
#include "web.h"
#include "db.h"
#include <algorithm>
-#include <vector>
+#include <chrono>
#include <leveldb/comparator.h>
-#include <iomanip>
static const leveldb::Comparator *cmp = leveldb::BytewiseComparator();
-static inline std::string make_key(uint64_t timestamp) {
- std::stringstream key;
- key << "ts-";
- key << std::setfill('0') << std::setw(20) << timestamp;
- return key.str();
+static inline uint64_t now() {
+ return std::chrono::duration_cast<std::chrono::milliseconds>(
+ std::chrono::high_resolution_clock::now().time_since_epoch()).count();
}
static inline bool parse_key(leveldb::Slice &&key, uint64_t *value) {
@@ -21,25 +18,36 @@ static inline bool parse_key(leveldb::Slice &&key, uint64_t *value) {
return true;
}
-
const boost::regex web_handle_api_value_R(
- "/api/value/([a-zA-Z0-9\\.]+)/([0-9]+)");
+ "/api/value/([a-zA-Z0-9\\.]+)");
void web_handle_api_value(const boost::cmatch &match, struct mg_connection *conn) {
std::string sensor(match[1].str());
- uint64_t timestamp = std::stoul(match[2].str());
+ uint64_t timestamp = now();
char buf[1024];
- int count = mg_read(conn, buf, 1024);
- std::string value(buf, count);
+ int bytesRead = mg_read(conn, buf, 1024);
+ std::string value(buf, bytesRead);
- leveldb::DB *db = getDB(sensor);
- if (db == nullptr) {
- std::cout << "failed to get db for " << sensor << std::endl;
+ if (db_insert(sensor, timestamp, value)) {
+ mg_printf(conn, "HTTP/1.1 200 OK Value received\r\n\r\n");
+ } else {
mg_printf(conn, "HTTP/1.1 500 Internal Error\r\n\r\n");
- return;
}
+}
- db->Put(leveldb::WriteOptions(), make_key(timestamp), value);
- mg_printf(conn, "HTTP/1.1 200 Value received\r\n\r\n");
+const boost::regex web_handle_api_value_timestamp_R(
+ "/api/value/([a-zA-Z0-9\\.]+)/([0-9]+)");
+void web_handle_api_value_timestamp(const boost::cmatch &match, struct mg_connection *conn) {
+ std::string sensor(match[1].str());
+ uint64_t timestamp = std::stoul(match[2].str());
+ char buf[1024];
+ int bytesRead = mg_read(conn, buf, 1024);
+ std::string value(buf, bytesRead);
+
+ if (db_insert(sensor, timestamp, value)) {
+ mg_printf(conn, "HTTP/1.1 200 OK Value received\r\n\r\n");
+ } else {
+ mg_printf(conn, "HTTP/1.1 500 Internal Error\r\n\r\n");
+ }
}
static inline void print_json_tuple(struct mg_connection *conn,
diff --git a/datastore-leveldb/src/web.h b/datastore-leveldb/src/web.h
index acb38f5..a9d4593 100644
--- a/datastore-leveldb/src/web.h
+++ b/datastore-leveldb/src/web.h
@@ -9,6 +9,9 @@ extern "C" {
extern const boost::regex web_handle_api_value_R;
void web_handle_api_value(const boost::cmatch &match, struct mg_connection *conn);
+extern const boost::regex web_handle_api_value_timestamp_R;
+void web_handle_api_value_timestamp(const boost::cmatch &match, struct mg_connection *conn);
+
extern const boost::regex web_handle_api_range_R;
void web_handle_api_range(const boost::cmatch &match, struct mg_connection *conn);