OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [tags/] [gnu-src/] [newlib-1.18.0/] [newlib-1.18.0-or32-1.0rc1/] [newlib/] [libc/] [search/] [hash_bigkey.c] - Diff between revs 207 and 345

Go to most recent revision | Only display areas with differences | Details | Blame | View Log

Rev 207 Rev 345
/*-
/*-
 * Copyright (c) 1990, 1993, 1994
 * Copyright (c) 1990, 1993, 1994
 *      The Regents of the University of California.  All rights reserved.
 *      The Regents of the University of California.  All rights reserved.
 *
 *
 * This code is derived from software contributed to Berkeley by
 * This code is derived from software contributed to Berkeley by
 * Margo Seltzer.
 * Margo Seltzer.
 *
 *
 * Redistribution and use in source and binary forms, with or without
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * modification, are permitted provided that the following conditions
 * are met:
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *    must display the following acknowledgement:
 *      This product includes software developed by the University of
 *      This product includes software developed by the University of
 *      California, Berkeley and its contributors.
 *      California, Berkeley and its contributors.
 * 4. Neither the name of the University nor the names of its contributors
 * 4. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *    without specific prior written permission.
 *
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 * SUCH DAMAGE.
 */
 */
 
 
#include <sys/param.h>
#include <sys/param.h>
#if defined(LIBC_SCCS) && !defined(lint)
#if defined(LIBC_SCCS) && !defined(lint)
static char sccsid[] = "@(#)hash_bigkey.c       8.3 (Berkeley) 5/31/94";
static char sccsid[] = "@(#)hash_bigkey.c       8.3 (Berkeley) 5/31/94";
#endif /* LIBC_SCCS and not lint */
#endif /* LIBC_SCCS and not lint */
#include <sys/cdefs.h>
#include <sys/cdefs.h>
 
 
/*
/*
 * PACKAGE: hash
 * PACKAGE: hash
 * DESCRIPTION:
 * DESCRIPTION:
 *      Big key/data handling for the hashing package.
 *      Big key/data handling for the hashing package.
 *
 *
 * ROUTINES:
 * ROUTINES:
 * External
 * External
 *      __big_keydata
 *      __big_keydata
 *      __big_split
 *      __big_split
 *      __big_insert
 *      __big_insert
 *      __big_return
 *      __big_return
 *      __big_delete
 *      __big_delete
 *      __find_last_page
 *      __find_last_page
 * Internal
 * Internal
 *      collect_key
 *      collect_key
 *      collect_data
 *      collect_data
 */
 */
 
 
#include <sys/param.h>
#include <sys/param.h>
 
 
#include <errno.h>
#include <errno.h>
#include <stdio.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdlib.h>
#include <string.h>
#include <string.h>
 
 
#ifdef DEBUG
#ifdef DEBUG
#include <assert.h>
#include <assert.h>
#endif
#endif
 
 
#include "db_local.h"
#include "db_local.h"
#include "hash.h"
#include "hash.h"
#include "page.h"
#include "page.h"
#include "extern.h"
#include "extern.h"
 
 
static int collect_key(HTAB *, BUFHEAD *, int, DBT *, int);
static int collect_key(HTAB *, BUFHEAD *, int, DBT *, int);
static int collect_data(HTAB *, BUFHEAD *, int, int);
static int collect_data(HTAB *, BUFHEAD *, int, int);
 
 
/*
/*
 * Big_insert
 * Big_insert
 *
 *
 * You need to do an insert and the key/data pair is too big
 * You need to do an insert and the key/data pair is too big
 *
 *
 * Returns:
 * Returns:
 * 0 ==> OK
 * 0 ==> OK
 *-1 ==> ERROR
 *-1 ==> ERROR
 */
 */
