]> git.sur5r.net Git - bacula/bacula/blob - bacula/patches/testing/tcdbtest.c
59f2c5e192d506144e2c1230b07453eb95cb47b7
[bacula/bacula] / bacula / patches / testing / tcdbtest.c
1 /* 
2  export LD_LIBRARY_PATH=/home/eric/dev/bacula/tcdb/lib/
3  g++ -I/home/eric/dev/bacula/tcdb/include -o tt -L /home/eric/dev/bacula/tcdb/lib/ -ltokyocabinet -lz -lpthread -lm tcdbtest.c 
4
5 ./tt $(wc -l src.txt2) 0
6
7  */
8
9 #include <tcutil.h>
10 #include <tchdb.h>
11 #include <stdlib.h>
12 #include <stdbool.h>
13 #include <stdint.h>
14 #include <string.h>
15 #include <sys/time.h>
16 #include <time.h>
17 #include <unistd.h>
18 #include <sys/stat.h>
19 #include <signal.h>
20
21 #define NITEMS 5000000
22
23
24
25 void atend()
26 {
27    unlink("casket.hdb");
28 }
29
30 int64_t get_current_time()
31 {
32    struct timeval tv;
33    if (gettimeofday(&tv, NULL) != 0) {
34       tv.tv_sec = (long)time(NULL);   /* fall back to old method */
35       tv.tv_usec = 0;
36    }
37    return (int64_t)tv.tv_sec * 1000000 + (int64_t)tv.tv_usec;
38 }
39
40 int main(int argc, char **argv){
41
42    FILE *fp, *res;
43    TCHDB *hdb;
44    int ecode;
45    char *key, *value;
46    int i=0;
47    char save_key[512];
48    char line[512];
49    int64_t ctime, ttime;;
50    char result[200];
51
52    if (argc != 4) {
53       fprintf(stderr, "Usage: tt count file cache %i\n");
54       exit(1);
55    }
56
57    atexit(atend);
58    signal(15, exit);
59    signal(2, exit);
60
61    snprintf(result, sizeof(result), "result.%i", getpid());
62    res = fopen(result, "w");
63    
64    /* create the object */
65    hdb = tchdbnew();
66
67    if (atoi(argv[3]) > 0) {
68       tchdbsetcache(hdb, atoi(argv[3]));
69    }
70    fprintf(res, "cache;%i\n", atoi(argv[3]));
71
72    /*
73     * apow : 128 (size of stat hash field)
74     */
75    int opt=0;//HDBTTCBS;
76    tchdbtune(hdb, atoll(argv[1]), 7, 16, opt);
77    fprintf(res, "bucket;%lli\n", atoll(argv[1]));
78    fprintf(res, "compress;%i\n", opt);
79
80    /* open the database */
81    if(!tchdbopen(hdb, "casket.hdb", HDBOWRITER | HDBOCREAT)){
82       ecode = tchdbecode(hdb);
83       fprintf(stderr, "open error: %s\n", tchdberrmsg(ecode));
84    }
85
86    ctime = get_current_time();
87
88    /* fill hash with real data (find / > src.txt) */
89    fp = fopen(argv[2], "r");
90    if (!fp) {
91       fprintf(stderr, "open %s file error\n", argv[2]);
92       exit (1);
93    }
94    while (fgets(line, sizeof(line), fp)) {
95       char *data = "A AAA B AZS SDSZ EZEZ SSDFS AEZEZEZ ZEZDSDDQe";
96       if (!tchdbputasync2(hdb, line, data)) {
97          ecode = tchdbecode(hdb);
98          fprintf(stderr, "put error: %s\n", tchdberrmsg(ecode));
99       }
100       if (i++ == 99) {
101          strcpy(save_key, line);
102       }
103    }
104    fclose(fp);
105
106    ttime= get_current_time();
107    fprintf(res, "nbelt;%lli\n", i);
108
109    fprintf(stderr, "loading %i file into hash database in %ims\n", 
110            i, (ttime - ctime)/1000);
111    fprintf(res, "load;%i\n", (ttime - ctime)/1000);
112
113
114    /* retrieve records */
115    value = tchdbget2(hdb, save_key);
116    if(value){
117       //printf("%s:%s\n", save_key, value);
118       free(value);
119    } else {
120       ecode = tchdbecode(hdb);
121       fprintf(stderr, "get error: %s\n", tchdberrmsg(ecode));
122    }
123
124    /* retrieve all records and mark them as seen */
125    i=0;
126    fp = fopen(argv[2], "r");
127    if (!fp) {
128       fprintf(stderr, "open %s file error\n", argv[2]);
129       exit (1);
130    }
131    while (fgets(line, sizeof(line), fp)) {
132       char *data = "A AAA B AZS SDSZ EZEZ SSDFS AEZEZEZ ZEZDSDDQe";
133       if (i++ != 200) {
134          value = tchdbget2(hdb, line);
135          if (value) {
136             *value=0;
137             if (!tchdbputasync2(hdb, line, value)) {
138                ecode = tchdbecode(hdb);
139                fprintf(stderr, "put error: %s\n", tchdberrmsg(ecode));
140             }
141             free(value);
142          } else {
143             fprintf(stderr, "can't find %s in hash\n", line);
144          }
145       }
146    }
147    fclose(fp);
148
149    ctime = get_current_time();
150    fprintf(stderr, "marking as seen in %ims\n", (ctime - ttime)/1000);
151    fprintf(res, "seen;%i\n", (ctime - ttime)/1000);
152
153   /* traverse records */
154   tchdbiterinit(hdb);
155   while((key = tchdbiternext2(hdb)) != NULL){
156     value = tchdbget2(hdb, key);
157     if(value && *value){
158        //printf("%s:%s\n", key, value);
159       free(value);
160     }
161     free(key);
162   }
163
164    ttime = get_current_time();
165    fprintf(stderr, "checking not seen in %ims\n", (ttime - ctime)/1000);
166    fprintf(res, "walk;%i\n", (ttime - ctime)/1000);
167
168   /* close the database */
169   if(!tchdbclose(hdb)){
170     ecode = tchdbecode(hdb);
171     fprintf(stderr, "close error: %s\n", tchdberrmsg(ecode));
172   }
173
174   /* delete the object */
175   tchdbdel(hdb);
176   struct stat statp;
177   stat("casket.hdb", &statp);
178   fprintf(res, "size;%lli\n", statp.st_size);
179   unlink("casket.hdb");
180   fclose(res);
181   return 0;
182 }