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