]> git.ozlabs.org Git - ccan/blobdiff - ccan/tdb2/traverse.c
tdb2: make tdb_check typesafe.
[ccan] / ccan / tdb2 / traverse.c
index 06f0bc68ee4016b5a90330025f1c2ee34d1ad108..9736e9f5a1d2c9667fb9d05c4966e9e5081f9457 100644 (file)
 #include "private.h"
 #include <ccan/likely/likely.h>
 
-int64_t tdb_traverse(struct tdb_context *tdb, tdb_traverse_func fn, void *p)
+int64_t tdb_traverse_(struct tdb_context *tdb,
+                     int (*fn)(struct tdb_context *,
+                               TDB_DATA, TDB_DATA, void *),
+                     void *p)
 {
        enum TDB_ERROR ecode;
        struct traverse_info tinfo;
@@ -34,55 +37,37 @@ int64_t tdb_traverse(struct tdb_context *tdb, tdb_traverse_func fn, void *p)
                count++;
                if (fn && fn(tdb, k, d, p)) {
                        free(k.dptr);
-                       break;
+                       return count;
                }
                free(k.dptr);
        }
 
        if (ecode != TDB_ERR_NOEXIST) {
-               tdb->ecode = ecode;
-               return -1;
+               return ecode;
        }
        return count;
 }
        
-TDB_DATA tdb_firstkey(struct tdb_context *tdb)
+enum TDB_ERROR tdb_firstkey(struct tdb_context *tdb, struct tdb_data *key)
 {
        struct traverse_info tinfo;
-       struct tdb_data k;
-       enum TDB_ERROR ecode;
 
-       ecode = first_in_hash(tdb, &tinfo, &k, NULL);
-       if (ecode == TDB_SUCCESS) {
-               return k;
-       }
-       if (ecode == TDB_ERR_NOEXIST)
-               ecode = TDB_SUCCESS;
-       tdb->ecode = ecode;
-       return tdb_null;
+       return first_in_hash(tdb, &tinfo, key, NULL);
 }
 
 /* We lock twice, not very efficient.  We could keep last key & tinfo cached. */
-TDB_DATA tdb_nextkey(struct tdb_context *tdb, TDB_DATA key)
+enum TDB_ERROR tdb_nextkey(struct tdb_context *tdb, struct tdb_data *key)
 {
        struct traverse_info tinfo;
        struct hash_info h;
        struct tdb_used_record rec;
-       enum TDB_ERROR ecode;
 
-       tinfo.prev = find_and_lock(tdb, key, F_RDLCK, &h, &rec, &tinfo);
+       tinfo.prev = find_and_lock(tdb, *key, F_RDLCK, &h, &rec, &tinfo);
+       free(key->dptr);
        if (TDB_OFF_IS_ERR(tinfo.prev)) {
-               tdb->ecode = tinfo.prev;
-               return tdb_null;
+               return tinfo.prev;
        }
        tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_RDLCK);
 
-       ecode = next_in_hash(tdb, &tinfo, &key, NULL);
-       if (ecode == TDB_SUCCESS) {
-               return key;
-       }
-       if (ecode == TDB_ERR_NOEXIST)
-               ecode = TDB_SUCCESS;
-       tdb->ecode = ecode;
-       return tdb_null;
+       return next_in_hash(tdb, &tinfo, key, NULL);
 }