ndbcache.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. return first;
  59. }
  60. /*
  61. * if found, move to front
  62. */
  63. int
  64. _ndbcachesearch(Ndb *db, Ndbs *s, char *attr, char *val, Ndbtuple **t)
  65. {
  66. Ndbcache *c, **l;
  67. *t = nil;
  68. c = nil;
  69. for(l = &db->cache; *l != nil; l = &(*l)->next){
  70. c = *l;
  71. if(strcmp(c->attr, attr) == 0 && strcmp(c->val, val) == 0)
  72. break;
  73. }
  74. if(*l == nil)
  75. return -1;
  76. /* move to front */
  77. *l = c->next;
  78. c->next = db->cache;
  79. db->cache = c;
  80. *t = ndbcopy(db, c->t, &c->s, s);
  81. return 0;
  82. }
  83. Ndbtuple*
  84. _ndbcacheadd(Ndb *db, Ndbs *s, char *attr, char *val, Ndbtuple *t)
  85. {
  86. Ndbcache *c, **l;
  87. c = mallocz(sizeof *c, 1);
  88. if(c == nil)
  89. return nil;
  90. c->attr = strdup(attr);
  91. if(c->attr == nil)
  92. goto err;
  93. c->val = strdup(val);
  94. if(c->val == nil)
  95. goto err;
  96. c->t = ndbcopy(db, t, s, &c->s);
  97. if(c->t == nil && t != nil)
  98. goto err;
  99. /* add to front */
  100. c->next = db->cache;
  101. db->cache = c;
  102. /* trim list */
  103. if(db->ncache < Maxcached){
  104. db->ncache++;
  105. return t;
  106. }
  107. for(l = &db->cache; (*l)->next; l = &(*l)->next)
  108. ;
  109. c = *l;
  110. *l = nil;
  111. err:
  112. ndbcachefree(c);
  113. return t;
  114. }
  115. void
  116. _ndbcacheflush(Ndb *db)
  117. {
  118. Ndbcache *c;
  119. while(db->cache != nil){
  120. c = db->cache;
  121. db->cache = c->next;
  122. ndbcachefree(c);
  123. }
  124. db->ncache = 0;
  125. }