From b4498b496e582ffbee4ca7cdc42edc395388d00e Mon Sep 17 00:00:00 2001 From: Jan-Piet Mens Date: Thu, 24 Sep 2015 13:18:16 +0200 Subject: [PATCH] implement topic2tid closes #38 --- README.md | 4 ++++ ot-recorder.c | 28 ++++++++++++++++++++++++++++ udata.h | 1 + 3 files changed, 33 insertions(+) diff --git a/README.md b/README.md index 1e03c04..db7b32b 100644 --- a/README.md +++ b/README.md @@ -540,3 +540,7 @@ server { ### The LMDB database `ocat --load` and `ocat --dump` can be use to load and dump the lmdb database respectively. There is some support for loading/dumping named databases using `--load=xx` or `--dump=xx` to specify the name. Use the mdb utilities to actually perform backups of these. + +#### `topic2tid` + +This named lmdb database is keyed on topic name (`owntracks/jane/phone`). If the topic of an incoming message is found in the database, the `tid` member in the JSON payload is replaced by the value of this key. diff --git a/ot-recorder.c b/ot-recorder.c index 1b0eabe..1dee602 100644 --- a/ot-recorder.c +++ b/ot-recorder.c @@ -451,6 +451,24 @@ void on_message(struct mosquitto *mosq, void *userdata, const struct mosquitto_m } } +#ifdef HAVE_LMDB + /* + * If the topic we are handling is in topic2tid, replace the TID + * in this payload with that from the database. + */ + + if (ud->t2t) { + char newtid[BUFSIZ]; + long blen; + + if ((blen = gcache_get(ud->t2t, m->topic, newtid, sizeof(newtid))) > 0) { + if ((j = json_find_member(json, "tid")) != NULL) + json_remove_from_parent(j); + json_append_member(json, "tid", json_mkstring(newtid)); + } + } +#endif + /* * Chances are high that what we have now contains lat, lon. Attempt to * perform or retrieve reverse-geo. @@ -735,6 +753,7 @@ int main(int argc, char **argv) udata.verbose = TRUE; #ifdef HAVE_LMDB udata.gc = NULL; + udata.t2t = NULL; /* Topic to TID */ #endif #ifdef HAVE_HTTP udata.mgserver = NULL; @@ -953,6 +972,10 @@ int main(int argc, char **argv) revgeo_init(); } +#ifdef HAVE_LMDB + snprintf(err, sizeof(err), "%s/ghash", STORAGEDIR); + ud->t2t = gcache_open(err, "topic2tid", TRUE); +#endif mosquitto_lib_init(); @@ -1074,6 +1097,11 @@ int main(int argc, char **argv) json_delete(ud->topics); +#ifdef HAVE_LMDB + if (ud->t2t) + gcache_close(ud->t2t); +#endif + #ifdef HAVE_HTTP mg_destroy_server(&udata.mgserver); #endif diff --git a/udata.h b/udata.h index cd1ce5f..a4b21ea 100644 --- a/udata.h +++ b/udata.h @@ -21,6 +21,7 @@ struct udata { int verbose; /* TRUE if print verbose messages to stdout */ #ifdef HAVE_LMDB struct gcache *gc; + struct gcache *t2t; /* topic to tid */ #endif #ifdef HAVE_HTTP struct mg_server *mgserver; /* Mongoose */