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){
130 t = ndbsearch(db->next, s, attr, val);
137 s->ptr = ndbhash(val, s->hf->hlen)*NDBPLEN;
138 p = hfread(s->hf, s->ptr+NDBHLEN, NDBPLEN);
140 t = _ndbcacheadd(db, s, attr, val, nil);
145 } else if(db->length > 128*1024){
146 print("Missing or out of date hash file %s.%s.\n", db->file, attr);
147 syslog(0, "ndb", "Missing or out of date hash file %s.%s.", db->file, attr);
149 /* advance search to next db file */
151 _ndbcacheadd(db, s, attr, val, nil);
154 t = ndbsearch(db->next, s, attr, val);
160 t = ndbsnext(s, attr, val);
161 _ndbcacheadd(db, s, attr, val, (t != nil && s->db == db)?t:nil);
163 ndbsetmalloctag(t, getcallerpc(&db));
168 match(Ndbtuple *t, char *attr, char *val)
172 for(nt = t; nt != nil; nt = nt->entry)
173 if(strcmp(attr, nt->attr) == 0
174 && strcmp(val, nt->val) == 0)
180 * return the next matching entry in the hash chain
183 ndbsnext(Ndbs *s, char *attr, char *val)
195 if(Bseek(&db->b, s->ptr, 0) < 0)
198 s->ptr = Boffset(&db->b);
201 if((s->t = match(t, attr, val)) != nil)
204 } else if(s->type == Cptr){
205 if(Bseek(&db->b, s->ptr, 0) < 0)
212 if((s->t = match(t, attr, val)) != nil)
215 } else if(s->type == Cptr1){
216 if(s->ptr & NDBCHAIN){ /* hash chain continuation */
218 p = hfread(s->hf, s->ptr+NDBHLEN, 2*NDBPLEN);
222 s->ptr1 = NDBGETP(p+NDBPLEN);
224 } else { /* end of hash chain */
225 if(Bseek(&db->b, s->ptr, 0) < 0)
231 if((s->t = match(t, attr, val)) != nil)
240 /* nothing left to search? */
245 /* advance search to next db file */
246 t = ndbsearch(db->next, s, attr, val);
248 ndbsetmalloctag(t, getcallerpc(&s));