6 #include <ccan/err/err.h>
8 static void logfn(struct ntdb_context *ntdb,
9 enum ntdb_log_level level,
10 enum NTDB_ERROR ecode,
14 fprintf(stderr, "ntdb:%s:%s:%s\n",
15 ntdb_name(ntdb), ntdb_errorstr(ecode), message);
18 int main(int argc, char *argv[])
20 unsigned int i, j, users, groups;
21 NTDB_DATA idxkey, idxdata;
24 struct ntdb_context *ntdb;
25 enum NTDB_ERROR ecode;
26 union ntdb_attribute log;
29 printf("Usage: growtdb-bench <users> <groups>\n");
32 users = atoi(argv[1]);
33 groups = atoi(argv[2]);
35 sprintf(cmd, "cat /proc/%i/statm", getpid());
37 log.base.attr = NTDB_ATTRIBUTE_LOG;
41 ntdb = ntdb_open("/tmp/growtdb.ntdb", NTDB_DEFAULT,
42 O_RDWR|O_CREAT|O_TRUNC, 0600, &log);
44 idxkey.dptr = (unsigned char *)"User index";
45 idxkey.dsize = strlen("User index");
47 idxdata.dptr = calloc(idxdata.dsize, 1);
48 if (idxdata.dptr == NULL) {
49 fprintf(stderr, "Unable to allocate memory for idxdata.dptr\n");
55 k.dptr = calloc(k.dsize, 1);
57 fprintf(stderr, "Unable to allocate memory for k.dptr\n");
61 d.dptr = calloc(d.dsize, 1);
63 fprintf(stderr, "Unable to allocate memory for d.dptr\n");
67 ntdb_transaction_start(ntdb);
68 for (i = 0; i < users; i++) {
69 memcpy(k.dptr, &i, sizeof(i));
70 ecode = ntdb_store(ntdb, k, d, NTDB_INSERT);
71 if (ecode != NTDB_SUCCESS)
72 errx(1, "ntdb insert failed: %s", ntdb_errorstr(ecode));
74 /* This simulates a growing index record. */
75 ecode = ntdb_append(ntdb, idxkey, idxdata);
76 if (ecode != NTDB_SUCCESS)
77 errx(1, "ntdb append failed: %s", ntdb_errorstr(ecode));
79 if ((ecode = ntdb_transaction_commit(ntdb)) != 0)
80 errx(1, "ntdb commit1 failed: %s", ntdb_errorstr(ecode));
82 if ((ecode = ntdb_check(ntdb, NULL, NULL)) != 0)
83 errx(1, "ntdb_check failed after initial insert!");
87 /* Now put them all in groups: add 32 bytes to each record for
90 gk.dptr = calloc(k.dsize, 1);
91 if (gk.dptr == NULL) {
92 fprintf(stderr, "Unable to allocate memory for gk.dptr\n");
95 gk.dptr[gk.dsize-1] = 1;
98 for (i = 0; i < groups; i++) {
99 ntdb_transaction_start(ntdb);
100 /* Create the "group". */
101 memcpy(gk.dptr, &i, sizeof(i));
102 ecode = ntdb_store(ntdb, gk, d, NTDB_INSERT);
103 if (ecode != NTDB_SUCCESS)
104 errx(1, "ntdb insert failed: %s", ntdb_errorstr(ecode));
106 /* Now populate it. */
107 for (j = 0; j < users; j++) {
108 /* Append to the user. */
109 memcpy(k.dptr, &j, sizeof(j));
110 if ((ecode = ntdb_append(ntdb, k, d)) != 0)
111 errx(1, "ntdb append failed: %s",
112 ntdb_errorstr(ecode));
114 /* Append to the group. */
115 if ((ecode = ntdb_append(ntdb, gk, d)) != 0)
116 errx(1, "ntdb append failed: %s",
117 ntdb_errorstr(ecode));
119 if ((ecode = ntdb_transaction_commit(ntdb)) != 0)
120 errx(1, "ntdb commit2 failed: %s", ntdb_errorstr(ecode));
121 if ((ecode = ntdb_check(ntdb, NULL, NULL)) != 0)
122 errx(1, "ntdb_check failed after iteration %i!", i);