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