diff options
author | Yves Fischer <yvesf-git@xapek.org> | 2013-04-27 20:20:44 +0200 |
---|---|---|
committer | Yves Fischer <yvesf-git@xapek.org> | 2013-04-27 20:21:07 +0200 |
commit | b8677cb17b8c4fdf2af1fcd0ead311b9bf2cfead (patch) | |
tree | 473ebaa82a66f86f2a7544f2cfe6567b43c4675a /datastore-leveldb | |
parent | 705af77c2fef7a27150b741cde228e2fe55a029b (diff) | |
download | ebus-alt-b8677cb17b8c4fdf2af1fcd0ead311b9bf2cfead.tar.gz ebus-alt-b8677cb17b8c4fdf2af1fcd0ead311b9bf2cfead.zip |
datastore: name refactorings
Diffstat (limited to 'datastore-leveldb')
-rw-r--r-- | datastore-leveldb/src/db.cpp | 10 | ||||
-rw-r--r-- | datastore-leveldb/src/db.h | 7 | ||||
-rw-r--r-- | datastore-leveldb/src/server.cpp | 5 | ||||
-rw-r--r-- | datastore-leveldb/src/web.cpp | 44 |
4 files changed, 33 insertions, 33 deletions
diff --git a/datastore-leveldb/src/db.cpp b/datastore-leveldb/src/db.cpp index 58889dd..1ca8d50 100644 --- a/datastore-leveldb/src/db.cpp +++ b/datastore-leveldb/src/db.cpp @@ -21,14 +21,14 @@ static bool sensor_name_is_sane(std::string& name) { return true; } -std::string make_key(const uint64_t timestamp) { +std::string db_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) { +leveldb::DB *db_get(std::string& name) { getDBmutex.lock(); if (dbs.find(name) == dbs.end()) { if (not sensor_name_is_sane(name)) { @@ -55,14 +55,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); + leveldb::DB *db = db_get(name); if (db == nullptr) return false; - auto status = db->Put(leveldb::WriteOptions(), make_key(timestamp), value); + auto status = db->Put(leveldb::WriteOptions(), db_make_key(timestamp), value); return status.ok(); } -void closeDB() { +void db_close() { std::cout << "Close Databases: "; auto it = dbs.begin(); while (it != dbs.end()) { diff --git a/datastore-leveldb/src/db.h b/datastore-leveldb/src/db.h index 9c4c9fb..5e31b00 100644 --- a/datastore-leveldb/src/db.h +++ b/datastore-leveldb/src/db.h @@ -3,13 +3,12 @@ #include <leveldb/db.h> -leveldb::DB *getDB(std::string& name); +leveldb::DB *db_get(std::string& name); bool db_insert(std::string& name, const uint64_t timestamp, std::string& value); -void closeDB(); +void db_close(); - -std::string make_key(const uint64_t timestamp); +std::string db_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 1337466..8dd0ee2 100644 --- a/datastore-leveldb/src/server.cpp +++ b/datastore-leveldb/src/server.cpp @@ -29,7 +29,6 @@ int begin_request_handler(struct mg_connection *conn) { return 1; } } - return 0; } @@ -38,14 +37,14 @@ struct sigaction old_action; // Stop the server. void sigint_handler(int s) { mg_stop(ctx); - closeDB(); + db_close(); exit(0); } int main(int argc, char **argv) { struct mg_callbacks callbacks; const char *options[] = { - "num_threads", "10", + "num_threads", "8", "listening_ports", "8080", "document_root", "wwwroot", NULL}; diff --git a/datastore-leveldb/src/web.cpp b/datastore-leveldb/src/web.cpp index d938a66..cbadc57 100644 --- a/datastore-leveldb/src/web.cpp +++ b/datastore-leveldb/src/web.cpp @@ -11,13 +11,23 @@ static inline uint64_t now() { std::chrono::high_resolution_clock::now().time_since_epoch()).count(); } +// uint64_t is long inline void strToL(unsigned long int *l, std::string s) { *l = std::stoul(s); } +// uint64_t is long long inline void strToL(unsigned long long int *l, std::string s) { *l = std::stoull(s); } +static inline void reply_header(struct mg_connection *conn, const bool ok, const char *msg, const char *extra = "") { + mg_printf(conn, + "HTTP/1.1 %s %s\r\n" + "%s", + (ok) ? "200" : "500", + msg, + extra); +} static inline bool parse_key(leveldb::Slice &&key, uint64_t *value) { if (key.size() != 20+3) @@ -36,9 +46,9 @@ void web_handle_api_value(const boost::cmatch &match, struct mg_connection *conn 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"); + reply_header(conn, true, "OK Value received", "\r\n"); } else { - mg_printf(conn, "HTTP/1.1 500 Internal Error\r\n\r\n"); + reply_header(conn, false, "Internal Error", "\r\n"); } } @@ -52,9 +62,9 @@ void web_handle_api_value_timestamp(const boost::cmatch &match, struct mg_connec 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"); + reply_header(conn, true, "OK Value received", "\r\n"); } else { - mg_printf(conn, "HTTP/1.1 500 Internal Error\r\n\r\n"); + reply_header(conn, false, "Internal Error", "\r\n"); } } @@ -90,20 +100,16 @@ void web_handle_api_range(const boost::cmatch &match, struct mg_connection *conn std::string sensor(match[1].str()); uint64_t start; strToL(&start, match[2].str()); uint64_t end; strToL(&end, match[3].str()); - std::string key_start(std::move(make_key(start))); - std::string key_end(std::move(make_key(end))); + std::string key_start(std::move(db_make_key(start))); + std::string key_end(std::move(db_make_key(end))); - leveldb::DB *db = getDB(sensor); + leveldb::DB *db = db_get(sensor); if (db == nullptr) { - mg_printf(conn, "HTTP/1.1 500 Internal Error\r\n" - "\r\n"); + reply_header(conn, false, "Internal Error", "\r\n"); return; } - mg_printf(conn, - "HTTP/1.1 200 OK\r\n" - "Content-Type: application/json\r\n" - "\r\n"); + reply_header(conn, true, "OK", "Content-Type: application/json\r\n\r\n"); std::ostringstream out; out << "{\"sensor\":\"" << sensor << "\", " @@ -134,17 +140,13 @@ void web_handle_api_range_size(const boost::cmatch &match, struct mg_connection uint64_t end; strToL(&end, match[3].str()); uint64_t size; strToL(&size, match[4].str()); - leveldb::DB *db = getDB(sensor); + leveldb::DB *db = db_get(sensor); if (db == nullptr) { - mg_printf(conn, "HTTP/1.1 500 Internal Error\r\n" - "\r\n"); + reply_header(conn, false, "Internal Error", "\r\n"); return; } - mg_printf(conn, - "HTTP/1.1 200 OK\r\n" - "Content-Type: application/json\r\n" - "\r\n"); + reply_header(conn, true, "OK", "Content-Type: application/json\r\n\r\n"); uint64_t step = std::max((uint64_t)1, (end-start) / size); @@ -161,7 +163,7 @@ void web_handle_api_range_size(const boost::cmatch &match, struct mg_connection std::ostringstream outbuf; leveldb::Iterator* it = db->NewIterator(leveldb::ReadOptions()); for (uint64_t key = start; key < end; key += step) { - it->Seek(make_key(key)); + it->Seek(db_make_key(key)); if (!it->Valid()) continue; if (first) first = false; |