extern int
extern int
__big_insert(hashp, bufp, key, val)
__big_insert(hashp, bufp, key, val)
        HTAB *hashp;
        HTAB *hashp;
        BUFHEAD *bufp;
        BUFHEAD *bufp;
        const DBT *key, *val;
        const DBT *key, *val;
{
{
        __uint16_t *p;
        __uint16_t *p;
        int key_size, n, val_size;
        int key_size, n, val_size;
        __uint16_t space, move_bytes, off;
        __uint16_t space, move_bytes, off;
        char *cp, *key_data, *val_data;
        char *cp, *key_data, *val_data;
 
 
        cp = bufp->page;                /* Character pointer of p. */
        cp = bufp->page;                /* Character pointer of p. */
        p = (__uint16_t *)cp;
        p = (__uint16_t *)cp;
 
 
        key_data = (char *)key->data;
        key_data = (char *)key->data;
        key_size = key->size;
        key_size = key->size;
        val_data = (char *)val->data;
        val_data = (char *)val->data;
        val_size = val->size;
        val_size = val->size;
 
 
        /* First move the Key */
        /* First move the Key */
        for (space = FREESPACE(p) - BIGOVERHEAD; key_size;
        for (space = FREESPACE(p) - BIGOVERHEAD; key_size;
            space = FREESPACE(p) - BIGOVERHEAD) {
            space = FREESPACE(p) - BIGOVERHEAD) {
                move_bytes = MIN(space, key_size);
                move_bytes = MIN(space, key_size);
                off = OFFSET(p) - move_bytes;
                off = OFFSET(p) - move_bytes;
                memmove(cp + off, key_data, move_bytes);
                memmove(cp + off, key_data, move_bytes);
                key_size -= move_bytes;
                key_size -= move_bytes;
                key_data += move_bytes;
                key_data += move_bytes;
                n = p[0];
                n = p[0];
                p[++n] = off;
                p[++n] = off;
                p[0] = ++n;
                p[0] = ++n;
                FREESPACE(p) = off - PAGE_META(n);
                FREESPACE(p) = off - PAGE_META(n);
                OFFSET(p) = off;
                OFFSET(p) = off;
                p[n] = PARTIAL_KEY;
                p[n] = PARTIAL_KEY;
                bufp = __add_ovflpage(hashp, bufp);
                bufp = __add_ovflpage(hashp, bufp);
                if (!bufp)
                if (!bufp)
                        return (-1);
                        return (-1);
                n = p[0];
                n = p[0];
                if (!key_size)
                if (!key_size)
                        if (FREESPACE(p)) {
                        if (FREESPACE(p)) {
                                move_bytes = MIN(FREESPACE(p), val_size);
                                move_bytes = MIN(FREESPACE(p), val_size);
                                off = OFFSET(p) - move_bytes;
                                off = OFFSET(p) - move_bytes;
                                p[n] = off;
                                p[n] = off;
                                memmove(cp + off, val_data, move_bytes);
                                memmove(cp + off, val_data, move_bytes);
                                val_data += move_bytes;
                                val_data += move_bytes;
                                val_size -= move_bytes;
                                val_size -= move_bytes;
                                p[n - 2] = FULL_KEY_DATA;
                                p[n - 2] = FULL_KEY_DATA;
                                FREESPACE(p) = FREESPACE(p) - move_bytes;
                                FREESPACE(p) = FREESPACE(p) - move_bytes;
                                OFFSET(p) = off;
                                OFFSET(p) = off;
                        } else
                        } else
                                p[n - 2] = FULL_KEY;
                                p[n - 2] = FULL_KEY;
                p = (__uint16_t *)bufp->page;
                p = (__uint16_t *)bufp->page;
                cp = bufp->page;
                cp = bufp->page;
                bufp->flags |= BUF_MOD;
                bufp->flags |= BUF_MOD;
        }
        }
 
 
        /* Now move the data */
        /* Now move the data */
        for (space = FREESPACE(p) - BIGOVERHEAD; val_size;
        for (space = FREESPACE(p) - BIGOVERHEAD; val_size;
            space = FREESPACE(p) - BIGOVERHEAD) {
            space = FREESPACE(p) - BIGOVERHEAD) {
                move_bytes = MIN(space, val_size);
                move_bytes = MIN(space, val_size);
                /*
                /*
                 * Here's the hack to make sure that if the data ends on the
                 * Here's the hack to make sure that if the data ends on the
                 * same page as the key ends, FREESPACE is at least one.
                 * same page as the key ends, FREESPACE is at least one.
                 */
                 */
                if (space == val_size && val_size == val->size)
                if (space == val_size && val_size == val->size)
                        move_bytes--;
                        move_bytes--;
                off = OFFSET(p) - move_bytes;
                off = OFFSET(p) - move_bytes;
                memmove(cp + off, val_data, move_bytes);
                memmove(cp + off, val_data, move_bytes);
                val_size -= move_bytes;
                val_size -= move_bytes;
                val_data += move_bytes;
                val_data += move_bytes;
                n = p[0];
                n = p[0];
                p[++n] = off;
                p[++n] = off;
                p[0] = ++n;
                p[0] = ++n;
                FREESPACE(p) = off - PAGE_META(n);
                FREESPACE(p) = off - PAGE_META(n);
                OFFSET(p) = off;
                OFFSET(p) = off;
                if (val_size) {
                if (val_size) {
                        p[n] = FULL_KEY;
                        p[n] = FULL_KEY;
                        bufp = __add_ovflpage(hashp, bufp);
                        bufp = __add_ovflpage(hashp, bufp);
                        if (!bufp)
                        if (!bufp)
                                return (-1);
                                return (-1);
                        cp = bufp->page;
                        cp = bufp->page;
                        p = (__uint16_t *)cp;
                        p = (__uint16_t *)cp;
                } else
                } else
                        p[n] = FULL_KEY_DATA;
                        p[n] = FULL_KEY_DATA;
                bufp->flags |= BUF_MOD;
                bufp->flags |= BUF_MOD;
        }
        }
        return (0);
        return (0);
}
}
 
 
/*
/*
 * Called when bufp's page  contains a partial key (index should be 1)
 * Called when bufp's page  contains a partial key (index should be 1)
 *
 *
 * All pages in the big key/data pair except bufp are freed.  We cannot
 * All pages in the big key/data pair except bufp are freed.  We cannot
 * free bufp because the page pointing to it is lost and we can't get rid
 * free bufp because the page pointing to it is lost and we can't get rid
 * of its pointer.
 * of its pointer.
 *
 *
 * Returns:
 * Returns:
 * 0 => OK
 * 0 => OK
 *-1 => ERROR
 *-1 => ERROR
 */
 */
