ndb.h 4.6 KB

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