ndbcache.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 = malloc(sizeof *to_t);
  40. if(to_t == nil){
  41. ndbfree(first);
  42. return nil;
  43. }
  44. *to_t = *from_t;
  45. /* have s point to matching tuple */
  46. if(from_s->t == from_t)
  47. to_s->t = to_t;
  48. if(newline)
  49. line = to_t;
  50. else
  51. last->line = to_t;
  52. if(last != nil)
  53. last->entry = to_t;
  54. else {
  55. first = to_t;
  56. line = to_t;
  57. }
  58. to_t->entry = nil;
  59. to_t->line = line;
  60. last = to_t;
  61. newline = from_t->line != from_t->entry;
  62. }
  63. return first;
  64. }
  65. /*
  66. * if found, move to front
  67. */
  68. int
  69. _ndbcachesearch(Ndb *db, Ndbs *s, char *attr, char *val, Ndbtuple **t)
  70. {
  71. Ndbcache *c, **l;
  72. *t = nil;
  73. c = nil;
  74. for(l = &db->cache; *l != nil; l = &(*l)->next){
  75. c = *l;
  76. if(strcmp(c->attr, attr) == 0 && strcmp(c->val, val) == 0)
  77. break;
  78. }
  79. if(*l == nil)
  80. return -1;
  81. /* move to front */
  82. *l = c->next;
  83. c->next = db->cache;
  84. db->cache = c;
  85. *t = ndbcopy(db, c->t, &c->s, s);
  86. return 0;
  87. }
  88. Ndbtuple*
  89. _ndbcacheadd(Ndb *db, Ndbs *s, char *attr, char *val, Ndbtuple *t)
  90. {
  91. Ndbcache *c, **l;
  92. c = mallocz(sizeof *c, 1);
  93. if(c == nil)
  94. return nil;
  95. c->attr = strdup(attr);
  96. if(c->attr == nil)
  97. goto err;
  98. c->val = strdup(val);
  99. if(c->val == nil)
  100. goto err;
  101. c->t = ndbcopy(db, t, s, &c->s);
  102. if(c->t == nil && t != nil)
  103. goto err;
  104. /* add to front */
  105. c->next = db->cache;
  106. db->cache = c;
  107. /* trim list */
  108. if(db->ncache < Maxcached){
  109. db->ncache++;
  110. return t;
  111. }
  112. for(l = &db->cache; (*l)->next; l = &(*l)->next)
  113. ;
  114. c = *l;
  115. *l = nil;
  116. err:
  117. ndbcachefree(c);
  118. return t;
  119. }
  120. void
  121. _ndbcacheflush(Ndb *db)
  122. {
  123. Ndbcache *c;
  124. while(db->cache != nil){
  125. c = db->cache;
  126. db->cache = c->next;
  127. ndbcachefree(c);
  128. }
  129. db->ncache = 0;
  130. }