]> git.ozlabs.org Git - ccan/blob - ccan/ntdb/test/api-94-expand-during-parse.c
3aca88bd78bb54ca2cfe4d953e5f135b5994df02
[ccan] / ccan / ntdb / test / api-94-expand-during-parse.c
1 /* We use direct access to hand to the parse function: what if db expands? */
2 #include "config.h"
3 #include "ntdb.h"
4 #include "tap-interface.h"
5 #include "logging.h"
6 #include "../private.h" /* To establish size, esp. for NTDB_INTERNAL dbs */
7
8 static struct ntdb_context *ntdb;
9
10 static off_t ntdb_size(void)
11 {
12         return ntdb->file->map_size;
13 }
14
15 struct parse_info {
16         unsigned int depth;
17         NTDB_DATA expected;
18 };
19
20 static enum NTDB_ERROR parse(NTDB_DATA key, NTDB_DATA data,
21                              struct parse_info *pinfo)
22 {
23         off_t flen;
24         unsigned int i;
25
26         if (!ntdb_deq(data, pinfo->expected))
27                 return NTDB_ERR_EINVAL;
28
29         flen = ntdb_size();
30
31         for (i = 0; ntdb_size() == flen; i++) {
32                 NTDB_DATA add = ntdb_mkdata(&i, sizeof(i));
33
34                 /* This is technically illegal parse(), which is why we
35                  * grabbed allrecord lock.*/
36                 ntdb_store(ntdb, add, add, NTDB_INSERT);
37         }
38
39         /* Access the record again. */
40         if (!ntdb_deq(data, pinfo->expected))
41                 return NTDB_ERR_EINVAL;
42
43         /* Recurse!  Woot! */
44         if (pinfo->depth != 0) {
45                 enum NTDB_ERROR ecode;
46
47                 pinfo->depth--;
48                 ecode = ntdb_parse_record(ntdb, key, parse, pinfo);
49                 if (ecode) {
50                         return ecode;
51                 }
52         }
53
54         /* Access the record one more time. */
55         if (!ntdb_deq(data, pinfo->expected))
56                 return NTDB_ERR_EINVAL;
57
58         return NTDB_SUCCESS;
59 }
60
61 int main(int argc, char *argv[])
62 {
63         unsigned int i;
64         int flags[] = { NTDB_INTERNAL, NTDB_DEFAULT, NTDB_NOMMAP,
65                         NTDB_INTERNAL|NTDB_CONVERT, NTDB_CONVERT,
66                         NTDB_NOMMAP|NTDB_CONVERT };
67         struct parse_info pinfo;
68         NTDB_DATA key = ntdb_mkdata("hello", 5), data = ntdb_mkdata("world", 5);
69
70         plan_tests(sizeof(flags) / sizeof(flags[0]) * 3 + 1);
71         for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
72                 ntdb = ntdb_open("api-94-expand-during-parse.ntdb",
73                                  flags[i]|MAYBE_NOSYNC,
74                                  O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
75                 ok1(ntdb_store(ntdb, key, data, NTDB_INSERT) == NTDB_SUCCESS);
76                 ok1(ntdb_lockall(ntdb) == NTDB_SUCCESS);
77                 pinfo.expected = data;
78                 pinfo.depth = 3;
79                 ok1(ntdb_parse_record(ntdb, key, parse, &pinfo) == NTDB_SUCCESS);
80                 ntdb_unlockall(ntdb);
81                 ntdb_close(ntdb);
82         }
83
84         ok1(tap_log_messages == 0);
85         return exit_status();
86 }