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