tdb2: test: fix run-57-die-during-transaction.c to be more efficient.
[ccan] / ccan / tdb2 / test / run-13-delete.c
index 9cf5962eca5be9aa257ffdb230340228c0561ba0..fdbc9db70aefaba4f91982e192b425b065c445ff 100644 (file)
@@ -1,17 +1,11 @@
-#include <ccan/tdb2/tdb.c>
-#include <ccan/tdb2/free.c>
-#include <ccan/tdb2/lock.c>
-#include <ccan/tdb2/io.c>
-#include <ccan/tdb2/hash.c>
-#include <ccan/tdb2/check.c>
-#include <ccan/tdb2/transaction.c>
+#include "tdb2-source.h"
 #include <ccan/tap/tap.h>
 #include "logging.h"
 
 /* We rig the hash so adjacent-numbered records always clash. */
 static uint64_t clash(const void *key, size_t len, uint64_t seed, void *priv)
 {
-       return ((uint64_t)*(unsigned int *)key)
+       return ((uint64_t)*(const unsigned int *)key)
                << (64 - TDB_TOPLEVEL_HASH_BITS - 1);
 }
 
@@ -31,10 +25,8 @@ static bool store_records(struct tdb_context *tdb)
        for (i = 0; i < 1000; i++) {
                if (tdb_store(tdb, key, data, TDB_REPLACE) != 0)
                        return false;
-               d = tdb_fetch(tdb, key);
-               if (d.dsize != data.dsize)
-                       return false;
-               if (memcmp(d.dptr, data.dptr, d.dsize) != 0)
+               tdb_fetch(tdb, key, &d);
+               if (!tdb_deq(d, data))
                        return false;
                free(d.dptr);
        }
@@ -50,8 +42,7 @@ static void test_val(struct tdb_context *tdb, uint64_t val)
        /* Insert an entry, then delete it. */
        v = val;
        /* Delete should fail. */
-       ok1(tdb_delete(tdb, key) == -1);
-       ok1(tdb_error(tdb) == TDB_ERR_NOEXIST);
+       ok1(tdb_delete(tdb, key) == TDB_ERR_NOEXIST);
        ok1(tdb_check(tdb, NULL, NULL) == 0);
 
        /* Insert should succeed. */
@@ -69,11 +60,11 @@ static void test_val(struct tdb_context *tdb, uint64_t val)
        ok1(tdb_check(tdb, NULL, NULL) == 0);
 
        /* Can find both? */
-       d = tdb_fetch(tdb, key);
+       ok1(tdb_fetch(tdb, key, &d) == TDB_SUCCESS);
        ok1(d.dsize == data.dsize);
        free(d.dptr);
        v = val;
-       d = tdb_fetch(tdb, key);
+       ok1(tdb_fetch(tdb, key, &d) == TDB_SUCCESS);
        ok1(d.dsize == data.dsize);
        free(d.dptr);
 
@@ -93,7 +84,7 @@ static void test_val(struct tdb_context *tdb, uint64_t val)
 
        /* Can still find second? */
        v = val + 1;
-       d = tdb_fetch(tdb, key);
+       ok1(tdb_fetch(tdb, key, &d) == TDB_SUCCESS);
        ok1(d.dsize == data.dsize);
        free(d.dptr);
 
@@ -107,15 +98,15 @@ static void test_val(struct tdb_context *tdb, uint64_t val)
        ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
 
        /* We can still find them all, right? */
-       d = tdb_fetch(tdb, key);
+       ok1(tdb_fetch(tdb, key, &d) == TDB_SUCCESS);
        ok1(d.dsize == data.dsize);
        free(d.dptr);
        v = val + 1;
-       d = tdb_fetch(tdb, key);
+       ok1(tdb_fetch(tdb, key, &d) == TDB_SUCCESS);
        ok1(d.dsize == data.dsize);
        free(d.dptr);
        v = val + 2;
-       d = tdb_fetch(tdb, key);
+       ok1(tdb_fetch(tdb, key, &d) == TDB_SUCCESS);
        ok1(d.dsize == data.dsize);
        free(d.dptr);
 
@@ -125,11 +116,11 @@ static void test_val(struct tdb_context *tdb, uint64_t val)
        ok1(tdb_check(tdb, NULL, NULL) == 0);
 
        v = val;
-       d = tdb_fetch(tdb, key);
+       ok1(tdb_fetch(tdb, key, &d) == TDB_SUCCESS);
        ok1(d.dsize == data.dsize);
        free(d.dptr);
        v = val + 2;
-       d = tdb_fetch(tdb, key);
+       ok1(tdb_fetch(tdb, key, &d) == TDB_SUCCESS);
        ok1(d.dsize == data.dsize);
        free(d.dptr);
 
@@ -148,14 +139,19 @@ int main(int argc, char *argv[])
        uint64_t seed = 16014841315512641303ULL;
        union tdb_attribute clash_hattr
                = { .hash = { .base = { TDB_ATTRIBUTE_HASH },
-                             .hash_fn = clash } };
+                             .fn = clash } };
        union tdb_attribute fixed_hattr
                = { .hash = { .base = { TDB_ATTRIBUTE_HASH },
-                             .hash_fn = fixedhash,
-                             .hash_private = &seed } };
+                             .fn = fixedhash,
+                             .data = &seed } };
        int flags[] = { TDB_INTERNAL, TDB_DEFAULT, TDB_NOMMAP,
                        TDB_INTERNAL|TDB_CONVERT, TDB_CONVERT,
-                       TDB_NOMMAP|TDB_CONVERT };
+                       TDB_NOMMAP|TDB_CONVERT,
+                       TDB_INTERNAL|TDB_VERSION1, TDB_VERSION1,
+                       TDB_NOMMAP|TDB_VERSION1,
+                       TDB_INTERNAL|TDB_CONVERT|TDB_VERSION1,
+                       TDB_CONVERT|TDB_VERSION1,
+                       TDB_NOMMAP|TDB_CONVERT|TDB_VERSION1 };
        /* These two values gave trouble before. */
        int vals[] = { 755, 837 };
 
@@ -163,7 +159,7 @@ int main(int argc, char *argv[])
        fixed_hattr.base.next = &tap_log_attr;
 
        plan_tests(sizeof(flags) / sizeof(flags[0])
-                  * (32 * 3 + 5 + sizeof(vals)/sizeof(vals[0])*2) + 1);
+                  * (39 * 3 + 5 + sizeof(vals)/sizeof(vals[0])*2) + 1);
        for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
                tdb = tdb_open("run-13-delete.tdb", flags[i],
                               O_RDWR|O_CREAT|O_TRUNC, 0600, &clash_hattr);
@@ -180,7 +176,8 @@ int main(int argc, char *argv[])
                /* Check mixed bitpattern. */
                test_val(tdb, 0x123456789ABCDEF0ULL);
 
-               ok1(tdb->allrecord_lock.count == 0 && tdb->num_lockrecs == 0);
+               ok1(!tdb->file || (tdb->file->allrecord_lock.count == 0
+                                  && tdb->file->num_lockrecs == 0));
                tdb_close(tdb);
 
                /* Deleting these entries in the db gave problems. */