Commit a657a9da authored by Jan Včelák's avatar Jan Včelák 🚀
Browse files

namedb: API change, allow init to return an error

parent a7b3267c
......@@ -42,7 +42,7 @@ struct namedb_api {
/* Context operations */
knot_namedb_t *(*init)(const char *config, mm_ctx_t *mm);
int (*init)(const char *config, knot_namedb_t **db, mm_ctx_t *mm);
void (*deinit)(knot_namedb_t *db);
/* Transactions */
......
......@@ -105,22 +105,24 @@ static void dbase_close(struct lmdb_env *env)
mdb_env_close(env->env);
}
static knot_namedb_t *init(const char *config, mm_ctx_t *mm)
static int init(const char *config, knot_namedb_t **db_ptr, mm_ctx_t *mm)
{
struct lmdb_env *env = mm_alloc(mm, sizeof(struct lmdb_env));
if (env == NULL) {
return NULL;
return KNOT_ENOMEM;
}
memset(env, 0, sizeof(struct lmdb_env));
int ret = dbase_open(env, config);
if (ret != 0) {
mm_free(mm, env);
return NULL;
return KNOT_ERROR;
}
env->pool = mm;
return env;
*db_ptr = env;
return KNOT_EOK;
}
static void deinit(knot_namedb_t *db)
......
......@@ -18,9 +18,20 @@
#include "common-knot/hattrie/hat-trie.h"
#include "libknot/errcode.h"
static knot_namedb_t *init(const char *handle, mm_ctx_t *mm)
static int init(const char *config, knot_namedb_t **db, mm_ctx_t *mm)
{
return hattrie_create_n(TRIE_BUCKET_SIZE, mm);
if (config != NULL || db == NULL) {
return KNOT_EINVAL;
}
hattrie_t *trie = hattrie_create_n(TRIE_BUCKET_SIZE, mm);
if (!trie) {
return KNOT_ENOMEM;
}
*db = trie;
return KNOT_EOK;
}
static void deinit(knot_namedb_t *db)
......
......@@ -56,12 +56,13 @@ static void namedb_test_set(unsigned nkeys, char **keys, char *dbid,
}
/* Create database */
knot_namedb_t *db = api->init(dbid, pool);
ok(db != NULL, "%s: create", api->name);
knot_namedb_t *db = NULL;
int ret = api->init(dbid, &db, pool);
ok(ret == KNOT_EOK && db != NULL, "%s: create", api->name);
/* Start WR transaction. */
knot_txn_t txn;
int ret = api->txn_begin(db, &txn, 0);
ret = api->txn_begin(db, &txn, 0);
ok(ret == KNOT_EOK, "%s: txn_begin(WR)", api->name);
/* Insert keys */
......@@ -169,7 +170,7 @@ int main(int argc, char *argv[])
/* Execute test set for all backends. */
namedb_test_set(nkeys, keys, dbid, namedb_lmdb_api(), &pool);
namedb_test_set(nkeys, keys, dbid, namedb_trie_api(), &pool);
namedb_test_set(nkeys, keys, NULL, namedb_trie_api(), &pool);
/* Cleanup */
mp_delete(pool.ctx);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment