xref: /freebsd/lib/libc/db/btree/btree.h (revision ef5d438ed4bc17ad7ece3e40fe4d1f9baf3aadf7)
158f0484fSRodney W. Grimes /*-
2ef5d438eSPaul Traina  * Copyright (c) 1991, 1993, 1994
358f0484fSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
458f0484fSRodney W. Grimes  *
558f0484fSRodney W. Grimes  * This code is derived from software contributed to Berkeley by
658f0484fSRodney W. Grimes  * Mike Olson.
758f0484fSRodney W. Grimes  *
858f0484fSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
958f0484fSRodney W. Grimes  * modification, are permitted provided that the following conditions
1058f0484fSRodney W. Grimes  * are met:
1158f0484fSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
1258f0484fSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
1358f0484fSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
1458f0484fSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
1558f0484fSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
1658f0484fSRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
1758f0484fSRodney W. Grimes  *    must display the following acknowledgement:
1858f0484fSRodney W. Grimes  *	This product includes software developed by the University of
1958f0484fSRodney W. Grimes  *	California, Berkeley and its contributors.
2058f0484fSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
2158f0484fSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
2258f0484fSRodney W. Grimes  *    without specific prior written permission.
2358f0484fSRodney W. Grimes  *
2458f0484fSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2558f0484fSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2658f0484fSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2758f0484fSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2858f0484fSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2958f0484fSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3058f0484fSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3158f0484fSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3258f0484fSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3358f0484fSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3458f0484fSRodney W. Grimes  * SUCH DAMAGE.
3558f0484fSRodney W. Grimes  *
36ef5d438eSPaul Traina  *	@(#)btree.h	8.11 (Berkeley) 8/17/94
3758f0484fSRodney W. Grimes  */
3858f0484fSRodney W. Grimes 
39ef5d438eSPaul Traina /* Macros to set/clear/test flags. */
40ef5d438eSPaul Traina #define	F_SET(p, f)	(p)->flags |= (f)
41ef5d438eSPaul Traina #define	F_CLR(p, f)	(p)->flags &= ~(f)
42ef5d438eSPaul Traina #define	F_ISSET(p, f)	((p)->flags & (f))
43ef5d438eSPaul Traina 
4458f0484fSRodney W. Grimes #include <mpool.h>
4558f0484fSRodney W. Grimes 
4658f0484fSRodney W. Grimes #define	DEFMINKEYPAGE	(2)		/* Minimum keys per page */
4758f0484fSRodney W. Grimes #define	MINCACHE	(5)		/* Minimum cached pages */
4858f0484fSRodney W. Grimes #define	MINPSIZE	(512)		/* Minimum page size */
4958f0484fSRodney W. Grimes 
5058f0484fSRodney W. Grimes /*
5158f0484fSRodney W. Grimes  * Page 0 of a btree file contains a copy of the meta-data.  This page is also
5258f0484fSRodney W. Grimes  * used as an out-of-band page, i.e. page pointers that point to nowhere point
5358f0484fSRodney W. Grimes  * to page 0.  Page 1 is the root of the btree.
5458f0484fSRodney W. Grimes  */
5558f0484fSRodney W. Grimes #define	P_INVALID	 0		/* Invalid tree page number. */
5658f0484fSRodney W. Grimes #define	P_META		 0		/* Tree metadata page number. */
5758f0484fSRodney W. Grimes #define	P_ROOT		 1		/* Tree root page number. */
5858f0484fSRodney W. Grimes 
5958f0484fSRodney W. Grimes /*
6058f0484fSRodney W. Grimes  * There are five page layouts in the btree: btree internal pages (BINTERNAL),
6158f0484fSRodney W. Grimes  * btree leaf pages (BLEAF), recno internal pages (RINTERNAL), recno leaf pages
6258f0484fSRodney W. Grimes  * (RLEAF) and overflow pages.  All five page types have a page header (PAGE).
6358f0484fSRodney W. Grimes  * This implementation requires that values within structures NOT be padded.
6458f0484fSRodney W. Grimes  * (ANSI C permits random padding.)  If your compiler pads randomly you'll have
6558f0484fSRodney W. Grimes  * to do some work to get this package to run.
6658f0484fSRodney W. Grimes  */
6758f0484fSRodney W. Grimes typedef struct _page {
6858f0484fSRodney W. Grimes 	pgno_t	pgno;			/* this page's page number */
6958f0484fSRodney W. Grimes 	pgno_t	prevpg;			/* left sibling */
7058f0484fSRodney W. Grimes 	pgno_t	nextpg;			/* right sibling */
7158f0484fSRodney W. Grimes 
7258f0484fSRodney W. Grimes #define	P_BINTERNAL	0x01		/* btree internal page */
7358f0484fSRodney W. Grimes #define	P_BLEAF		0x02		/* leaf page */
7458f0484fSRodney W. Grimes #define	P_OVERFLOW	0x04		/* overflow page */
7558f0484fSRodney W. Grimes #define	P_RINTERNAL	0x08		/* recno internal page */
7658f0484fSRodney W. Grimes #define	P_RLEAF		0x10		/* leaf page */
7758f0484fSRodney W. Grimes #define P_TYPE		0x1f		/* type mask */
7858f0484fSRodney W. Grimes #define	P_PRESERVE	0x20		/* never delete this chain of pages */
7958f0484fSRodney W. Grimes 	u_int32_t flags;
8058f0484fSRodney W. Grimes 
8158f0484fSRodney W. Grimes 	indx_t	lower;			/* lower bound of free space on page */
8258f0484fSRodney W. Grimes 	indx_t	upper;			/* upper bound of free space on page */
8358f0484fSRodney W. Grimes 	indx_t	linp[1];		/* indx_t-aligned VAR. LENGTH DATA */
8458f0484fSRodney W. Grimes } PAGE;
8558f0484fSRodney W. Grimes 
8658f0484fSRodney W. Grimes /* First and next index. */
87ef5d438eSPaul Traina #define	BTDATAOFF							\
88ef5d438eSPaul Traina 	(sizeof(pgno_t) + sizeof(pgno_t) + sizeof(pgno_t) +		\
8958f0484fSRodney W. Grimes 	    sizeof(u_int32_t) + sizeof(indx_t) + sizeof(indx_t))
9058f0484fSRodney W. Grimes #define	NEXTINDEX(p)	(((p)->lower - BTDATAOFF) / sizeof(indx_t))
9158f0484fSRodney W. Grimes 
9258f0484fSRodney W. Grimes /*
9358f0484fSRodney W. Grimes  * For pages other than overflow pages, there is an array of offsets into the
9458f0484fSRodney W. Grimes  * rest of the page immediately following the page header.  Each offset is to
9558f0484fSRodney W. Grimes  * an item which is unique to the type of page.  The h_lower offset is just
9658f0484fSRodney W. Grimes  * past the last filled-in index.  The h_upper offset is the first item on the
9758f0484fSRodney W. Grimes  * page.  Offsets are from the beginning of the page.
9858f0484fSRodney W. Grimes  *
9958f0484fSRodney W. Grimes  * If an item is too big to store on a single page, a flag is set and the item
10058f0484fSRodney W. Grimes  * is a { page, size } pair such that the page is the first page of an overflow
10158f0484fSRodney W. Grimes  * chain with size bytes of item.  Overflow pages are simply bytes without any
10258f0484fSRodney W. Grimes  * external structure.
10358f0484fSRodney W. Grimes  *
10458f0484fSRodney W. Grimes  * The page number and size fields in the items are pgno_t-aligned so they can
10558f0484fSRodney W. Grimes  * be manipulated without copying.  (This presumes that 32 bit items can be
10658f0484fSRodney W. Grimes  * manipulated on this system.)
10758f0484fSRodney W. Grimes  */
108ef5d438eSPaul Traina #define	LALIGN(n)	(((n) + sizeof(pgno_t) - 1) & ~(sizeof(pgno_t) - 1))
109ef5d438eSPaul Traina #define	NOVFLSIZE	(sizeof(pgno_t) + sizeof(u_int32_t))
11058f0484fSRodney W. Grimes 
11158f0484fSRodney W. Grimes /*
11258f0484fSRodney W. Grimes  * For the btree internal pages, the item is a key.  BINTERNALs are {key, pgno}
11358f0484fSRodney W. Grimes  * pairs, such that the key compares less than or equal to all of the records
11458f0484fSRodney W. Grimes  * on that page.  For a tree without duplicate keys, an internal page with two
11558f0484fSRodney W. Grimes  * consecutive keys, a and b, will have all records greater than or equal to a
11658f0484fSRodney W. Grimes  * and less than b stored on the page associated with a.  Duplicate keys are
11758f0484fSRodney W. Grimes  * somewhat special and can cause duplicate internal and leaf page records and
11858f0484fSRodney W. Grimes  * some minor modifications of the above rule.
11958f0484fSRodney W. Grimes  */
12058f0484fSRodney W. Grimes typedef struct _binternal {
121ef5d438eSPaul Traina 	u_int32_t ksize;		/* key size */
12258f0484fSRodney W. Grimes 	pgno_t	pgno;			/* page number stored on */
12358f0484fSRodney W. Grimes #define	P_BIGDATA	0x01		/* overflow data */
12458f0484fSRodney W. Grimes #define	P_BIGKEY	0x02		/* overflow key */
12558f0484fSRodney W. Grimes 	u_char	flags;
12658f0484fSRodney W. Grimes 	char	bytes[1];		/* data */
12758f0484fSRodney W. Grimes } BINTERNAL;
12858f0484fSRodney W. Grimes 
12958f0484fSRodney W. Grimes /* Get the page's BINTERNAL structure at index indx. */
13058f0484fSRodney W. Grimes #define	GETBINTERNAL(pg, indx)						\
13158f0484fSRodney W. Grimes 	((BINTERNAL *)((char *)(pg) + (pg)->linp[indx]))
13258f0484fSRodney W. Grimes 
13358f0484fSRodney W. Grimes /* Get the number of bytes in the entry. */
13458f0484fSRodney W. Grimes #define NBINTERNAL(len)							\
135ef5d438eSPaul Traina 	LALIGN(sizeof(u_int32_t) + sizeof(pgno_t) + sizeof(u_char) + (len))
13658f0484fSRodney W. Grimes 
13758f0484fSRodney W. Grimes /* Copy a BINTERNAL entry to the page. */
13858f0484fSRodney W. Grimes #define	WR_BINTERNAL(p, size, pgno, flags) {				\
139ef5d438eSPaul Traina 	*(u_int32_t *)p = size;						\
140ef5d438eSPaul Traina 	p += sizeof(u_int32_t);						\
14158f0484fSRodney W. Grimes 	*(pgno_t *)p = pgno;						\
14258f0484fSRodney W. Grimes 	p += sizeof(pgno_t);						\
14358f0484fSRodney W. Grimes 	*(u_char *)p = flags;						\
14458f0484fSRodney W. Grimes 	p += sizeof(u_char);						\
14558f0484fSRodney W. Grimes }
14658f0484fSRodney W. Grimes 
14758f0484fSRodney W. Grimes /*
14858f0484fSRodney W. Grimes  * For the recno internal pages, the item is a page number with the number of
14958f0484fSRodney W. Grimes  * keys found on that page and below.
15058f0484fSRodney W. Grimes  */
15158f0484fSRodney W. Grimes typedef struct _rinternal {
15258f0484fSRodney W. Grimes 	recno_t	nrecs;			/* number of records */
15358f0484fSRodney W. Grimes 	pgno_t	pgno;			/* page number stored below */
15458f0484fSRodney W. Grimes } RINTERNAL;
15558f0484fSRodney W. Grimes 
15658f0484fSRodney W. Grimes /* Get the page's RINTERNAL structure at index indx. */
15758f0484fSRodney W. Grimes #define	GETRINTERNAL(pg, indx)						\
15858f0484fSRodney W. Grimes 	((RINTERNAL *)((char *)(pg) + (pg)->linp[indx]))
15958f0484fSRodney W. Grimes 
16058f0484fSRodney W. Grimes /* Get the number of bytes in the entry. */
16158f0484fSRodney W. Grimes #define NRINTERNAL							\
16258f0484fSRodney W. Grimes 	LALIGN(sizeof(recno_t) + sizeof(pgno_t))
16358f0484fSRodney W. Grimes 
16458f0484fSRodney W. Grimes /* Copy a RINTERAL entry to the page. */
16558f0484fSRodney W. Grimes #define	WR_RINTERNAL(p, nrecs, pgno) {					\
16658f0484fSRodney W. Grimes 	*(recno_t *)p = nrecs;						\
16758f0484fSRodney W. Grimes 	p += sizeof(recno_t);						\
16858f0484fSRodney W. Grimes 	*(pgno_t *)p = pgno;						\
16958f0484fSRodney W. Grimes }
17058f0484fSRodney W. Grimes 
17158f0484fSRodney W. Grimes /* For the btree leaf pages, the item is a key and data pair. */
17258f0484fSRodney W. Grimes typedef struct _bleaf {
173ef5d438eSPaul Traina 	u_int32_t	ksize;		/* size of key */
174ef5d438eSPaul Traina 	u_int32_t	dsize;		/* size of data */
17558f0484fSRodney W. Grimes 	u_char	flags;			/* P_BIGDATA, P_BIGKEY */
17658f0484fSRodney W. Grimes 	char	bytes[1];		/* data */
17758f0484fSRodney W. Grimes } BLEAF;
17858f0484fSRodney W. Grimes 
17958f0484fSRodney W. Grimes /* Get the page's BLEAF structure at index indx. */
18058f0484fSRodney W. Grimes #define	GETBLEAF(pg, indx)						\
18158f0484fSRodney W. Grimes 	((BLEAF *)((char *)(pg) + (pg)->linp[indx]))
18258f0484fSRodney W. Grimes 
18358f0484fSRodney W. Grimes /* Get the number of bytes in the entry. */
18458f0484fSRodney W. Grimes #define NBLEAF(p)	NBLEAFDBT((p)->ksize, (p)->dsize)
18558f0484fSRodney W. Grimes 
18658f0484fSRodney W. Grimes /* Get the number of bytes in the user's key/data pair. */
18758f0484fSRodney W. Grimes #define NBLEAFDBT(ksize, dsize)						\
188ef5d438eSPaul Traina 	LALIGN(sizeof(u_int32_t) + sizeof(u_int32_t) + sizeof(u_char) +	\
18958f0484fSRodney W. Grimes 	    (ksize) + (dsize))
19058f0484fSRodney W. Grimes 
19158f0484fSRodney W. Grimes /* Copy a BLEAF entry to the page. */
19258f0484fSRodney W. Grimes #define	WR_BLEAF(p, key, data, flags) {					\
193ef5d438eSPaul Traina 	*(u_int32_t *)p = key->size;					\
194ef5d438eSPaul Traina 	p += sizeof(u_int32_t);						\
195ef5d438eSPaul Traina 	*(u_int32_t *)p = data->size;					\
196ef5d438eSPaul Traina 	p += sizeof(u_int32_t);						\
19758f0484fSRodney W. Grimes 	*(u_char *)p = flags;						\
19858f0484fSRodney W. Grimes 	p += sizeof(u_char);						\
19958f0484fSRodney W. Grimes 	memmove(p, key->data, key->size);				\
20058f0484fSRodney W. Grimes 	p += key->size;							\
20158f0484fSRodney W. Grimes 	memmove(p, data->data, data->size);				\
20258f0484fSRodney W. Grimes }
20358f0484fSRodney W. Grimes 
20458f0484fSRodney W. Grimes /* For the recno leaf pages, the item is a data entry. */
20558f0484fSRodney W. Grimes typedef struct _rleaf {
206ef5d438eSPaul Traina 	u_int32_t	dsize;		/* size of data */
20758f0484fSRodney W. Grimes 	u_char	flags;			/* P_BIGDATA */
20858f0484fSRodney W. Grimes 	char	bytes[1];
20958f0484fSRodney W. Grimes } RLEAF;
21058f0484fSRodney W. Grimes 
21158f0484fSRodney W. Grimes /* Get the page's RLEAF structure at index indx. */
21258f0484fSRodney W. Grimes #define	GETRLEAF(pg, indx)						\
21358f0484fSRodney W. Grimes 	((RLEAF *)((char *)(pg) + (pg)->linp[indx]))
21458f0484fSRodney W. Grimes 
21558f0484fSRodney W. Grimes /* Get the number of bytes in the entry. */
21658f0484fSRodney W. Grimes #define NRLEAF(p)	NRLEAFDBT((p)->dsize)
21758f0484fSRodney W. Grimes 
21858f0484fSRodney W. Grimes /* Get the number of bytes from the user's data. */
21958f0484fSRodney W. Grimes #define	NRLEAFDBT(dsize)						\
220ef5d438eSPaul Traina 	LALIGN(sizeof(u_int32_t) + sizeof(u_char) + (dsize))
22158f0484fSRodney W. Grimes 
22258f0484fSRodney W. Grimes /* Copy a RLEAF entry to the page. */
22358f0484fSRodney W. Grimes #define	WR_RLEAF(p, data, flags) {					\
224ef5d438eSPaul Traina 	*(u_int32_t *)p = data->size;					\
225ef5d438eSPaul Traina 	p += sizeof(u_int32_t);						\
22658f0484fSRodney W. Grimes 	*(u_char *)p = flags;						\
22758f0484fSRodney W. Grimes 	p += sizeof(u_char);						\
22858f0484fSRodney W. Grimes 	memmove(p, data->data, data->size);				\
22958f0484fSRodney W. Grimes }
23058f0484fSRodney W. Grimes 
23158f0484fSRodney W. Grimes /*
23258f0484fSRodney W. Grimes  * A record in the tree is either a pointer to a page and an index in the page
23358f0484fSRodney W. Grimes  * or a page number and an index.  These structures are used as a cursor, stack
23458f0484fSRodney W. Grimes  * entry and search returns as well as to pass records to other routines.
23558f0484fSRodney W. Grimes  *
23658f0484fSRodney W. Grimes  * One comment about searches.  Internal page searches must find the largest
23758f0484fSRodney W. Grimes  * record less than key in the tree so that descents work.  Leaf page searches
23858f0484fSRodney W. Grimes  * must find the smallest record greater than key so that the returned index
23958f0484fSRodney W. Grimes  * is the record's correct position for insertion.
24058f0484fSRodney W. Grimes  */
24158f0484fSRodney W. Grimes typedef struct _epgno {
24258f0484fSRodney W. Grimes 	pgno_t	pgno;			/* the page number */
24358f0484fSRodney W. Grimes 	indx_t	index;			/* the index on the page */
24458f0484fSRodney W. Grimes } EPGNO;
24558f0484fSRodney W. Grimes 
24658f0484fSRodney W. Grimes typedef struct _epg {
24758f0484fSRodney W. Grimes 	PAGE	*page;			/* the (pinned) page */
24858f0484fSRodney W. Grimes 	indx_t	 index;			/* the index on the page */
24958f0484fSRodney W. Grimes } EPG;
25058f0484fSRodney W. Grimes 
25158f0484fSRodney W. Grimes /*
252ef5d438eSPaul Traina  * About cursors.  The cursor (and the page that contained the key/data pair
253ef5d438eSPaul Traina  * that it referenced) can be deleted, which makes things a bit tricky.  If
254ef5d438eSPaul Traina  * there are no duplicates of the cursor key in the tree (i.e. B_NODUPS is set
255ef5d438eSPaul Traina  * or there simply aren't any duplicates of the key) we copy the key that it
256ef5d438eSPaul Traina  * referenced when it's deleted, and reacquire a new cursor key if the cursor
257ef5d438eSPaul Traina  * is used again.  If there are duplicates keys, we move to the next/previous
258ef5d438eSPaul Traina  * key, and set a flag so that we know what happened.  NOTE: if duplicate (to
259ef5d438eSPaul Traina  * the cursor) keys are added to the tree during this process, it is undefined
260ef5d438eSPaul Traina  * if they will be returned or not in a cursor scan.
261ef5d438eSPaul Traina  *
262ef5d438eSPaul Traina  * The flags determine the possible states of the cursor:
263ef5d438eSPaul Traina  *
264ef5d438eSPaul Traina  * CURS_INIT	The cursor references *something*.
265ef5d438eSPaul Traina  * CURS_ACQUIRE	The cursor was deleted, and a key has been saved so that
266ef5d438eSPaul Traina  *		we can reacquire the right position in the tree.
267ef5d438eSPaul Traina  * CURS_AFTER, CURS_BEFORE
268ef5d438eSPaul Traina  *		The cursor was deleted, and now references a key/data pair
269ef5d438eSPaul Traina  *		that has not yet been returned, either before or after the
270ef5d438eSPaul Traina  *		deleted key/data pair.
271ef5d438eSPaul Traina  * XXX
272ef5d438eSPaul Traina  * This structure is broken out so that we can eventually offer multiple
273ef5d438eSPaul Traina  * cursors as part of the DB interface.
274ef5d438eSPaul Traina  */
275ef5d438eSPaul Traina typedef struct _cursor {
276ef5d438eSPaul Traina 	EPGNO	 pg;			/* B: Saved tree reference. */
277ef5d438eSPaul Traina 	DBT	 key;			/* B: Saved key, or key.data == NULL. */
278ef5d438eSPaul Traina 	recno_t	 rcursor;		/* R: recno cursor (1-based) */
279ef5d438eSPaul Traina 
280ef5d438eSPaul Traina #define	CURS_ACQUIRE	0x01		/*  B: Cursor needs to be reacquired. */
281ef5d438eSPaul Traina #define	CURS_AFTER	0x02		/*  B: Unreturned cursor after key. */
282ef5d438eSPaul Traina #define	CURS_BEFORE	0x04		/*  B: Unreturned cursor before key. */
283ef5d438eSPaul Traina #define	CURS_INIT	0x08		/* RB: Cursor initialized. */
284ef5d438eSPaul Traina 	u_int8_t flags;
285ef5d438eSPaul Traina } CURSOR;
286ef5d438eSPaul Traina 
287ef5d438eSPaul Traina /*
288ef5d438eSPaul Traina  * The metadata of the tree.  The nrecs field is used only by the RECNO code.
28958f0484fSRodney W. Grimes  * This is because the btree doesn't really need it and it requires that every
29058f0484fSRodney W. Grimes  * put or delete call modify the metadata.
29158f0484fSRodney W. Grimes  */
29258f0484fSRodney W. Grimes typedef struct _btmeta {
293ef5d438eSPaul Traina 	u_int32_t	magic;		/* magic number */
294ef5d438eSPaul Traina 	u_int32_t	version;	/* version */
295ef5d438eSPaul Traina 	u_int32_t	psize;		/* page size */
296ef5d438eSPaul Traina 	u_int32_t	free;		/* page number of first free page */
297ef5d438eSPaul Traina 	u_int32_t	nrecs;		/* R: number of records */
298ef5d438eSPaul Traina 
29958f0484fSRodney W. Grimes #define	SAVEMETA	(B_NODUPS | R_RECNO)
300ef5d438eSPaul Traina 	u_int32_t	flags;		/* bt_flags & SAVEMETA */
30158f0484fSRodney W. Grimes } BTMETA;
30258f0484fSRodney W. Grimes 
30358f0484fSRodney W. Grimes /* The in-memory btree/recno data structure. */
30458f0484fSRodney W. Grimes typedef struct _btree {
30558f0484fSRodney W. Grimes 	MPOOL	 *bt_mp;		/* memory pool cookie */
30658f0484fSRodney W. Grimes 
30758f0484fSRodney W. Grimes 	DB	 *bt_dbp;		/* pointer to enclosing DB */
30858f0484fSRodney W. Grimes 
30958f0484fSRodney W. Grimes 	EPG	  bt_cur;		/* current (pinned) page */
31058f0484fSRodney W. Grimes 	PAGE	 *bt_pinned;		/* page pinned across calls */
31158f0484fSRodney W. Grimes 
312ef5d438eSPaul Traina 	CURSOR	  bt_cursor;		/* cursor */
31358f0484fSRodney W. Grimes 
314ef5d438eSPaul Traina #define	BT_PUSH(t, p, i) {						\
315ef5d438eSPaul Traina 	t->bt_sp->pgno = p; 						\
316ef5d438eSPaul Traina 	t->bt_sp->index = i; 						\
317ef5d438eSPaul Traina 	++t->bt_sp;							\
318ef5d438eSPaul Traina }
319ef5d438eSPaul Traina #define	BT_POP(t)	(t->bt_sp == t->bt_stack ? NULL : --t->bt_sp)
320ef5d438eSPaul Traina #define	BT_CLR(t)	(t->bt_sp = t->bt_stack)
321ef5d438eSPaul Traina 	EPGNO	  bt_stack[50];		/* stack of parent pages */
322ef5d438eSPaul Traina 	EPGNO	 *bt_sp;		/* current stack pointer */
32358f0484fSRodney W. Grimes 
324ef5d438eSPaul Traina 	DBT	  bt_rkey;		/* returned key */
325ef5d438eSPaul Traina 	DBT	  bt_rdata;		/* returned data */
32658f0484fSRodney W. Grimes 
32758f0484fSRodney W. Grimes 	int	  bt_fd;		/* tree file descriptor */
32858f0484fSRodney W. Grimes 
32958f0484fSRodney W. Grimes 	pgno_t	  bt_free;		/* next free page */
33058f0484fSRodney W. Grimes 	u_int32_t bt_psize;		/* page size */
33158f0484fSRodney W. Grimes 	indx_t	  bt_ovflsize;		/* cut-off for key/data overflow */
33258f0484fSRodney W. Grimes 	int	  bt_lorder;		/* byte order */
33358f0484fSRodney W. Grimes 					/* sorted order */
33458f0484fSRodney W. Grimes 	enum { NOT, BACK, FORWARD } bt_order;
33558f0484fSRodney W. Grimes 	EPGNO	  bt_last;		/* last insert */
33658f0484fSRodney W. Grimes 
33758f0484fSRodney W. Grimes 					/* B: key comparison function */
33858f0484fSRodney W. Grimes 	int	(*bt_cmp) __P((const DBT *, const DBT *));
33958f0484fSRodney W. Grimes 					/* B: prefix comparison function */
34058f0484fSRodney W. Grimes 	size_t	(*bt_pfx) __P((const DBT *, const DBT *));
34158f0484fSRodney W. Grimes 					/* R: recno input function */
34258f0484fSRodney W. Grimes 	int	(*bt_irec) __P((struct _btree *, recno_t));
34358f0484fSRodney W. Grimes 
34458f0484fSRodney W. Grimes 	FILE	 *bt_rfp;		/* R: record FILE pointer */
34558f0484fSRodney W. Grimes 	int	  bt_rfd;		/* R: record file descriptor */
34658f0484fSRodney W. Grimes 
34758f0484fSRodney W. Grimes 	caddr_t	  bt_cmap;		/* R: current point in mapped space */
34858f0484fSRodney W. Grimes 	caddr_t	  bt_smap;		/* R: start of mapped space */
34958f0484fSRodney W. Grimes 	caddr_t   bt_emap;		/* R: end of mapped space */
35058f0484fSRodney W. Grimes 	size_t	  bt_msize;		/* R: size of mapped region. */
35158f0484fSRodney W. Grimes 
35258f0484fSRodney W. Grimes 	recno_t	  bt_nrecs;		/* R: number of records */
35358f0484fSRodney W. Grimes 	size_t	  bt_reclen;		/* R: fixed record length */
35458f0484fSRodney W. Grimes 	u_char	  bt_bval;		/* R: delimiting byte/pad character */
35558f0484fSRodney W. Grimes 
35658f0484fSRodney W. Grimes /*
35758f0484fSRodney W. Grimes  * NB:
35858f0484fSRodney W. Grimes  * B_NODUPS and R_RECNO are stored on disk, and may not be changed.
35958f0484fSRodney W. Grimes  */
360ef5d438eSPaul Traina #define	B_INMEM		0x00001		/* in-memory tree */
361ef5d438eSPaul Traina #define	B_METADIRTY	0x00002		/* need to write metadata */
362ef5d438eSPaul Traina #define	B_MODIFIED	0x00004		/* tree modified */
363ef5d438eSPaul Traina #define	B_NEEDSWAP	0x00008		/* if byte order requires swapping */
364ef5d438eSPaul Traina #define	B_RDONLY	0x00010		/* read-only tree */
365ef5d438eSPaul Traina 
36658f0484fSRodney W. Grimes #define	B_NODUPS	0x00020		/* no duplicate keys permitted */
36758f0484fSRodney W. Grimes #define	R_RECNO		0x00080		/* record oriented tree */
36858f0484fSRodney W. Grimes 
369ef5d438eSPaul Traina #define	R_CLOSEFP	0x00040		/* opened a file pointer */
370ef5d438eSPaul Traina #define	R_EOF		0x00100		/* end of input file reached. */
371ef5d438eSPaul Traina #define	R_FIXLEN	0x00200		/* fixed length records */
372ef5d438eSPaul Traina #define	R_MEMMAPPED	0x00400		/* memory mapped file. */
373ef5d438eSPaul Traina #define	R_INMEM		0x00800		/* in-memory file */
374ef5d438eSPaul Traina #define	R_MODIFIED	0x01000		/* modified file */
375ef5d438eSPaul Traina #define	R_RDONLY	0x02000		/* read-only file */
37658f0484fSRodney W. Grimes 
377ef5d438eSPaul Traina #define	B_DB_LOCK	0x04000		/* DB_LOCK specified. */
378ef5d438eSPaul Traina #define	B_DB_SHMEM	0x08000		/* DB_SHMEM specified. */
379ef5d438eSPaul Traina #define	B_DB_TXN	0x10000		/* DB_TXN specified. */
380ef5d438eSPaul Traina 	u_int32_t flags;
38158f0484fSRodney W. Grimes } BTREE;
38258f0484fSRodney W. Grimes 
38358f0484fSRodney W. Grimes #include "extern.h"
384