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/db.cpp | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) (limited to 'datastore-leveldb/src/db.cpp') 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 #include #include -#include #include +#include static std::unordered_map 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(); -- cgit v1.2.1