xref: /original-bsd/lib/libc/db/btree/btree.h (revision 95ecee29)
1 /*-
2  * Copyright (c) 1991, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Mike Olson.
7  *
8  * %sccs.include.redist.c%
9  *
10  *	@(#)btree.h	8.3 (Berkeley) 09/14/93
11  */
12 
13 #include <mpool.h>
14 
15 #define	DEFMINKEYPAGE	(2)		/* Minimum keys per page */
16 #define	MINCACHE	(5)		/* Minimum cached pages */
17 #define	MINPSIZE	(512)		/* Minimum page size */
18 
19 /*
20  * Page 0 of a btree file contains a copy of the meta-data.  This page is also
21  * used as an out-of-band page, i.e. page pointers that point to nowhere point
22  * to page 0.  Page 1 is the root of the btree.
23  */
24 #define	P_INVALID	 0		/* Invalid tree page number. */
25 #define	P_META		 0		/* Tree metadata page number. */
26 #define	P_ROOT		 1		/* Tree root page number. */
27 
28 /*
29  * There are five page layouts in the btree: btree internal pages (BINTERNAL),
30  * btree leaf pages (BLEAF), recno internal pages (RINTERNAL), recno leaf pages
31  * (RLEAF) and overflow pages.  All five page types have a page header (PAGE).
32  * This implementation requires that longs within structures are NOT padded.
33  * (ANSI C permits random padding.)  If your compiler pads randomly you'll have
34  * to do some work to get this package to run.
35  */
36 typedef struct _page {
37 	pgno_t	pgno;			/* this page's page number */
38 	pgno_t	prevpg;			/* left sibling */
39 	pgno_t	nextpg;			/* right sibling */
40 
41 #define	P_BINTERNAL	0x01		/* btree internal page */
42 #define	P_BLEAF		0x02		/* leaf page */
43 #define	P_OVERFLOW	0x04		/* overflow page */
44 #define	P_RINTERNAL	0x08		/* recno internal page */
45 #define	P_RLEAF		0x10		/* leaf page */
46 #define P_TYPE		0x1f		/* type mask */
47 
48 #define	P_PRESERVE	0x20		/* never delete this chain of pages */
49 	u_long	flags;
50 
51 	indx_t	lower;			/* lower bound of free space on page */
52 	indx_t	upper;			/* upper bound of free space on page */
53 	indx_t	linp[1];		/* long-aligned VARIABLE LENGTH DATA */
54 } PAGE;
55 
56 /* First and next index. */
57 #define	BTDATAOFF	(sizeof(pgno_t) + sizeof(pgno_t) + sizeof(pgno_t) + \
58 			    sizeof(u_long) + sizeof(indx_t) + sizeof(indx_t))
59 #define	NEXTINDEX(p)	(((p)->lower - BTDATAOFF) / sizeof(indx_t))
60 
61 /*
62  * For pages other than overflow pages, there is an array of offsets into the
63  * rest of the page immediately following the page header.  Each offset is to
64  * an item which is unique to the type of page.  The h_lower offset is just
65  * past the last filled-in index.  The h_upper offset is the first item on the
66  * page.  Offsets are from the beginning of the page.
67  *
68  * If an item is too big to store on a single page, a flag is set and the item
69  * is a { page, size } pair such that the page is the first page of an overflow
70  * chain with size bytes of item.  Overflow pages are simply bytes without any
71  * external structure.
72  *
73  * The size and page number fields in the items are long aligned so they can be
74  * manipulated without copying.
75  */
76 #define	LALIGN(n)	(((n) + sizeof(u_long) - 1) & ~(sizeof(u_long) - 1))
77 #define	NOVFLSIZE	(sizeof(pgno_t) + sizeof(size_t))
78 
79 /*
80  * For the btree internal pages, the item is a key.  BINTERNALs are {key, pgno}
81  * pairs, such that the key compares less than or equal to all of the records
82  * on that page.  For a tree without duplicate keys, an internal page with two
83  * consecutive keys, a and b, will have all records greater than or equal to a
84  * and less than b stored on the page associated with a.  Duplicate keys are
85  * somewhat special and can cause duplicate internal and leaf page records and
86  * some minor modifications of the above rule.
87  */
88 typedef struct _binternal {
89 	size_t	ksize;			/* key size */
90 	pgno_t	pgno;			/* page number stored on */
91 #define	P_BIGDATA	0x01		/* overflow data */
92 #define	P_BIGKEY	0x02		/* overflow key */
93 	u_char	flags;
94 	char	bytes[1];		/* data */
95 } BINTERNAL;
96 
97 /* Get the page's BINTERNAL structure at index indx. */
98 #define	GETBINTERNAL(pg, indx) \
99 	((BINTERNAL *)((char *)(pg) + (pg)->linp[indx]))
100 
101 /* Get the number of bytes in the entry. */
102 #define NBINTERNAL(len) \
103 	LALIGN(sizeof(size_t) + sizeof(pgno_t) + sizeof(u_char) + (len))
104 
105 /* Copy a BINTERNAL entry to the page. */
106 #define	WR_BINTERNAL(p, size, pgno, flags) { \
107 	*(size_t *)p = size; \
108 	p += sizeof(size_t); \
109 	*(pgno_t *)p = pgno; \
110 	p += sizeof(pgno_t); \
111 	*(u_char *)p = flags; \
112 	p += sizeof(u_char); \
113 }
114 
115 /*
116  * For the recno internal pages, the item is a page number with the number of
117  * keys found on that page and below.
118  */
119 typedef struct _rinternal {
120 	recno_t	nrecs;			/* number of records */
121 	pgno_t	pgno;			/* page number stored below */
122 } RINTERNAL;
123 
124 /* Get the page's RINTERNAL structure at index indx. */
125 #define	GETRINTERNAL(pg, indx) \
126 	((RINTERNAL *)((char *)(pg) + (pg)->linp[indx]))
127 
128 /* Get the number of bytes in the entry. */
129 #define NRINTERNAL \
130 	LALIGN(sizeof(recno_t) + sizeof(pgno_t))
131 
132 /* Copy a RINTERAL entry to the page. */
133 #define	WR_RINTERNAL(p, nrecs, pgno) { \
134 	*(recno_t *)p = nrecs; \
135 	p += sizeof(recno_t); \
136 	*(pgno_t *)p = pgno; \
137 }
138 
139 /* For the btree leaf pages, the item is a key and data pair. */
140 typedef struct _bleaf {
141 	size_t	ksize;			/* size of key */
142 	size_t	dsize;			/* size of data */
143 	u_char	flags;			/* P_BIGDATA, P_BIGKEY */
144 	char	bytes[1];		/* data */
145 } BLEAF;
146 
147 /* Get the page's BLEAF structure at index indx. */
148 #define	GETBLEAF(pg, indx) \
149 	((BLEAF *)((char *)(pg) + (pg)->linp[indx]))
150 
151 /* Get the number of bytes in the entry. */
152 #define NBLEAF(p)	NBLEAFDBT((p)->ksize, (p)->dsize)
153 
154 /* Get the number of bytes in the user's key/data pair. */
155 #define NBLEAFDBT(ksize, dsize) \
156 	LALIGN(sizeof(size_t) + sizeof(size_t) + sizeof(u_char) + \
157 	    (ksize) + (dsize))
158 
159 /* Copy a BLEAF entry to the page. */
160 #define	WR_BLEAF(p, key, data, flags) { \
161 	*(size_t *)p = key->size; \
162 	p += sizeof(size_t); \
163 	*(size_t *)p = data->size; \
164 	p += sizeof(size_t); \
165 	*(u_char *)p = flags; \
166 	p += sizeof(u_char); \
167 	memmove(p, key->data, key->size); \
168 	p += key->size; \
169 	memmove(p, data->data, data->size); \
170 }
171 
172 /* For the recno leaf pages, the item is a data entry. */
173 typedef struct _rleaf {
174 	size_t	dsize;			/* size of data */
175 	u_char	flags;			/* P_BIGDATA */
176 	char	bytes[1];
177 } RLEAF;
178 
179 /* Get the page's RLEAF structure at index indx. */
180 #define	GETRLEAF(pg, indx) \
181 	((RLEAF *)((char *)(pg) + (pg)->linp[indx]))
182 
183 /* Get the number of bytes in the entry. */
184 #define NRLEAF(p)	NRLEAFDBT((p)->dsize)
185 
186 /* Get the number of bytes from the user's data. */
187 #define	NRLEAFDBT(dsize) \
188 	LALIGN(sizeof(size_t) + sizeof(u_char) + (dsize))
189 
190 /* Copy a RLEAF entry to the page. */
191 #define	WR_RLEAF(p, data, flags) { \
192 	*(size_t *)p = data->size; \
193 	p += sizeof(size_t); \
194 	*(u_char *)p = flags; \
195 	p += sizeof(u_char); \
196 	memmove(p, data->data, data->size); \
197 }
198 
199 /*
200  * A record in the tree is either a pointer to a page and an index in the page
201  * or a page number and an index.  These structures are used as a cursor, stack
202  * entry and search returns as well as to pass records to other routines.
203  *
204  * One comment about searches.  Internal page searches must find the largest
205  * record less than key in the tree so that descents work.  Leaf page searches
206  * must find the smallest record greater than key so that the returned index
207  * is the record's correct position for insertion.
208  *
209  * One comment about cursors.  The cursor key is never removed from the tree,
210  * even if deleted.  This is because it is quite difficult to decide where the
211  * cursor should be when other keys have been inserted/deleted in the tree;
212  * duplicate keys make it impossible.  This scheme does require extra work
213  * though, to make sure that we don't perform an operation on a deleted key.
214  */
215 typedef struct _epgno {
216 	pgno_t	pgno;			/* the page number */
217 	indx_t	index;			/* the index on the page */
218 } EPGNO;
219 
220 typedef struct _epg {
221 	PAGE	*page;			/* the (pinned) page */
222 	indx_t	 index;			/* the index on the page */
223 } EPG;
224 
225 /*
226  * The metadata of the tree.  The m_nrecs field is used only by the RECNO code.
227  * This is because the btree doesn't really need it and it requires that every
228  * put or delete call modify the metadata.
229  */
230 typedef struct _btmeta {
231 	u_long	m_magic;		/* magic number */
232 	u_long	m_version;		/* version */
233 	u_long	m_psize;		/* page size */
234 	u_long	m_free;			/* page number of first free page */
235 	u_long	m_nrecs;		/* R: number of records */
236 #define	SAVEMETA	(B_NODUPS | R_RECNO)
237 	u_long	m_flags;		/* bt_flags & SAVEMETA */
238 	u_long	m_unused;		/* unused */
239 } BTMETA;
240 
241 /* The in-memory btree/recno data structure. */
242 typedef struct _btree {
243 	MPOOL	*bt_mp;			/* memory pool cookie */
244 
245 	DB	*bt_dbp;		/* pointer to enclosing DB */
246 
247 	EPG	bt_cur;			/* current (pinned) page */
248 	PAGE	*bt_pinned;		/* page pinned across calls */
249 
250 	EPGNO	bt_bcursor;		/* B: btree cursor */
251 	recno_t	bt_rcursor;		/* R: recno cursor (1-based) */
252 
253 #define	BT_POP(t)	(t->bt_sp ? t->bt_stack + --t->bt_sp : NULL)
254 #define	BT_CLR(t)	(t->bt_sp = 0)
255 	EPGNO	*bt_stack;		/* stack of parent pages */
256 	u_int	bt_sp;			/* current stack pointer */
257 	u_int	bt_maxstack;		/* largest stack */
258 
259 	char	*bt_kbuf;		/* key buffer */
260 	size_t	bt_kbufsz;		/* key buffer size */
261 	char	*bt_dbuf;		/* data buffer */
262 	size_t	bt_dbufsz;		/* data buffer size */
263 
264 	int	bt_fd;			/* tree file descriptor */
265 
266 	pgno_t	bt_free;		/* next free page */
267 	u_long	bt_psize;		/* page size */
268 	indx_t	bt_ovflsize;		/* cut-off for key/data overflow */
269 	int	bt_lorder;		/* byte order */
270 					/* sorted order */
271 	enum { NOT, BACK, FORWARD, } bt_order;
272 	EPGNO	bt_last;		/* last insert */
273 
274 					/* B: key comparison function */
275 	int	(*bt_cmp) __P((const DBT *, const DBT *));
276 					/* B: prefix comparison function */
277 	int	(*bt_pfx) __P((const DBT *, const DBT *));
278 					/* R: recno input function */
279 	int	(*bt_irec) __P((struct _btree *, recno_t));
280 
281 	FILE	*bt_rfp;		/* R: record FILE pointer */
282 	int	bt_rfd;			/* R: record file descriptor */
283 
284 	caddr_t	bt_cmap;		/* R: current point in mapped space */
285 	caddr_t	bt_smap;		/* R: start of mapped space */
286 	caddr_t bt_emap;		/* R: end of mapped space */
287 	size_t	bt_msize;		/* R: size of mapped region. */
288 
289 	recno_t	bt_nrecs;		/* R: number of records */
290 	size_t	bt_reclen;		/* R: fixed record length */
291 	u_char	bt_bval;		/* R: delimiting byte/pad character */
292 
293 /*
294  * NB:
295  * B_NODUPS and R_RECNO are stored on disk, and may not be changed.
296  */
297 #define	B_DELCRSR	0x00001		/* cursor has been deleted */
298 #define	B_INMEM		0x00002		/* in-memory tree */
299 #define	B_METADIRTY	0x00004		/* need to write metadata */
300 #define	B_MODIFIED	0x00008		/* tree modified */
301 #define	B_NEEDSWAP	0x00010		/* if byte order requires swapping */
302 #define	B_NODUPS	0x00020		/* no duplicate keys permitted */
303 #define	B_RDONLY	0x00040		/* read-only tree */
304 #define	R_RECNO		0x00080		/* record oriented tree */
305 #define	B_SEQINIT	0x00100		/* sequential scan initialized */
306 
307 #define	R_CLOSEFP	0x00200		/* opened a file pointer */
308 #define	R_EOF		0x00400		/* end of input file reached. */
309 #define	R_FIXLEN	0x00800		/* fixed length records */
310 #define	R_MEMMAPPED	0x01000		/* memory mapped file. */
311 #define	R_INMEM		0x02000		/* in-memory file */
312 #define	R_MODIFIED	0x04000		/* modified file */
313 #define	R_RDONLY	0x08000		/* read-only file */
314 
315 #define	B_DB_LOCK	0x10000		/* DB_LOCK specified. */
316 #define	B_DB_SHMEM	0x20000		/* DB_SHMEM specified. */
317 #define	B_DB_TXN	0x40000		/* DB_TXN specified. */
318 
319 	u_long		bt_flags;	/* btree state */
320 } BTREE;
321 
322 #define	SET(t, f)	((t)->bt_flags |= (f))
323 #define	CLR(t, f)	((t)->bt_flags &= ~(f))
324 #define	ISSET(t, f)	((t)->bt_flags & (f))
325 
326 #include "extern.h"
327