From 8de2253c28d95907436b2e26f0805dd83e85a8ce Mon Sep 17 00:00:00 2001 From: Yves Fischer Date: Fri, 19 Apr 2013 21:54:11 +0200 Subject: leveldb: /api/value mit server geniertem timestamp --- datastore-leveldb/src/web.cpp | 44 +++++++++++++++++++++++++------------------ 1 file changed, 26 insertions(+), 18 deletions(-) (limited to 'datastore-leveldb/src/web.cpp') 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 -#include +#include #include -#include 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::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, -- cgit v1.2.1