6 #include <ccan/err/err.h>
11 static void logfn(struct ntdb_context
*ntdb
,
12 enum ntdb_log_level level
,
13 enum NTDB_ERROR ecode
,
17 fprintf(stderr
, "ntdb:%s:%s:%s\n",
18 ntdb_name(ntdb
), ntdb_errorstr(ecode
), message
);
21 int main(int argc
, char *argv
[])
23 unsigned int i
, j
, users
, groups
;
24 NTDB_DATA idxkey
, idxdata
;
27 struct ntdb_context
*ntdb
;
28 enum NTDB_ERROR ecode
;
29 union ntdb_attribute log
;
32 printf("Usage: growtdb-bench <users> <groups>\n");
35 users
= atoi(argv
[1]);
36 groups
= atoi(argv
[2]);
38 sprintf(cmd
, "cat /proc/%i/statm", getpid());
40 log
.base
.attr
= NTDB_ATTRIBUTE_LOG
;
44 ntdb
= ntdb_open("/tmp/growtdb.ntdb", NTDB_DEFAULT
,
45 O_RDWR
|O_CREAT
|O_TRUNC
, 0600, &log
);
47 idxkey
.dptr
= (unsigned char *)"User index";
48 idxkey
.dsize
= strlen("User index");
50 idxdata
.dptr
= calloc(idxdata
.dsize
, 1);
51 if (idxdata
.dptr
== NULL
) {
52 fprintf(stderr
, "Unable to allocate memory for idxdata.dptr\n");
58 k
.dptr
= calloc(k
.dsize
, 1);
60 fprintf(stderr
, "Unable to allocate memory for k.dptr\n");
64 d
.dptr
= calloc(d
.dsize
, 1);
66 fprintf(stderr
, "Unable to allocate memory for d.dptr\n");
70 ntdb_transaction_start(ntdb
);
71 for (i
= 0; i
< users
; i
++) {
72 memcpy(k
.dptr
, &i
, sizeof(i
));
73 ecode
= ntdb_store(ntdb
, k
, d
, NTDB_INSERT
);
74 if (ecode
!= NTDB_SUCCESS
)
75 errx(1, "ntdb insert failed: %s", ntdb_errorstr(ecode
));
77 /* This simulates a growing index record. */
78 ecode
= ntdb_append(ntdb
, idxkey
, idxdata
);
79 if (ecode
!= NTDB_SUCCESS
)
80 errx(1, "ntdb append failed: %s", ntdb_errorstr(ecode
));
82 if ((ecode
= ntdb_transaction_commit(ntdb
)) != 0)
83 errx(1, "ntdb commit1 failed: %s", ntdb_errorstr(ecode
));
85 if ((ecode
= ntdb_check(ntdb
, NULL
, NULL
)) != 0)
86 errx(1, "ntdb_check failed after initial insert!");
90 /* Now put them all in groups: add 32 bytes to each record for
93 gk
.dptr
= calloc(k
.dsize
, 1);
94 if (gk
.dptr
== NULL
) {
95 fprintf(stderr
, "Unable to allocate memory for gk.dptr\n");
98 gk
.dptr
[gk
.dsize
-1] = 1;
101 for (i
= 0; i
< groups
; i
++) {
102 ntdb_transaction_start(ntdb
);
103 /* Create the "group". */
104 memcpy(gk
.dptr
, &i
, sizeof(i
));
105 ecode
= ntdb_store(ntdb
, gk
, d
, NTDB_INSERT
);
106 if (ecode
!= NTDB_SUCCESS
)
107 errx(1, "ntdb insert failed: %s", ntdb_errorstr(ecode
));
109 /* Now populate it. */
110 for (j
= 0; j
< users
; j
++) {
111 /* Append to the user. */
112 memcpy(k
.dptr
, &j
, sizeof(j
));
113 if ((ecode
= ntdb_append(ntdb
, k
, d
)) != 0)
114 errx(1, "ntdb append failed: %s",
115 ntdb_errorstr(ecode
));
117 /* Append to the group. */
118 if ((ecode
= ntdb_append(ntdb
, gk
, d
)) != 0)
119 errx(1, "ntdb append failed: %s",
120 ntdb_errorstr(ecode
));
122 if ((ecode
= ntdb_transaction_commit(ntdb
)) != 0)
123 errx(1, "ntdb commit2 failed: %s", ntdb_errorstr(ecode
));
124 if ((ecode
= ntdb_check(ntdb
, NULL
, NULL
)) != 0)
125 errx(1, "ntdb_check failed after iteration %i!", i
);