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.13 (Berkeley) 05/01/93 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 indx_t lower; /* lower bound of free space on page */ 52 indx_t upper; /* upper bound of free space on page */ 53 indx_t linp[1]; /* long-aligned VARIABLE LENGTH DATA */ 54 } PAGE; 55 56 /* First and next index. */ 57 #define BTDATAOFF (sizeof(pgno_t) + sizeof(pgno_t) + sizeof(pgno_t) + \ 58 sizeof(u_long) + sizeof(indx_t) + sizeof(indx_t)) 59 #define NEXTINDEX(p) (((p)->lower - BTDATAOFF) / sizeof(indx_t)) 60 61 /* 62 * For pages other than overflow pages, there is an array of offsets into the 63 * rest of the page immediately following the page header. Each offset is to 64 * an item which is unique to the type of page. The h_lower offset is just 65 * past the last filled-in index. The h_upper offset is the first item on the 66 * page. Offsets are from the beginning of the page. 67 * 68 * If an item is too big to store on a single page, a flag is set and the item 69 * is a { page, size } pair such that the page is the first page of an overflow 70 * chain with size bytes of item. Overflow pages are simply bytes without any 71 * external structure. 72 * 73 * The size and page number fields in the items are long aligned so they can be 74 * manipulated without copying. 75 */ 76 #define LALIGN(n) (((n) + sizeof(u_long) - 1) & ~(sizeof(u_long) - 1)) 77 #define NOVFLSIZE (sizeof(pgno_t) + sizeof(size_t)) 78 79 /* 80 * For the btree internal pages, the item is a key. BINTERNALs are {key, pgno} 81 * pairs, such that the key compares less than or equal to all of the records 82 * on that page. For a tree without duplicate keys, an internal page with two 83 * consecutive keys, a and b, will have all records greater than or equal to a 84 * and less than b stored on the page associated with a. Duplicate keys are 85 * somewhat special and can cause duplicate internal and leaf page records and 86 * some minor modifications of the above rule. 87 */ 88 typedef struct BINTERNAL { 89 size_t ksize; /* key size */ 90 pgno_t pgno; /* page number stored on */ 91 #define P_BIGDATA 0x01 /* overflow data */ 92 #define P_BIGKEY 0x02 /* overflow key */ 93 u_char flags; 94 char bytes[1]; /* data */ 95 } BINTERNAL; 96 97 /* Get the page's BINTERNAL structure at index indx. */ 98 #define GETBINTERNAL(pg, indx) \ 99 ((BINTERNAL *)((char *)(pg) + (pg)->linp[indx])) 100 101 /* Get the number of bytes in the entry. */ 102 #define NBINTERNAL(len) \ 103 LALIGN(sizeof(size_t) + sizeof(pgno_t) + sizeof(u_char) + (len)) 104 105 /* Copy a BINTERNAL entry to the page. */ 106 #define WR_BINTERNAL(p, size, pgno, flags) { \ 107 *(size_t *)p = size; \ 108 p += sizeof(size_t); \ 109 *(pgno_t *)p = pgno; \ 110 p += sizeof(pgno_t); \ 111 *(u_char *)p = flags; \ 112 p += sizeof(u_char); \ 113 } 114 115 /* 116 * For the recno internal pages, the item is a page number with the number of 117 * keys found on that page and below. 118 */ 119 typedef struct RINTERNAL { 120 recno_t nrecs; /* number of records */ 121 pgno_t pgno; /* page number stored below */ 122 } RINTERNAL; 123 124 /* Get the page's RINTERNAL structure at index indx. */ 125 #define GETRINTERNAL(pg, indx) \ 126 ((RINTERNAL *)((char *)(pg) + (pg)->linp[indx])) 127 128 /* Get the number of bytes in the entry. */ 129 #define NRINTERNAL \ 130 LALIGN(sizeof(recno_t) + sizeof(pgno_t)) 131 132 /* Copy a RINTERAL entry to the page. */ 133 #define WR_RINTERNAL(p, nrecs, pgno) { \ 134 *(recno_t *)p = nrecs; \ 135 p += sizeof(recno_t); \ 136 *(pgno_t *)p = pgno; \ 137 } 138 139 /* For the btree leaf pages, the item is a key and data pair. */ 140 typedef struct BLEAF { 141 size_t ksize; /* size of key */ 142 size_t dsize; /* size of data */ 143 u_char flags; /* P_BIGDATA, P_BIGKEY */ 144 char bytes[1]; /* data */ 145 } BLEAF; 146 147 /* Get the page's BLEAF structure at index indx. */ 148 #define GETBLEAF(pg, indx) \ 149 ((BLEAF *)((char *)(pg) + (pg)->linp[indx])) 150 151 /* Get the number of bytes in the entry. */ 152 #define NBLEAF(p) NBLEAFDBT((p)->ksize, (p)->dsize) 153 154 /* Get the number of bytes in the user's key/data pair. */ 155 #define NBLEAFDBT(ksize, dsize) \ 156 LALIGN(sizeof(size_t) + sizeof(size_t) + sizeof(u_char) + \ 157 (ksize) + (dsize)) 158 159 /* Copy a BLEAF entry to the page. */ 160 #define WR_BLEAF(p, key, data, flags) { \ 161 *(size_t *)p = key->size; \ 162 p += sizeof(size_t); \ 163 *(size_t *)p = data->size; \ 164 p += sizeof(size_t); \ 165 *(u_char *)p = flags; \ 166 p += sizeof(u_char); \ 167 memmove(p, key->data, key->size); \ 168 p += key->size; \ 169 memmove(p, data->data, data->size); \ 170 } 171 172 /* For the recno leaf pages, the item is a data entry. */ 173 typedef struct RLEAF { 174 size_t dsize; /* size of data */ 175 u_char flags; /* P_BIGDATA */ 176 char bytes[1]; 177 } RLEAF; 178 179 /* Get the page's RLEAF structure at index indx. */ 180 #define GETRLEAF(pg, indx) \ 181 ((RLEAF *)((char *)(pg) + (pg)->linp[indx])) 182 183 /* Get the number of bytes in the entry. */ 184 #define NRLEAF(p) NRLEAFDBT((p)->dsize) 185 186 /* Get the number of bytes from the user's data. */ 187 #define NRLEAFDBT(dsize) \ 188 LALIGN(sizeof(size_t) + sizeof(u_char) + (dsize)) 189 190 /* Copy a RLEAF entry to the page. */ 191 #define WR_RLEAF(p, data, flags) { \ 192 *(size_t *)p = data->size; \ 193 p += sizeof(size_t); \ 194 *(u_char *)p = flags; \ 195 p += sizeof(u_char); \ 196 memmove(p, data->data, data->size); \ 197 } 198 199 /* 200 * A record in the tree is either a pointer to a page and an index in the page 201 * or a page number and an index. These structures are used as a cursor, stack 202 * entry and search returns as well as to pass records to other routines. 203 * 204 * One comment about searches. Internal page searches must find the largest 205 * record less than key in the tree so that descents work. Leaf page searches 206 * must find the smallest record greater than key so that the returned index 207 * is the record's correct position for insertion. 208 * 209 * One comment about cursors. The cursor key is never removed from the tree, 210 * even if deleted. This is because it is quite difficult to decide where the 211 * cursor should be when other keys have been inserted/deleted in the tree; 212 * duplicate keys make it impossible. This scheme does require extra work 213 * though, to make sure that we don't perform an operation on a deleted key. 214 */ 215 typedef struct EPGNO { 216 pgno_t pgno; /* the page number */ 217 indx_t index; /* the index on the page */ 218 } EPGNO; 219 220 typedef struct EPG { 221 PAGE *page; /* the (pinned) page */ 222 indx_t index; /* the index on the page */ 223 } EPG; 224 225 /* 226 * The metadata of the tree. The m_nrecs field is used only by the RECNO code. 227 * This is because the btree doesn't really need it and it requires that every 228 * put or delete call modify the metadata. 229 */ 230 typedef struct BTMETA { 231 u_long m_magic; /* magic number */ 232 u_long m_version; /* version */ 233 u_long m_psize; /* page size */ 234 u_long m_free; /* page number of first free page */ 235 u_long m_nrecs; /* R: number of records */ 236 #define SAVEMETA (BTF_NODUPS | BTF_RECNO) 237 u_long m_flags; /* bt_flags & SAVEMETA */ 238 u_long m_unused; /* unused */ 239 } BTMETA; 240 241 /* The in-memory btree/recno data structure. */ 242 typedef struct BTREE { 243 MPOOL *bt_mp; /* memory pool cookie */ 244 245 DB *bt_dbp; /* pointer to enclosing DB */ 246 247 EPGNO bt_bcursor; /* B: btree cursor */ 248 recno_t bt_rcursor; /* R: recno cursor (1-based) */ 249 250 #define BT_POP(t) (t->bt_sp ? t->bt_stack + --t->bt_sp : NULL) 251 #define BT_CLR(t) (t->bt_sp = 0) 252 EPGNO *bt_stack; /* stack of parent pages */ 253 u_int bt_sp; /* current stack pointer */ 254 u_int bt_maxstack; /* largest stack */ 255 256 char *bt_kbuf; /* key buffer */ 257 size_t bt_kbufsz; /* key buffer size */ 258 char *bt_dbuf; /* data buffer */ 259 size_t bt_dbufsz; /* data buffer size */ 260 261 int bt_fd; /* tree file descriptor */ 262 263 pgno_t bt_free; /* next free page */ 264 indx_t bt_psize; /* page size */ 265 indx_t bt_ovflsize; /* cut-off for key/data overflow */ 266 int bt_lorder; /* byte order */ 267 /* sorted order */ 268 enum { NOT, BACK, FORWARD, } bt_order; 269 EPGNO bt_last; /* last insert */ 270 271 /* B: key comparison function */ 272 int (*bt_cmp) __P((const DBT *, const DBT *)); 273 /* B: prefix comparison function */ 274 int (*bt_pfx) __P((const DBT *, const DBT *)); 275 /* R: recno input function */ 276 int (*bt_irec) __P((struct BTREE *, recno_t)); 277 278 FILE *bt_rfp; /* R: record FILE pointer */ 279 int bt_rfd; /* R: record file descriptor */ 280 281 caddr_t bt_cmap; /* R: current point in mapped space */ 282 caddr_t bt_smap; /* R: start of mapped space */ 283 caddr_t bt_emap; /* R: end of mapped space */ 284 size_t bt_msize; /* R: size of mapped region. */ 285 286 recno_t bt_nrecs; /* R: number of records */ 287 size_t bt_reclen; /* R: fixed record length */ 288 u_char bt_bval; /* R: delimiting byte/pad character */ 289 290 /* 291 * NB: 292 * BTF_NODUPS and BTF_RECNO are stored on disk, and may not be changed. 293 */ 294 #define BTF_CLOSEFP 0x0001 /* R: opened a file pointer */ 295 #define BTF_DELCRSR 0x0002 /* cursor has been deleted */ 296 #define BTF_EOF 0x0004 /* R: end of input file reached. */ 297 #define BTF_FIXEDLEN 0x0008 /* R: fixed length records */ 298 #define BTF_INMEM 0x0010 /* B: in-memory tree */ 299 #define BTF_NODUPS 0x0020 /* B: no duplicate keys permitted */ 300 #define BTF_MEMMAPPED 0x0040 /* R: memory mapped file. */ 301 #define BTF_RECNO 0x0080 /* R: record oriented tree */ 302 #define BTF_METADIRTY 0x0100 /* B: need to write metadata */ 303 #define BTF_MODIFIED 0x0200 /* tree modified */ 304 #define BTF_NEEDSWAP 0x0400 /* if byte order requires swapping */ 305 #define BTF_RDONLY 0x0800 /* read-only tree */ 306 #define BTF_RINMEM 0x1000 /* R: in-memory tree */ 307 #define BTF_SEQINIT 0x2000 /* sequential scan initialized */ 308 309 u_long bt_flags; /* btree state */ 310 } BTREE; 311 312 #define SET(t, f) ((t)->bt_flags |= (f)) 313 #define CLR(t, f) ((t)->bt_flags &= ~(f)) 314 #define ISSET(t, f) ((t)->bt_flags & (f)) 315 316 #include "extern.h" 317