11 static void logfn(struct tdb_context *tdb,
12 enum tdb_log_level level,
16 fprintf(stderr, "tdb:%s:%s\n", tdb_name(tdb), message);
19 int main(int argc, char *argv[])
21 unsigned int i, j, users, groups;
22 TDB_DATA idxkey, idxdata;
25 struct tdb_context *tdb;
27 union tdb_attribute log;
30 printf("Usage: growtdb-bench <users> <groups>\n");
33 users = atoi(argv[1]);
34 groups = atoi(argv[2]);
36 sprintf(cmd, "cat /proc/%i/statm", getpid());
38 log.base.attr = TDB_ATTRIBUTE_LOG;
42 tdb = tdb_open("/tmp/growtdb.tdb", TDB_DEFAULT,
43 O_RDWR|O_CREAT|O_TRUNC, 0600, &log);
45 idxkey.dptr = (unsigned char *)"User index";
46 idxkey.dsize = strlen("User index");
48 idxdata.dptr = calloc(idxdata.dsize, 1);
52 k.dptr = calloc(k.dsize, 1);
54 d.dptr = calloc(d.dsize, 1);
56 tdb_transaction_start(tdb);
57 for (i = 0; i < users; i++) {
58 memcpy(k.dptr, &i, sizeof(i));
59 ecode = tdb_store(tdb, k, d, TDB_INSERT);
60 if (ecode != TDB_SUCCESS)
61 errx(1, "tdb insert failed: %s", tdb_errorstr(ecode));
63 /* This simulates a growing index record. */
64 ecode = tdb_append(tdb, idxkey, idxdata);
65 if (ecode != TDB_SUCCESS)
66 errx(1, "tdb append failed: %s", tdb_errorstr(ecode));
68 if ((ecode = tdb_transaction_commit(tdb)) != 0)
69 errx(1, "tdb commit1 failed: %s", tdb_errorstr(ecode));
71 if ((ecode = tdb_check(tdb, NULL, NULL)) != 0)
72 errx(1, "tdb_check failed after initial insert!");
76 /* Now put them all in groups: add 32 bytes to each record for
79 gk.dptr = calloc(k.dsize, 1);
80 gk.dptr[gk.dsize-1] = 1;
83 for (i = 0; i < groups; i++) {
84 tdb_transaction_start(tdb);
85 /* Create the "group". */
86 memcpy(gk.dptr, &i, sizeof(i));
87 ecode = tdb_store(tdb, gk, d, TDB_INSERT);
88 if (ecode != TDB_SUCCESS)
89 errx(1, "tdb insert failed: %s", tdb_errorstr(ecode));
91 /* Now populate it. */
92 for (j = 0; j < users; j++) {
93 /* Append to the user. */
94 memcpy(k.dptr, &j, sizeof(j));
95 if ((ecode = tdb_append(tdb, k, d)) != 0)
96 errx(1, "tdb append failed: %s",
99 /* Append to the group. */
100 if ((ecode = tdb_append(tdb, gk, d)) != 0)
101 errx(1, "tdb append failed: %s",
102 tdb_errorstr(ecode));
104 if ((ecode = tdb_transaction_commit(tdb)) != 0)
105 errx(1, "tdb commit2 failed: %s", tdb_errorstr(ecode));
106 if ((ecode = tdb_check(tdb, NULL, NULL)) != 0)
107 errx(1, "tdb_check failed after iteration %i!", i);