]> git.ozlabs.org Git - ccan/blobdiff - ccan/tdb2/traverse.c
tdb2: rework transaction.c internal functions to return enum TDB_ERROR.
[ccan] / ccan / tdb2 / traverse.c
index b79cc8b47e7c090e64ee12b69b225e8ba4b33f78..06f0bc68ee4016b5a90330025f1c2ee34d1ad108 100644 (file)
 #include "private.h"
 #include <ccan/likely/likely.h>
 
-static int64_t traverse(struct tdb_context *tdb, int ltype,
-                       tdb_traverse_func fn, void *p)
+int64_t tdb_traverse(struct tdb_context *tdb, tdb_traverse_func fn, void *p)
 {
-       int ret;
+       enum TDB_ERROR ecode;
        struct traverse_info tinfo;
        struct tdb_data k, d;
        int64_t count = 0;
 
        k.dptr = NULL;
-       for (ret = first_in_hash(tdb, ltype, &tinfo, &k, &d.dsize);
-            ret == 1;
-            ret = next_in_hash(tdb, ltype, &tinfo, &k, &d.dsize)) {
+       for (ecode = first_in_hash(tdb, &tinfo, &k, &d.dsize);
+            ecode == TDB_SUCCESS;
+            ecode = next_in_hash(tdb, &tinfo, &k, &d.dsize)) {
                d.dptr = k.dptr + k.dsize;
                
                count++;
@@ -40,40 +39,27 @@ static int64_t traverse(struct tdb_context *tdb, int ltype,
                free(k.dptr);
        }
 
-       if (ret < 0)
+       if (ecode != TDB_ERR_NOEXIST) {
+               tdb->ecode = ecode;
                return -1;
+       }
        return count;
 }
-
-int64_t tdb_traverse(struct tdb_context *tdb, tdb_traverse_func fn, void *p)
-{
-       return traverse(tdb, F_WRLCK, fn, p);
-}
        
-int64_t tdb_traverse_read(struct tdb_context *tdb,
-                         tdb_traverse_func fn, void *p)
-{
-       int64_t ret;
-       bool was_ro = tdb->read_only;
-       tdb->read_only = true;
-       ret = traverse(tdb, F_RDLCK, fn, p);
-       tdb->read_only = was_ro;
-       return ret;
-}
-
 TDB_DATA tdb_firstkey(struct tdb_context *tdb)
 {
        struct traverse_info tinfo;
        struct tdb_data k;
-       switch (first_in_hash(tdb, F_RDLCK, &tinfo, &k, NULL)) {
-       case 1:
+       enum TDB_ERROR ecode;
+
+       ecode = first_in_hash(tdb, &tinfo, &k, NULL);
+       if (ecode == TDB_SUCCESS) {
                return k;
-       case 0:
-               tdb->ecode = TDB_SUCCESS;
-               /* Fall thru... */
-       default:
-               return tdb_null;
        }
+       if (ecode == TDB_ERR_NOEXIST)
+               ecode = TDB_SUCCESS;
+       tdb->ecode = ecode;
+       return tdb_null;
 }
 
 /* We lock twice, not very efficient.  We could keep last key & tinfo cached. */
@@ -82,19 +68,21 @@ TDB_DATA tdb_nextkey(struct tdb_context *tdb, 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);
-       if (unlikely(tinfo.prev == TDB_OFF_ERR))
+       if (TDB_OFF_IS_ERR(tinfo.prev)) {
+               tdb->ecode = tinfo.prev;
                return tdb_null;
+       }
        tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_RDLCK);
 
-       switch (next_in_hash(tdb, F_RDLCK, &tinfo, &key, NULL)) {
-       case 1:
+       ecode = next_in_hash(tdb, &tinfo, &key, NULL);
+       if (ecode == TDB_SUCCESS) {
                return key;
-       case 0:
-               tdb->ecode = TDB_SUCCESS;
-               /* Fall thru... */
-       default:
-               return tdb_null;
        }
+       if (ecode == TDB_ERR_NOEXIST)
+               ecode = TDB_SUCCESS;
+       tdb->ecode = ecode;
+       return tdb_null;
 }