ndbhash.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include <u.h>
  10. #include <libc.h>
  11. #include <bio.h>
  12. #include "ndb.h"
  13. #include "ndbhf.h"
  14. enum {
  15. Dptr, /* pointer to database file */
  16. Cptr, /* pointer to first chain entry */
  17. Cptr1, /* pointer to second chain entry */
  18. };
  19. /*
  20. * generate a hash value for an ascii string (val) given
  21. * a hash table length (hlen)
  22. */
  23. uint32_t
  24. ndbhash(char *vp, int hlen)
  25. {
  26. uint32_t hash;
  27. uint8_t *val = (uint8_t*)vp;
  28. for(hash = 0; *val; val++)
  29. hash = (hash*13) + *val-'a';
  30. return hash % hlen;
  31. }
  32. /*
  33. * read a hash file with buffering
  34. */
  35. static uint8_t*
  36. hfread(Ndbhf *hf, int32_t off, int len)
  37. {
  38. if(off < hf->off || off + len > hf->off + hf->len){
  39. if(seek(hf->fd, off, 0) < 0
  40. || (hf->len = read(hf->fd, hf->buf, sizeof(hf->buf))) < len){
  41. hf->off = -1;
  42. return 0;
  43. }
  44. hf->off = off;
  45. }
  46. return &hf->buf[off-hf->off];
  47. }
  48. /*
  49. * return an opened hash file if one exists for the
  50. * attribute and if it is current vis-a-vis the data
  51. * base file
  52. */
  53. static Ndbhf*
  54. hfopen(Ndb *db, char *attr)
  55. {
  56. Ndbhf *hf;
  57. char buf[sizeof(hf->attr)+sizeof(db->file)+2];
  58. uint8_t *p;
  59. Dir *d;
  60. /* try opening the data base if it's closed */
  61. if(db->mtime==0 && ndbreopen(db) < 0)
  62. return 0;
  63. /* if the database has changed, throw out hash files and reopen db */
  64. if((d = dirfstat(Bfildes(&db->b))) == nil || db->qid.path != d->qid.path
  65. || db->qid.vers != d->qid.vers){
  66. if(ndbreopen(db) < 0){
  67. free(d);
  68. return 0;
  69. }
  70. }
  71. free(d);
  72. if(db->nohash)
  73. return 0;
  74. /* see if a hash file exists for this attribute */
  75. for(hf = db->hf; hf; hf= hf->next){
  76. if(strcmp(hf->attr, attr) == 0)
  77. return hf;
  78. }
  79. /* create a new one */
  80. hf = (Ndbhf*)malloc(sizeof(Ndbhf));
  81. if(hf == 0)
  82. return 0;
  83. memset(hf, 0, sizeof(Ndbhf));
  84. /* compare it to the database file */
  85. strncpy(hf->attr, attr, sizeof(hf->attr)-1);
  86. sprint(buf, "%s.%s", db->file, hf->attr);
  87. hf->fd = open(buf, OREAD);
  88. if(hf->fd >= 0){
  89. hf->len = 0;
  90. hf->off = 0;
  91. p = hfread(hf, 0, 2*NDBULLEN);
  92. if(p){
  93. hf->dbmtime = NDBGETUL(p);
  94. hf->hlen = NDBGETUL(p+NDBULLEN);
  95. if(hf->dbmtime == db->mtime){
  96. hf->next = db->hf;
  97. db->hf = hf;
  98. return hf;
  99. }
  100. }
  101. close(hf->fd);
  102. }
  103. free(hf);
  104. return 0;
  105. }
  106. /*
  107. * return the first matching entry
  108. */
  109. Ndbtuple*
  110. ndbsearch(Ndb *db, Ndbs *s, char *attr, char *val)
  111. {
  112. uint8_t *p;
  113. Ndbtuple *t;
  114. Ndbhf *hf;
  115. hf = hfopen(db, attr);
  116. memset(s, 0, sizeof(*s));
  117. if(_ndbcachesearch(db, s, attr, val, &t) == 0){
  118. /* found in cache */
  119. if(t != nil){
  120. ndbsetmalloctag(t, getcallerpc());
  121. return t; /* answer from this file */
  122. }
  123. if(db->next == nil)
  124. return nil;
  125. t = ndbsearch(db->next, s, attr, val);
  126. ndbsetmalloctag(t, getcallerpc());
  127. return t;
  128. }
  129. s->db = db;
  130. s->hf = hf;
  131. if(s->hf){
  132. s->ptr = ndbhash(val, s->hf->hlen)*NDBPLEN;
  133. p = hfread(s->hf, s->ptr+NDBHLEN, NDBPLEN);
  134. if(p == 0){
  135. t = _ndbcacheadd(db, s, attr, val, nil);
  136. ndbsetmalloctag(t, getcallerpc());
  137. return t;
  138. }
  139. s->ptr = NDBGETP(p);
  140. s->type = Cptr1;
  141. } else if(db->length > 128*1024){
  142. print("Missing or out of date hash file %s.%s.\n", db->file, attr);
  143. syslog(0, "ndb", "Missing or out of date hash file %s.%s.", db->file, attr);
  144. /* advance search to next db file */
  145. s->ptr = NDBNAP;
  146. _ndbcacheadd(db, s, attr, val, nil);
  147. if(db->next == 0)
  148. return nil;
  149. t = ndbsearch(db->next, s, attr, val);
  150. ndbsetmalloctag(t, getcallerpc());
  151. return t;
  152. } else {
  153. s->ptr = 0;
  154. s->type = Dptr;
  155. }
  156. t = ndbsnext(s, attr, val);
  157. _ndbcacheadd(db, s, attr, val, (t != nil && s->db == db)?t:nil);
  158. ndbsetmalloctag(t, getcallerpc());
  159. return t;
  160. }
  161. static Ndbtuple*
  162. match(Ndbtuple *t, char *attr, char *val)
  163. {
  164. Ndbtuple *nt;
  165. for(nt = t; nt; nt = nt->entry)
  166. if(strcmp(attr, nt->attr) == 0
  167. && strcmp(val, nt->val) == 0)
  168. return nt;
  169. return 0;
  170. }
  171. /*
  172. * return the next matching entry in the hash chain
  173. */
  174. Ndbtuple*
  175. ndbsnext(Ndbs *s, char *attr, char *val)
  176. {
  177. Ndbtuple *t;
  178. Ndb *db;
  179. uint8_t *p;
  180. db = s->db;
  181. if(s->ptr == NDBNAP)
  182. goto nextfile;
  183. for(;;){
  184. if(s->type == Dptr){
  185. if(Bseek(&db->b, s->ptr, 0) < 0)
  186. break;
  187. t = ndbparse(db);
  188. s->ptr = Boffset(&db->b);
  189. if(t == 0)
  190. break;
  191. if((s->t = match(t, attr, val)) != 0){
  192. ndbsetmalloctag(t, getcallerpc());
  193. return t;
  194. }
  195. ndbfree(t);
  196. } else if(s->type == Cptr){
  197. if(Bseek(&db->b, s->ptr, 0) < 0)
  198. break;
  199. s->ptr = s->ptr1;
  200. s->type = Cptr1;
  201. t = ndbparse(db);
  202. if(t == 0)
  203. break;
  204. if((s->t = match(t, attr, val)) != 0){
  205. ndbsetmalloctag(t, getcallerpc());
  206. return t;
  207. }
  208. ndbfree(t);
  209. } else if(s->type == Cptr1){
  210. if(s->ptr & NDBCHAIN){ /* hash chain continuation */
  211. s->ptr &= ~NDBCHAIN;
  212. p = hfread(s->hf, s->ptr+NDBHLEN, 2*NDBPLEN);
  213. if(p == 0)
  214. break;
  215. s->ptr = NDBGETP(p);
  216. s->ptr1 = NDBGETP(p+NDBPLEN);
  217. s->type = Cptr;
  218. } else { /* end of hash chain */
  219. if(Bseek(&db->b, s->ptr, 0) < 0)
  220. break;
  221. s->ptr = NDBNAP;
  222. t = ndbparse(db);
  223. if(t == 0)
  224. break;
  225. if((s->t = match(t, attr, val)) != 0){
  226. ndbsetmalloctag(t, getcallerpc());
  227. return t;
  228. }
  229. ndbfree(t);
  230. break;
  231. }
  232. }
  233. }
  234. nextfile:
  235. /* nothing left to search? */
  236. s->ptr = NDBNAP;
  237. if(db->next == 0)
  238. return 0;
  239. /* advance search to next db file */
  240. t = ndbsearch(db->next, s, attr, val);
  241. ndbsetmalloctag(t, getcallerpc());
  242. return t;
  243. }