158f0484fSRodney W. Grimes /*- 2ef5d438eSPaul Traina * Copyright (c) 1990, 1993, 1994 358f0484fSRodney W. Grimes * The Regents of the University of California. All rights reserved. 458f0484fSRodney W. Grimes * 558f0484fSRodney W. Grimes * This code is derived from software contributed to Berkeley by 658f0484fSRodney W. Grimes * Mike Olson. 758f0484fSRodney W. Grimes * 858f0484fSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 958f0484fSRodney W. Grimes * modification, are permitted provided that the following conditions 1058f0484fSRodney W. Grimes * are met: 1158f0484fSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 1258f0484fSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 1358f0484fSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 1458f0484fSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 1558f0484fSRodney W. Grimes * documentation and/or other materials provided with the distribution. 1658f0484fSRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 1758f0484fSRodney W. Grimes * must display the following acknowledgement: 1858f0484fSRodney W. Grimes * This product includes software developed by the University of 1958f0484fSRodney W. Grimes * California, Berkeley and its contributors. 2058f0484fSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 2158f0484fSRodney W. Grimes * may be used to endorse or promote products derived from this software 2258f0484fSRodney W. Grimes * without specific prior written permission. 2358f0484fSRodney W. Grimes * 2458f0484fSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2558f0484fSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2658f0484fSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2758f0484fSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2858f0484fSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2958f0484fSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 3058f0484fSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 3158f0484fSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 3258f0484fSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3358f0484fSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3458f0484fSRodney W. Grimes * SUCH DAMAGE. 3558f0484fSRodney W. Grimes */ 3658f0484fSRodney W. Grimes 3758f0484fSRodney W. Grimes #if defined(LIBC_SCCS) && !defined(lint) 38ef5d438eSPaul Traina static char sccsid[] = "@(#)rec_delete.c 8.7 (Berkeley) 7/14/94"; 3958f0484fSRodney W. Grimes #endif /* LIBC_SCCS and not lint */ 4058f0484fSRodney W. Grimes 4158f0484fSRodney W. Grimes #include <sys/types.h> 4258f0484fSRodney W. Grimes 4358f0484fSRodney W. Grimes #include <errno.h> 4458f0484fSRodney W. Grimes #include <stdio.h> 4558f0484fSRodney W. Grimes #include <string.h> 4658f0484fSRodney W. Grimes 4758f0484fSRodney W. Grimes #include <db.h> 4858f0484fSRodney W. Grimes #include "recno.h" 4958f0484fSRodney W. Grimes 5058f0484fSRodney W. Grimes static int rec_rdelete __P((BTREE *, recno_t)); 5158f0484fSRodney W. Grimes 5258f0484fSRodney W. Grimes /* 5358f0484fSRodney W. Grimes * __REC_DELETE -- Delete the item(s) referenced by a key. 5458f0484fSRodney W. Grimes * 5558f0484fSRodney W. Grimes * Parameters: 5658f0484fSRodney W. Grimes * dbp: pointer to access method 5758f0484fSRodney W. Grimes * key: key to delete 5858f0484fSRodney W. Grimes * flags: R_CURSOR if deleting what the cursor references 5958f0484fSRodney W. Grimes * 6058f0484fSRodney W. Grimes * Returns: 6158f0484fSRodney W. Grimes * RET_ERROR, RET_SUCCESS and RET_SPECIAL if the key not found. 6258f0484fSRodney W. Grimes */ 6358f0484fSRodney W. Grimes int 6458f0484fSRodney W. Grimes __rec_delete(dbp, key, flags) 6558f0484fSRodney W. Grimes const DB *dbp; 6658f0484fSRodney W. Grimes const DBT *key; 6758f0484fSRodney W. Grimes u_int flags; 6858f0484fSRodney W. Grimes { 6958f0484fSRodney W. Grimes BTREE *t; 7058f0484fSRodney W. Grimes recno_t nrec; 7158f0484fSRodney W. Grimes int status; 7258f0484fSRodney W. Grimes 7358f0484fSRodney W. Grimes t = dbp->internal; 7458f0484fSRodney W. Grimes 7558f0484fSRodney W. Grimes /* Toss any page pinned across calls. */ 7658f0484fSRodney W. Grimes if (t->bt_pinned != NULL) { 7758f0484fSRodney W. Grimes mpool_put(t->bt_mp, t->bt_pinned, 0); 7858f0484fSRodney W. Grimes t->bt_pinned = NULL; 7958f0484fSRodney W. Grimes } 8058f0484fSRodney W. Grimes 8158f0484fSRodney W. Grimes switch(flags) { 8258f0484fSRodney W. Grimes case 0: 8358f0484fSRodney W. Grimes if ((nrec = *(recno_t *)key->data) == 0) 8458f0484fSRodney W. Grimes goto einval; 8558f0484fSRodney W. Grimes if (nrec > t->bt_nrecs) 8658f0484fSRodney W. Grimes return (RET_SPECIAL); 8758f0484fSRodney W. Grimes --nrec; 8858f0484fSRodney W. Grimes status = rec_rdelete(t, nrec); 8958f0484fSRodney W. Grimes break; 9058f0484fSRodney W. Grimes case R_CURSOR: 91ef5d438eSPaul Traina if (!F_ISSET(&t->bt_cursor, CURS_INIT)) 9258f0484fSRodney W. Grimes goto einval; 9358f0484fSRodney W. Grimes if (t->bt_nrecs == 0) 9458f0484fSRodney W. Grimes return (RET_SPECIAL); 95ef5d438eSPaul Traina status = rec_rdelete(t, t->bt_cursor.rcursor - 1); 9658f0484fSRodney W. Grimes if (status == RET_SUCCESS) 97ef5d438eSPaul Traina --t->bt_cursor.rcursor; 9858f0484fSRodney W. Grimes break; 9958f0484fSRodney W. Grimes default: 10058f0484fSRodney W. Grimes einval: errno = EINVAL; 10158f0484fSRodney W. Grimes return (RET_ERROR); 10258f0484fSRodney W. Grimes } 10358f0484fSRodney W. Grimes 10458f0484fSRodney W. Grimes if (status == RET_SUCCESS) 105ef5d438eSPaul Traina F_SET(t, B_MODIFIED | R_MODIFIED); 10658f0484fSRodney W. Grimes return (status); 10758f0484fSRodney W. Grimes } 10858f0484fSRodney W. Grimes 10958f0484fSRodney W. Grimes /* 11058f0484fSRodney W. Grimes * REC_RDELETE -- Delete the data matching the specified key. 11158f0484fSRodney W. Grimes * 11258f0484fSRodney W. Grimes * Parameters: 11358f0484fSRodney W. Grimes * tree: tree 11458f0484fSRodney W. Grimes * nrec: record to delete 11558f0484fSRodney W. Grimes * 11658f0484fSRodney W. Grimes * Returns: 11758f0484fSRodney W. Grimes * RET_ERROR, RET_SUCCESS and RET_SPECIAL if the key not found. 11858f0484fSRodney W. Grimes */ 11958f0484fSRodney W. Grimes static int 12058f0484fSRodney W. Grimes rec_rdelete(t, nrec) 12158f0484fSRodney W. Grimes BTREE *t; 12258f0484fSRodney W. Grimes recno_t nrec; 12358f0484fSRodney W. Grimes { 12458f0484fSRodney W. Grimes EPG *e; 12558f0484fSRodney W. Grimes PAGE *h; 12658f0484fSRodney W. Grimes int status; 12758f0484fSRodney W. Grimes 12858f0484fSRodney W. Grimes /* Find the record; __rec_search pins the page. */ 12958f0484fSRodney W. Grimes if ((e = __rec_search(t, nrec, SDELETE)) == NULL) 13058f0484fSRodney W. Grimes return (RET_ERROR); 13158f0484fSRodney W. Grimes 13258f0484fSRodney W. Grimes /* Delete the record. */ 13358f0484fSRodney W. Grimes h = e->page; 13458f0484fSRodney W. Grimes status = __rec_dleaf(t, h, e->index); 13558f0484fSRodney W. Grimes if (status != RET_SUCCESS) { 13658f0484fSRodney W. Grimes mpool_put(t->bt_mp, h, 0); 13758f0484fSRodney W. Grimes return (status); 13858f0484fSRodney W. Grimes } 13958f0484fSRodney W. Grimes mpool_put(t->bt_mp, h, MPOOL_DIRTY); 14058f0484fSRodney W. Grimes return (RET_SUCCESS); 14158f0484fSRodney W. Grimes } 14258f0484fSRodney W. Grimes 14358f0484fSRodney W. Grimes /* 14458f0484fSRodney W. Grimes * __REC_DLEAF -- Delete a single record from a recno leaf page. 14558f0484fSRodney W. Grimes * 14658f0484fSRodney W. Grimes * Parameters: 14758f0484fSRodney W. Grimes * t: tree 14858f0484fSRodney W. Grimes * index: index on current page to delete 14958f0484fSRodney W. Grimes * 15058f0484fSRodney W. Grimes * Returns: 15158f0484fSRodney W. Grimes * RET_SUCCESS, RET_ERROR. 15258f0484fSRodney W. Grimes */ 15358f0484fSRodney W. Grimes int 15458f0484fSRodney W. Grimes __rec_dleaf(t, h, index) 15558f0484fSRodney W. Grimes BTREE *t; 15658f0484fSRodney W. Grimes PAGE *h; 157ef5d438eSPaul Traina u_int32_t index; 15858f0484fSRodney W. Grimes { 159ef5d438eSPaul Traina RLEAF *rl; 160ef5d438eSPaul Traina indx_t *ip, cnt, offset; 161ef5d438eSPaul Traina u_int32_t nbytes; 16258f0484fSRodney W. Grimes char *from; 16358f0484fSRodney W. Grimes void *to; 16458f0484fSRodney W. Grimes 16558f0484fSRodney W. Grimes /* 16658f0484fSRodney W. Grimes * Delete a record from a recno leaf page. Internal records are never 16758f0484fSRodney W. Grimes * deleted from internal pages, regardless of the records that caused 16858f0484fSRodney W. Grimes * them to be added being deleted. Pages made empty by deletion are 16958f0484fSRodney W. Grimes * not reclaimed. They are, however, made available for reuse. 17058f0484fSRodney W. Grimes * 17158f0484fSRodney W. Grimes * Pack the remaining entries at the end of the page, shift the indices 17258f0484fSRodney W. Grimes * down, overwriting the deleted record and its index. If the record 17358f0484fSRodney W. Grimes * uses overflow pages, make them available for reuse. 17458f0484fSRodney W. Grimes */ 17558f0484fSRodney W. Grimes to = rl = GETRLEAF(h, index); 17658f0484fSRodney W. Grimes if (rl->flags & P_BIGDATA && __ovfl_delete(t, rl->bytes) == RET_ERROR) 17758f0484fSRodney W. Grimes return (RET_ERROR); 17858f0484fSRodney W. Grimes nbytes = NRLEAF(rl); 17958f0484fSRodney W. Grimes 18058f0484fSRodney W. Grimes /* 18158f0484fSRodney W. Grimes * Compress the key/data pairs. Compress and adjust the [BR]LEAF 18258f0484fSRodney W. Grimes * offsets. Reset the headers. 18358f0484fSRodney W. Grimes */ 18458f0484fSRodney W. Grimes from = (char *)h + h->upper; 18558f0484fSRodney W. Grimes memmove(from + nbytes, from, (char *)to - from); 18658f0484fSRodney W. Grimes h->upper += nbytes; 18758f0484fSRodney W. Grimes 18858f0484fSRodney W. Grimes offset = h->linp[index]; 18958f0484fSRodney W. Grimes for (cnt = &h->linp[index] - (ip = &h->linp[0]); cnt--; ++ip) 19058f0484fSRodney W. Grimes if (ip[0] < offset) 19158f0484fSRodney W. Grimes ip[0] += nbytes; 19258f0484fSRodney W. Grimes for (cnt = &h->linp[NEXTINDEX(h)] - ip; --cnt; ++ip) 19358f0484fSRodney W. Grimes ip[0] = ip[1] < offset ? ip[1] + nbytes : ip[1]; 19458f0484fSRodney W. Grimes h->lower -= sizeof(indx_t); 19558f0484fSRodney W. Grimes --t->bt_nrecs; 19658f0484fSRodney W. Grimes return (RET_SUCCESS); 19758f0484fSRodney W. Grimes } 198