Commit b1053f49 authored by Marek Vavruša's avatar Marek Vavruša

lib/cache: open with generic storage options

the api is still a global variable, but it would be the
best if the namedb_t carried the pointer to the api,
so it needs to change in libknot later
parent 9843c55c
......@@ -31,30 +31,19 @@
/* Key size */
#define KEY_SIZE (sizeof(uint8_t) + KNOT_DNAME_MAXLEN + sizeof(uint16_t))
/** Used cache storage engine (default LMDB) */
/**
* Used cache storage engine (default LMDB)
* @todo namedb_t should change so it always contains a pointer
* to its API, so it can be carried around instead of keeping it in a
* global variable.
*/
const namedb_api_t *(*kr_cache_storage)(void) = namedb_lmdb_api;
#define db_api kr_cache_storage()
/** Generic storage options */
union storage_opts {
struct namedb_lmdb_opts lmdb;
};
namedb_t *kr_cache_open(const char *handle, mm_ctx_t *mm, size_t maxsize)
namedb_t *kr_cache_open(void *opts, mm_ctx_t *mm)
{
if (!handle || maxsize == 0) {
return NULL;
}
union storage_opts opts;
memset(&opts, 0, sizeof(opts));
if (db_api == namedb_lmdb_api()) {
opts.lmdb.mapsize = maxsize;
opts.lmdb.path = handle;
}
namedb_t *db = NULL;
int ret = db_api->init(&db, mm, &opts);
int ret = db_api->init(&db, mm, opts);
if (ret != 0) {
return NULL;
}
......
......@@ -37,17 +37,22 @@ struct kr_cache_entry
uint8_t data[];
};
/** Used storage API for cache (default LMDB) */
/** Used storage backend for cache (default LMDB) */
extern const namedb_api_t *(*kr_cache_storage)(void);
/** Replace used cache storage backend. */
static inline void kr_cache_storage_set(const namedb_api_t *(*api)(void))
{
kr_cache_storage = api;
}
/**
* Open/create persistent cache in given path.
* @param handle Configuration string (e.g. path to existing directory where the DB should be created)
* Open/create cache with provided storage options.
* @param storage_opts Storage-specific options (may be NULL for default)
* @param mm Memory context.
* @param maxsize Maximum database size (bytes)
* @return database instance or NULL
*/
namedb_t *kr_cache_open(const char *handle, mm_ctx_t *mm, size_t maxsize);
namedb_t *kr_cache_open(void *storage_opts, mm_ctx_t *mm);
/**
* Close persistent cache.
......@@ -81,7 +86,7 @@ void kr_cache_txn_abort(namedb_txn_t *txn);
/**
* Peek the cache for asset (name, type, tag)
* @note The 'drift' is the time passed between the cache time of the RRSet and now (in seconds).
* @note The 'drift' is the time passed between the inception time and now (in seconds).
* @param txn transaction instance
* @param tag asset tag
* @param name asset name
......
......@@ -15,6 +15,7 @@
*/
#include <libknot/internal/mempool.h>
#include <libknot/internal/namedb/namedb_lmdb.h>
#include "tests/test.h"
#include "lib/cache.h"
......@@ -32,8 +33,6 @@ const char *global_env;
/* Test invalid parameters. */
static void test_invalid(void **state)
{
assert_null((void *)kr_cache_open(NULL, NULL, 0));
assert_null((void *)kr_cache_open(global_env, NULL, 0));
assert_int_not_equal(kr_cache_txn_begin(NULL, &global_txn, 0), 0);
assert_int_not_equal(kr_cache_txn_begin(&global_env, NULL, 0), 0);
assert_int_not_equal(kr_cache_txn_commit(NULL), 0);
......@@ -50,7 +49,11 @@ static void test_invalid(void **state)
/* Test cache open */
static void test_open(void **state)
{
*state = kr_cache_open(global_env, &global_mm, CACHE_SIZE);
struct namedb_lmdb_opts opts;
memset(&opts, 0, sizeof(opts));
opts.path = global_env;
opts.mapsize = CACHE_SIZE;
*state = kr_cache_open(&opts, &global_mm);
assert_non_null(*state);
}
......
......@@ -15,6 +15,7 @@
*/
#include <Python.h>
#include <libknot/internal/namedb/namedb_lmdb.h>
#include "tests/test.h"
#include "lib/rplan.h"
......@@ -67,7 +68,11 @@ static PyObject* init(PyObject* self, PyObject* args)
global_tmpdir = test_tmpdir_create();
assert(global_tmpdir);
global_context.cache = kr_cache_open(global_tmpdir, &global_mm, 100 * 4096);
struct namedb_lmdb_opts opts;
memset(&opts, 0, sizeof(opts));
opts.path = global_tmpdir;
opts.mapsize = 100 * 4096;
global_context.cache = kr_cache_open(&opts, &global_mm);
assert(global_context.cache);
/* No configuration parsing support yet. */
......
......@@ -72,11 +72,11 @@ static void test_get(void **state)
in = malloc(strlen(dict[23])+1);
strcpy(in, dict[23]);
assert_int_equal(map_get(tree, in), dict[23]);
assert_int_equal(map_get(tree, notin), NULL);
assert_int_equal(map_get(tree, ""), NULL);
assert_true(map_get(tree, in) == dict[23]);
assert_true(map_get(tree, notin) == NULL);
assert_true(map_get(tree, "") == NULL);
in[strlen(in)/2] = '\0';
assert_int_equal(map_get(tree, in), NULL);
assert_true(map_get(tree, in) == NULL);
free(in);
}
......
......@@ -23,20 +23,20 @@ static void test_rplan_params(void **state)
/* NULL rplan */
assert_int_equal(kr_rplan_init(NULL, NULL, NULL), KNOT_EINVAL);
assert_null(kr_rplan_push(NULL, NULL, NULL, 0, 0));
assert_null((void *)kr_rplan_push(NULL, NULL, NULL, 0, 0));
assert_int_equal(kr_rplan_pop(NULL, NULL), KNOT_EINVAL);
assert_true(kr_rplan_empty(NULL));
assert_null(kr_rplan_current(NULL));
assert_true(kr_rplan_empty(NULL) == true);
assert_null((void *)kr_rplan_current(NULL));
kr_rplan_deinit(NULL);
/* NULL mandatory parameters */
struct kr_rplan rplan;
assert_int_equal(kr_rplan_init(&rplan, NULL, NULL), KNOT_EOK);
assert_null(kr_rplan_push(&rplan, NULL, NULL, 0, 0));
assert_null((void *)kr_rplan_push(&rplan, NULL, NULL, 0, 0));
assert_int_equal(kr_rplan_pop(&rplan, NULL), KNOT_EINVAL);
assert_true(kr_rplan_empty(&rplan));
assert_null(kr_rplan_current(&rplan));
assert_true(kr_rplan_empty(&rplan) == true);
assert_null((void *)kr_rplan_current(&rplan));
kr_rplan_deinit(&rplan);
}
......@@ -52,7 +52,7 @@ static void test_rplan_push(void **state)
kr_rplan_init(&rplan, &context, &mm);
/* Push query. */
assert_non_null(kr_rplan_push(&rplan, NULL, (knot_dname_t *)"", 0, 0));
assert_non_null((void *)kr_rplan_push(&rplan, NULL, (knot_dname_t *)"", 0, 0));
kr_rplan_deinit(&rplan);
}
......
......@@ -27,8 +27,8 @@ static void test_zonecut_params(void **state)
kr_zonecut_set(NULL, NULL);
kr_zonecut_set(&cut, NULL);
assert_int_not_equal(kr_zonecut_add(NULL, NULL, NULL), 0);
assert_null(kr_zonecut_find(NULL, NULL));
assert_null(kr_zonecut_find(&cut, NULL));
assert_null((void *)kr_zonecut_find(NULL, NULL));
assert_null((void *)kr_zonecut_find(&cut, NULL));
assert_int_not_equal(kr_zonecut_set_sbelt(NULL), 0);
assert_int_not_equal(kr_zonecut_find_cached(NULL, NULL, 0), 0);
}
......
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