]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/tools/tcdbtest.c
ebl Add program to validate TCDBM lib with accurate mode
[bacula/bacula] / bacula / src / tools / tcdbtest.c
1 #include <tcutil.h>
2 #include <tchdb.h>
3 #include <stdlib.h>
4 #include <stdbool.h>
5 #include <stdint.h>
6 #include <string.h>
7
8 #define NITEMS 5000000
9
10 int main(int argc, char **argv){
11
12   TCHDB *hdb;
13   int ecode;
14   char *key, *value;
15   int i;
16   char save_key[200];
17
18   /* create the object */
19   hdb = tchdbnew();
20
21   tchdbsetcache(hdb, 5000000);
22   tchdbtune(hdb, 9000000, -1, 16, 0);
23
24   /* open the database */
25   if(!tchdbopen(hdb, "casket.hdb", HDBOWRITER | HDBOCREAT)){
26     ecode = tchdbecode(hdb);
27     fprintf(stderr, "open error: %s\n", tchdberrmsg(ecode));
28   }
29
30    for (i=0; i<NITEMS; i++) {
31       char mkey[200];
32       char data[200];
33       sprintf(mkey, "This is htable item %d", i);
34       sprintf(data, "Data for key %d", i);
35       if (!tchdbputasync2(hdb, mkey, data)) {
36          ecode = tchdbecode(hdb);
37          fprintf(stderr, "put error: %s\n", tchdberrmsg(ecode));
38       }
39       if (i == 99) {
40          strcpy(save_key, mkey);
41       }
42    }
43
44   /* retrieve records */
45   value = tchdbget2(hdb, save_key);
46   if(value){
47     printf("%s\n", value);
48     free(value);
49   } else {
50     ecode = tchdbecode(hdb);
51     fprintf(stderr, "get error: %s\n", tchdberrmsg(ecode));
52   }
53
54   /* traverse records */
55   tchdbiterinit(hdb);
56   while((key = tchdbiternext2(hdb)) != NULL){
57     value = tchdbget2(hdb, key);
58     if(value){
59       printf("%s:%s\n", key, value);
60       free(value);
61     }
62     free(key);
63   }
64
65   /* close the database */
66   if(!tchdbclose(hdb)){
67     ecode = tchdbecode(hdb);
68     fprintf(stderr, "close error: %s\n", tchdberrmsg(ecode));
69   }
70
71   /* delete the object */
72   tchdbdel(hdb);
73
74   return 0;
75 }