extern int
extern int
__big_delete(hashp, bufp)
__big_delete(hashp, bufp)
        HTAB *hashp;
        HTAB *hashp;
        BUFHEAD *bufp;
        BUFHEAD *bufp;
{
{
        BUFHEAD *last_bfp, *rbufp;
        BUFHEAD *last_bfp, *rbufp;
        __uint16_t *bp, pageno;
        __uint16_t *bp, pageno;
        int key_done, n;
        int key_done, n;
 
 
        rbufp = bufp;
        rbufp = bufp;
        last_bfp = NULL;
        last_bfp = NULL;
        bp = (__uint16_t *)bufp->page;
        bp = (__uint16_t *)bufp->page;
        pageno = 0;
        pageno = 0;
        key_done = 0;
        key_done = 0;
 
 
        while (!key_done || (bp[2] != FULL_KEY_DATA)) {
        while (!key_done || (bp[2] != FULL_KEY_DATA)) {
                if (bp[2] == FULL_KEY || bp[2] == FULL_KEY_DATA)
                if (bp[2] == FULL_KEY || bp[2] == FULL_KEY_DATA)
                        key_done = 1;
                        key_done = 1;
 
 
                /*
                /*
                 * If there is freespace left on a FULL_KEY_DATA page, then
                 * If there is freespace left on a FULL_KEY_DATA page, then
                 * the data is short and fits entirely on this page, and this
                 * the data is short and fits entirely on this page, and this
                 * is the last page.
                 * is the last page.
                 */
                 */
                if (bp[2] == FULL_KEY_DATA && FREESPACE(bp))
                if (bp[2] == FULL_KEY_DATA && FREESPACE(bp))
                        break;
                        break;
                pageno = bp[bp[0] - 1];
                pageno = bp[bp[0] - 1];
                rbufp->flags |= BUF_MOD;
                rbufp->flags |= BUF_MOD;
                rbufp = __get_buf(hashp, pageno, rbufp, 0);
                rbufp = __get_buf(hashp, pageno, rbufp, 0);
                if (last_bfp)
                if (last_bfp)
                        __free_ovflpage(hashp, last_bfp);
                        __free_ovflpage(hashp, last_bfp);
                last_bfp = rbufp;
                last_bfp = rbufp;
                if (!rbufp)
                if (!rbufp)
                        return (-1);            /* Error. */
                        return (-1);            /* Error. */
                bp = (__uint16_t *)rbufp->page;
                bp = (__uint16_t *)rbufp->page;
        }
        }
 
 
        /*
        /*
         * If we get here then rbufp points to the last page of the big
         * If we get here then rbufp points to the last page of the big
         * key/data pair.  Bufp points to the first one -- it should now be
         * key/data pair.  Bufp points to the first one -- it should now be
         * empty pointing to the next page after this pair.  Can't free it
         * empty pointing to the next page after this pair.  Can't free it
         * because we don't have the page pointing to it.
         * because we don't have the page pointing to it.
         */
         */
 
 
        /* This is information from the last page of the pair. */
        /* This is information from the last page of the pair. */
        n = bp[0];
        n = bp[0];
        pageno = bp[n - 1];
        pageno = bp[n - 1];
 
 
        /* Now, bp is the first page of the pair. */
        /* Now, bp is the first page of the pair. */
        bp = (__uint16_t *)bufp->page;
        bp = (__uint16_t *)bufp->page;
        if (n > 2) {
        if (n > 2) {
                /* There is an overflow page. */
                /* There is an overflow page. */
                bp[1] = pageno;
                bp[1] = pageno;
                bp[2] = OVFLPAGE;
                bp[2] = OVFLPAGE;
                bufp->ovfl = rbufp->ovfl;
                bufp->ovfl = rbufp->ovfl;
        } else
        } else
                /* This is the last page. */
                /* This is the last page. */
                bufp->ovfl = NULL;
                bufp->ovfl = NULL;
        n -= 2;
        n -= 2;
        bp[0] = n;
        bp[0] = n;
        FREESPACE(bp) = hashp->BSIZE - PAGE_META(n);
        FREESPACE(bp) = hashp->BSIZE - PAGE_META(n);
        OFFSET(bp) = hashp->BSIZE - 1;
        OFFSET(bp) = hashp->BSIZE - 1;
 
 
        bufp->flags |= BUF_MOD;
        bufp->flags |= BUF_MOD;
        if (rbufp)
        if (rbufp)
                __free_ovflpage(hashp, rbufp);
                __free_ovflpage(hashp, rbufp);
        if (last_bfp != rbufp)
        if (last_bfp != rbufp)
                __free_ovflpage(hashp, last_bfp);
                __free_ovflpage(hashp, last_bfp);
 
 
        hashp->NKEYS--;
        hashp->NKEYS--;
        return (0);
        return (0);
}
}
/*
/*
 * Returns:
 * Returns:
 *  0 = key not found
 *  0 = key not found
 * -1 = get next overflow page
 * -1 = get next overflow page
 * -2 means key not found and this is big key/data
 * -2 means key not found and this is big key/data
 * -3 error
 * -3 error
 */
 */
