2N/A/*-
2N/A * Copyright (c) 1991, 1993, 1994
2N/A * The Regents of the University of California. All rights reserved.
2N/A *
2N/A * This code is derived from software contributed to Berkeley by
2N/A * Mike Olson.
2N/A *
2N/A * Redistribution and use in source and binary forms, with or without
2N/A * modification, are permitted provided that the following conditions
2N/A * are met:
2N/A * 1. Redistributions of source code must retain the above copyright
2N/A * notice, this list of conditions and the following disclaimer.
2N/A * 2. Redistributions in binary form must reproduce the above copyright
2N/A * notice, this list of conditions and the following disclaimer in the
2N/A * documentation and/or other materials provided with the distribution.
2N/A * 3. All advertising materials mentioning features or use of this software
2N/A * must display the following acknowledgement:
2N/A * This product includes software developed by the University of
2N/A * California, Berkeley and its contributors.
2N/A * 4. Neither the name of the University nor the names of its contributors
2N/A * may be used to endorse or promote products derived from this software
2N/A * without specific prior written permission.
2N/A *
2N/A * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2N/A * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2N/A * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2N/A * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2N/A * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2N/A * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2N/A * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2N/A * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2N/A * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2N/A * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2N/A * SUCH DAMAGE.
2N/A *
2N/A * @(#)btree.h 8.11 (Berkeley) 8/17/94
2N/A */
2N/A
2N/A/* Macros to set/clear/test flags. */
2N/A#define F_SET(p, f) (p)->flags |= (f)
2N/A#define F_CLR(p, f) (p)->flags &= ~(f)
2N/A#define F_ISSET(p, f) ((p)->flags & (f))
2N/A
2N/A#include "mpool.h"
2N/A
2N/A#define DEFMINKEYPAGE (2) /* Minimum keys per page */
2N/A#define MINCACHE (5) /* Minimum cached pages */
2N/A#define MINPSIZE (512) /* Minimum page size */
2N/A
2N/A/*
2N/A * Page 0 of a btree file contains a copy of the meta-data. This page is also
2N/A * used as an out-of-band page, i.e. page pointers that point to nowhere point
2N/A * to page 0. Page 1 is the root of the btree.
2N/A */
2N/A#define P_INVALID 0 /* Invalid tree page number. */
2N/A#define P_META 0 /* Tree metadata page number. */
2N/A#define P_ROOT 1 /* Tree root page number. */
2N/A
2N/A/*
2N/A * There are five page layouts in the btree: btree internal pages (BINTERNAL),
2N/A * btree leaf pages (BLEAF), recno internal pages (RINTERNAL), recno leaf pages
2N/A * (RLEAF) and overflow pages. All five page types have a page header (PAGE).
2N/A * This implementation requires that values within structures NOT be padded.
2N/A * (ANSI C permits random padding.) If your compiler pads randomly you'll have
2N/A * to do some work to get this package to run.
2N/A */
2N/Atypedef struct _page {
2N/A db_pgno_t pgno; /* this page's page number */
2N/A db_pgno_t prevpg; /* left sibling */
2N/A db_pgno_t nextpg; /* right sibling */
2N/A
2N/A#define P_BINTERNAL 0x01 /* btree internal page */
2N/A#define P_BLEAF 0x02 /* leaf page */
2N/A#define P_OVERFLOW 0x04 /* overflow page */
2N/A#define P_RINTERNAL 0x08 /* recno internal page */
2N/A#define P_RLEAF 0x10 /* leaf page */
2N/A#define P_TYPE 0x1f /* type mask */
2N/A#define P_PRESERVE 0x20 /* never delete this chain of pages */
2N/A u_int32_t flags;
2N/A
2N/A indx_t lower; /* lower bound of free space on page */
2N/A indx_t upper; /* upper bound of free space on page */
2N/A indx_t linp[1]; /* indx_t-aligned VAR. LENGTH DATA */
2N/A} PAGE;
2N/A
2N/A/* First and next index. */
2N/A#define BTDATAOFF \
2N/A (sizeof(db_pgno_t) + sizeof(db_pgno_t) + sizeof(db_pgno_t) + \
2N/A sizeof(u_int32_t) + sizeof(indx_t) + sizeof(indx_t))
2N/A#define NEXTINDEX(p) (((p)->lower - BTDATAOFF) / sizeof(indx_t))
2N/A
2N/A/*
2N/A * For pages other than overflow pages, there is an array of offsets into the
2N/A * rest of the page immediately following the page header. Each offset is to
2N/A * an item which is unique to the type of page. The h_lower offset is just
2N/A * past the last filled-in index. The h_upper offset is the first item on the
2N/A * page. Offsets are from the beginning of the page.
2N/A *
2N/A * If an item is too big to store on a single page, a flag is set and the item
2N/A * is a { page, size } pair such that the page is the first page of an overflow
2N/A * chain with size bytes of item. Overflow pages are simply bytes without any
2N/A * external structure.
2N/A *
2N/A * The page number and size fields in the items are db_pgno_t-aligned so they can
2N/A * be manipulated without copying. (This presumes that 32 bit items can be
2N/A * manipulated on this system.)
2N/A */
2N/A#define LALIGN(n) (((n) + sizeof(db_pgno_t) - 1) & ~(sizeof(db_pgno_t) - 1))
2N/A#define NOVFLSIZE (sizeof(db_pgno_t) + sizeof(u_int32_t))
2N/A
2N/A/*
2N/A * For the btree internal pages, the item is a key. BINTERNALs are {key, pgno}
2N/A * pairs, such that the key compares less than or equal to all of the records
2N/A * on that page. For a tree without duplicate keys, an internal page with two
2N/A * consecutive keys, a and b, will have all records greater than or equal to a
2N/A * and less than b stored on the page associated with a. Duplicate keys are
2N/A * somewhat special and can cause duplicate internal and leaf page records and
2N/A * some minor modifications of the above rule.
2N/A */
2N/Atypedef struct _binternal {
2N/A u_int32_t ksize; /* key size */
2N/A db_pgno_t pgno; /* page number stored on */
2N/A#define P_BIGDATA 0x01 /* overflow data */
2N/A#define P_BIGKEY 0x02 /* overflow key */
2N/A u_char flags;
2N/A char bytes[1]; /* data */
2N/A} BINTERNAL;
2N/A
2N/A/* Get the page's BINTERNAL structure at index indx. */
2N/A#define GETBINTERNAL(pg, indx) \
2N/A ((BINTERNAL *)((char *)(pg) + (pg)->linp[indx]))
2N/A
2N/A/* Get the number of bytes in the entry. */
2N/A#define NBINTERNAL(len) \
2N/A LALIGN(sizeof(u_int32_t) + sizeof(db_pgno_t) + sizeof(u_char) + (len))
2N/A
2N/A/* Copy a BINTERNAL entry to the page. */
2N/A#define WR_BINTERNAL(p, size, pgno, flags) { \
2N/A *(u_int32_t *)p = size; \
2N/A p += sizeof(u_int32_t); \
2N/A *(db_pgno_t *)p = pgno; \
2N/A p += sizeof(db_pgno_t); \
2N/A *(u_char *)p = flags; \
2N/A p += sizeof(u_char); \
2N/A}
2N/A
2N/A/*
2N/A * For the recno internal pages, the item is a page number with the number of
2N/A * keys found on that page and below.
2N/A */
2N/Atypedef struct _rinternal {
2N/A recno_t nrecs; /* number of records */
2N/A db_pgno_t pgno; /* page number stored below */
2N/A} RINTERNAL;
2N/A
2N/A/* Get the page's RINTERNAL structure at index indx. */
2N/A#define GETRINTERNAL(pg, indx) \
2N/A ((RINTERNAL *)((char *)(pg) + (pg)->linp[indx]))
2N/A
2N/A/* Get the number of bytes in the entry. */
2N/A#define NRINTERNAL \
2N/A LALIGN(sizeof(recno_t) + sizeof(db_pgno_t))
2N/A
2N/A/* Copy a RINTERAL entry to the page. */
2N/A#define WR_RINTERNAL(p, nrecs, pgno) { \
2N/A *(recno_t *)p = nrecs; \
2N/A p += sizeof(recno_t); \
2N/A *(db_pgno_t *)p = pgno; \
2N/A}
2N/A
2N/A/* For the btree leaf pages, the item is a key and data pair. */
2N/Atypedef struct _bleaf {
2N/A u_int32_t ksize; /* size of key */
2N/A u_int32_t dsize; /* size of data */
2N/A u_char flags; /* P_BIGDATA, P_BIGKEY */
2N/A char bytes[1]; /* data */
2N/A} BLEAF;
2N/A
2N/A/* Get the page's BLEAF structure at index indx. */
2N/A#define GETBLEAF(pg, indx) \
2N/A ((BLEAF *)((char *)(pg) + (pg)->linp[indx]))
2N/A
2N/A/* Get the number of bytes in the entry. */
2N/A#define NBLEAF(p) NBLEAFDBT((p)->ksize, (p)->dsize)
2N/A
2N/A/* Get the number of bytes in the user's key/data pair. */
2N/A#define NBLEAFDBT(ksize, dsize) \
2N/A LALIGN(sizeof(u_int32_t) + sizeof(u_int32_t) + sizeof(u_char) + \
2N/A (ksize) + (dsize))
2N/A
2N/A/* Copy a BLEAF entry to the page. */
2N/A#define WR_BLEAF(p, key, data, flags) { \
2N/A *(u_int32_t *)p = key->size; \
2N/A p += sizeof(u_int32_t); \
2N/A *(u_int32_t *)p = data->size; \
2N/A p += sizeof(u_int32_t); \
2N/A *(u_char *)p = flags; \
2N/A p += sizeof(u_char); \
2N/A memmove(p, key->data, key->size); \
2N/A p += key->size; \
2N/A memmove(p, data->data, data->size); \
2N/A}
2N/A
2N/A/* For the recno leaf pages, the item is a data entry. */
2N/Atypedef struct _rleaf {
2N/A u_int32_t dsize; /* size of data */
2N/A u_char flags; /* P_BIGDATA */
2N/A char bytes[1];
2N/A} RLEAF;
2N/A
2N/A/* Get the page's RLEAF structure at index indx. */
2N/A#define GETRLEAF(pg, indx) \
2N/A ((RLEAF *)((char *)(pg) + (pg)->linp[indx]))
2N/A
2N/A/* Get the number of bytes in the entry. */
2N/A#define NRLEAF(p) NRLEAFDBT((p)->dsize)
2N/A
2N/A/* Get the number of bytes from the user's data. */
2N/A#define NRLEAFDBT(dsize) \
2N/A LALIGN(sizeof(u_int32_t) + sizeof(u_char) + (dsize))
2N/A
2N/A/* Copy a RLEAF entry to the page. */
2N/A#define WR_RLEAF(p, data, flags) { \
2N/A *(u_int32_t *)p = data->size; \
2N/A p += sizeof(u_int32_t); \
2N/A *(u_char *)p = flags; \
2N/A p += sizeof(u_char); \
2N/A memmove(p, data->data, data->size); \
2N/A}
2N/A
2N/A/*
2N/A * A record in the tree is either a pointer to a page and an index in the page
2N/A * or a page number and an index. These structures are used as a cursor, stack
2N/A * entry and search returns as well as to pass records to other routines.
2N/A *
2N/A * One comment about searches. Internal page searches must find the largest
2N/A * record less than key in the tree so that descents work. Leaf page searches
2N/A * must find the smallest record greater than key so that the returned index
2N/A * is the record's correct position for insertion.
2N/A */
2N/Atypedef struct _epgno {
2N/A db_pgno_t pgno; /* the page number */
2N/A indx_t index; /* the index on the page */
2N/A} EPGNO;
2N/A
2N/Atypedef struct _epg {
2N/A PAGE *page; /* the (pinned) page */
2N/A indx_t index; /* the index on the page */
2N/A} EPG;
2N/A
2N/A/*
2N/A * About cursors. The cursor (and the page that contained the key/data pair
2N/A * that it referenced) can be deleted, which makes things a bit tricky. If
2N/A * there are no duplicates of the cursor key in the tree (i.e. B_NODUPS is set
2N/A * or there simply aren't any duplicates of the key) we copy the key that it
2N/A * referenced when it's deleted, and reacquire a new cursor key if the cursor
2N/A * is used again. If there are duplicates keys, we move to the next/previous
2N/A * key, and set a flag so that we know what happened. NOTE: if duplicate (to
2N/A * the cursor) keys are added to the tree during this process, it is undefined
2N/A * if they will be returned or not in a cursor scan.
2N/A *
2N/A * The flags determine the possible states of the cursor:
2N/A *
2N/A * CURS_INIT The cursor references *something*.
2N/A * CURS_ACQUIRE The cursor was deleted, and a key has been saved so that
2N/A * we can reacquire the right position in the tree.
2N/A * CURS_AFTER, CURS_BEFORE
2N/A * The cursor was deleted, and now references a key/data pair
2N/A * that has not yet been returned, either before or after the
2N/A * deleted key/data pair.
2N/A * XXX
2N/A * This structure is broken out so that we can eventually offer multiple
2N/A * cursors as part of the DB interface.
2N/A */
2N/Atypedef struct _cursor {
2N/A EPGNO pg; /* B: Saved tree reference. */
2N/A DBT key; /* B: Saved key, or key.data == NULL. */
2N/A recno_t rcursor; /* R: recno cursor (1-based) */
2N/A
2N/A#define CURS_ACQUIRE 0x01 /* B: Cursor needs to be reacquired. */
2N/A#define CURS_AFTER 0x02 /* B: Unreturned cursor after key. */
2N/A#define CURS_BEFORE 0x04 /* B: Unreturned cursor before key. */
2N/A#define CURS_INIT 0x08 /* RB: Cursor initialized. */
2N/A u_int8_t flags;
2N/A} CURSOR;
2N/A
2N/A/*
2N/A * The metadata of the tree. The nrecs field is used only by the RECNO code.
2N/A * This is because the btree doesn't really need it and it requires that every
2N/A * put or delete call modify the metadata.
2N/A */
2N/Atypedef struct _btmeta {
2N/A u_int32_t magic; /* magic number */
2N/A u_int32_t version; /* version */
2N/A u_int32_t psize; /* page size */
2N/A u_int32_t free; /* page number of first free page */
2N/A u_int32_t nrecs; /* R: number of records */
2N/A
2N/A#define SAVEMETA (B_NODUPS | R_RECNO)
2N/A u_int32_t flags; /* bt_flags & SAVEMETA */
2N/A} BTMETA;
2N/A
2N/A/* The in-memory btree/recno data structure. */
2N/Atypedef struct _btree {
2N/A MPOOL *bt_mp; /* memory pool cookie */
2N/A
2N/A DB *bt_dbp; /* pointer to enclosing DB */
2N/A
2N/A EPG bt_cur; /* current (pinned) page */
2N/A PAGE *bt_pinned; /* page pinned across calls */
2N/A
2N/A CURSOR bt_cursor; /* cursor */
2N/A
2N/A#define BT_PUSH(t, p, i) { \
2N/A t->bt_sp->pgno = p; \
2N/A t->bt_sp->index = i; \
2N/A ++t->bt_sp; \
2N/A}
2N/A#define BT_POP(t) (t->bt_sp == t->bt_stack ? NULL : --t->bt_sp)
2N/A#define BT_CLR(t) (t->bt_sp = t->bt_stack)
2N/A EPGNO bt_stack[50]; /* stack of parent pages */
2N/A EPGNO *bt_sp; /* current stack pointer */
2N/A
2N/A DBT bt_rkey; /* returned key */
2N/A DBT bt_rdata; /* returned data */
2N/A
2N/A int bt_fd; /* tree file descriptor */
2N/A
2N/A db_pgno_t bt_free; /* next free page */
2N/A u_int32_t bt_psize; /* page size */
2N/A indx_t bt_ovflsize; /* cut-off for key/data overflow */
2N/A int bt_lorder; /* byte order */
2N/A /* sorted order */
2N/A enum { NOT, BACK, FORWARD } bt_order;
2N/A EPGNO bt_last; /* last insert */
2N/A
2N/A /* B: key comparison function */
2N/A int (*bt_cmp) __P((const DBT *, const DBT *));
2N/A /* B: prefix comparison function */
2N/A size_t (*bt_pfx) __P((const DBT *, const DBT *));
2N/A /* R: recno input function */
2N/A int (*bt_irec) __P((struct _btree *, recno_t));
2N/A
2N/A FILE *bt_rfp; /* R: record FILE pointer */
2N/A int bt_rfd; /* R: record file descriptor */
2N/A
2N/A caddr_t bt_cmap; /* R: current point in mapped space */
2N/A caddr_t bt_smap; /* R: start of mapped space */
2N/A caddr_t bt_emap; /* R: end of mapped space */
2N/A size_t bt_msize; /* R: size of mapped region. */
2N/A
2N/A recno_t bt_nrecs; /* R: number of records */
2N/A size_t bt_reclen; /* R: fixed record length */
2N/A u_char bt_bval; /* R: delimiting byte/pad character */
2N/A
2N/A/*
2N/A * NB:
2N/A * B_NODUPS and R_RECNO are stored on disk, and may not be changed.
2N/A */
2N/A#define B_INMEM 0x00001 /* in-memory tree */
2N/A#define B_METADIRTY 0x00002 /* need to write metadata */
2N/A#define B_MODIFIED 0x00004 /* tree modified */
2N/A#define B_NEEDSWAP 0x00008 /* if byte order requires swapping */
2N/A#define B_RDONLY 0x00010 /* read-only tree */
2N/A
2N/A#define B_NODUPS 0x00020 /* no duplicate keys permitted */
2N/A#define R_RECNO 0x00080 /* record oriented tree */
2N/A
2N/A#define R_CLOSEFP 0x00040 /* opened a file pointer */
2N/A#define R_EOF 0x00100 /* end of input file reached. */
2N/A#define R_FIXLEN 0x00200 /* fixed length records */
2N/A#define R_MEMMAPPED 0x00400 /* memory mapped file. */
2N/A#define R_INMEM 0x00800 /* in-memory file */
2N/A#define R_MODIFIED 0x01000 /* modified file */
2N/A#define R_RDONLY 0x02000 /* read-only file */
2N/A
2N/A#define B_DB_LOCK 0x04000 /* DB_LOCK specified. */
2N/A#define B_DB_SHMEM 0x08000 /* DB_SHMEM specified. */
2N/A#define B_DB_TXN 0x10000 /* DB_TXN specified. */
2N/A u_int32_t flags;
2N/A} BTREE;
2N/A
2N/A#include "extern.h"