Path: blob/main/crypto/krb5/src/plugins/kdb/db2/libdb2/btree/btree.h
34928 views
/*-1* Copyright (c) 1991, 1993, 19942* The Regents of the University of California. All rights reserved.3*4* This code is derived from software contributed to Berkeley by5* Mike Olson.6*7* Redistribution and use in source and binary forms, with or without8* modification, are permitted provided that the following conditions9* are met:10* 1. Redistributions of source code must retain the above copyright11* notice, this list of conditions and the following disclaimer.12* 2. Redistributions in binary form must reproduce the above copyright13* notice, this list of conditions and the following disclaimer in the14* documentation and/or other materials provided with the distribution.15* 3. All advertising materials mentioning features or use of this software16* must display the following acknowledgement:17* This product includes software developed by the University of18* California, Berkeley and its contributors.19* 4. Neither the name of the University nor the names of its contributors20* may be used to endorse or promote products derived from this software21* without specific prior written permission.22*23* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND24* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE25* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE26* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE27* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL28* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS29* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)30* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT31* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY32* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF33* SUCH DAMAGE.34*35* @(#)btree.h 8.11 (Berkeley) 8/17/9436*/3738/* Macros to set/clear/test flags. */39#define F_SET(p, f) (p)->flags |= (f)40#define F_CLR(p, f) (p)->flags &= ~(f)41#define F_ISSET(p, f) ((p)->flags & (f))4243#include "mpool.h"4445#define DEFMINKEYPAGE (2) /* Minimum keys per page */46#define MINCACHE (5) /* Minimum cached pages */47#define MINPSIZE (512) /* Minimum page size */4849/*50* Page 0 of a btree file contains a copy of the meta-data. This page is also51* used as an out-of-band page, i.e. page pointers that point to nowhere point52* to page 0. Page 1 is the root of the btree.53*/54#define P_INVALID 0 /* Invalid tree page number. */55#define P_META 0 /* Tree metadata page number. */56#define P_ROOT 1 /* Tree root page number. */5758/*59* There are five page layouts in the btree: btree internal pages (BINTERNAL),60* btree leaf pages (BLEAF), recno internal pages (RINTERNAL), recno leaf pages61* (RLEAF) and overflow pages. All five page types have a page header (PAGE).62* This implementation requires that values within structures NOT be padded.63* (ANSI C permits random padding.) If your compiler pads randomly you'll have64* to do some work to get this package to run.65*/66typedef struct _page {67db_pgno_t pgno; /* this page's page number */68db_pgno_t prevpg; /* left sibling */69db_pgno_t nextpg; /* right sibling */7071#define P_BINTERNAL 0x01 /* btree internal page */72#define P_BLEAF 0x02 /* leaf page */73#define P_OVERFLOW 0x04 /* overflow page */74#define P_RINTERNAL 0x08 /* recno internal page */75#define P_RLEAF 0x10 /* leaf page */76#define P_TYPE 0x1f /* type mask */77#define P_PRESERVE 0x20 /* never delete this chain of pages */78u_int32_t flags;7980indx_t lower; /* lower bound of free space on page */81indx_t upper; /* upper bound of free space on page */82indx_t linp[1]; /* indx_t-aligned VAR. LENGTH DATA */83} PAGE;8485/* First and next index. */86#define BTDATAOFF \87(sizeof(db_pgno_t) + sizeof(db_pgno_t) + sizeof(db_pgno_t) + \88sizeof(u_int32_t) + sizeof(indx_t) + sizeof(indx_t))89#define NEXTINDEX(p) (((p)->lower - BTDATAOFF) / sizeof(indx_t))9091/*92* For pages other than overflow pages, there is an array of offsets into the93* rest of the page immediately following the page header. Each offset is to94* an item which is unique to the type of page. The h_lower offset is just95* past the last filled-in index. The h_upper offset is the first item on the96* page. Offsets are from the beginning of the page.97*98* If an item is too big to store on a single page, a flag is set and the item99* is a { page, size } pair such that the page is the first page of an overflow100* chain with size bytes of item. Overflow pages are simply bytes without any101* external structure.102*103* The page number and size fields in the items are db_pgno_t-aligned so they can104* be manipulated without copying. (This presumes that 32 bit items can be105* manipulated on this system.)106*/107#define LALIGN(n) (((n) + sizeof(db_pgno_t) - 1) & ~(sizeof(db_pgno_t) - 1))108#define NOVFLSIZE (sizeof(db_pgno_t) + sizeof(u_int32_t))109110/*111* For the btree internal pages, the item is a key. BINTERNALs are {key, pgno}112* pairs, such that the key compares less than or equal to all of the records113* on that page. For a tree without duplicate keys, an internal page with two114* consecutive keys, a and b, will have all records greater than or equal to a115* and less than b stored on the page associated with a. Duplicate keys are116* somewhat special and can cause duplicate internal and leaf page records and117* some minor modifications of the above rule.118*/119typedef struct _binternal {120u_int32_t ksize; /* key size */121db_pgno_t pgno; /* page number stored on */122#define P_BIGDATA 0x01 /* overflow data */123#define P_BIGKEY 0x02 /* overflow key */124u_char flags;125char bytes[1]; /* data */126} BINTERNAL;127128/* Get the page's BINTERNAL structure at index indx. */129#define GETBINTERNAL(pg, indx) \130((BINTERNAL *)(void *)((char *)(pg) + (pg)->linp[indx]))131132/* Get the number of bytes in the entry. */133#define NBINTERNAL(len) \134LALIGN(sizeof(u_int32_t) + sizeof(db_pgno_t) + sizeof(u_char) + (len))135136/* Copy a BINTERNAL entry to the page. */137#define WR_BINTERNAL(p, size, pgno, flags) { \138*(u_int32_t *)(void *)p = size; \139p += sizeof(u_int32_t); \140*(db_pgno_t *)(void *)p = pgno; \141p += sizeof(db_pgno_t); \142*(u_char *)p = flags; \143p += sizeof(u_char); \144}145146/*147* For the recno internal pages, the item is a page number with the number of148* keys found on that page and below.149*/150typedef struct _rinternal {151recno_t nrecs; /* number of records */152db_pgno_t pgno; /* page number stored below */153} RINTERNAL;154155/* Get the page's RINTERNAL structure at index indx. */156#define GETRINTERNAL(pg, indx) \157((RINTERNAL *)(void *)((char *)(void *)(pg) + (pg)->linp[indx]))158159/* Get the number of bytes in the entry. */160#define NRINTERNAL \161LALIGN(sizeof(recno_t) + sizeof(db_pgno_t))162163/* Copy a RINTERAL entry to the page. */164#define WR_RINTERNAL(p, nrecs, pgno) { \165*(recno_t *)(void *)p = nrecs; \166p += sizeof(recno_t); \167*(db_pgno_t *)(void *)p = pgno; \168}169170/* For the btree leaf pages, the item is a key and data pair. */171typedef struct _bleaf {172u_int32_t ksize; /* size of key */173u_int32_t dsize; /* size of data */174u_char flags; /* P_BIGDATA, P_BIGKEY */175char bytes[1]; /* data */176} BLEAF;177178/* Get the page's BLEAF structure at index indx. */179#define GETBLEAF(pg, indx) \180((BLEAF *)(void *)((char *)(pg) + (pg)->linp[indx]))181182/* Get the number of bytes in the entry. */183#define NBLEAF(p) NBLEAFDBT((p)->ksize, (p)->dsize)184185/* Get the number of bytes in the user's key/data pair. */186#define NBLEAFDBT(ksize, dsize) \187LALIGN(sizeof(u_int32_t) + sizeof(u_int32_t) + sizeof(u_char) + \188(ksize) + (dsize))189190/* Copy a BLEAF entry to the page. */191#define WR_BLEAF(p, key, data, flags) { \192*(u_int32_t *)(void *)p = key->size; \193p += sizeof(u_int32_t); \194*(u_int32_t *)(void *)p = data->size; \195p += sizeof(u_int32_t); \196*(u_char *)p = flags; \197p += sizeof(u_char); \198memmove(p, key->data, key->size); \199p += key->size; \200memmove(p, data->data, data->size); \201}202203/* For the recno leaf pages, the item is a data entry. */204typedef struct _rleaf {205u_int32_t dsize; /* size of data */206u_char flags; /* P_BIGDATA */207char bytes[1];208} RLEAF;209210/* Get the page's RLEAF structure at index indx. */211#define GETRLEAF(pg, indx) \212((RLEAF *)(void *)((char *)(pg) + (pg)->linp[indx]))213214/* Get the number of bytes in the entry. */215#define NRLEAF(p) NRLEAFDBT((p)->dsize)216217/* Get the number of bytes from the user's data. */218#define NRLEAFDBT(dsize) \219LALIGN(sizeof(u_int32_t) + sizeof(u_char) + (dsize))220221/* Copy a RLEAF entry to the page. */222#define WR_RLEAF(p, data, flags) { \223*(u_int32_t *)(void *)p = data->size; \224p += sizeof(u_int32_t); \225*(u_char *)p = flags; \226p += sizeof(u_char); \227memmove(p, data->data, data->size); \228}229230/*231* A record in the tree is either a pointer to a page and an index in the page232* or a page number and an index. These structures are used as a cursor, stack233* entry and search returns as well as to pass records to other routines.234*235* One comment about searches. Internal page searches must find the largest236* record less than key in the tree so that descents work. Leaf page searches237* must find the smallest record greater than key so that the returned index238* is the record's correct position for insertion.239*/240typedef struct _epgno {241db_pgno_t pgno; /* the page number */242indx_t index; /* the index on the page */243} EPGNO;244245typedef struct _epg {246PAGE *page; /* the (pinned) page */247indx_t index; /* the index on the page */248} EPG;249250/*251* About cursors. The cursor (and the page that contained the key/data pair252* that it referenced) can be deleted, which makes things a bit tricky. If253* there are no duplicates of the cursor key in the tree (i.e. B_NODUPS is set254* or there simply aren't any duplicates of the key) we copy the key that it255* referenced when it's deleted, and reacquire a new cursor key if the cursor256* is used again. If there are duplicates keys, we move to the next/previous257* key, and set a flag so that we know what happened. NOTE: if duplicate (to258* the cursor) keys are added to the tree during this process, it is undefined259* if they will be returned or not in a cursor scan.260*261* The flags determine the possible states of the cursor:262*263* CURS_INIT The cursor references *something*.264* CURS_ACQUIRE The cursor was deleted, and a key has been saved so that265* we can reacquire the right position in the tree.266* CURS_AFTER, CURS_BEFORE267* The cursor was deleted, and now references a key/data pair268* that has not yet been returned, either before or after the269* deleted key/data pair.270* XXX271* This structure is broken out so that we can eventually offer multiple272* cursors as part of the DB interface.273*/274typedef struct _cursor {275EPGNO pg; /* B: Saved tree reference. */276DBT key; /* B: Saved key, or key.data == NULL. */277recno_t rcursor; /* R: recno cursor (1-based) */278279#define CURS_ACQUIRE 0x01 /* B: Cursor needs to be reacquired. */280#define CURS_AFTER 0x02 /* B: Unreturned cursor after key. */281#define CURS_BEFORE 0x04 /* B: Unreturned cursor before key. */282#define CURS_INIT 0x08 /* RB: Cursor initialized. */283u_int8_t flags;284} CURSOR;285286/*287* The metadata of the tree. The nrecs field is used only by the RECNO code.288* This is because the btree doesn't really need it and it requires that every289* put or delete call modify the metadata.290*/291typedef struct _btmeta {292u_int32_t magic; /* magic number */293u_int32_t version; /* version */294u_int32_t psize; /* page size */295u_int32_t free; /* page number of first free page */296u_int32_t nrecs; /* R: number of records */297298#define SAVEMETA (B_NODUPS | R_RECNO)299u_int32_t flags; /* bt_flags & SAVEMETA */300} BTMETA;301302/* The in-memory btree/recno data structure. */303typedef struct _btree {304MPOOL *bt_mp; /* memory pool cookie */305306DB *bt_dbp; /* pointer to enclosing DB */307308EPG bt_cur; /* current (pinned) page */309PAGE *bt_pinned; /* page pinned across calls */310311CURSOR bt_cursor; /* cursor */312313#define BT_PUSH(t, p, i) { \314t->bt_sp->pgno = p; \315t->bt_sp->index = i; \316++t->bt_sp; \317}318#define BT_POP(t) (t->bt_sp == t->bt_stack ? NULL : --t->bt_sp)319#define BT_CLR(t) (t->bt_sp = t->bt_stack)320EPGNO bt_stack[50]; /* stack of parent pages */321EPGNO *bt_sp; /* current stack pointer */322323DBT bt_rkey; /* returned key */324DBT bt_rdata; /* returned data */325326int bt_fd; /* tree file descriptor */327328db_pgno_t bt_free; /* next free page */329u_int32_t bt_psize; /* page size */330indx_t bt_ovflsize; /* cut-off for key/data overflow */331int bt_lorder; /* byte order */332/* sorted order */333enum { NOT, BACK, FORWARD } bt_order;334EPGNO bt_last; /* last insert */335336/* B: key comparison function */337int (*bt_cmp) __P((const DBT *, const DBT *));338/* B: prefix comparison function */339size_t (*bt_pfx) __P((const DBT *, const DBT *));340/* R: recno input function */341int (*bt_irec) __P((struct _btree *, recno_t));342343FILE *bt_rfp; /* R: record FILE pointer */344int bt_rfd; /* R: record file descriptor */345346caddr_t bt_cmap; /* R: current point in mapped space */347caddr_t bt_smap; /* R: start of mapped space */348caddr_t bt_emap; /* R: end of mapped space */349size_t bt_msize; /* R: size of mapped region. */350351recno_t bt_nrecs; /* R: number of records */352size_t bt_reclen; /* R: fixed record length */353u_char bt_bval; /* R: delimiting byte/pad character */354355/*356* NB:357* B_NODUPS and R_RECNO are stored on disk, and may not be changed.358*/359#define B_INMEM 0x00001 /* in-memory tree */360#define B_METADIRTY 0x00002 /* need to write metadata */361#define B_MODIFIED 0x00004 /* tree modified */362#define B_NEEDSWAP 0x00008 /* if byte order requires swapping */363#define B_RDONLY 0x00010 /* read-only tree */364365#define B_NODUPS 0x00020 /* no duplicate keys permitted */366#define R_RECNO 0x00080 /* record oriented tree */367368#define R_CLOSEFP 0x00040 /* opened a file pointer */369#define R_EOF 0x00100 /* end of input file reached. */370#define R_FIXLEN 0x00200 /* fixed length records */371#define R_MEMMAPPED 0x00400 /* memory mapped file. */372#define R_INMEM 0x00800 /* in-memory file */373#define R_MODIFIED 0x01000 /* modified file */374#define R_RDONLY 0x02000 /* read-only file */375376#define B_DB_LOCK 0x04000 /* DB_LOCK specified. */377#define B_DB_SHMEM 0x08000 /* DB_SHMEM specified. */378#define B_DB_TXN 0x10000 /* DB_TXN specified. */379u_int32_t flags;380} BTREE;381382#include "extern.h"383384385