extern int
extern int
__find_bigpair(hashp, bufp, ndx, key, size)
__find_bigpair(hashp, bufp, ndx, key, size)
        HTAB *hashp;
        HTAB *hashp;
        BUFHEAD *bufp;
        BUFHEAD *bufp;
        int ndx;
        int ndx;
        char *key;
        char *key;
        int size;
        int size;
{
{
        __uint16_t *bp;
        __uint16_t *bp;
        char *p;
        char *p;
        int ksize;
        int ksize;
        __uint16_t bytes;
        __uint16_t bytes;
        char *kkey;
        char *kkey;
 
 
        bp = (__uint16_t *)bufp->page;
        bp = (__uint16_t *)bufp->page;
        p = bufp->page;
        p = bufp->page;
        ksize = size;
        ksize = size;
        kkey = key;
        kkey = key;
 
 
        for (bytes = hashp->BSIZE - bp[ndx];
        for (bytes = hashp->BSIZE - bp[ndx];
            bytes <= size && bp[ndx + 1] == PARTIAL_KEY;
            bytes <= size && bp[ndx + 1] == PARTIAL_KEY;
            bytes = hashp->BSIZE - bp[ndx]) {
            bytes = hashp->BSIZE - bp[ndx]) {
                if (memcmp(p + bp[ndx], kkey, bytes))
                if (memcmp(p + bp[ndx], kkey, bytes))
                        return (-2);
                        return (-2);
                kkey += bytes;
                kkey += bytes;
                ksize -= bytes;
                ksize -= bytes;
                bufp = __get_buf(hashp, bp[ndx + 2], bufp, 0);
                bufp = __get_buf(hashp, bp[ndx + 2], bufp, 0);
                if (!bufp)
                if (!bufp)
                        return (-3);
                        return (-3);
                p = bufp->page;
                p = bufp->page;
                bp = (__uint16_t *)p;
                bp = (__uint16_t *)p;
                ndx = 1;
                ndx = 1;
        }
        }
 
 
        if (bytes != ksize || memcmp(p + bp[ndx], kkey, bytes)) {
        if (bytes != ksize || memcmp(p + bp[ndx], kkey, bytes)) {
#ifdef HASH_STATISTICS
#ifdef HASH_STATISTICS
                ++hash_collisions;
                ++hash_collisions;
#endif
#endif
                return (-2);
                return (-2);
        } else
        } else
                return (ndx);
                return (ndx);
}
}
 
 
/*
/*
 * Given the buffer pointer of the first overflow page of a big pair,
 * Given the buffer pointer of the first overflow page of a big pair,
 * find the end of the big pair
 * find the end of the big pair
 *
 *
 * This will set bpp to the buffer header of the last page of the big pair.
 * This will set bpp to the buffer header of the last page of the big pair.
 * It will return the pageno of the overflow page following the last page
 * It will return the pageno of the overflow page following the last page
 * of the pair; 0 if there isn't any (i.e. big pair is the last key in the
 * of the pair; 0 if there isn't any (i.e. big pair is the last key in the
 * bucket)
 * bucket)
 */
 */
extern __uint16_t
extern __uint16_t
__find_last_page(hashp, bpp)
__find_last_page(hashp, bpp)
        HTAB *hashp;
        HTAB *hashp;
        BUFHEAD **bpp;
        BUFHEAD **bpp;
{
{
        BUFHEAD *bufp;
        BUFHEAD *bufp;
        __uint16_t *bp, pageno;
        __uint16_t *bp, pageno;
        int n;
        int n;
 
 
        bufp = *bpp;
        bufp = *bpp;
        bp = (__uint16_t *)bufp->page;
        bp = (__uint16_t *)bufp->page;
        for (;;) {
        for (;;) {
                n = bp[0];
                n = bp[0];
 
 
                /*
                /*
                 * This is the last page if: the tag is FULL_KEY_DATA and
                 * This is the last page if: the tag is FULL_KEY_DATA and
                 * either only 2 entries OVFLPAGE marker is explicit there
                 * either only 2 entries OVFLPAGE marker is explicit there
                 * is freespace on the page.
                 * is freespace on the page.
                 */
                 */
                if (bp[2] == FULL_KEY_DATA &&
                if (bp[2] == FULL_KEY_DATA &&
                    ((n == 2) || (bp[n] == OVFLPAGE) || (FREESPACE(bp))))
                    ((n == 2) || (bp[n] == OVFLPAGE) || (FREESPACE(bp))))
                        break;
                        break;
 
 
                pageno = bp[n - 1];
                pageno = bp[n - 1];
                bufp = __get_buf(hashp, pageno, bufp, 0);
                bufp = __get_buf(hashp, pageno, bufp, 0);
                if (!bufp)
                if (!bufp)
                        return (0);      /* Need to indicate an error! */
                        return (0);      /* Need to indicate an error! */
                bp = (__uint16_t *)bufp->page;
                bp = (__uint16_t *)bufp->page;
        }
        }
 
 
        *bpp = bufp;
        *bpp = bufp;
        if (bp[0] > 2)
        if (bp[0] > 2)
                return (bp[3]);
                return (bp[3]);
        else
        else
                return (0);
                return (0);
}
}
 
 
/*
/*
 * Return the data for the key/data pair that begins on this page at this
 * Return the data for the key/data pair that begins on this page at this
 * index (index should always be 1).
 * index (index should always be 1).
 */
 */
