xref: /netbsd/lib/libc/db/btree/btree.h (revision bf9ec67e)
1 /*	$NetBSD: btree.h,v 1.12 2002/01/21 21:33:42 tv Exp $	*/
2 
3 /*-
4  * Copyright (c) 1991, 1993, 1994
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Mike Olson.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed by the University of
21  *	California, Berkeley and its contributors.
22  * 4. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  *
38  *	@(#)btree.h	8.11 (Berkeley) 8/17/94
39  */
40 
41 #if HAVE_CONFIG_H
42 #include "config.h"
43 #endif
44 
45 /* Macros to set/clear/test flags. */
46 #define	F_SET(p, f)	(p)->flags |= (f)
47 #define	F_CLR(p, f)	(p)->flags &= ~(f)
48 #define	F_ISSET(p, f)	((p)->flags & (f))
49 
50 #include <mpool.h>
51 
52 #define	DEFMINKEYPAGE	(2)		/* Minimum keys per page */
53 #define	MINCACHE	(5)		/* Minimum cached pages */
54 #define	MINPSIZE	(512)		/* Minimum page size */
55 
56 /*
57  * Page 0 of a btree file contains a copy of the meta-data.  This page is also
58  * used as an out-of-band page, i.e. page pointers that point to nowhere point
59  * to page 0.  Page 1 is the root of the btree.
60  */
61 #define	P_INVALID	 0		/* Invalid tree page number. */
62 #define	P_META		 0		/* Tree metadata page number. */
63 #define	P_ROOT		 1		/* Tree root page number. */
64 
65 /*
66  * There are five page layouts in the btree: btree internal pages (BINTERNAL),
67  * btree leaf pages (BLEAF), recno internal pages (RINTERNAL), recno leaf pages
68  * (RLEAF) and overflow pages.  All five page types have a page header (PAGE).
69  * This implementation requires that values within structures NOT be padded.
70  * (ANSI C permits random padding.)  If your compiler pads randomly you'll have
71  * to do some work to get this package to run.
72  */
73 typedef struct _page {
74 	pgno_t	pgno;			/* this page's page number */
75 	pgno_t	prevpg;			/* left sibling */
76 	pgno_t	nextpg;			/* right sibling */
77 
78 #define	P_BINTERNAL	0x01		/* btree internal page */
79 #define	P_BLEAF		0x02		/* leaf page */
80 #define	P_OVERFLOW	0x04		/* overflow page */
81 #define	P_RINTERNAL	0x08		/* recno internal page */
82 #define	P_RLEAF		0x10		/* leaf page */
83 #define P_TYPE		0x1f		/* type mask */
84 #define	P_PRESERVE	0x20		/* never delete this chain of pages */
85 	u_int32_t flags;
86 
87 	indx_t	lower;			/* lower bound of free space on page */
88 	indx_t	upper;			/* upper bound of free space on page */
89 	indx_t	linp[1];		/* indx_t-aligned VAR. LENGTH DATA */
90 } PAGE;
91 
92 /* First and next index. */
93 #define	BTDATAOFF							\
94 	(sizeof(pgno_t) + sizeof(pgno_t) + sizeof(pgno_t) +		\
95 	    sizeof(u_int32_t) + sizeof(indx_t) + sizeof(indx_t))
96 #define	NEXTINDEX(p)	(((p)->lower - BTDATAOFF) / sizeof(indx_t))
97 
98 /*
99  * For pages other than overflow pages, there is an array of offsets into the
100  * rest of the page immediately following the page header.  Each offset is to
101  * an item which is unique to the type of page.  The h_lower offset is just
102  * past the last filled-in index.  The h_upper offset is the first item on the
103  * page.  Offsets are from the beginning of the page.
104  *
105  * If an item is too big to store on a single page, a flag is set and the item
106  * is a { page, size } pair such that the page is the first page of an overflow
107  * chain with size bytes of item.  Overflow pages are simply bytes without any
108  * external structure.
109  *
110  * The page number and size fields in the items are pgno_t-aligned so they can
111  * be manipulated without copying.  (This presumes that 32 bit items can be
112  * manipulated on this system.)
113  */
114 #define	BTLALIGN(n)	(((n) + sizeof(pgno_t) - 1) & ~(sizeof(pgno_t) - 1))
115 #define	NOVFLSIZE	(sizeof(pgno_t) + sizeof(u_int32_t))
116 
117 /*
118  * For the btree internal pages, the item is a key.  BINTERNALs are {key, pgno}
119  * pairs, such that the key compares less than or equal to all of the records
120  * on that page.  For a tree without duplicate keys, an internal page with two
121  * consecutive keys, a and b, will have all records greater than or equal to a
122  * and less than b stored on the page associated with a.  Duplicate keys are
123  * somewhat special and can cause duplicate internal and leaf page records and
124  * some minor modifications of the above rule.
125  */
126 typedef struct _binternal {
127 	u_int32_t ksize;		/* key size */
128 	pgno_t	pgno;			/* page number stored on */
129 #define	P_BIGDATA	0x01		/* overflow data */
130 #define	P_BIGKEY	0x02		/* overflow key */
131 	u_char	flags;
132 	char	bytes[1];		/* data */
133 } BINTERNAL;
134 
135 /* Get the page's BINTERNAL structure at index indx. */
136 #define	GETBINTERNAL(pg, indx)						\
137 	((BINTERNAL *)(void *)((char *)(void *)(pg) + (pg)->linp[indx]))
138 
139 /* Get the number of bytes in the entry. */
140 #define NBINTERNAL(len)							\
141 	BTLALIGN(sizeof(u_int32_t) + sizeof(pgno_t) + sizeof(u_char) + (len))
142 
143 /* Copy a BINTERNAL entry to the page. */
144 #define	WR_BINTERNAL(p, size, pgno, flags) {				\
145 	*(u_int32_t *)(void *)p = size;					\
146 	p += sizeof(u_int32_t);						\
147 	*(pgno_t *)(void *)p = pgno;					\
148 	p += sizeof(pgno_t);						\
149 	*(u_char *)(void *)p = flags;					\
150 	p += sizeof(u_char);						\
151 }
152 
153 /*
154  * For the recno internal pages, the item is a page number with the number of
155  * keys found on that page and below.
156  */
157 typedef struct _rinternal {
158 	recno_t	nrecs;			/* number of records */
159 	pgno_t	pgno;			/* page number stored below */
160 } RINTERNAL;
161 
162 /* Get the page's RINTERNAL structure at index indx. */
163 #define	GETRINTERNAL(pg, indx)						\
164 	((RINTERNAL *)(void *)((char *)(void *)(pg) + (pg)->linp[indx]))
165 
166 /* Get the number of bytes in the entry. */
167 #define NRINTERNAL							\
168 	BTLALIGN(sizeof(recno_t) + sizeof(pgno_t))
169 
170 /* Copy a RINTERAL entry to the page. */
171 #define	WR_RINTERNAL(p, nrecs, pgno) {					\
172 	*(recno_t *)(void *)p = nrecs;					\
173 	p += sizeof(recno_t);						\
174 	*(pgno_t *)(void *)p = pgno;					\
175 }
176 
177 /* For the btree leaf pages, the item is a key and data pair. */
178 typedef struct _bleaf {
179 	u_int32_t	ksize;		/* size of key */
180 	u_int32_t	dsize;		/* size of data */
181 	u_char	flags;			/* P_BIGDATA, P_BIGKEY */
182 	char	bytes[1];		/* data */
183 } BLEAF;
184 
185 /* Get the page's BLEAF structure at index indx. */
186 #define	GETBLEAF(pg, indx)						\
187 	((BLEAF *)(void *)((char *)(void *)(pg) + (pg)->linp[indx]))
188 
189 /* Get the number of bytes in the entry. */
190 #define NBLEAF(p)	NBLEAFDBT((p)->ksize, (p)->dsize)
191 
192 /* Get the number of bytes in the user's key/data pair. */
193 #define NBLEAFDBT(ksize, dsize)						\
194 	BTLALIGN(sizeof(u_int32_t) + sizeof(u_int32_t) + sizeof(u_char) +	\
195 	    (ksize) + (dsize))
196 
197 /* Copy a BLEAF entry to the page. */
198 #define	WR_BLEAF(p, key, data, flags) {					\
199 	*(u_int32_t *)(void *)p = key->size;				\
200 	p += sizeof(u_int32_t);						\
201 	*(u_int32_t *)(void *)p = data->size;				\
202 	p += sizeof(u_int32_t);						\
203 	*(u_char *)(void *)p = flags;					\
204 	p += sizeof(u_char);						\
205 	memmove(p, key->data, key->size);				\
206 	p += key->size;							\
207 	memmove(p, data->data, data->size);				\
208 }
209 
210 /* For the recno leaf pages, the item is a data entry. */
211 typedef struct _rleaf {
212 	u_int32_t	dsize;		/* size of data */
213 	u_char	flags;			/* P_BIGDATA */
214 	char	bytes[1];
215 } RLEAF;
216 
217 /* Get the page's RLEAF structure at index indx. */
218 #define	GETRLEAF(pg, indx)						\
219 	((RLEAF *)(void *)((char *)(void *)(pg) + (pg)->linp[indx]))
220 
221 /* Get the number of bytes in the entry. */
222 #define NRLEAF(p)	NRLEAFDBT((p)->dsize)
223 
224 /* Get the number of bytes from the user's data. */
225 #define	NRLEAFDBT(dsize)						\
226 	BTLALIGN(sizeof(u_int32_t) + sizeof(u_char) + (dsize))
227 
228 /* Copy a RLEAF entry to the page. */
229 #define	WR_RLEAF(p, data, flags) {					\
230 	*(u_int32_t *)(void *)p = data->size;				\
231 	p += sizeof(u_int32_t);						\
232 	*(u_char *)(void *)p = flags;					\
233 	p += sizeof(u_char);						\
234 	memmove(p, data->data, data->size);				\
235 }
236 
237 /*
238  * A record in the tree is either a pointer to a page and an index in the page
239  * or a page number and an index.  These structures are used as a cursor, stack
240  * entry and search returns as well as to pass records to other routines.
241  *
242  * One comment about searches.  Internal page searches must find the largest
243  * record less than key in the tree so that descents work.  Leaf page searches
244  * must find the smallest record greater than key so that the returned index
245  * is the record's correct position for insertion.
246  */
247 typedef struct _epgno {
248 	pgno_t	pgno;			/* the page number */
249 	indx_t	index;			/* the index on the page */
250 } EPGNO;
251 
252 typedef struct _epg {
253 	PAGE	*page;			/* the (pinned) page */
254 	indx_t	 index;			/* the index on the page */
255 } EPG;
256 
257 /*
258  * About cursors.  The cursor (and the page that contained the key/data pair
259  * that it referenced) can be deleted, which makes things a bit tricky.  If
260  * there are no duplicates of the cursor key in the tree (i.e. B_NODUPS is set
261  * or there simply aren't any duplicates of the key) we copy the key that it
262  * referenced when it's deleted, and reacquire a new cursor key if the cursor
263  * is used again.  If there are duplicates keys, we move to the next/previous
264  * key, and set a flag so that we know what happened.  NOTE: if duplicate (to
265  * the cursor) keys are added to the tree during this process, it is undefined
266  * if they will be returned or not in a cursor scan.
267  *
268  * The flags determine the possible states of the cursor:
269  *
270  * CURS_INIT	The cursor references *something*.
271  * CURS_ACQUIRE	The cursor was deleted, and a key has been saved so that
272  *		we can reacquire the right position in the tree.
273  * CURS_AFTER, CURS_BEFORE
274  *		The cursor was deleted, and now references a key/data pair
275  *		that has not yet been returned, either before or after the
276  *		deleted key/data pair.
277  * XXX
278  * This structure is broken out so that we can eventually offer multiple
279  * cursors as part of the DB interface.
280  */
281 typedef struct _cursor {
282 	EPGNO	 pg;			/* B: Saved tree reference. */
283 	DBT	 key;			/* B: Saved key, or key.data == NULL. */
284 	recno_t	 rcursor;		/* R: recno cursor (1-based) */
285 
286 #define	CURS_ACQUIRE	0x01		/*  B: Cursor needs to be reacquired. */
287 #define	CURS_AFTER	0x02		/*  B: Unreturned cursor after key. */
288 #define	CURS_BEFORE	0x04		/*  B: Unreturned cursor before key. */
289 #define	CURS_INIT	0x08		/* RB: Cursor initialized. */
290 	u_int8_t flags;
291 } CURSOR;
292 
293 /*
294  * The metadata of the tree.  The nrecs field is used only by the RECNO code.
295  * This is because the btree doesn't really need it and it requires that every
296  * put or delete call modify the metadata.
297  */
298 typedef struct _btmeta {
299 	u_int32_t	magic;		/* magic number */
300 	u_int32_t	version;	/* version */
301 	u_int32_t	psize;		/* page size */
302 	u_int32_t	free;		/* page number of first free page */
303 	u_int32_t	nrecs;		/* R: number of records */
304 
305 #define	SAVEMETA	(B_NODUPS | R_RECNO)
306 	u_int32_t	flags;		/* bt_flags & SAVEMETA */
307 } BTMETA;
308 
309 /* The in-memory btree/recno data structure. */
310 typedef struct _btree {
311 	MPOOL	 *bt_mp;		/* memory pool cookie */
312 
313 	DB	 *bt_dbp;		/* pointer to enclosing DB */
314 
315 	EPG	  bt_cur;		/* current (pinned) page */
316 	PAGE	 *bt_pinned;		/* page pinned across calls */
317 
318 	CURSOR	  bt_cursor;		/* cursor */
319 
320 #define	BT_PUSH(t, p, i) {						\
321 	t->bt_sp->pgno = p; 						\
322 	t->bt_sp->index = i; 						\
323 	++t->bt_sp;							\
324 }
325 #define	BT_POP(t)	(t->bt_sp == t->bt_stack ? NULL : --t->bt_sp)
326 #define	BT_CLR(t)	(t->bt_sp = t->bt_stack)
327 	EPGNO	  bt_stack[50];		/* stack of parent pages */
328 	EPGNO	 *bt_sp;		/* current stack pointer */
329 
330 	DBT	  bt_rkey;		/* returned key */
331 	DBT	  bt_rdata;		/* returned data */
332 
333 	int	  bt_fd;		/* tree file descriptor */
334 
335 	pgno_t	  bt_free;		/* next free page */
336 	u_int32_t bt_psize;		/* page size */
337 	indx_t	  bt_ovflsize;		/* cut-off for key/data overflow */
338 	int	  bt_lorder;		/* byte order */
339 					/* sorted order */
340 	enum { NOT, BACK, FORWARD } bt_order;
341 	EPGNO	  bt_last;		/* last insert */
342 
343 					/* B: key comparison function */
344 	int	(*bt_cmp) __P((const DBT *, const DBT *));
345 					/* B: prefix comparison function */
346 	size_t	(*bt_pfx) __P((const DBT *, const DBT *));
347 					/* R: recno input function */
348 	int	(*bt_irec) __P((struct _btree *, recno_t));
349 
350 	FILE	 *bt_rfp;		/* R: record FILE pointer */
351 	int	  bt_rfd;		/* R: record file descriptor */
352 
353 	caddr_t	  bt_cmap;		/* R: current point in mapped space */
354 	caddr_t	  bt_smap;		/* R: start of mapped space */
355 	caddr_t   bt_emap;		/* R: end of mapped space */
356 	size_t	  bt_msize;		/* R: size of mapped region. */
357 
358 	recno_t	  bt_nrecs;		/* R: number of records */
359 	size_t	  bt_reclen;		/* R: fixed record length */
360 	u_char	  bt_bval;		/* R: delimiting byte/pad character */
361 
362 /*
363  * NB:
364  * B_NODUPS and R_RECNO are stored on disk, and may not be changed.
365  */
366 #define	B_INMEM		0x00001		/* in-memory tree */
367 #define	B_METADIRTY	0x00002		/* need to write metadata */
368 #define	B_MODIFIED	0x00004		/* tree modified */
369 #define	B_NEEDSWAP	0x00008		/* if byte order requires swapping */
370 #define	B_RDONLY	0x00010		/* read-only tree */
371 
372 #define	B_NODUPS	0x00020		/* no duplicate keys permitted */
373 #define	R_RECNO		0x00080		/* record oriented tree */
374 
375 #define	R_CLOSEFP	0x00040		/* opened a file pointer */
376 #define	R_EOF		0x00100		/* end of input file reached. */
377 #define	R_FIXLEN	0x00200		/* fixed length records */
378 #define	R_MEMMAPPED	0x00400		/* memory mapped file. */
379 #define	R_INMEM		0x00800		/* in-memory file */
380 #define	R_MODIFIED	0x01000		/* modified file */
381 #define	R_RDONLY	0x02000		/* read-only file */
382 
383 #define	B_DB_LOCK	0x04000		/* DB_LOCK specified. */
384 #define	B_DB_SHMEM	0x08000		/* DB_SHMEM specified. */
385 #define	B_DB_TXN	0x10000		/* DB_TXN specified. */
386 	u_int32_t flags;
387 } BTREE;
388 
389 #include "extern.h"
390