tdb2: feature support.
[ccan] / ccan / tdb2 / test / layout.c
1 /* TDB tools to create various canned database layouts. */
2 #include "layout.h"
3 #include <stdlib.h>
4 #include <string.h>
5 #include <assert.h>
6 #include <err.h>
7 #include "logging.h"
8
9 struct tdb_layout *new_tdb_layout(const char *filename)
10 {
11         struct tdb_layout *layout = malloc(sizeof(*layout));
12         layout->filename = filename;
13         layout->num_elems = 0;
14         layout->elem = NULL;
15         return layout;
16 }
17
18 static void add(struct tdb_layout *layout, union tdb_layout_elem elem)
19 {
20         layout->elem = realloc(layout->elem,
21                                sizeof(layout->elem[0])
22                                * (layout->num_elems+1));
23         layout->elem[layout->num_elems++] = elem;
24 }
25
26 void tdb_layout_add_freetable(struct tdb_layout *layout)
27 {
28         union tdb_layout_elem elem;
29         elem.base.type = FREETABLE;
30         add(layout, elem);
31 }
32
33 void tdb_layout_add_free(struct tdb_layout *layout, tdb_len_t len,
34                          unsigned ftable)
35 {
36         union tdb_layout_elem elem;
37         elem.base.type = FREE;
38         elem.free.len = len;
39         elem.free.ftable_num = ftable;
40         add(layout, elem);
41 }
42
43 static struct tdb_data dup_key(struct tdb_data key)
44 {
45         struct tdb_data ret;
46         ret.dsize = key.dsize;
47         ret.dptr = malloc(ret.dsize);
48         memcpy(ret.dptr, key.dptr, ret.dsize);
49         return ret;
50 }
51
52 void tdb_layout_add_used(struct tdb_layout *layout,
53                          TDB_DATA key, TDB_DATA data,
54                          tdb_len_t extra)
55 {
56         union tdb_layout_elem elem;
57         elem.base.type = DATA;
58         elem.used.key = dup_key(key);
59         elem.used.data = dup_key(data);
60         elem.used.extra = extra;
61         add(layout, elem);
62 }
63
64 static tdb_len_t free_record_len(tdb_len_t len)
65 {
66         return sizeof(struct tdb_used_record) + len;
67 }
68
69 static tdb_len_t data_record_len(struct tle_used *used)
70 {
71         tdb_len_t len;
72         len = sizeof(struct tdb_used_record)
73                 + used->key.dsize + used->data.dsize + used->extra;
74         assert(len >= sizeof(struct tdb_free_record));
75         return len;
76 }
77
78 static tdb_len_t hashtable_len(struct tle_hashtable *htable)
79 {
80         return sizeof(struct tdb_used_record)
81                 + (sizeof(tdb_off_t) << TDB_SUBLEVEL_HASH_BITS)
82                 + htable->extra;
83 }
84
85 static tdb_len_t freetable_len(struct tle_freetable *ftable)
86 {
87         return sizeof(struct tdb_freetable);
88 }
89
90 static void set_free_record(void *mem, tdb_len_t len)
91 {
92         /* We do all the work in add_to_freetable */
93 }
94
95 static void set_data_record(void *mem, struct tdb_context *tdb,
96                             struct tle_used *used)
97 {
98         struct tdb_used_record *u = mem;
99
100         set_header(tdb, u, TDB_USED_MAGIC, used->key.dsize, used->data.dsize,
101                    used->key.dsize + used->data.dsize + used->extra,
102                    tdb_hash(tdb, used->key.dptr, used->key.dsize));
103         memcpy(u + 1, used->key.dptr, used->key.dsize);
104         memcpy((char *)(u + 1) + used->key.dsize,
105                used->data.dptr, used->data.dsize);
106 }
107
108 static void set_hashtable(void *mem, struct tdb_context *tdb,
109                           struct tle_hashtable *htable)
110 {
111         struct tdb_used_record *u = mem;
112         tdb_len_t len = sizeof(tdb_off_t) << TDB_SUBLEVEL_HASH_BITS;
113
114         set_header(tdb, u, TDB_HTABLE_MAGIC, 0, len, len + htable->extra, 0);
115         memset(u + 1, 0, len);
116 }
117
118 static void set_freetable(void *mem, struct tdb_context *tdb,
119                          struct tle_freetable *freetable, struct tdb_header *hdr,
120                          tdb_off_t last_ftable)
121 {
122         struct tdb_freetable *ftable = mem;
123         memset(ftable, 0, sizeof(*ftable));
124         set_header(tdb, &ftable->hdr, TDB_FTABLE_MAGIC, 0,
125                         sizeof(*ftable) - sizeof(ftable->hdr),
126                         sizeof(*ftable) - sizeof(ftable->hdr), 0);
127
128         if (last_ftable) {
129                 ftable = (struct tdb_freetable *)((char *)hdr + last_ftable);
130                 ftable->next = freetable->base.off;
131         } else {
132                 hdr->free_table = freetable->base.off;
133         }
134 }
135
136 static void add_to_freetable(struct tdb_context *tdb,
137                              tdb_off_t eoff,
138                              tdb_off_t elen,
139                              unsigned ftable,
140                              struct tle_freetable *freetable)
141 {
142         tdb->ftable_off = freetable->base.off;
143         tdb->ftable = ftable;
144         add_free_record(tdb, eoff, sizeof(struct tdb_used_record) + elen);
145 }
146
147 static tdb_off_t hbucket_off(tdb_off_t group_start, unsigned ingroup)
148 {
149         return group_start
150                 + (ingroup % (1 << TDB_HASH_GROUP_BITS)) * sizeof(tdb_off_t);
151 }
152
153 /* Get bits from a value. */
154 static uint32_t bits(uint64_t val, unsigned start, unsigned num)
155 {
156         assert(num <= 32);
157         return (val >> start) & ((1U << num) - 1);
158 }
159
160 /* We take bits from the top: that way we can lock whole sections of the hash
161  * by using lock ranges. */
162 static uint32_t use_bits(uint64_t h, unsigned num, unsigned *used)
163 {
164         *used += num;
165         return bits(h, 64 - *used, num);
166 }
167
168 static tdb_off_t encode_offset(tdb_off_t new_off, unsigned bucket,
169                                uint64_t h)
170 {
171         return bucket
172                 | new_off
173                 | ((uint64_t)bits(h, 64 - TDB_OFF_UPPER_STEAL_EXTRA,
174                                   TDB_OFF_UPPER_STEAL_EXTRA)
175                    << TDB_OFF_HASH_EXTRA_BIT);
176 }
177
178 /* FIXME: Our hash table handling here is primitive: we don't expand! */
179 static void add_to_hashtable(struct tdb_context *tdb,
180                              tdb_off_t eoff,
181                              struct tdb_data key)
182 {
183         uint64_t h = tdb_hash(tdb, key.dptr, key.dsize);
184         tdb_off_t b_off, group_start;
185         unsigned i, group, in_group;
186         unsigned used = 0;
187
188         group = use_bits(h, TDB_TOPLEVEL_HASH_BITS-TDB_HASH_GROUP_BITS, &used);
189         in_group = use_bits(h, TDB_HASH_GROUP_BITS, &used);
190
191         group_start = offsetof(struct tdb_header, hashtable)
192                 + group * (sizeof(tdb_off_t) << TDB_HASH_GROUP_BITS);
193
194         for (i = 0; i < (1 << TDB_HASH_GROUP_BITS); i++) {
195                 unsigned bucket = (in_group + i) % (1 << TDB_HASH_GROUP_BITS);
196
197                 b_off = hbucket_off(group_start, bucket);               
198                 if (tdb_read_off(tdb, b_off) == 0) {
199                         tdb_write_off(tdb, b_off,
200                                       encode_offset(eoff, bucket, h));
201                         return;
202                 }
203         }
204         abort();
205 }
206
207 static struct tle_freetable *find_ftable(struct tdb_layout *layout, unsigned num)
208 {
209         unsigned i;
210
211         for (i = 0; i < layout->num_elems; i++) {
212                 if (layout->elem[i].base.type != FREETABLE)
213                         continue;
214                 if (num == 0)
215                         return &layout->elem[i].ftable;
216                 num--;
217         }
218         abort();
219 }
220
221 /* FIXME: Support TDB_CONVERT */
222 struct tdb_context *tdb_layout_get(struct tdb_layout *layout)
223 {
224         unsigned int i;
225         tdb_off_t off, len, last_ftable;
226         char *mem;
227         struct tdb_context *tdb;
228
229         off = sizeof(struct tdb_header);
230
231         /* First pass of layout: calc lengths */
232         for (i = 0; i < layout->num_elems; i++) {
233                 union tdb_layout_elem *e = &layout->elem[i];
234                 e->base.off = off;
235                 switch (e->base.type) {
236                 case FREETABLE:
237                         len = freetable_len(&e->ftable);
238                         break;
239                 case FREE:
240                         len = free_record_len(e->free.len);
241                         break;
242                 case DATA:
243                         len = data_record_len(&e->used);
244                         break;
245                 case HASHTABLE:
246                         len = hashtable_len(&e->hashtable);
247                         break;
248                 default:
249                         abort();
250                 }
251                 off += len;
252         }
253
254         mem = malloc(off);
255         /* Now populate our header, cribbing from a real TDB header. */
256         tdb = tdb_open(NULL, TDB_INTERNAL, O_RDWR, 0, &tap_log_attr);
257         memcpy(mem, tdb->map_ptr, sizeof(struct tdb_header));
258
259         /* Mug the tdb we have to make it use this. */
260         free(tdb->map_ptr);
261         tdb->map_ptr = mem;
262         tdb->map_size = off;
263
264         last_ftable = 0;
265         for (i = 0; i < layout->num_elems; i++) {
266                 union tdb_layout_elem *e = &layout->elem[i];
267                 switch (e->base.type) {
268                 case FREETABLE:
269                         set_freetable(mem + e->base.off, tdb, &e->ftable,
270                                      (struct tdb_header *)mem, last_ftable);
271                         last_ftable = e->base.off;
272                         break;
273                 case FREE:
274                         set_free_record(mem + e->base.off, e->free.len);
275                         break;
276                 case DATA:
277                         set_data_record(mem + e->base.off, tdb, &e->used);
278                         break;
279                 case HASHTABLE:
280                         set_hashtable(mem + e->base.off, tdb, &e->hashtable);
281                         break;
282                 }
283         }
284         /* Must have a free table! */
285         assert(last_ftable);
286
287         /* Now fill the free and hash tables. */
288         for (i = 0; i < layout->num_elems; i++) {
289                 union tdb_layout_elem *e = &layout->elem[i];
290                 switch (e->base.type) {
291                 case FREE:
292                         add_to_freetable(tdb, e->base.off, e->free.len,
293                                          e->free.ftable_num,
294                                          find_ftable(layout, e->free.ftable_num));
295                         break;
296                 case DATA:
297                         add_to_hashtable(tdb, e->base.off, e->used.key);
298                         break;
299                 default:
300                         break;
301                 }
302         }
303
304         tdb->ftable_off = find_ftable(layout, 0)->base.off;
305
306         /* Get physical if they asked for it. */
307         if (layout->filename) {
308                 int fd = open(layout->filename, O_WRONLY|O_TRUNC|O_CREAT,
309                               0600);
310                 if (fd < 0)
311                         err(1, "opening %s for writing", layout->filename);
312                 if (write(fd, tdb->map_ptr, tdb->map_size) != tdb->map_size)
313                         err(1, "writing %s", layout->filename);
314                 close(fd);
315                 tdb_close(tdb);
316                 /* NOMMAP is for lockcheck. */
317                 tdb = tdb_open(layout->filename, TDB_NOMMAP, O_RDWR, 0,
318                                &tap_log_attr);
319         }
320
321         return tdb;
322 }
323
324 void tdb_layout_free(struct tdb_layout *layout)
325 {
326         unsigned int i;
327
328         for (i = 0; i < layout->num_elems; i++) {
329                 if (layout->elem[i].base.type == DATA) {
330                         free(layout->elem[i].used.key.dptr);
331                         free(layout->elem[i].used.data.dptr);
332                 }
333         }
334         free(layout->elem);
335         free(layout);
336 }