extern int
extern int
__big_return(hashp, bufp, ndx, val, set_current)
__big_return(hashp, bufp, ndx, val, set_current)
        HTAB *hashp;
        HTAB *hashp;
        BUFHEAD *bufp;
        BUFHEAD *bufp;
        int ndx;
        int ndx;
        DBT *val;
        DBT *val;
        int set_current;
        int set_current;
{
{
        BUFHEAD *save_p;
        BUFHEAD *save_p;
        __uint16_t *bp, len, off, save_addr;
        __uint16_t *bp, len, off, save_addr;
        char *tp;
        char *tp;
 
 
        bp = (__uint16_t *)bufp->page;
        bp = (__uint16_t *)bufp->page;
        while (bp[ndx + 1] == PARTIAL_KEY) {
        while (bp[ndx + 1] == PARTIAL_KEY) {
                bufp = __get_buf(hashp, bp[bp[0] - 1], bufp, 0);
                bufp = __get_buf(hashp, bp[bp[0] - 1], bufp, 0);
                if (!bufp)
                if (!bufp)
                        return (-1);
                        return (-1);
                bp = (__uint16_t *)bufp->page;
                bp = (__uint16_t *)bufp->page;
                ndx = 1;
                ndx = 1;
        }
        }
 
 
        if (bp[ndx + 1] == FULL_KEY) {
        if (bp[ndx + 1] == FULL_KEY) {
                bufp = __get_buf(hashp, bp[bp[0] - 1], bufp, 0);
                bufp = __get_buf(hashp, bp[bp[0] - 1], bufp, 0);
                if (!bufp)
                if (!bufp)
                        return (-1);
                        return (-1);
                bp = (__uint16_t *)bufp->page;
                bp = (__uint16_t *)bufp->page;
                save_p = bufp;
                save_p = bufp;
                save_addr = save_p->addr;
                save_addr = save_p->addr;
                off = bp[1];
                off = bp[1];
                len = 0;
                len = 0;
        } else
        } else
                if (!FREESPACE(bp)) {
                if (!FREESPACE(bp)) {
                        /*
                        /*
                         * This is a hack.  We can't distinguish between
                         * This is a hack.  We can't distinguish between
                         * FULL_KEY_DATA that contains complete data or
                         * FULL_KEY_DATA that contains complete data or
                         * incomplete data, so we require that if the data
                         * incomplete data, so we require that if the data
                         * is complete, there is at least 1 byte of free
                         * is complete, there is at least 1 byte of free
                         * space left.
                         * space left.
                         */
                         */
                        off = bp[bp[0]];
                        off = bp[bp[0]];
                        len = bp[1] - off;
                        len = bp[1] - off;
                        save_p = bufp;
                        save_p = bufp;
                        save_addr = bufp->addr;
                        save_addr = bufp->addr;
                        bufp = __get_buf(hashp, bp[bp[0] - 1], bufp, 0);
                        bufp = __get_buf(hashp, bp[bp[0] - 1], bufp, 0);
                        if (!bufp)
                        if (!bufp)
                                return (-1);
                                return (-1);
                        bp = (__uint16_t *)bufp->page;
                        bp = (__uint16_t *)bufp->page;
                } else {
                } else {
                        /* The data is all on one page. */
                        /* The data is all on one page. */
                        tp = (char *)bp;
                        tp = (char *)bp;
                        off = bp[bp[0]];
                        off = bp[bp[0]];
                        val->data = (u_char *)tp + off;
                        val->data = (u_char *)tp + off;
                        val->size = bp[1] - off;
                        val->size = bp[1] - off;
                        if (set_current) {
                        if (set_current) {
                                if (bp[0] == 2) {        /* No more buckets in
                                if (bp[0] == 2) {        /* No more buckets in
                                                         * chain */
                                                         * chain */
                                        hashp->cpage = NULL;
                                        hashp->cpage = NULL;
                                        hashp->cbucket++;
                                        hashp->cbucket++;
                                        hashp->cndx = 1;
                                        hashp->cndx = 1;
                                } else {
                                } else {
                                        hashp->cpage = __get_buf(hashp,
                                        hashp->cpage = __get_buf(hashp,
                                            bp[bp[0] - 1], bufp, 0);
                                            bp[bp[0] - 1], bufp, 0);
                                        if (!hashp->cpage)
                                        if (!hashp->cpage)
                                                return (-1);
                                                return (-1);
                                        hashp->cndx = 1;
                                        hashp->cndx = 1;
                                        if (!((__uint16_t *)
                                        if (!((__uint16_t *)
                                            hashp->cpage->page)[0]) {
                                            hashp->cpage->page)[0]) {
                                                hashp->cbucket++;
                                                hashp->cbucket++;
                                                hashp->cpage = NULL;
                                                hashp->cpage = NULL;
                                        }
                                        }
                                }
                                }
                        }
                        }
                        return (0);
                        return (0);
                }
                }
 
 
        val->size = collect_data(hashp, bufp, (int)len, set_current);
        val->size = collect_data(hashp, bufp, (int)len, set_current);
        if (val->size == -1)
        if (val->size == -1)
                return (-1);
                return (-1);
        if (save_p->addr != save_addr) {
        if (save_p->addr != save_addr) {
                /* We are pretty short on buffers. */
                /* We are pretty short on buffers. */
                errno = EINVAL;                 /* OUT OF BUFFERS */
                errno = EINVAL;                 /* OUT OF BUFFERS */
                return (-1);
                return (-1);
        }
        }
        memmove(hashp->tmp_buf, (save_p->page) + off, len);
        memmove(hashp->tmp_buf, (save_p->page) + off, len);
        val->data = (u_char *)hashp->tmp_buf;
        val->data = (u_char *)hashp->tmp_buf;
        return (0);
        return (0);
}
}
/*
/*
 * Count how big the total datasize is by recursing through the pages.  Then
 * Count how big the total datasize is by recursing through the pages.  Then
 * allocate a buffer and copy the data as you recurse up.
 * allocate a buffer and copy the data as you recurse up.
 */
 */
