ccan/ntdb: demote to junkcode.
[ccan] / junkcode / rusty@rustcorp.com.au-ntdb / test / helprun-layout.h
1 /* NTDB 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 <ccan/err/err.h>
7 #include "logging.h"
8
9 struct ntdb_layout *new_ntdb_layout(void)
10 {
11         struct ntdb_layout *layout = malloc(sizeof(*layout));
12         layout->num_elems = 0;
13         layout->elem = NULL;
14         return layout;
15 }
16
17 static void add(struct ntdb_layout *layout, union ntdb_layout_elem elem)
18 {
19         layout->elem = realloc(layout->elem,
20                                sizeof(layout->elem[0])
21                                * (layout->num_elems+1));
22         layout->elem[layout->num_elems++] = elem;
23 }
24
25 void ntdb_layout_add_freetable(struct ntdb_layout *layout)
26 {
27         union ntdb_layout_elem elem;
28         elem.base.type = FREETABLE;
29         add(layout, elem);
30 }
31
32 void ntdb_layout_add_free(struct ntdb_layout *layout, ntdb_len_t len,
33                          unsigned ftable)
34 {
35         union ntdb_layout_elem elem;
36         elem.base.type = FREE;
37         elem.free.len = len;
38         elem.free.ftable_num = ftable;
39         add(layout, elem);
40 }
41
42 void ntdb_layout_add_capability(struct ntdb_layout *layout,
43                                uint64_t type,
44                                bool write_breaks,
45                                bool check_breaks,
46                                bool open_breaks,
47                                ntdb_len_t extra)
48 {
49         union ntdb_layout_elem elem;
50         elem.base.type = CAPABILITY;
51         elem.capability.type = type;
52         if (write_breaks)
53                 elem.capability.type |= NTDB_CAP_NOWRITE;
54         if (open_breaks)
55                 elem.capability.type |= NTDB_CAP_NOOPEN;
56         if (check_breaks)
57                 elem.capability.type |= NTDB_CAP_NOCHECK;
58         elem.capability.extra = extra;
59         add(layout, elem);
60 }
61
62 static NTDB_DATA dup_key(NTDB_DATA key)
63 {
64         NTDB_DATA ret;
65         ret.dsize = key.dsize;
66         ret.dptr = malloc(ret.dsize);
67         memcpy(ret.dptr, key.dptr, ret.dsize);
68         return ret;
69 }
70
71 void ntdb_layout_add_used(struct ntdb_layout *layout,
72                          NTDB_DATA key, NTDB_DATA data,
73                          ntdb_len_t extra)
74 {
75         union ntdb_layout_elem elem;
76         elem.base.type = DATA;
77         elem.used.key = dup_key(key);
78         elem.used.data = dup_key(data);
79         elem.used.extra = extra;
80         add(layout, elem);
81 }
82
83 static ntdb_len_t free_record_len(ntdb_len_t len)
84 {
85         return sizeof(struct ntdb_used_record) + len;
86 }
87
88 static ntdb_len_t data_record_len(struct tle_used *used)
89 {
90         ntdb_len_t len;
91         len = sizeof(struct ntdb_used_record)
92                 + used->key.dsize + used->data.dsize + used->extra;
93         assert(len >= sizeof(struct ntdb_free_record));
94         return len;
95 }
96
97 static ntdb_len_t capability_len(struct tle_capability *cap)
98 {
99         return sizeof(struct ntdb_capability) + cap->extra;
100 }
101
102 static ntdb_len_t freetable_len(struct tle_freetable *ftable)
103 {
104         return sizeof(struct ntdb_freetable);
105 }
106
107 static void set_free_record(void *mem, ntdb_len_t len)
108 {
109         /* We do all the work in add_to_freetable */
110 }
111
112 static void add_zero_pad(struct ntdb_used_record *u, size_t len, size_t extra)
113 {
114         if (extra)
115                 ((char *)(u + 1))[len] = '\0';
116 }
117
118 static void set_data_record(void *mem, struct ntdb_context *ntdb,
119                             struct tle_used *used)
120 {
121         struct ntdb_used_record *u = mem;
122
123         set_header(ntdb, u, NTDB_USED_MAGIC, used->key.dsize, used->data.dsize,
124                    used->key.dsize + used->data.dsize + used->extra);
125         memcpy(u + 1, used->key.dptr, used->key.dsize);
126         memcpy((char *)(u + 1) + used->key.dsize,
127                used->data.dptr, used->data.dsize);
128         add_zero_pad(u, used->key.dsize + used->data.dsize, used->extra);
129 }
130
131 static void set_capability(void *mem, struct ntdb_context *ntdb,
132                            struct tle_capability *cap, struct ntdb_header *hdr,
133                            ntdb_off_t last_cap)
134 {
135         struct ntdb_capability *c = mem;
136         ntdb_len_t len = sizeof(*c) - sizeof(struct ntdb_used_record) + cap->extra;
137
138         c->type = cap->type;
139         c->next = 0;
140         set_header(ntdb, &c->hdr, NTDB_CAP_MAGIC, 0, len, len);
141
142         /* Append to capability list. */
143         if (!last_cap) {
144                 hdr->capabilities = cap->base.off;
145         } else {
146                 c = (struct ntdb_capability *)((char *)hdr + last_cap);
147                 c->next = cap->base.off;
148         }
149 }
150
151 static void set_freetable(void *mem, struct ntdb_context *ntdb,
152                          struct tle_freetable *freetable, struct ntdb_header *hdr,
153                          ntdb_off_t last_ftable)
154 {
155         struct ntdb_freetable *ftable = mem;
156         memset(ftable, 0, sizeof(*ftable));
157         set_header(ntdb, &ftable->hdr, NTDB_FTABLE_MAGIC, 0,
158                         sizeof(*ftable) - sizeof(ftable->hdr),
159                         sizeof(*ftable) - sizeof(ftable->hdr));
160
161         if (last_ftable) {
162                 ftable = (struct ntdb_freetable *)((char *)hdr + last_ftable);
163                 ftable->next = freetable->base.off;
164         } else {
165                 hdr->free_table = freetable->base.off;
166         }
167 }
168
169 static void add_to_freetable(struct ntdb_context *ntdb,
170                              ntdb_off_t eoff,
171                              ntdb_off_t elen,
172                              unsigned ftable,
173                              struct tle_freetable *freetable)
174 {
175         ntdb->ftable_off = freetable->base.off;
176         ntdb->ftable = ftable;
177         add_free_record(ntdb, eoff, sizeof(struct ntdb_used_record) + elen,
178                         NTDB_LOCK_WAIT, false);
179 }
180
181 static ntdb_off_t hbucket_offset(ntdb_len_t idx)
182 {
183         return sizeof(struct ntdb_header) + sizeof(struct ntdb_used_record)
184                 + idx * sizeof(ntdb_off_t);
185 }
186
187 /* FIXME: Our hash table handling here is primitive: we don't expand! */
188 static void add_to_hashtable(struct ntdb_context *ntdb,
189                              ntdb_off_t eoff,
190                              NTDB_DATA key)
191 {
192         ntdb_off_t b_off;
193         uint32_t h = ntdb_hash(ntdb, key.dptr, key.dsize);
194
195         b_off = hbucket_offset(h & ((1 << ntdb->hash_bits)-1));
196         if (ntdb_read_off(ntdb, b_off) != 0)
197                 abort();
198
199         ntdb_write_off(ntdb, b_off, encode_offset(ntdb, eoff, h));
200 }
201
202 static struct tle_freetable *find_ftable(struct ntdb_layout *layout, unsigned num)
203 {
204         unsigned i;
205
206         for (i = 0; i < layout->num_elems; i++) {
207                 if (layout->elem[i].base.type != FREETABLE)
208                         continue;
209                 if (num == 0)
210                         return &layout->elem[i].ftable;
211                 num--;
212         }
213         abort();
214 }
215
216 /* FIXME: Support NTDB_CONVERT */
217 struct ntdb_context *ntdb_layout_get(struct ntdb_layout *layout,
218                                    void (*freefn)(void *),
219                                    union ntdb_attribute *attr)
220 {
221         unsigned int i;
222         ntdb_off_t off, hdrlen, len, last_ftable, last_cap;
223         char *mem;
224         struct ntdb_context *ntdb;
225
226         /* Now populate our header, cribbing from a real NTDB header. */
227         ntdb = ntdb_open("layout", NTDB_INTERNAL, O_RDWR, 0, attr);
228
229         off = sizeof(struct ntdb_header) + sizeof(struct ntdb_used_record)
230                 + (sizeof(ntdb_off_t) << ntdb->hash_bits);
231         hdrlen = off;
232
233         /* First pass of layout: calc lengths */
234         for (i = 0; i < layout->num_elems; i++) {
235                 union ntdb_layout_elem *e = &layout->elem[i];
236                 e->base.off = off;
237                 switch (e->base.type) {
238                 case FREETABLE:
239                         len = freetable_len(&e->ftable);
240                         break;
241                 case FREE:
242                         len = free_record_len(e->free.len);
243                         break;
244                 case DATA:
245                         len = data_record_len(&e->used);
246                         break;
247                 case CAPABILITY:
248                         len = capability_len(&e->capability);
249                         break;
250                 default:
251                         abort();
252                 }
253                 off += len;
254         }
255
256         mem = malloc(off);
257         /* Fill with some weird pattern. */
258         memset(mem, 0x99, off);
259         memcpy(mem, ntdb->file->map_ptr, hdrlen);
260
261         /* Mug the ntdb we have to make it use this. */
262         freefn(ntdb->file->map_ptr);
263         ntdb->file->map_ptr = mem;
264         ntdb->file->map_size = off;
265
266         last_ftable = 0;
267         last_cap = 0;
268         for (i = 0; i < layout->num_elems; i++) {
269                 union ntdb_layout_elem *e = &layout->elem[i];
270                 switch (e->base.type) {
271                 case FREETABLE:
272                         set_freetable(mem + e->base.off, ntdb, &e->ftable,
273                                      (struct ntdb_header *)mem, last_ftable);
274                         last_ftable = e->base.off;
275                         break;
276                 case FREE:
277                         set_free_record(mem + e->base.off, e->free.len);
278                         break;
279                 case DATA:
280                         set_data_record(mem + e->base.off, ntdb, &e->used);
281                         break;
282                 case CAPABILITY:
283                         set_capability(mem + e->base.off, ntdb, &e->capability,
284                                        (struct ntdb_header *)mem, last_cap);
285                         last_cap = e->base.off;
286                         break;
287                 }
288         }
289         /* Must have a free table! */
290         assert(last_ftable);
291
292         /* Now fill the free and hash tables. */
293         for (i = 0; i < layout->num_elems; i++) {
294                 union ntdb_layout_elem *e = &layout->elem[i];
295                 switch (e->base.type) {
296                 case FREE:
297                         add_to_freetable(ntdb, e->base.off, e->free.len,
298                                          e->free.ftable_num,
299                                          find_ftable(layout, e->free.ftable_num));
300                         break;
301                 case DATA:
302                         add_to_hashtable(ntdb, e->base.off, e->used.key);
303                         break;
304                 default:
305                         break;
306                 }
307         }
308
309         ntdb->ftable_off = find_ftable(layout, 0)->base.off;
310         return ntdb;
311 }
312
313 void ntdb_layout_write(struct ntdb_layout *layout, void (*freefn)(void *),
314                        union ntdb_attribute *attr, const char *filename)
315 {
316         struct ntdb_context *ntdb = ntdb_layout_get(layout, freefn, attr);
317         int fd;
318
319         fd = open(filename, O_WRONLY|O_TRUNC|O_CREAT,  0600);
320         if (fd < 0)
321                 err(1, "opening %s for writing", filename);
322         if (write(fd, ntdb->file->map_ptr, ntdb->file->map_size)
323             != ntdb->file->map_size)
324                 err(1, "writing %s", filename);
325         close(fd);
326         ntdb_close(ntdb);
327 }
328
329 void ntdb_layout_free(struct ntdb_layout *layout)
330 {
331         unsigned int i;
332
333         for (i = 0; i < layout->num_elems; i++) {
334                 if (layout->elem[i].base.type == DATA) {
335                         free(layout->elem[i].used.key.dptr);
336                         free(layout->elem[i].used.data.dptr);
337                 }
338         }
339         free(layout->elem);
340         free(layout);
341 }