ndbcache.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <bio.h>
  4. #include <ndb.h>
  5. struct Ndbcache
  6. {
  7. Ndbcache *next;
  8. char *attr;
  9. char *val;
  10. Ndbs s;
  11. Ndbtuple *t;
  12. };
  13. enum
  14. {
  15. Maxcached= 128,
  16. };
  17. static void
  18. ndbcachefree(Ndbcache *c)
  19. {
  20. free(c->val);
  21. free(c->attr);
  22. if(c->t)
  23. ndbfree(c->t);
  24. free(c);
  25. }
  26. static Ndbtuple*
  27. ndbcopy(Ndb *db, Ndbtuple *from_t, Ndbs *from_s, Ndbs *to_s)
  28. {
  29. Ndbtuple *first, *to_t, *last, *line;
  30. int newline;
  31. *to_s = *from_s;
  32. to_s->t = nil;
  33. to_s->db = db;
  34. newline = 1;
  35. last = nil;
  36. first = nil;
  37. line = nil;
  38. for(; from_t != nil; from_t = from_t->entry){
  39. to_t = ndbnew(from_t->attr, from_t->val);
  40. /* have s point to matching tuple */
  41. if(from_s->t == from_t)
  42. to_s->t = to_t;
  43. if(newline)
  44. line = to_t;
  45. else
  46. last->line = to_t;
  47. if(last != nil)
  48. last->entry = to_t;
  49. else {
  50. first = to_t;
  51. line = to_t;
  52. }
  53. to_t->entry = nil;
  54. to_t->line = line;
  55. last = to_t;
  56. newline = from_t->line != from_t->entry;
  57. }
  58. ndbsetmalloctag(first, getcallerpc(&db));
  59. return first;
  60. }
  61. /*
  62. * if found, move to front
  63. */
  64. int
  65. _ndbcachesearch(Ndb *db, Ndbs *s, char *attr, char *val, Ndbtuple **t)
  66. {
  67. Ndbcache *c, **l;
  68. *t = nil;
  69. c = nil;
  70. for(l = &db->cache; *l != nil; l = &(*l)->next){
  71. c = *l;
  72. if(strcmp(c->attr, attr) == 0 && strcmp(c->val, val) == 0)
  73. break;
  74. }
  75. if(*l == nil)
  76. return -1;
  77. /* move to front */
  78. *l = c->next;
  79. c->next = db->cache;
  80. db->cache = c;
  81. *t = ndbcopy(db, c->t, &c->s, s);
  82. return 0;
  83. }
  84. Ndbtuple*
  85. _ndbcacheadd(Ndb *db, Ndbs *s, char *attr, char *val, Ndbtuple *t)
  86. {
  87. Ndbcache *c, **l;
  88. c = mallocz(sizeof *c, 1);
  89. if(c == nil)
  90. return nil;
  91. c->attr = strdup(attr);
  92. if(c->attr == nil)
  93. goto err;
  94. c->val = strdup(val);
  95. if(c->val == nil)
  96. goto err;
  97. c->t = ndbcopy(db, t, s, &c->s);
  98. if(c->t == nil && t != nil)
  99. goto err;
  100. /* add to front */
  101. c->next = db->cache;
  102. db->cache = c;
  103. /* trim list */
  104. if(db->ncache < Maxcached){
  105. db->ncache++;
  106. return t;
  107. }
  108. for(l = &db->cache; (*l)->next; l = &(*l)->next)
  109. ;
  110. c = *l;
  111. *l = nil;
  112. err:
  113. ndbcachefree(c);
  114. ndbsetmalloctag(t, getcallerpc(&db));
  115. return t;
  116. }
  117. void
  118. _ndbcacheflush(Ndb *db)
  119. {
  120. Ndbcache *c;
  121. while(db->cache != nil){
  122. c = db->cache;
  123. db->cache = c->next;
  124. ndbcachefree(c);
  125. }
  126. db->ncache = 0;
  127. }