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.5 (Berkeley) 11/20/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 p += sizeof(size_t); \ 108 *(pgno_t *)p = pgno; \ 109 p += sizeof(pgno_t); \ 110 *(u_char *)p = flags; \ 111 p += sizeof(u_char); \ 112 } 113 114 /* 115 * For the recno internal pages, the item is a page number with the number of 116 * keys found on that page and below. 117 */ 118 typedef struct RINTERNAL { 119 recno_t nrecs; /* number of records */ 120 pgno_t pgno; /* page number stored below */ 121 } RINTERNAL; 122 123 /* Get the page's RINTERNAL structure at index indx. */ 124 #define GETRINTERNAL(pg, indx) \ 125 ((RINTERNAL *)((char *)(pg) + (pg)->linp[indx])) 126 127 /* Get the number of bytes in the entry. */ 128 #define NRINTERNAL \ 129 LALIGN(sizeof(recno_t) + sizeof(pgno_t)) 130 131 /* Copy a RINTERAL entry to the page. */ 132 #define WR_RINTERNAL(p, nrecs, pgno) { \ 133 *(recno_t *)p = nrecs; \ 134 p += sizeof(recno_t); \ 135 *(pgno_t *)p = pgno; \ 136 } 137 138 /* For the btree leaf pages, the item is a key and data pair. */ 139 typedef struct BLEAF { 140 size_t ksize; /* size of key */ 141 size_t dsize; /* size of data */ 142 u_char flags; /* P_BIGDATA, P_BIGKEY */ 143 char bytes[1]; /* data */ 144 } BLEAF; 145 146 /* Get the page's BLEAF structure at index indx. */ 147 #define GETBLEAF(pg, indx) \ 148 ((BLEAF *)((char *)(pg) + (pg)->linp[indx])) 149 150 /* Get the number of bytes in the entry. */ 151 #define NBLEAF(p) NBLEAFDBT((p)->ksize, (p)->dsize) 152 153 /* Get the number of bytes in the user's key/data pair. */ 154 #define NBLEAFDBT(ksize, dsize) \ 155 LALIGN(sizeof(size_t) + sizeof(size_t) + sizeof(u_char) + \ 156 (ksize) + (dsize)) 157 158 /* Copy a BLEAF entry to the page. */ 159 #define WR_BLEAF(p, key, data, flags) { \ 160 *(size_t *)p = key->size; \ 161 p += sizeof(size_t); \ 162 *(size_t *)p = data->size; \ 163 p += sizeof(size_t); \ 164 *(u_char *)p = flags; \ 165 p += sizeof(u_char); \ 166 bcopy(key->data, p, key->size); \ 167 p += key->size; \ 168 bcopy(data->data, p, data->size); \ 169 } 170 171 /* For the recno leaf pages, the item is a data entry. */ 172 typedef struct RLEAF { 173 size_t dsize; /* size of data */ 174 u_char flags; /* P_BIGDATA */ 175 char bytes[1]; 176 } RLEAF; 177 178 /* Get the page's RLEAF structure at index indx. */ 179 #define GETRLEAF(pg, indx) \ 180 ((RLEAF *)((char *)(pg) + (pg)->linp[indx])) 181 182 /* Get the number of bytes in the entry. */ 183 #define NRLEAF(p) NRLEAFDBT((p)->dsize) 184 185 /* Get the number of bytes from the user's data. */ 186 #define NRLEAFDBT(dsize) \ 187 LALIGN(sizeof(size_t) + sizeof(u_char) + (dsize)) 188 189 /* Copy a RLEAF entry to the page. */ 190 #define WR_RLEAF(p, data, flags) { \ 191 *(size_t *)p = data->size; \ 192 p += sizeof(size_t); \ 193 *(u_char *)p = flags; \ 194 p += sizeof(u_char); \ 195 bcopy(data->data, p, data->size); \ 196 } 197 198 /* 199 * A record in the tree is either a pointer to a page and an index in the page 200 * or a page number and an index. These structures are used as a cursor, stack 201 * entry and search returns as well as to pass records to other routines. 202 * 203 * One comment about searches. Internal page searches must find the largest 204 * record less than key in the tree so that descents work. Leaf page searches 205 * must find the smallest record greater than key so that the returned index 206 * is the record's correct position for insertion. 207 * 208 * One comment about cursors. The cursor key is never removed from the tree, 209 * even if deleted. This is because it is quite difficult to decide where the 210 * cursor should be when other keys have been inserted/deleted in the tree; 211 * duplicate keys make it impossible. This scheme does require extra work 212 * though, to make sure that we don't perform an operation on a deleted key. 213 */ 214 typedef struct EPGNO { 215 pgno_t pgno; /* the page number */ 216 index_t index; /* the index on the page */ 217 } EPGNO; 218 219 typedef struct EPG { 220 PAGE *page; /* the (pinned) page */ 221 index_t index; /* the index on the page */ 222 } EPG; 223 224 /* 225 * The metadata of the tree. The m_nrecs field is used only by the RECNO code. 226 * This is because the btree doesn't really need it and it requires that every 227 * put or delete call modify the metadata. 228 */ 229 typedef struct BTMETA { 230 u_long m_magic; /* magic number */ 231 u_long m_version; /* version */ 232 u_long m_psize; /* page size */ 233 u_long m_free; /* page number of first free page */ 234 u_long m_nrecs; /* R: number of records */ 235 #define SAVEMETA (BTF_NODUPS | BTF_RECNO) 236 u_long m_flags; /* bt_flags & SAVEMETA */ 237 u_long m_lorder; /* byte order */ 238 } BTMETA; 239 240 /* The in-memory btree/recno data structure. */ 241 typedef struct BTREE { 242 MPOOL *bt_mp; /* memory pool cookie */ 243 244 DB *bt_dbp; /* pointer to enclosing DB */ 245 246 EPGNO bt_bcursor; /* B: btree cursor */ 247 recno_t bt_rcursor; /* R: recno cursor (1-based) */ 248 249 #define BT_POP(t) (t->bt_sp ? t->bt_stack + --t->bt_sp : NULL) 250 #define BT_CLR(t) (t->bt_sp = 0) 251 EPGNO *bt_stack; /* stack of parent pages */ 252 u_int bt_sp; /* current stack pointer */ 253 u_int bt_maxstack; /* largest stack */ 254 255 char *bt_kbuf; /* key buffer */ 256 size_t bt_kbufsz; /* key buffer size */ 257 char *bt_dbuf; /* data buffer */ 258 size_t bt_dbufsz; /* data buffer size */ 259 260 int bt_fd; /* tree file descriptor */ 261 FILE *bt_rfp; /* R: record FILE pointer */ 262 int bt_rfd; /* R: record file descriptor */ 263 264 pgno_t bt_free; /* XXX next free page */ 265 index_t bt_psize; /* page size */ 266 index_t bt_ovflsize; /* cut-off for key/data overflow */ 267 int bt_lorder; /* byte order */ 268 /* sorted order */ 269 enum { NOT, BACK, FORWARD, } bt_order; 270 EPGNO bt_last; /* last insert */ 271 272 /* B: key comparison function */ 273 int (*bt_cmp) __P((const DBT *, const DBT *)); 274 /* B: prefix comparison function */ 275 int (*bt_pfx) __P((const DBT *, const DBT *)); 276 /* R: recno input function */ 277 int (*bt_irec) __P((struct BTREE *, recno_t)); 278 recno_t bt_nrecs; /* R: number of records */ 279 caddr_t bt_smap; /* R: start of mapped space */ 280 caddr_t bt_emap; /* R: end of mapped space */ 281 size_t bt_reclen; /* R: fixed record length */ 282 u_char bt_bval; /* R: delimiting byte/pad character */ 283 284 #define BTF_DELCRSR 0x001 /* cursor has been deleted */ 285 #define BTF_FIXEDLEN 0x002 /* fixed length records */ 286 #define BTF_INMEM 0x004 /* in-memory tree */ 287 #define BTF_METADIRTY 0x008 /* B: need to write metadata */ 288 #define BTF_MODIFIED 0x010 /* tree modified */ 289 #define BTF_NODUPS 0x020 /* B: no duplicate keys permitted */ 290 #define BTF_RDONLY 0x040 /* read-only tree */ 291 #define BTF_RECNO 0x080 /* record oriented tree */ 292 #define BTF_SEQINIT 0x100 /* sequential scan initialized */ 293 u_long bt_flags; /* btree state */ 294 } BTREE; 295 296 #define ISSET(t, f) ((t)->bt_flags & (f)) 297 #define NOTSET(t, f) (!((t)->bt_flags & (f))) 298 #define SET(t, f) ((t)->bt_flags |= (f)) 299 #define UNSET(t, f) ((t)->bt_flags &= ~(f)) 300 301 #include "extern.h" 302