From: Petr Špaček Date: Wed, 15 Aug 2018 15:53:29 +0000 (+0200) Subject: cache: return number of removed entries from remove_* functions X-Git-Tag: v3.0.0~1^2~14 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=6a8e28b844f3f58fb016bba46b65898a3dc5cc91;p=thirdparty%2Fknot-resolver.git cache: return number of removed entries from remove_* functions It is more convenient to return 0 instead of ENOENT. --- diff --git a/lib/cache/api.c b/lib/cache/api.c index eae62d401..a3e3bb794 100644 --- a/lib/cache/api.c +++ b/lib/cache/api.c @@ -775,13 +775,7 @@ int kr_cache_remove(struct kr_cache *cache, const knot_dname_t *name, uint16_t t if (ret) return kr_error(ret); knot_db_val_t key = key_exact_type(k, type); - ret = cache_op(cache, remove, &key, 1); - switch (ret) { - case 0: return 1; - case -ABS(ENOENT): return 0; - default: return ret; - } - + return cache_op(cache, remove, &key, 1); } int kr_cache_match(struct kr_cache *cache, const knot_dname_t *name, @@ -852,8 +846,8 @@ int kr_cache_remove_subtree(struct kr_cache *cache, const knot_dname_t *name, knot_db_val_t keyval[maxcount][2], keys[maxcount]; int ret = kr_cache_match(cache, name, exact_name, keyval, maxcount); - if (ret < 0) { - return ret; + if (ret <= 0) { /* ENOENT -> nothing to remove */ + return (ret == KNOT_ENOENT) ? 0 : ret; } const int count = ret; /* Duplicate the key strings, as deletion may invalidate the pointers. */ @@ -874,6 +868,6 @@ cleanup: while (--i >= 0) { free(keys[i].data); } - return ret ? ret : count; + return ret; } diff --git a/lib/cache/api.h b/lib/cache/api.h index 72751ce38..9034fa182 100644 --- a/lib/cache/api.h +++ b/lib/cache/api.h @@ -166,6 +166,7 @@ int kr_cache_match(struct kr_cache *cache, const knot_dname_t *name, /** * Remove a subtree in cache. It's like _match but removing them instead of returning. + * @return number of deleted entries or an errcode */ KR_EXPORT int kr_cache_remove_subtree(struct kr_cache *cache, const knot_dname_t *name, diff --git a/lib/cache/cdb_api.h b/lib/cache/cdb_api.h index 8836d57a2..814a5f5dd 100644 --- a/lib/cache/cdb_api.h +++ b/lib/cache/cdb_api.h @@ -47,7 +47,10 @@ struct kr_cdb_api { int maxcount); int (*write)(knot_db_t *db, const knot_db_val_t *key, knot_db_val_t *val, int maxcount); - /** Remove maxcount keys. Return error code. (Returns on first error.) */ + + /** Remove maxcount keys. + * \returns the number of succesfully removed keys or the first error code + * It returns on first error, but ENOENT is not considered an error. */ int (*remove)(knot_db_t *db, knot_db_val_t keys[], int maxcount); /* Specialised operations */ diff --git a/lib/cache/cdb_lmdb.c b/lib/cache/cdb_lmdb.c index e4e323638..621d2e881 100644 --- a/lib/cache/cdb_lmdb.c +++ b/lib/cache/cdb_lmdb.c @@ -552,14 +552,19 @@ static int cdb_remove(knot_db_t *db, knot_db_val_t keys[], int maxcount) struct lmdb_env *env = db; MDB_txn *txn = NULL; int ret = txn_get(env, &txn, false); + int deleted = 0; for (int i = 0; ret == kr_ok() && i < maxcount; ++i) { MDB_val _key = val_knot2mdb(keys[i]); MDB_val val = { 0, NULL }; ret = lmdb_error(mdb_del(txn, env->dbi, &_key, &val)); + if (ret == kr_ok()) + deleted++; + else if (ret == KNOT_ENOENT) + ret = kr_ok(); /* skip over non-existing entries */ } - return ret; + return ret < 0 ? ret : deleted; } static int cdb_match(knot_db_t *db, knot_db_val_t *key, knot_db_val_t keyval[][2], int maxcount)