Annotation of src/lib/libc/db/btree/btree.h, Revision 1.15.12.1
1.15.12.1! wrstuden 1: /* $NetBSD: btree.h,v 1.15 2007/02/03 23:46:09 christos Exp $ */
1.8 cgd 2:
1.1 cgd 3: /*-
1.7 cgd 4: * Copyright (c) 1991, 1993, 1994
1.2 cgd 5: * The Regents of the University of California. All rights reserved.
1.1 cgd 6: *
7: * This code is derived from software contributed to Berkeley by
8: * Mike Olson.
9: *
10: * Redistribution and use in source and binary forms, with or without
11: * modification, are permitted provided that the following conditions
12: * are met:
13: * 1. Redistributions of source code must retain the above copyright
14: * notice, this list of conditions and the following disclaimer.
15: * 2. Redistributions in binary form must reproduce the above copyright
16: * notice, this list of conditions and the following disclaimer in the
17: * documentation and/or other materials provided with the distribution.
1.13 agc 18: * 3. Neither the name of the University nor the names of its contributors
1.1 cgd 19: * may be used to endorse or promote products derived from this software
20: * without specific prior written permission.
21: *
22: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32: * SUCH DAMAGE.
1.2 cgd 33: *
1.9 cgd 34: * @(#)btree.h 8.11 (Berkeley) 8/17/94
1.1 cgd 35: */
1.12 tv 36:
1.14 lukem 37: #if HAVE_NBTOOL_CONFIG_H
38: #include "nbtool_config.h"
1.12 tv 39: #endif
1.1 cgd 40:
1.9 cgd 41: /* Macros to set/clear/test flags. */
42: #define F_SET(p, f) (p)->flags |= (f)
43: #define F_CLR(p, f) (p)->flags &= ~(f)
44: #define F_ISSET(p, f) ((p)->flags & (f))
45:
1.2 cgd 46: #include <mpool.h>
1.1 cgd 47:
1.2 cgd 48: #define DEFMINKEYPAGE (2) /* Minimum keys per page */
49: #define MINCACHE (5) /* Minimum cached pages */
50: #define MINPSIZE (512) /* Minimum page size */
1.1 cgd 51:
52: /*
1.2 cgd 53: * Page 0 of a btree file contains a copy of the meta-data. This page is also
54: * used as an out-of-band page, i.e. page pointers that point to nowhere point
55: * to page 0. Page 1 is the root of the btree.
1.1 cgd 56: */
1.2 cgd 57: #define P_INVALID 0 /* Invalid tree page number. */
58: #define P_META 0 /* Tree metadata page number. */
59: #define P_ROOT 1 /* Tree root page number. */
1.1 cgd 60:
61: /*
1.2 cgd 62: * There are five page layouts in the btree: btree internal pages (BINTERNAL),
63: * btree leaf pages (BLEAF), recno internal pages (RINTERNAL), recno leaf pages
64: * (RLEAF) and overflow pages. All five page types have a page header (PAGE).
1.7 cgd 65: * This implementation requires that values within structures NOT be padded.
1.2 cgd 66: * (ANSI C permits random padding.) If your compiler pads randomly you'll have
67: * to do some work to get this package to run.
1.1 cgd 68: */
1.4 cgd 69: typedef struct _page {
1.2 cgd 70: pgno_t pgno; /* this page's page number */
71: pgno_t prevpg; /* left sibling */
72: pgno_t nextpg; /* right sibling */
1.1 cgd 73:
1.2 cgd 74: #define P_BINTERNAL 0x01 /* btree internal page */
75: #define P_BLEAF 0x02 /* leaf page */
76: #define P_OVERFLOW 0x04 /* overflow page */
77: #define P_RINTERNAL 0x08 /* recno internal page */
78: #define P_RLEAF 0x10 /* leaf page */
79: #define P_TYPE 0x1f /* type mask */
80: #define P_PRESERVE 0x20 /* never delete this chain of pages */
1.15.12.1! wrstuden 81: uint32_t flags;
1.1 cgd 82:
1.2 cgd 83: indx_t lower; /* lower bound of free space on page */
84: indx_t upper; /* upper bound of free space on page */
1.7 cgd 85: indx_t linp[1]; /* indx_t-aligned VAR. LENGTH DATA */
1.2 cgd 86: } PAGE;
1.1 cgd 87:
1.2 cgd 88: /* First and next index. */
1.9 cgd 89: #define BTDATAOFF \
90: (sizeof(pgno_t) + sizeof(pgno_t) + sizeof(pgno_t) + \
1.15.12.1! wrstuden 91: sizeof(uint32_t) + sizeof(indx_t) + sizeof(indx_t))
1.15 christos 92:
93: #define _NEXTINDEX(p) (((p)->lower - BTDATAOFF) / sizeof(indx_t))
94: #ifdef _DIAGNOSTIC
95: static __inline indx_t
96: NEXTINDEX(const PAGE *p) {
97: size_t x = _NEXTINDEX(p);
98: _DBFIT(x, indx_t);
99: return (indx_t)x;
100: }
101: #else
102: #define NEXTINDEX(p) (indx_t)_NEXTINDEX(p)
103: #endif
1.1 cgd 104:
105: /*
1.2 cgd 106: * For pages other than overflow pages, there is an array of offsets into the
107: * rest of the page immediately following the page header. Each offset is to
108: * an item which is unique to the type of page. The h_lower offset is just
109: * past the last filled-in index. The h_upper offset is the first item on the
110: * page. Offsets are from the beginning of the page.
1.1 cgd 111: *
1.2 cgd 112: * If an item is too big to store on a single page, a flag is set and the item
113: * is a { page, size } pair such that the page is the first page of an overflow
114: * chain with size bytes of item. Overflow pages are simply bytes without any
115: * external structure.
1.1 cgd 116: *
1.7 cgd 117: * The page number and size fields in the items are pgno_t-aligned so they can
118: * be manipulated without copying. (This presumes that 32 bit items can be
119: * manipulated on this system.)
1.1 cgd 120: */
1.10 christos 121: #define BTLALIGN(n) (((n) + sizeof(pgno_t) - 1) & ~(sizeof(pgno_t) - 1))
1.15.12.1! wrstuden 122: #define NOVFLSIZE (sizeof(pgno_t) + sizeof(uint32_t))
1.1 cgd 123:
124: /*
1.2 cgd 125: * For the btree internal pages, the item is a key. BINTERNALs are {key, pgno}
126: * pairs, such that the key compares less than or equal to all of the records
127: * on that page. For a tree without duplicate keys, an internal page with two
128: * consecutive keys, a and b, will have all records greater than or equal to a
129: * and less than b stored on the page associated with a. Duplicate keys are
130: * somewhat special and can cause duplicate internal and leaf page records and
131: * some minor modifications of the above rule.
132: */
1.4 cgd 133: typedef struct _binternal {
1.15.12.1! wrstuden 134: uint32_t ksize; /* key size */
1.2 cgd 135: pgno_t pgno; /* page number stored on */
136: #define P_BIGDATA 0x01 /* overflow data */
137: #define P_BIGKEY 0x02 /* overflow key */
1.15.12.1! wrstuden 138: uint8_t flags;
1.2 cgd 139: char bytes[1]; /* data */
140: } BINTERNAL;
141:
142: /* Get the page's BINTERNAL structure at index indx. */
1.9 cgd 143: #define GETBINTERNAL(pg, indx) \
1.11 christos 144: ((BINTERNAL *)(void *)((char *)(void *)(pg) + (pg)->linp[indx]))
1.2 cgd 145:
146: /* Get the number of bytes in the entry. */
1.15 christos 147: #define _NBINTERNAL(len) \
1.15.12.1! wrstuden 148: BTLALIGN(sizeof(uint32_t) + sizeof(pgno_t) + sizeof(uint8_t) + (len))
1.15 christos 149: #ifdef _DIAGNOSTIC
1.15.12.1! wrstuden 150: static __inline uint32_t
! 151: NBINTERNAL(uint32_t len) {
1.15 christos 152: size_t x = _NBINTERNAL(len);
1.15.12.1! wrstuden 153: _DBFIT(x, uint32_t);
! 154: return (uint32_t)x;
1.15 christos 155: }
156: #else
1.15.12.1! wrstuden 157: #define NBINTERNAL(len) (uint32_t)_NBINTERNAL(len)
1.15 christos 158: #endif
1.2 cgd 159:
160: /* Copy a BINTERNAL entry to the page. */
1.15 christos 161: #define WR_BINTERNAL(p, size, pgno, flags) do { \
1.15.12.1! wrstuden 162: _DBFIT(size, uint32_t); \
! 163: *(uint32_t *)(void *)p = (uint32_t)size; \
! 164: p += sizeof(uint32_t); \
1.11 christos 165: *(pgno_t *)(void *)p = pgno; \
1.7 cgd 166: p += sizeof(pgno_t); \
1.15.12.1! wrstuden 167: *(uint8_t *)(void *)p = flags; \
! 168: p += sizeof(uint8_t); \
1.15 christos 169: } while (/*CONSTCOND*/0)
1.2 cgd 170:
171: /*
172: * For the recno internal pages, the item is a page number with the number of
173: * keys found on that page and below.
174: */
1.4 cgd 175: typedef struct _rinternal {
1.2 cgd 176: recno_t nrecs; /* number of records */
177: pgno_t pgno; /* page number stored below */
178: } RINTERNAL;
179:
180: /* Get the page's RINTERNAL structure at index indx. */
1.9 cgd 181: #define GETRINTERNAL(pg, indx) \
1.11 christos 182: ((RINTERNAL *)(void *)((char *)(void *)(pg) + (pg)->linp[indx]))
1.2 cgd 183:
184: /* Get the number of bytes in the entry. */
1.9 cgd 185: #define NRINTERNAL \
1.10 christos 186: BTLALIGN(sizeof(recno_t) + sizeof(pgno_t))
1.2 cgd 187:
188: /* Copy a RINTERAL entry to the page. */
1.15 christos 189: #define WR_RINTERNAL(p, nrecs, pgno) do { \
1.11 christos 190: *(recno_t *)(void *)p = nrecs; \
1.9 cgd 191: p += sizeof(recno_t); \
1.11 christos 192: *(pgno_t *)(void *)p = pgno; \
1.15 christos 193: } while (/*CONSTCOND*/0)
1.2 cgd 194:
195: /* For the btree leaf pages, the item is a key and data pair. */
1.4 cgd 196: typedef struct _bleaf {
1.15.12.1! wrstuden 197: uint32_t ksize; /* size of key */
! 198: uint32_t dsize; /* size of data */
! 199: uint8_t flags; /* P_BIGDATA, P_BIGKEY */
1.2 cgd 200: char bytes[1]; /* data */
201: } BLEAF;
202:
203: /* Get the page's BLEAF structure at index indx. */
1.9 cgd 204: #define GETBLEAF(pg, indx) \
1.11 christos 205: ((BLEAF *)(void *)((char *)(void *)(pg) + (pg)->linp[indx]))
1.2 cgd 206:
207:
208: /* Get the number of bytes in the user's key/data pair. */
1.15 christos 209: #define _NBLEAFDBT(ksize, dsize) \
1.15.12.1! wrstuden 210: BTLALIGN(sizeof(uint32_t) + sizeof(uint32_t) + sizeof(uint8_t) + \
1.2 cgd 211: (ksize) + (dsize))
1.15 christos 212: #ifdef _DIAGNOSTIC
1.15.12.1! wrstuden 213: static __inline uint32_t
1.15 christos 214: NBLEAFDBT(size_t k, size_t d) {
215: size_t x = _NBLEAFDBT(k, d);
1.15.12.1! wrstuden 216: _DBFIT(x, uint32_t);
! 217: return (uint32_t)x;
1.15 christos 218: }
219: #else
1.15.12.1! wrstuden 220: #define NBLEAFDBT(p, q) (uint32_t)_NBLEAFDBT(p, q)
1.15 christos 221: #endif
222:
223: /* Get the number of bytes in the entry. */
224: #define NBLEAF(p) NBLEAFDBT((p)->ksize, (p)->dsize)
1.2 cgd 225:
226: /* Copy a BLEAF entry to the page. */
1.15 christos 227: #define WR_BLEAF(p, key, data, flags) do { \
1.15.12.1! wrstuden 228: _DBFIT(key->size, uint32_t); \
! 229: *(uint32_t *)(void *)p = (uint32_t)key->size; \
! 230: p += sizeof(uint32_t); \
! 231: _DBFIT(data->size, uint32_t); \
! 232: *(uint32_t *)(void *)p = (uint32_t)data->size; \
! 233: p += sizeof(uint32_t); \
! 234: *(uint8_t *)(void *)p = flags; \
! 235: p += sizeof(uint8_t); \
1.15 christos 236: (void)memmove(p, key->data, key->size); \
1.7 cgd 237: p += key->size; \
1.15 christos 238: (void)memmove(p, data->data, data->size); \
239: } while (/*CONSTCOND*/0)
1.2 cgd 240:
241: /* For the recno leaf pages, the item is a data entry. */
1.4 cgd 242: typedef struct _rleaf {
1.15.12.1! wrstuden 243: uint32_t dsize; /* size of data */
! 244: uint8_t flags; /* P_BIGDATA */
1.2 cgd 245: char bytes[1];
246: } RLEAF;
247:
248: /* Get the page's RLEAF structure at index indx. */
1.9 cgd 249: #define GETRLEAF(pg, indx) \
1.11 christos 250: ((RLEAF *)(void *)((char *)(void *)(pg) + (pg)->linp[indx]))
1.2 cgd 251:
1.15 christos 252: #define _NRLEAFDBT(dsize) \
1.15.12.1! wrstuden 253: BTLALIGN(sizeof(uint32_t) + sizeof(uint8_t) + (dsize))
1.15 christos 254:
255: #ifdef _DIAGNOSTIC
1.15.12.1! wrstuden 256: static __inline uint32_t
1.15 christos 257: NRLEAFDBT(size_t d) {
258: size_t x = _NRLEAFDBT(d);
1.15.12.1! wrstuden 259: _DBFIT(x, uint32_t);
! 260: return (uint32_t)x;
1.15 christos 261: }
262: #else
1.15.12.1! wrstuden 263: #define NRLEAFDBT(d) (uint32_t)_NRLEAFDBT(d)
1.15 christos 264: #endif
265:
1.2 cgd 266: /* Get the number of bytes in the entry. */
267: #define NRLEAF(p) NRLEAFDBT((p)->dsize)
268:
269: /* Get the number of bytes from the user's data. */
270:
271: /* Copy a RLEAF entry to the page. */
1.15 christos 272: #define WR_RLEAF(p, data, flags) do { \
1.15.12.1! wrstuden 273: _DBFIT(data->size, uint32_t); \
! 274: *(uint32_t *)(void *)p = (uint32_t)data->size; \
! 275: p += sizeof(uint32_t); \
! 276: *(uint8_t *)(void *)p = flags; \
! 277: p += sizeof(uint8_t); \
1.7 cgd 278: memmove(p, data->data, data->size); \
1.15 christos 279: } while (/*CONSTCOND*/0)
1.2 cgd 280:
281: /*
282: * A record in the tree is either a pointer to a page and an index in the page
283: * or a page number and an index. These structures are used as a cursor, stack
284: * entry and search returns as well as to pass records to other routines.
1.1 cgd 285: *
1.2 cgd 286: * One comment about searches. Internal page searches must find the largest
287: * record less than key in the tree so that descents work. Leaf page searches
288: * must find the smallest record greater than key so that the returned index
289: * is the record's correct position for insertion.
290: */
1.4 cgd 291: typedef struct _epgno {
1.2 cgd 292: pgno_t pgno; /* the page number */
293: indx_t index; /* the index on the page */
294: } EPGNO;
295:
1.4 cgd 296: typedef struct _epg {
1.2 cgd 297: PAGE *page; /* the (pinned) page */
298: indx_t index; /* the index on the page */
299: } EPG;
300:
301: /*
1.9 cgd 302: * About cursors. The cursor (and the page that contained the key/data pair
303: * that it referenced) can be deleted, which makes things a bit tricky. If
304: * there are no duplicates of the cursor key in the tree (i.e. B_NODUPS is set
305: * or there simply aren't any duplicates of the key) we copy the key that it
306: * referenced when it's deleted, and reacquire a new cursor key if the cursor
307: * is used again. If there are duplicates keys, we move to the next/previous
308: * key, and set a flag so that we know what happened. NOTE: if duplicate (to
309: * the cursor) keys are added to the tree during this process, it is undefined
310: * if they will be returned or not in a cursor scan.
311: *
312: * The flags determine the possible states of the cursor:
313: *
314: * CURS_INIT The cursor references *something*.
315: * CURS_ACQUIRE The cursor was deleted, and a key has been saved so that
316: * we can reacquire the right position in the tree.
317: * CURS_AFTER, CURS_BEFORE
318: * The cursor was deleted, and now references a key/data pair
319: * that has not yet been returned, either before or after the
320: * deleted key/data pair.
321: * XXX
322: * This structure is broken out so that we can eventually offer multiple
323: * cursors as part of the DB interface.
324: */
325: typedef struct _cursor {
326: EPGNO pg; /* B: Saved tree reference. */
327: DBT key; /* B: Saved key, or key.data == NULL. */
328: recno_t rcursor; /* R: recno cursor (1-based) */
329:
330: #define CURS_ACQUIRE 0x01 /* B: Cursor needs to be reacquired. */
331: #define CURS_AFTER 0x02 /* B: Unreturned cursor after key. */
332: #define CURS_BEFORE 0x04 /* B: Unreturned cursor before key. */
333: #define CURS_INIT 0x08 /* RB: Cursor initialized. */
1.15.12.1! wrstuden 334: uint8_t flags;
1.9 cgd 335: } CURSOR;
336:
337: /*
338: * The metadata of the tree. The nrecs field is used only by the RECNO code.
1.2 cgd 339: * This is because the btree doesn't really need it and it requires that every
340: * put or delete call modify the metadata.
1.1 cgd 341: */
1.4 cgd 342: typedef struct _btmeta {
1.15.12.1! wrstuden 343: uint32_t magic; /* magic number */
! 344: uint32_t version; /* version */
! 345: uint32_t psize; /* page size */
! 346: uint32_t free; /* page number of first free page */
! 347: uint32_t nrecs; /* R: number of records */
1.9 cgd 348:
1.2 cgd 349: #define SAVEMETA (B_NODUPS | R_RECNO)
1.15.12.1! wrstuden 350: uint32_t flags; /* bt_flags & SAVEMETA */
1.2 cgd 351: } BTMETA;
1.1 cgd 352:
1.2 cgd 353: /* The in-memory btree/recno data structure. */
1.4 cgd 354: typedef struct _btree {
1.9 cgd 355: MPOOL *bt_mp; /* memory pool cookie */
1.2 cgd 356:
1.9 cgd 357: DB *bt_dbp; /* pointer to enclosing DB */
1.2 cgd 358:
1.9 cgd 359: EPG bt_cur; /* current (pinned) page */
360: PAGE *bt_pinned; /* page pinned across calls */
1.4 cgd 361:
1.9 cgd 362: CURSOR bt_cursor; /* cursor */
1.2 cgd 363:
1.9 cgd 364: #define BT_PUSH(t, p, i) { \
365: t->bt_sp->pgno = p; \
366: t->bt_sp->index = i; \
367: ++t->bt_sp; \
368: }
369: #define BT_POP(t) (t->bt_sp == t->bt_stack ? NULL : --t->bt_sp)
370: #define BT_CLR(t) (t->bt_sp = t->bt_stack)
371: EPGNO bt_stack[50]; /* stack of parent pages */
372: EPGNO *bt_sp; /* current stack pointer */
1.2 cgd 373:
1.9 cgd 374: DBT bt_rkey; /* returned key */
375: DBT bt_rdata; /* returned data */
1.2 cgd 376:
1.9 cgd 377: int bt_fd; /* tree file descriptor */
1.2 cgd 378:
1.9 cgd 379: pgno_t bt_free; /* next free page */
1.15.12.1! wrstuden 380: uint32_t bt_psize; /* page size */
1.9 cgd 381: indx_t bt_ovflsize; /* cut-off for key/data overflow */
382: int bt_lorder; /* byte order */
1.2 cgd 383: /* sorted order */
1.6 cgd 384: enum { NOT, BACK, FORWARD } bt_order;
1.9 cgd 385: EPGNO bt_last; /* last insert */
1.2 cgd 386:
387: /* B: key comparison function */
1.15 christos 388: int (*bt_cmp)(const DBT *, const DBT *);
1.2 cgd 389: /* B: prefix comparison function */
1.15 christos 390: size_t (*bt_pfx)(const DBT *, const DBT *);
1.2 cgd 391: /* R: recno input function */
1.15 christos 392: int (*bt_irec)(struct _btree *, recno_t);
1.2 cgd 393:
1.9 cgd 394: FILE *bt_rfp; /* R: record FILE pointer */
395: int bt_rfd; /* R: record file descriptor */
1.2 cgd 396:
1.9 cgd 397: caddr_t bt_cmap; /* R: current point in mapped space */
398: caddr_t bt_smap; /* R: start of mapped space */
399: caddr_t bt_emap; /* R: end of mapped space */
400: size_t bt_msize; /* R: size of mapped region. */
401:
402: recno_t bt_nrecs; /* R: number of records */
403: size_t bt_reclen; /* R: fixed record length */
1.15.12.1! wrstuden 404: uint8_t bt_bval; /* R: delimiting byte/pad character */
1.2 cgd 405:
406: /*
407: * NB:
408: * B_NODUPS and R_RECNO are stored on disk, and may not be changed.
409: */
1.9 cgd 410: #define B_INMEM 0x00001 /* in-memory tree */
411: #define B_METADIRTY 0x00002 /* need to write metadata */
412: #define B_MODIFIED 0x00004 /* tree modified */
413: #define B_NEEDSWAP 0x00008 /* if byte order requires swapping */
414: #define B_RDONLY 0x00010 /* read-only tree */
415:
1.2 cgd 416: #define B_NODUPS 0x00020 /* no duplicate keys permitted */
1.4 cgd 417: #define R_RECNO 0x00080 /* record oriented tree */
1.2 cgd 418:
1.9 cgd 419: #define R_CLOSEFP 0x00040 /* opened a file pointer */
420: #define R_EOF 0x00100 /* end of input file reached. */
421: #define R_FIXLEN 0x00200 /* fixed length records */
422: #define R_MEMMAPPED 0x00400 /* memory mapped file. */
423: #define R_INMEM 0x00800 /* in-memory file */
424: #define R_MODIFIED 0x01000 /* modified file */
425: #define R_RDONLY 0x02000 /* read-only file */
426:
427: #define B_DB_LOCK 0x04000 /* DB_LOCK specified. */
428: #define B_DB_SHMEM 0x08000 /* DB_SHMEM specified. */
429: #define B_DB_TXN 0x10000 /* DB_TXN specified. */
1.15.12.1! wrstuden 430: uint32_t flags;
1.2 cgd 431: } BTREE;
1.1 cgd 432:
1.2 cgd 433: #include "extern.h"
CVSweb <webmaster@jp.NetBSD.org>