failtest: add --trace to replace --tracepath
[ccan] / ccan / tdb2 / traverse.c
index b79cc8b47e7c090e64ee12b69b225e8ba4b33f78..0bf418990b55deec32e84f662261920962c832dd 100644 (file)
 #include "private.h"
 #include <ccan/likely/likely.h>
 
 #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,
+                     int (*fn)(struct tdb_context *,
+                               TDB_DATA, TDB_DATA, void *),
+                     void *p)
 {
 {
-       int ret;
+       enum TDB_ERROR ecode;
        struct traverse_info tinfo;
        struct tdb_data k, d;
        int64_t count = 0;
 
        struct traverse_info tinfo;
        struct tdb_data k, d;
        int64_t count = 0;
 
+       if (tdb->flags & TDB_VERSION1) {
+               count = tdb1_traverse(tdb, fn, p);
+               if (count == -1)
+                       return TDB_ERR_TO_OFF(tdb->last_error);
+               return count;
+       }
+
        k.dptr = NULL;
        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++;
                if (fn && fn(tdb, k, d, p)) {
                        free(k.dptr);
                d.dptr = k.dptr + k.dsize;
                
                count++;
                if (fn && fn(tdb, k, d, p)) {
                        free(k.dptr);
-                       break;
+                       tdb->last_error = TDB_SUCCESS;
+                       return count;
                }
                free(k.dptr);
        }
 
                }
                free(k.dptr);
        }
 
-       if (ret < 0)
-               return -1;
+       if (ecode != TDB_ERR_NOEXIST) {
+               return TDB_ERR_TO_OFF(tdb->last_error = ecode);
+       }
+       tdb->last_error = TDB_SUCCESS;
        return count;
 }
        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)
+enum TDB_ERROR tdb_firstkey(struct tdb_context *tdb, struct tdb_data *key)
 {
        struct traverse_info tinfo;
 {
        struct traverse_info tinfo;
-       struct tdb_data k;
-       switch (first_in_hash(tdb, F_RDLCK, &tinfo, &k, NULL)) {
-       case 1:
-               return k;
-       case 0:
-               tdb->ecode = TDB_SUCCESS;
-               /* Fall thru... */
-       default:
-               return tdb_null;
+
+       if (tdb->flags & TDB_VERSION1) {
+               tdb->last_error = TDB_SUCCESS;
+               *key = tdb1_firstkey(tdb);
+               /* TDB1 didn't set error for last key. */
+               if (!key->dptr && tdb->last_error == TDB_SUCCESS) {
+                       tdb->last_error = TDB_ERR_NOEXIST;
+               }
+               return tdb->last_error;
        }
        }
+
+       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. */
-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;
 
 {
        struct traverse_info tinfo;
        struct hash_info h;
        struct tdb_used_record rec;
 
-       tinfo.prev = find_and_lock(tdb, key, F_RDLCK, &h, &rec, &tinfo);
-       if (unlikely(tinfo.prev == TDB_OFF_ERR))
-               return tdb_null;
+       if (tdb->flags & TDB_VERSION1) {
+               struct tdb_data last_key = *key;
+               tdb->last_error = TDB_SUCCESS;
+               *key = tdb1_nextkey(tdb, last_key);
+               free(last_key.dptr);
+               /* TDB1 didn't set error for last key. */
+               if (!key->dptr && tdb->last_error == TDB_SUCCESS) {
+                       tdb->last_error = TDB_ERR_NOEXIST;
+               }
+               return tdb->last_error;
+       }
+
+       tinfo.prev = find_and_lock(tdb, *key, F_RDLCK, &h, &rec, &tinfo);
+       free(key->dptr);
+       if (TDB_OFF_IS_ERR(tinfo.prev)) {
+               return tdb->last_error = TDB_OFF_TO_ERR(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);
 
-       switch (next_in_hash(tdb, F_RDLCK, &tinfo, &key, NULL)) {
-       case 1:
-               return key;
-       case 0:
-               tdb->ecode = TDB_SUCCESS;
-               /* Fall thru... */
-       default:
-               return tdb_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;
+
+       if (tdb->flags & TDB_VERSION1) {
+               if (tdb1_wipe_all(tdb) == -1)
+                       return tdb->last_error;
+               return TDB_SUCCESS;
        }
        }
+
+       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 = TDB_OFF_TO_ERR(count);
+       tdb_allrecord_unlock(tdb, F_WRLCK);
+       return tdb->last_error = ecode;
 }
 }