Blob


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