Home | History | Annotate | Line # | Download | only in btree
      1  1.18    rillig /*	$NetBSD: btree.h,v 1.18 2022/04/19 20:32:15 rillig Exp $	*/
      2   1.8       cgd 
      3   1.1       cgd /*-
      4   1.7       cgd  * Copyright (c) 1991, 1993, 1994
      5   1.2       cgd  *	The Regents of the University of California.  All rights reserved.
      6   1.1       cgd  *
      7   1.1       cgd  * This code is derived from software contributed to Berkeley by
      8   1.1       cgd  * Mike Olson.
      9   1.1       cgd  *
     10   1.1       cgd  * Redistribution and use in source and binary forms, with or without
     11   1.1       cgd  * modification, are permitted provided that the following conditions
     12   1.1       cgd  * are met:
     13   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     14   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     15   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     17   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     18  1.13       agc  * 3. Neither the name of the University nor the names of its contributors
     19   1.1       cgd  *    may be used to endorse or promote products derived from this software
     20   1.1       cgd  *    without specific prior written permission.
     21   1.1       cgd  *
     22   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32   1.1       cgd  * SUCH DAMAGE.
     33   1.2       cgd  *
     34   1.9       cgd  *	@(#)btree.h	8.11 (Berkeley) 8/17/94
     35   1.1       cgd  */
     36  1.12        tv 
     37  1.14     lukem #if HAVE_NBTOOL_CONFIG_H
     38  1.14     lukem #include "nbtool_config.h"
     39  1.12        tv #endif
     40   1.1       cgd 
     41   1.9       cgd /* Macros to set/clear/test flags. */
     42   1.9       cgd #define	F_SET(p, f)	(p)->flags |= (f)
     43   1.9       cgd #define	F_CLR(p, f)	(p)->flags &= ~(f)
     44   1.9       cgd #define	F_ISSET(p, f)	((p)->flags & (f))
     45   1.9       cgd 
     46   1.2       cgd #include <mpool.h>
     47   1.1       cgd 
     48   1.2       cgd #define	DEFMINKEYPAGE	(2)		/* Minimum keys per page */
     49   1.2       cgd #define	MINCACHE	(5)		/* Minimum cached pages */
     50   1.2       cgd #define	MINPSIZE	(512)		/* Minimum page size */
     51   1.1       cgd 
     52   1.1       cgd /*
     53   1.2       cgd  * Page 0 of a btree file contains a copy of the meta-data.  This page is also
     54   1.2       cgd  * used as an out-of-band page, i.e. page pointers that point to nowhere point
     55   1.2       cgd  * to page 0.  Page 1 is the root of the btree.
     56   1.1       cgd  */
     57   1.2       cgd #define	P_INVALID	 0		/* Invalid tree page number. */
     58   1.2       cgd #define	P_META		 0		/* Tree metadata page number. */
     59   1.2       cgd #define	P_ROOT		 1		/* Tree root page number. */
     60   1.1       cgd 
     61   1.1       cgd /*
     62   1.2       cgd  * There are five page layouts in the btree: btree internal pages (BINTERNAL),
     63   1.2       cgd  * btree leaf pages (BLEAF), recno internal pages (RINTERNAL), recno leaf pages
     64   1.2       cgd  * (RLEAF) and overflow pages.  All five page types have a page header (PAGE).
     65   1.7       cgd  * This implementation requires that values within structures NOT be padded.
     66   1.2       cgd  * (ANSI C permits random padding.)  If your compiler pads randomly you'll have
     67   1.2       cgd  * to do some work to get this package to run.
     68   1.1       cgd  */
     69   1.4       cgd typedef struct _page {
     70   1.2       cgd 	pgno_t	pgno;			/* this page's page number */
     71   1.2       cgd 	pgno_t	prevpg;			/* left sibling */
     72   1.2       cgd 	pgno_t	nextpg;			/* right sibling */
     73   1.1       cgd 
     74   1.2       cgd #define	P_BINTERNAL	0x01		/* btree internal page */
     75   1.2       cgd #define	P_BLEAF		0x02		/* leaf page */
     76   1.2       cgd #define	P_OVERFLOW	0x04		/* overflow page */
     77   1.2       cgd #define	P_RINTERNAL	0x08		/* recno internal page */
     78   1.2       cgd #define	P_RLEAF		0x10		/* leaf page */
     79   1.2       cgd #define P_TYPE		0x1f		/* type mask */
     80   1.2       cgd #define	P_PRESERVE	0x20		/* never delete this chain of pages */
     81  1.16     joerg 	uint32_t flags;
     82   1.1       cgd 
     83   1.2       cgd 	indx_t	lower;			/* lower bound of free space on page */
     84   1.2       cgd 	indx_t	upper;			/* upper bound of free space on page */
     85   1.7       cgd 	indx_t	linp[1];		/* indx_t-aligned VAR. LENGTH DATA */
     86   1.2       cgd } PAGE;
     87   1.1       cgd 
     88   1.2       cgd /* First and next index. */
     89   1.9       cgd #define	BTDATAOFF							\
     90   1.9       cgd 	(sizeof(pgno_t) + sizeof(pgno_t) + sizeof(pgno_t) +		\
     91  1.16     joerg 	    sizeof(uint32_t) + sizeof(indx_t) + sizeof(indx_t))
     92  1.15  christos 
     93  1.15  christos #define	_NEXTINDEX(p) (((p)->lower - BTDATAOFF) / sizeof(indx_t))
     94  1.15  christos #ifdef _DIAGNOSTIC
     95  1.15  christos static __inline indx_t
     96  1.15  christos NEXTINDEX(const PAGE *p) {
     97  1.15  christos 	size_t x = _NEXTINDEX(p);
     98  1.15  christos 	_DBFIT(x, indx_t);
     99  1.15  christos 	return (indx_t)x;
    100  1.15  christos }
    101  1.15  christos #else
    102  1.15  christos #define	NEXTINDEX(p) (indx_t)_NEXTINDEX(p)
    103  1.15  christos #endif
    104   1.1       cgd 
    105   1.1       cgd /*
    106   1.2       cgd  * For pages other than overflow pages, there is an array of offsets into the
    107   1.2       cgd  * rest of the page immediately following the page header.  Each offset is to
    108   1.2       cgd  * an item which is unique to the type of page.  The h_lower offset is just
    109   1.2       cgd  * past the last filled-in index.  The h_upper offset is the first item on the
    110   1.2       cgd  * page.  Offsets are from the beginning of the page.
    111   1.1       cgd  *
    112   1.2       cgd  * If an item is too big to store on a single page, a flag is set and the item
    113   1.2       cgd  * is a { page, size } pair such that the page is the first page of an overflow
    114   1.2       cgd  * chain with size bytes of item.  Overflow pages are simply bytes without any
    115   1.2       cgd  * external structure.
    116   1.1       cgd  *
    117   1.7       cgd  * The page number and size fields in the items are pgno_t-aligned so they can
    118   1.7       cgd  * be manipulated without copying.  (This presumes that 32 bit items can be
    119   1.7       cgd  * manipulated on this system.)
    120   1.1       cgd  */
    121  1.10  christos #define	BTLALIGN(n)	(((n) + sizeof(pgno_t) - 1) & ~(sizeof(pgno_t) - 1))
    122  1.16     joerg #define	NOVFLSIZE	(sizeof(pgno_t) + sizeof(uint32_t))
    123   1.1       cgd 
    124   1.1       cgd /*
    125   1.2       cgd  * For the btree internal pages, the item is a key.  BINTERNALs are {key, pgno}
    126   1.2       cgd  * pairs, such that the key compares less than or equal to all of the records
    127   1.2       cgd  * on that page.  For a tree without duplicate keys, an internal page with two
    128   1.2       cgd  * consecutive keys, a and b, will have all records greater than or equal to a
    129   1.2       cgd  * and less than b stored on the page associated with a.  Duplicate keys are
    130   1.2       cgd  * somewhat special and can cause duplicate internal and leaf page records and
    131   1.2       cgd  * some minor modifications of the above rule.
    132   1.2       cgd  */
    133   1.4       cgd typedef struct _binternal {
    134  1.16     joerg 	uint32_t ksize;			/* key size */
    135   1.2       cgd 	pgno_t	pgno;			/* page number stored on */
    136   1.2       cgd #define	P_BIGDATA	0x01		/* overflow data */
    137   1.2       cgd #define	P_BIGKEY	0x02		/* overflow key */
    138  1.16     joerg 	uint8_t	flags;
    139   1.2       cgd 	char	bytes[1];		/* data */
    140   1.2       cgd } BINTERNAL;
    141   1.2       cgd 
    142   1.2       cgd /* Get the page's BINTERNAL structure at index indx. */
    143   1.9       cgd #define	GETBINTERNAL(pg, indx)						\
    144  1.11  christos 	((BINTERNAL *)(void *)((char *)(void *)(pg) + (pg)->linp[indx]))
    145   1.2       cgd 
    146   1.2       cgd /* Get the number of bytes in the entry. */
    147  1.15  christos #define _NBINTERNAL(len)						\
    148  1.16     joerg     BTLALIGN(sizeof(uint32_t) + sizeof(pgno_t) + sizeof(uint8_t) + (len))
    149  1.15  christos #ifdef _DIAGNOSTIC
    150  1.16     joerg static __inline uint32_t
    151  1.16     joerg NBINTERNAL(uint32_t len) {
    152  1.15  christos 	size_t x = _NBINTERNAL(len);
    153  1.16     joerg 	_DBFIT(x, uint32_t);
    154  1.16     joerg 	return (uint32_t)x;
    155  1.15  christos }
    156  1.15  christos #else
    157  1.16     joerg #define NBINTERNAL(len)	(uint32_t)_NBINTERNAL(len)
    158  1.15  christos #endif
    159   1.2       cgd 
    160   1.2       cgd /* Copy a BINTERNAL entry to the page. */
    161  1.15  christos #define	WR_BINTERNAL(p, size, pgno, flags) do {				\
    162  1.16     joerg 	_DBFIT(size, uint32_t);						\
    163  1.16     joerg 	*(uint32_t *)(void *)p = (uint32_t)size;			\
    164  1.16     joerg 	p += sizeof(uint32_t);						\
    165  1.11  christos 	*(pgno_t *)(void *)p = pgno;					\
    166   1.7       cgd 	p += sizeof(pgno_t);						\
    167  1.16     joerg 	*(uint8_t *)(void *)p = flags;					\
    168  1.16     joerg 	p += sizeof(uint8_t);						\
    169  1.18    rillig } while (0)
    170   1.2       cgd 
    171   1.2       cgd /*
    172   1.2       cgd  * For the recno internal pages, the item is a page number with the number of
    173   1.2       cgd  * keys found on that page and below.
    174   1.2       cgd  */
    175   1.4       cgd typedef struct _rinternal {
    176   1.2       cgd 	recno_t	nrecs;			/* number of records */
    177   1.2       cgd 	pgno_t	pgno;			/* page number stored below */
    178   1.2       cgd } RINTERNAL;
    179   1.2       cgd 
    180   1.2       cgd /* Get the page's RINTERNAL structure at index indx. */
    181   1.9       cgd #define	GETRINTERNAL(pg, indx)						\
    182  1.11  christos 	((RINTERNAL *)(void *)((char *)(void *)(pg) + (pg)->linp[indx]))
    183   1.2       cgd 
    184   1.2       cgd /* Get the number of bytes in the entry. */
    185   1.9       cgd #define NRINTERNAL							\
    186  1.10  christos 	BTLALIGN(sizeof(recno_t) + sizeof(pgno_t))
    187   1.2       cgd 
    188  1.17     ryoon /* Copy a RINTERNAL entry to the page. */
    189  1.15  christos #define	WR_RINTERNAL(p, nrecs, pgno) do {				\
    190  1.11  christos 	*(recno_t *)(void *)p = nrecs;					\
    191   1.9       cgd 	p += sizeof(recno_t);						\
    192  1.11  christos 	*(pgno_t *)(void *)p = pgno;					\
    193  1.18    rillig } while (0)
    194   1.2       cgd 
    195   1.2       cgd /* For the btree leaf pages, the item is a key and data pair. */
    196   1.4       cgd typedef struct _bleaf {
    197  1.16     joerg 	uint32_t	ksize;		/* size of key */
    198  1.16     joerg 	uint32_t	dsize;		/* size of data */
    199  1.16     joerg 	uint8_t	flags;			/* P_BIGDATA, P_BIGKEY */
    200   1.2       cgd 	char	bytes[1];		/* data */
    201   1.2       cgd } BLEAF;
    202   1.2       cgd 
    203   1.2       cgd /* Get the page's BLEAF structure at index indx. */
    204   1.9       cgd #define	GETBLEAF(pg, indx)						\
    205  1.11  christos 	((BLEAF *)(void *)((char *)(void *)(pg) + (pg)->linp[indx]))
    206   1.2       cgd 
    207   1.2       cgd 
    208   1.2       cgd /* Get the number of bytes in the user's key/data pair. */
    209  1.15  christos #define _NBLEAFDBT(ksize, dsize)					\
    210  1.16     joerg     BTLALIGN(sizeof(uint32_t) + sizeof(uint32_t) + sizeof(uint8_t) +	\
    211   1.2       cgd 	    (ksize) + (dsize))
    212  1.15  christos #ifdef _DIAGNOSTIC
    213  1.16     joerg static __inline uint32_t
    214  1.15  christos NBLEAFDBT(size_t k, size_t d) {
    215  1.15  christos 	size_t x = _NBLEAFDBT(k, d);
    216  1.16     joerg 	_DBFIT(x, uint32_t);
    217  1.16     joerg 	return (uint32_t)x;
    218  1.15  christos }
    219  1.15  christos #else
    220  1.16     joerg #define NBLEAFDBT(p, q)	(uint32_t)_NBLEAFDBT(p, q)
    221  1.15  christos #endif
    222  1.15  christos 
    223  1.15  christos /* Get the number of bytes in the entry. */
    224  1.15  christos #define NBLEAF(p)	NBLEAFDBT((p)->ksize, (p)->dsize)
    225   1.2       cgd 
    226   1.2       cgd /* Copy a BLEAF entry to the page. */
    227  1.15  christos #define	WR_BLEAF(p, key, data, flags) do {				\
    228  1.16     joerg 	_DBFIT(key->size, uint32_t);					\
    229  1.16     joerg 	*(uint32_t *)(void *)p = (uint32_t)key->size;			\
    230  1.16     joerg 	p += sizeof(uint32_t);						\
    231  1.16     joerg 	_DBFIT(data->size, uint32_t);					\
    232  1.16     joerg 	*(uint32_t *)(void *)p = (uint32_t)data->size;			\
    233  1.16     joerg 	p += sizeof(uint32_t);						\
    234  1.16     joerg 	*(uint8_t *)(void *)p = flags;					\
    235  1.16     joerg 	p += sizeof(uint8_t);						\
    236  1.15  christos 	(void)memmove(p, key->data, key->size);				\
    237   1.7       cgd 	p += key->size;							\
    238  1.15  christos 	(void)memmove(p, data->data, data->size);			\
    239  1.18    rillig } while (0)
    240   1.2       cgd 
    241   1.2       cgd /* For the recno leaf pages, the item is a data entry. */
    242   1.4       cgd typedef struct _rleaf {
    243  1.16     joerg 	uint32_t	dsize;		/* size of data */
    244  1.16     joerg 	uint8_t	flags;			/* P_BIGDATA */
    245   1.2       cgd 	char	bytes[1];
    246   1.2       cgd } RLEAF;
    247   1.2       cgd 
    248   1.2       cgd /* Get the page's RLEAF structure at index indx. */
    249   1.9       cgd #define	GETRLEAF(pg, indx)						\
    250  1.11  christos 	((RLEAF *)(void *)((char *)(void *)(pg) + (pg)->linp[indx]))
    251   1.2       cgd 
    252  1.15  christos #define	_NRLEAFDBT(dsize)						\
    253  1.16     joerg 	BTLALIGN(sizeof(uint32_t) + sizeof(uint8_t) + (dsize))
    254  1.15  christos 
    255  1.15  christos #ifdef _DIAGNOSTIC
    256  1.16     joerg static __inline uint32_t
    257  1.15  christos NRLEAFDBT(size_t d) {
    258  1.15  christos 	size_t x = _NRLEAFDBT(d);
    259  1.16     joerg 	_DBFIT(x, uint32_t);
    260  1.16     joerg 	return (uint32_t)x;
    261  1.15  christos }
    262  1.15  christos #else
    263  1.16     joerg #define NRLEAFDBT(d)	(uint32_t)_NRLEAFDBT(d)
    264  1.15  christos #endif
    265  1.15  christos 
    266   1.2       cgd /* Get the number of bytes in the entry. */
    267   1.2       cgd #define NRLEAF(p)	NRLEAFDBT((p)->dsize)
    268   1.2       cgd 
    269   1.2       cgd /* Get the number of bytes from the user's data. */
    270   1.2       cgd 
    271   1.2       cgd /* Copy a RLEAF entry to the page. */
    272  1.15  christos #define	WR_RLEAF(p, data, flags) do {					\
    273  1.16     joerg 	_DBFIT(data->size, uint32_t);					\
    274  1.16     joerg 	*(uint32_t *)(void *)p = (uint32_t)data->size;			\
    275  1.16     joerg 	p += sizeof(uint32_t);						\
    276  1.16     joerg 	*(uint8_t *)(void *)p = flags;					\
    277  1.16     joerg 	p += sizeof(uint8_t);						\
    278   1.7       cgd 	memmove(p, data->data, data->size);				\
    279  1.18    rillig } while (0)
    280   1.2       cgd 
    281   1.2       cgd /*
    282   1.2       cgd  * A record in the tree is either a pointer to a page and an index in the page
    283   1.2       cgd  * or a page number and an index.  These structures are used as a cursor, stack
    284   1.2       cgd  * entry and search returns as well as to pass records to other routines.
    285   1.1       cgd  *
    286   1.2       cgd  * One comment about searches.  Internal page searches must find the largest
    287   1.2       cgd  * record less than key in the tree so that descents work.  Leaf page searches
    288   1.2       cgd  * must find the smallest record greater than key so that the returned index
    289   1.2       cgd  * is the record's correct position for insertion.
    290   1.2       cgd  */
    291   1.4       cgd typedef struct _epgno {
    292   1.2       cgd 	pgno_t	pgno;			/* the page number */
    293   1.2       cgd 	indx_t	index;			/* the index on the page */
    294   1.2       cgd } EPGNO;
    295   1.2       cgd 
    296   1.4       cgd typedef struct _epg {
    297   1.2       cgd 	PAGE	*page;			/* the (pinned) page */
    298   1.2       cgd 	indx_t	 index;			/* the index on the page */
    299   1.2       cgd } EPG;
    300   1.2       cgd 
    301   1.2       cgd /*
    302   1.9       cgd  * About cursors.  The cursor (and the page that contained the key/data pair
    303   1.9       cgd  * that it referenced) can be deleted, which makes things a bit tricky.  If
    304   1.9       cgd  * there are no duplicates of the cursor key in the tree (i.e. B_NODUPS is set
    305   1.9       cgd  * or there simply aren't any duplicates of the key) we copy the key that it
    306   1.9       cgd  * referenced when it's deleted, and reacquire a new cursor key if the cursor
    307   1.9       cgd  * is used again.  If there are duplicates keys, we move to the next/previous
    308   1.9       cgd  * key, and set a flag so that we know what happened.  NOTE: if duplicate (to
    309   1.9       cgd  * the cursor) keys are added to the tree during this process, it is undefined
    310   1.9       cgd  * if they will be returned or not in a cursor scan.
    311   1.9       cgd  *
    312   1.9       cgd  * The flags determine the possible states of the cursor:
    313   1.9       cgd  *
    314   1.9       cgd  * CURS_INIT	The cursor references *something*.
    315   1.9       cgd  * CURS_ACQUIRE	The cursor was deleted, and a key has been saved so that
    316   1.9       cgd  *		we can reacquire the right position in the tree.
    317   1.9       cgd  * CURS_AFTER, CURS_BEFORE
    318   1.9       cgd  *		The cursor was deleted, and now references a key/data pair
    319   1.9       cgd  *		that has not yet been returned, either before or after the
    320   1.9       cgd  *		deleted key/data pair.
    321   1.9       cgd  * XXX
    322   1.9       cgd  * This structure is broken out so that we can eventually offer multiple
    323   1.9       cgd  * cursors as part of the DB interface.
    324   1.9       cgd  */
    325   1.9       cgd typedef struct _cursor {
    326   1.9       cgd 	EPGNO	 pg;			/* B: Saved tree reference. */
    327   1.9       cgd 	DBT	 key;			/* B: Saved key, or key.data == NULL. */
    328   1.9       cgd 	recno_t	 rcursor;		/* R: recno cursor (1-based) */
    329   1.9       cgd 
    330   1.9       cgd #define	CURS_ACQUIRE	0x01		/*  B: Cursor needs to be reacquired. */
    331   1.9       cgd #define	CURS_AFTER	0x02		/*  B: Unreturned cursor after key. */
    332   1.9       cgd #define	CURS_BEFORE	0x04		/*  B: Unreturned cursor before key. */
    333   1.9       cgd #define	CURS_INIT	0x08		/* RB: Cursor initialized. */
    334  1.16     joerg 	uint8_t flags;
    335   1.9       cgd } CURSOR;
    336   1.9       cgd 
    337   1.9       cgd /*
    338   1.9       cgd  * The metadata of the tree.  The nrecs field is used only by the RECNO code.
    339   1.2       cgd  * This is because the btree doesn't really need it and it requires that every
    340   1.2       cgd  * put or delete call modify the metadata.
    341   1.1       cgd  */
    342   1.4       cgd typedef struct _btmeta {
    343  1.16     joerg 	uint32_t	magic;		/* magic number */
    344  1.16     joerg 	uint32_t	version;	/* version */
    345  1.16     joerg 	uint32_t	psize;		/* page size */
    346  1.16     joerg 	uint32_t	free;		/* page number of first free page */
    347  1.16     joerg 	uint32_t	nrecs;		/* R: number of records */
    348   1.9       cgd 
    349   1.2       cgd #define	SAVEMETA	(B_NODUPS | R_RECNO)
    350  1.16     joerg 	uint32_t	flags;		/* bt_flags & SAVEMETA */
    351   1.2       cgd } BTMETA;
    352   1.1       cgd 
    353   1.2       cgd /* The in-memory btree/recno data structure. */
    354   1.4       cgd typedef struct _btree {
    355   1.9       cgd 	MPOOL	 *bt_mp;		/* memory pool cookie */
    356   1.2       cgd 
    357   1.9       cgd 	DB	 *bt_dbp;		/* pointer to enclosing DB */
    358   1.2       cgd 
    359   1.9       cgd 	EPG	  bt_cur;		/* current (pinned) page */
    360   1.9       cgd 	PAGE	 *bt_pinned;		/* page pinned across calls */
    361   1.4       cgd 
    362   1.9       cgd 	CURSOR	  bt_cursor;		/* cursor */
    363   1.2       cgd 
    364   1.9       cgd #define	BT_PUSH(t, p, i) {						\
    365   1.9       cgd 	t->bt_sp->pgno = p; 						\
    366   1.9       cgd 	t->bt_sp->index = i; 						\
    367   1.9       cgd 	++t->bt_sp;							\
    368   1.9       cgd }
    369   1.9       cgd #define	BT_POP(t)	(t->bt_sp == t->bt_stack ? NULL : --t->bt_sp)
    370   1.9       cgd #define	BT_CLR(t)	(t->bt_sp = t->bt_stack)
    371   1.9       cgd 	EPGNO	  bt_stack[50];		/* stack of parent pages */
    372   1.9       cgd 	EPGNO	 *bt_sp;		/* current stack pointer */
    373   1.2       cgd 
    374   1.9       cgd 	DBT	  bt_rkey;		/* returned key */
    375   1.9       cgd 	DBT	  bt_rdata;		/* returned data */
    376   1.2       cgd 
    377   1.9       cgd 	int	  bt_fd;		/* tree file descriptor */
    378   1.2       cgd 
    379   1.9       cgd 	pgno_t	  bt_free;		/* next free page */
    380  1.16     joerg 	uint32_t bt_psize;		/* page size */
    381   1.9       cgd 	indx_t	  bt_ovflsize;		/* cut-off for key/data overflow */
    382   1.9       cgd 	int	  bt_lorder;		/* byte order */
    383   1.2       cgd 					/* sorted order */
    384   1.6       cgd 	enum { NOT, BACK, FORWARD } bt_order;
    385   1.9       cgd 	EPGNO	  bt_last;		/* last insert */
    386   1.2       cgd 
    387   1.2       cgd 					/* B: key comparison function */
    388  1.15  christos 	int	(*bt_cmp)(const DBT *, const DBT *);
    389   1.2       cgd 					/* B: prefix comparison function */
    390  1.15  christos 	size_t	(*bt_pfx)(const DBT *, const DBT *);
    391   1.2       cgd 					/* R: recno input function */
    392  1.15  christos 	int	(*bt_irec)(struct _btree *, recno_t);
    393   1.2       cgd 
    394   1.9       cgd 	FILE	 *bt_rfp;		/* R: record FILE pointer */
    395   1.9       cgd 	int	  bt_rfd;		/* R: record file descriptor */
    396   1.2       cgd 
    397   1.9       cgd 	caddr_t	  bt_cmap;		/* R: current point in mapped space */
    398   1.9       cgd 	caddr_t	  bt_smap;		/* R: start of mapped space */
    399   1.9       cgd 	caddr_t   bt_emap;		/* R: end of mapped space */
    400   1.9       cgd 	size_t	  bt_msize;		/* R: size of mapped region. */
    401   1.9       cgd 
    402   1.9       cgd 	recno_t	  bt_nrecs;		/* R: number of records */
    403   1.9       cgd 	size_t	  bt_reclen;		/* R: fixed record length */
    404  1.16     joerg 	uint8_t	  bt_bval;		/* R: delimiting byte/pad character */
    405   1.2       cgd 
    406   1.2       cgd /*
    407   1.2       cgd  * NB:
    408   1.2       cgd  * B_NODUPS and R_RECNO are stored on disk, and may not be changed.
    409   1.2       cgd  */
    410   1.9       cgd #define	B_INMEM		0x00001		/* in-memory tree */
    411   1.9       cgd #define	B_METADIRTY	0x00002		/* need to write metadata */
    412   1.9       cgd #define	B_MODIFIED	0x00004		/* tree modified */
    413   1.9       cgd #define	B_NEEDSWAP	0x00008		/* if byte order requires swapping */
    414   1.9       cgd #define	B_RDONLY	0x00010		/* read-only tree */
    415   1.9       cgd 
    416   1.2       cgd #define	B_NODUPS	0x00020		/* no duplicate keys permitted */
    417   1.4       cgd #define	R_RECNO		0x00080		/* record oriented tree */
    418   1.2       cgd 
    419   1.9       cgd #define	R_CLOSEFP	0x00040		/* opened a file pointer */
    420   1.9       cgd #define	R_EOF		0x00100		/* end of input file reached. */
    421   1.9       cgd #define	R_FIXLEN	0x00200		/* fixed length records */
    422   1.9       cgd #define	R_MEMMAPPED	0x00400		/* memory mapped file. */
    423   1.9       cgd #define	R_INMEM		0x00800		/* in-memory file */
    424   1.9       cgd #define	R_MODIFIED	0x01000		/* modified file */
    425   1.9       cgd #define	R_RDONLY	0x02000		/* read-only file */
    426   1.9       cgd 
    427   1.9       cgd #define	B_DB_LOCK	0x04000		/* DB_LOCK specified. */
    428   1.9       cgd #define	B_DB_SHMEM	0x08000		/* DB_SHMEM specified. */
    429   1.9       cgd #define	B_DB_TXN	0x10000		/* DB_TXN specified. */
    430  1.16     joerg 	uint32_t flags;
    431   1.2       cgd } BTREE;
    432   1.1       cgd 
    433   1.2       cgd #include "extern.h"
    434