]> git.ozlabs.org Git - ccan/blobdiff - ccan/tdb2/traverse.c
tdb2: allow multiple chain locks.
[ccan] / ccan / tdb2 / traverse.c
index 9736e9f5a1d2c9667fb9d05c4966e9e5081f9457..f8a2504dcd61489eb23bf7b84c8e0a88ced07f43 100644 (file)
@@ -37,14 +37,16 @@ int64_t tdb_traverse_(struct tdb_context *tdb,
                count++;
                if (fn && fn(tdb, k, d, p)) {
                        free(k.dptr);
                count++;
                if (fn && fn(tdb, k, d, p)) {
                        free(k.dptr);
+                       tdb->last_error = TDB_SUCCESS;
                        return count;
                }
                free(k.dptr);
        }
 
        if (ecode != TDB_ERR_NOEXIST) {
                        return count;
                }
                free(k.dptr);
        }
 
        if (ecode != TDB_ERR_NOEXIST) {
-               return ecode;
+               return tdb->last_error = ecode;
        }
        }
+       tdb->last_error = TDB_SUCCESS;
        return count;
 }
        
        return count;
 }
        
@@ -52,7 +54,7 @@ enum TDB_ERROR tdb_firstkey(struct tdb_context *tdb, struct tdb_data *key)
 {
        struct traverse_info tinfo;
 
 {
        struct traverse_info tinfo;
 
-       return first_in_hash(tdb, &tinfo, key, NULL);
+       return tdb->last_error = first_in_hash(tdb, &tinfo, key, NULL);
 }
 
 /* We lock twice, not very efficient.  We could keep last key & tinfo cached. */
 }
 
 /* We lock twice, not very efficient.  We could keep last key & tinfo cached. */
@@ -65,9 +67,33 @@ enum TDB_ERROR tdb_nextkey(struct tdb_context *tdb, struct tdb_data *key)
        tinfo.prev = find_and_lock(tdb, *key, F_RDLCK, &h, &rec, &tinfo);
        free(key->dptr);
        if (TDB_OFF_IS_ERR(tinfo.prev)) {
        tinfo.prev = find_and_lock(tdb, *key, F_RDLCK, &h, &rec, &tinfo);
        free(key->dptr);
        if (TDB_OFF_IS_ERR(tinfo.prev)) {
-               return tinfo.prev;
+               return tdb->last_error = tinfo.prev;
        }
        tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_RDLCK);
 
        }
        tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_RDLCK);
 
-       return next_in_hash(tdb, &tinfo, key, NULL);
+       return tdb->last_error = next_in_hash(tdb, &tinfo, key, NULL);
+}
+
+static int wipe_one(struct tdb_context *tdb,
+                   TDB_DATA key, TDB_DATA data, enum TDB_ERROR *ecode)
+{
+       *ecode = tdb_delete(tdb, key);
+       return (*ecode != TDB_SUCCESS);
+}
+
+enum TDB_ERROR tdb_wipe_all(struct tdb_context *tdb)
+{
+       enum TDB_ERROR ecode;
+       int64_t count;
+
+       ecode = tdb_allrecord_lock(tdb, F_WRLCK, TDB_LOCK_WAIT, false);
+       if (ecode != TDB_SUCCESS)
+               return tdb->last_error = ecode;
+
+       /* FIXME: Be smarter. */
+       count = tdb_traverse(tdb, wipe_one, &ecode);
+       if (count < 0)
+               ecode = count;
+       tdb_allrecord_unlock(tdb, F_WRLCK);
+       return tdb->last_error = ecode;
 }
 }