]> git.sur5r.net Git - bacula/bacula/blob - bacula/patches/testing/hash.c
ebl add new hash lib test code
[bacula/bacula] / bacula / patches / testing / hash.c
1
2 #include <stdlib.h>
3 #include <stdbool.h>
4 #include <stdint.h>
5 #include <string.h>
6 #include <unistd.h>
7 #include <sys/time.h>
8 #include <time.h>
9
10 int64_t get_current_time()
11 {
12    struct timeval tv;
13    if (gettimeofday(&tv, NULL) != 0) {
14       tv.tv_sec = (long)time(NULL);   /* fall back to old method */
15       tv.tv_usec = 0;
16    }
17    return (int64_t)tv.tv_sec * 1000000 + (int64_t)tv.tv_usec;
18 }
19
20 #ifdef USE_TCHDB
21 # include <tcutil.h>
22 # include <tchdb.h>
23
24 TCHDB *hdb;
25 TCXSTR *kstr;
26 TCXSTR *dstr;
27
28 bool hash_init(int nbelt)
29 {
30   int opt=HDBTLARGE ;//| HDBTTCBS;
31    
32   /* create the object */
33   hdb = tchdbnew();
34
35   kstr = tcxstrnew();
36   dstr = tcxstrnew();
37
38   tchdbsetcache(hdb, nbelt);
39   fprintf(stderr, "cache;%i\n", nbelt);
40   tchdbtune(hdb, nbelt*4, -1, 16, 0);
41
42   fprintf(stderr, "bucket;%lli\n", (int64_t) nbelt*4);
43   fprintf(stderr, "option;%i\n", opt);
44   unlink("casket.hdb");
45
46   /* open the database */
47   if(!tchdbopen(hdb, "casket.hdb", HDBOWRITER | HDBOCREAT)){
48      fprintf(stderr, "open error\n");
49      exit (1);
50   }
51   return 1;
52 }
53
54 bool hash_put(char *key, char *val)
55 {
56    int ecode;
57
58    if (!tchdbputasync2(hdb, key, val)) {
59       ecode = tchdbecode(hdb);
60       fprintf(stderr, "put error: %s\n", tchdberrmsg(ecode));
61       return 0;
62    }
63    return 1;
64 }
65
66 char *hash_get(char *key, char *ret, int len)
67 {
68    return tchdbget2(hdb, key);
69 }
70
71 void hash_free(char *ret)
72 {
73    free(ret);
74 }
75
76 void cleanup()
77 {
78    tchdbclose(hdb);
79    tchdbdel(hdb);
80 }
81
82 void hash_iterinit()
83 {
84   /* traverse records */
85   tchdbiterinit(hdb);
86 }
87
88 bool hash_next(char *line, int len1, char *data, int len2)
89 {
90    return tchdbiternext3(hdb, kstr, dstr);
91 }
92
93 void hash_iterdone()
94 {
95 }
96
97 #endif
98
99
100 #ifdef USE_TCBDB
101 # include <tcutil.h>
102 # include <tcbdb.h>
103
104 TCBDB *hdb;
105 TCXSTR *kstr;
106 TCXSTR *dstr;
107
108 bool hash_init(int nbelt)
109 {
110   int opt=HDBTLARGE ;//| HDBTTCBS;
111    
112   /* create the object */
113   hdb = tcbdbnew();
114
115   kstr = tcxstrnew();
116   dstr = tcxstrnew();
117
118   fprintf(stderr, "cache;%i\n", -1);
119   tcbdbtune(hdb, 0, 0, nbelt*4, -1, -1, opt);
120
121   fprintf(stderr, "bucket;%lli\n", (int64_t) nbelt*4);
122   fprintf(stderr, "option;%i\n", opt);
123   unlink("casket.hdb");
124
125   /* open the database */
126   if(!tcbdbopen(hdb, "casket.hdb", HDBOWRITER | HDBOCREAT)){
127      fprintf(stderr, "open error\n");
128      exit (1);
129   }
130   return 1;
131 }
132
133 bool hash_put(char *key, char *val)
134 {
135    int ecode;
136
137    if (!tcbdbput2(hdb, key, val)) {
138       ecode = tcbdbecode(hdb);
139       fprintf(stderr, "put error: %s\n", tcbdberrmsg(ecode));
140       return 0;
141    }
142    return 1;
143 }
144
145 char *hash_get(char *key, char *ret, int len)
146 {
147    return tcbdbget2(hdb, key);
148 }
149
150 void hash_free(char *ret)
151 {
152    free(ret);
153 }
154
155 void cleanup()
156 {
157    tcbdbclose(hdb);
158    tcbdbdel(hdb);
159 }
160 BDBCUR *iter;
161 void hash_iterinit()
162 {
163   /* traverse records */
164    iter = tcbdbcurnew(hdb);
165    tcbdbcurfirst(iter);
166 }
167
168 bool hash_next(char *line, int len1, char *data, int len2)
169 {
170    bool ret =  tcbdbcurrec(iter, kstr, dstr);
171    tcbdbcurnext(iter);
172    return ret;
173 }
174
175 void hash_iterdone()
176 {
177    tcbdbcurdel(iter);
178 }
179
180 #endif
181
182
183 #ifdef USE_DB
184 # include <db.h>
185 DBT dbkey, dbdata;
186 DB *hdb;
187 bool hash_init(int nbelt)
188 {
189    int ret;
190
191    /* Create and initialize database object, open the database. */
192    if ((ret = db_create(&hdb, NULL, 0)) != 0) {
193       fprintf(stderr,
194               "db_create: %s\n", db_strerror(ret));
195       exit (1);
196    }
197    hdb->set_errfile(hdb, stderr);
198    hdb->set_errpfx(hdb, "hash");
199    if ((ret = hdb->set_pagesize(hdb, 1024)) != 0) {
200       hdb->err(hdb, ret, "set_pagesize");
201       exit (1);
202    }
203    if ((ret = hdb->set_cachesize(hdb, 0, 32 * 1024 * 1024, 0)) != 0) {
204       hdb->err(hdb, ret, "set_cachesize");
205       exit (1);;
206    }
207    fprintf(stderr, "cache;%lli\n", (int64_t)32 * 1024 * 1024);
208    fprintf(stderr, "bucket;0\n");
209    fprintf(stderr, "option;0\n");
210
211    remove("access.db");
212    if ((ret = hdb->open(hdb,
213                         NULL, "access.db", NULL, DB_BTREE, DB_CREATE, 0664)) != 0) {
214       hdb->err(hdb, ret, "access.db: open");
215       exit (1);
216    }
217    memset(&dbkey, 0, sizeof(DBT));
218    memset(&dbdata, 0, sizeof(DBT));
219
220    return 1;
221 }
222
223 bool hash_put(char *key, char *val)
224 {
225    int ret;
226    dbkey.data = key;
227    dbkey.size = (u_int32_t)strlen(key);
228    dbdata.data = val;
229    dbdata.size = strlen(val)+1;
230    if ((ret = hdb->put(hdb, NULL, &dbkey, &dbdata, 0))) {
231       hdb->err(hdb, ret, "DBcursor->put");
232       return 0;
233    }
234    return 1;
235 }
236
237 void cleanup()
238 {
239    hdb->close(hdb, 0);
240 }
241
242 char *hash_get(char *key, char *ret, int len)
243 {
244 /* Zero out the DBTs before using them. */
245    memset(&dbkey, 0, sizeof(DBT));
246    memset(&dbdata, 0, sizeof(DBT));
247    
248    dbkey.data = key;
249    dbkey.ulen = strlen(key);
250    
251    dbdata.data = ret;
252    dbdata.ulen = len;
253    dbdata.flags = DB_DBT_USERMEM;
254
255    if (hdb->get(hdb, NULL, &dbkey, &dbdata, 0) == 0) {
256       return (char *)dbdata.data;
257    } else {
258       return NULL;
259    }
260 }
261
262 void hash_free(char *ret)
263 {
264 }
265
266 DBC *cursorp=NULL;
267 void hash_iterinit()
268 {
269    hdb->cursor(hdb, NULL, &cursorp, 0); 
270 }
271
272 bool hash_next(char *line, int len1, char *data, int len2)
273 {
274    return cursorp->c_get(cursorp, &dbkey, &dbdata, DB_NEXT) == 0;
275 }
276
277 void hash_iterdone()
278 {
279    if (cursorp != NULL) 
280       cursorp->c_close(cursorp); 
281 }
282
283
284 #endif
285
286
287
288 int main(int argc, char **argv)
289 {
290   char *start_heap = (char *)sbrk(0);
291
292   char *value;
293   int i=0;
294   char save_key[200];
295   char mkey[200];
296   char data[200];
297   int64_t ctime, ttime;
298
299   if (argc != 4) {
300       fprintf(stderr, "Usage: tt count file cache\n");
301       exit(1);
302   }
303   
304   hash_init(atoll(argv[1]));
305
306   FILE *fp = fopen(argv[2], "r");
307   if (!fp) {
308      fprintf(stderr, "open %s file error\n", argv[2]);
309      exit (1);
310   }
311   char line[2048];
312   sprintf(data, "AOOODS SDOOQSD A BBBBB AAAAA ZEZEZQS SQDSQDQ DSQ DAZEA ZEA S");
313
314   ctime = get_current_time();
315   fprintf(stderr, "elt;time\n"); 
316   while (fgets(line, sizeof(line), fp)) {
317      hash_put(line, data);
318
319      if (i++ == 99) {
320         strcpy(save_key, mkey);
321      }
322
323      if (i%100000 == 0) {
324         ttime= get_current_time();
325         fprintf(stderr, "%i;%i\n", 
326           i/100000, (int) ((ttime - ctime)/1000));
327      }
328   }
329   ttime= get_current_time();
330   fclose(fp);
331
332   printf("loading %i file into hash database in %ims\n", 
333          i, (ttime - ctime)/1000);
334
335   fprintf(stderr, "load;%i\n", (ttime - ctime)/1000);
336   fprintf(stderr, "elt;%i\n", i);
337
338   ////////////////////////////////////////////////////////////////
339
340   fp = fopen(argv[2], "r");
341   if (!fp) {
342      fprintf(stderr, "open %s file error\n", argv[2]);
343      exit (1);
344   }
345   i=0;
346   ctime = get_current_time();
347   char *p;
348   fprintf(stderr, "elt;time\n"); 
349   while (fgets(line, sizeof(line), fp)) {
350      p = hash_get(line, data, sizeof(data));
351      if (p) {
352         p[5]='H';
353         hash_put(line, p);
354      }
355
356      if (i%100000 == 0) {
357         ttime= get_current_time();
358         fprintf(stderr, "%i;%i\n", 
359           i/100000, (int) ((ttime - ctime)/1000));
360      }
361      i++;
362
363      hash_free(p);
364   }
365   ttime= get_current_time();
366   fprintf(stderr, "seen;%i\n", (ttime - ctime)/1000);
367
368   fprintf(stderr, "elt;time\n"); 
369   i=0;
370   hash_iterinit();
371   while(hash_next(line, sizeof(line), data, sizeof(data)))
372   {
373      if (i%100000 == 0) {
374         ttime= get_current_time();
375         fprintf(stderr, "%i;%i\n", 
376           i/100000, (int) ((ttime - ctime)/1000));
377      }
378      i++;
379   }
380   hash_iterdone();
381
382   ttime = get_current_time();
383   fprintf(stderr, "walk;%i\n", (ttime - ctime)/1000);
384
385   fprintf(stderr, "heap;%lld\n", (long long)((char *)sbrk(0) - start_heap));
386   
387   cleanup();
388
389   return 0;
390 }