/*-1* SPDX-License-Identifier: BSD-3-Clause2*3* Copyright (c) 1991, 1993, 19944* The Regents of the University of California. All rights reserved.5*6* This code is derived from software contributed to Berkeley by7* Mike Olson.8*9* Redistribution and use in source and binary forms, with or without10* modification, are permitted provided that the following conditions11* are met:12* 1. Redistributions of source code must retain the above copyright13* notice, this list of conditions and the following disclaimer.14* 2. Redistributions in binary form must reproduce the above copyright15* notice, this list of conditions and the following disclaimer in the16* documentation and/or other materials provided with the distribution.17* 3. Neither the name of the University nor the names of its contributors18* may be used to endorse or promote products derived from this software19* without specific prior written permission.20*21* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND22* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE23* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE24* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE25* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL26* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS27* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)28* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT29* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY30* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF31* SUCH DAMAGE.32*/3334/* Macros to set/clear/test flags. */35#define F_SET(p, f) (p)->flags |= (f)36#define F_CLR(p, f) (p)->flags &= ~(f)37#define F_ISSET(p, f) ((p)->flags & (f))3839#include <mpool.h>4041#define DEFMINKEYPAGE (2) /* Minimum keys per page */42#define MINCACHE (5) /* Minimum cached pages */43#define MINPSIZE (512) /* Minimum page size */4445/*46* Page 0 of a btree file contains a copy of the meta-data. This page is also47* used as an out-of-band page, i.e. page pointers that point to nowhere point48* to page 0. Page 1 is the root of the btree.49*/50#define P_INVALID 0 /* Invalid tree page number. */51#define P_META 0 /* Tree metadata page number. */52#define P_ROOT 1 /* Tree root page number. */5354/*55* There are five page layouts in the btree: btree internal pages (BINTERNAL),56* btree leaf pages (BLEAF), recno internal pages (RINTERNAL), recno leaf pages57* (RLEAF) and overflow pages. All five page types have a page header (PAGE).58* This implementation requires that values within structures NOT be padded.59* (ANSI C permits random padding.) If your compiler pads randomly you'll have60* to do some work to get this package to run.61*/62typedef struct _page {63pgno_t pgno; /* this page's page number */64pgno_t prevpg; /* left sibling */65pgno_t nextpg; /* right sibling */6667#define P_BINTERNAL 0x01 /* btree internal page */68#define P_BLEAF 0x02 /* leaf page */69#define P_OVERFLOW 0x04 /* overflow page */70#define P_RINTERNAL 0x08 /* recno internal page */71#define P_RLEAF 0x10 /* leaf page */72#define P_TYPE 0x1f /* type mask */73#define P_PRESERVE 0x20 /* never delete this chain of pages */74u_int32_t flags;7576indx_t lower; /* lower bound of free space on page */77indx_t upper; /* upper bound of free space on page */78indx_t linp[1]; /* indx_t-aligned VAR. LENGTH DATA */79} PAGE;8081/* First and next index. */82#define BTDATAOFF \83(sizeof(pgno_t) + sizeof(pgno_t) + sizeof(pgno_t) + \84sizeof(u_int32_t) + sizeof(indx_t) + sizeof(indx_t))85#define NEXTINDEX(p) (((p)->lower - BTDATAOFF) / sizeof(indx_t))8687/*88* For pages other than overflow pages, there is an array of offsets into the89* rest of the page immediately following the page header. Each offset is to90* an item which is unique to the type of page. The h_lower offset is just91* past the last filled-in index. The h_upper offset is the first item on the92* page. Offsets are from the beginning of the page.93*94* If an item is too big to store on a single page, a flag is set and the item95* is a { page, size } pair such that the page is the first page of an overflow96* chain with size bytes of item. Overflow pages are simply bytes without any97* external structure.98*99* The page number and size fields in the items are pgno_t-aligned so they can100* be manipulated without copying. (This presumes that 32 bit items can be101* manipulated on this system.)102*/103#define LALIGN(n) (((n) + sizeof(pgno_t) - 1) & ~(sizeof(pgno_t) - 1))104#define NOVFLSIZE (sizeof(pgno_t) + sizeof(u_int32_t))105106/*107* For the btree internal pages, the item is a key. BINTERNALs are {key, pgno}108* pairs, such that the key compares less than or equal to all of the records109* on that page. For a tree without duplicate keys, an internal page with two110* consecutive keys, a and b, will have all records greater than or equal to a111* and less than b stored on the page associated with a. Duplicate keys are112* somewhat special and can cause duplicate internal and leaf page records and113* some minor modifications of the above rule.114*/115typedef struct _binternal {116u_int32_t ksize; /* key size */117pgno_t pgno; /* page number stored on */118#define P_BIGDATA 0x01 /* overflow data */119#define P_BIGKEY 0x02 /* overflow key */120u_char flags;121char bytes[1]; /* data */122} BINTERNAL;123124/* Get the page's BINTERNAL structure at index indx. */125#define GETBINTERNAL(pg, indx) \126((BINTERNAL *)((char *)(pg) + (pg)->linp[indx]))127128/* Get the number of bytes in the entry. */129#define NBINTERNAL(len) \130LALIGN(sizeof(u_int32_t) + sizeof(pgno_t) + sizeof(u_char) + (len))131132/* Copy a BINTERNAL entry to the page. */133#define WR_BINTERNAL(p, size, pgno, flags) { \134*(u_int32_t *)p = size; \135p += sizeof(u_int32_t); \136*(pgno_t *)p = pgno; \137p += sizeof(pgno_t); \138*(u_char *)p = flags; \139p += sizeof(u_char); \140}141142/*143* For the recno internal pages, the item is a page number with the number of144* keys found on that page and below.145*/146typedef struct _rinternal {147recno_t nrecs; /* number of records */148pgno_t pgno; /* page number stored below */149} RINTERNAL;150151/* Get the page's RINTERNAL structure at index indx. */152#define GETRINTERNAL(pg, indx) \153((RINTERNAL *)((char *)(pg) + (pg)->linp[indx]))154155/* Get the number of bytes in the entry. */156#define NRINTERNAL \157LALIGN(sizeof(recno_t) + sizeof(pgno_t))158159/* Copy a RINTERAL entry to the page. */160#define WR_RINTERNAL(p, nrecs, pgno) { \161*(recno_t *)p = nrecs; \162p += sizeof(recno_t); \163*(pgno_t *)p = pgno; \164}165166/* For the btree leaf pages, the item is a key and data pair. */167typedef struct _bleaf {168u_int32_t ksize; /* size of key */169u_int32_t dsize; /* size of data */170u_char flags; /* P_BIGDATA, P_BIGKEY */171char bytes[1]; /* data */172} BLEAF;173174/* Get the page's BLEAF structure at index indx. */175#define GETBLEAF(pg, indx) \176((BLEAF *)((char *)(pg) + (pg)->linp[indx]))177178/* Get the number of bytes in the entry. */179#define NBLEAF(p) NBLEAFDBT((p)->ksize, (p)->dsize)180181/* Get the number of bytes in the user's key/data pair. */182#define NBLEAFDBT(ksize, dsize) \183LALIGN(sizeof(u_int32_t) + sizeof(u_int32_t) + sizeof(u_char) + \184(ksize) + (dsize))185186/* Copy a BLEAF entry to the page. */187#define WR_BLEAF(p, key, data, flags) { \188*(u_int32_t *)p = key->size; \189p += sizeof(u_int32_t); \190*(u_int32_t *)p = data->size; \191p += sizeof(u_int32_t); \192*(u_char *)p = flags; \193p += sizeof(u_char); \194memmove(p, key->data, key->size); \195p += key->size; \196memmove(p, data->data, data->size); \197}198199/* For the recno leaf pages, the item is a data entry. */200typedef struct _rleaf {201u_int32_t dsize; /* size of data */202u_char flags; /* P_BIGDATA */203char bytes[1];204} RLEAF;205206/* Get the page's RLEAF structure at index indx. */207#define GETRLEAF(pg, indx) \208((RLEAF *)((char *)(pg) + (pg)->linp[indx]))209210/* Get the number of bytes in the entry. */211#define NRLEAF(p) NRLEAFDBT((p)->dsize)212213/* Get the number of bytes from the user's data. */214#define NRLEAFDBT(dsize) \215LALIGN(sizeof(u_int32_t) + sizeof(u_char) + (dsize))216217/* Copy a RLEAF entry to the page. */218#define WR_RLEAF(p, data, flags) { \219*(u_int32_t *)p = data->size; \220p += sizeof(u_int32_t); \221*(u_char *)p = flags; \222p += sizeof(u_char); \223memmove(p, data->data, data->size); \224}225226/*227* A record in the tree is either a pointer to a page and an index in the page228* or a page number and an index. These structures are used as a cursor, stack229* entry and search returns as well as to pass records to other routines.230*231* One comment about searches. Internal page searches must find the largest232* record less than key in the tree so that descents work. Leaf page searches233* must find the smallest record greater than key so that the returned index234* is the record's correct position for insertion.235*/236typedef struct _epgno {237pgno_t pgno; /* the page number */238indx_t index; /* the index on the page */239} EPGNO;240241typedef struct _epg {242PAGE *page; /* the (pinned) page */243indx_t index; /* the index on the page */244} EPG;245246/*247* About cursors. The cursor (and the page that contained the key/data pair248* that it referenced) can be deleted, which makes things a bit tricky. If249* there are no duplicates of the cursor key in the tree (i.e. B_NODUPS is set250* or there simply aren't any duplicates of the key) we copy the key that it251* referenced when it's deleted, and reacquire a new cursor key if the cursor252* is used again. If there are duplicates keys, we move to the next/previous253* key, and set a flag so that we know what happened. NOTE: if duplicate (to254* the cursor) keys are added to the tree during this process, it is undefined255* if they will be returned or not in a cursor scan.256*257* The flags determine the possible states of the cursor:258*259* CURS_INIT The cursor references *something*.260* CURS_ACQUIRE The cursor was deleted, and a key has been saved so that261* we can reacquire the right position in the tree.262* CURS_AFTER, CURS_BEFORE263* The cursor was deleted, and now references a key/data pair264* that has not yet been returned, either before or after the265* deleted key/data pair.266* XXX267* This structure is broken out so that we can eventually offer multiple268* cursors as part of the DB interface.269*/270typedef struct _cursor {271EPGNO pg; /* B: Saved tree reference. */272DBT key; /* B: Saved key, or key.data == NULL. */273recno_t rcursor; /* R: recno cursor (1-based) */274275#define CURS_ACQUIRE 0x01 /* B: Cursor needs to be reacquired. */276#define CURS_AFTER 0x02 /* B: Unreturned cursor after key. */277#define CURS_BEFORE 0x04 /* B: Unreturned cursor before key. */278#define CURS_INIT 0x08 /* RB: Cursor initialized. */279u_int8_t flags;280} CURSOR;281282/*283* The metadata of the tree. The nrecs field is used only by the RECNO code.284* This is because the btree doesn't really need it and it requires that every285* put or delete call modify the metadata.286*/287typedef struct _btmeta {288u_int32_t magic; /* magic number */289u_int32_t version; /* version */290u_int32_t psize; /* page size */291u_int32_t free; /* page number of first free page */292u_int32_t nrecs; /* R: number of records */293294#define SAVEMETA (B_NODUPS | R_RECNO)295u_int32_t flags; /* bt_flags & SAVEMETA */296} BTMETA;297298/* The in-memory btree/recno data structure. */299typedef struct _btree {300MPOOL *bt_mp; /* memory pool cookie */301302DB *bt_dbp; /* pointer to enclosing DB */303304EPG bt_cur; /* current (pinned) page */305PAGE *bt_pinned; /* page pinned across calls */306307CURSOR bt_cursor; /* cursor */308309#define BT_PUSH(t, p, i) { \310t->bt_sp->pgno = p; \311t->bt_sp->index = i; \312++t->bt_sp; \313}314#define BT_POP(t) (t->bt_sp == t->bt_stack ? NULL : --t->bt_sp)315#define BT_CLR(t) (t->bt_sp = t->bt_stack)316EPGNO bt_stack[50]; /* stack of parent pages */317EPGNO *bt_sp; /* current stack pointer */318319DBT bt_rkey; /* returned key */320DBT bt_rdata; /* returned data */321322int bt_fd; /* tree file descriptor */323324pgno_t bt_free; /* next free page */325u_int32_t bt_psize; /* page size */326indx_t bt_ovflsize; /* cut-off for key/data overflow */327int bt_lorder; /* byte order */328/* sorted order */329enum { NOT, BACK, FORWARD } bt_order;330EPGNO bt_last; /* last insert */331332/* B: key comparison function */333int (*bt_cmp)(const DBT *, const DBT *);334/* B: prefix comparison function */335size_t (*bt_pfx)(const DBT *, const DBT *);336/* R: recno input function */337int (*bt_irec)(struct _btree *, recno_t);338339FILE *bt_rfp; /* R: record FILE pointer */340int bt_rfd; /* R: record file descriptor */341342caddr_t bt_cmap; /* R: current point in mapped space */343caddr_t bt_smap; /* R: start of mapped space */344caddr_t bt_emap; /* R: end of mapped space */345size_t bt_msize; /* R: size of mapped region. */346347recno_t bt_nrecs; /* R: number of records */348size_t bt_reclen; /* R: fixed record length */349u_char bt_bval; /* R: delimiting byte/pad character */350351/*352* NB:353* B_NODUPS and R_RECNO are stored on disk, and may not be changed.354*/355#define B_INMEM 0x00001 /* in-memory tree */356#define B_METADIRTY 0x00002 /* need to write metadata */357#define B_MODIFIED 0x00004 /* tree modified */358#define B_NEEDSWAP 0x00008 /* if byte order requires swapping */359#define B_RDONLY 0x00010 /* read-only tree */360361#define B_NODUPS 0x00020 /* no duplicate keys permitted */362#define R_RECNO 0x00080 /* record oriented tree */363364#define R_CLOSEFP 0x00040 /* opened a file pointer */365#define R_EOF 0x00100 /* end of input file reached. */366#define R_FIXLEN 0x00200 /* fixed length records */367#define R_MEMMAPPED 0x00400 /* memory mapped file. */368#define R_INMEM 0x00800 /* in-memory file */369#define R_MODIFIED 0x01000 /* modified file */370#define R_RDONLY 0x02000 /* read-only file */371372#define B_DB_LOCK 0x04000 /* DB_LOCK specified. */373#define B_DB_SHMEM 0x08000 /* DB_SHMEM specified. */374#define B_DB_TXN 0x10000 /* DB_TXN specified. */375u_int32_t flags;376} BTREE;377378#include "extern.h"379380381