5338d5640185512cd76c2c2f3707c401f069e40c
[ccan] / ccan / tdb2 / test / run-30-exhaust-before-expand.c
1 #include <ccan/tdb2/tdb.c>
2 #include <ccan/tdb2/free.c>
3 #include <ccan/tdb2/lock.c>
4 #include <ccan/tdb2/io.c>
5 #include <ccan/tdb2/check.c>
6 #include <ccan/tdb2/hash.c>
7 #include <ccan/tap/tap.h>
8 #include <err.h>
9 #include "logging.h"
10
11 static uint64_t fixedhash(const void *key, size_t len, uint64_t seed, void *p)
12 {
13         return hash64_stable((const unsigned char *)key, len,
14                              *(uint64_t *)p);
15 }
16
17 int main(int argc, char *argv[])
18 {
19         unsigned int i, j;
20         struct tdb_context *tdb;
21         int flags[] = { TDB_INTERNAL, TDB_DEFAULT, TDB_NOMMAP,
22                         TDB_INTERNAL|TDB_CONVERT, TDB_CONVERT,
23                         TDB_NOMMAP|TDB_CONVERT };
24         uint64_t seed = 0;
25         union tdb_attribute fixed_hattr
26                 = { .hash = { .base = { TDB_ATTRIBUTE_HASH },
27                               .hash_fn = fixedhash,
28                               .hash_private = &seed } };
29
30         fixed_hattr.base.next = &tap_log_attr;
31         plan_tests(sizeof(flags) / sizeof(flags[0]) * 5 + 1);
32
33         for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
34                 tdb_off_t free[BUCKETS_FOR_ZONE(INITIAL_ZONE_BITS) + 1];
35                 bool all_empty;
36                 TDB_DATA k, d;
37
38                 k.dptr = (void *)&j;
39                 k.dsize = sizeof(j);
40
41                 tdb = tdb_open("run-30-exhaust-before-expand.tdb", flags[i],
42                                O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
43                 ok1(tdb);
44                 if (!tdb)
45                         continue;
46
47                 /* We don't want the hash to expand, so we use one alloc to
48                  * chew up over 90% of the space first. */
49                 j = -1;
50                 d.dsize = (1 << INITIAL_ZONE_BITS) * 9 / 10;
51                 d.dptr = malloc(d.dsize);
52                 ok1(tdb_store(tdb, k, d, TDB_INSERT) == 0);
53                 ok1(tdb->map_size == sizeof(struct tdb_header)
54                     + (1 << INITIAL_ZONE_BITS)+1);
55
56                 /* Insert minimal-length records until we add a zone. */ 
57                 for (j = 0;
58                      tdb->map_size == sizeof(struct tdb_header)
59                              + (1 << INITIAL_ZONE_BITS)+1;
60                      j++) {
61                         if (tdb_store(tdb, k, k, TDB_INSERT) != 0)
62                                 err(1, "Failed to store record %i", j);
63                 }
64
65                 /* Now, free list should be completely exhausted in zone 0 */
66                 ok1(tdb_read_convert(tdb,
67                                      sizeof(struct tdb_header)
68                                      + sizeof(struct free_zone_header),
69                                      &free, sizeof(free)) == 0);
70
71                 all_empty = true;
72                 for (j = 0; j < sizeof(free)/sizeof(free[0]); j++) {
73                         if (free[j]) {
74                                 diag("Free bucket %i not empty", j);
75                                 all_empty = false;
76                         }
77                 }
78                 ok1(all_empty);
79                 tdb_close(tdb);
80         }
81
82         ok1(tap_log_messages == 0);
83         return exit_status();
84 }