xref: /freebsd/lib/libc/db/recno/rec_delete.c (revision 4c66e4b64b1055c1d43ace08b78f84597fc982bc)
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  * 4. Neither the name of the University nor the names of its contributors
1758f0484fSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
1858f0484fSRodney W. Grimes  *    without specific prior written permission.
1958f0484fSRodney W. Grimes  *
2058f0484fSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2158f0484fSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2258f0484fSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2358f0484fSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2458f0484fSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2558f0484fSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2658f0484fSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2758f0484fSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2858f0484fSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2958f0484fSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3058f0484fSRodney W. Grimes  * SUCH DAMAGE.
3158f0484fSRodney W. Grimes  */
3258f0484fSRodney W. Grimes 
3358f0484fSRodney W. Grimes #if defined(LIBC_SCCS) && !defined(lint)
34ef5d438eSPaul Traina static char sccsid[] = "@(#)rec_delete.c	8.7 (Berkeley) 7/14/94";
3558f0484fSRodney W. Grimes #endif /* LIBC_SCCS and not lint */
36c05ac53bSDavid E. O'Brien #include <sys/cdefs.h>
37c05ac53bSDavid E. O'Brien __FBSDID("$FreeBSD$");
3858f0484fSRodney W. Grimes 
3958f0484fSRodney W. Grimes #include <sys/types.h>
4058f0484fSRodney W. Grimes 
4158f0484fSRodney W. Grimes #include <errno.h>
4258f0484fSRodney W. Grimes #include <stdio.h>
4358f0484fSRodney W. Grimes #include <string.h>
4458f0484fSRodney W. Grimes 
4558f0484fSRodney W. Grimes #include <db.h>
4658f0484fSRodney W. Grimes #include "recno.h"
4758f0484fSRodney W. Grimes 
48c05ac53bSDavid E. O'Brien static int rec_rdelete(BTREE *, recno_t);
4958f0484fSRodney W. Grimes 
5058f0484fSRodney W. Grimes /*
5158f0484fSRodney W. Grimes  * __REC_DELETE -- Delete the item(s) referenced by a key.
5258f0484fSRodney W. Grimes  *
5358f0484fSRodney W. Grimes  * Parameters:
5458f0484fSRodney W. Grimes  *	dbp:	pointer to access method
5558f0484fSRodney W. Grimes  *	key:	key to delete
5658f0484fSRodney W. Grimes  *	flags:	R_CURSOR if deleting what the cursor references
5758f0484fSRodney W. Grimes  *
5858f0484fSRodney W. Grimes  * Returns:
5958f0484fSRodney W. Grimes  *	RET_ERROR, RET_SUCCESS and RET_SPECIAL if the key not found.
6058f0484fSRodney W. Grimes  */
6158f0484fSRodney W. Grimes int
620ac22237SXin LI __rec_delete(const DB *dbp, const DBT *key, u_int flags)
6358f0484fSRodney W. Grimes {
6458f0484fSRodney W. Grimes 	BTREE *t;
6558f0484fSRodney W. Grimes 	recno_t nrec;
6658f0484fSRodney W. Grimes 	int status;
6758f0484fSRodney W. Grimes 
6858f0484fSRodney W. Grimes 	t = dbp->internal;
6958f0484fSRodney W. Grimes 
7058f0484fSRodney W. Grimes 	/* Toss any page pinned across calls. */
7158f0484fSRodney W. Grimes 	if (t->bt_pinned != NULL) {
7258f0484fSRodney W. Grimes 		mpool_put(t->bt_mp, t->bt_pinned, 0);
7358f0484fSRodney W. Grimes 		t->bt_pinned = NULL;
7458f0484fSRodney W. Grimes 	}
7558f0484fSRodney W. Grimes 
7658f0484fSRodney W. Grimes 	switch(flags) {
7758f0484fSRodney W. Grimes 	case 0:
7858f0484fSRodney W. Grimes 		if ((nrec = *(recno_t *)key->data) == 0)
7958f0484fSRodney W. Grimes 			goto einval;
8058f0484fSRodney W. Grimes 		if (nrec > t->bt_nrecs)
8158f0484fSRodney W. Grimes 			return (RET_SPECIAL);
8258f0484fSRodney W. Grimes 		--nrec;
8358f0484fSRodney W. Grimes 		status = rec_rdelete(t, nrec);
8458f0484fSRodney W. Grimes 		break;
8558f0484fSRodney W. Grimes 	case R_CURSOR:
86ef5d438eSPaul Traina 		if (!F_ISSET(&t->bt_cursor, CURS_INIT))
8758f0484fSRodney W. Grimes 			goto einval;
8858f0484fSRodney W. Grimes 		if (t->bt_nrecs == 0)
8958f0484fSRodney W. Grimes 			return (RET_SPECIAL);
90ef5d438eSPaul Traina 		status = rec_rdelete(t, t->bt_cursor.rcursor - 1);
9158f0484fSRodney W. Grimes 		if (status == RET_SUCCESS)
92ef5d438eSPaul Traina 			--t->bt_cursor.rcursor;
9358f0484fSRodney W. Grimes 		break;
9458f0484fSRodney W. Grimes 	default:
9558f0484fSRodney W. Grimes einval:		errno = EINVAL;
9658f0484fSRodney W. Grimes 		return (RET_ERROR);
9758f0484fSRodney W. Grimes 	}
9858f0484fSRodney W. Grimes 
9958f0484fSRodney W. Grimes 	if (status == RET_SUCCESS)
100ef5d438eSPaul Traina 		F_SET(t, B_MODIFIED | R_MODIFIED);
10158f0484fSRodney W. Grimes 	return (status);
10258f0484fSRodney W. Grimes }
10358f0484fSRodney W. Grimes 
10458f0484fSRodney W. Grimes /*
10558f0484fSRodney W. Grimes  * REC_RDELETE -- Delete the data matching the specified key.
10658f0484fSRodney W. Grimes  *
10758f0484fSRodney W. Grimes  * Parameters:
10858f0484fSRodney W. Grimes  *	tree:	tree
10958f0484fSRodney W. Grimes  *	nrec:	record to delete
11058f0484fSRodney W. Grimes  *
11158f0484fSRodney W. Grimes  * Returns:
11258f0484fSRodney W. Grimes  *	RET_ERROR, RET_SUCCESS and RET_SPECIAL if the key not found.
11358f0484fSRodney W. Grimes  */
11458f0484fSRodney W. Grimes static int
1150ac22237SXin LI rec_rdelete(BTREE *t, recno_t nrec)
11658f0484fSRodney W. Grimes {
11758f0484fSRodney W. Grimes 	EPG *e;
11858f0484fSRodney W. Grimes 	PAGE *h;
11958f0484fSRodney W. Grimes 	int status;
12058f0484fSRodney W. Grimes 
12158f0484fSRodney W. Grimes 	/* Find the record; __rec_search pins the page. */
12258f0484fSRodney W. Grimes 	if ((e = __rec_search(t, nrec, SDELETE)) == NULL)
12358f0484fSRodney W. Grimes 		return (RET_ERROR);
12458f0484fSRodney W. Grimes 
12558f0484fSRodney W. Grimes 	/* Delete the record. */
12658f0484fSRodney W. Grimes 	h = e->page;
12758f0484fSRodney W. Grimes 	status = __rec_dleaf(t, h, e->index);
12858f0484fSRodney W. Grimes 	if (status != RET_SUCCESS) {
12958f0484fSRodney W. Grimes 		mpool_put(t->bt_mp, h, 0);
13058f0484fSRodney W. Grimes 		return (status);
13158f0484fSRodney W. Grimes 	}
13258f0484fSRodney W. Grimes 	mpool_put(t->bt_mp, h, MPOOL_DIRTY);
13358f0484fSRodney W. Grimes 	return (RET_SUCCESS);
13458f0484fSRodney W. Grimes }
13558f0484fSRodney W. Grimes 
13658f0484fSRodney W. Grimes /*
13758f0484fSRodney W. Grimes  * __REC_DLEAF -- Delete a single record from a recno leaf page.
13858f0484fSRodney W. Grimes  *
13958f0484fSRodney W. Grimes  * Parameters:
14058f0484fSRodney W. Grimes  *	t:	tree
1414c66e4b6SXin LI  *	idx:	index on current page to delete
14258f0484fSRodney W. Grimes  *
14358f0484fSRodney W. Grimes  * Returns:
14458f0484fSRodney W. Grimes  *	RET_SUCCESS, RET_ERROR.
14558f0484fSRodney W. Grimes  */
14658f0484fSRodney W. Grimes int
1474c66e4b6SXin LI __rec_dleaf(BTREE *t, PAGE *h, u_int32_t idx)
14858f0484fSRodney W. Grimes {
149ef5d438eSPaul Traina 	RLEAF *rl;
150ef5d438eSPaul Traina 	indx_t *ip, cnt, offset;
151ef5d438eSPaul Traina 	u_int32_t nbytes;
15258f0484fSRodney W. Grimes 	char *from;
15358f0484fSRodney W. Grimes 	void *to;
15458f0484fSRodney W. Grimes 
15558f0484fSRodney W. Grimes 	/*
15658f0484fSRodney W. Grimes 	 * Delete a record from a recno leaf page.  Internal records are never
15758f0484fSRodney W. Grimes 	 * deleted from internal pages, regardless of the records that caused
15858f0484fSRodney W. Grimes 	 * them to be added being deleted.  Pages made empty by deletion are
15958f0484fSRodney W. Grimes 	 * not reclaimed.  They are, however, made available for reuse.
16058f0484fSRodney W. Grimes 	 *
16158f0484fSRodney W. Grimes 	 * Pack the remaining entries at the end of the page, shift the indices
16258f0484fSRodney W. Grimes 	 * down, overwriting the deleted record and its index.  If the record
16358f0484fSRodney W. Grimes 	 * uses overflow pages, make them available for reuse.
16458f0484fSRodney W. Grimes 	 */
1654c66e4b6SXin LI 	to = rl = GETRLEAF(h, idx);
16658f0484fSRodney W. Grimes 	if (rl->flags & P_BIGDATA && __ovfl_delete(t, rl->bytes) == RET_ERROR)
16758f0484fSRodney W. Grimes 		return (RET_ERROR);
16858f0484fSRodney W. Grimes 	nbytes = NRLEAF(rl);
16958f0484fSRodney W. Grimes 
17058f0484fSRodney W. Grimes 	/*
17158f0484fSRodney W. Grimes 	 * Compress the key/data pairs.  Compress and adjust the [BR]LEAF
17258f0484fSRodney W. Grimes 	 * offsets.  Reset the headers.
17358f0484fSRodney W. Grimes 	 */
17458f0484fSRodney W. Grimes 	from = (char *)h + h->upper;
17558f0484fSRodney W. Grimes 	memmove(from + nbytes, from, (char *)to - from);
17658f0484fSRodney W. Grimes 	h->upper += nbytes;
17758f0484fSRodney W. Grimes 
1784c66e4b6SXin LI 	offset = h->linp[idx];
1794c66e4b6SXin LI 	for (cnt = &h->linp[idx] - (ip = &h->linp[0]); cnt--; ++ip)
18058f0484fSRodney W. Grimes 		if (ip[0] < offset)
18158f0484fSRodney W. Grimes 			ip[0] += nbytes;
18258f0484fSRodney W. Grimes 	for (cnt = &h->linp[NEXTINDEX(h)] - ip; --cnt; ++ip)
18358f0484fSRodney W. Grimes 		ip[0] = ip[1] < offset ? ip[1] + nbytes : ip[1];
18458f0484fSRodney W. Grimes 	h->lower -= sizeof(indx_t);
18558f0484fSRodney W. Grimes 	--t->bt_nrecs;
18658f0484fSRodney W. Grimes 	return (RET_SUCCESS);
18758f0484fSRodney W. Grimes }
188