10 int64_t get_current_time()
13 if (gettimeofday(&tv, NULL) != 0) {
14 tv.tv_sec = (long)time(NULL); /* fall back to old method */
17 return (int64_t)tv.tv_sec * 1000000 + (int64_t)tv.tv_usec;
28 bool hash_init(int nbelt)
30 int opt=HDBTLARGE ;//| HDBTTCBS;
32 /* create the object */
38 tchdbsetcache(hdb, nbelt);
39 fprintf(stderr, "cache;%i\n", nbelt);
40 tchdbtune(hdb, nbelt*4, -1, 16, 0);
42 fprintf(stderr, "bucket;%lli\n", (int64_t) nbelt*4);
43 fprintf(stderr, "option;%i\n", opt);
46 /* open the database */
47 if(!tchdbopen(hdb, "casket.hdb", HDBOWRITER | HDBOCREAT)){
48 fprintf(stderr, "open error\n");
54 bool hash_put(char *key, char *val)
58 if (!tchdbputasync2(hdb, key, val)) {
59 ecode = tchdbecode(hdb);
60 fprintf(stderr, "put error: %s\n", tchdberrmsg(ecode));
66 char *hash_get(char *key, char *ret, int len)
68 return tchdbget2(hdb, key);
71 void hash_free(char *ret)
84 /* traverse records */
88 bool hash_next(char *line, int len1, char *data, int len2)
90 return tchdbiternext3(hdb, kstr, dstr);
108 bool hash_init(int nbelt)
110 int opt=HDBTLARGE ;//| HDBTTCBS;
112 /* create the object */
118 fprintf(stderr, "cache;%i\n", -1);
119 tcbdbtune(hdb, 0, 0, nbelt*4, -1, -1, opt);
121 fprintf(stderr, "bucket;%lli\n", (int64_t) nbelt*4);
122 fprintf(stderr, "option;%i\n", opt);
123 unlink("casket.hdb");
125 /* open the database */
126 if(!tcbdbopen(hdb, "casket.hdb", HDBOWRITER | HDBOCREAT)){
127 fprintf(stderr, "open error\n");
133 bool hash_put(char *key, char *val)
137 if (!tcbdbput2(hdb, key, val)) {
138 ecode = tcbdbecode(hdb);
139 fprintf(stderr, "put error: %s\n", tcbdberrmsg(ecode));
145 char *hash_get(char *key, char *ret, int len)
147 return tcbdbget2(hdb, key);
150 void hash_free(char *ret)
163 /* traverse records */
164 iter = tcbdbcurnew(hdb);
168 bool hash_next(char *line, int len1, char *data, int len2)
170 bool ret = tcbdbcurrec(iter, kstr, dstr);
187 bool hash_init(int nbelt)
191 /* Create and initialize database object, open the database. */
192 if ((ret = db_create(&hdb, NULL, 0)) != 0) {
194 "db_create: %s\n", db_strerror(ret));
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");
203 if ((ret = hdb->set_cachesize(hdb, 0, 32 * 1024 * 1024, 0)) != 0) {
204 hdb->err(hdb, ret, "set_cachesize");
207 fprintf(stderr, "cache;%lli\n", (int64_t)32 * 1024 * 1024);
208 fprintf(stderr, "bucket;0\n");
209 fprintf(stderr, "option;0\n");
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");
217 memset(&dbkey, 0, sizeof(DBT));
218 memset(&dbdata, 0, sizeof(DBT));
223 bool hash_put(char *key, char *val)
227 dbkey.size = (u_int32_t)strlen(key);
229 dbdata.size = strlen(val)+1;
230 if ((ret = hdb->put(hdb, NULL, &dbkey, &dbdata, 0))) {
231 hdb->err(hdb, ret, "DBcursor->put");
242 char *hash_get(char *key, char *ret, int len)
244 /* Zero out the DBTs before using them. */
245 memset(&dbkey, 0, sizeof(DBT));
246 memset(&dbdata, 0, sizeof(DBT));
249 dbkey.ulen = strlen(key);
253 dbdata.flags = DB_DBT_USERMEM;
255 if (hdb->get(hdb, NULL, &dbkey, &dbdata, 0) == 0) {
256 return (char *)dbdata.data;
262 void hash_free(char *ret)
269 hdb->cursor(hdb, NULL, &cursorp, 0);
272 bool hash_next(char *line, int len1, char *data, int len2)
274 return cursorp->c_get(cursorp, &dbkey, &dbdata, DB_NEXT) == 0;
280 cursorp->c_close(cursorp);
288 int main(int argc, char **argv)
290 char *start_heap = (char *)sbrk(0);
297 int64_t ctime, ttime;
300 fprintf(stderr, "Usage: tt count file cache\n");
304 hash_init(atoll(argv[1]));
306 FILE *fp = fopen(argv[2], "r");
308 fprintf(stderr, "open %s file error\n", argv[2]);
312 sprintf(data, "AOOODS SDOOQSD A BBBBB AAAAA ZEZEZQS SQDSQDQ DSQ DAZEA ZEA S");
314 ctime = get_current_time();
315 fprintf(stderr, "elt;time\n");
316 while (fgets(line, sizeof(line), fp)) {
317 hash_put(line, data);
320 strcpy(save_key, mkey);
324 ttime= get_current_time();
325 fprintf(stderr, "%i;%i\n",
326 i/100000, (int) ((ttime - ctime)/1000));
329 ttime= get_current_time();
332 printf("loading %i file into hash database in %ims\n",
333 i, (ttime - ctime)/1000);
335 fprintf(stderr, "load;%i\n", (ttime - ctime)/1000);
336 fprintf(stderr, "elt;%i\n", i);
338 ////////////////////////////////////////////////////////////////
340 fp = fopen(argv[2], "r");
342 fprintf(stderr, "open %s file error\n", argv[2]);
346 ctime = get_current_time();
348 fprintf(stderr, "elt;time\n");
349 while (fgets(line, sizeof(line), fp)) {
350 p = hash_get(line, data, sizeof(data));
357 ttime= get_current_time();
358 fprintf(stderr, "%i;%i\n",
359 i/100000, (int) ((ttime - ctime)/1000));
365 ttime= get_current_time();
366 fprintf(stderr, "seen;%i\n", (ttime - ctime)/1000);
368 fprintf(stderr, "elt;time\n");
371 while(hash_next(line, sizeof(line), data, sizeof(data)))
374 ttime= get_current_time();
375 fprintf(stderr, "%i;%i\n",
376 i/100000, (int) ((ttime - ctime)/1000));
382 ttime = get_current_time();
383 fprintf(stderr, "walk;%i\n", (ttime - ctime)/1000);
385 fprintf(stderr, "heap;%lld\n", (long long)((char *)sbrk(0) - start_heap));