]> git.ozlabs.org Git - ccan/blobdiff - ccan/tdb2/test/run-15-append.c
tdb2: test: try (almost) all tests with TDB_VERSION1 flag.
[ccan] / ccan / tdb2 / test / run-15-append.c
index 3f1fb5b3e878c0dd81db11607cda28949efb7a33..39afaf7cd8ad5c864a43676ec0fc44bfbdf38eaf 100644 (file)
@@ -1,24 +1,45 @@
-#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 "tdb2-source.h"
 #include <ccan/tap/tap.h>
+#include <ccan/ilog/ilog.h>
 #include "logging.h"
 
 #define MAX_SIZE 13100
 #define SIZE_STEP 131
 
+static tdb_off_t tdb_offset(struct tdb_context *tdb, struct tdb_data key)
+{
+       tdb_off_t off;
+       struct tdb_used_record rec;
+       struct hash_info h;
+
+       if (tdb_get_flags(tdb) & TDB_VERSION1) {
+               struct tdb1_record rec;
+               return tdb1_find(tdb, key, tdb_hash(tdb, key.dptr, key.dsize),
+                                &rec);
+       }
+
+       off = find_and_lock(tdb, key, F_RDLCK, &h, &rec, NULL);
+       if (TDB_OFF_IS_ERR(off))
+               return 0;
+       tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_RDLCK);
+       return off;
+}
+
 int main(int argc, char *argv[])
 {
-       unsigned int i, j;
+       unsigned int i, j, moves;
        struct tdb_context *tdb;
        unsigned char *buffer;
+       tdb_off_t oldoff = 0, newoff;
        int flags[] = { TDB_INTERNAL, TDB_DEFAULT, TDB_NOMMAP,
                        TDB_INTERNAL|TDB_CONVERT, TDB_CONVERT, 
-                       TDB_NOMMAP|TDB_CONVERT };
-       struct tdb_data key = { (unsigned char *)"key", 3 };
+                       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 };
+       struct tdb_data key = tdb_mkdata("key", 3);
        struct tdb_data data;
 
        buffer = malloc(MAX_SIZE);
@@ -26,7 +47,7 @@ int main(int argc, char *argv[])
                buffer[i] = i;
 
        plan_tests(sizeof(flags) / sizeof(flags[0])
-                  * ((2 + MAX_SIZE/SIZE_STEP * 4) * 2 + 6)
+                  * ((3 + MAX_SIZE/SIZE_STEP * 5) * 2 + 7)
                   + 1);
 
        /* Using tdb_store. */
@@ -37,17 +58,32 @@ int main(int argc, char *argv[])
                if (!tdb)
                        continue;
 
+               moves = 0;
                for (j = 0; j < MAX_SIZE; j += SIZE_STEP) {
                        data.dptr = buffer;
                        data.dsize = j;
                        ok1(tdb_store(tdb, key, data, TDB_REPLACE) == 0);
                        ok1(tdb_check(tdb, NULL, NULL) == 0);
-                       data = tdb_fetch(tdb, key);
+                       ok1(tdb_fetch(tdb, key, &data) == TDB_SUCCESS);
                        ok1(data.dsize == j);
                        ok1(memcmp(data.dptr, buffer, data.dsize) == 0);
                        free(data.dptr);
+                       newoff = tdb_offset(tdb, key);
+                       if (newoff != oldoff)
+                               moves++;
+                       oldoff = newoff;
+               }
+               ok1(!tdb->file || (tdb->file->allrecord_lock.count == 0
+                                  && tdb->file->num_lockrecs == 0));
+               if (flags[i] & TDB_VERSION1) {
+                       /* TDB1 simply over-size by 25%. */
+                       ok(moves <= ilog64(j / SIZE_STEP)*4,
+                          "Moved %u times", moves);
+               } else {
+                       /* We should increase by 50% each time... */
+                       ok(moves <= ilog64(j / SIZE_STEP)*2,
+                          "Moved %u times", moves);
                }
-               ok1(!tdb_has_locks(tdb));
                tdb_close(tdb);
        }
 
@@ -60,18 +96,33 @@ int main(int argc, char *argv[])
                if (!tdb)
                        continue;
 
+               moves = 0;
                for (j = 0; j < MAX_SIZE; j += SIZE_STEP) {
                        data.dptr = buffer + prev_len;
                        data.dsize = j - prev_len;
                        ok1(tdb_append(tdb, key, data) == 0);
                        ok1(tdb_check(tdb, NULL, NULL) == 0);
-                       data = tdb_fetch(tdb, key);
+                       ok1(tdb_fetch(tdb, key, &data) == TDB_SUCCESS);
                        ok1(data.dsize == j);
                        ok1(memcmp(data.dptr, buffer, data.dsize) == 0);
                        free(data.dptr);
                        prev_len = data.dsize;
+                       newoff = tdb_offset(tdb, key);
+                       if (newoff != oldoff)
+                               moves++;
+                       oldoff = newoff;
+               }
+               ok1(!tdb->file || (tdb->file->allrecord_lock.count == 0
+                                  && tdb->file->num_lockrecs == 0));
+               if (flags[i] & TDB_VERSION1) {
+                       /* TDB1 simply over-size by 25%. */
+                       ok(moves <= ilog64(j / SIZE_STEP)*4,
+                          "Moved %u times", moves);
+               } else {
+                       /* We should increase by 50% each time... */
+                       ok(moves <= ilog64(j / SIZE_STEP)*2,
+                          "Moved %u times", moves);
                }
-               ok1(!tdb_has_locks(tdb));
                tdb_close(tdb);
        }
 
@@ -87,14 +138,16 @@ int main(int argc, char *argv[])
                data.dsize = MAX_SIZE;
                ok1(tdb_append(tdb, key, data) == 0);
                ok1(tdb_check(tdb, NULL, NULL) == 0);
-               data = tdb_fetch(tdb, key);
+               ok1(tdb_fetch(tdb, key, &data) == TDB_SUCCESS);
                ok1(data.dsize == MAX_SIZE);
                ok1(memcmp(data.dptr, buffer, data.dsize) == 0);
                free(data.dptr);
-               ok1(!tdb_has_locks(tdb));
+               ok1(!tdb->file || (tdb->file->allrecord_lock.count == 0
+                                  && tdb->file->num_lockrecs == 0));
                tdb_close(tdb);
        }
 
        ok1(tap_log_messages == 0);
+       free(buffer);
        return exit_status();
 }