17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * Copyright (c) 1997-2000 by Sun Microsystems, Inc.
37c478bd9Sstevel@tonic-gate  * All rights reserved.
47c478bd9Sstevel@tonic-gate  */
57c478bd9Sstevel@tonic-gate 
67c478bd9Sstevel@tonic-gate #ifndef _KRB5_BTREE_H
77c478bd9Sstevel@tonic-gate #define	_KRB5_BTREE_H
87c478bd9Sstevel@tonic-gate 
97c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
107c478bd9Sstevel@tonic-gate extern "C" {
117c478bd9Sstevel@tonic-gate #endif
127c478bd9Sstevel@tonic-gate 
137c478bd9Sstevel@tonic-gate 
147c478bd9Sstevel@tonic-gate /*-
157c478bd9Sstevel@tonic-gate  * Copyright (c) 1991, 1993, 1994
167c478bd9Sstevel@tonic-gate  *	The Regents of the University of California.  All rights reserved.
177c478bd9Sstevel@tonic-gate  *
187c478bd9Sstevel@tonic-gate  * This code is derived from software contributed to Berkeley by
197c478bd9Sstevel@tonic-gate  * Mike Olson.
207c478bd9Sstevel@tonic-gate  *
217c478bd9Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
227c478bd9Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
237c478bd9Sstevel@tonic-gate  * are met:
247c478bd9Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
257c478bd9Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
267c478bd9Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
277c478bd9Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
287c478bd9Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
297c478bd9Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this software
307c478bd9Sstevel@tonic-gate  *    must display the following acknowledgement:
317c478bd9Sstevel@tonic-gate  *	This product includes software developed by the University of
327c478bd9Sstevel@tonic-gate  *	California, Berkeley and its contributors.
337c478bd9Sstevel@tonic-gate  * 4. Neither the name of the University nor the names of its contributors
347c478bd9Sstevel@tonic-gate  *    may be used to endorse or promote products derived from this software
357c478bd9Sstevel@tonic-gate  *    without specific prior written permission.
367c478bd9Sstevel@tonic-gate  *
377c478bd9Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
387c478bd9Sstevel@tonic-gate  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
397c478bd9Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
407c478bd9Sstevel@tonic-gate  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
417c478bd9Sstevel@tonic-gate  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
427c478bd9Sstevel@tonic-gate  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
437c478bd9Sstevel@tonic-gate  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
447c478bd9Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
457c478bd9Sstevel@tonic-gate  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
467c478bd9Sstevel@tonic-gate  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
477c478bd9Sstevel@tonic-gate  * SUCH DAMAGE.
487c478bd9Sstevel@tonic-gate  *
497c478bd9Sstevel@tonic-gate  *	@(#)btree.h	8.11 (Berkeley) 8/17/94
507c478bd9Sstevel@tonic-gate  */
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate /* Macros to set/clear/test flags. */
537c478bd9Sstevel@tonic-gate #define	F_SET(p, f)	(p)->flags |= (f)
547c478bd9Sstevel@tonic-gate #define	F_CLR(p, f)	(p)->flags &= ~(f)
557c478bd9Sstevel@tonic-gate #define	F_ISSET(p, f)	((p)->flags & (f))
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate #include "mpool.h"
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate #define	DEFMINKEYPAGE	(2)		/* Minimum keys per page */
607c478bd9Sstevel@tonic-gate #define	MINCACHE	(5)		/* Minimum cached pages */
617c478bd9Sstevel@tonic-gate #define	MINPSIZE	(512)		/* Minimum page size */
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate /*
647c478bd9Sstevel@tonic-gate  * Page 0 of a btree file contains a copy of the meta-data.  This page is also
657c478bd9Sstevel@tonic-gate  * used as an out-of-band page, i.e. page pointers that point to nowhere point
667c478bd9Sstevel@tonic-gate  * to page 0.  Page 1 is the root of the btree.
677c478bd9Sstevel@tonic-gate  */
687c478bd9Sstevel@tonic-gate #define	P_INVALID	 0		/* Invalid tree page number. */
697c478bd9Sstevel@tonic-gate #define	P_META		 0		/* Tree metadata page number. */
707c478bd9Sstevel@tonic-gate #define	P_ROOT		 1		/* Tree root page number. */
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate /*
737c478bd9Sstevel@tonic-gate  * There are five page layouts in the btree: btree internal pages (BINTERNAL),
747c478bd9Sstevel@tonic-gate  * btree leaf pages (BLEAF), recno internal pages (RINTERNAL), recno leaf pages
757c478bd9Sstevel@tonic-gate  * (RLEAF) and overflow pages.  All five page types have a page header (PAGE).
767c478bd9Sstevel@tonic-gate  * This implementation requires that values within structures NOT be padded.
777c478bd9Sstevel@tonic-gate  * (ANSI C permits random padding.)  If your compiler pads randomly you'll have
787c478bd9Sstevel@tonic-gate  * to do some work to get this package to run.
797c478bd9Sstevel@tonic-gate  */
807c478bd9Sstevel@tonic-gate typedef struct _page {
817c478bd9Sstevel@tonic-gate 	db_pgno_t	pgno;			/* this page's page number */
827c478bd9Sstevel@tonic-gate 	db_pgno_t	prevpg;			/* left sibling */
837c478bd9Sstevel@tonic-gate 	db_pgno_t	nextpg;			/* right sibling */
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate #define	P_BINTERNAL	0x01		/* btree internal page */
867c478bd9Sstevel@tonic-gate #define	P_BLEAF		0x02		/* leaf page */
877c478bd9Sstevel@tonic-gate #define	P_OVERFLOW	0x04		/* overflow page */
887c478bd9Sstevel@tonic-gate #define	P_RINTERNAL	0x08		/* recno internal page */
897c478bd9Sstevel@tonic-gate #define	P_RLEAF		0x10		/* leaf page */
907c478bd9Sstevel@tonic-gate #define P_TYPE		0x1f		/* type mask */
917c478bd9Sstevel@tonic-gate #define	P_PRESERVE	0x20		/* never delete this chain of pages */
927c478bd9Sstevel@tonic-gate 	u_int32_t flags;
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate 	indx_t	lower;			/* lower bound of free space on page */
957c478bd9Sstevel@tonic-gate 	indx_t	upper;			/* upper bound of free space on page */
967c478bd9Sstevel@tonic-gate 	indx_t	linp[1];		/* indx_t-aligned VAR. LENGTH DATA */
977c478bd9Sstevel@tonic-gate } PAGE;
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate /* First and next index. */
1007c478bd9Sstevel@tonic-gate #define	BTDATAOFF							\
1017c478bd9Sstevel@tonic-gate 	(sizeof(db_pgno_t) + sizeof(db_pgno_t) + sizeof(db_pgno_t) +		\
1027c478bd9Sstevel@tonic-gate 	    sizeof(u_int32_t) + sizeof(indx_t) + sizeof(indx_t))
1037c478bd9Sstevel@tonic-gate #define	NEXTINDEX(p)	(((p)->lower - BTDATAOFF) / sizeof(indx_t))
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate /*
1067c478bd9Sstevel@tonic-gate  * For pages other than overflow pages, there is an array of offsets into the
1077c478bd9Sstevel@tonic-gate  * rest of the page immediately following the page header.  Each offset is to
1087c478bd9Sstevel@tonic-gate  * an item which is unique to the type of page.  The h_lower offset is just
1097c478bd9Sstevel@tonic-gate  * past the last filled-in index.  The h_upper offset is the first item on the
1107c478bd9Sstevel@tonic-gate  * page.  Offsets are from the beginning of the page.
1117c478bd9Sstevel@tonic-gate  *
1127c478bd9Sstevel@tonic-gate  * If an item is too big to store on a single page, a flag is set and the item
1137c478bd9Sstevel@tonic-gate  * is a { page, size } pair such that the page is the first page of an overflow
1147c478bd9Sstevel@tonic-gate  * chain with size bytes of item.  Overflow pages are simply bytes without any
1157c478bd9Sstevel@tonic-gate  * external structure.
1167c478bd9Sstevel@tonic-gate  *
1177c478bd9Sstevel@tonic-gate  * The page number and size fields in the items are db_pgno_t-aligned so they can
1187c478bd9Sstevel@tonic-gate  * be manipulated without copying.  (This presumes that 32 bit items can be
1197c478bd9Sstevel@tonic-gate  * manipulated on this system.)
1207c478bd9Sstevel@tonic-gate  */
1217c478bd9Sstevel@tonic-gate #define	LALIGN(n)	(((n) + sizeof(db_pgno_t) - 1) & ~(sizeof(db_pgno_t) - 1))
1227c478bd9Sstevel@tonic-gate #define	NOVFLSIZE	(sizeof(db_pgno_t) + sizeof(u_int32_t))
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate /*
1257c478bd9Sstevel@tonic-gate  * For the btree internal pages, the item is a key.  BINTERNALs are {key, pgno}
1267c478bd9Sstevel@tonic-gate  * pairs, such that the key compares less than or equal to all of the records
1277c478bd9Sstevel@tonic-gate  * on that page.  For a tree without duplicate keys, an internal page with two
1287c478bd9Sstevel@tonic-gate  * consecutive keys, a and b, will have all records greater than or equal to a
1297c478bd9Sstevel@tonic-gate  * and less than b stored on the page associated with a.  Duplicate keys are
1307c478bd9Sstevel@tonic-gate  * somewhat special and can cause duplicate internal and leaf page records and
1317c478bd9Sstevel@tonic-gate  * some minor modifications of the above rule.
1327c478bd9Sstevel@tonic-gate  */
1337c478bd9Sstevel@tonic-gate typedef struct _binternal {
1347c478bd9Sstevel@tonic-gate 	u_int32_t ksize;		/* key size */
1357c478bd9Sstevel@tonic-gate 	db_pgno_t	pgno;			/* page number stored on */
1367c478bd9Sstevel@tonic-gate #define	P_BIGDATA	0x01		/* overflow data */
1377c478bd9Sstevel@tonic-gate #define	P_BIGKEY	0x02		/* overflow key */
1387c478bd9Sstevel@tonic-gate 	u_char	flags;
1397c478bd9Sstevel@tonic-gate 	char	bytes[1];		/* data */
1407c478bd9Sstevel@tonic-gate } BINTERNAL;
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate /* Get the page's BINTERNAL structure at index indx. */
1437c478bd9Sstevel@tonic-gate #define	GETBINTERNAL(pg, indx)						\
1447c478bd9Sstevel@tonic-gate 	((BINTERNAL *)((char *)(pg) + (pg)->linp[indx]))
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate /* Get the number of bytes in the entry. */
1477c478bd9Sstevel@tonic-gate #define NBINTERNAL(len)							\
1487c478bd9Sstevel@tonic-gate 	LALIGN(sizeof(u_int32_t) + sizeof(db_pgno_t) + sizeof(u_char) + (len))
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate /* Copy a BINTERNAL entry to the page. */
1517c478bd9Sstevel@tonic-gate #define	WR_BINTERNAL(p, size, pgno, flags) {				\
1527c478bd9Sstevel@tonic-gate 	*(u_int32_t *)p = size;						\
1537c478bd9Sstevel@tonic-gate 	p += sizeof(u_int32_t);						\
1547c478bd9Sstevel@tonic-gate 	*(db_pgno_t *)p = pgno;						\
1557c478bd9Sstevel@tonic-gate 	p += sizeof(db_pgno_t);						\
1567c478bd9Sstevel@tonic-gate 	*(u_char *)p = flags;						\
1577c478bd9Sstevel@tonic-gate 	p += sizeof(u_char);						\
1587c478bd9Sstevel@tonic-gate }
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate /*
1617c478bd9Sstevel@tonic-gate  * For the recno internal pages, the item is a page number with the number of
1627c478bd9Sstevel@tonic-gate  * keys found on that page and below.
1637c478bd9Sstevel@tonic-gate  */
1647c478bd9Sstevel@tonic-gate typedef struct _rinternal {
1657c478bd9Sstevel@tonic-gate 	recno_t	nrecs;			/* number of records */
1667c478bd9Sstevel@tonic-gate 	db_pgno_t	pgno;			/* page number stored below */
1677c478bd9Sstevel@tonic-gate } RINTERNAL;
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate /* Get the page's RINTERNAL structure at index indx. */
1707c478bd9Sstevel@tonic-gate #define	GETRINTERNAL(pg, indx)						\
1717c478bd9Sstevel@tonic-gate 	((RINTERNAL *)((char *)(pg) + (pg)->linp[indx]))
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate /* Get the number of bytes in the entry. */
1747c478bd9Sstevel@tonic-gate #define NRINTERNAL							\
1757c478bd9Sstevel@tonic-gate 	LALIGN(sizeof(recno_t) + sizeof(db_pgno_t))
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate /* Copy a RINTERAL entry to the page. */
1787c478bd9Sstevel@tonic-gate #define	WR_RINTERNAL(p, nrecs, pgno) {					\
1797c478bd9Sstevel@tonic-gate 	*(recno_t *)p = nrecs;						\
1807c478bd9Sstevel@tonic-gate 	p += sizeof(recno_t);						\
1817c478bd9Sstevel@tonic-gate 	*(db_pgno_t *)p = pgno;						\
1827c478bd9Sstevel@tonic-gate }
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate /* For the btree leaf pages, the item is a key and data pair. */
1857c478bd9Sstevel@tonic-gate typedef struct _bleaf {
1867c478bd9Sstevel@tonic-gate 	u_int32_t	ksize;		/* size of key */
1877c478bd9Sstevel@tonic-gate 	u_int32_t	dsize;		/* size of data */
1887c478bd9Sstevel@tonic-gate 	u_char	flags;			/* P_BIGDATA, P_BIGKEY */
1897c478bd9Sstevel@tonic-gate 	char	bytes[1];		/* data */
1907c478bd9Sstevel@tonic-gate } BLEAF;
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate /* Get the page's BLEAF structure at index indx. */
1937c478bd9Sstevel@tonic-gate #define	GETBLEAF(pg, indx)						\
1947c478bd9Sstevel@tonic-gate 	((BLEAF *)((char *)(pg) + (pg)->linp[indx]))
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate /* Get the number of bytes in the entry. */
1977c478bd9Sstevel@tonic-gate #define NBLEAF(p)	NBLEAFDBT((p)->ksize, (p)->dsize)
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate /* Get the number of bytes in the user's key/data pair. */
2007c478bd9Sstevel@tonic-gate #define NBLEAFDBT(ksize, dsize)						\
2017c478bd9Sstevel@tonic-gate 	LALIGN(sizeof(u_int32_t) + sizeof(u_int32_t) + sizeof(u_char) +	\
2027c478bd9Sstevel@tonic-gate 	    (ksize) + (dsize))
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate /* Copy a BLEAF entry to the page. */
2057c478bd9Sstevel@tonic-gate #define	WR_BLEAF(p, key, data, flags) {					\
2067c478bd9Sstevel@tonic-gate 	*(u_int32_t *)p = key->size;					\
2077c478bd9Sstevel@tonic-gate 	p += sizeof(u_int32_t);						\
2087c478bd9Sstevel@tonic-gate 	*(u_int32_t *)p = data->size;					\
2097c478bd9Sstevel@tonic-gate 	p += sizeof(u_int32_t);						\
2107c478bd9Sstevel@tonic-gate 	*(u_char *)p = flags;						\
2117c478bd9Sstevel@tonic-gate 	p += sizeof(u_char);						\
2127c478bd9Sstevel@tonic-gate 	memmove(p, key->data, key->size);				\
2137c478bd9Sstevel@tonic-gate 	p += key->size;							\
2147c478bd9Sstevel@tonic-gate 	memmove(p, data->data, data->size);				\
2157c478bd9Sstevel@tonic-gate }
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate /* For the recno leaf pages, the item is a data entry. */
2187c478bd9Sstevel@tonic-gate typedef struct _rleaf {
2197c478bd9Sstevel@tonic-gate 	u_int32_t	dsize;		/* size of data */
2207c478bd9Sstevel@tonic-gate 	u_char	flags;			/* P_BIGDATA */
2217c478bd9Sstevel@tonic-gate 	char	bytes[1];
2227c478bd9Sstevel@tonic-gate } RLEAF;
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate /* Get the page's RLEAF structure at index indx. */
2257c478bd9Sstevel@tonic-gate #define	GETRLEAF(pg, indx)						\
2267c478bd9Sstevel@tonic-gate 	((RLEAF *)((char *)(pg) + (pg)->linp[indx]))
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate /* Get the number of bytes in the entry. */
2297c478bd9Sstevel@tonic-gate #define NRLEAF(p)	NRLEAFDBT((p)->dsize)
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate /* Get the number of bytes from the user's data. */
2327c478bd9Sstevel@tonic-gate #define	NRLEAFDBT(dsize)						\
2337c478bd9Sstevel@tonic-gate 	LALIGN(sizeof(u_int32_t) + sizeof(u_char) + (dsize))
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate /* Copy a RLEAF entry to the page. */
2367c478bd9Sstevel@tonic-gate #define	WR_RLEAF(p, data, flags) {					\
2377c478bd9Sstevel@tonic-gate 	*(u_int32_t *)p = data->size;					\
2387c478bd9Sstevel@tonic-gate 	p += sizeof(u_int32_t);						\
2397c478bd9Sstevel@tonic-gate 	*(u_char *)p = flags;						\
2407c478bd9Sstevel@tonic-gate 	p += sizeof(u_char);						\
2417c478bd9Sstevel@tonic-gate 	memmove(p, data->data, data->size);				\
2427c478bd9Sstevel@tonic-gate }
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate /*
2457c478bd9Sstevel@tonic-gate  * A record in the tree is either a pointer to a page and an index in the page
2467c478bd9Sstevel@tonic-gate  * or a page number and an index.  These structures are used as a cursor, stack
2477c478bd9Sstevel@tonic-gate  * entry and search returns as well as to pass records to other routines.
2487c478bd9Sstevel@tonic-gate  *
2497c478bd9Sstevel@tonic-gate  * One comment about searches.  Internal page searches must find the largest
2507c478bd9Sstevel@tonic-gate  * record less than key in the tree so that descents work.  Leaf page searches
2517c478bd9Sstevel@tonic-gate  * must find the smallest record greater than key so that the returned index
2527c478bd9Sstevel@tonic-gate  * is the record's correct position for insertion.
2537c478bd9Sstevel@tonic-gate  */
2547c478bd9Sstevel@tonic-gate typedef struct _epgno {
2557c478bd9Sstevel@tonic-gate 	db_pgno_t	pgno;			/* the page number */
2567c478bd9Sstevel@tonic-gate 	indx_t	index;			/* the index on the page */
2577c478bd9Sstevel@tonic-gate } EPGNO;
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate typedef struct _epg {
2607c478bd9Sstevel@tonic-gate 	PAGE	*page;			/* the (pinned) page */
2617c478bd9Sstevel@tonic-gate 	indx_t	 index;			/* the index on the page */
2627c478bd9Sstevel@tonic-gate } EPG;
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate /*
2657c478bd9Sstevel@tonic-gate  * About cursors.  The cursor (and the page that contained the key/data pair
2667c478bd9Sstevel@tonic-gate  * that it referenced) can be deleted, which makes things a bit tricky.  If
2677c478bd9Sstevel@tonic-gate  * there are no duplicates of the cursor key in the tree (i.e. B_NODUPS is set
2687c478bd9Sstevel@tonic-gate  * or there simply aren't any duplicates of the key) we copy the key that it
2697c478bd9Sstevel@tonic-gate  * referenced when it's deleted, and reacquire a new cursor key if the cursor
2707c478bd9Sstevel@tonic-gate  * is used again.  If there are duplicates keys, we move to the next/previous
2717c478bd9Sstevel@tonic-gate  * key, and set a flag so that we know what happened.  NOTE: if duplicate (to
2727c478bd9Sstevel@tonic-gate  * the cursor) keys are added to the tree during this process, it is undefined
2737c478bd9Sstevel@tonic-gate  * if they will be returned or not in a cursor scan.
2747c478bd9Sstevel@tonic-gate  *
2757c478bd9Sstevel@tonic-gate  * The flags determine the possible states of the cursor:
2767c478bd9Sstevel@tonic-gate  *
2777c478bd9Sstevel@tonic-gate  * CURS_INIT	The cursor references *something*.
2787c478bd9Sstevel@tonic-gate  * CURS_ACQUIRE	The cursor was deleted, and a key has been saved so that
2797c478bd9Sstevel@tonic-gate  *		we can reacquire the right position in the tree.
2807c478bd9Sstevel@tonic-gate  * CURS_AFTER, CURS_BEFORE
2817c478bd9Sstevel@tonic-gate  *		The cursor was deleted, and now references a key/data pair
2827c478bd9Sstevel@tonic-gate  *		that has not yet been returned, either before or after the
2837c478bd9Sstevel@tonic-gate  *		deleted key/data pair.
2847c478bd9Sstevel@tonic-gate  * XXX
2857c478bd9Sstevel@tonic-gate  * This structure is broken out so that we can eventually offer multiple
2867c478bd9Sstevel@tonic-gate  * cursors as part of the DB interface.
2877c478bd9Sstevel@tonic-gate  */
2887c478bd9Sstevel@tonic-gate typedef struct _cursor {
2897c478bd9Sstevel@tonic-gate 	EPGNO	 pg;			/* B: Saved tree reference. */
2907c478bd9Sstevel@tonic-gate 	DBT	 key;			/* B: Saved key, or key.data == NULL. */
2917c478bd9Sstevel@tonic-gate 	recno_t	 rcursor;		/* R: recno cursor (1-based) */
2927c478bd9Sstevel@tonic-gate 
2937c478bd9Sstevel@tonic-gate #define	CURS_ACQUIRE	0x01		/*  B: Cursor needs to be reacquired. */
2947c478bd9Sstevel@tonic-gate #define	CURS_AFTER	0x02		/*  B: Unreturned cursor after key. */
2957c478bd9Sstevel@tonic-gate #define	CURS_BEFORE	0x04		/*  B: Unreturned cursor before key. */
2967c478bd9Sstevel@tonic-gate #define	CURS_INIT	0x08		/* RB: Cursor initialized. */
2977c478bd9Sstevel@tonic-gate 	u_int8_t flags;
2987c478bd9Sstevel@tonic-gate } CURSOR;
2997c478bd9Sstevel@tonic-gate 
3007c478bd9Sstevel@tonic-gate /*
3017c478bd9Sstevel@tonic-gate  * The metadata of the tree.  The nrecs field is used only by the RECNO code.
3027c478bd9Sstevel@tonic-gate  * This is because the btree doesn't really need it and it requires that every
3037c478bd9Sstevel@tonic-gate  * put or delete call modify the metadata.
3047c478bd9Sstevel@tonic-gate  */
3057c478bd9Sstevel@tonic-gate typedef struct _btmeta {
3067c478bd9Sstevel@tonic-gate 	u_int32_t	magic;		/* magic number */
3077c478bd9Sstevel@tonic-gate 	u_int32_t	version;	/* version */
3087c478bd9Sstevel@tonic-gate 	u_int32_t	psize;		/* page size */
3097c478bd9Sstevel@tonic-gate 	u_int32_t	free;		/* page number of first free page */
3107c478bd9Sstevel@tonic-gate 	u_int32_t	nrecs;		/* R: number of records */
3117c478bd9Sstevel@tonic-gate 
3127c478bd9Sstevel@tonic-gate #define	SAVEMETA	(B_NODUPS | R_RECNO)
3137c478bd9Sstevel@tonic-gate 	u_int32_t	flags;		/* bt_flags & SAVEMETA */
3147c478bd9Sstevel@tonic-gate } BTMETA;
3157c478bd9Sstevel@tonic-gate 
3167c478bd9Sstevel@tonic-gate /* The in-memory btree/recno data structure. */
3177c478bd9Sstevel@tonic-gate typedef struct _btree {
3187c478bd9Sstevel@tonic-gate 	MPOOL	 *bt_mp;		/* memory pool cookie */
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate 	DB	 *bt_dbp;		/* pointer to enclosing DB */
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate 	EPG	  bt_cur;		/* current (pinned) page */
3237c478bd9Sstevel@tonic-gate 	PAGE	 *bt_pinned;		/* page pinned across calls */
3247c478bd9Sstevel@tonic-gate 
3257c478bd9Sstevel@tonic-gate 	CURSOR	  bt_cursor;		/* cursor */
3267c478bd9Sstevel@tonic-gate 
3277c478bd9Sstevel@tonic-gate #define	BT_PUSH(t, p, i) {						\
3287c478bd9Sstevel@tonic-gate 	t->bt_sp->pgno = p; 						\
3297c478bd9Sstevel@tonic-gate 	t->bt_sp->index = i; 						\
3307c478bd9Sstevel@tonic-gate 	++t->bt_sp;							\
3317c478bd9Sstevel@tonic-gate }
3327c478bd9Sstevel@tonic-gate #define	BT_POP(t)	(t->bt_sp == t->bt_stack ? NULL : --t->bt_sp)
3337c478bd9Sstevel@tonic-gate #define	BT_CLR(t)	(t->bt_sp = t->bt_stack)
3347c478bd9Sstevel@tonic-gate 	EPGNO	  bt_stack[50];		/* stack of parent pages */
3357c478bd9Sstevel@tonic-gate 	EPGNO	 *bt_sp;		/* current stack pointer */
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate 	DBT	  bt_rkey;		/* returned key */
3387c478bd9Sstevel@tonic-gate 	DBT	  bt_rdata;		/* returned data */
3397c478bd9Sstevel@tonic-gate 
3407c478bd9Sstevel@tonic-gate 	int	  bt_fd;		/* tree file descriptor */
3417c478bd9Sstevel@tonic-gate 
3427c478bd9Sstevel@tonic-gate 	db_pgno_t	  bt_free;		/* next free page */
3437c478bd9Sstevel@tonic-gate 	u_int32_t bt_psize;		/* page size */
3447c478bd9Sstevel@tonic-gate 	indx_t	  bt_ovflsize;		/* cut-off for key/data overflow */
3457c478bd9Sstevel@tonic-gate 	int	  bt_lorder;		/* byte order */
3467c478bd9Sstevel@tonic-gate 					/* sorted order */
3477c478bd9Sstevel@tonic-gate 	enum { NOT, BACK, FORWARD } bt_order;
3487c478bd9Sstevel@tonic-gate 	EPGNO	  bt_last;		/* last insert */
3497c478bd9Sstevel@tonic-gate 
3507c478bd9Sstevel@tonic-gate 					/* B: key comparison function */
3517c478bd9Sstevel@tonic-gate 	int	(*bt_cmp) __P((const DBT *, const DBT *));
3527c478bd9Sstevel@tonic-gate 					/* B: prefix comparison function */
3537c478bd9Sstevel@tonic-gate 	size_t	(*bt_pfx) __P((const DBT *, const DBT *));
3547c478bd9Sstevel@tonic-gate 					/* R: recno input function */
3557c478bd9Sstevel@tonic-gate 	int	(*bt_irec) __P((struct _btree *, recno_t));
3567c478bd9Sstevel@tonic-gate 
3577c478bd9Sstevel@tonic-gate 	FILE	 *bt_rfp;		/* R: record FILE pointer */
3587c478bd9Sstevel@tonic-gate 	int	  bt_rfd;		/* R: record file descriptor */
3597c478bd9Sstevel@tonic-gate 
3607c478bd9Sstevel@tonic-gate 	caddr_t	  bt_cmap;		/* R: current point in mapped space */
3617c478bd9Sstevel@tonic-gate 	caddr_t	  bt_smap;		/* R: start of mapped space */
3627c478bd9Sstevel@tonic-gate 	caddr_t   bt_emap;		/* R: end of mapped space */
3637c478bd9Sstevel@tonic-gate 	size_t	  bt_msize;		/* R: size of mapped region. */
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate 	recno_t	  bt_nrecs;		/* R: number of records */
3667c478bd9Sstevel@tonic-gate 	size_t	  bt_reclen;		/* R: fixed record length */
3677c478bd9Sstevel@tonic-gate 	u_char	  bt_bval;		/* R: delimiting byte/pad character */
3687c478bd9Sstevel@tonic-gate 
3697c478bd9Sstevel@tonic-gate /*
3707c478bd9Sstevel@tonic-gate  * NB:
3717c478bd9Sstevel@tonic-gate  * B_NODUPS and R_RECNO are stored on disk, and may not be changed.
3727c478bd9Sstevel@tonic-gate  */
3737c478bd9Sstevel@tonic-gate #define	B_INMEM		0x00001		/* in-memory tree */
3747c478bd9Sstevel@tonic-gate #define	B_METADIRTY	0x00002		/* need to write metadata */
3757c478bd9Sstevel@tonic-gate #define	B_MODIFIED	0x00004		/* tree modified */
3767c478bd9Sstevel@tonic-gate #define	B_NEEDSWAP	0x00008		/* if byte order requires swapping */
3777c478bd9Sstevel@tonic-gate #define	B_RDONLY	0x00010		/* read-only tree */
3787c478bd9Sstevel@tonic-gate 
3797c478bd9Sstevel@tonic-gate #define	B_NODUPS	0x00020		/* no duplicate keys permitted */
3807c478bd9Sstevel@tonic-gate #define	R_RECNO		0x00080		/* record oriented tree */
3817c478bd9Sstevel@tonic-gate 
3827c478bd9Sstevel@tonic-gate #define	R_CLOSEFP	0x00040		/* opened a file pointer */
3837c478bd9Sstevel@tonic-gate #define	R_EOF		0x00100		/* end of input file reached. */
3847c478bd9Sstevel@tonic-gate #define	R_FIXLEN	0x00200		/* fixed length records */
3857c478bd9Sstevel@tonic-gate #define	R_MEMMAPPED	0x00400		/* memory mapped file. */
3867c478bd9Sstevel@tonic-gate #define	R_INMEM		0x00800		/* in-memory file */
3877c478bd9Sstevel@tonic-gate #define	R_MODIFIED	0x01000		/* modified file */
3887c478bd9Sstevel@tonic-gate #define	R_RDONLY	0x02000		/* read-only file */
3897c478bd9Sstevel@tonic-gate 
3907c478bd9Sstevel@tonic-gate #define	B_DB_LOCK	0x04000		/* DB_LOCK specified. */
3917c478bd9Sstevel@tonic-gate #define	B_DB_SHMEM	0x08000		/* DB_SHMEM specified. */
3927c478bd9Sstevel@tonic-gate #define	B_DB_TXN	0x10000		/* DB_TXN specified. */
3937c478bd9Sstevel@tonic-gate 	u_int32_t flags;
3947c478bd9Sstevel@tonic-gate } BTREE;
3957c478bd9Sstevel@tonic-gate 
3967c478bd9Sstevel@tonic-gate #include "extern.h"
3977c478bd9Sstevel@tonic-gate 
3987c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
3997c478bd9Sstevel@tonic-gate }
4007c478bd9Sstevel@tonic-gate #endif
4017c478bd9Sstevel@tonic-gate 
4027c478bd9Sstevel@tonic-gate #endif	/* !_KRB5_BTREE_H */
403