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 * 4. Neither the name of the University nor the names of its contributors
171573Srgrimes *    may be used to endorse or promote products derived from this software
181573Srgrimes *    without specific prior written permission.
191573Srgrimes *
201573Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
211573Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
221573Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
231573Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
241573Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
251573Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
261573Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
271573Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
281573Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
291573Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
301573Srgrimes * SUCH DAMAGE.
311573Srgrimes *
3214272Spst *	@(#)btree.h	8.11 (Berkeley) 8/17/94
3392991Sobrien * $FreeBSD$
341573Srgrimes */
351573Srgrimes
3614272Spst/* Macros to set/clear/test flags. */
3714272Spst#define	F_SET(p, f)	(p)->flags |= (f)
3814272Spst#define	F_CLR(p, f)	(p)->flags &= ~(f)
3914272Spst#define	F_ISSET(p, f)	((p)->flags & (f))
4014272Spst
411573Srgrimes#include <mpool.h>
421573Srgrimes
431573Srgrimes#define	DEFMINKEYPAGE	(2)		/* Minimum keys per page */
441573Srgrimes#define	MINCACHE	(5)		/* Minimum cached pages */
451573Srgrimes#define	MINPSIZE	(512)		/* Minimum page size */
461573Srgrimes
471573Srgrimes/*
481573Srgrimes * Page 0 of a btree file contains a copy of the meta-data.  This page is also
491573Srgrimes * used as an out-of-band page, i.e. page pointers that point to nowhere point
501573Srgrimes * to page 0.  Page 1 is the root of the btree.
511573Srgrimes */
521573Srgrimes#define	P_INVALID	 0		/* Invalid tree page number. */
531573Srgrimes#define	P_META		 0		/* Tree metadata page number. */
541573Srgrimes#define	P_ROOT		 1		/* Tree root page number. */
551573Srgrimes
561573Srgrimes/*
571573Srgrimes * There are five page layouts in the btree: btree internal pages (BINTERNAL),
581573Srgrimes * btree leaf pages (BLEAF), recno internal pages (RINTERNAL), recno leaf pages
591573Srgrimes * (RLEAF) and overflow pages.  All five page types have a page header (PAGE).
601573Srgrimes * This implementation requires that values within structures NOT be padded.
611573Srgrimes * (ANSI C permits random padding.)  If your compiler pads randomly you'll have
621573Srgrimes * to do some work to get this package to run.
631573Srgrimes */
641573Srgrimestypedef struct _page {
651573Srgrimes	pgno_t	pgno;			/* this page's page number */
661573Srgrimes	pgno_t	prevpg;			/* left sibling */
671573Srgrimes	pgno_t	nextpg;			/* right sibling */
681573Srgrimes
691573Srgrimes#define	P_BINTERNAL	0x01		/* btree internal page */
701573Srgrimes#define	P_BLEAF		0x02		/* leaf page */
711573Srgrimes#define	P_OVERFLOW	0x04		/* overflow page */
721573Srgrimes#define	P_RINTERNAL	0x08		/* recno internal page */
731573Srgrimes#define	P_RLEAF		0x10		/* leaf page */
741573Srgrimes#define P_TYPE		0x1f		/* type mask */
751573Srgrimes#define	P_PRESERVE	0x20		/* never delete this chain of pages */
761573Srgrimes	u_int32_t flags;
771573Srgrimes
781573Srgrimes	indx_t	lower;			/* lower bound of free space on page */
791573Srgrimes	indx_t	upper;			/* upper bound of free space on page */
801573Srgrimes	indx_t	linp[1];		/* indx_t-aligned VAR. LENGTH DATA */
811573Srgrimes} PAGE;
821573Srgrimes
831573Srgrimes/* First and next index. */
8414272Spst#define	BTDATAOFF							\
8514272Spst	(sizeof(pgno_t) + sizeof(pgno_t) + sizeof(pgno_t) +		\
8614272Spst	    sizeof(u_int32_t) + sizeof(indx_t) + sizeof(indx_t))
871573Srgrimes#define	NEXTINDEX(p)	(((p)->lower - BTDATAOFF) / sizeof(indx_t))
881573Srgrimes
891573Srgrimes/*
901573Srgrimes * For pages other than overflow pages, there is an array of offsets into the
911573Srgrimes * rest of the page immediately following the page header.  Each offset is to
921573Srgrimes * an item which is unique to the type of page.  The h_lower offset is just
931573Srgrimes * past the last filled-in index.  The h_upper offset is the first item on the
941573Srgrimes * page.  Offsets are from the beginning of the page.
951573Srgrimes *
961573Srgrimes * If an item is too big to store on a single page, a flag is set and the item
971573Srgrimes * is a { page, size } pair such that the page is the first page of an overflow
981573Srgrimes * chain with size bytes of item.  Overflow pages are simply bytes without any
991573Srgrimes * external structure.
1001573Srgrimes *
1011573Srgrimes * The page number and size fields in the items are pgno_t-aligned so they can
1021573Srgrimes * be manipulated without copying.  (This presumes that 32 bit items can be
1031573Srgrimes * manipulated on this system.)
1041573Srgrimes */
10514272Spst#define	LALIGN(n)	(((n) + sizeof(pgno_t) - 1) & ~(sizeof(pgno_t) - 1))
10614272Spst#define	NOVFLSIZE	(sizeof(pgno_t) + sizeof(u_int32_t))
1071573Srgrimes
1081573Srgrimes/*
1091573Srgrimes * For the btree internal pages, the item is a key.  BINTERNALs are {key, pgno}
1101573Srgrimes * pairs, such that the key compares less than or equal to all of the records
1111573Srgrimes * on that page.  For a tree without duplicate keys, an internal page with two
1121573Srgrimes * consecutive keys, a and b, will have all records greater than or equal to a
1131573Srgrimes * and less than b stored on the page associated with a.  Duplicate keys are
1141573Srgrimes * somewhat special and can cause duplicate internal and leaf page records and
1151573Srgrimes * some minor modifications of the above rule.
1161573Srgrimes */
1171573Srgrimestypedef struct _binternal {
11814272Spst	u_int32_t ksize;		/* key size */
1191573Srgrimes	pgno_t	pgno;			/* page number stored on */
1201573Srgrimes#define	P_BIGDATA	0x01		/* overflow data */
1211573Srgrimes#define	P_BIGKEY	0x02		/* overflow key */
1221573Srgrimes	u_char	flags;
1231573Srgrimes	char	bytes[1];		/* data */
1241573Srgrimes} BINTERNAL;
1251573Srgrimes
1261573Srgrimes/* Get the page's BINTERNAL structure at index indx. */
12714272Spst#define	GETBINTERNAL(pg, indx)						\
1281573Srgrimes	((BINTERNAL *)((char *)(pg) + (pg)->linp[indx]))
1291573Srgrimes
1301573Srgrimes/* Get the number of bytes in the entry. */
13114272Spst#define NBINTERNAL(len)							\
13214272Spst	LALIGN(sizeof(u_int32_t) + sizeof(pgno_t) + sizeof(u_char) + (len))
1331573Srgrimes
1341573Srgrimes/* Copy a BINTERNAL entry to the page. */
13514272Spst#define	WR_BINTERNAL(p, size, pgno, flags) {				\
13614272Spst	*(u_int32_t *)p = size;						\
13714272Spst	p += sizeof(u_int32_t);						\
13814272Spst	*(pgno_t *)p = pgno;						\
13914272Spst	p += sizeof(pgno_t);						\
14014272Spst	*(u_char *)p = flags;						\
14114272Spst	p += sizeof(u_char);						\
1421573Srgrimes}
1431573Srgrimes
1441573Srgrimes/*
1451573Srgrimes * For the recno internal pages, the item is a page number with the number of
1461573Srgrimes * keys found on that page and below.
1471573Srgrimes */
1481573Srgrimestypedef struct _rinternal {
1491573Srgrimes	recno_t	nrecs;			/* number of records */
1501573Srgrimes	pgno_t	pgno;			/* page number stored below */
1511573Srgrimes} RINTERNAL;
1521573Srgrimes
1531573Srgrimes/* Get the page's RINTERNAL structure at index indx. */
15414272Spst#define	GETRINTERNAL(pg, indx)						\
1551573Srgrimes	((RINTERNAL *)((char *)(pg) + (pg)->linp[indx]))
1561573Srgrimes
1571573Srgrimes/* Get the number of bytes in the entry. */
15814272Spst#define NRINTERNAL							\
1591573Srgrimes	LALIGN(sizeof(recno_t) + sizeof(pgno_t))
1601573Srgrimes
1611573Srgrimes/* Copy a RINTERAL entry to the page. */
16214272Spst#define	WR_RINTERNAL(p, nrecs, pgno) {					\
16314272Spst	*(recno_t *)p = nrecs;						\
16414272Spst	p += sizeof(recno_t);						\
16514272Spst	*(pgno_t *)p = pgno;						\
1661573Srgrimes}
1671573Srgrimes
1681573Srgrimes/* For the btree leaf pages, the item is a key and data pair. */
1691573Srgrimestypedef struct _bleaf {
17014272Spst	u_int32_t	ksize;		/* size of key */
17114272Spst	u_int32_t	dsize;		/* size of data */
1721573Srgrimes	u_char	flags;			/* P_BIGDATA, P_BIGKEY */
1731573Srgrimes	char	bytes[1];		/* data */
1741573Srgrimes} BLEAF;
1751573Srgrimes
1761573Srgrimes/* Get the page's BLEAF structure at index indx. */
17714272Spst#define	GETBLEAF(pg, indx)						\
1781573Srgrimes	((BLEAF *)((char *)(pg) + (pg)->linp[indx]))
1791573Srgrimes
1801573Srgrimes/* Get the number of bytes in the entry. */
1811573Srgrimes#define NBLEAF(p)	NBLEAFDBT((p)->ksize, (p)->dsize)
1821573Srgrimes
1831573Srgrimes/* Get the number of bytes in the user's key/data pair. */
18414272Spst#define NBLEAFDBT(ksize, dsize)						\
18514272Spst	LALIGN(sizeof(u_int32_t) + sizeof(u_int32_t) + sizeof(u_char) +	\
1861573Srgrimes	    (ksize) + (dsize))
1871573Srgrimes
1881573Srgrimes/* Copy a BLEAF entry to the page. */
18914272Spst#define	WR_BLEAF(p, key, data, flags) {					\
19014272Spst	*(u_int32_t *)p = key->size;					\
19114272Spst	p += sizeof(u_int32_t);						\
19214272Spst	*(u_int32_t *)p = data->size;					\
19314272Spst	p += sizeof(u_int32_t);						\
19414272Spst	*(u_char *)p = flags;						\
19514272Spst	p += sizeof(u_char);						\
19614272Spst	memmove(p, key->data, key->size);				\
19714272Spst	p += key->size;							\
19814272Spst	memmove(p, data->data, data->size);				\
1991573Srgrimes}
2001573Srgrimes
2011573Srgrimes/* For the recno leaf pages, the item is a data entry. */
2021573Srgrimestypedef struct _rleaf {
20314272Spst	u_int32_t	dsize;		/* size of data */
2041573Srgrimes	u_char	flags;			/* P_BIGDATA */
2051573Srgrimes	char	bytes[1];
2061573Srgrimes} RLEAF;
2071573Srgrimes
2081573Srgrimes/* Get the page's RLEAF structure at index indx. */
20914272Spst#define	GETRLEAF(pg, indx)						\
2101573Srgrimes	((RLEAF *)((char *)(pg) + (pg)->linp[indx]))
2111573Srgrimes
2121573Srgrimes/* Get the number of bytes in the entry. */
2131573Srgrimes#define NRLEAF(p)	NRLEAFDBT((p)->dsize)
2141573Srgrimes
2151573Srgrimes/* Get the number of bytes from the user's data. */
21614272Spst#define	NRLEAFDBT(dsize)						\
21714272Spst	LALIGN(sizeof(u_int32_t) + sizeof(u_char) + (dsize))
2181573Srgrimes
2191573Srgrimes/* Copy a RLEAF entry to the page. */
22014272Spst#define	WR_RLEAF(p, data, flags) {					\
22114272Spst	*(u_int32_t *)p = data->size;					\
22214272Spst	p += sizeof(u_int32_t);						\
22314272Spst	*(u_char *)p = flags;						\
22414272Spst	p += sizeof(u_char);						\
22514272Spst	memmove(p, data->data, data->size);				\
2261573Srgrimes}
2271573Srgrimes
2281573Srgrimes/*
2291573Srgrimes * A record in the tree is either a pointer to a page and an index in the page
2301573Srgrimes * or a page number and an index.  These structures are used as a cursor, stack
2311573Srgrimes * entry and search returns as well as to pass records to other routines.
2321573Srgrimes *
2331573Srgrimes * One comment about searches.  Internal page searches must find the largest
2341573Srgrimes * record less than key in the tree so that descents work.  Leaf page searches
2351573Srgrimes * must find the smallest record greater than key so that the returned index
2361573Srgrimes * is the record's correct position for insertion.
2371573Srgrimes */
2381573Srgrimestypedef struct _epgno {
2391573Srgrimes	pgno_t	pgno;			/* the page number */
2401573Srgrimes	indx_t	index;			/* the index on the page */
2411573Srgrimes} EPGNO;
2421573Srgrimes
2431573Srgrimestypedef struct _epg {
2441573Srgrimes	PAGE	*page;			/* the (pinned) page */
2451573Srgrimes	indx_t	 index;			/* the index on the page */
2461573Srgrimes} EPG;
2471573Srgrimes
2481573Srgrimes/*
24914272Spst * About cursors.  The cursor (and the page that contained the key/data pair
25014272Spst * that it referenced) can be deleted, which makes things a bit tricky.  If
25114272Spst * there are no duplicates of the cursor key in the tree (i.e. B_NODUPS is set
25214272Spst * or there simply aren't any duplicates of the key) we copy the key that it
25314272Spst * referenced when it's deleted, and reacquire a new cursor key if the cursor
25414272Spst * is used again.  If there are duplicates keys, we move to the next/previous
25514272Spst * key, and set a flag so that we know what happened.  NOTE: if duplicate (to
25614272Spst * the cursor) keys are added to the tree during this process, it is undefined
25714272Spst * if they will be returned or not in a cursor scan.
25814272Spst *
25914272Spst * The flags determine the possible states of the cursor:
26014272Spst *
26114272Spst * CURS_INIT	The cursor references *something*.
26214272Spst * CURS_ACQUIRE	The cursor was deleted, and a key has been saved so that
26314272Spst *		we can reacquire the right position in the tree.
26414272Spst * CURS_AFTER, CURS_BEFORE
26514272Spst *		The cursor was deleted, and now references a key/data pair
26614272Spst *		that has not yet been returned, either before or after the
26714272Spst *		deleted key/data pair.
26814272Spst * XXX
26914272Spst * This structure is broken out so that we can eventually offer multiple
27014272Spst * cursors as part of the DB interface.
27114272Spst */
27214272Spsttypedef struct _cursor {
27314272Spst	EPGNO	 pg;			/* B: Saved tree reference. */
27414272Spst	DBT	 key;			/* B: Saved key, or key.data == NULL. */
27514272Spst	recno_t	 rcursor;		/* R: recno cursor (1-based) */
27614272Spst
27714272Spst#define	CURS_ACQUIRE	0x01		/*  B: Cursor needs to be reacquired. */
27814272Spst#define	CURS_AFTER	0x02		/*  B: Unreturned cursor after key. */
27914272Spst#define	CURS_BEFORE	0x04		/*  B: Unreturned cursor before key. */
28014272Spst#define	CURS_INIT	0x08		/* RB: Cursor initialized. */
28114272Spst	u_int8_t flags;
28214272Spst} CURSOR;
28314272Spst
28414272Spst/*
28514272Spst * The metadata of the tree.  The nrecs field is used only by the RECNO code.
2861573Srgrimes * This is because the btree doesn't really need it and it requires that every
2871573Srgrimes * put or delete call modify the metadata.
2881573Srgrimes */
2891573Srgrimestypedef struct _btmeta {
29014272Spst	u_int32_t	magic;		/* magic number */
29114272Spst	u_int32_t	version;	/* version */
29214272Spst	u_int32_t	psize;		/* page size */
29314272Spst	u_int32_t	free;		/* page number of first free page */
29414272Spst	u_int32_t	nrecs;		/* R: number of records */
29514272Spst
2961573Srgrimes#define	SAVEMETA	(B_NODUPS | R_RECNO)
29714272Spst	u_int32_t	flags;		/* bt_flags & SAVEMETA */
2981573Srgrimes} BTMETA;
2991573Srgrimes
3001573Srgrimes/* The in-memory btree/recno data structure. */
3011573Srgrimestypedef struct _btree {
30214272Spst	MPOOL	 *bt_mp;		/* memory pool cookie */
3031573Srgrimes
30414272Spst	DB	 *bt_dbp;		/* pointer to enclosing DB */
3051573Srgrimes
30614272Spst	EPG	  bt_cur;		/* current (pinned) page */
30714272Spst	PAGE	 *bt_pinned;		/* page pinned across calls */
3081573Srgrimes
30914272Spst	CURSOR	  bt_cursor;		/* cursor */
3101573Srgrimes
31114272Spst#define	BT_PUSH(t, p, i) {						\
312189327Sdelphij	t->bt_sp->pgno = p;						\
313189327Sdelphij	t->bt_sp->index = i;						\
31414272Spst	++t->bt_sp;							\
31514272Spst}
31614272Spst#define	BT_POP(t)	(t->bt_sp == t->bt_stack ? NULL : --t->bt_sp)
31714272Spst#define	BT_CLR(t)	(t->bt_sp = t->bt_stack)
31814272Spst	EPGNO	  bt_stack[50];		/* stack of parent pages */
31914272Spst	EPGNO	 *bt_sp;		/* current stack pointer */
3201573Srgrimes
32114272Spst	DBT	  bt_rkey;		/* returned key */
32214272Spst	DBT	  bt_rdata;		/* returned data */
3231573Srgrimes
32414272Spst	int	  bt_fd;		/* tree file descriptor */
3251573Srgrimes
32614272Spst	pgno_t	  bt_free;		/* next free page */
3271573Srgrimes	u_int32_t bt_psize;		/* page size */
32814272Spst	indx_t	  bt_ovflsize;		/* cut-off for key/data overflow */
32914272Spst	int	  bt_lorder;		/* byte order */
3301573Srgrimes					/* sorted order */
3311573Srgrimes	enum { NOT, BACK, FORWARD } bt_order;
33214272Spst	EPGNO	  bt_last;		/* last insert */
3331573Srgrimes
3341573Srgrimes					/* B: key comparison function */
33592905Sobrien	int	(*bt_cmp)(const DBT *, const DBT *);
3361573Srgrimes					/* B: prefix comparison function */
33792905Sobrien	size_t	(*bt_pfx)(const DBT *, const DBT *);
3381573Srgrimes					/* R: recno input function */
33992905Sobrien	int	(*bt_irec)(struct _btree *, recno_t);
3401573Srgrimes
34114272Spst	FILE	 *bt_rfp;		/* R: record FILE pointer */
34214272Spst	int	  bt_rfd;		/* R: record file descriptor */
3431573Srgrimes
34414272Spst	caddr_t	  bt_cmap;		/* R: current point in mapped space */
34514272Spst	caddr_t	  bt_smap;		/* R: start of mapped space */
34614272Spst	caddr_t   bt_emap;		/* R: end of mapped space */
34714272Spst	size_t	  bt_msize;		/* R: size of mapped region. */
3481573Srgrimes
34914272Spst	recno_t	  bt_nrecs;		/* R: number of records */
35014272Spst	size_t	  bt_reclen;		/* R: fixed record length */
35114272Spst	u_char	  bt_bval;		/* R: delimiting byte/pad character */
3521573Srgrimes
3531573Srgrimes/*
3541573Srgrimes * NB:
3551573Srgrimes * B_NODUPS and R_RECNO are stored on disk, and may not be changed.
3561573Srgrimes */
35714272Spst#define	B_INMEM		0x00001		/* in-memory tree */
35814272Spst#define	B_METADIRTY	0x00002		/* need to write metadata */
35914272Spst#define	B_MODIFIED	0x00004		/* tree modified */
36014272Spst#define	B_NEEDSWAP	0x00008		/* if byte order requires swapping */
36114272Spst#define	B_RDONLY	0x00010		/* read-only tree */
36214272Spst
3631573Srgrimes#define	B_NODUPS	0x00020		/* no duplicate keys permitted */
3641573Srgrimes#define	R_RECNO		0x00080		/* record oriented tree */
3651573Srgrimes
36614272Spst#define	R_CLOSEFP	0x00040		/* opened a file pointer */
36714272Spst#define	R_EOF		0x00100		/* end of input file reached. */
36814272Spst#define	R_FIXLEN	0x00200		/* fixed length records */
36914272Spst#define	R_MEMMAPPED	0x00400		/* memory mapped file. */
37014272Spst#define	R_INMEM		0x00800		/* in-memory file */
37114272Spst#define	R_MODIFIED	0x01000		/* modified file */
37214272Spst#define	R_RDONLY	0x02000		/* read-only file */
3731573Srgrimes
37414272Spst#define	B_DB_LOCK	0x04000		/* DB_LOCK specified. */
37514272Spst#define	B_DB_SHMEM	0x08000		/* DB_SHMEM specified. */
37614272Spst#define	B_DB_TXN	0x10000		/* DB_TXN specified. */
37714272Spst	u_int32_t flags;
3781573Srgrimes} BTREE;
3791573Srgrimes
3801573Srgrimes#include "extern.h"
381