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
5 ./tt $(wc -l src.txt2) 0
21 #define NITEMS 5000000
30 int64_t get_current_time()
33 if (gettimeofday(&tv, NULL) != 0) {
34 tv.tv_sec = (long)time(NULL); /* fall back to old method */
37 return (int64_t)tv.tv_sec * 1000000 + (int64_t)tv.tv_usec;
40 typedef struct PrivateCurFile {
41 char *fname; /* not stored with tchdb mode */
47 int main(int argc, char **argv)
58 int64_t ctime, ttime;;
62 fprintf(stderr, "Usage: tt count file cache %i\n");
70 snprintf(result, sizeof(result), "result.%i.csv", getpid());
71 res = fopen(result, "w");
73 /* create the object */
76 if (atoi(argv[3]) > 0) {
77 tchdbsetcache(hdb, atoi(argv[3]));
79 fprintf(res, "cache;%i\n", atoi(argv[3]));
82 * apow : 128 (size of stat hash field)
84 int opt=HDBTLARGE | HDBTTCBS;
85 tchdbtune(hdb, atoll(argv[1]), 7, 16, opt);
86 fprintf(res, "bucket;%lli\n", atoll(argv[1]));
87 fprintf(res, "compress;%i\n", opt);
89 /* open the database */
90 if(!tchdbopen(hdb, "casket.hdb", HDBOWRITER | HDBOCREAT)){
91 ecode = tchdbecode(hdb);
92 fprintf(stderr, "open error: %s\n", tchdberrmsg(ecode));
95 ctime = get_current_time();
97 /* fill hash with real data (find / > src.txt) */
98 fp = fopen(argv[2], "r");
100 fprintf(stderr, "open %s file error\n", argv[2]);
103 while (fgets(line, sizeof(line), fp)) {
104 if (!tchdbputasync(hdb, line, strlen(line)+1, &elt, sizeof(elt))) {
105 ecode = tchdbecode(hdb);
106 fprintf(stderr, "put error: %s\n", tchdberrmsg(ecode));
109 strcpy(save_key, line);
114 ttime= get_current_time();
115 fprintf(res, "nbelt;%u\n", i);
117 fprintf(stderr, "loading %i file into hash database in %ims\n",
118 i, (ttime - ctime)/1000);
119 fprintf(res, "load;%i\n", (ttime - ctime)/1000);
122 /* retrieve records */
123 value = tchdbget3(hdb, save_key, strlen(save_key)+1, &elt, sizeof(elt));
125 ecode = tchdbecode(hdb);
126 fprintf(stderr, "get error: %s\n", tchdberrmsg(ecode));
129 /* retrieve all records and mark them as seen */
131 fp = fopen(argv[2], "r");
133 fprintf(stderr, "open %s file error\n", argv[2]);
136 while (fgets(line, sizeof(line), fp)) {
138 value = tchdbget3(hdb, line, strlen(line)+1, &elt, sizeof(elt));
141 if (!tchdbputasync(hdb, line, strlen(line)+1, &elt, sizeof(elt))) {
142 ecode = tchdbecode(hdb);
143 fprintf(stderr, "put error: %s\n", tchdberrmsg(ecode));
146 fprintf(stderr, "can't find %s in hash\n", line);
152 ctime = get_current_time();
153 fprintf(stderr, "marking as seen in %ims\n", (ctime - ttime)/1000);
154 fprintf(res, "seen;%i\n", (ctime - ttime)/1000);
156 /* traverse records */
158 while((key = tchdbiternext2(hdb)) != NULL){
159 value = tchdbget3(hdb, key, strlen(key)+1, &elt, sizeof(elt));
161 elt.seen=1; // check seen element
163 fprintf(stderr, "can't find %s in hash\n", line);
167 ttime = get_current_time();
168 fprintf(stderr, "checking not seen in %ims\n", (ttime - ctime)/1000);
169 fprintf(res, "walk;%i\n", (ttime - ctime)/1000);
171 /* close the database */
172 if(!tchdbclose(hdb)){
173 ecode = tchdbecode(hdb);
174 fprintf(stderr, "close error: %s\n", tchdberrmsg(ecode));
177 /* delete the object */
180 stat("casket.hdb", &statp);
181 fprintf(res, "size;%lli\n", statp.st_size);
182 unlink("casket.hdb");