ndb.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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[Ndbvlen]; /* 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. };
  44. /*
  45. * each hash file is of the form
  46. *
  47. * +---------------------------------------+
  48. * | mtime of db file (4 bytes) |
  49. * +---------------------------------------+
  50. * | size of table (in entries - 4 bytes) |
  51. * +---------------------------------------+
  52. * | hash table |
  53. * +---------------------------------------+
  54. * | hash chains |
  55. * +---------------------------------------+
  56. *
  57. * hash collisions are resolved using chained entries added to the
  58. * the end of the hash table.
  59. *
  60. * Hash entries are of the form
  61. *
  62. * +-------------------------------+
  63. * | offset (3 bytes) |
  64. * +-------------------------------+
  65. *
  66. * Chain entries are of the form
  67. *
  68. * +-------------------------------+
  69. * | offset1 (3 bytes) |
  70. * +-------------------------------+
  71. * | offset2 (3 bytes) |
  72. * +-------------------------------+
  73. *
  74. * The top bit of an offset set to 1 indicates a pointer to a hash chain entry.
  75. */
  76. #define NDBULLEN 4 /* unsigned long length in bytes */
  77. #define NDBPLEN 3 /* pointer length in bytes */
  78. #define NDBHLEN (2*NDBULLEN) /* hash file header length in bytes */
  79. /*
  80. * finger pointing to current point in a search
  81. */
  82. struct Ndbs
  83. {
  84. Ndb *db; /* data base file being searched */
  85. Ndbhf *hf; /* hash file being searched */
  86. int type;
  87. ulong ptr; /* current pointer */
  88. ulong ptr1; /* next pointer */
  89. Ndbtuple *t; /* last attribute value pair found */
  90. };
  91. /*
  92. * bit defs for pointers in hash files
  93. */
  94. #define NDBSPEC (1<<23)
  95. #define NDBCHAIN NDBSPEC /* points to a collision chain */
  96. #define NDBNAP (NDBSPEC|1) /* not a pointer */
  97. /*
  98. * macros for packing and unpacking pointers
  99. */
  100. #define NDBPUTP(v,a) { (a)[0] = v; (a)[1] = (v)>>8; (a)[2] = (v)>>16; }
  101. #define NDBGETP(a) ((a)[0] | ((a)[1]<<8) | ((a)[2]<<16))
  102. /*
  103. * macros for packing and unpacking unsigned longs
  104. */
  105. #define NDBPUTUL(v,a) { (a)[0] = v; (a)[1] = (v)>>8; (a)[2] = (v)>>16; (a)[3] = (v)>>24; }
  106. #define NDBGETUL(a) ((a)[0] | ((a)[1]<<8) | ((a)[2]<<16) | ((a)[3]<<24))
  107. #define NDB_IPlen 16
  108. ulong ndbhash(char*, int);
  109. Ndbtuple* ndbparse(Ndb*);
  110. void ndbfree(Ndbtuple*);
  111. Ndb* ndbopen(char*);
  112. Ndb* ndbcat(Ndb*, Ndb*);
  113. int ndbreopen(Ndb*);
  114. void ndbclose(Ndb*);
  115. Ndbtuple* ndblookval(Ndbtuple*, Ndbtuple*, char*, char*);
  116. long ndbseek(Ndb*, long);
  117. Ndbtuple* ndbsearch(Ndb*, Ndbs*, char*, char*);
  118. Ndbtuple* ndbsnext(Ndbs*, char*, char*);
  119. Ndbtuple* ndbgetval(Ndb*, Ndbs*, char*, char*, char*, char*);
  120. Ndbtuple* csgetval(char*, char*, char*, char*, char*);
  121. char* ipattr(char*);
  122. Ndbtuple* ndbipinfo(Ndb*, char*, char*, char**, int);
  123. Ndbtuple* csipinfo(char*, char*, char*, char**, int);
  124. Ndbtuple* dnsquery(char*, char*, char*);
  125. int ndbchanged(Ndb *db);