ndb.h 4.3 KB

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