ndbhash.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. return t;
  142. }
  143. static Ndbtuple*
  144. match(Ndbtuple *t, char *attr, char *val)
  145. {
  146. Ndbtuple *nt;
  147. for(nt = t; nt; nt = nt->entry)
  148. if(strcmp(attr, nt->attr) == 0
  149. && strcmp(val, nt->val) == 0)
  150. return nt;
  151. return 0;
  152. }
  153. /*
  154. * return the next matching entry in the hash chain
  155. */
  156. Ndbtuple*
  157. ndbsnext(Ndbs *s, char *attr, char *val)
  158. {
  159. Ndbtuple *t;
  160. Ndb *db;
  161. uchar *p;
  162. db = s->db;
  163. if(s->ptr == NDBNAP)
  164. goto nextfile;
  165. for(;;){
  166. if(s->type == Dptr){
  167. if(Bseek(&db->b, s->ptr, 0) < 0)
  168. break;
  169. t = ndbparse(db);
  170. s->ptr = Boffset(&db->b);
  171. if(t == 0)
  172. break;
  173. if(s->t = match(t, attr, val))
  174. return t;
  175. ndbfree(t);
  176. } else if(s->type == Cptr){
  177. if(Bseek(&db->b, s->ptr, 0) < 0)
  178. break;
  179. s->ptr = s->ptr1;
  180. s->type = Cptr1;
  181. t = ndbparse(db);
  182. if(t == 0)
  183. break;
  184. if(s->t = match(t, attr, val))
  185. return t;
  186. ndbfree(t);
  187. } else if(s->type == Cptr1){
  188. if(s->ptr & NDBCHAIN){ /* hash chain continuation */
  189. s->ptr &= ~NDBCHAIN;
  190. p = hfread(s->hf, s->ptr+NDBHLEN, 2*NDBPLEN);
  191. if(p == 0)
  192. break;
  193. s->ptr = NDBGETP(p);
  194. s->ptr1 = NDBGETP(p+NDBPLEN);
  195. s->type = Cptr;
  196. } else { /* end of hash chain */
  197. if(Bseek(&db->b, s->ptr, 0) < 0)
  198. break;
  199. s->ptr = NDBNAP;
  200. t = ndbparse(db);
  201. if(t == 0)
  202. break;
  203. if(s->t = match(t, attr, val))
  204. return t;
  205. ndbfree(t);
  206. break;
  207. }
  208. }
  209. }
  210. nextfile:
  211. /* nothing left to search? */
  212. s->ptr = NDBNAP;
  213. if(db->next == 0)
  214. return 0;
  215. /* advance search to next db file */
  216. return ndbsearch(db->next, s, attr, val);
  217. }