btree.h

Go to the documentation of this file.
00001 /*-
00002  * Copyright (c) 1991, 1993, 1994
00003  * The Regents of the University of California.  All rights reserved.
00004  *
00005  * This code is derived from software contributed to Berkeley by
00006  * Mike Olson.
00007  *
00008  * Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted provided that the following conditions
00010  * are met:
00011  * 1. Redistributions of source code must retain the above copyright
00012  *    notice, this list of conditions and the following disclaimer.
00013  * 2. Redistributions in binary form must reproduce the above copyright
00014  *    notice, this list of conditions and the following disclaimer in the
00015  *    documentation and/or other materials provided with the distribution.
00016  * 3. All advertising materials mentioning features or use of this software
00017  *    must display the following acknowledgement:
00018  * This product includes software developed by the University of
00019  * California, Berkeley and its contributors.
00020  * 4. Neither the name of the University nor the names of its contributors
00021  *    may be used to endorse or promote products derived from this software
00022  *    without specific prior written permission.
00023  *
00024  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
00025  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00026  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00027  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
00028  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00029  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00030  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00031  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00032  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00033  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00034  * SUCH DAMAGE.
00035  *
00036  * @(#)btree.h 8.11 (Berkeley) 8/17/94
00037  */
00038 
00039 /* Macros to set/clear/test flags. */
00040 #define  F_SET(p, f) (p)->flags |= (f)
00041 #define  F_CLR(p, f) (p)->flags &= ~(f)
00042 #define  F_ISSET(p, f)  ((p)->flags & (f))
00043 
00044 #include <mpool.h>
00045 
00046 #define mpool_open __mpool_open
00047 #define mpool_filter __mpool_filter
00048 #define mpool_new __mpool_new
00049 #define mpool_get __mpool_get
00050 #define mpool_put __mpool_put
00051 #define mpool_sync __mpool_sync
00052 #define mpool_close __mpool_close
00053 
00054 #define  DEFMINKEYPAGE  (2)      /* Minimum keys per page */
00055 #define  MINCACHE (5)      /* Minimum cached pages */
00056 #define  MINPSIZE (512)    /* Minimum page size */
00057 
00058 /*
00059  * Page 0 of a btree file contains a copy of the meta-data.  This page is also
00060  * used as an out-of-band page, i.e. page pointers that point to nowhere point
00061  * to page 0.  Page 1 is the root of the btree.
00062  */
00063 #define  P_INVALID    0    /* Invalid tree page number. */
00064 #define  P_META       0    /* Tree metadata page number. */
00065 #define  P_ROOT       1    /* Tree root page number. */
00066 
00067 /*
00068  * There are five page layouts in the btree: btree internal pages (BINTERNAL),
00069  * btree leaf pages (BLEAF), recno internal pages (RINTERNAL), recno leaf pages
00070  * (RLEAF) and overflow pages.  All five page types have a page header (PAGE).
00071  * This implementation requires that values within structures NOT be padded.
00072  * (ANSI C permits random padding.)  If your compiler pads randomly you'll have
00073  * to do some work to get this package to run.
00074  */
00075 typedef struct _page {
00076    pgno_t   pgno;       /* this page's page number */
00077    pgno_t   prevpg;        /* left sibling */
00078    pgno_t   nextpg;        /* right sibling */
00079 
00080 #define  P_BINTERNAL 0x01     /* btree internal page */
00081 #define  P_BLEAF     0x02     /* leaf page */
00082 #define  P_OVERFLOW  0x04     /* overflow page */
00083 #define  P_RINTERNAL 0x08     /* recno internal page */
00084 #define  P_RLEAF     0x10     /* leaf page */
00085 #define P_TYPE    0x1f     /* type mask */
00086 #define  P_PRESERVE  0x20     /* never delete this chain of pages */
00087    u_int32_t flags;
00088 
00089    indx_t   lower;         /* lower bound of free space on page */
00090    indx_t   upper;         /* upper bound of free space on page */
00091    indx_t   linp[1];    /* indx_t-aligned VAR. LENGTH DATA */
00092 } PAGE;
00093 
00094 /* First and next index. */
00095 #define  BTDATAOFF                     \
00096    (sizeof(pgno_t) + sizeof(pgno_t) + sizeof(pgno_t) +      \
00097        sizeof(u_int32_t) + sizeof(indx_t) + sizeof(indx_t))
00098 #define  NEXTINDEX(p)   (((p)->lower - BTDATAOFF) / sizeof(indx_t))
00099 
00100 /*
00101  * For pages other than overflow pages, there is an array of offsets into the
00102  * rest of the page immediately following the page header.  Each offset is to
00103  * an item which is unique to the type of page.  The h_lower offset is just
00104  * past the last filled-in index.  The h_upper offset is the first item on the
00105  * page.  Offsets are from the beginning of the page.
00106  *
00107  * If an item is too big to store on a single page, a flag is set and the item
00108  * is a { page, size } pair such that the page is the first page of an overflow
00109  * chain with size bytes of item.  Overflow pages are simply bytes without any
00110  * external structure.
00111  *
00112  * The page number and size fields in the items are pgno_t-aligned so they can
00113  * be manipulated without copying.  (This presumes that 32 bit items can be
00114  * manipulated on this system.)
00115  */
00116 #define  LALIGN(n)   (((n) + sizeof(pgno_t) - 1) & ~(sizeof(pgno_t) - 1))
00117 #define  NOVFLSIZE   (sizeof(pgno_t) + sizeof(u_int32_t))
00118 
00119 /*
00120  * For the btree internal pages, the item is a key.  BINTERNALs are {key, pgno}
00121  * pairs, such that the key compares less than or equal to all of the records
00122  * on that page.  For a tree without duplicate keys, an internal page with two
00123  * consecutive keys, a and b, will have all records greater than or equal to a
00124  * and less than b stored on the page associated with a.  Duplicate keys are
00125  * somewhat special and can cause duplicate internal and leaf page records and
00126  * some minor modifications of the above rule.
00127  */
00128 typedef struct _binternal {
00129    u_int32_t ksize;     /* key size */
00130    pgno_t   pgno;       /* page number stored on */
00131 #define  P_BIGDATA   0x01     /* overflow data */
00132 #define  P_BIGKEY 0x02     /* overflow key */
00133    u_char   flags;
00134    char  bytes[1];      /* data */
00135 } BINTERNAL;
00136 
00137 /* Get the page's BINTERNAL structure at index indx. */
00138 #define  GETBINTERNAL(pg, indx)                 \
00139    ((BINTERNAL *)((char *)(pg) + (pg)->linp[indx]))
00140 
00141 /* Get the number of bytes in the entry. */
00142 #define NBINTERNAL(len)                   \
00143    LALIGN(sizeof(u_int32_t) + sizeof(pgno_t) + sizeof(u_char) + (len))
00144 
00145 /* Copy a BINTERNAL entry to the page. */
00146 #define  WR_BINTERNAL(p, size, pgno, flags) {            \
00147    *(u_int32_t *)p = size;                \
00148    p += sizeof(u_int32_t);                \
00149    *(pgno_t *)p = pgno;                \
00150    p += sizeof(pgno_t);                \
00151    *(u_char *)p = flags;                  \
00152    p += sizeof(u_char);                \
00153 }
00154 
00155 /*
00156  * For the recno internal pages, the item is a page number with the number of
00157  * keys found on that page and below.
00158  */
00159 typedef struct _rinternal {
00160    recno_t  nrecs;         /* number of records */
00161    pgno_t   pgno;       /* page number stored below */
00162 } RINTERNAL;
00163 
00164 /* Get the page's RINTERNAL structure at index indx. */
00165 #define  GETRINTERNAL(pg, indx)                 \
00166    ((RINTERNAL *)((char *)(pg) + (pg)->linp[indx]))
00167 
00168 /* Get the number of bytes in the entry. */
00169 #define NRINTERNAL                     \
00170    LALIGN(sizeof(recno_t) + sizeof(pgno_t))
00171 
00172 /* Copy a RINTERNAL entry to the page. */
00173 #define  WR_RINTERNAL(p, nrecs, pgno) {               \
00174    *(recno_t *)p = nrecs;                 \
00175    p += sizeof(recno_t);                  \
00176    *(pgno_t *)p = pgno;                \
00177 }
00178 
00179 /* For the btree leaf pages, the item is a key and data pair. */
00180 typedef struct _bleaf {
00181    u_int32_t   ksize;      /* size of key */
00182    u_int32_t   dsize;      /* size of data */
00183    u_char   flags;         /* P_BIGDATA, P_BIGKEY */
00184    char  bytes[1];      /* data */
00185 } BLEAF;
00186 
00187 /* Get the page's BLEAF structure at index indx. */
00188 #define  GETBLEAF(pg, indx)                  \
00189    ((BLEAF *)((char *)(pg) + (pg)->linp[indx]))
00190 
00191 /* Get the number of bytes in the entry. */
00192 #define NBLEAF(p) NBLEAFDBT((p)->ksize, (p)->dsize)
00193 
00194 /* Get the number of bytes in the user's key/data pair. */
00195 #define NBLEAFDBT(ksize, dsize)                 \
00196    LALIGN(sizeof(u_int32_t) + sizeof(u_int32_t) + sizeof(u_char) +   \
00197        (ksize) + (dsize))
00198 
00199 /* Copy a BLEAF entry to the page. */
00200 #define  WR_BLEAF(p, key, data, flags) {              \
00201    *(u_int32_t *)p = key->size;              \
00202    p += sizeof(u_int32_t);                \
00203    *(u_int32_t *)p = data->size;             \
00204    p += sizeof(u_int32_t);                \
00205    *(u_char *)p = flags;                  \
00206    p += sizeof(u_char);                \
00207    memmove(p, key->data, key->size);            \
00208    p += key->size;                     \
00209    memmove(p, data->data, data->size);          \
00210 }
00211 
00212 /* For the recno leaf pages, the item is a data entry. */
00213 typedef struct _rleaf {
00214    u_int32_t   dsize;      /* size of data */
00215    u_char   flags;         /* P_BIGDATA */
00216    char  bytes[1];
00217 } RLEAF;
00218 
00219 /* Get the page's RLEAF structure at index indx. */
00220 #define  GETRLEAF(pg, indx)                  \
00221    ((RLEAF *)((char *)(pg) + (pg)->linp[indx]))
00222 
00223 /* Get the number of bytes in the entry. */
00224 #define NRLEAF(p) NRLEAFDBT((p)->dsize)
00225 
00226 /* Get the number of bytes from the user's data. */
00227 #define  NRLEAFDBT(dsize)                 \
00228    LALIGN(sizeof(u_int32_t) + sizeof(u_char) + (dsize))
00229 
00230 /* Copy a RLEAF entry to the page. */
00231 #define  WR_RLEAF(p, data, flags) {             \
00232    *(u_int32_t *)p = data->size;             \
00233    p += sizeof(u_int32_t);                \
00234    *(u_char *)p = flags;                  \
00235    p += sizeof(u_char);                \
00236    memmove(p, data->data, data->size);          \
00237 }
00238 
00239 /*
00240  * A record in the tree is either a pointer to a page and an index in the page
00241  * or a page number and an index.  These structures are used as a cursor, stack
00242  * entry and search returns as well as to pass records to other routines.
00243  *
00244  * One comment about searches.  Internal page searches must find the largest
00245  * record less than key in the tree so that descents work.  Leaf page searches
00246  * must find the smallest record greater than key so that the returned index
00247  * is the record's correct position for insertion.
00248  */
00249 typedef struct _epgno {
00250    pgno_t   pgno;       /* the page number */
00251    indx_t   index;         /* the index on the page */
00252 } EPGNO;
00253 
00254 typedef struct _epg {
00255    PAGE  *page;         /* the (pinned) page */
00256    indx_t    index;        /* the index on the page */
00257 } EPG;
00258 
00259 /*
00260  * About cursors.  The cursor (and the page that contained the key/data pair
00261  * that it referenced) can be deleted, which makes things a bit tricky.  If
00262  * there are no duplicates of the cursor key in the tree (i.e. B_NODUPS is set
00263  * or there simply aren't any duplicates of the key) we copy the key that it
00264  * referenced when it's deleted, and reacquire a new cursor key if the cursor
00265  * is used again.  If there are duplicates keys, we move to the next/previous
00266  * key, and set a flag so that we know what happened.  NOTE: if duplicate (to
00267  * the cursor) keys are added to the tree during this process, it is undefined
00268  * if they will be returned or not in a cursor scan.
00269  *
00270  * The flags determine the possible states of the cursor:
00271  *
00272  * CURS_INIT   The cursor references *something*.
00273  * CURS_ACQUIRE   The cursor was deleted, and a key has been saved so that
00274  *    we can reacquire the right position in the tree.
00275  * CURS_AFTER, CURS_BEFORE
00276  *    The cursor was deleted, and now references a key/data pair
00277  *    that has not yet been returned, either before or after the
00278  *    deleted key/data pair.
00279  * XXX
00280  * This structure is broken out so that we can eventually offer multiple
00281  * cursors as part of the DB interface.
00282  */
00283 typedef struct _cursor {
00284    EPGNO  pg;        /* B: Saved tree reference. */
00285    DBT    key;       /* B: Saved key, or key.data == NULL. */
00286    recno_t   rcursor;      /* R: recno cursor (1-based) */
00287 
00288 #define  CURS_ACQUIRE   0x01     /*  B: Cursor needs to be reacquired. */
00289 #define  CURS_AFTER  0x02     /*  B: Unreturned cursor after key. */
00290 #define  CURS_BEFORE 0x04     /*  B: Unreturned cursor before key. */
00291 #define  CURS_INIT   0x08     /* RB: Cursor initialized. */
00292    u_int8_t flags;
00293 } CURSOR;
00294 
00295 /*
00296  * The metadata of the tree.  The nrecs field is used only by the RECNO code.
00297  * This is because the btree doesn't really need it and it requires that every
00298  * put or delete call modify the metadata.
00299  */
00300 typedef struct _btmeta {
00301    u_int32_t   magic;      /* magic number */
00302    u_int32_t   version; /* version */
00303    u_int32_t   psize;      /* page size */
00304    u_int32_t   free;    /* page number of first free page */
00305    u_int32_t   nrecs;      /* R: number of records */
00306 
00307 #define  SAVEMETA (B_NODUPS | R_RECNO)
00308    u_int32_t   flags;      /* bt_flags & SAVEMETA */
00309 } BTMETA;
00310 
00311 /* The in-memory btree/recno data structure. */
00312 typedef struct _btree {
00313    MPOOL  *bt_mp;    /* memory pool cookie */
00314 
00315    DB  *bt_dbp;      /* pointer to enclosing DB */
00316 
00317    EPG     bt_cur;      /* current (pinned) page */
00318    PAGE   *bt_pinned;      /* page pinned across calls */
00319 
00320    CURSOR     bt_cursor;      /* cursor */
00321 
00322 #define  BT_PUSH(t, p, i) {                  \
00323    t->bt_sp->pgno = p;                 \
00324    t->bt_sp->index = i;                   \
00325    ++t->bt_sp;                   \
00326 }
00327 #define  BT_POP(t)   (t->bt_sp == t->bt_stack ? NULL : --t->bt_sp)
00328 #define  BT_CLR(t)   (t->bt_sp = t->bt_stack)
00329    EPGNO   bt_stack[50];      /* stack of parent pages */
00330    EPGNO  *bt_sp;    /* current stack pointer */
00331 
00332    DBT     bt_rkey;     /* returned key */
00333    DBT     bt_rdata;    /* returned data */
00334 
00335    int     bt_fd;    /* tree file descriptor */
00336 
00337    pgno_t     bt_free;     /* next free page */
00338    u_int32_t bt_psize;     /* page size */
00339    indx_t     bt_ovflsize;    /* cut-off for key/data overflow */
00340    int     bt_lorder;      /* byte order */
00341                /* sorted order */
00342    enum { NOT, BACK, FORWARD } bt_order;
00343    EPGNO   bt_last;     /* last insert */
00344 
00345                /* B: key comparison function */
00346    int   (*bt_cmp) __P((const DBT *, const DBT *));
00347                /* B: prefix comparison function */
00348    size_t   (*bt_pfx) __P((const DBT *, const DBT *));
00349                /* R: recno input function */
00350    int   (*bt_irec) __P((struct _btree *, recno_t));
00351 
00352    FILE   *bt_rfp;      /* R: record FILE pointer */
00353    int     bt_rfd;      /* R: record file descriptor */
00354 
00355    caddr_t    bt_cmap;     /* R: current point in mapped space */
00356    caddr_t    bt_smap;     /* R: start of mapped space */
00357    caddr_t   bt_emap;      /* R: end of mapped space */
00358    size_t     bt_msize;    /* R: size of mapped region. */
00359 
00360    recno_t    bt_nrecs;    /* R: number of records */
00361    size_t     bt_reclen;      /* R: fixed record length */
00362    u_char     bt_bval;     /* R: delimiting byte/pad character */
00363 
00364 /*
00365  * NB:
00366  * B_NODUPS and R_RECNO are stored on disk, and may not be changed.
00367  */
00368 #define  B_INMEM     0x00001     /* in-memory tree */
00369 #define  B_METADIRTY 0x00002     /* need to write metadata */
00370 #define  B_MODIFIED  0x00004     /* tree modified */
00371 #define  B_NEEDSWAP  0x00008     /* if byte order requires swapping */
00372 #define  B_RDONLY 0x00010     /* read-only tree */
00373 
00374 #define  B_NODUPS 0x00020     /* no duplicate keys permitted */
00375 #define  R_RECNO     0x00080     /* record oriented tree */
00376 
00377 #define  R_CLOSEFP   0x00040     /* opened a file pointer */
00378 #define  R_EOF    0x00100     /* end of input file reached. */
00379 #define  R_FIXLEN 0x00200     /* fixed length records */
00380 #define  R_MEMMAPPED 0x00400     /* memory mapped file. */
00381 #define  R_INMEM     0x00800     /* in-memory file */
00382 #define  R_MODIFIED  0x01000     /* modified file */
00383 #define  R_RDONLY 0x02000     /* read-only file */
00384 
00385 #define  B_DB_LOCK   0x04000     /* DB_LOCK specified. */
00386 #define  B_DB_SHMEM  0x08000     /* DB_SHMEM specified. */
00387 #define  B_DB_TXN 0x10000     /* DB_TXN specified. */
00388    u_int32_t flags;
00389 } BTREE;
00390 
00391 #include "extern.h"

Generated on Thu Apr 16 06:27:16 2015 for Asterisk - The Open Source Telephony Project by  doxygen 1.5.6