tdb2: more tests, hash collision fixes, attribute support.
[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, &tap_log_attr);
32                 ok1(tdb);
33                 if (!tdb)
34                         continue;
35
36                 ok1(tdb_allrecord_lock(tdb, F_WRLCK, TDB_LOCK_WAIT, false)
37                     == 0);
38
39                 /* Expanding file is pretty easy. */
40                 off = expand_to_fill_zones(tdb);
41                 ok1(off > 0 && off != TDB_OFF_ERR);
42                 check(tdb);
43
44                 /* Second expand should do nothing. */
45                 ok1(expand_to_fill_zones(tdb) == 0);
46                 check(tdb);
47
48                 /* Now, try adding a zone. */
49                 val = tdb->header.v.num_zones + 1;
50                 ok1(update_zones(tdb, val,
51                                  tdb->header.v.zone_bits,
52                                  tdb->header.v.free_buckets,
53                                  1ULL << tdb->header.v.zone_bits) == 0);
54                 ok1(tdb->header.v.num_zones == val);
55                 check(tdb);
56
57                 /* Now, try doubling zone size. */
58                 val = tdb->header.v.zone_bits + 1;
59                 ok1(update_zones(tdb, tdb->header.v.num_zones,
60                                  val,
61                                  tdb->header.v.free_buckets,
62                                  1ULL << val) == 0);
63                 ok1(tdb->header.v.zone_bits == val);
64                 check(tdb);
65
66                 /* Now, try adding a zone, and a bucket. */
67                 val = tdb->header.v.num_zones + 1;
68                 buckets = tdb->header.v.free_buckets + 1;
69                 ok1(update_zones(tdb, val,
70                                  tdb->header.v.zone_bits,
71                                  buckets,
72                                  1ULL << tdb->header.v.zone_bits) == 0);
73                 ok1(tdb->header.v.num_zones == val);
74                 ok1(tdb->header.v.free_buckets == buckets);
75                 check(tdb);
76
77                 /* Now, try doubling zone size, and adding a bucket. */
78                 val = tdb->header.v.zone_bits + 1;
79                 buckets = tdb->header.v.free_buckets + 1;
80                 ok1(update_zones(tdb, tdb->header.v.num_zones,
81                                  val,
82                                  buckets,
83                                  1ULL << val) == 0);
84                 ok1(tdb->header.v.zone_bits == val);
85                 ok1(tdb->header.v.free_buckets == buckets);
86                 check(tdb);
87
88                 /* Now, try massive zone increase. */
89                 val = tdb->header.v.zone_bits + 4;
90                 ok1(update_zones(tdb, tdb->header.v.num_zones,
91                                  val,
92                                  tdb->header.v.free_buckets,
93                                  1ULL << val) == 0);
94                 ok1(tdb->header.v.zone_bits == val);
95                 check(tdb);
96
97                 tdb_allrecord_unlock(tdb, F_WRLCK);
98                 tdb_close(tdb);
99         }
100
101         /* Now using tdb_expand. */
102         for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
103                 tdb = tdb_open("run-expand.tdb", flags[i],
104                                O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
105                 ok1(tdb);
106                 if (!tdb)
107                         continue;
108
109                 /* First expand (expand file to fill zone). */
110                 ok1(tdb_expand(tdb, 1, 1, false) == 0);
111                 ok1(tdb->header.v.num_zones == 1);
112                 ok1(tdb_check(tdb, NULL, NULL) == 0);
113                 /* Little expand (extra zone). */
114                 ok1(tdb_expand(tdb, 1, 1, false) == 0);
115                 ok1(tdb->header.v.num_zones == 2);
116                 ok1(tdb_check(tdb, NULL, NULL) == 0);
117                 /* Big expand (enlarge zones) */
118                 ok1(tdb_expand(tdb, 1, 4096, false) == 0);
119                 ok1(tdb->header.v.num_zones == 2);
120                 ok1(tdb_check(tdb, NULL, NULL) == 0);
121                 tdb_close(tdb);
122         }
123
124         ok1(tap_log_messages == 0);
125         return exit_status();
126 }