1 /*- 2 * Copyright (c) 1991 The Regents of the University of California. 3 * All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * Mike Olson. 7 * 8 * %sccs.include.redist.c% 9 * 10 * @(#)btree.h 5.4 (Berkeley) 09/12/91 11 */ 12 13 #include <mpool.h> 14 15 #define DEFMINKEYPAGE (2) /* Minimum keys per page */ 16 #define MINCACHE (5) /* Minimum cached pages */ 17 #define MINPSIZE (512) /* Minimum page size */ 18 19 /* 20 * Page 0 of a btree file contains a copy of the meta-data. This page is also 21 * used as an out-of-band page, i.e. page pointers that point to nowhere point 22 * to page 0. Page 1 is the root of the btree. 23 */ 24 #define P_INVALID 0 /* Invalid tree page number. */ 25 #define P_META 0 /* Tree metadata page number. */ 26 #define P_ROOT 1 /* Tree root page number. */ 27 28 /* 29 * There are five page layouts in the btree: btree internal pages (BINTERNAL), 30 * btree leaf pages (BLEAF), recno internal pages (RINTERNAL), recno leaf pages 31 * (RLEAF) and overflow pages. All five page types have a page header (PAGE). 32 * This implementation requires that longs within structures are NOT padded. 33 * (ANSI C permits random padding.) If your compiler pads randomly you'll have 34 * to do some work to get this package to run. 35 */ 36 typedef struct PAGE { 37 pgno_t pgno; /* this page's page number */ 38 pgno_t prevpg; /* left sibling */ 39 pgno_t nextpg; /* right sibling */ 40 41 #define P_BINTERNAL 0x01 /* btree internal page */ 42 #define P_BLEAF 0x02 /* leaf page */ 43 #define P_OVERFLOW 0x04 /* overflow page */ 44 #define P_RINTERNAL 0x08 /* recno internal page */ 45 #define P_RLEAF 0x10 /* leaf page */ 46 #define P_TYPE 0x1f /* type mask */ 47 48 #define P_PRESERVE 0x20 /* never delete this chain of pages */ 49 u_long flags; 50 51 index_t lower; /* lower bound of free space on page */ 52 index_t upper; /* upper bound of free space on page */ 53 index_t linp[1]; /* long-aligned VARIABLE LENGTH DATA */ 54 } PAGE; 55 56 /* First and next index. */ 57 #define BTDATAOFF (sizeof(PAGE) - sizeof(index_t)) 58 #define NEXTINDEX(p) (((p)->lower - BTDATAOFF) / sizeof(index_t)) 59 60 /* 61 * For pages other than overflow pages, there is an array of offsets into the 62 * rest of the page immediately following the page header. Each offset is to 63 * an item which is unique to the type of page. The h_lower offset is just 64 * past the last filled-in index. The h_upper offset is the first item on the 65 * page. Offsets are from the beginning of the page. 66 * 67 * If an item is too big to store on a single page, a flag is set and the item 68 * is a { page, size } pair such that the page is the first page of an overflow 69 * chain with size bytes of item. Overflow pages are simply bytes without any 70 * external structure. 71 * 72 * The size and page number fields in the items are long aligned so they can be 73 * manipulated without copying. 74 */ 75 #define LALIGN(n) (((n) + sizeof(u_long) - 1) & ~(sizeof(u_long) - 1)) 76 #define NOVFLSIZE (sizeof(pgno_t) + sizeof(size_t)) 77 78 /* 79 * For the btree internal pages, the item is a key. BINTERNALs are {key, pgno} 80 * pairs, such that the key compares less than or equal to all of the records 81 * on that page. For a tree without duplicate keys, an internal page with two 82 * consecutive keys, a and b, will have all records greater than or equal to a 83 * and less than b stored on the page associated with a. Duplicate keys are 84 * somewhat special and can cause duplicate internal and leaf page records and 85 * some minor modifications of the above rule. 86 */ 87 typedef struct BINTERNAL { 88 size_t ksize; /* key size */ 89 pgno_t pgno; /* page number stored on */ 90 #define P_BIGDATA 0x01 /* overflow data */ 91 #define P_BIGKEY 0x02 /* overflow key */ 92 u_char flags; 93 char bytes[1]; /* data */ 94 } BINTERNAL; 95 96 /* Get the page's BINTERNAL structure at index indx. */ 97 #define GETBINTERNAL(pg, indx) \ 98 ((BINTERNAL *)((char *)(pg) + (pg)->linp[indx])) 99 100 /* Get the number of bytes in the entry. */ 101 #define NBINTERNAL(len) \ 102 LALIGN(sizeof(size_t) + sizeof(pgno_t) + sizeof(u_char) + (len)) 103 104 /* Copy a BINTERNAL entry to the page. */ 105 #define WR_BINTERNAL(p, size, pgno, flags) { \ 106 *((size_t *)p)++ = size; \ 107 *((pgno_t *)p)++ = pgno; \ 108 *((u_char *)p)++ = flags; \ 109 } 110 111 /* 112 * For the recno internal pages, the item is a page number with the number of 113 * keys found on that page and below. 114 */ 115 typedef struct RINTERNAL { 116 recno_t nrecs; /* number of records */ 117 pgno_t pgno; /* page number stored below */ 118 } RINTERNAL; 119 120 /* Get the page's RINTERNAL structure at index indx. */ 121 #define GETRINTERNAL(pg, indx) \ 122 ((RINTERNAL *)((char *)(pg) + (pg)->linp[indx])) 123 124 /* Get the number of bytes in the entry. */ 125 #define NRINTERNAL \ 126 LALIGN(sizeof(recno_t) + sizeof(pgno_t)) 127 128 /* Copy a RINTERAL entry to the page. */ 129 #define WR_RINTERNAL(p, nrecs, pgno) { \ 130 *((recno_t *)p)++ = nrecs; \ 131 *(pgno_t *)p = pgno; \ 132 } 133 134 /* For the btree leaf pages, the item is a key and data pair. */ 135 typedef struct BLEAF { 136 size_t ksize; /* size of key */ 137 size_t dsize; /* size of data */ 138 u_char flags; /* P_BIGDATA, P_BIGKEY */ 139 char bytes[1]; /* data */ 140 } BLEAF; 141 142 /* Get the page's BLEAF structure at index indx. */ 143 #define GETBLEAF(pg, indx) \ 144 ((BLEAF *)((char *)(pg) + (pg)->linp[indx])) 145 146 /* Get the number of bytes in the entry. */ 147 #define NBLEAF(p) NBLEAFDBT((p)->ksize, (p)->dsize) 148 149 /* Get the number of bytes in the user's key/data pair. */ 150 #define NBLEAFDBT(ksize, dsize) \ 151 LALIGN(sizeof(size_t) + sizeof(size_t) + sizeof(u_char) + \ 152 (ksize) + (dsize)) 153 154 /* Copy a BLEAF entry to the page. */ 155 #define WR_BLEAF(p, key, data, flags) { \ 156 *((size_t *)p)++ = key->size; \ 157 *((size_t *)p)++ = data->size; \ 158 *((u_char *)p)++ = flags; \ 159 bcopy(key->data, p, key->size); \ 160 p += key->size; \ 161 bcopy(data->data, p, data->size); \ 162 } 163 164 /* For the recno leaf pages, the item is a data entry. */ 165 typedef struct RLEAF { 166 size_t dsize; /* size of data */ 167 u_char flags; /* P_BIGDATA */ 168 char bytes[1]; 169 } RLEAF; 170 171 /* Get the page's RLEAF structure at index indx. */ 172 #define GETRLEAF(pg, indx) \ 173 ((RLEAF *)((char *)(pg) + (pg)->linp[indx])) 174 175 /* Get the number of bytes in the entry. */ 176 #define NRLEAF(p) NRLEAFDBT((p)->dsize) 177 178 /* Get the number of bytes from the user's data. */ 179 #define NRLEAFDBT(dsize) \ 180 LALIGN(sizeof(size_t) + sizeof(u_char) + (dsize)) 181 182 /* Copy a RLEAF entry to the page. */ 183 #define WR_RLEAF(p, data, flags) { \ 184 *((size_t *)p)++ = data->size; \ 185 *((u_char *)p)++ = flags; \ 186 bcopy(data->data, p, data->size); \ 187 } 188 189 /* 190 * A record in the tree is either a pointer to a page and an index in the page 191 * or a page number and an index. These structures are used as a cursor, stack 192 * entry and search returns as well as to pass records to other routines. 193 * 194 * One comment about searches. Internal page searches must find the largest 195 * record less than key in the tree so that descents work. Leaf page searches 196 * must find the smallest record greater than key so that the returned index 197 * is the record's correct position for insertion. 198 * 199 * One comment about cursors. The cursor key is never removed from the tree, 200 * even if deleted. This is because it is quite difficult to decide where the 201 * cursor should be when other keys have been inserted/deleted in the tree; 202 * duplicate keys make it impossible. This scheme does require extra work 203 * though, to make sure that we don't perform an operation on a deleted key. 204 */ 205 typedef struct EPGNO { 206 pgno_t pgno; /* the page number */ 207 index_t index; /* the index on the page */ 208 } EPGNO; 209 210 typedef struct EPG { 211 PAGE *page; /* the (pinned) page */ 212 index_t index; /* the index on the page */ 213 } EPG; 214 215 /* 216 * The metadata of the tree. The m_nrecs field is used only by the RECNO code. 217 * This is because the btree doesn't really need it and it requires that every 218 * put or delete call modify the metadata. 219 */ 220 typedef struct BTMETA { 221 u_long m_magic; /* magic number */ 222 u_long m_version; /* version */ 223 u_long m_psize; /* page size */ 224 u_long m_free; /* page number of first free page */ 225 u_long m_nrecs; /* R: number of records */ 226 #define SAVEMETA (BTF_NODUPS | BTF_RECNO) 227 u_long m_flags; /* bt_flags & SAVEMETA */ 228 u_long m_lorder; /* byte order */ 229 } BTMETA; 230 231 /* The in-memory btree/recno data structure. */ 232 typedef struct BTREE { 233 MPOOL *bt_mp; /* memory pool cookie */ 234 235 DB *bt_dbp; /* pointer to enclosing DB */ 236 237 EPGNO bt_bcursor; /* B: btree cursor */ 238 recno_t bt_rcursor; /* R: recno cursor (1-based) */ 239 240 #define BT_POP(t) (t->bt_sp ? t->bt_stack + --t->bt_sp : NULL) 241 #define BT_CLR(t) (t->bt_sp = 0) 242 EPGNO *bt_stack; /* stack of parent pages */ 243 u_int bt_sp; /* current stack pointer */ 244 u_int bt_maxstack; /* largest stack */ 245 246 char *bt_kbuf; /* key buffer */ 247 size_t bt_kbufsz; /* key buffer size */ 248 char *bt_dbuf; /* data buffer */ 249 size_t bt_dbufsz; /* data buffer size */ 250 251 int bt_fd; /* tree file descriptor */ 252 FILE *bt_rfp; /* R: record FILE pointer */ 253 int bt_rfd; /* R: record file descriptor */ 254 255 pgno_t bt_free; /* XXX next free page */ 256 index_t bt_psize; /* page size */ 257 index_t bt_ovflsize; /* cut-off for key/data overflow */ 258 int bt_lorder; /* byte order */ 259 /* sorted order */ 260 enum { NOT, BACK, FORWARD, } bt_order; 261 EPGNO bt_last; /* last insert */ 262 263 /* B: key comparison function */ 264 int (*bt_cmp) __P((const DBT *, const DBT *)); 265 /* B: prefix comparison function */ 266 int (*bt_pfx) __P((const DBT *, const DBT *)); 267 /* R: recno input function */ 268 int (*bt_irec) __P((struct BTREE *, recno_t)); 269 recno_t bt_nrecs; /* R: number of records */ 270 caddr_t bt_smap; /* R: start of mapped space */ 271 caddr_t bt_emap; /* R: end of mapped space */ 272 size_t bt_reclen; /* R: fixed record length */ 273 u_char bt_bval; /* R: delimiting byte/pad character */ 274 275 #define BTF_DELCRSR 0x001 /* cursor has been deleted */ 276 #define BTF_FIXEDLEN 0x002 /* fixed length records */ 277 #define BTF_INMEM 0x004 /* in-memory tree */ 278 #define BTF_METADIRTY 0x008 /* B: need to write metadata */ 279 #define BTF_MODIFIED 0x010 /* tree modified */ 280 #define BTF_NODUPS 0x020 /* B: no duplicate keys permitted */ 281 #define BTF_RDONLY 0x040 /* read-only tree */ 282 #define BTF_RECNO 0x080 /* record oriented tree */ 283 #define BTF_SEQINIT 0x100 /* sequential scan initialized */ 284 u_long bt_flags; /* btree state */ 285 } BTREE; 286 287 #define ISSET(t, f) ((t)->bt_flags & (f)) 288 #define NOTSET(t, f) (!((t)->bt_flags & (f))) 289 #define SET(t, f) ((t)->bt_flags |= (f)) 290 #define UNSET(t, f) ((t)->bt_flags &= ~(f)) 291 292 #include "extern.h" 293