8 Dptr, /* pointer to database file */
9 Cptr, /* pointer to first chain entry */
10 Cptr1, /* pointer to second chain entry */
14 * generate a hash value for an ascii string (val) given
15 * a hash table length (hlen)
18 ndbhash(char *vp, int hlen)
21 uchar *val = (uchar*)vp;
23 for(hash = 0; *val; val++)
24 hash = (hash*13) + *val-'a';
29 * read a hash file with buffering
32 hfread(Ndbhf *hf, long off, int len)
34 if(off < hf->off || off + len > hf->off + hf->len){
35 if(seek(hf->fd, off, 0) < 0
36 || (hf->len = read(hf->fd, hf->buf, sizeof(hf->buf))) < len){
42 return &hf->buf[off-hf->off];
46 * return an opened hash file if one exists for the
47 * attribute and if it is current vis-a-vis the data
51 hfopen(Ndb *db, char *attr)
54 char buf[sizeof(hf->attr)+sizeof(db->file)+2];
58 /* try opening the data base if it's closed */
59 if(db->mtime==0 && ndbreopen(db) < 0)
62 /* if the database has changed, throw out hash files and reopen db */
63 if((d = dirfstat(Bfildes(&db->b))) == nil || db->qid.path != d->qid.path
64 || db->qid.vers != d->qid.vers){
65 if(ndbreopen(db) < 0){
75 /* see if a hash file exists for this attribute */
76 for(hf = db->hf; hf; hf= hf->next){
77 if(strcmp(hf->attr, attr) == 0)
81 /* create a new one */
82 hf = (Ndbhf*)malloc(sizeof(Ndbhf));
85 memset(hf, 0, sizeof(Ndbhf));
87 /* compare it to the database file */
88 strncpy(hf->attr, attr, sizeof(hf->attr)-1);
89 sprint(buf, "%s.%s", db->file, hf->attr);
90 hf->fd = open(buf, OREAD);
94 p = hfread(hf, 0, 2*NDBULLEN);
96 hf->dbmtime = NDBGETUL(p);
97 hf->hlen = NDBGETUL(p+NDBULLEN);
98 if(hf->dbmtime == db->mtime){
112 * return the first matching entry
115 ndbsearch(Ndb *db, Ndbs *s, char *attr, char *val)
121 hf = hfopen(db, attr);
123 memset(s, 0, sizeof(*s));
124 if(_ndbcachesearch(db, s, attr, val, &t) == 0){
127 ndbsetmalloctag(t, getcallerpc(&db));
128 return t; /* answer from this file */
132 t = ndbsearch(db->next, s, attr, val);
133 ndbsetmalloctag(t, getcallerpc(&db));
140 s->ptr = ndbhash(val, s->hf->hlen)*NDBPLEN;
141 p = hfread(s->hf, s->ptr+NDBHLEN, NDBPLEN);
143 t = _ndbcacheadd(db, s, attr, val, nil);
144 ndbsetmalloctag(t, getcallerpc(&db));
149 } else if(db->length > 128*1024){
150 print("Missing or out of date hash file %s.%s.\n", db->file, attr);
151 syslog(0, "ndb", "Missing or out of date hash file %s.%s.", db->file, attr);
153 /* advance search to next db file */
155 _ndbcacheadd(db, s, attr, val, nil);
158 t = ndbsearch(db->next, s, attr, val);
159 ndbsetmalloctag(t, getcallerpc(&db));
165 t = ndbsnext(s, attr, val);
166 _ndbcacheadd(db, s, attr, val, (t != nil && s->db == db)?t:nil);
167 ndbsetmalloctag(t, getcallerpc(&db));
172 match(Ndbtuple *t, char *attr, char *val)
176 for(nt = t; nt; nt = nt->entry)
177 if(strcmp(attr, nt->attr) == 0
178 && strcmp(val, nt->val) == 0)
184 * return the next matching entry in the hash chain
187 ndbsnext(Ndbs *s, char *attr, char *val)
199 if(Bseek(&db->b, s->ptr, 0) < 0)
202 s->ptr = Boffset(&db->b);
205 if(s->t = match(t, attr, val)){
206 ndbsetmalloctag(t, getcallerpc(&s));
210 } else if(s->type == Cptr){
211 if(Bseek(&db->b, s->ptr, 0) < 0)
218 if(s->t = match(t, attr, val)){
219 ndbsetmalloctag(t, getcallerpc(&s));
223 } else if(s->type == Cptr1){
224 if(s->ptr & NDBCHAIN){ /* hash chain continuation */
226 p = hfread(s->hf, s->ptr+NDBHLEN, 2*NDBPLEN);
230 s->ptr1 = NDBGETP(p+NDBPLEN);
232 } else { /* end of hash chain */
233 if(Bseek(&db->b, s->ptr, 0) < 0)
239 if(s->t = match(t, attr, val)){
240 ndbsetmalloctag(t, getcallerpc(&s));
251 /* nothing left to search? */
256 /* advance search to next db file */
257 t = ndbsearch(db->next, s, attr, val);
258 ndbsetmalloctag(t, getcallerpc(&s));