146139Smao /*- 261198Sbostic * Copyright (c) 1991, 1993 361198Sbostic * The Regents of the University of California. All rights reserved. 446139Smao * 546139Smao * This code is derived from software contributed to Berkeley by 646139Smao * Mike Olson. 746139Smao * 846139Smao * %sccs.include.redist.c% 950989Sbostic * 10*66192Sbostic * @(#)btree.h 8.5 (Berkeley) 02/21/94 1146139Smao */ 1246139Smao 1350989Sbostic #include <mpool.h> 1450989Sbostic 1550989Sbostic #define DEFMINKEYPAGE (2) /* Minimum keys per page */ 1650989Sbostic #define MINCACHE (5) /* Minimum cached pages */ 1750989Sbostic #define MINPSIZE (512) /* Minimum page size */ 1850989Sbostic 1946139Smao /* 2051104Sbostic * Page 0 of a btree file contains a copy of the meta-data. This page is also 2151104Sbostic * used as an out-of-band page, i.e. page pointers that point to nowhere point 2251104Sbostic * to page 0. Page 1 is the root of the btree. 2346139Smao */ 2450989Sbostic #define P_INVALID 0 /* Invalid tree page number. */ 2551104Sbostic #define P_META 0 /* Tree metadata page number. */ 2650989Sbostic #define P_ROOT 1 /* Tree root page number. */ 2746139Smao 2850989Sbostic /* 2951104Sbostic * There are five page layouts in the btree: btree internal pages (BINTERNAL), 3051104Sbostic * btree leaf pages (BLEAF), recno internal pages (RINTERNAL), recno leaf pages 3151104Sbostic * (RLEAF) and overflow pages. All five page types have a page header (PAGE). 32*66192Sbostic * This implementation requires that values within structures NOT be padded. 3351104Sbostic * (ANSI C permits random padding.) If your compiler pads randomly you'll have 3451104Sbostic * to do some work to get this package to run. 3550989Sbostic */ 3664458Sbostic typedef struct _page { 3750989Sbostic pgno_t pgno; /* this page's page number */ 3850989Sbostic pgno_t prevpg; /* left sibling */ 3950989Sbostic pgno_t nextpg; /* right sibling */ 4046139Smao 4150989Sbostic #define P_BINTERNAL 0x01 /* btree internal page */ 4250989Sbostic #define P_BLEAF 0x02 /* leaf page */ 4350989Sbostic #define P_OVERFLOW 0x04 /* overflow page */ 4450989Sbostic #define P_RINTERNAL 0x08 /* recno internal page */ 4550989Sbostic #define P_RLEAF 0x10 /* leaf page */ 4650989Sbostic #define P_TYPE 0x1f /* type mask */ 4750989Sbostic #define P_PRESERVE 0x20 /* never delete this chain of pages */ 48*66192Sbostic u_int32_t flags; 4946139Smao 5057989Sbostic indx_t lower; /* lower bound of free space on page */ 5157989Sbostic indx_t upper; /* upper bound of free space on page */ 52*66192Sbostic indx_t linp[1]; /* indx_t-aligned VAR. LENGTH DATA */ 5350989Sbostic } PAGE; 5446139Smao 5550989Sbostic /* First and next index. */ 5651969Sbostic #define BTDATAOFF (sizeof(pgno_t) + sizeof(pgno_t) + sizeof(pgno_t) + \ 57*66192Sbostic sizeof(u_int32_t) + sizeof(indx_t) + sizeof(indx_t)) 5857989Sbostic #define NEXTINDEX(p) (((p)->lower - BTDATAOFF) / sizeof(indx_t)) 5946139Smao 6046139Smao /* 6150989Sbostic * For pages other than overflow pages, there is an array of offsets into the 6250989Sbostic * rest of the page immediately following the page header. Each offset is to 6350989Sbostic * an item which is unique to the type of page. The h_lower offset is just 6450989Sbostic * past the last filled-in index. The h_upper offset is the first item on the 6550989Sbostic * page. Offsets are from the beginning of the page. 6650989Sbostic * 6750989Sbostic * If an item is too big to store on a single page, a flag is set and the item 6850989Sbostic * is a { page, size } pair such that the page is the first page of an overflow 6950989Sbostic * chain with size bytes of item. Overflow pages are simply bytes without any 7050989Sbostic * external structure. 7150989Sbostic * 72*66192Sbostic * The page number and size fields in the items are pgno_t-aligned so they can 73*66192Sbostic * be manipulated without copying. (This presumes that 32 bit items can be 74*66192Sbostic * manipulated on this system.) 7546139Smao */ 76*66192Sbostic #define LALIGN(n) \ 77*66192Sbostic (((n) + sizeof(pgno_t) - 1) & ~(sizeof(pgno_t) - 1)) 7850989Sbostic #define NOVFLSIZE (sizeof(pgno_t) + sizeof(size_t)) 7946139Smao 8046139Smao /* 8150989Sbostic * For the btree internal pages, the item is a key. BINTERNALs are {key, pgno} 8250989Sbostic * pairs, such that the key compares less than or equal to all of the records 8350989Sbostic * on that page. For a tree without duplicate keys, an internal page with two 8450989Sbostic * consecutive keys, a and b, will have all records greater than or equal to a 8550989Sbostic * and less than b stored on the page associated with a. Duplicate keys are 8650989Sbostic * somewhat special and can cause duplicate internal and leaf page records and 8750989Sbostic * some minor modifications of the above rule. 8846139Smao */ 8964458Sbostic typedef struct _binternal { 9050989Sbostic size_t ksize; /* key size */ 9150989Sbostic pgno_t pgno; /* page number stored on */ 9250989Sbostic #define P_BIGDATA 0x01 /* overflow data */ 9350989Sbostic #define P_BIGKEY 0x02 /* overflow key */ 9450989Sbostic u_char flags; 9550989Sbostic char bytes[1]; /* data */ 9650989Sbostic } BINTERNAL; 9746139Smao 9850989Sbostic /* Get the page's BINTERNAL structure at index indx. */ 9950989Sbostic #define GETBINTERNAL(pg, indx) \ 10050989Sbostic ((BINTERNAL *)((char *)(pg) + (pg)->linp[indx])) 10146139Smao 10250989Sbostic /* Get the number of bytes in the entry. */ 10350989Sbostic #define NBINTERNAL(len) \ 10450989Sbostic LALIGN(sizeof(size_t) + sizeof(pgno_t) + sizeof(u_char) + (len)) 10546139Smao 10650989Sbostic /* Copy a BINTERNAL entry to the page. */ 10750989Sbostic #define WR_BINTERNAL(p, size, pgno, flags) { \ 10851764Sbostic *(size_t *)p = size; \ 10951764Sbostic p += sizeof(size_t); \ 11051764Sbostic *(pgno_t *)p = pgno; \ 11151764Sbostic p += sizeof(pgno_t); \ 11251764Sbostic *(u_char *)p = flags; \ 11351764Sbostic p += sizeof(u_char); \ 11450989Sbostic } 11546139Smao 11646139Smao /* 11750989Sbostic * For the recno internal pages, the item is a page number with the number of 11850989Sbostic * keys found on that page and below. 11946139Smao */ 12064458Sbostic typedef struct _rinternal { 12150989Sbostic recno_t nrecs; /* number of records */ 12250989Sbostic pgno_t pgno; /* page number stored below */ 12350989Sbostic } RINTERNAL; 12446139Smao 12550989Sbostic /* Get the page's RINTERNAL structure at index indx. */ 12650989Sbostic #define GETRINTERNAL(pg, indx) \ 12750989Sbostic ((RINTERNAL *)((char *)(pg) + (pg)->linp[indx])) 12846139Smao 12950989Sbostic /* Get the number of bytes in the entry. */ 13050989Sbostic #define NRINTERNAL \ 13150989Sbostic LALIGN(sizeof(recno_t) + sizeof(pgno_t)) 13246139Smao 13350989Sbostic /* Copy a RINTERAL entry to the page. */ 13450989Sbostic #define WR_RINTERNAL(p, nrecs, pgno) { \ 13551764Sbostic *(recno_t *)p = nrecs; \ 13651764Sbostic p += sizeof(recno_t); \ 13750989Sbostic *(pgno_t *)p = pgno; \ 13850989Sbostic } 13946139Smao 14050989Sbostic /* For the btree leaf pages, the item is a key and data pair. */ 14164458Sbostic typedef struct _bleaf { 14250989Sbostic size_t ksize; /* size of key */ 14350989Sbostic size_t dsize; /* size of data */ 14450989Sbostic u_char flags; /* P_BIGDATA, P_BIGKEY */ 14550989Sbostic char bytes[1]; /* data */ 14650989Sbostic } BLEAF; 14746139Smao 14850989Sbostic /* Get the page's BLEAF structure at index indx. */ 14950989Sbostic #define GETBLEAF(pg, indx) \ 15050989Sbostic ((BLEAF *)((char *)(pg) + (pg)->linp[indx])) 15146139Smao 15250989Sbostic /* Get the number of bytes in the entry. */ 15351104Sbostic #define NBLEAF(p) NBLEAFDBT((p)->ksize, (p)->dsize) 15446139Smao 15550989Sbostic /* Get the number of bytes in the user's key/data pair. */ 15650989Sbostic #define NBLEAFDBT(ksize, dsize) \ 15750989Sbostic LALIGN(sizeof(size_t) + sizeof(size_t) + sizeof(u_char) + \ 15850989Sbostic (ksize) + (dsize)) 15946139Smao 16050989Sbostic /* Copy a BLEAF entry to the page. */ 16150989Sbostic #define WR_BLEAF(p, key, data, flags) { \ 16251764Sbostic *(size_t *)p = key->size; \ 16351764Sbostic p += sizeof(size_t); \ 16451764Sbostic *(size_t *)p = data->size; \ 16551764Sbostic p += sizeof(size_t); \ 16651764Sbostic *(u_char *)p = flags; \ 16751764Sbostic p += sizeof(u_char); \ 16858017Sbostic memmove(p, key->data, key->size); \ 16950989Sbostic p += key->size; \ 17058017Sbostic memmove(p, data->data, data->size); \ 17150989Sbostic } 17246139Smao 17350989Sbostic /* For the recno leaf pages, the item is a data entry. */ 17464458Sbostic typedef struct _rleaf { 17550989Sbostic size_t dsize; /* size of data */ 17650989Sbostic u_char flags; /* P_BIGDATA */ 17750989Sbostic char bytes[1]; 17850989Sbostic } RLEAF; 17946139Smao 18050989Sbostic /* Get the page's RLEAF structure at index indx. */ 18150989Sbostic #define GETRLEAF(pg, indx) \ 18250989Sbostic ((RLEAF *)((char *)(pg) + (pg)->linp[indx])) 18346139Smao 18450989Sbostic /* Get the number of bytes in the entry. */ 18551104Sbostic #define NRLEAF(p) NRLEAFDBT((p)->dsize) 18646139Smao 18750989Sbostic /* Get the number of bytes from the user's data. */ 18850989Sbostic #define NRLEAFDBT(dsize) \ 18950989Sbostic LALIGN(sizeof(size_t) + sizeof(u_char) + (dsize)) 19046139Smao 19150989Sbostic /* Copy a RLEAF entry to the page. */ 19250989Sbostic #define WR_RLEAF(p, data, flags) { \ 19351764Sbostic *(size_t *)p = data->size; \ 19451764Sbostic p += sizeof(size_t); \ 19551764Sbostic *(u_char *)p = flags; \ 19651764Sbostic p += sizeof(u_char); \ 19758017Sbostic memmove(p, data->data, data->size); \ 19850989Sbostic } 19946139Smao 20046139Smao /* 20150989Sbostic * A record in the tree is either a pointer to a page and an index in the page 20250989Sbostic * or a page number and an index. These structures are used as a cursor, stack 20350989Sbostic * entry and search returns as well as to pass records to other routines. 20450989Sbostic * 20550989Sbostic * One comment about searches. Internal page searches must find the largest 20650989Sbostic * record less than key in the tree so that descents work. Leaf page searches 20750989Sbostic * must find the smallest record greater than key so that the returned index 20850989Sbostic * is the record's correct position for insertion. 20951104Sbostic * 21051104Sbostic * One comment about cursors. The cursor key is never removed from the tree, 21151104Sbostic * even if deleted. This is because it is quite difficult to decide where the 21251104Sbostic * cursor should be when other keys have been inserted/deleted in the tree; 21351104Sbostic * duplicate keys make it impossible. This scheme does require extra work 21451104Sbostic * though, to make sure that we don't perform an operation on a deleted key. 21546139Smao */ 21664458Sbostic typedef struct _epgno { 21750989Sbostic pgno_t pgno; /* the page number */ 21857989Sbostic indx_t index; /* the index on the page */ 21950989Sbostic } EPGNO; 22046139Smao 22164458Sbostic typedef struct _epg { 22250989Sbostic PAGE *page; /* the (pinned) page */ 22357989Sbostic indx_t index; /* the index on the page */ 22450989Sbostic } EPG; 22546139Smao 22651104Sbostic /* 22751104Sbostic * The metadata of the tree. The m_nrecs field is used only by the RECNO code. 22851104Sbostic * This is because the btree doesn't really need it and it requires that every 22951104Sbostic * put or delete call modify the metadata. 23051104Sbostic */ 23164458Sbostic typedef struct _btmeta { 232*66192Sbostic u_int32_t m_magic; /* magic number */ 233*66192Sbostic u_int32_t m_version; /* version */ 234*66192Sbostic u_int32_t m_psize; /* page size */ 235*66192Sbostic u_int32_t m_free; /* page number of first free page */ 236*66192Sbostic u_int32_t m_nrecs; /* R: number of records */ 23760044Sbostic #define SAVEMETA (B_NODUPS | R_RECNO) 238*66192Sbostic u_int32_t m_flags; /* bt_flags & SAVEMETA */ 239*66192Sbostic u_int32_t m_unused; /* unused */ 24051104Sbostic } BTMETA; 24151104Sbostic 24250989Sbostic /* The in-memory btree/recno data structure. */ 24364458Sbostic typedef struct _btree { 24450989Sbostic MPOOL *bt_mp; /* memory pool cookie */ 24546139Smao 24650989Sbostic DB *bt_dbp; /* pointer to enclosing DB */ 24746139Smao 24864484Sbostic EPG bt_cur; /* current (pinned) page */ 24964458Sbostic PAGE *bt_pinned; /* page pinned across calls */ 25064458Sbostic 25151104Sbostic EPGNO bt_bcursor; /* B: btree cursor */ 25251104Sbostic recno_t bt_rcursor; /* R: recno cursor (1-based) */ 25346139Smao 25450989Sbostic #define BT_POP(t) (t->bt_sp ? t->bt_stack + --t->bt_sp : NULL) 25550989Sbostic #define BT_CLR(t) (t->bt_sp = 0) 25650989Sbostic EPGNO *bt_stack; /* stack of parent pages */ 25750989Sbostic u_int bt_sp; /* current stack pointer */ 25850989Sbostic u_int bt_maxstack; /* largest stack */ 25946139Smao 26050989Sbostic char *bt_kbuf; /* key buffer */ 26150989Sbostic size_t bt_kbufsz; /* key buffer size */ 26250989Sbostic char *bt_dbuf; /* data buffer */ 26350989Sbostic size_t bt_dbufsz; /* data buffer size */ 26446139Smao 26550989Sbostic int bt_fd; /* tree file descriptor */ 26646139Smao 26756491Sbostic pgno_t bt_free; /* next free page */ 268*66192Sbostic u_int32_t bt_psize; /* page size */ 26957989Sbostic indx_t bt_ovflsize; /* cut-off for key/data overflow */ 27050989Sbostic int bt_lorder; /* byte order */ 27150989Sbostic /* sorted order */ 27265176Sbostic enum { NOT, BACK, FORWARD } bt_order; 27350989Sbostic EPGNO bt_last; /* last insert */ 27446139Smao 27550989Sbostic /* B: key comparison function */ 27650989Sbostic int (*bt_cmp) __P((const DBT *, const DBT *)); 27750989Sbostic /* B: prefix comparison function */ 278*66192Sbostic size_t (*bt_pfx) __P((const DBT *, const DBT *)); 27950989Sbostic /* R: recno input function */ 28064458Sbostic int (*bt_irec) __P((struct _btree *, recno_t)); 28158746Sbostic 28258746Sbostic FILE *bt_rfp; /* R: record FILE pointer */ 28358746Sbostic int bt_rfd; /* R: record file descriptor */ 28458746Sbostic 28558746Sbostic caddr_t bt_cmap; /* R: current point in mapped space */ 28650989Sbostic caddr_t bt_smap; /* R: start of mapped space */ 28750989Sbostic caddr_t bt_emap; /* R: end of mapped space */ 28858746Sbostic size_t bt_msize; /* R: size of mapped region. */ 28958746Sbostic 29058746Sbostic recno_t bt_nrecs; /* R: number of records */ 29150989Sbostic size_t bt_reclen; /* R: fixed record length */ 29250989Sbostic u_char bt_bval; /* R: delimiting byte/pad character */ 29346139Smao 29459629Sbostic /* 29559629Sbostic * NB: 29660044Sbostic * B_NODUPS and R_RECNO are stored on disk, and may not be changed. 29759629Sbostic */ 29860044Sbostic #define B_DELCRSR 0x00001 /* cursor has been deleted */ 29960044Sbostic #define B_INMEM 0x00002 /* in-memory tree */ 30060044Sbostic #define B_METADIRTY 0x00004 /* need to write metadata */ 30160044Sbostic #define B_MODIFIED 0x00008 /* tree modified */ 30260044Sbostic #define B_NEEDSWAP 0x00010 /* if byte order requires swapping */ 30360044Sbostic #define B_NODUPS 0x00020 /* no duplicate keys permitted */ 30460044Sbostic #define B_RDONLY 0x00040 /* read-only tree */ 30564458Sbostic #define R_RECNO 0x00080 /* record oriented tree */ 30660044Sbostic #define B_SEQINIT 0x00100 /* sequential scan initialized */ 30759629Sbostic 30860044Sbostic #define R_CLOSEFP 0x00200 /* opened a file pointer */ 30960044Sbostic #define R_EOF 0x00400 /* end of input file reached. */ 31060044Sbostic #define R_FIXLEN 0x00800 /* fixed length records */ 31160044Sbostic #define R_MEMMAPPED 0x01000 /* memory mapped file. */ 31260044Sbostic #define R_INMEM 0x02000 /* in-memory file */ 31360044Sbostic #define R_MODIFIED 0x04000 /* modified file */ 31460044Sbostic #define R_RDONLY 0x08000 /* read-only file */ 31560044Sbostic 31664458Sbostic #define B_DB_LOCK 0x10000 /* DB_LOCK specified. */ 31764458Sbostic #define B_DB_SHMEM 0x20000 /* DB_SHMEM specified. */ 31864458Sbostic #define B_DB_TXN 0x40000 /* DB_TXN specified. */ 31964458Sbostic 320*66192Sbostic u_int32_t bt_flags; /* btree state */ 32150989Sbostic } BTREE; 32246139Smao 32356736Sbostic #define SET(t, f) ((t)->bt_flags |= (f)) 32456736Sbostic #define CLR(t, f) ((t)->bt_flags &= ~(f)) 32550989Sbostic #define ISSET(t, f) ((t)->bt_flags & (f)) 32646139Smao 32750989Sbostic #include "extern.h" 328