ndb.h 4.3 KB

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