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