storage: add support for LMDB "named" databases

This commit is contained in:
Jan-Piet Mens 2015-09-07 13:48:47 +02:00
parent bfe49ff1b5
commit 03c119b43b
2 changed files with 27 additions and 12 deletions

View File

@ -27,11 +27,15 @@
#include "gcache.h" #include "gcache.h"
#include "util.h" #include "util.h"
struct gcache *gcache_open(char *path, int rdonly) /*
* dbname is an named LMDB database; may be NULL.
*/
struct gcache *gcache_open(char *path, char *dbname, int rdonly)
{ {
MDB_txn *txn = NULL; MDB_txn *txn = NULL;
int rc; int rc;
unsigned int flags = 0; unsigned int flags = 0, dbiflags = 0;
struct gcache *gc; struct gcache *gc;
if (!is_directory(path)) { if (!is_directory(path)) {
@ -46,6 +50,8 @@ struct gcache *gcache_open(char *path, int rdonly)
if (rdonly) { if (rdonly) {
flags |= MDB_RDONLY; flags |= MDB_RDONLY;
} else {
dbiflags = MDB_CREATE;
} }
rc = mdb_env_create(&gc->env); rc = mdb_env_create(&gc->env);
@ -57,6 +63,13 @@ struct gcache *gcache_open(char *path, int rdonly)
mdb_env_set_mapsize(gc->env, LMDB_DB_SIZE); mdb_env_set_mapsize(gc->env, LMDB_DB_SIZE);
rc = mdb_env_set_maxdbs(gc->env, 10);
if (rc != 0) {
fprintf(stderr, "mdb_env_set_maxdbs%s\n", mdb_strerror(rc));
free(gc);
return (NULL);
}
rc = mdb_env_open(gc->env, path, flags, 0664); rc = mdb_env_open(gc->env, path, flags, 0664);
if (rc != 0) { if (rc != 0) {
fprintf(stderr, "%s\n", mdb_strerror(rc)); fprintf(stderr, "%s\n", mdb_strerror(rc));
@ -74,10 +87,9 @@ struct gcache *gcache_open(char *path, int rdonly)
return (NULL); return (NULL);
} }
rc = mdb_dbi_open(txn, NULL, 0, &gc->dbi); rc = mdb_dbi_open(txn, dbname, dbiflags, &gc->dbi);
if (rc != 0) { if (rc != 0) {
write(1, "HELLO\n", 6); fprintf(stderr, "mdb_dbi_open: %s\n", mdb_strerror(rc));
fprintf(stderr, "%s\n", mdb_strerror(rc));
mdb_txn_abort(txn); mdb_txn_abort(txn);
mdb_env_close(gc->env); mdb_env_close(gc->env);
free(gc); free(gc);
@ -154,19 +166,20 @@ int gcache_json_put(struct gcache *gc, char *ghash, JsonNode *geo)
return (rc); return (rc);
} }
int gcache_get(struct gcache *gc, char *k) long gcache_get(struct gcache *gc, char *k, char *buf, long buflen)
{ {
MDB_val key, data; MDB_val key, data;
MDB_txn *txn; MDB_txn *txn;
int rc; int rc;
long len;
if (gc == NULL) if (gc == NULL)
return (1); return (-1);
rc = mdb_txn_begin(gc->env, NULL, MDB_RDONLY, &txn); rc = mdb_txn_begin(gc->env, NULL, MDB_RDONLY, &txn);
if (rc) { if (rc) {
fprintf(stderr, "gcache_get: cannot txn_begin: (%d) %s\n", rc, mdb_strerror(rc)); fprintf(stderr, "gcache_get: cannot txn_begin: (%d) %s\n", rc, mdb_strerror(rc));
return (1); return (-1);
} }
key.mv_data = k; key.mv_data = k;
@ -180,10 +193,12 @@ int gcache_get(struct gcache *gc, char *k)
printf(" [%s] not found\n", k); printf(" [%s] not found\n", k);
} }
} else { } else {
printf("%s\n", (char *)data.mv_data); len = (data.mv_size < buflen) ? data.mv_size : buflen;
memcpy(buf, data.mv_data, len);
// printf("%s\n", (char *)data.mv_data);
} }
mdb_txn_commit(txn); mdb_txn_commit(txn);
return (0); return (len);
} }
/* /*

View File

@ -13,11 +13,11 @@ struct gcache {
MDB_dbi dbi; MDB_dbi dbi;
}; };
struct gcache *gcache_open(char *path, int rdonly); struct gcache *gcache_open(char *path, char *dbname, int rdonly);
void gcache_close(struct gcache *); void gcache_close(struct gcache *);
int gcache_put(struct gcache *, char *ghash, char *payload); int gcache_put(struct gcache *, char *ghash, char *payload);
int gcache_json_put(struct gcache *, char *ghash, JsonNode *geo); int gcache_json_put(struct gcache *, char *ghash, JsonNode *geo);
int gcache_get(struct gcache *, char *key); long gcache_get(struct gcache *, char *key, char *buf, long buflen);
JsonNode *gcache_json_get(struct gcache *, char *key); JsonNode *gcache_json_get(struct gcache *, char *key);
#endif #endif