ndb.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. #pragma src "/sys/src/libndb"
  10. #pragma lib "libndb.a"
  11. /*
  12. * this include file requires includes of <u.h> and <bio.h>
  13. */
  14. typedef struct Ndb Ndb;
  15. typedef struct Ndbtuple Ndbtuple;
  16. typedef struct Ndbhf Ndbhf;
  17. typedef struct Ndbs Ndbs;
  18. typedef struct Ndbcache Ndbcache;
  19. #pragma incomplete Ndbhf
  20. #pragma incomplete Ndbcache
  21. enum
  22. {
  23. Ndbalen= 32, /* max attribute length */
  24. Ndbvlen= 64, /* max value length */
  25. };
  26. /*
  27. * the database
  28. */
  29. struct Ndb
  30. {
  31. Ndb *next;
  32. Biobufhdr b; /* buffered input file */
  33. uint8_t buf[256]; /* and its buffer */
  34. uint32_t mtime; /* mtime of db file */
  35. Qid qid; /* qid of db file */
  36. char file[128];/* path name of db file */
  37. uint32_t length; /* length of db file */
  38. int nohash; /* don't look for hash files */
  39. Ndbhf *hf; /* open hash files */
  40. int ncache; /* size of tuple cache */
  41. Ndbcache *cache; /* cached entries */
  42. };
  43. /*
  44. * a parsed entry, doubly linked
  45. */
  46. struct Ndbtuple
  47. {
  48. char attr[Ndbalen]; /* attribute name */
  49. char *val; /* value(s) */
  50. Ndbtuple *entry; /* next tuple in this entry */
  51. Ndbtuple *line; /* next tuple on this line */
  52. uint32_t ptr; /* (for the application - starts 0) */
  53. char valbuf[Ndbvlen]; /* initial allocation for value */
  54. };
  55. /*
  56. * each hash file is of the form
  57. *
  58. * +---------------------------------------+
  59. * | mtime of db file (4 bytes) |
  60. * +---------------------------------------+
  61. * | size of table (in entries - 4 bytes) |
  62. * +---------------------------------------+
  63. * | hash table |
  64. * +---------------------------------------+
  65. * | hash chains |
  66. * +---------------------------------------+
  67. *
  68. * hash collisions are resolved using chained entries added to the
  69. * the end of the hash table.
  70. *
  71. * Hash entries are of the form
  72. *
  73. * +-------------------------------+
  74. * | offset (3 bytes) |
  75. * +-------------------------------+
  76. *
  77. * Chain entries are of the form
  78. *
  79. * +-------------------------------+
  80. * | offset1 (3 bytes) |
  81. * +-------------------------------+
  82. * | offset2 (3 bytes) |
  83. * +-------------------------------+
  84. *
  85. * The top bit of an offset set to 1 indicates a pointer to a hash chain entry.
  86. */
  87. #define NDBULLEN 4 /* unsigned long length in bytes */
  88. #define NDBPLEN 3 /* pointer length in bytes */
  89. #define NDBHLEN (2*NDBULLEN) /* hash file header length in bytes */
  90. /*
  91. * finger pointing to current point in a search
  92. */
  93. struct Ndbs
  94. {
  95. Ndb *db; /* data base file being searched */
  96. Ndbhf *hf; /* hash file being searched */
  97. int type;
  98. uint32_t ptr; /* current pointer */
  99. uint32_t ptr1; /* next pointer */
  100. Ndbtuple *t; /* last attribute value pair found */
  101. };
  102. /*
  103. * bit defs for pointers in hash files
  104. */
  105. #define NDBSPEC (1<<23)
  106. #define NDBCHAIN NDBSPEC /* points to a collision chain */
  107. #define NDBNAP (NDBSPEC|1) /* not a pointer */
  108. /*
  109. * macros for packing and unpacking pointers
  110. */
  111. #define NDBPUTP(v,a) { (a)[0] = v; (a)[1] = (v)>>8; (a)[2] = (v)>>16; }
  112. #define NDBGETP(a) ((a)[0] | ((a)[1]<<8) | ((a)[2]<<16))
  113. /*
  114. * macros for packing and unpacking unsigned longs
  115. */
  116. #define NDBPUTUL(v,a) { (a)[0] = v; (a)[1] = (v)>>8; (a)[2] = (v)>>16; (a)[3] = (v)>>24; }
  117. #define NDBGETUL(a) ((a)[0] | ((a)[1]<<8) | ((a)[2]<<16) | ((a)[3]<<24))
  118. #define NDB_IPlen 16
  119. Ndbtuple* csgetval(char*, char*, char*, char*, char*);
  120. char* csgetvalue(char*, char*, char*, char*,
  121. Ndbtuple**);
  122. Ndbtuple* csipinfo(char*, char*, char*, char**, int);
  123. Ndbtuple* dnsquery(char*, char*, char*);
  124. char* ipattr(char*);
  125. Ndb* ndbcat(Ndb*, Ndb*);
  126. int ndbchanged(Ndb*);
  127. void ndbclose(Ndb*);
  128. Ndbtuple* ndbconcatenate(Ndbtuple*, Ndbtuple*);
  129. Ndbtuple* ndbdiscard(Ndbtuple*, Ndbtuple*);
  130. void ndbfree(Ndbtuple*);
  131. Ndbtuple* ndbgetipaddr(Ndb*, char*);
  132. Ndbtuple* ndbgetval(Ndb*, Ndbs*, char*, char*, char*, char*);
  133. char* ndbgetvalue(Ndb*, Ndbs*, char*, char*, char*,
  134. Ndbtuple**);
  135. Ndbtuple* ndbfindattr(Ndbtuple*, Ndbtuple*, char*);
  136. uint32_t ndbhash(char*, int);
  137. Ndbtuple* ndbipinfo(Ndb*, char*, char*, char**, int);
  138. Ndbtuple* ndblookval(Ndbtuple*, Ndbtuple*, char*, char*);
  139. Ndbtuple* ndbnew(char*, char*);
  140. Ndb* ndbopen(char*);
  141. Ndbtuple* ndbparse(Ndb*);
  142. int ndbreopen(Ndb*);
  143. Ndbtuple* ndbreorder(Ndbtuple*, Ndbtuple*);
  144. Ndbtuple* ndbsearch(Ndb*, Ndbs*, char*, char*);
  145. void ndbsetval(Ndbtuple*, char*, int);
  146. Ndbtuple* ndbsnext(Ndbs*, char*, char*);
  147. Ndbtuple* ndbsubstitute(Ndbtuple*, Ndbtuple*, Ndbtuple*);
  148. void ndbsetmalloctag(Ndbtuple*, uintptr);