static int
static int
collect_data(hashp, bufp, len, set)
collect_data(hashp, bufp, len, set)
        HTAB *hashp;
        HTAB *hashp;
        BUFHEAD *bufp;
        BUFHEAD *bufp;
        int len, set;
        int len, set;
{
{
        __uint16_t *bp;
        __uint16_t *bp;
        char *p;
        char *p;
        BUFHEAD *xbp;
        BUFHEAD *xbp;
        __uint16_t save_addr;
        __uint16_t save_addr;
        int mylen, totlen;
        int mylen, totlen;
 
 
        p = bufp->page;
        p = bufp->page;
        bp = (__uint16_t *)p;
        bp = (__uint16_t *)p;
        mylen = hashp->BSIZE - bp[1];
        mylen = hashp->BSIZE - bp[1];
        save_addr = bufp->addr;
        save_addr = bufp->addr;
 
 
        if (bp[2] == FULL_KEY_DATA) {           /* End of Data */
        if (bp[2] == FULL_KEY_DATA) {           /* End of Data */
                totlen = len + mylen;
                totlen = len + mylen;
                if (hashp->tmp_buf)
                if (hashp->tmp_buf)
                        free(hashp->tmp_buf);
                        free(hashp->tmp_buf);
                if ((hashp->tmp_buf = (char *)malloc(totlen)) == NULL)
                if ((hashp->tmp_buf = (char *)malloc(totlen)) == NULL)
                        return (-1);
                        return (-1);
                if (set) {
                if (set) {
                        hashp->cndx = 1;
                        hashp->cndx = 1;
                        if (bp[0] == 2) {        /* No more buckets in chain */
                        if (bp[0] == 2) {        /* No more buckets in chain */
                                hashp->cpage = NULL;
                                hashp->cpage = NULL;
                                hashp->cbucket++;
                                hashp->cbucket++;
                        } else {
                        } else {
                                hashp->cpage =
                                hashp->cpage =
                                    __get_buf(hashp, bp[bp[0] - 1], bufp, 0);
                                    __get_buf(hashp, bp[bp[0] - 1], bufp, 0);
                                if (!hashp->cpage)
                                if (!hashp->cpage)
                                        return (-1);
                                        return (-1);
                                else if (!((__uint16_t *)hashp->cpage->page)[0]) {
                                else if (!((__uint16_t *)hashp->cpage->page)[0]) {
                                        hashp->cbucket++;
                                        hashp->cbucket++;
                                        hashp->cpage = NULL;
                                        hashp->cpage = NULL;
                                }
                                }
                        }
                        }
                }
                }
        } else {
        } else {
                xbp = __get_buf(hashp, bp[bp[0] - 1], bufp, 0);
                xbp = __get_buf(hashp, bp[bp[0] - 1], bufp, 0);
                if (!xbp || ((totlen =
                if (!xbp || ((totlen =
                    collect_data(hashp, xbp, len + mylen, set)) < 1))
                    collect_data(hashp, xbp, len + mylen, set)) < 1))
                        return (-1);
                        return (-1);
        }
        }
        if (bufp->addr != save_addr) {
        if (bufp->addr != save_addr) {
                errno = EINVAL;                 /* Out of buffers. */
                errno = EINVAL;                 /* Out of buffers. */
                return (-1);
                return (-1);
        }
        }
        memmove(&hashp->tmp_buf[len], (bufp->page) + bp[1], mylen);
        memmove(&hashp->tmp_buf[len], (bufp->page) + bp[1], mylen);
        return (totlen);
        return (totlen);
}
}
 
 
/*
/*
 * Fill in the key and data for this big pair.
 * Fill in the key and data for this big pair.
 */
 */
extern int
extern int
__big_keydata(hashp, bufp, key, val, set)
__big_keydata(hashp, bufp, key, val, set)
        HTAB *hashp;
        HTAB *hashp;
        BUFHEAD *bufp;
        BUFHEAD *bufp;
        DBT *key, *val;
        DBT *key, *val;
        int set;
        int set;
{
{
        key->size = collect_key(hashp, bufp, 0, val, set);
        key->size = collect_key(hashp, bufp, 0, val, set);
        if (key->size == -1)
        if (key->size == -1)
                return (-1);
                return (-1);
        key->data = (u_char *)hashp->tmp_key;
        key->data = (u_char *)hashp->tmp_key;
        return (0);
        return (0);
}
}
 
 
/*
/*
 * Count how big the total key size is by recursing through the pages.  Then
 * Count how big the total key size is by recursing through the pages.  Then
 * collect the data, allocate a buffer and copy the key as you recurse up.
 * collect the data, allocate a buffer and copy the key as you recurse up.
 */
 */
