ndbhash.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. ndbsetmalloctag(t, getcallerpc(&db));
  113. return t; /* answer from this file */
  114. }
  115. if(db->next == nil)
  116. return nil;
  117. t = ndbsearch(db->next, s, attr, val);
  118. ndbsetmalloctag(t, getcallerpc(&db));
  119. return t;
  120. }
  121. s->db = db;
  122. s->hf = hf;
  123. if(s->hf){
  124. s->ptr = ndbhash(val, s->hf->hlen)*NDBPLEN;
  125. p = hfread(s->hf, s->ptr+NDBHLEN, NDBPLEN);
  126. if(p == 0){
  127. t = _ndbcacheadd(db, s, attr, val, nil);
  128. ndbsetmalloctag(t, getcallerpc(&db));
  129. return t;
  130. }
  131. s->ptr = NDBGETP(p);
  132. s->type = Cptr1;
  133. } else if(db->length > 128*1024){
  134. print("Missing or out of date hash file %s.%s.\n", db->file, attr);
  135. syslog(0, "ndb", "Missing or out of date hash file %s.%s.", db->file, attr);
  136. /* advance search to next db file */
  137. s->ptr = NDBNAP;
  138. _ndbcacheadd(db, s, attr, val, nil);
  139. if(db->next == 0)
  140. return nil;
  141. t = ndbsearch(db->next, s, attr, val);
  142. ndbsetmalloctag(t, getcallerpc(&db));
  143. return t;
  144. } else {
  145. s->ptr = 0;
  146. s->type = Dptr;
  147. }
  148. t = ndbsnext(s, attr, val);
  149. _ndbcacheadd(db, s, attr, val, (t != nil && s->db == db)?t:nil);
  150. ndbsetmalloctag(t, getcallerpc(&db));
  151. return t;
  152. }
  153. static Ndbtuple*
  154. match(Ndbtuple *t, char *attr, char *val)
  155. {
  156. Ndbtuple *nt;
  157. for(nt = t; nt; nt = nt->entry)
  158. if(strcmp(attr, nt->attr) == 0
  159. && strcmp(val, nt->val) == 0)
  160. return nt;
  161. return 0;
  162. }
  163. /*
  164. * return the next matching entry in the hash chain
  165. */
  166. Ndbtuple*
  167. ndbsnext(Ndbs *s, char *attr, char *val)
  168. {
  169. Ndbtuple *t;
  170. Ndb *db;
  171. uchar *p;
  172. db = s->db;
  173. if(s->ptr == NDBNAP)
  174. goto nextfile;
  175. for(;;){
  176. if(s->type == Dptr){
  177. if(Bseek(&db->b, s->ptr, 0) < 0)
  178. break;
  179. t = ndbparse(db);
  180. s->ptr = Boffset(&db->b);
  181. if(t == 0)
  182. break;
  183. if(s->t = match(t, attr, val)){
  184. ndbsetmalloctag(t, getcallerpc(&s));
  185. return t;
  186. }
  187. ndbfree(t);
  188. } else if(s->type == Cptr){
  189. if(Bseek(&db->b, s->ptr, 0) < 0)
  190. break;
  191. s->ptr = s->ptr1;
  192. s->type = Cptr1;
  193. t = ndbparse(db);
  194. if(t == 0)
  195. break;
  196. if(s->t = match(t, attr, val)){
  197. ndbsetmalloctag(t, getcallerpc(&s));
  198. return t;
  199. }
  200. ndbfree(t);
  201. } else if(s->type == Cptr1){
  202. if(s->ptr & NDBCHAIN){ /* hash chain continuation */
  203. s->ptr &= ~NDBCHAIN;
  204. p = hfread(s->hf, s->ptr+NDBHLEN, 2*NDBPLEN);
  205. if(p == 0)
  206. break;
  207. s->ptr = NDBGETP(p);
  208. s->ptr1 = NDBGETP(p+NDBPLEN);
  209. s->type = Cptr;
  210. } else { /* end of hash chain */
  211. if(Bseek(&db->b, s->ptr, 0) < 0)
  212. break;
  213. s->ptr = NDBNAP;
  214. t = ndbparse(db);
  215. if(t == 0)
  216. break;
  217. if(s->t = match(t, attr, val)){
  218. ndbsetmalloctag(t, getcallerpc(&s));
  219. return t;
  220. }
  221. ndbfree(t);
  222. break;
  223. }
  224. }
  225. }
  226. nextfile:
  227. /* nothing left to search? */
  228. s->ptr = NDBNAP;
  229. if(db->next == 0)
  230. return 0;
  231. /* advance search to next db file */
  232. t = ndbsearch(db->next, s, attr, val);
  233. ndbsetmalloctag(t, getcallerpc(&s));
  234. return t;
  235. }