diff options
author | Yves Fischer <yvesf-git@xapek.org> | 2013-04-19 21:54:11 +0200 |
---|---|---|
committer | Yves Fischer <yvesf-git@xapek.org> | 2013-04-19 21:54:11 +0200 |
commit | 8de2253c28d95907436b2e26f0805dd83e85a8ce (patch) | |
tree | c05a4792108752db15c0429655271680a02aa853 /datastore-leveldb/src/db.cpp | |
parent | c2d48c0082cdeb38eff3a3f8eb68ae7758fa4439 (diff) | |
download | ebus-alt-8de2253c28d95907436b2e26f0805dd83e85a8ce.tar.gz ebus-alt-8de2253c28d95907436b2e26f0805dd83e85a8ce.zip |
leveldb: /api/value mit server geniertem timestamp
Diffstat (limited to 'datastore-leveldb/src/db.cpp')
-rw-r--r-- | datastore-leveldb/src/db.cpp | 20 |
1 files changed, 18 insertions, 2 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(); |