check_type: fix incorrect documentation.
[ccan] / ccan / tdb2 / tools / growtdb-bench.c
1 #include "tdb2.h"
2 #include <stdlib.h>
3 #include <string.h>
4 #include <stdio.h>
5 #include <unistd.h>
6 #include <err.h>
7 #include <sys/types.h>
8 #include <sys/stat.h>
9 #include <fcntl.h>
10
11 static void logfn(struct tdb_context *tdb,
12                   enum tdb_log_level level,
13                   enum TDB_ERROR ecode,
14                   const char *message,
15                   void *data)
16 {
17         fprintf(stderr, "tdb:%s:%s:%s\n",
18                 tdb_name(tdb), tdb_errorstr(ecode), message);
19 }
20
21 int main(int argc, char *argv[])
22 {
23         unsigned int i, j, users, groups;
24         TDB_DATA idxkey, idxdata;
25         TDB_DATA k, d, gk;
26         char cmd[100];
27         struct tdb_context *tdb;
28         enum TDB_ERROR ecode;
29         union tdb_attribute log;
30
31         if (argc != 3) {
32                 printf("Usage: growtdb-bench <users> <groups>\n");
33                 exit(1);
34         }
35         users = atoi(argv[1]);
36         groups = atoi(argv[2]);
37
38         sprintf(cmd, "cat /proc/%i/statm", getpid());
39
40         log.base.attr = TDB_ATTRIBUTE_LOG;
41         log.base.next = NULL;
42         log.log.fn = logfn;
43         
44         tdb = tdb_open("/tmp/growtdb.tdb", TDB_DEFAULT,
45                        O_RDWR|O_CREAT|O_TRUNC, 0600, &log);
46
47         idxkey.dptr = (unsigned char *)"User index";
48         idxkey.dsize = strlen("User index");
49         idxdata.dsize = 51;
50         idxdata.dptr = calloc(idxdata.dsize, 1);
51
52         /* Create users. */
53         k.dsize = 48;
54         k.dptr = calloc(k.dsize, 1);
55         d.dsize = 64;
56         d.dptr = calloc(d.dsize, 1);
57
58         tdb_transaction_start(tdb);
59         for (i = 0; i < users; i++) {
60                 memcpy(k.dptr, &i, sizeof(i));
61                 ecode = tdb_store(tdb, k, d, TDB_INSERT);
62                 if (ecode != TDB_SUCCESS)
63                         errx(1, "tdb insert failed: %s", tdb_errorstr(ecode));
64
65                 /* This simulates a growing index record. */
66                 ecode = tdb_append(tdb, idxkey, idxdata);
67                 if (ecode != TDB_SUCCESS)
68                         errx(1, "tdb append failed: %s", tdb_errorstr(ecode));
69         }
70         if ((ecode = tdb_transaction_commit(tdb)) != 0)
71                 errx(1, "tdb commit1 failed: %s", tdb_errorstr(ecode));
72
73         if ((ecode = tdb_check(tdb, NULL, NULL)) != 0)
74                 errx(1, "tdb_check failed after initial insert!");
75
76         system(cmd);
77
78         /* Now put them all in groups: add 32 bytes to each record for
79          * a group. */
80         gk.dsize = 48;
81         gk.dptr = calloc(k.dsize, 1);
82         gk.dptr[gk.dsize-1] = 1;
83
84         d.dsize = 32;
85         for (i = 0; i < groups; i++) {
86                 tdb_transaction_start(tdb);
87                 /* Create the "group". */
88                 memcpy(gk.dptr, &i, sizeof(i));
89                 ecode = tdb_store(tdb, gk, d, TDB_INSERT);
90                 if (ecode != TDB_SUCCESS)
91                         errx(1, "tdb insert failed: %s", tdb_errorstr(ecode));
92
93                 /* Now populate it. */
94                 for (j = 0; j < users; j++) {
95                         /* Append to the user. */
96                         memcpy(k.dptr, &j, sizeof(j));
97                         if ((ecode = tdb_append(tdb, k, d)) != 0)
98                                 errx(1, "tdb append failed: %s",
99                                      tdb_errorstr(ecode));
100                         
101                         /* Append to the group. */
102                         if ((ecode = tdb_append(tdb, gk, d)) != 0)
103                                 errx(1, "tdb append failed: %s",
104                                      tdb_errorstr(ecode));
105                 }
106                 if ((ecode = tdb_transaction_commit(tdb)) != 0)
107                         errx(1, "tdb commit2 failed: %s", tdb_errorstr(ecode));
108                 if ((ecode = tdb_check(tdb, NULL, NULL)) != 0)
109                         errx(1, "tdb_check failed after iteration %i!", i);
110                 system(cmd);
111         }
112
113         return 0;
114 }