]> git.ozlabs.org Git - ccan/blobdiff - ccan/tdb2/tdb1_tdb.c
tdb2: unify tdb1_parse_record into tdb_parse_record
[ccan] / ccan / tdb2 / tdb1_tdb.c
index cdcf76d3689f4a02f4ea11178d91556bb27d68fe..a50303c33ce4e6333e823ceea2cceff6788e60f7 100644 (file)
@@ -26,6 +26,7 @@
 */
 
 #include "tdb1_private.h"
+#include <assert.h>
 
 TDB_DATA tdb1_null;
 
@@ -200,39 +201,23 @@ static TDB_DATA _tdb1_fetch(struct tdb_context *tdb, TDB_DATA key)
        return ret;
 }
 
-TDB_DATA tdb1_fetch(struct tdb_context *tdb, TDB_DATA key)
+enum TDB_ERROR tdb1_fetch(struct tdb_context *tdb, TDB_DATA key, TDB_DATA *data)
 {
-       TDB_DATA ret = _tdb1_fetch(tdb, key);
-
-       return ret;
+       *data = _tdb1_fetch(tdb, key);
+       if (data->dptr == NULL)
+               return tdb->last_error;
+       return TDB_SUCCESS;
 }
 
-/*
- * Find an entry in the database and hand the record's data to a parsing
- * function. The parsing function is executed under the chain read lock, so it
- * should be fast and should not block on other syscalls.
- *
- * DON'T CALL OTHER TDB CALLS FROM THE PARSER, THIS MIGHT LEAD TO SEGFAULTS.
- *
- * For mmapped tdb's that do not have a transaction open it points the parsing
- * function directly at the mmap area, it avoids the malloc/memcpy in this
- * case. If a transaction is open or no mmap is available, it has to do
- * malloc/read/parse/free.
- *
- * This is interesting for all readers of potentially large data structures in
- * the tdb records, ldb indexes being one example.
- *
- * Return -1 if the record was not found.
- */
-
-int tdb1_parse_record(struct tdb_context *tdb, TDB_DATA key,
-                    int (*parser)(TDB_DATA key, TDB_DATA data,
-                                  void *private_data),
-                    void *private_data)
+enum TDB_ERROR tdb1_parse_record(struct tdb_context *tdb, TDB_DATA key,
+                                enum TDB_ERROR (*parser)(TDB_DATA key,
+                                                         TDB_DATA data,
+                                                         void *private_data),
+                                void *private_data)
 {
        tdb1_off_t rec_ptr;
        struct tdb1_record rec;
-       int ret;
+       enum TDB_ERROR ret;
        uint32_t hash;
 
        /* find which hash bucket it is in */
@@ -240,8 +225,7 @@ int tdb1_parse_record(struct tdb_context *tdb, TDB_DATA key,
 
        if (!(rec_ptr = tdb1_find_lock_hash(tdb,key,hash,F_RDLCK,&rec))) {
                /* record not found */
-               tdb->last_error = TDB_ERR_NOEXIST;
-               return -1;
+               return TDB_ERR_NOEXIST;
        }
 
        ret = tdb1_parse_data(tdb, key, rec_ptr + sizeof(rec) + rec.key_len,
@@ -273,6 +257,7 @@ int tdb1_exists(struct tdb_context *tdb, TDB_DATA key)
        uint32_t hash = tdb_hash(tdb, key.dptr, key.dsize);
        int ret;
 
+       assert(tdb->flags & TDB_VERSION1);
        ret = tdb1_exists_hash(tdb, key, hash);
        return ret;
 }
@@ -432,6 +417,7 @@ int tdb1_delete(struct tdb_context *tdb, TDB_DATA key)
        uint32_t hash = tdb_hash(tdb, key.dptr, key.dsize);
        int ret;
 
+       assert(tdb->flags & TDB_VERSION1);
        ret = tdb1_delete_hash(tdb, key, hash);
        return ret;
 }
@@ -601,6 +587,8 @@ int tdb1_store(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf, int flag)
        uint32_t hash;
        int ret;
 
+       assert(tdb->flags & TDB_VERSION1);
+
        if ((tdb->flags & TDB_RDONLY) || tdb->tdb1.traverse_read) {
                tdb->last_error = TDB_ERR_RDONLY;
                return -1;
@@ -623,6 +611,8 @@ int tdb1_append(struct tdb_context *tdb, TDB_DATA key, TDB_DATA new_dbuf)
        TDB_DATA dbuf;
        int ret = -1;
 
+       assert(tdb->flags & TDB_VERSION1);
+
        /* find which hash bucket it is in */
        hash = tdb_hash(tdb, key.dptr, key.dsize);
        if (tdb1_lock(tdb, TDB1_BUCKET(hash), F_WRLCK) == -1)
@@ -724,7 +714,7 @@ int tdb1_wipe_all(struct tdb_context *tdb)
        tdb1_off_t recovery_head;
        tdb1_len_t recovery_size = 0;
 
-       if (tdb1_lockall(tdb) != 0) {
+       if (tdb_lockall(tdb) != TDB_SUCCESS) {
                return -1;
        }
 
@@ -793,16 +783,11 @@ int tdb1_wipe_all(struct tdb_context *tdb)
                }
        }
 
-       if (tdb1_unlockall(tdb) != 0) {
-               tdb_logerr(tdb, tdb->last_error, TDB_LOG_ERROR,
-                          "tdb1_wipe_all: failed to unlock");
-               goto failed;
-       }
-
+       tdb_unlockall(tdb);
        return 0;
 
 failed:
-       tdb1_unlockall(tdb);
+       tdb_unlockall(tdb);
        return -1;
 }
 
@@ -854,7 +839,7 @@ int tdb1_repack(struct tdb_context *tdb)
        state.error = TDB_SUCCESS;
        state.dest_db = tmp_db;
 
-       if (tdb1_traverse_read(tdb, repack_traverse, &state) == -1) {
+       if (tdb1_traverse(tdb, repack_traverse, &state) == -1) {
                tdb_logerr(tdb, tdb->last_error, TDB_LOG_ERROR,
                           __location__ " Failed to traverse copying out");
                tdb1_transaction_cancel(tdb);
@@ -881,7 +866,7 @@ int tdb1_repack(struct tdb_context *tdb)
        state.error = TDB_SUCCESS;
        state.dest_db = tdb;
 
-       if (tdb1_traverse_read(tmp_db, repack_traverse, &state) == -1) {
+       if (tdb1_traverse(tmp_db, repack_traverse, &state) == -1) {
                tdb_logerr(tdb, tdb->last_error, TDB_LOG_ERROR,
                           __location__ " Failed to traverse copying back");
                tdb1_transaction_cancel(tdb);