ndb.h 4.2 KB

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