home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 July / macformat-026.iso / mac / Shareware City / Developers / berkeleydb1.73 / Berkeley_db / btree / btree.h < prev    next >
Encoding:
C/C++ Source or Header  |  1994-05-11  |  12.8 KB  |  353 lines  |  [TEXT/MPS ]

  1. /*-
  2.  * Copyright (c) 1991, 1993
  3.  *    The Regents of the University of California.  All rights reserved.
  4.  *
  5.  * This code is derived from software contributed to Berkeley by
  6.  * Mike Olson.
  7.  *
  8.  * Redistribution and use in source and binary forms, with or without
  9.  * modification, are permitted provided that the following conditions
  10.  * are met:
  11.  * 1. Redistributions of source code must retain the above copyright
  12.  *    notice, this list of conditions and the following disclaimer.
  13.  * 2. Redistributions in binary form must reproduce the above copyright
  14.  *    notice, this list of conditions and the following disclaimer in the
  15.  *    documentation and/or other materials provided with the distribution.
  16.  * 3. All advertising materials mentioning features or use of this software
  17.  *    must display the following acknowledgement:
  18.  *    This product includes software developed by the University of
  19.  *    California, Berkeley and its contributors.
  20.  * 4. Neither the name of the University nor the names of its contributors
  21.  *    may be used to endorse or promote products derived from this software
  22.  *    without specific prior written permission.
  23.  *
  24.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  25.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  28.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  29.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  30.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  31.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  32.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  33.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  34.  * SUCH DAMAGE.
  35.  *
  36.  *    @(#)btree.h    8.4 (Berkeley) 12/18/93
  37.  */
  38.  
  39. #include <mpool.h>
  40.  
  41. #define    DEFMINKEYPAGE    (2)        /* Minimum keys per page */
  42. #define    MINCACHE    (5)        /* Minimum cached pages */
  43. #define    MINPSIZE    (512)        /* Minimum page size */
  44.  
  45. /*
  46.  * Page 0 of a btree file contains a copy of the meta-data.  This page is also
  47.  * used as an out-of-band page, i.e. page pointers that point to nowhere point
  48.  * to page 0.  Page 1 is the root of the btree.
  49.  */
  50. #define    P_INVALID     0        /* Invalid tree page number. */
  51. #define    P_META         0        /* Tree metadata page number. */
  52. #define    P_ROOT         1        /* Tree root page number. */
  53.  
  54. /*
  55.  * There are five page layouts in the btree: btree internal pages (BINTERNAL),
  56.  * btree leaf pages (BLEAF), recno internal pages (RINTERNAL), recno leaf pages
  57.  * (RLEAF) and overflow pages.  All five page types have a page header (PAGE).
  58.  * This implementation requires that longs within structures are NOT padded.
  59.  * (ANSI C permits random padding.)  If your compiler pads randomly you'll have
  60.  * to do some work to get this package to run.
  61.  */
  62. typedef struct _page {
  63.     pgno_t    pgno;            /* this page's page number */
  64.     pgno_t    prevpg;            /* left sibling */
  65.     pgno_t    nextpg;            /* right sibling */
  66.  
  67. #define    P_BINTERNAL    0x01        /* btree internal page */
  68. #define    P_BLEAF        0x02        /* leaf page */
  69. #define    P_OVERFLOW    0x04        /* overflow page */
  70. #define    P_RINTERNAL    0x08        /* recno internal page */
  71. #define    P_RLEAF        0x10        /* leaf page */
  72. #define P_TYPE        0x1f        /* type mask */
  73.  
  74. #define    P_PRESERVE    0x20        /* never delete this chain of pages */
  75.     u_long    flags;
  76.  
  77.     indx_t    lower;            /* lower bound of free space on page */
  78.     indx_t    upper;            /* upper bound of free space on page */
  79.     indx_t    linp[1];        /* long-aligned VARIABLE LENGTH DATA */
  80. } PAGE;
  81.  
  82. /* First and next index. */
  83. #define    BTDATAOFF    (sizeof(pgno_t) + sizeof(pgno_t) + sizeof(pgno_t) + \
  84.                 sizeof(u_long) + sizeof(indx_t) + sizeof(indx_t))
  85. #define    NEXTINDEX(p)    (((p)->lower - BTDATAOFF) / sizeof(indx_t))
  86.  
  87. /*
  88.  * For pages other than overflow pages, there is an array of offsets into the
  89.  * rest of the page immediately following the page header.  Each offset is to
  90.  * an item which is unique to the type of page.  The h_lower offset is just
  91.  * past the last filled-in index.  The h_upper offset is the first item on the
  92.  * page.  Offsets are from the beginning of the page.
  93.  *
  94.  * If an item is too big to store on a single page, a flag is set and the item
  95.  * is a { page, size } pair such that the page is the first page of an overflow
  96.  * chain with size bytes of item.  Overflow pages are simply bytes without any
  97.  * external structure.
  98.  *
  99.  * The size and page number fields in the items are long aligned so they can be
  100.  * manipulated without copying.
  101.  */
  102. #define    LALIGN(n)    (((n) + sizeof(u_long) - 1) & ~(sizeof(u_long) - 1))
  103. #define    NOVFLSIZE    (sizeof(pgno_t) + sizeof(size_t))
  104.  
  105. /*
  106.  * For the btree internal pages, the item is a key.  BINTERNALs are {key, pgno}
  107.  * pairs, such that the key compares less than or equal to all of the records
  108.  * on that page.  For a tree without duplicate keys, an internal page with two
  109.  * consecutive keys, a and b, will have all records greater than or equal to a
  110.  * and less than b stored on the page associated with a.  Duplicate keys are
  111.  * somewhat special and can cause duplicate internal and leaf page records and
  112.  * some minor modifications of the above rule.
  113.  */
  114. typedef struct _binternal {
  115.     size_t    ksize;            /* key size */
  116.     pgno_t    pgno;            /* page number stored on */
  117. #define    P_BIGDATA    0x01        /* overflow data */
  118. #define    P_BIGKEY    0x02        /* overflow key */
  119.     u_char    flags;
  120.     char    bytes[1];        /* data */
  121. } BINTERNAL;
  122.  
  123. /* Get the page's BINTERNAL structure at index indx. */
  124. #define    GETBINTERNAL(pg, indx) \
  125.     ((BINTERNAL *)((char *)(pg) + (pg)->linp[indx]))
  126.  
  127. /* Get the number of bytes in the entry. */
  128. #define NBINTERNAL(len) \
  129.     LALIGN(sizeof(size_t) + sizeof(pgno_t) + sizeof(u_char) + (len))
  130.  
  131. /* Copy a BINTERNAL entry to the page. */
  132. #define    WR_BINTERNAL(p, size, pgno, flags) { \
  133.     *(size_t *)p = size; \
  134.     p += sizeof(size_t); \
  135.     *(pgno_t *)p = pgno; \
  136.     p += sizeof(pgno_t); \
  137.     *(u_char *)p = flags; \
  138.     p += sizeof(u_char); \
  139. }
  140.  
  141. /*
  142.  * For the recno internal pages, the item is a page number with the number of
  143.  * keys found on that page and below.
  144.  */
  145. typedef struct _rinternal {
  146.     recno_t    nrecs;            /* number of records */
  147.     pgno_t    pgno;            /* page number stored below */
  148. } RINTERNAL;
  149.  
  150. /* Get the page's RINTERNAL structure at index indx. */
  151. #define    GETRINTERNAL(pg, indx) \
  152.     ((RINTERNAL *)((char *)(pg) + (pg)->linp[indx]))
  153.  
  154. /* Get the number of bytes in the entry. */
  155. #define NRINTERNAL \
  156.     LALIGN(sizeof(recno_t) + sizeof(pgno_t))
  157.  
  158. /* Copy a RINTERAL entry to the page. */
  159. #define    WR_RINTERNAL(p, nrecs, pgno) { \
  160.     *(recno_t *)p = nrecs; \
  161.     p += sizeof(recno_t); \
  162.     *(pgno_t *)p = pgno; \
  163. }
  164.  
  165. /* For the btree leaf pages, the item is a key and data pair. */
  166. typedef struct _bleaf {
  167.     size_t    ksize;            /* size of key */
  168.     size_t    dsize;            /* size of data */
  169.     u_char    flags;            /* P_BIGDATA, P_BIGKEY */
  170.     char    bytes[1];        /* data */
  171. } BLEAF;
  172.  
  173. /* Get the page's BLEAF structure at index indx. */
  174. #define    GETBLEAF(pg, indx) \
  175.     ((BLEAF *)((char *)(pg) + (pg)->linp[indx]))
  176.  
  177. /* Get the number of bytes in the entry. */
  178. #define NBLEAF(p)    NBLEAFDBT((p)->ksize, (p)->dsize)
  179.  
  180. /* Get the number of bytes in the user's key/data pair. */
  181. #define NBLEAFDBT(ksize, dsize) \
  182.     LALIGN(sizeof(size_t) + sizeof(size_t) + sizeof(u_char) + \
  183.         (ksize) + (dsize))
  184.  
  185. /* Copy a BLEAF entry to the page. */
  186. #define    WR_BLEAF(p, key, data, flags) { \
  187.     *(size_t *)p = key->size; \
  188.     p += sizeof(size_t); \
  189.     *(size_t *)p = data->size; \
  190.     p += sizeof(size_t); \
  191.     *(u_char *)p = flags; \
  192.     p += sizeof(u_char); \
  193.     memmove(p, key->data, key->size); \
  194.     p += key->size; \
  195.     memmove(p, data->data, data->size); \
  196. }
  197.  
  198. /* For the recno leaf pages, the item is a data entry. */
  199. typedef struct _rleaf {
  200.     size_t    dsize;            /* size of data */
  201.     u_char    flags;            /* P_BIGDATA */
  202.     char    bytes[1];
  203. } RLEAF;
  204.  
  205. /* Get the page's RLEAF structure at index indx. */
  206. #define    GETRLEAF(pg, indx) \
  207.     ((RLEAF *)((char *)(pg) + (pg)->linp[indx]))
  208.  
  209. /* Get the number of bytes in the entry. */
  210. #define NRLEAF(p)    NRLEAFDBT((p)->dsize)
  211.  
  212. /* Get the number of bytes from the user's data. */
  213. #define    NRLEAFDBT(dsize) \
  214.     LALIGN(sizeof(size_t) + sizeof(u_char) + (dsize))
  215.  
  216. /* Copy a RLEAF entry to the page. */
  217. #define    WR_RLEAF(p, data, flags) { \
  218.     *(size_t *)p = data->size; \
  219.     p += sizeof(size_t); \
  220.     *(u_char *)p = flags; \
  221.     p += sizeof(u_char); \
  222.     memmove(p, data->data, data->size); \
  223. }
  224.  
  225. /*
  226.  * A record in the tree is either a pointer to a page and an index in the page
  227.  * or a page number and an index.  These structures are used as a cursor, stack
  228.  * entry and search returns as well as to pass records to other routines.
  229.  *
  230.  * One comment about searches.  Internal page searches must find the largest
  231.  * record less than key in the tree so that descents work.  Leaf page searches
  232.  * must find the smallest record greater than key so that the returned index
  233.  * is the record's correct position for insertion.
  234.  *
  235.  * One comment about cursors.  The cursor key is never removed from the tree,
  236.  * even if deleted.  This is because it is quite difficult to decide where the
  237.  * cursor should be when other keys have been inserted/deleted in the tree;
  238.  * duplicate keys make it impossible.  This scheme does require extra work
  239.  * though, to make sure that we don't perform an operation on a deleted key.
  240.  */
  241. typedef struct _epgno {
  242.     pgno_t    pgno;            /* the page number */
  243.     indx_t    index;            /* the index on the page */
  244. } EPGNO;
  245.  
  246. typedef struct _epg {
  247.     PAGE    *page;            /* the (pinned) page */
  248.     indx_t     index;            /* the index on the page */
  249. } EPG;
  250.  
  251. /*
  252.  * The metadata of the tree.  The m_nrecs field is used only by the RECNO code.
  253.  * This is because the btree doesn't really need it and it requires that every
  254.  * put or delete call modify the metadata.
  255.  */
  256. typedef struct _btmeta {
  257.     u_long    m_magic;        /* magic number */
  258.     u_long    m_version;        /* version */
  259.     u_long    m_psize;        /* page size */
  260.     u_long    m_free;            /* page number of first free page */
  261.     u_long    m_nrecs;        /* R: number of records */
  262. #define    SAVEMETA    (B_NODUPS | R_RECNO)
  263.     u_long    m_flags;        /* bt_flags & SAVEMETA */
  264.     u_long    m_unused;        /* unused */
  265. } BTMETA;
  266.  
  267. /* The in-memory btree/recno data structure. */
  268. typedef struct _btree {
  269.     MPOOL    *bt_mp;            /* memory pool cookie */
  270.  
  271.     DB    *bt_dbp;        /* pointer to enclosing DB */
  272.  
  273.     EPG    bt_cur;            /* current (pinned) page */
  274.     PAGE    *bt_pinned;        /* page pinned across calls */
  275.  
  276.     EPGNO    bt_bcursor;        /* B: btree cursor */
  277.     recno_t    bt_rcursor;        /* R: recno cursor (1-based) */
  278.  
  279. #define    BT_POP(t)    (t->bt_sp ? t->bt_stack + --t->bt_sp : NULL)
  280. #define    BT_CLR(t)    (t->bt_sp = 0)
  281.     EPGNO    *bt_stack;        /* stack of parent pages */
  282.     u_int    bt_sp;            /* current stack pointer */
  283.     u_int    bt_maxstack;        /* largest stack */
  284.  
  285.     char    *bt_kbuf;        /* key buffer */
  286.     size_t    bt_kbufsz;        /* key buffer size */
  287.     char    *bt_dbuf;        /* data buffer */
  288.     size_t    bt_dbufsz;        /* data buffer size */
  289.  
  290.     int    bt_fd;            /* tree file descriptor */
  291.  
  292.     pgno_t    bt_free;        /* next free page */
  293.     u_long    bt_psize;        /* page size */
  294.     indx_t    bt_ovflsize;        /* cut-off for key/data overflow */
  295.     int    bt_lorder;        /* byte order */
  296.                     /* sorted order */
  297.     enum { NOT, BACK, FORWARD } bt_order;
  298.     EPGNO    bt_last;        /* last insert */
  299.  
  300.                     /* B: key comparison function */
  301.     int    (*bt_cmp) __P((const DBT *, const DBT *));
  302.                     /* B: prefix comparison function */
  303.     int    (*bt_pfx) __P((const DBT *, const DBT *));
  304.                     /* R: recno input function */
  305.     int    (*bt_irec) __P((struct _btree *, recno_t));
  306.  
  307.     FILE    *bt_rfp;        /* R: record FILE pointer */
  308.     int    bt_rfd;            /* R: record file descriptor */
  309.  
  310.     caddr_t    bt_cmap;        /* R: current point in mapped space */
  311.     caddr_t    bt_smap;        /* R: start of mapped space */
  312.     caddr_t bt_emap;        /* R: end of mapped space */
  313.     size_t    bt_msize;        /* R: size of mapped region. */
  314.  
  315.     recno_t    bt_nrecs;        /* R: number of records */
  316.     size_t    bt_reclen;        /* R: fixed record length */
  317.     u_char    bt_bval;        /* R: delimiting byte/pad character */
  318.  
  319. /*
  320.  * NB:
  321.  * B_NODUPS and R_RECNO are stored on disk, and may not be changed.
  322.  */
  323. #define    B_DELCRSR    0x00001        /* cursor has been deleted */
  324. #define    B_INMEM        0x00002        /* in-memory tree */
  325. #define    B_METADIRTY    0x00004        /* need to write metadata */
  326. #define    B_MODIFIED    0x00008        /* tree modified */
  327. #define    B_NEEDSWAP    0x00010        /* if byte order requires swapping */
  328. #define    B_NODUPS    0x00020        /* no duplicate keys permitted */
  329. #define    B_RDONLY    0x00040        /* read-only tree */
  330. #define    R_RECNO        0x00080        /* record oriented tree */
  331. #define    B_SEQINIT    0x00100        /* sequential scan initialized */
  332.  
  333. #define    R_CLOSEFP    0x00200        /* opened a file pointer */
  334. #define    R_EOF        0x00400        /* end of input file reached. */
  335. #define    R_FIXLEN    0x00800        /* fixed length records */
  336. #define    R_MEMMAPPED    0x01000        /* memory mapped file. */
  337. #define    R_INMEM        0x02000        /* in-memory file */
  338. #define    R_MODIFIED    0x04000        /* modified file */
  339. #define    R_RDONLY    0x08000        /* read-only file */
  340.  
  341. #define    B_DB_LOCK    0x10000        /* DB_LOCK specified. */
  342. #define    B_DB_SHMEM    0x20000        /* DB_SHMEM specified. */
  343. #define    B_DB_TXN    0x40000        /* DB_TXN specified. */
  344.  
  345.     u_long        bt_flags;    /* btree state */
  346. } BTREE;
  347.  
  348. #define    SET(t, f)    ((t)->bt_flags |= (f))
  349. #define    CLR(t, f)    ((t)->bt_flags &= ~(f))
  350. #define    ISSET(t, f)    ((t)->bt_flags & (f))
  351.  
  352. #include "extern.h"
  353.