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