ndbhash.c 4.9 KB

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