bt_overflow.c

Go to the documentation of this file.
00001 /*-
00002  * Copyright (c) 1990, 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 
00037 #if defined(LIBC_SCCS) && !defined(lint)
00038 static char sccsid[] = "@(#)bt_overflow.c 8.5 (Berkeley) 7/16/94";
00039 #endif /* LIBC_SCCS and not lint */
00040 
00041 #include <sys/param.h>
00042 
00043 #include <stdio.h>
00044 #include <stdlib.h>
00045 #include <string.h>
00046 
00047 #include "../include/db.h"
00048 #include "btree.h"
00049 
00050 /*
00051  * Big key/data code.
00052  *
00053  * Big key and data entries are stored on linked lists of pages.  The initial
00054  * reference is byte string stored with the key or data and is the page number
00055  * and size.  The actual record is stored in a chain of pages linked by the
00056  * nextpg field of the PAGE header.
00057  *
00058  * The first page of the chain has a special property.  If the record is used
00059  * by an internal page, it cannot be deleted and the P_PRESERVE bit will be set
00060  * in the header.
00061  *
00062  * XXX
00063  * A single DBT is written to each chain, so a lot of space on the last page
00064  * is wasted.  This is a fairly major bug for some data sets.
00065  */
00066 
00067 /*
00068  * __OVFL_GET -- Get an overflow key/data item.
00069  *
00070  * Parameters:
00071  * t: tree
00072  * p: pointer to { pgno_t, u_int32_t }
00073  * buf:  storage address
00074  * bufsz:   storage size
00075  *
00076  * Returns:
00077  * RET_ERROR, RET_SUCCESS
00078  */
00079 int
00080 __ovfl_get(t, p, ssz, buf, bufsz)
00081    BTREE *t;
00082    void *p;
00083    size_t *ssz;
00084    void **buf;
00085    size_t *bufsz;
00086 {
00087    PAGE *h;
00088    pgno_t pg;
00089    size_t nb, plen;
00090    u_int32_t sz;
00091 
00092    memmove(&pg, p, sizeof(pgno_t));
00093    memmove(&sz, (char *)p + sizeof(pgno_t), sizeof(u_int32_t));
00094    *ssz = sz;
00095 
00096 #ifdef DEBUG
00097    if (pg == P_INVALID || sz == 0)
00098       abort();
00099 #endif
00100    /* Make the buffer bigger as necessary. */
00101    if (*bufsz < sz) {
00102       *buf = (char *)(*buf == NULL ? malloc(sz) : realloc(*buf, sz));
00103       if (*buf == NULL)
00104          return (RET_ERROR);
00105       *bufsz = sz;
00106    }
00107 
00108    /*
00109     * Step through the linked list of pages, copying the data on each one
00110     * into the buffer.  Never copy more than the data's length.
00111     */
00112    plen = t->bt_psize - BTDATAOFF;
00113    for (p = *buf;; p = (char *)p + nb, pg = h->nextpg) {
00114       if ((h = mpool_get(t->bt_mp, pg, 0)) == NULL)
00115          return (RET_ERROR);
00116 
00117       nb = MIN(sz, plen);
00118       memmove(p, (char *)h + BTDATAOFF, nb);
00119       mpool_put(t->bt_mp, h, 0);
00120 
00121       if ((sz -= nb) == 0)
00122          break;
00123    }
00124    return (RET_SUCCESS);
00125 }
00126 
00127 /*
00128  * __OVFL_PUT -- Store an overflow key/data item.
00129  *
00130  * Parameters:
00131  * t: tree
00132  * data: DBT to store
00133  * pgno: storage page number
00134  *
00135  * Returns:
00136  * RET_ERROR, RET_SUCCESS
00137  */
00138 int
00139 __ovfl_put(t, dbt, pg)
00140    BTREE *t;
00141    const DBT *dbt;
00142    pgno_t *pg;
00143 {
00144    PAGE *h, *last;
00145    void *p;
00146    pgno_t npg;
00147    size_t nb, plen;
00148    u_int32_t sz;
00149 
00150    /*
00151     * Allocate pages and copy the key/data record into them.  Store the
00152     * number of the first page in the chain.
00153     */
00154    plen = t->bt_psize - BTDATAOFF;
00155    for (last = NULL, p = dbt->data, sz = dbt->size;;
00156        p = (char *)p + plen, last = h) {
00157       if ((h = __bt_new(t, &npg)) == NULL)
00158          return (RET_ERROR);
00159 
00160       h->pgno = npg;
00161       h->nextpg = h->prevpg = P_INVALID;
00162       h->flags = P_OVERFLOW;
00163       h->lower = h->upper = 0;
00164 
00165       nb = MIN(sz, plen);
00166       memmove((char *)h + BTDATAOFF, p, nb);
00167 
00168       if (last) {
00169          last->nextpg = h->pgno;
00170          mpool_put(t->bt_mp, last, MPOOL_DIRTY);
00171       } else
00172          *pg = h->pgno;
00173 
00174       if ((sz -= nb) == 0) {
00175          mpool_put(t->bt_mp, h, MPOOL_DIRTY);
00176          break;
00177       }
00178    }
00179    return (RET_SUCCESS);
00180 }
00181 
00182 /*
00183  * __OVFL_DELETE -- Delete an overflow chain.
00184  *
00185  * Parameters:
00186  * t: tree
00187  * p: pointer to { pgno_t, u_int32_t }
00188  *
00189  * Returns:
00190  * RET_ERROR, RET_SUCCESS
00191  */
00192 int
00193 __ovfl_delete(t, p)
00194    BTREE *t;
00195    void *p;
00196 {
00197    PAGE *h;
00198    pgno_t pg;
00199    size_t plen;
00200    u_int32_t sz;
00201 
00202    memmove(&pg, p, sizeof(pgno_t));
00203    memmove(&sz, (char *)p + sizeof(pgno_t), sizeof(u_int32_t));
00204 
00205 #ifdef DEBUG
00206    if (pg == P_INVALID || sz == 0)
00207       abort();
00208 #endif
00209    if ((h = mpool_get(t->bt_mp, pg, 0)) == NULL)
00210       return (RET_ERROR);
00211 
00212    /* Don't delete chains used by internal pages. */
00213    if (h->flags & P_PRESERVE) {
00214       mpool_put(t->bt_mp, h, 0);
00215       return (RET_SUCCESS);
00216    }
00217 
00218    /* Step through the chain, calling the free routine for each page. */
00219    for (plen = t->bt_psize - BTDATAOFF;; sz -= plen) {
00220       pg = h->nextpg;
00221       __bt_free(t, h);
00222       if (sz <= plen)
00223          break;
00224       if ((h = mpool_get(t->bt_mp, pg, 0)) == NULL)
00225          return (RET_ERROR);
00226    }
00227    return (RET_SUCCESS);
00228 }

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