static int
static int
collect_key(hashp, bufp, len, val, set)
collect_key(hashp, bufp, len, val, set)
        HTAB *hashp;
        HTAB *hashp;
        BUFHEAD *bufp;
        BUFHEAD *bufp;
        int len;
        int len;
        DBT *val;
        DBT *val;
        int set;
        int set;
{
{
        BUFHEAD *xbp;
        BUFHEAD *xbp;
        char *p;
        char *p;
        int mylen, totlen;
        int mylen, totlen;
        __uint16_t *bp, save_addr;
        __uint16_t *bp, save_addr;
 
 
        p = bufp->page;
        p = bufp->page;
        bp = (__uint16_t *)p;
        bp = (__uint16_t *)p;
        mylen = hashp->BSIZE - bp[1];
        mylen = hashp->BSIZE - bp[1];
 
 
        save_addr = bufp->addr;
        save_addr = bufp->addr;
        totlen = len + mylen;
        totlen = len + mylen;
        if (bp[2] == FULL_KEY || bp[2] == FULL_KEY_DATA) {    /* End of Key. */
        if (bp[2] == FULL_KEY || bp[2] == FULL_KEY_DATA) {    /* End of Key. */
                if (hashp->tmp_key != NULL)
                if (hashp->tmp_key != NULL)
                        free(hashp->tmp_key);
                        free(hashp->tmp_key);
                if ((hashp->tmp_key = (char *)malloc(totlen)) == NULL)
                if ((hashp->tmp_key = (char *)malloc(totlen)) == NULL)
                        return (-1);
                        return (-1);
                if (__big_return(hashp, bufp, 1, val, set))
                if (__big_return(hashp, bufp, 1, val, set))
                        return (-1);
                        return (-1);
        } else {
        } else {
                xbp = __get_buf(hashp, bp[bp[0] - 1], bufp, 0);
                xbp = __get_buf(hashp, bp[bp[0] - 1], bufp, 0);
                if (!xbp || ((totlen =
                if (!xbp || ((totlen =
                    collect_key(hashp, xbp, totlen, val, set)) < 1))
                    collect_key(hashp, xbp, totlen, val, set)) < 1))
                        return (-1);
                        return (-1);
        }
        }
        if (bufp->addr != save_addr) {
        if (bufp->addr != save_addr) {
                errno = EINVAL;         /* MIS -- OUT OF BUFFERS */
                errno = EINVAL;         /* MIS -- OUT OF BUFFERS */
                return (-1);
                return (-1);
        }
        }
        memmove(&hashp->tmp_key[len], (bufp->page) + bp[1], mylen);
        memmove(&hashp->tmp_key[len], (bufp->page) + bp[1], mylen);
        return (totlen);
        return (totlen);
}
}
 
 
/*
/*
 * Returns:
 * Returns:
 *  0 => OK
 *  0 => OK
 * -1 => error
 * -1 => error
 */
 */
