void gcache_load(char *path, char *lmdbname) { struct gcache *gc; char buf[8192], *bp; if ((gc = gcache_open(path, lmdbname, FALSE)) == NULL) { olog(LOG_ERR, "gcache_load: gcache_open"); return; } while (fgets(buf, sizeof(buf), stdin) != NULL) { if ((bp = strchr(buf, '\r')) != NULL) *bp = 0; if ((bp = strchr(buf, '\n')) != NULL) *bp = 0; if ((bp = strchr(buf, ' ')) != NULL) { *bp = 0; if (gcache_put(gc, buf, bp+1) != 0) { fprintf(stderr, "Cannot load key\n"); } } } gcache_close(gc); }
void storage_init(int revgeo) { char path[BUFSIZ]; setenv("TZ", "UTC", 1); if (revgeo) { snprintf(path, BUFSIZ, "%s/ghash", STORAGEDIR); gc = gcache_open(path, NULL, TRUE); if (gc == NULL) { olog(LOG_ERR, "storage_init(): gc is NULL"); } } }
void gcache_dump(char *path, char *lmdbname) { struct gcache *gc; MDB_val key, data; MDB_txn *txn; MDB_cursor *cursor; int rc; if ((gc = gcache_open(path, lmdbname, TRUE)) == NULL) { fprintf(stderr, "Cannot open lmdb/%s at %s\n", lmdbname ? lmdbname : "NULL", path); return; } key.mv_size = 0; key.mv_data = NULL; rc = mdb_txn_begin(gc->env, NULL, MDB_RDONLY, &txn); if (rc) { olog(LOG_ERR, "gcache_dump: mdb_txn_begin: (%d) %s", rc, mdb_strerror(rc)); gcache_close(gc); return; } rc = mdb_cursor_open(txn, gc->dbi, &cursor); /* -1 because we 0-terminate strings */ while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT)) == 0) { printf("%*.*s %*.*s\n", (int)key.mv_size, (int)key.mv_size, (char *)key.mv_data, (int)data.mv_size - 1, (int)data.mv_size - 1, (char *)data.mv_data); } mdb_cursor_close(cursor); mdb_txn_commit(txn); gcache_close(gc); }