]> git.ozlabs.org Git - ccan/blob - ccan/tdb2/test/run-30-exhaust-before-expand.c
tdb2: merge tdb1_context into tdb_context.
[ccan] / ccan / tdb2 / test / run-30-exhaust-before-expand.c
1 #include "tdb2-source.h"
2 #include <ccan/tap/tap.h>
3 #include <err.h>
4 #include "logging.h"
5
6 static bool empty_freetable(struct tdb_context *tdb)
7 {
8         struct tdb_freetable ftab;
9         unsigned int i;
10
11         /* Now, free table should be completely exhausted in zone 0 */
12         if (tdb_read_convert(tdb, tdb->tdb2.ftable_off, &ftab, sizeof(ftab)) != 0)
13                 abort();
14
15         for (i = 0; i < sizeof(ftab.buckets)/sizeof(ftab.buckets[0]); i++) {
16                 if (ftab.buckets[i])
17                         return false;
18         }
19         return true;
20 }
21
22
23 int main(int argc, char *argv[])
24 {
25         unsigned int i, j;
26         struct tdb_context *tdb;
27         int flags[] = { TDB_INTERNAL, TDB_DEFAULT, TDB_NOMMAP,
28                         TDB_INTERNAL|TDB_CONVERT, TDB_CONVERT,
29                         TDB_NOMMAP|TDB_CONVERT };
30
31         plan_tests(sizeof(flags) / sizeof(flags[0]) * 9 + 1);
32
33         for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
34                 TDB_DATA k;
35                 uint64_t size;
36                 bool was_empty = false;
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                 ok1(empty_freetable(tdb));
48                 /* Need some hash lock for expand. */
49                 ok1(tdb_lock_hashes(tdb, 0, 1, F_WRLCK, TDB_LOCK_WAIT) == 0);
50                 /* Create some free space. */
51                 ok1(tdb_expand(tdb, 1) == 0);
52                 ok1(tdb_unlock_hashes(tdb, 0, 1, F_WRLCK) == 0);
53                 ok1(tdb_check(tdb, NULL, NULL) == 0);
54                 ok1(!empty_freetable(tdb));
55
56                 size = tdb->file->map_size;
57                 /* Insert minimal-length records until we expand. */
58                 for (j = 0; tdb->file->map_size == size; j++) {
59                         was_empty = empty_freetable(tdb);
60                         if (tdb_store(tdb, k, k, TDB_INSERT) != 0)
61                                 err(1, "Failed to store record %i", j);
62                 }
63
64                 /* Would have been empty before expansion, but no longer. */
65                 ok1(was_empty);
66                 ok1(!empty_freetable(tdb));
67                 tdb_close(tdb);
68         }
69
70         ok1(tap_log_messages == 0);
71         return exit_status();
72 }