extern int
extern int
__big_split(hashp, op, np, big_keyp, addr, obucket, ret)
__big_split(hashp, op, np, big_keyp, addr, obucket, ret)
        HTAB *hashp;
        HTAB *hashp;
        BUFHEAD *op;    /* Pointer to where to put keys that go in old bucket */
        BUFHEAD *op;    /* Pointer to where to put keys that go in old bucket */
        BUFHEAD *np;    /* Pointer to new bucket page */
        BUFHEAD *np;    /* Pointer to new bucket page */
                        /* Pointer to first page containing the big key/data */
                        /* Pointer to first page containing the big key/data */
        BUFHEAD *big_keyp;
        BUFHEAD *big_keyp;
        int addr;       /* Address of big_keyp */
        int addr;       /* Address of big_keyp */
        __uint32_t   obucket;/* Old Bucket */
        __uint32_t   obucket;/* Old Bucket */
        SPLIT_RETURN *ret;
        SPLIT_RETURN *ret;
{
{
        BUFHEAD *tmpp;
        BUFHEAD *tmpp;
        __uint16_t *tp;
        __uint16_t *tp;
        BUFHEAD *bp;
        BUFHEAD *bp;
        DBT key, val;
        DBT key, val;
        __uint32_t change;
        __uint32_t change;
        __uint16_t free_space, n, off;
        __uint16_t free_space, n, off;
 
 
        bp = big_keyp;
        bp = big_keyp;
 
 
        /* Now figure out where the big key/data goes */
        /* Now figure out where the big key/data goes */
        if (__big_keydata(hashp, big_keyp, &key, &val, 0))
        if (__big_keydata(hashp, big_keyp, &key, &val, 0))
                return (-1);
                return (-1);
        change = (__call_hash(hashp, key.data, key.size) != obucket);
        change = (__call_hash(hashp, key.data, key.size) != obucket);
 
 
        if ( (ret->next_addr = __find_last_page(hashp, &big_keyp)) ) {
        if ( (ret->next_addr = __find_last_page(hashp, &big_keyp)) ) {
                if (!(ret->nextp =
                if (!(ret->nextp =
                    __get_buf(hashp, ret->next_addr, big_keyp, 0)))
                    __get_buf(hashp, ret->next_addr, big_keyp, 0)))
                        return (-1);;
                        return (-1);;
        } else
        } else
                ret->nextp = NULL;
                ret->nextp = NULL;
 
 
        /* Now make one of np/op point to the big key/data pair */
        /* Now make one of np/op point to the big key/data pair */
#ifdef DEBUG
#ifdef DEBUG
        assert(np->ovfl == NULL);
        assert(np->ovfl == NULL);
#endif
#endif
        if (change)
        if (change)
                tmpp = np;
                tmpp = np;
        else
        else
                tmpp = op;
                tmpp = op;
 
 
        tmpp->flags |= BUF_MOD;
        tmpp->flags |= BUF_MOD;
#ifdef DEBUG1
#ifdef DEBUG1
        (void)fprintf(stderr,
        (void)fprintf(stderr,
            "BIG_SPLIT: %d->ovfl was %d is now %d\n", tmpp->addr,
            "BIG_SPLIT: %d->ovfl was %d is now %d\n", tmpp->addr,
            (tmpp->ovfl ? tmpp->ovfl->addr : 0), (bp ? bp->addr : 0));
            (tmpp->ovfl ? tmpp->ovfl->addr : 0), (bp ? bp->addr : 0));
#endif
#endif
        tmpp->ovfl = bp;        /* one of op/np point to big_keyp */
        tmpp->ovfl = bp;        /* one of op/np point to big_keyp */
        tp = (__uint16_t *)tmpp->page;
        tp = (__uint16_t *)tmpp->page;
#ifdef DEBUG
#ifdef DEBUG
        assert(FREESPACE(tp) >= OVFLSIZE);
        assert(FREESPACE(tp) >= OVFLSIZE);
#endif
#endif
        n = tp[0];
        n = tp[0];
        off = OFFSET(tp);
        off = OFFSET(tp);
        free_space = FREESPACE(tp);
        free_space = FREESPACE(tp);
        tp[++n] = (__uint16_t)addr;
        tp[++n] = (__uint16_t)addr;
        tp[++n] = OVFLPAGE;
        tp[++n] = OVFLPAGE;
        tp[0] = n;
        tp[0] = n;
        OFFSET(tp) = off;
        OFFSET(tp) = off;
        FREESPACE(tp) = free_space - OVFLSIZE;
        FREESPACE(tp) = free_space - OVFLSIZE;
 
 
        /*
        /*
         * Finally, set the new and old return values. BIG_KEYP contains a
         * Finally, set the new and old return values. BIG_KEYP contains a
         * pointer to the last page of the big key_data pair. Make sure that
         * pointer to the last page of the big key_data pair. Make sure that
         * big_keyp has no following page (2 elements) or create an empty
         * big_keyp has no following page (2 elements) or create an empty
         * following page.
         * following page.
         */
         */
 
 
        ret->newp = np;
        ret->newp = np;
        ret->oldp = op;
        ret->oldp = op;
 
 
        tp = (__uint16_t *)big_keyp->page;
        tp = (__uint16_t *)big_keyp->page;
        big_keyp->flags |= BUF_MOD;
        big_keyp->flags |= BUF_MOD;
        if (tp[0] > 2) {
        if (tp[0] > 2) {
                /*
                /*
                 * There may be either one or two offsets on this page.  If
                 * There may be either one or two offsets on this page.  If
                 * there is one, then the overflow page is linked on normally
                 * there is one, then the overflow page is linked on normally
                 * and tp[4] is OVFLPAGE.  If there are two, tp[4] contains
                 * and tp[4] is OVFLPAGE.  If there are two, tp[4] contains
                 * the second offset and needs to get stuffed in after the
                 * the second offset and needs to get stuffed in after the
                 * next overflow page is added.
                 * next overflow page is added.
                 */
                 */
                n = tp[4];
                n = tp[4];
                free_space = FREESPACE(tp);
                free_space = FREESPACE(tp);
                off = OFFSET(tp);
                off = OFFSET(tp);
                tp[0] -= 2;
                tp[0] -= 2;
                FREESPACE(tp) = free_space + OVFLSIZE;
                FREESPACE(tp) = free_space + OVFLSIZE;
                OFFSET(tp) = off;
                OFFSET(tp) = off;
                tmpp = __add_ovflpage(hashp, big_keyp);
                tmpp = __add_ovflpage(hashp, big_keyp);
                if (!tmpp)
                if (!tmpp)
                        return (-1);
                        return (-1);
                tp[4] = n;
                tp[4] = n;
        } else
        } else
                tmpp = big_keyp;
                tmpp = big_keyp;
 
 
        if (change)
        if (change)
                ret->newp = tmpp;
                ret->newp = tmpp;
        else
        else
                ret->oldp = tmpp;
                ret->oldp = tmpp;
        return (0);
        return (0);
}
}
 
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.