X-Git-Url: https://git.ozlabs.org/?p=ccan;a=blobdiff_plain;f=ccan%2Ftdb2%2Ftest%2Frun-30-exhaust-before-expand.c;h=db391e7e59c4b391358366633eb7f6f76e533db0;hp=5338d5640185512cd76c2c2f3707c401f069e40c;hb=bbeb528e74c0e234e1f724ac8d54be404cfc6f9a;hpb=e4b3b1e90d20594d124d01682bc43ab59a01c6e2 diff --git a/ccan/tdb2/test/run-30-exhaust-before-expand.c b/ccan/tdb2/test/run-30-exhaust-before-expand.c index 5338d564..db391e7e 100644 --- a/ccan/tdb2/test/run-30-exhaust-before-expand.c +++ b/ccan/tdb2/test/run-30-exhaust-before-expand.c @@ -1,19 +1,25 @@ -#include -#include -#include -#include -#include -#include +#include "tdb2-source.h" #include #include #include "logging.h" -static uint64_t fixedhash(const void *key, size_t len, uint64_t seed, void *p) +static bool empty_freetable(struct tdb_context *tdb) { - return hash64_stable((const unsigned char *)key, len, - *(uint64_t *)p); + struct tdb_freetable ftab; + unsigned int i; + + /* Now, free table should be completely exhausted in zone 0 */ + if (tdb_read_convert(tdb, tdb->tdb2.ftable_off, &ftab, sizeof(ftab)) != 0) + abort(); + + for (i = 0; i < sizeof(ftab.buckets)/sizeof(ftab.buckets[0]); i++) { + if (ftab.buckets[i]) + return false; + } + return true; } + int main(int argc, char *argv[]) { unsigned int i, j; @@ -21,19 +27,13 @@ int main(int argc, char *argv[]) int flags[] = { TDB_INTERNAL, TDB_DEFAULT, TDB_NOMMAP, TDB_INTERNAL|TDB_CONVERT, TDB_CONVERT, TDB_NOMMAP|TDB_CONVERT }; - uint64_t seed = 0; - union tdb_attribute fixed_hattr - = { .hash = { .base = { TDB_ATTRIBUTE_HASH }, - .hash_fn = fixedhash, - .hash_private = &seed } }; - fixed_hattr.base.next = &tap_log_attr; - plan_tests(sizeof(flags) / sizeof(flags[0]) * 5 + 1); + plan_tests(sizeof(flags) / sizeof(flags[0]) * 9 + 1); for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) { - tdb_off_t free[BUCKETS_FOR_ZONE(INITIAL_ZONE_BITS) + 1]; - bool all_empty; - TDB_DATA k, d; + TDB_DATA k; + uint64_t size; + bool was_empty = false; k.dptr = (void *)&j; k.dsize = sizeof(j); @@ -44,38 +44,26 @@ int main(int argc, char *argv[]) if (!tdb) continue; - /* We don't want the hash to expand, so we use one alloc to - * chew up over 90% of the space first. */ - j = -1; - d.dsize = (1 << INITIAL_ZONE_BITS) * 9 / 10; - d.dptr = malloc(d.dsize); - ok1(tdb_store(tdb, k, d, TDB_INSERT) == 0); - ok1(tdb->map_size == sizeof(struct tdb_header) - + (1 << INITIAL_ZONE_BITS)+1); + ok1(empty_freetable(tdb)); + /* Need some hash lock for expand. */ + ok1(tdb_lock_hashes(tdb, 0, 1, F_WRLCK, TDB_LOCK_WAIT) == 0); + /* Create some free space. */ + ok1(tdb_expand(tdb, 1) == 0); + ok1(tdb_unlock_hashes(tdb, 0, 1, F_WRLCK) == 0); + ok1(tdb_check(tdb, NULL, NULL) == 0); + ok1(!empty_freetable(tdb)); - /* Insert minimal-length records until we add a zone. */ - for (j = 0; - tdb->map_size == sizeof(struct tdb_header) - + (1 << INITIAL_ZONE_BITS)+1; - j++) { + size = tdb->file->map_size; + /* Insert minimal-length records until we expand. */ + for (j = 0; tdb->file->map_size == size; j++) { + was_empty = empty_freetable(tdb); if (tdb_store(tdb, k, k, TDB_INSERT) != 0) err(1, "Failed to store record %i", j); } - /* Now, free list should be completely exhausted in zone 0 */ - ok1(tdb_read_convert(tdb, - sizeof(struct tdb_header) - + sizeof(struct free_zone_header), - &free, sizeof(free)) == 0); - - all_empty = true; - for (j = 0; j < sizeof(free)/sizeof(free[0]); j++) { - if (free[j]) { - diag("Free bucket %i not empty", j); - all_empty = false; - } - } - ok1(all_empty); + /* Would have been empty before expansion, but no longer. */ + ok1(was_empty); + ok1(!empty_freetable(tdb)); tdb_close(tdb); }