]> git.ozlabs.org Git - ccan/blobdiff - ccan/tdb2/traverse.c
tdb2: rework hash.c functions to return enum TDB_ERROR.
[ccan] / ccan / tdb2 / traverse.c
index 9c71d0d1fb7e7752b9bdb1738427bd3c49421720..06f0bc68ee4016b5a90330025f1c2ee34d1ad108 100644 (file)
 
 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, &tinfo, &k, &d.dsize);
-            ret == 1;
-            ret = next_in_hash(tdb, &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++;
@@ -39,8 +39,10 @@ int64_t tdb_traverse(struct tdb_context *tdb, tdb_traverse_func fn, void *p)
                free(k.dptr);
        }
 
-       if (ret < 0)
+       if (ecode != TDB_ERR_NOEXIST) {
+               tdb->ecode = ecode;
                return -1;
+       }
        return count;
 }
        
@@ -48,15 +50,16 @@ TDB_DATA tdb_firstkey(struct tdb_context *tdb)
 {
        struct traverse_info tinfo;
        struct tdb_data k;
-       switch (first_in_hash(tdb, &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. */
@@ -65,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, &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;
 }