7 * make the hash table completely in memory and then write as a file
18 static char buf[ERRMAX];
20 errstr(buf, sizeof buf);
25 enter(char *val, ulong dboff)
31 h = ndbhash(val, hlen);
41 /* walk the chain to the last entry */
44 last = &ht[ptr+NDBPLEN];
50 if(!(ptr & NDBCHAIN)){
51 NDBPUTP(nextchain|NDBCHAIN, last);
56 NDBPUTP(nextchain|NDBCHAIN, last);
58 /* add a chained entry */
59 NDBPUTP(ptr, &ht[nextchain]);
60 NDBPUTP(dboff, &ht[nextchain + NDBPLEN]);
61 nextchain += 2*NDBPLEN;
67 main(int argc, char **argv)
79 fprint(2, "mkhash: usage file attribute\n");
82 db = ndbopen(argv[1]);
84 fprint(2, "mkhash: can't open %s\n", argv[1]);
88 /* try a bigger than normal buffer */
89 Binits(&db->b, Bfildes(&db->b), OREAD, nbuf, sizeof(nbuf));
91 /* count entries to calculate hash size */
94 while(nt = ndbparse(db)){
95 for(t = nt; t; t = t->entry){
96 if(strcmp(t->attr, argv[2]) == 0)
102 /* allocate an array large enough for worst case */
104 n = hlen*NDBPLEN + hlen*2*NDBPLEN;
107 fprint(2, "mkhash: not enough memory\n");
110 for(p = ht; p < &ht[n]; p += NDBPLEN)
112 nextchain = hlen*NDBPLEN;
114 /* create the in core hash table */
117 while(nt = ndbparse(db)){
118 for(t = nt; t; t = t->entry){
119 if(strcmp(t->attr, argv[2]) == 0)
123 off = Boffset(&db->b);
126 /* create the hash file */
127 snprint(file, sizeof(file), "%s.%s", argv[1], argv[2]);
128 fd = create(file, ORDWR, 0664);
130 fprint(2, "mkhash: can't create %s\n", file);
133 NDBPUTUL(db->mtime, buf);
134 NDBPUTUL(hlen, buf+NDBULLEN);
135 if(write(fd, buf, NDBHLEN) != NDBHLEN){
136 fprint(2, "mkhash: writing %s\n", file);
139 if(write(fd, ht, nextchain) != nextchain){
140 fprint(2, "mkhash: writing %s\n", file);
145 /* make sure file didn't change while we were making the hash */
146 d = dirstat(argv[1]);
147 if(d == nil || d->qid.path != db->qid.path
148 || d->qid.vers != db->qid.vers){
149 fprint(2, "mkhash: %s changed underfoot\n", argv[1]);