blob: deb4968bc1679b52abdaacf81218f6e53af1c286 [file] [log] [blame]
rscad017cf2005-02-11 19:40:20 +00001/*
2#pragma src "/sys/src/libndb"
3#pragma lib "libndb.a"
4*/
5AUTOLIB(ndb)
6
7/*
8 * this include file requires includes of <u.h> and <bio.h>
9 */
10typedef struct Ndb Ndb;
11typedef struct Ndbtuple Ndbtuple;
12typedef struct Ndbhf Ndbhf;
13typedef struct Ndbs Ndbs;
14typedef struct Ndbcache Ndbcache;
15
16/*
17#pragma incomplete Ndbhf
18#pragma incomplete Ndbcache
19*/
20
21enum
22{
23 Ndbalen= 32, /* max attribute length */
24 Ndbvlen= 64, /* max value length */
25};
26
27/*
28 * the database
29 */
30struct Ndb
31{
32 Ndb *next;
33
34 Biobuf b; /* buffered input file */
35
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 */
40
41 int nohash; /* don't look for hash files */
42 Ndbhf *hf; /* open hash files */
43
44 int ncache; /* size of tuple cache */
45 Ndbcache *cache; /* cached entries */
46};
47
48/*
49 * a parsed entry, doubly linked
50 */
51struct 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};
60
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 */
96
97/*
98 * finger pointing to current point in a search
99 */
100struct Ndbs
101{
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};
109
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 */
116
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))
122
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))
128
129#define NDB_IPlen 16
130
131Ndbtuple* csgetval(char*, char*, char*, char*, char*);
132char* csgetvalue(char*, char*, char*, char*, Ndbtuple**);
133Ndbtuple* csipinfo(char*, char*, char*, char**, int);
134Ndbtuple* dnsquery(char*, char*, char*);
135char* ipattr(char*);
136Ndb* ndbcat(Ndb*, Ndb*);
137int ndbchanged(Ndb*);
138void ndbclose(Ndb*);
139Ndbtuple* ndbconcatenate(Ndbtuple*, Ndbtuple*);
140Ndbtuple* ndbdiscard(Ndbtuple*, Ndbtuple*);
141void ndbfree(Ndbtuple*);
142Ndbtuple* ndbgetipaddr(Ndb*, char*);
143Ndbtuple* ndbgetval(Ndb*, Ndbs*, char*, char*, char*, char*);
144char* ndbgetvalue(Ndb*, Ndbs*, char*, char*, char*, Ndbtuple**);
145Ndbtuple* ndbfindattr(Ndbtuple*, Ndbtuple*, char*);
146ulong ndbhash(char*, int);
147Ndbtuple* ndbipinfo(Ndb*, char*, char*, char**, int);
148Ndbtuple* ndblookval(Ndbtuple*, Ndbtuple*, char*, char*);
149Ndbtuple* ndbnew(char*, char*);
150Ndb* ndbopen(char*);
151Ndbtuple* ndbparse(Ndb*);
152int ndbreopen(Ndb*);
153Ndbtuple* ndbreorder(Ndbtuple*, Ndbtuple*);
154Ndbtuple* ndbsearch(Ndb*, Ndbs*, char*, char*);
155long ndbseek(Ndb*, long);
156void ndbsetval(Ndbtuple*, char*, int);
157Ndbtuple* ndbsnext(Ndbs*, char*, char*);
158Ndbtuple* ndbsubstitute(Ndbtuple*, Ndbtuple*, Ndbtuple*);