Support without LMDB

This commit is contained in:
Jan-Piet Mens
2015-10-22 21:10:15 +02:00
parent f0f78964de
commit e43a767f45
6 changed files with 56 additions and 8 deletions
+8
View File
@@ -18,10 +18,13 @@
static int otr_log(lua_State *lua);
static int otr_strftime(lua_State *lua);
#if WITH_LMDB
static int otr_putdb(lua_State *lua);
static int otr_getdb(lua_State *lua);
static struct gcache *LuaDB = NULL;
#endif
/*
* Invoke the function `name' in the Lua script, which _may_ return
@@ -75,11 +78,13 @@ struct luadata *hooks_init(struct udata *ud, char *script)
lua_pushcfunction(ld->L, otr_strftime);
lua_setfield(ld->L, -2, "strftime");
#if WITH_LMDB
lua_pushcfunction(ld->L, otr_putdb);
lua_setfield(ld->L, -2, "putdb");
lua_pushcfunction(ld->L, otr_getdb);
lua_setfield(ld->L, -2, "getdb");
#endif
lua_setglobal(ld->L, "otr");
@@ -269,6 +274,8 @@ static int otr_strftime(lua_State *lua)
return (0);
}
#ifdef WITH_LMDB
/*
* Requires two string arguments: key, value
* These are written into the named LMDB database
@@ -308,4 +315,5 @@ static int otr_getdb(lua_State *lua)
}
return (rc);
}
#endif /* LMDB */
#endif /* WITH_LUA */
+8
View File
@@ -301,7 +301,9 @@ static int dispatch(struct mg_connection *conn, const char *uri)
char *time_from = NULL, *time_to = NULL;
time_t s_lo, s_hi;
JsonNode *json, *obj, *locs;
#ifdef WITH_LMDB
struct udata *ud = (struct udata *)conn->server_param;
#endif
if ((nparts = splitter((char *)uri, "/", uparts)) == -1) {
@@ -441,6 +443,7 @@ static int dispatch(struct mg_connection *conn, const char *uri)
}
}
#ifdef WITH_LMDB
if (nparts == 1 && !strcmp(uparts[0], "q")) {
JsonNode *geo = NULL;
char *lat = field(conn, "lat");
@@ -460,6 +463,7 @@ static int dispatch(struct mg_connection *conn, const char *uri)
return (json_response(conn, geo));
}
#endif
CLEANUP;
// mg_printf_data(conn, "user=[%s], device=[%s]\n", (u) ? u : "<nil>", (d) ? d : "<NIL>");
@@ -492,7 +496,9 @@ static int photo(struct mg_connection *conn)
int ev_handler(struct mg_connection *conn, enum mg_event ev)
{
#ifdef WITH_LMDB
struct udata *ud = (struct udata *)conn->server_param;
#endif
switch (ev) {
case MG_AUTH:
@@ -551,6 +557,7 @@ int ev_handler(struct mg_connection *conn, enum mg_event ev)
if (!strcmp(conn->request_method, "POST")) {
#ifdef WITH_LMDB
if (!strcmp(conn->uri, "/block")) {
int blocked = TRUE;
char buf[BUFSIZ], *u;
@@ -565,6 +572,7 @@ int ev_handler(struct mg_connection *conn, enum mg_event ev)
mg_printf_data(conn, "User %s %s", buf, (blocked) ? "BLOCKED" : "UNblocked");
return (MG_TRUE);
}
#endif
}
/*
* We can't handle this request ourselves. Return
+16 -2
View File
@@ -173,7 +173,9 @@ void usage(char *prog)
printf(" --norevgeo -G disable ghash to reverge-geo lookups\n");
printf(" --precision ghash precision (dflt: %d)\n", GHASHPREC);
printf(" --version -v print version information\n");
#if WITH_LMDB
printf(" --dump / --load [<db>] dump/load content of db (default ghash)\n");
#endif
printf("\n");
printf("Options override these environment variables:\n");
printf(" $OCAT_USERNAME\n");
@@ -211,16 +213,22 @@ void print_versioninfo()
printf("\tGHASHPREC = %d\n", GHASHPREC);
printf("\tDEFAULT_HISTORY_HOURS = %d\n", DEFAULT_HISTORY_HOURS);
printf("\tJSON_INDENT = \"%s\"\n", (JSON_INDENT) ? JSON_INDENT : "NULL");
#ifdef WITH_LMDB
printf("\tMDB VERSION = %s\n", MDB_VERSION_STRING);
#endif
exit(0);
}
int main(int argc, char **argv)
{
char *progname = *argv, *p, *lmdbname = NULL;
char *progname = *argv, *p;
int c;
int list = 0, last = 0, limit = 0, dumpghash = FALSE, loadghash = FALSE;
int list = 0, last = 0, limit = 0;
#if WITH_LMDB
char *lmdbname = NULL;
int dumpghash = FALSE, loadghash = FALSE;
#endif
#if WITH_KILL
int killdata = FALSE;
#endif
@@ -271,8 +279,10 @@ int main(int argc, char **argv)
{ "last", no_argument, 0, 'L'},
{ "fields", required_argument, 0, 1},
{ "precision", required_argument, 0, 2},
#if WITH_LMDB
{ "dump", optional_argument, 0, 3},
{ "load", optional_argument, 0, 4},
#endif
#if WITH_KILL
{ "killdata", no_argument, 0, 'K'},
#endif
@@ -293,6 +303,7 @@ int main(int argc, char **argv)
case 2:
geohash_setprec(atoi(optarg));
break;
#if WITH_LMDB
case 3:
dumpghash = TRUE;
if (optarg)
@@ -303,6 +314,7 @@ int main(int argc, char **argv)
if (optarg)
lmdbname = strdup(optarg);
break;
#endif /* LMDB */
case 'v':
print_versioninfo();
break;
@@ -376,6 +388,7 @@ int main(int argc, char **argv)
// printf("lmdbname = %s\n", (lmdbname) ? lmdbname : "NULL");
#ifdef WITH_LMDB
if (loadghash) {
storage_gcache_load(lmdbname);
exit(0);
@@ -385,6 +398,7 @@ int main(int argc, char **argv)
storage_gcache_dump(lmdbname);
exit(0);
}
#endif
storage_init(revgeo);
+13 -3
View File
@@ -747,6 +747,7 @@ void on_message(struct mosquitto *mosq, void *userdata, const struct mosquitto_m
cached = FALSE;
if (ud->revgeo == TRUE) {
#ifdef WITH_LMDB
if ((geo = gcache_json_get(ud->gc, UB(ghash))) != NULL) {
/* Habemus cached data */
@@ -775,6 +776,11 @@ void on_message(struct mosquitto *mosq, void *userdata, const struct mosquitto_m
}
}
}
#else /* !LMDB */
if ((geo = revgeo(lat, lon, addr, cc)) != NULL) {
;
}
#endif /* LMDB */
} else {
utstring_printf(cc, "??");
utstring_printf(addr, "n.a.");
@@ -860,9 +866,11 @@ void on_message(struct mosquitto *mosq, void *userdata, const struct mosquitto_m
#endif
#ifdef WITH_LUA
# ifdef WITH_LMDB
if (ud->luadata && !pingping) {
hooks_hook(ud, m->topic, json);
}
# endif /* LMDB */
#endif
if (ud->verbose) {
@@ -1041,8 +1049,8 @@ int main(int argc, char **argv)
udata.mgserver = NULL;
#endif
#ifdef WITH_LUA
udata.luadata = NULL;
# ifdef WITH_LMDB
udata.luadata = NULL;
udata.luadb = NULL;
# endif /* WITH_LMDB */
#endif /* WITH_LUA */
@@ -1185,7 +1193,9 @@ int main(int argc, char **argv)
*/
if (initialize == TRUE) {
#ifdef WITH_LMDB
struct gcache *gt;
#endif
char path[BUFSIZ], *pp;
snprintf(path, BUFSIZ, "%s/ghash", STORAGEDIR);
@@ -1317,7 +1327,7 @@ int main(int argc, char **argv)
# endif
#endif
#ifdef WITH_LUA
#if WITH_LUA && WITH_LMDB
/*
* If option for lua-script has not been given, ignore all hooks.
*/
@@ -1474,7 +1484,7 @@ int main(int argc, char **argv)
mg_destroy_server(&udata.mgserver);
#endif
#ifdef WITH_LUA
#if WITH_LUA && WITH_LMDB
hooks_exit(ud->luadata, "recorder stops");
#endif
mosquitto_disconnect(mosq);
+10 -2
View File
@@ -43,23 +43,28 @@ char STORAGEDIR[BUFSIZ] = STORAGEDEFAULT;
#define LINESIZE 8192
#ifdef WITH_LMDB
static struct gcache *gc = NULL;
#endif
void storage_init(int revgeo)
{
char path[BUFSIZ];
setenv("TZ", "UTC", 1);
#ifdef WITH_LMDB
if (revgeo) {
char path[BUFSIZ];
snprintf(path, BUFSIZ, "%s/ghash", STORAGEDIR);
gc = gcache_open(path, NULL, TRUE);
if (gc == NULL) {
olog(LOG_ERR, "storage_init(): gc is NULL");
}
}
#endif
}
#ifdef WITH_LMDB
void storage_gcache_dump(char *lmdbname)
{
char path[BUFSIZ];
@@ -75,15 +80,18 @@ void storage_gcache_load(char *lmdbname)
snprintf(path, BUFSIZ, "%s/ghash", STORAGEDIR);
gcache_load(path, lmdbname);
}
#endif /* !LMDB */
void get_geo(JsonNode *o, char *ghash)
{
#ifdef WITH_LMDB
JsonNode *geo;
if ((geo = gcache_json_get(gc, ghash)) != NULL) {
json_copy_to_object(o, geo, FALSE);
json_delete(geo);
}
#endif
}
+1 -1
View File
@@ -30,8 +30,8 @@ struct udata {
struct mg_server *mgserver; /* Mongoose */
#endif
#ifdef WITH_LUA
# ifdef WITH_LMDB
struct luadata *luadata; /* Lua stuff */
# ifdef WITH_LMDB
struct gcache *luadb; /* lmdb named database 'luadb' */
# endif
#endif