2319e91bb5ef8579cf0c0f517c36b349e8abf4e5
[ccan] / ccan / tdb2 / test / run-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/tap/tap.h>
7 #include "logging.h"
8
9 /* Release lock to check db. */
10 static void check(struct tdb_context *tdb)
11 {
12         tdb_allrecord_unlock(tdb, F_WRLCK);
13         ok1(tdb_check(tdb, NULL, NULL) == 0);
14         ok1(tdb_allrecord_lock(tdb, F_WRLCK, TDB_LOCK_WAIT, false) == 0);
15 }
16
17 int main(int argc, char *argv[])
18 {
19         unsigned int i;
20         tdb_off_t off;
21         uint64_t val, buckets;
22         struct tdb_context *tdb;
23         int flags[] = { TDB_INTERNAL, TDB_DEFAULT,
24                         TDB_INTERNAL|TDB_CONVERT, TDB_CONVERT };
25
26         plan_tests(sizeof(flags) / sizeof(flags[0]) * 40 + 1);
27
28         /* First, lower level expansion tests. */
29         for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
30                 tdb = tdb_open("run-expand.tdb", flags[i],
31                                O_RDWR|O_CREAT|O_TRUNC, 0600, NULL);
32                 tdb->log = tap_log_fn;
33                 ok1(tdb);
34                 if (!tdb)
35                         continue;
36
37                 ok1(tdb_allrecord_lock(tdb, F_WRLCK, TDB_LOCK_WAIT, false)
38                     == 0);
39
40                 /* Expanding file is pretty easy. */
41                 off = expand_to_fill_zones(tdb);
42                 ok1(off > 0 && off != TDB_OFF_ERR);
43                 check(tdb);
44
45                 /* Second expand should do nothing. */
46                 ok1(expand_to_fill_zones(tdb) == 0);
47                 check(tdb);
48
49                 /* Now, try adding a zone. */
50                 val = tdb->header.v.num_zones + 1;
51                 ok1(update_zones(tdb, val,
52                                  tdb->header.v.zone_bits,
53                                  tdb->header.v.free_buckets,
54                                  1ULL << tdb->header.v.zone_bits) == 0);
55                 ok1(tdb->header.v.num_zones == val);
56                 check(tdb);
57
58                 /* Now, try doubling zone size. */
59                 val = tdb->header.v.zone_bits + 1;
60                 ok1(update_zones(tdb, tdb->header.v.num_zones,
61                                  val,
62                                  tdb->header.v.free_buckets,
63                                  1ULL << val) == 0);
64                 ok1(tdb->header.v.zone_bits == val);
65                 check(tdb);
66
67                 /* Now, try adding a zone, and a bucket. */
68                 val = tdb->header.v.num_zones + 1;
69                 buckets = tdb->header.v.free_buckets + 1;
70                 ok1(update_zones(tdb, val,
71                                  tdb->header.v.zone_bits,
72                                  buckets,
73                                  1ULL << tdb->header.v.zone_bits) == 0);
74                 ok1(tdb->header.v.num_zones == val);
75                 ok1(tdb->header.v.free_buckets == buckets);
76                 check(tdb);
77
78                 /* Now, try doubling zone size, and adding a bucket. */
79                 val = tdb->header.v.zone_bits + 1;
80                 buckets = tdb->header.v.free_buckets + 1;
81                 ok1(update_zones(tdb, tdb->header.v.num_zones,
82                                  val,
83                                  buckets,
84                                  1ULL << val) == 0);
85                 ok1(tdb->header.v.zone_bits == val);
86                 ok1(tdb->header.v.free_buckets == buckets);
87                 check(tdb);
88
89                 /* Now, try massive zone increase. */
90                 val = tdb->header.v.zone_bits + 4;
91                 ok1(update_zones(tdb, tdb->header.v.num_zones,
92                                  val,
93                                  tdb->header.v.free_buckets,
94                                  1ULL << val) == 0);
95                 ok1(tdb->header.v.zone_bits == val);
96                 check(tdb);
97
98                 tdb_allrecord_unlock(tdb, F_WRLCK);
99                 tdb_close(tdb);
100         }
101
102         /* Now using tdb_expand. */
103         for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
104                 tdb = tdb_open("run-expand.tdb", flags[i],
105                                O_RDWR|O_CREAT|O_TRUNC, 0600, NULL);
106                 tdb->log = tap_log_fn;
107                 ok1(tdb);
108                 if (!tdb)
109                         continue;
110
111                 /* First expand (expand file to fill zone). */
112                 ok1(tdb_expand(tdb, 1, 1, false) == 0);
113                 ok1(tdb->header.v.num_zones == 1);
114                 ok1(tdb_check(tdb, NULL, NULL) == 0);
115                 /* Little expand (extra zone). */
116                 ok1(tdb_expand(tdb, 1, 1, false) == 0);
117                 ok1(tdb->header.v.num_zones == 2);
118                 ok1(tdb_check(tdb, NULL, NULL) == 0);
119                 /* Big expand (enlarge zones) */
120                 ok1(tdb_expand(tdb, 1, 4096, false) == 0);
121                 ok1(tdb->header.v.num_zones == 2);
122                 ok1(tdb_check(tdb, NULL, NULL) == 0);
123                 tdb_close(tdb);
124         }
125
126         ok1(tap_log_messages == 0);
127         return exit_status();
128 }