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