tdb: simplify tdb_delete_hash() a bit
authorMichael Adam <obnox@samba.org>
Thu, 13 Feb 2014 15:48:35 +0000 (16:48 +0100)
committerJeremy Allison <jra@samba.org>
Fri, 14 Feb 2014 23:55:46 +0000 (15:55 -0800)
Make the lock/unlock bracket more obvious by extracting
locking (and finding) from the special cases to the top
of the function. This also lets us take lock and find
the record outside the special case branches (use dead
records or not).

There is a small semantic change implied:

In the dead records case, the record to delete is looked
up before the current dead records are potentially purged.
Hence, if the record to delete is not found, the dead
records are also not purge. This does not make a big
difference though, because purging is only delayed until
directly befor the next record to delete is in fact found.

Signed-off-by: Michael Adam <obnox@samba.org>
Reviewed-by: Jeremy Allison <jra@samba.org>
lib/tdb/common/tdb.c

index 6256a05d04ebf53f7cd9913abaf713802fe95530..f24493ccd84b3ff17b9fb2427da31c207e80ce65 100644 (file)
@@ -387,6 +387,11 @@ static int tdb_delete_hash(struct tdb_context *tdb, TDB_DATA key, uint32_t hash)
        struct tdb_record rec;
        int ret;
 
+       rec_ptr = tdb_find_lock_hash(tdb, key, hash, F_WRLCK, &rec);
+       if (rec_ptr == 0) {
+               return -1;
+       }
+
        if (tdb->max_dead_records != 0) {
 
                /*
@@ -394,9 +399,6 @@ static int tdb_delete_hash(struct tdb_context *tdb, TDB_DATA key, uint32_t hash)
                 * tdb's with a very high create/delete rate like locking.tdb.
                 */
 
-               if (tdb_lock(tdb, BUCKET(hash), F_WRLCK) == -1)
-                       return -1;
-
                if (tdb_count_dead(tdb, hash) >= tdb->max_dead_records) {
                        /*
                         * Don't let the per-chain freelist grow too large,
@@ -405,11 +407,6 @@ static int tdb_delete_hash(struct tdb_context *tdb, TDB_DATA key, uint32_t hash)
                        tdb_purge_dead(tdb, hash);
                }
 
-               if (!(rec_ptr = tdb_find(tdb, key, hash, &rec))) {
-                       tdb_unlock(tdb, BUCKET(hash), F_WRLCK);
-                       return -1;
-               }
-
                /*
                 * Just mark the record as dead.
                 */
@@ -417,10 +414,6 @@ static int tdb_delete_hash(struct tdb_context *tdb, TDB_DATA key, uint32_t hash)
                ret = tdb_rec_write(tdb, rec_ptr, &rec);
        }
        else {
-               if (!(rec_ptr = tdb_find_lock_hash(tdb, key, hash, F_WRLCK,
-                                                  &rec)))
-                       return -1;
-
                ret = tdb_do_delete(tdb, rec_ptr, &rec);
        }