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);
- MDB_val k = make_key_str(KASPDBKEY_PARAMS, zone_name, params->id);
- return knot_lmdb_quick_insert(db, k, v);
+ MDB_val val = params_serialize(params);
+ MDB_val key = make_key_str(KASPDBKEY_PARAMS, zone_name, params->id);
+ MDB_val del_prefix = make_key_str(KASPDBKEY_TRASH, NULL, params->id);
+ knot_lmdb_txn_t txn = { 0 };
+ knot_lmdb_begin(db, &txn, true);
+ knot_lmdb_insert(&txn, &key, &val);
+ // More than one KASPDBKEY_TRASH aren't expected, but none must remain.
+ knot_lmdb_del_prefix(&txn, &del_prefix);
+ free(key.mv_data);
+ free(val.mv_data);
+ free(del_prefix.mv_data);
+ knot_lmdb_commit(&txn);
+
+ return txn.ret;
}
int kasp_db_share_key(knot_lmdb_db_t *db, const knot_dname_t *zone_from,
/*!
* \brief Add a key to the DB (possibly overwrite) and link it to a zone.
+ * Remove all trash records related to the key, if there are any.
*
* Stores new key with given params into KASP db. If a key with the same ID had been present
* in KASP db already, its params get silently overwritten by those new params.
- * Moreover, the key ID is linked to the zone.
+ * Moreover, the key ID is linked to the zone and all related trash records are removed.
*
* \param db KASP db
* \param zone_name name of the zone the new key shall belong to
for (size_t i = 0; i < zone->num_keys; i++) {
kaspkey2params(&zone->keys[i], &parm);
- // Force overwrite already existing key-val pairs.
+ // Force overwrite already existing key-val pairs
+ // and remove related trash records.
int ret = kasp_db_add_key(kdb, zone_name, &parm);
if (ret != KNOT_EOK) {
return ret;