Commit 230a6d4f authored by Libor Peltan's avatar Libor Peltan Committed by Daniel Salzman

kasp db: better organized code for clearing orphans in kasp db

parent f0e8a6db
......@@ -1065,19 +1065,8 @@ static int orphans_purge(ctl_args_t *args)
if (args->data[KNOT_CTL_IDX_ZONE] == NULL) {
// Purge KASP DB.
if (only_orphan || MATCH_AND_FILTER(args, CTL_FILTER_PURGE_KASPDB)) {
list_t zones;
if (knot_lmdb_open(&args->server->kaspdb) == KNOT_EOK &&
kasp_db_list_zones(&args->server->kaspdb, &zones) == KNOT_EOK) {
ptrnode_t *zn;
WALK_LIST(zn, zones) {
knot_dname_t *zone_name = (knot_dname_t *)zn->d;
if (!zone_exists(zone_name, &args->server->zone_db)) {
(void)kasp_db_delete_all(&args->server->kaspdb, zone_name);
}
knot_dname_free(zone_name, NULL);
}
ptrlist_free(&zones, NULL);
}
(void)kasp_db_sweep(&args->server->kaspdb,
zone_exists, args->server->zone_db);
}
// Purge zone journals of unconfigured zones.
......
......@@ -223,6 +223,29 @@ int kasp_db_delete_all(knot_lmdb_db_t *db, const knot_dname_t *zone)
return txn.ret;
}
int kasp_db_sweep(knot_lmdb_db_t *db, sweep_cb keep_zone, void *cb_data)
{
if (!knot_lmdb_exists(db)) {
return KNOT_EOK;
}
int ret = knot_lmdb_open(db);
if (ret != KNOT_EOK) {
return ret;
}
knot_lmdb_txn_t txn = { 0 };
knot_lmdb_begin(db, &txn, true);
bool found = knot_lmdb_first(&txn);
while (found) {
if (*(const uint8_t *)txn.cur_key.mv_data != KASPDBKEY_POLICYLAST &&
!keep_zone((const knot_dname_t *)txn.cur_key.mv_data + 1, cb_data)) {
knot_lmdb_del_cur(&txn);
}
found = knot_lmdb_next(&txn);
}
knot_lmdb_commit(&txn);
return txn.ret;
}
int kasp_db_add_key(knot_lmdb_db_t *db, const knot_dname_t *zone_name, const key_params_t *params)
{
MDB_val v = params_serialize(params);
......
......@@ -64,6 +64,17 @@ int kasp_db_delete_key(knot_lmdb_db_t *db, const knot_dname_t *zone_name, const
*/
int kasp_db_delete_all(knot_lmdb_db_t *db, const knot_dname_t *zone_name);
/*!
* \brief Selectively delete zones from the database.
*
* \param db KASP dababase.
* \param keep_zone Filtering callback.
* \param cb_data Data passed to callback function.
*
* \return KNOT_E*
*/
int kasp_db_sweep(knot_lmdb_db_t *db, sweep_cb keep_zone, void *cb_data);
/*!
* \brief Add a key to the DB (possibly overwrite) and link it to a zone.
*
......
......@@ -19,6 +19,7 @@
#include <lmdb.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <pthread.h>
......@@ -57,6 +58,14 @@ typedef enum {
KNOT_LMDB_FORCE = 4, /*! \brief If no matching key found, consider it a transaction failure (KNOT_ENOENT). */
} knot_lmdb_find_t;
/*!
* \brief Callback used in sweep functions.
*
* \retval true for zones to preserve.
* \retval false for zones to remove.
*/
typedef bool (*sweep_cb)(const uint8_t *zone, void *data);
/*!
* \brief Initialise the DB handling structure.
*
......
......@@ -76,14 +76,6 @@ int zone_timers_write(knot_lmdb_db_t *db, const knot_dname_t *zone,
*/
int zone_timers_write_all(knot_lmdb_db_t *db, knot_zonedb_t *zonedb);
/*!
* \brief Callback used in \ref zone_timers_sweep.
*
* \retval true for zones to preserve.
* \retval false for zones to remove.
*/
typedef bool (*sweep_cb)(const knot_dname_t *zone, void *data);
/*!
* \brief Selectively delete zones from the database.
*
......
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