ndbcache.c 2.6 KB

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