xref: /minix3/lib/libc/db/btree/btree.h (revision 84d9c625bfea59e274550651111ae9edfdc40fbd)
1*84d9c625SLionel Sambuc /*	$NetBSD: btree.h,v 1.17 2013/09/04 13:03:22 ryoon Exp $	*/
22639ae9bSBen Gras 
32639ae9bSBen Gras /*-
42639ae9bSBen Gras  * Copyright (c) 1991, 1993, 1994
52639ae9bSBen Gras  *	The Regents of the University of California.  All rights reserved.
62639ae9bSBen Gras  *
72639ae9bSBen Gras  * This code is derived from software contributed to Berkeley by
82639ae9bSBen Gras  * Mike Olson.
92639ae9bSBen Gras  *
102639ae9bSBen Gras  * Redistribution and use in source and binary forms, with or without
112639ae9bSBen Gras  * modification, are permitted provided that the following conditions
122639ae9bSBen Gras  * are met:
132639ae9bSBen Gras  * 1. Redistributions of source code must retain the above copyright
142639ae9bSBen Gras  *    notice, this list of conditions and the following disclaimer.
152639ae9bSBen Gras  * 2. Redistributions in binary form must reproduce the above copyright
162639ae9bSBen Gras  *    notice, this list of conditions and the following disclaimer in the
172639ae9bSBen Gras  *    documentation and/or other materials provided with the distribution.
182639ae9bSBen Gras  * 3. Neither the name of the University nor the names of its contributors
192639ae9bSBen Gras  *    may be used to endorse or promote products derived from this software
202639ae9bSBen Gras  *    without specific prior written permission.
212639ae9bSBen Gras  *
222639ae9bSBen Gras  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
232639ae9bSBen Gras  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
242639ae9bSBen Gras  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
252639ae9bSBen Gras  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
262639ae9bSBen Gras  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
272639ae9bSBen Gras  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
282639ae9bSBen Gras  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
292639ae9bSBen Gras  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
302639ae9bSBen Gras  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
312639ae9bSBen Gras  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
322639ae9bSBen Gras  * SUCH DAMAGE.
332639ae9bSBen Gras  *
342639ae9bSBen Gras  *	@(#)btree.h	8.11 (Berkeley) 8/17/94
352639ae9bSBen Gras  */
362639ae9bSBen Gras 
372639ae9bSBen Gras #if HAVE_NBTOOL_CONFIG_H
382639ae9bSBen Gras #include "nbtool_config.h"
392639ae9bSBen Gras #endif
402639ae9bSBen Gras 
412639ae9bSBen Gras /* Macros to set/clear/test flags. */
422639ae9bSBen Gras #define	F_SET(p, f)	(p)->flags |= (f)
432639ae9bSBen Gras #define	F_CLR(p, f)	(p)->flags &= ~(f)
442639ae9bSBen Gras #define	F_ISSET(p, f)	((p)->flags & (f))
452639ae9bSBen Gras 
462639ae9bSBen Gras #include <mpool.h>
472639ae9bSBen Gras 
482639ae9bSBen Gras #define	DEFMINKEYPAGE	(2)		/* Minimum keys per page */
492639ae9bSBen Gras #define	MINCACHE	(5)		/* Minimum cached pages */
502639ae9bSBen Gras #define	MINPSIZE	(512)		/* Minimum page size */
512639ae9bSBen Gras 
522639ae9bSBen Gras /*
532639ae9bSBen Gras  * Page 0 of a btree file contains a copy of the meta-data.  This page is also
542639ae9bSBen Gras  * used as an out-of-band page, i.e. page pointers that point to nowhere point
552639ae9bSBen Gras  * to page 0.  Page 1 is the root of the btree.
562639ae9bSBen Gras  */
572639ae9bSBen Gras #define	P_INVALID	 0		/* Invalid tree page number. */
582639ae9bSBen Gras #define	P_META		 0		/* Tree metadata page number. */
592639ae9bSBen Gras #define	P_ROOT		 1		/* Tree root page number. */
602639ae9bSBen Gras 
612639ae9bSBen Gras /*
622639ae9bSBen Gras  * There are five page layouts in the btree: btree internal pages (BINTERNAL),
632639ae9bSBen Gras  * btree leaf pages (BLEAF), recno internal pages (RINTERNAL), recno leaf pages
642639ae9bSBen Gras  * (RLEAF) and overflow pages.  All five page types have a page header (PAGE).
652639ae9bSBen Gras  * This implementation requires that values within structures NOT be padded.
662639ae9bSBen Gras  * (ANSI C permits random padding.)  If your compiler pads randomly you'll have
672639ae9bSBen Gras  * to do some work to get this package to run.
682639ae9bSBen Gras  */
692639ae9bSBen Gras typedef struct _page {
702639ae9bSBen Gras 	pgno_t	pgno;			/* this page's page number */
712639ae9bSBen Gras 	pgno_t	prevpg;			/* left sibling */
722639ae9bSBen Gras 	pgno_t	nextpg;			/* right sibling */
732639ae9bSBen Gras 
742639ae9bSBen Gras #define	P_BINTERNAL	0x01		/* btree internal page */
752639ae9bSBen Gras #define	P_BLEAF		0x02		/* leaf page */
762639ae9bSBen Gras #define	P_OVERFLOW	0x04		/* overflow page */
772639ae9bSBen Gras #define	P_RINTERNAL	0x08		/* recno internal page */
782639ae9bSBen Gras #define	P_RLEAF		0x10		/* leaf page */
792639ae9bSBen Gras #define P_TYPE		0x1f		/* type mask */
802639ae9bSBen Gras #define	P_PRESERVE	0x20		/* never delete this chain of pages */
812639ae9bSBen Gras 	uint32_t flags;
822639ae9bSBen Gras 
832639ae9bSBen Gras 	indx_t	lower;			/* lower bound of free space on page */
842639ae9bSBen Gras 	indx_t	upper;			/* upper bound of free space on page */
852639ae9bSBen Gras 	indx_t	linp[1];		/* indx_t-aligned VAR. LENGTH DATA */
862639ae9bSBen Gras } PAGE;
872639ae9bSBen Gras 
882639ae9bSBen Gras /* First and next index. */
892639ae9bSBen Gras #define	BTDATAOFF							\
902639ae9bSBen Gras 	(sizeof(pgno_t) + sizeof(pgno_t) + sizeof(pgno_t) +		\
912639ae9bSBen Gras 	    sizeof(uint32_t) + sizeof(indx_t) + sizeof(indx_t))
922639ae9bSBen Gras 
932639ae9bSBen Gras #define	_NEXTINDEX(p) (((p)->lower - BTDATAOFF) / sizeof(indx_t))
942639ae9bSBen Gras #ifdef _DIAGNOSTIC
952639ae9bSBen Gras static __inline indx_t
NEXTINDEX(const PAGE * p)962639ae9bSBen Gras NEXTINDEX(const PAGE *p) {
972639ae9bSBen Gras 	size_t x = _NEXTINDEX(p);
982639ae9bSBen Gras 	_DBFIT(x, indx_t);
992639ae9bSBen Gras 	return (indx_t)x;
1002639ae9bSBen Gras }
1012639ae9bSBen Gras #else
1022639ae9bSBen Gras #define	NEXTINDEX(p) (indx_t)_NEXTINDEX(p)
1032639ae9bSBen Gras #endif
1042639ae9bSBen Gras 
1052639ae9bSBen Gras /*
1062639ae9bSBen Gras  * For pages other than overflow pages, there is an array of offsets into the
1072639ae9bSBen Gras  * rest of the page immediately following the page header.  Each offset is to
1082639ae9bSBen Gras  * an item which is unique to the type of page.  The h_lower offset is just
1092639ae9bSBen Gras  * past the last filled-in index.  The h_upper offset is the first item on the
1102639ae9bSBen Gras  * page.  Offsets are from the beginning of the page.
1112639ae9bSBen Gras  *
1122639ae9bSBen Gras  * If an item is too big to store on a single page, a flag is set and the item
1132639ae9bSBen Gras  * is a { page, size } pair such that the page is the first page of an overflow
1142639ae9bSBen Gras  * chain with size bytes of item.  Overflow pages are simply bytes without any
1152639ae9bSBen Gras  * external structure.
1162639ae9bSBen Gras  *
1172639ae9bSBen Gras  * The page number and size fields in the items are pgno_t-aligned so they can
1182639ae9bSBen Gras  * be manipulated without copying.  (This presumes that 32 bit items can be
1192639ae9bSBen Gras  * manipulated on this system.)
1202639ae9bSBen Gras  */
1212639ae9bSBen Gras #define	BTLALIGN(n)	(((n) + sizeof(pgno_t) - 1) & ~(sizeof(pgno_t) - 1))
1222639ae9bSBen Gras #define	NOVFLSIZE	(sizeof(pgno_t) + sizeof(uint32_t))
1232639ae9bSBen Gras 
1242639ae9bSBen Gras /*
1252639ae9bSBen Gras  * For the btree internal pages, the item is a key.  BINTERNALs are {key, pgno}
1262639ae9bSBen Gras  * pairs, such that the key compares less than or equal to all of the records
1272639ae9bSBen Gras  * on that page.  For a tree without duplicate keys, an internal page with two
1282639ae9bSBen Gras  * consecutive keys, a and b, will have all records greater than or equal to a
1292639ae9bSBen Gras  * and less than b stored on the page associated with a.  Duplicate keys are
1302639ae9bSBen Gras  * somewhat special and can cause duplicate internal and leaf page records and
1312639ae9bSBen Gras  * some minor modifications of the above rule.
1322639ae9bSBen Gras  */
1332639ae9bSBen Gras typedef struct _binternal {
1342639ae9bSBen Gras 	uint32_t ksize;			/* key size */
1352639ae9bSBen Gras 	pgno_t	pgno;			/* page number stored on */
1362639ae9bSBen Gras #define	P_BIGDATA	0x01		/* overflow data */
1372639ae9bSBen Gras #define	P_BIGKEY	0x02		/* overflow key */
1382639ae9bSBen Gras 	uint8_t	flags;
1392639ae9bSBen Gras 	char	bytes[1];		/* data */
1402639ae9bSBen Gras } BINTERNAL;
1412639ae9bSBen Gras 
1422639ae9bSBen Gras /* Get the page's BINTERNAL structure at index indx. */
1432639ae9bSBen Gras #define	GETBINTERNAL(pg, indx)						\
1442639ae9bSBen Gras 	((BINTERNAL *)(void *)((char *)(void *)(pg) + (pg)->linp[indx]))
1452639ae9bSBen Gras 
1462639ae9bSBen Gras /* Get the number of bytes in the entry. */
1472639ae9bSBen Gras #define _NBINTERNAL(len)						\
1482639ae9bSBen Gras     BTLALIGN(sizeof(uint32_t) + sizeof(pgno_t) + sizeof(uint8_t) + (len))
1492639ae9bSBen Gras #ifdef _DIAGNOSTIC
1502639ae9bSBen Gras static __inline uint32_t
NBINTERNAL(uint32_t len)1512639ae9bSBen Gras NBINTERNAL(uint32_t len) {
1522639ae9bSBen Gras 	size_t x = _NBINTERNAL(len);
1532639ae9bSBen Gras 	_DBFIT(x, uint32_t);
1542639ae9bSBen Gras 	return (uint32_t)x;
1552639ae9bSBen Gras }
1562639ae9bSBen Gras #else
1572639ae9bSBen Gras #define NBINTERNAL(len)	(uint32_t)_NBINTERNAL(len)
1582639ae9bSBen Gras #endif
1592639ae9bSBen Gras 
1602639ae9bSBen Gras /* Copy a BINTERNAL entry to the page. */
1612639ae9bSBen Gras #define	WR_BINTERNAL(p, size, pgno, flags) do {				\
1622639ae9bSBen Gras 	_DBFIT(size, uint32_t);						\
1632639ae9bSBen Gras 	*(uint32_t *)(void *)p = (uint32_t)size;			\
1642639ae9bSBen Gras 	p += sizeof(uint32_t);						\
1652639ae9bSBen Gras 	*(pgno_t *)(void *)p = pgno;					\
1662639ae9bSBen Gras 	p += sizeof(pgno_t);						\
1672639ae9bSBen Gras 	*(uint8_t *)(void *)p = flags;					\
1682639ae9bSBen Gras 	p += sizeof(uint8_t);						\
1692639ae9bSBen Gras } while (/*CONSTCOND*/0)
1702639ae9bSBen Gras 
1712639ae9bSBen Gras /*
1722639ae9bSBen Gras  * For the recno internal pages, the item is a page number with the number of
1732639ae9bSBen Gras  * keys found on that page and below.
1742639ae9bSBen Gras  */
1752639ae9bSBen Gras typedef struct _rinternal {
1762639ae9bSBen Gras 	recno_t	nrecs;			/* number of records */
1772639ae9bSBen Gras 	pgno_t	pgno;			/* page number stored below */
1782639ae9bSBen Gras } RINTERNAL;
1792639ae9bSBen Gras 
1802639ae9bSBen Gras /* Get the page's RINTERNAL structure at index indx. */
1812639ae9bSBen Gras #define	GETRINTERNAL(pg, indx)						\
1822639ae9bSBen Gras 	((RINTERNAL *)(void *)((char *)(void *)(pg) + (pg)->linp[indx]))
1832639ae9bSBen Gras 
1842639ae9bSBen Gras /* Get the number of bytes in the entry. */
1852639ae9bSBen Gras #define NRINTERNAL							\
1862639ae9bSBen Gras 	BTLALIGN(sizeof(recno_t) + sizeof(pgno_t))
1872639ae9bSBen Gras 
188*84d9c625SLionel Sambuc /* Copy a RINTERNAL entry to the page. */
1892639ae9bSBen Gras #define	WR_RINTERNAL(p, nrecs, pgno) do {				\
1902639ae9bSBen Gras 	*(recno_t *)(void *)p = nrecs;					\
1912639ae9bSBen Gras 	p += sizeof(recno_t);						\
1922639ae9bSBen Gras 	*(pgno_t *)(void *)p = pgno;					\
1932639ae9bSBen Gras } while (/*CONSTCOND*/0)
1942639ae9bSBen Gras 
1952639ae9bSBen Gras /* For the btree leaf pages, the item is a key and data pair. */
1962639ae9bSBen Gras typedef struct _bleaf {
1972639ae9bSBen Gras 	uint32_t	ksize;		/* size of key */
1982639ae9bSBen Gras 	uint32_t	dsize;		/* size of data */
1992639ae9bSBen Gras 	uint8_t	flags;			/* P_BIGDATA, P_BIGKEY */
2002639ae9bSBen Gras 	char	bytes[1];		/* data */
2012639ae9bSBen Gras } BLEAF;
2022639ae9bSBen Gras 
2032639ae9bSBen Gras /* Get the page's BLEAF structure at index indx. */
2042639ae9bSBen Gras #define	GETBLEAF(pg, indx)						\
2052639ae9bSBen Gras 	((BLEAF *)(void *)((char *)(void *)(pg) + (pg)->linp[indx]))
2062639ae9bSBen Gras 
2072639ae9bSBen Gras 
2082639ae9bSBen Gras /* Get the number of bytes in the user's key/data pair. */
2092639ae9bSBen Gras #define _NBLEAFDBT(ksize, dsize)					\
2102639ae9bSBen Gras     BTLALIGN(sizeof(uint32_t) + sizeof(uint32_t) + sizeof(uint8_t) +	\
2112639ae9bSBen Gras 	    (ksize) + (dsize))
2122639ae9bSBen Gras #ifdef _DIAGNOSTIC
2132639ae9bSBen Gras static __inline uint32_t
NBLEAFDBT(size_t k,size_t d)2142639ae9bSBen Gras NBLEAFDBT(size_t k, size_t d) {
2152639ae9bSBen Gras 	size_t x = _NBLEAFDBT(k, d);
2162639ae9bSBen Gras 	_DBFIT(x, uint32_t);
2172639ae9bSBen Gras 	return (uint32_t)x;
2182639ae9bSBen Gras }
2192639ae9bSBen Gras #else
2202639ae9bSBen Gras #define NBLEAFDBT(p, q)	(uint32_t)_NBLEAFDBT(p, q)
2212639ae9bSBen Gras #endif
2222639ae9bSBen Gras 
2232639ae9bSBen Gras /* Get the number of bytes in the entry. */
2242639ae9bSBen Gras #define NBLEAF(p)	NBLEAFDBT((p)->ksize, (p)->dsize)
2252639ae9bSBen Gras 
2262639ae9bSBen Gras /* Copy a BLEAF entry to the page. */
2272639ae9bSBen Gras #define	WR_BLEAF(p, key, data, flags) do {				\
2282639ae9bSBen Gras 	_DBFIT(key->size, uint32_t);					\
2292639ae9bSBen Gras 	*(uint32_t *)(void *)p = (uint32_t)key->size;			\
2302639ae9bSBen Gras 	p += sizeof(uint32_t);						\
2312639ae9bSBen Gras 	_DBFIT(data->size, uint32_t);					\
2322639ae9bSBen Gras 	*(uint32_t *)(void *)p = (uint32_t)data->size;			\
2332639ae9bSBen Gras 	p += sizeof(uint32_t);						\
2342639ae9bSBen Gras 	*(uint8_t *)(void *)p = flags;					\
2352639ae9bSBen Gras 	p += sizeof(uint8_t);						\
2362639ae9bSBen Gras 	(void)memmove(p, key->data, key->size);				\
2372639ae9bSBen Gras 	p += key->size;							\
2382639ae9bSBen Gras 	(void)memmove(p, data->data, data->size);			\
2392639ae9bSBen Gras } while (/*CONSTCOND*/0)
2402639ae9bSBen Gras 
2412639ae9bSBen Gras /* For the recno leaf pages, the item is a data entry. */
2422639ae9bSBen Gras typedef struct _rleaf {
2432639ae9bSBen Gras 	uint32_t	dsize;		/* size of data */
2442639ae9bSBen Gras 	uint8_t	flags;			/* P_BIGDATA */
2452639ae9bSBen Gras 	char	bytes[1];
2462639ae9bSBen Gras } RLEAF;
2472639ae9bSBen Gras 
2482639ae9bSBen Gras /* Get the page's RLEAF structure at index indx. */
2492639ae9bSBen Gras #define	GETRLEAF(pg, indx)						\
2502639ae9bSBen Gras 	((RLEAF *)(void *)((char *)(void *)(pg) + (pg)->linp[indx]))
2512639ae9bSBen Gras 
2522639ae9bSBen Gras #define	_NRLEAFDBT(dsize)						\
2532639ae9bSBen Gras 	BTLALIGN(sizeof(uint32_t) + sizeof(uint8_t) + (dsize))
2542639ae9bSBen Gras 
2552639ae9bSBen Gras #ifdef _DIAGNOSTIC
2562639ae9bSBen Gras static __inline uint32_t
NRLEAFDBT(size_t d)2572639ae9bSBen Gras NRLEAFDBT(size_t d) {
2582639ae9bSBen Gras 	size_t x = _NRLEAFDBT(d);
2592639ae9bSBen Gras 	_DBFIT(x, uint32_t);
2602639ae9bSBen Gras 	return (uint32_t)x;
2612639ae9bSBen Gras }
2622639ae9bSBen Gras #else
2632639ae9bSBen Gras #define NRLEAFDBT(d)	(uint32_t)_NRLEAFDBT(d)
2642639ae9bSBen Gras #endif
2652639ae9bSBen Gras 
2662639ae9bSBen Gras /* Get the number of bytes in the entry. */
2672639ae9bSBen Gras #define NRLEAF(p)	NRLEAFDBT((p)->dsize)
2682639ae9bSBen Gras 
2692639ae9bSBen Gras /* Get the number of bytes from the user's data. */
2702639ae9bSBen Gras 
2712639ae9bSBen Gras /* Copy a RLEAF entry to the page. */
2722639ae9bSBen Gras #define	WR_RLEAF(p, data, flags) do {					\
2732639ae9bSBen Gras 	_DBFIT(data->size, uint32_t);					\
2742639ae9bSBen Gras 	*(uint32_t *)(void *)p = (uint32_t)data->size;			\
2752639ae9bSBen Gras 	p += sizeof(uint32_t);						\
2762639ae9bSBen Gras 	*(uint8_t *)(void *)p = flags;					\
2772639ae9bSBen Gras 	p += sizeof(uint8_t);						\
2782639ae9bSBen Gras 	memmove(p, data->data, data->size);				\
2792639ae9bSBen Gras } while (/*CONSTCOND*/0)
2802639ae9bSBen Gras 
2812639ae9bSBen Gras /*
2822639ae9bSBen Gras  * A record in the tree is either a pointer to a page and an index in the page
2832639ae9bSBen Gras  * or a page number and an index.  These structures are used as a cursor, stack
2842639ae9bSBen Gras  * entry and search returns as well as to pass records to other routines.
2852639ae9bSBen Gras  *
2862639ae9bSBen Gras  * One comment about searches.  Internal page searches must find the largest
2872639ae9bSBen Gras  * record less than key in the tree so that descents work.  Leaf page searches
2882639ae9bSBen Gras  * must find the smallest record greater than key so that the returned index
2892639ae9bSBen Gras  * is the record's correct position for insertion.
2902639ae9bSBen Gras  */
2912639ae9bSBen Gras typedef struct _epgno {
2922639ae9bSBen Gras 	pgno_t	pgno;			/* the page number */
2932639ae9bSBen Gras 	indx_t	index;			/* the index on the page */
2942639ae9bSBen Gras } EPGNO;
2952639ae9bSBen Gras 
2962639ae9bSBen Gras typedef struct _epg {
2972639ae9bSBen Gras 	PAGE	*page;			/* the (pinned) page */
2982639ae9bSBen Gras 	indx_t	 index;			/* the index on the page */
2992639ae9bSBen Gras } EPG;
3002639ae9bSBen Gras 
3012639ae9bSBen Gras /*
3022639ae9bSBen Gras  * About cursors.  The cursor (and the page that contained the key/data pair
3032639ae9bSBen Gras  * that it referenced) can be deleted, which makes things a bit tricky.  If
3042639ae9bSBen Gras  * there are no duplicates of the cursor key in the tree (i.e. B_NODUPS is set
3052639ae9bSBen Gras  * or there simply aren't any duplicates of the key) we copy the key that it
3062639ae9bSBen Gras  * referenced when it's deleted, and reacquire a new cursor key if the cursor
3072639ae9bSBen Gras  * is used again.  If there are duplicates keys, we move to the next/previous
3082639ae9bSBen Gras  * key, and set a flag so that we know what happened.  NOTE: if duplicate (to
3092639ae9bSBen Gras  * the cursor) keys are added to the tree during this process, it is undefined
3102639ae9bSBen Gras  * if they will be returned or not in a cursor scan.
3112639ae9bSBen Gras  *
3122639ae9bSBen Gras  * The flags determine the possible states of the cursor:
3132639ae9bSBen Gras  *
3142639ae9bSBen Gras  * CURS_INIT	The cursor references *something*.
3152639ae9bSBen Gras  * CURS_ACQUIRE	The cursor was deleted, and a key has been saved so that
3162639ae9bSBen Gras  *		we can reacquire the right position in the tree.
3172639ae9bSBen Gras  * CURS_AFTER, CURS_BEFORE
3182639ae9bSBen Gras  *		The cursor was deleted, and now references a key/data pair
3192639ae9bSBen Gras  *		that has not yet been returned, either before or after the
3202639ae9bSBen Gras  *		deleted key/data pair.
3212639ae9bSBen Gras  * XXX
3222639ae9bSBen Gras  * This structure is broken out so that we can eventually offer multiple
3232639ae9bSBen Gras  * cursors as part of the DB interface.
3242639ae9bSBen Gras  */
3252639ae9bSBen Gras typedef struct _cursor {
3262639ae9bSBen Gras 	EPGNO	 pg;			/* B: Saved tree reference. */
3272639ae9bSBen Gras 	DBT	 key;			/* B: Saved key, or key.data == NULL. */
3282639ae9bSBen Gras 	recno_t	 rcursor;		/* R: recno cursor (1-based) */
3292639ae9bSBen Gras 
3302639ae9bSBen Gras #define	CURS_ACQUIRE	0x01		/*  B: Cursor needs to be reacquired. */
3312639ae9bSBen Gras #define	CURS_AFTER	0x02		/*  B: Unreturned cursor after key. */
3322639ae9bSBen Gras #define	CURS_BEFORE	0x04		/*  B: Unreturned cursor before key. */
3332639ae9bSBen Gras #define	CURS_INIT	0x08		/* RB: Cursor initialized. */
3342639ae9bSBen Gras 	uint8_t flags;
3352639ae9bSBen Gras } CURSOR;
3362639ae9bSBen Gras 
3372639ae9bSBen Gras /*
3382639ae9bSBen Gras  * The metadata of the tree.  The nrecs field is used only by the RECNO code.
3392639ae9bSBen Gras  * This is because the btree doesn't really need it and it requires that every
3402639ae9bSBen Gras  * put or delete call modify the metadata.
3412639ae9bSBen Gras  */
3422639ae9bSBen Gras typedef struct _btmeta {
3432639ae9bSBen Gras 	uint32_t	magic;		/* magic number */
3442639ae9bSBen Gras 	uint32_t	version;	/* version */
3452639ae9bSBen Gras 	uint32_t	psize;		/* page size */
3462639ae9bSBen Gras 	uint32_t	free;		/* page number of first free page */
3472639ae9bSBen Gras 	uint32_t	nrecs;		/* R: number of records */
3482639ae9bSBen Gras 
3492639ae9bSBen Gras #define	SAVEMETA	(B_NODUPS | R_RECNO)
3502639ae9bSBen Gras 	uint32_t	flags;		/* bt_flags & SAVEMETA */
3512639ae9bSBen Gras } BTMETA;
3522639ae9bSBen Gras 
3532639ae9bSBen Gras /* The in-memory btree/recno data structure. */
3542639ae9bSBen Gras typedef struct _btree {
3552639ae9bSBen Gras 	MPOOL	 *bt_mp;		/* memory pool cookie */
3562639ae9bSBen Gras 
3572639ae9bSBen Gras 	DB	 *bt_dbp;		/* pointer to enclosing DB */
3582639ae9bSBen Gras 
3592639ae9bSBen Gras 	EPG	  bt_cur;		/* current (pinned) page */
3602639ae9bSBen Gras 	PAGE	 *bt_pinned;		/* page pinned across calls */
3612639ae9bSBen Gras 
3622639ae9bSBen Gras 	CURSOR	  bt_cursor;		/* cursor */
3632639ae9bSBen Gras 
3642639ae9bSBen Gras #define	BT_PUSH(t, p, i) {						\
3652639ae9bSBen Gras 	t->bt_sp->pgno = p; 						\
3662639ae9bSBen Gras 	t->bt_sp->index = i; 						\
3672639ae9bSBen Gras 	++t->bt_sp;							\
3682639ae9bSBen Gras }
3692639ae9bSBen Gras #define	BT_POP(t)	(t->bt_sp == t->bt_stack ? NULL : --t->bt_sp)
3702639ae9bSBen Gras #define	BT_CLR(t)	(t->bt_sp = t->bt_stack)
3712639ae9bSBen Gras 	EPGNO	  bt_stack[50];		/* stack of parent pages */
3722639ae9bSBen Gras 	EPGNO	 *bt_sp;		/* current stack pointer */
3732639ae9bSBen Gras 
3742639ae9bSBen Gras 	DBT	  bt_rkey;		/* returned key */
3752639ae9bSBen Gras 	DBT	  bt_rdata;		/* returned data */
3762639ae9bSBen Gras 
3772639ae9bSBen Gras 	int	  bt_fd;		/* tree file descriptor */
3782639ae9bSBen Gras 
3792639ae9bSBen Gras 	pgno_t	  bt_free;		/* next free page */
3802639ae9bSBen Gras 	uint32_t bt_psize;		/* page size */
3812639ae9bSBen Gras 	indx_t	  bt_ovflsize;		/* cut-off for key/data overflow */
3822639ae9bSBen Gras 	int	  bt_lorder;		/* byte order */
3832639ae9bSBen Gras 					/* sorted order */
3842639ae9bSBen Gras 	enum { NOT, BACK, FORWARD } bt_order;
3852639ae9bSBen Gras 	EPGNO	  bt_last;		/* last insert */
3862639ae9bSBen Gras 
3872639ae9bSBen Gras 					/* B: key comparison function */
3882639ae9bSBen Gras 	int	(*bt_cmp)(const DBT *, const DBT *);
3892639ae9bSBen Gras 					/* B: prefix comparison function */
3902639ae9bSBen Gras 	size_t	(*bt_pfx)(const DBT *, const DBT *);
3912639ae9bSBen Gras 					/* R: recno input function */
3922639ae9bSBen Gras 	int	(*bt_irec)(struct _btree *, recno_t);
3932639ae9bSBen Gras 
3942639ae9bSBen Gras 	FILE	 *bt_rfp;		/* R: record FILE pointer */
3952639ae9bSBen Gras 	int	  bt_rfd;		/* R: record file descriptor */
3962639ae9bSBen Gras 
3972639ae9bSBen Gras 	caddr_t	  bt_cmap;		/* R: current point in mapped space */
3982639ae9bSBen Gras 	caddr_t	  bt_smap;		/* R: start of mapped space */
3992639ae9bSBen Gras 	caddr_t   bt_emap;		/* R: end of mapped space */
4002639ae9bSBen Gras 	size_t	  bt_msize;		/* R: size of mapped region. */
4012639ae9bSBen Gras 
4022639ae9bSBen Gras 	recno_t	  bt_nrecs;		/* R: number of records */
4032639ae9bSBen Gras 	size_t	  bt_reclen;		/* R: fixed record length */
4042639ae9bSBen Gras 	uint8_t	  bt_bval;		/* R: delimiting byte/pad character */
4052639ae9bSBen Gras 
4062639ae9bSBen Gras /*
4072639ae9bSBen Gras  * NB:
4082639ae9bSBen Gras  * B_NODUPS and R_RECNO are stored on disk, and may not be changed.
4092639ae9bSBen Gras  */
4102639ae9bSBen Gras #define	B_INMEM		0x00001		/* in-memory tree */
4112639ae9bSBen Gras #define	B_METADIRTY	0x00002		/* need to write metadata */
4122639ae9bSBen Gras #define	B_MODIFIED	0x00004		/* tree modified */
4132639ae9bSBen Gras #define	B_NEEDSWAP	0x00008		/* if byte order requires swapping */
4142639ae9bSBen Gras #define	B_RDONLY	0x00010		/* read-only tree */
4152639ae9bSBen Gras 
4162639ae9bSBen Gras #define	B_NODUPS	0x00020		/* no duplicate keys permitted */
4172639ae9bSBen Gras #define	R_RECNO		0x00080		/* record oriented tree */
4182639ae9bSBen Gras 
4192639ae9bSBen Gras #define	R_CLOSEFP	0x00040		/* opened a file pointer */
4202639ae9bSBen Gras #define	R_EOF		0x00100		/* end of input file reached. */
4212639ae9bSBen Gras #define	R_FIXLEN	0x00200		/* fixed length records */
4222639ae9bSBen Gras #define	R_MEMMAPPED	0x00400		/* memory mapped file. */
4232639ae9bSBen Gras #define	R_INMEM		0x00800		/* in-memory file */
4242639ae9bSBen Gras #define	R_MODIFIED	0x01000		/* modified file */
4252639ae9bSBen Gras #define	R_RDONLY	0x02000		/* read-only file */
4262639ae9bSBen Gras 
4272639ae9bSBen Gras #define	B_DB_LOCK	0x04000		/* DB_LOCK specified. */
4282639ae9bSBen Gras #define	B_DB_SHMEM	0x08000		/* DB_SHMEM specified. */
4292639ae9bSBen Gras #define	B_DB_TXN	0x10000		/* DB_TXN specified. */
4302639ae9bSBen Gras 	uint32_t flags;
4312639ae9bSBen Gras } BTREE;
4322639ae9bSBen Gras 
4332639ae9bSBen Gras #include "extern.h"
434