]> git.ipfire.org Git - thirdparty/knot-resolver.git/commitdiff
cache: return number of removed entries from remove_* functions
authorPetr Špaček <petr.spacek@nic.cz>
Wed, 15 Aug 2018 15:53:29 +0000 (17:53 +0200)
committerVladimír Čunát <vladimir.cunat@nic.cz>
Fri, 17 Aug 2018 13:58:48 +0000 (15:58 +0200)
It is more convenient to return 0 instead of ENOENT.

lib/cache/api.c
lib/cache/api.h
lib/cache/cdb_api.h
lib/cache/cdb_lmdb.c

index eae62d4017d3bbf33496b97464b46799f1c7c364..a3e3bb794af16b32d95d7fa7ff232b3ea0b3f657 100644 (file)
@@ -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;
 }
 
index 72751ce3868fa9e46d3c5a4f42470993f930eb2a..9034fa182e4dd232d6ca36b669c092a1151cf945 100644 (file)
@@ -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,
index 8836d57a253316d3801bef81f45952172f15c8e3..814a5f5ddf738faa974b9e08f34138a83de73175 100644 (file)
@@ -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 */
index e4e32363834db90069d86f263ebf08c68820a90f..621d2e881c867eedae10bdbece0c4863ff7a8ca3 100644 (file)
@@ -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)