btree.h revision 14272
11573Srgrimes/*-
214272Spst * Copyright (c) 1991, 1993, 1994
31573Srgrimes *	The Regents of the University of California.  All rights reserved.
41573Srgrimes *
51573Srgrimes * This code is derived from software contributed to Berkeley by
61573Srgrimes * Mike Olson.
71573Srgrimes *
81573Srgrimes * Redistribution and use in source and binary forms, with or without
91573Srgrimes * modification, are permitted provided that the following conditions
101573Srgrimes * are met:
111573Srgrimes * 1. Redistributions of source code must retain the above copyright
121573Srgrimes *    notice, this list of conditions and the following disclaimer.
131573Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
141573Srgrimes *    notice, this list of conditions and the following disclaimer in the
151573Srgrimes *    documentation and/or other materials provided with the distribution.
161573Srgrimes * 3. All advertising materials mentioning features or use of this software
171573Srgrimes *    must display the following acknowledgement:
181573Srgrimes *	This product includes software developed by the University of
191573Srgrimes *	California, Berkeley and its contributors.
201573Srgrimes * 4. Neither the name of the University nor the names of its contributors
211573Srgrimes *    may be used to endorse or promote products derived from this software
221573Srgrimes *    without specific prior written permission.
231573Srgrimes *
241573Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
251573Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
261573Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
271573Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
281573Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
291573Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
301573Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
311573Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
321573Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
331573Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
341573Srgrimes * SUCH DAMAGE.
351573Srgrimes *
3614272Spst *	@(#)btree.h	8.11 (Berkeley) 8/17/94
371573Srgrimes */
381573Srgrimes
3914272Spst/* Macros to set/clear/test flags. */
4014272Spst#define	F_SET(p, f)	(p)->flags |= (f)
4114272Spst#define	F_CLR(p, f)	(p)->flags &= ~(f)
4214272Spst#define	F_ISSET(p, f)	((p)->flags & (f))
4314272Spst
441573Srgrimes#include <mpool.h>
451573Srgrimes
461573Srgrimes#define	DEFMINKEYPAGE	(2)		/* Minimum keys per page */
471573Srgrimes#define	MINCACHE	(5)		/* Minimum cached pages */
481573Srgrimes#define	MINPSIZE	(512)		/* Minimum page size */
491573Srgrimes
501573Srgrimes/*
511573Srgrimes * Page 0 of a btree file contains a copy of the meta-data.  This page is also
521573Srgrimes * used as an out-of-band page, i.e. page pointers that point to nowhere point
531573Srgrimes * to page 0.  Page 1 is the root of the btree.
541573Srgrimes */
551573Srgrimes#define	P_INVALID	 0		/* Invalid tree page number. */
561573Srgrimes#define	P_META		 0		/* Tree metadata page number. */
571573Srgrimes#define	P_ROOT		 1		/* Tree root page number. */
581573Srgrimes
591573Srgrimes/*
601573Srgrimes * There are five page layouts in the btree: btree internal pages (BINTERNAL),
611573Srgrimes * btree leaf pages (BLEAF), recno internal pages (RINTERNAL), recno leaf pages
621573Srgrimes * (RLEAF) and overflow pages.  All five page types have a page header (PAGE).
631573Srgrimes * This implementation requires that values within structures NOT be padded.
641573Srgrimes * (ANSI C permits random padding.)  If your compiler pads randomly you'll have
651573Srgrimes * to do some work to get this package to run.
661573Srgrimes */
671573Srgrimestypedef struct _page {
681573Srgrimes	pgno_t	pgno;			/* this page's page number */
691573Srgrimes	pgno_t	prevpg;			/* left sibling */
701573Srgrimes	pgno_t	nextpg;			/* right sibling */
711573Srgrimes
721573Srgrimes#define	P_BINTERNAL	0x01		/* btree internal page */
731573Srgrimes#define	P_BLEAF		0x02		/* leaf page */
741573Srgrimes#define	P_OVERFLOW	0x04		/* overflow page */
751573Srgrimes#define	P_RINTERNAL	0x08		/* recno internal page */
761573Srgrimes#define	P_RLEAF		0x10		/* leaf page */
771573Srgrimes#define P_TYPE		0x1f		/* type mask */
781573Srgrimes#define	P_PRESERVE	0x20		/* never delete this chain of pages */
791573Srgrimes	u_int32_t flags;
801573Srgrimes
811573Srgrimes	indx_t	lower;			/* lower bound of free space on page */
821573Srgrimes	indx_t	upper;			/* upper bound of free space on page */
831573Srgrimes	indx_t	linp[1];		/* indx_t-aligned VAR. LENGTH DATA */
841573Srgrimes} PAGE;
851573Srgrimes
861573Srgrimes/* First and next index. */
8714272Spst#define	BTDATAOFF							\
8814272Spst	(sizeof(pgno_t) + sizeof(pgno_t) + sizeof(pgno_t) +		\
8914272Spst	    sizeof(u_int32_t) + sizeof(indx_t) + sizeof(indx_t))
901573Srgrimes#define	NEXTINDEX(p)	(((p)->lower - BTDATAOFF) / sizeof(indx_t))
911573Srgrimes
921573Srgrimes/*
931573Srgrimes * For pages other than overflow pages, there is an array of offsets into the
941573Srgrimes * rest of the page immediately following the page header.  Each offset is to
951573Srgrimes * an item which is unique to the type of page.  The h_lower offset is just
961573Srgrimes * past the last filled-in index.  The h_upper offset is the first item on the
971573Srgrimes * page.  Offsets are from the beginning of the page.
981573Srgrimes *
991573Srgrimes * If an item is too big to store on a single page, a flag is set and the item
1001573Srgrimes * is a { page, size } pair such that the page is the first page of an overflow
1011573Srgrimes * chain with size bytes of item.  Overflow pages are simply bytes without any
1021573Srgrimes * external structure.
1031573Srgrimes *
1041573Srgrimes * The page number and size fields in the items are pgno_t-aligned so they can
1051573Srgrimes * be manipulated without copying.  (This presumes that 32 bit items can be
1061573Srgrimes * manipulated on this system.)
1071573Srgrimes */
10814272Spst#define	LALIGN(n)	(((n) + sizeof(pgno_t) - 1) & ~(sizeof(pgno_t) - 1))
10914272Spst#define	NOVFLSIZE	(sizeof(pgno_t) + sizeof(u_int32_t))
1101573Srgrimes
1111573Srgrimes/*
1121573Srgrimes * For the btree internal pages, the item is a key.  BINTERNALs are {key, pgno}
1131573Srgrimes * pairs, such that the key compares less than or equal to all of the records
1141573Srgrimes * on that page.  For a tree without duplicate keys, an internal page with two
1151573Srgrimes * consecutive keys, a and b, will have all records greater than or equal to a
1161573Srgrimes * and less than b stored on the page associated with a.  Duplicate keys are
1171573Srgrimes * somewhat special and can cause duplicate internal and leaf page records and
1181573Srgrimes * some minor modifications of the above rule.
1191573Srgrimes */
1201573Srgrimestypedef struct _binternal {
12114272Spst	u_int32_t ksize;		/* key size */
1221573Srgrimes	pgno_t	pgno;			/* page number stored on */
1231573Srgrimes#define	P_BIGDATA	0x01		/* overflow data */
1241573Srgrimes#define	P_BIGKEY	0x02		/* overflow key */
1251573Srgrimes	u_char	flags;
1261573Srgrimes	char	bytes[1];		/* data */
1271573Srgrimes} BINTERNAL;
1281573Srgrimes
1291573Srgrimes/* Get the page's BINTERNAL structure at index indx. */
13014272Spst#define	GETBINTERNAL(pg, indx)						\
1311573Srgrimes	((BINTERNAL *)((char *)(pg) + (pg)->linp[indx]))
1321573Srgrimes
1331573Srgrimes/* Get the number of bytes in the entry. */
13414272Spst#define NBINTERNAL(len)							\
13514272Spst	LALIGN(sizeof(u_int32_t) + sizeof(pgno_t) + sizeof(u_char) + (len))
1361573Srgrimes
1371573Srgrimes/* Copy a BINTERNAL entry to the page. */
13814272Spst#define	WR_BINTERNAL(p, size, pgno, flags) {				\
13914272Spst	*(u_int32_t *)p = size;						\
14014272Spst	p += sizeof(u_int32_t);						\
14114272Spst	*(pgno_t *)p = pgno;						\
14214272Spst	p += sizeof(pgno_t);						\
14314272Spst	*(u_char *)p = flags;						\
14414272Spst	p += sizeof(u_char);						\
1451573Srgrimes}
1461573Srgrimes
1471573Srgrimes/*
1481573Srgrimes * For the recno internal pages, the item is a page number with the number of
1491573Srgrimes * keys found on that page and below.
1501573Srgrimes */
1511573Srgrimestypedef struct _rinternal {
1521573Srgrimes	recno_t	nrecs;			/* number of records */
1531573Srgrimes	pgno_t	pgno;			/* page number stored below */
1541573Srgrimes} RINTERNAL;
1551573Srgrimes
1561573Srgrimes/* Get the page's RINTERNAL structure at index indx. */
15714272Spst#define	GETRINTERNAL(pg, indx)						\
1581573Srgrimes	((RINTERNAL *)((char *)(pg) + (pg)->linp[indx]))
1591573Srgrimes
1601573Srgrimes/* Get the number of bytes in the entry. */
16114272Spst#define NRINTERNAL							\
1621573Srgrimes	LALIGN(sizeof(recno_t) + sizeof(pgno_t))
1631573Srgrimes
1641573Srgrimes/* Copy a RINTERAL entry to the page. */
16514272Spst#define	WR_RINTERNAL(p, nrecs, pgno) {					\
16614272Spst	*(recno_t *)p = nrecs;						\
16714272Spst	p += sizeof(recno_t);						\
16814272Spst	*(pgno_t *)p = pgno;						\
1691573Srgrimes}
1701573Srgrimes
1711573Srgrimes/* For the btree leaf pages, the item is a key and data pair. */
1721573Srgrimestypedef struct _bleaf {
17314272Spst	u_int32_t	ksize;		/* size of key */
17414272Spst	u_int32_t	dsize;		/* size of data */
1751573Srgrimes	u_char	flags;			/* P_BIGDATA, P_BIGKEY */
1761573Srgrimes	char	bytes[1];		/* data */
1771573Srgrimes} BLEAF;
1781573Srgrimes
1791573Srgrimes/* Get the page's BLEAF structure at index indx. */
18014272Spst#define	GETBLEAF(pg, indx)						\
1811573Srgrimes	((BLEAF *)((char *)(pg) + (pg)->linp[indx]))
1821573Srgrimes
1831573Srgrimes/* Get the number of bytes in the entry. */
1841573Srgrimes#define NBLEAF(p)	NBLEAFDBT((p)->ksize, (p)->dsize)
1851573Srgrimes
1861573Srgrimes/* Get the number of bytes in the user's key/data pair. */
18714272Spst#define NBLEAFDBT(ksize, dsize)						\
18814272Spst	LALIGN(sizeof(u_int32_t) + sizeof(u_int32_t) + sizeof(u_char) +	\
1891573Srgrimes	    (ksize) + (dsize))
1901573Srgrimes
1911573Srgrimes/* Copy a BLEAF entry to the page. */
19214272Spst#define	WR_BLEAF(p, key, data, flags) {					\
19314272Spst	*(u_int32_t *)p = key->size;					\
19414272Spst	p += sizeof(u_int32_t);						\
19514272Spst	*(u_int32_t *)p = data->size;					\
19614272Spst	p += sizeof(u_int32_t);						\
19714272Spst	*(u_char *)p = flags;						\
19814272Spst	p += sizeof(u_char);						\
19914272Spst	memmove(p, key->data, key->size);				\
20014272Spst	p += key->size;							\
20114272Spst	memmove(p, data->data, data->size);				\
2021573Srgrimes}
2031573Srgrimes
2041573Srgrimes/* For the recno leaf pages, the item is a data entry. */
2051573Srgrimestypedef struct _rleaf {
20614272Spst	u_int32_t	dsize;		/* size of data */
2071573Srgrimes	u_char	flags;			/* P_BIGDATA */
2081573Srgrimes	char	bytes[1];
2091573Srgrimes} RLEAF;
2101573Srgrimes
2111573Srgrimes/* Get the page's RLEAF structure at index indx. */
21214272Spst#define	GETRLEAF(pg, indx)						\
2131573Srgrimes	((RLEAF *)((char *)(pg) + (pg)->linp[indx]))
2141573Srgrimes
2151573Srgrimes/* Get the number of bytes in the entry. */
2161573Srgrimes#define NRLEAF(p)	NRLEAFDBT((p)->dsize)
2171573Srgrimes
2181573Srgrimes/* Get the number of bytes from the user's data. */
21914272Spst#define	NRLEAFDBT(dsize)						\
22014272Spst	LALIGN(sizeof(u_int32_t) + sizeof(u_char) + (dsize))
2211573Srgrimes
2221573Srgrimes/* Copy a RLEAF entry to the page. */
22314272Spst#define	WR_RLEAF(p, data, flags) {					\
22414272Spst	*(u_int32_t *)p = data->size;					\
22514272Spst	p += sizeof(u_int32_t);						\
22614272Spst	*(u_char *)p = flags;						\
22714272Spst	p += sizeof(u_char);						\
22814272Spst	memmove(p, data->data, data->size);				\
2291573Srgrimes}
2301573Srgrimes
2311573Srgrimes/*
2321573Srgrimes * A record in the tree is either a pointer to a page and an index in the page
2331573Srgrimes * or a page number and an index.  These structures are used as a cursor, stack
2341573Srgrimes * entry and search returns as well as to pass records to other routines.
2351573Srgrimes *
2361573Srgrimes * One comment about searches.  Internal page searches must find the largest
2371573Srgrimes * record less than key in the tree so that descents work.  Leaf page searches
2381573Srgrimes * must find the smallest record greater than key so that the returned index
2391573Srgrimes * is the record's correct position for insertion.
2401573Srgrimes */
2411573Srgrimestypedef struct _epgno {
2421573Srgrimes	pgno_t	pgno;			/* the page number */
2431573Srgrimes	indx_t	index;			/* the index on the page */
2441573Srgrimes} EPGNO;
2451573Srgrimes
2461573Srgrimestypedef struct _epg {
2471573Srgrimes	PAGE	*page;			/* the (pinned) page */
2481573Srgrimes	indx_t	 index;			/* the index on the page */
2491573Srgrimes} EPG;
2501573Srgrimes
2511573Srgrimes/*
25214272Spst * About cursors.  The cursor (and the page that contained the key/data pair
25314272Spst * that it referenced) can be deleted, which makes things a bit tricky.  If
25414272Spst * there are no duplicates of the cursor key in the tree (i.e. B_NODUPS is set
25514272Spst * or there simply aren't any duplicates of the key) we copy the key that it
25614272Spst * referenced when it's deleted, and reacquire a new cursor key if the cursor
25714272Spst * is used again.  If there are duplicates keys, we move to the next/previous
25814272Spst * key, and set a flag so that we know what happened.  NOTE: if duplicate (to
25914272Spst * the cursor) keys are added to the tree during this process, it is undefined
26014272Spst * if they will be returned or not in a cursor scan.
26114272Spst *
26214272Spst * The flags determine the possible states of the cursor:
26314272Spst *
26414272Spst * CURS_INIT	The cursor references *something*.
26514272Spst * CURS_ACQUIRE	The cursor was deleted, and a key has been saved so that
26614272Spst *		we can reacquire the right position in the tree.
26714272Spst * CURS_AFTER, CURS_BEFORE
26814272Spst *		The cursor was deleted, and now references a key/data pair
26914272Spst *		that has not yet been returned, either before or after the
27014272Spst *		deleted key/data pair.
27114272Spst * XXX
27214272Spst * This structure is broken out so that we can eventually offer multiple
27314272Spst * cursors as part of the DB interface.
27414272Spst */
27514272Spsttypedef struct _cursor {
27614272Spst	EPGNO	 pg;			/* B: Saved tree reference. */
27714272Spst	DBT	 key;			/* B: Saved key, or key.data == NULL. */
27814272Spst	recno_t	 rcursor;		/* R: recno cursor (1-based) */
27914272Spst
28014272Spst#define	CURS_ACQUIRE	0x01		/*  B: Cursor needs to be reacquired. */
28114272Spst#define	CURS_AFTER	0x02		/*  B: Unreturned cursor after key. */
28214272Spst#define	CURS_BEFORE	0x04		/*  B: Unreturned cursor before key. */
28314272Spst#define	CURS_INIT	0x08		/* RB: Cursor initialized. */
28414272Spst	u_int8_t flags;
28514272Spst} CURSOR;
28614272Spst
28714272Spst/*
28814272Spst * The metadata of the tree.  The nrecs field is used only by the RECNO code.
2891573Srgrimes * This is because the btree doesn't really need it and it requires that every
2901573Srgrimes * put or delete call modify the metadata.
2911573Srgrimes */
2921573Srgrimestypedef struct _btmeta {
29314272Spst	u_int32_t	magic;		/* magic number */
29414272Spst	u_int32_t	version;	/* version */
29514272Spst	u_int32_t	psize;		/* page size */
29614272Spst	u_int32_t	free;		/* page number of first free page */
29714272Spst	u_int32_t	nrecs;		/* R: number of records */
29814272Spst
2991573Srgrimes#define	SAVEMETA	(B_NODUPS | R_RECNO)
30014272Spst	u_int32_t	flags;		/* bt_flags & SAVEMETA */
3011573Srgrimes} BTMETA;
3021573Srgrimes
3031573Srgrimes/* The in-memory btree/recno data structure. */
3041573Srgrimestypedef struct _btree {
30514272Spst	MPOOL	 *bt_mp;		/* memory pool cookie */
3061573Srgrimes
30714272Spst	DB	 *bt_dbp;		/* pointer to enclosing DB */
3081573Srgrimes
30914272Spst	EPG	  bt_cur;		/* current (pinned) page */
31014272Spst	PAGE	 *bt_pinned;		/* page pinned across calls */
3111573Srgrimes
31214272Spst	CURSOR	  bt_cursor;		/* cursor */
3131573Srgrimes
31414272Spst#define	BT_PUSH(t, p, i) {						\
31514272Spst	t->bt_sp->pgno = p; 						\
31614272Spst	t->bt_sp->index = i; 						\
31714272Spst	++t->bt_sp;							\
31814272Spst}
31914272Spst#define	BT_POP(t)	(t->bt_sp == t->bt_stack ? NULL : --t->bt_sp)
32014272Spst#define	BT_CLR(t)	(t->bt_sp = t->bt_stack)
32114272Spst	EPGNO	  bt_stack[50];		/* stack of parent pages */
32214272Spst	EPGNO	 *bt_sp;		/* current stack pointer */
3231573Srgrimes
32414272Spst	DBT	  bt_rkey;		/* returned key */
32514272Spst	DBT	  bt_rdata;		/* returned data */
3261573Srgrimes
32714272Spst	int	  bt_fd;		/* tree file descriptor */
3281573Srgrimes
32914272Spst	pgno_t	  bt_free;		/* next free page */
3301573Srgrimes	u_int32_t bt_psize;		/* page size */
33114272Spst	indx_t	  bt_ovflsize;		/* cut-off for key/data overflow */
33214272Spst	int	  bt_lorder;		/* byte order */
3331573Srgrimes					/* sorted order */
3341573Srgrimes	enum { NOT, BACK, FORWARD } bt_order;
33514272Spst	EPGNO	  bt_last;		/* last insert */
3361573Srgrimes
3371573Srgrimes					/* B: key comparison function */
3381573Srgrimes	int	(*bt_cmp) __P((const DBT *, const DBT *));
3391573Srgrimes					/* B: prefix comparison function */
3401573Srgrimes	size_t	(*bt_pfx) __P((const DBT *, const DBT *));
3411573Srgrimes					/* R: recno input function */
3421573Srgrimes	int	(*bt_irec) __P((struct _btree *, recno_t));
3431573Srgrimes
34414272Spst	FILE	 *bt_rfp;		/* R: record FILE pointer */
34514272Spst	int	  bt_rfd;		/* R: record file descriptor */
3461573Srgrimes
34714272Spst	caddr_t	  bt_cmap;		/* R: current point in mapped space */
34814272Spst	caddr_t	  bt_smap;		/* R: start of mapped space */
34914272Spst	caddr_t   bt_emap;		/* R: end of mapped space */
35014272Spst	size_t	  bt_msize;		/* R: size of mapped region. */
3511573Srgrimes
35214272Spst	recno_t	  bt_nrecs;		/* R: number of records */
35314272Spst	size_t	  bt_reclen;		/* R: fixed record length */
35414272Spst	u_char	  bt_bval;		/* R: delimiting byte/pad character */
3551573Srgrimes
3561573Srgrimes/*
3571573Srgrimes * NB:
3581573Srgrimes * B_NODUPS and R_RECNO are stored on disk, and may not be changed.
3591573Srgrimes */
36014272Spst#define	B_INMEM		0x00001		/* in-memory tree */
36114272Spst#define	B_METADIRTY	0x00002		/* need to write metadata */
36214272Spst#define	B_MODIFIED	0x00004		/* tree modified */
36314272Spst#define	B_NEEDSWAP	0x00008		/* if byte order requires swapping */
36414272Spst#define	B_RDONLY	0x00010		/* read-only tree */
36514272Spst
3661573Srgrimes#define	B_NODUPS	0x00020		/* no duplicate keys permitted */
3671573Srgrimes#define	R_RECNO		0x00080		/* record oriented tree */
3681573Srgrimes
36914272Spst#define	R_CLOSEFP	0x00040		/* opened a file pointer */
37014272Spst#define	R_EOF		0x00100		/* end of input file reached. */
37114272Spst#define	R_FIXLEN	0x00200		/* fixed length records */
37214272Spst#define	R_MEMMAPPED	0x00400		/* memory mapped file. */
37314272Spst#define	R_INMEM		0x00800		/* in-memory file */
37414272Spst#define	R_MODIFIED	0x01000		/* modified file */
37514272Spst#define	R_RDONLY	0x02000		/* read-only file */
3761573Srgrimes
37714272Spst#define	B_DB_LOCK	0x04000		/* DB_LOCK specified. */
37814272Spst#define	B_DB_SHMEM	0x08000		/* DB_SHMEM specified. */
37914272Spst#define	B_DB_TXN	0x10000		/* DB_TXN specified. */
38014272Spst	u_int32_t flags;
3811573Srgrimes} BTREE;
3821573Srgrimes
3831573Srgrimes#include "extern.h"
384