xref: /freebsd/lib/libc/db/btree/bt_delete.c (revision c05ac53b8bbbbd998e18c6544011dd90f0f082da)
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[] = "@(#)bt_delete.c	8.13 (Berkeley) 7/28/94";
3958f0484fSRodney W. Grimes #endif /* LIBC_SCCS and not lint */
40c05ac53bSDavid E. O'Brien #include <sys/cdefs.h>
41c05ac53bSDavid E. O'Brien __FBSDID("$FreeBSD$");
4258f0484fSRodney W. Grimes 
4358f0484fSRodney W. Grimes #include <sys/types.h>
4458f0484fSRodney W. Grimes 
4558f0484fSRodney W. Grimes #include <errno.h>
4658f0484fSRodney W. Grimes #include <stdio.h>
4758f0484fSRodney W. Grimes #include <string.h>
4858f0484fSRodney W. Grimes 
4958f0484fSRodney W. Grimes #include <db.h>
5058f0484fSRodney W. Grimes #include "btree.h"
5158f0484fSRodney W. Grimes 
52c05ac53bSDavid E. O'Brien static int __bt_bdelete(BTREE *, const DBT *);
53c05ac53bSDavid E. O'Brien static int __bt_curdel(BTREE *, const DBT *, PAGE *, u_int);
54c05ac53bSDavid E. O'Brien static int __bt_pdelete(BTREE *, PAGE *);
55c05ac53bSDavid E. O'Brien static int __bt_relink(BTREE *, PAGE *);
56c05ac53bSDavid E. O'Brien static int __bt_stkacq(BTREE *, PAGE **, CURSOR *);
5758f0484fSRodney W. Grimes 
5858f0484fSRodney W. Grimes /*
59ef5d438eSPaul Traina  * __bt_delete
60ef5d438eSPaul Traina  *	Delete the item(s) referenced by a key.
6158f0484fSRodney W. Grimes  *
62ef5d438eSPaul Traina  * Return RET_SPECIAL if the key is not found.
6358f0484fSRodney W. Grimes  */
6458f0484fSRodney W. Grimes int
6558f0484fSRodney W. Grimes __bt_delete(dbp, key, flags)
6658f0484fSRodney W. Grimes 	const DB *dbp;
6758f0484fSRodney W. Grimes 	const DBT *key;
6858f0484fSRodney W. Grimes 	u_int flags;
6958f0484fSRodney W. Grimes {
7058f0484fSRodney W. Grimes 	BTREE *t;
71ef5d438eSPaul Traina 	CURSOR *c;
72ef5d438eSPaul Traina 	PAGE *h;
7358f0484fSRodney W. Grimes 	int status;
7458f0484fSRodney W. Grimes 
7558f0484fSRodney W. Grimes 	t = dbp->internal;
7658f0484fSRodney W. Grimes 
7758f0484fSRodney W. Grimes 	/* Toss any page pinned across calls. */
7858f0484fSRodney W. Grimes 	if (t->bt_pinned != NULL) {
7958f0484fSRodney W. Grimes 		mpool_put(t->bt_mp, t->bt_pinned, 0);
8058f0484fSRodney W. Grimes 		t->bt_pinned = NULL;
8158f0484fSRodney W. Grimes 	}
8258f0484fSRodney W. Grimes 
83ef5d438eSPaul Traina 	/* Check for change to a read-only tree. */
84ef5d438eSPaul Traina 	if (F_ISSET(t, B_RDONLY)) {
8558f0484fSRodney W. Grimes 		errno = EPERM;
8658f0484fSRodney W. Grimes 		return (RET_ERROR);
8758f0484fSRodney W. Grimes 	}
8858f0484fSRodney W. Grimes 
8958f0484fSRodney W. Grimes 	switch (flags) {
9058f0484fSRodney W. Grimes 	case 0:
91ef5d438eSPaul Traina 		status = __bt_bdelete(t, key);
9258f0484fSRodney W. Grimes 		break;
9358f0484fSRodney W. Grimes 	case R_CURSOR:
9458f0484fSRodney W. Grimes 		/*
95ef5d438eSPaul Traina 		 * If flags is R_CURSOR, delete the cursor.  Must already
96ef5d438eSPaul Traina 		 * have started a scan and not have already deleted it.
9758f0484fSRodney W. Grimes 		 */
98ef5d438eSPaul Traina 		c = &t->bt_cursor;
99ef5d438eSPaul Traina 		if (F_ISSET(c, CURS_INIT)) {
100ef5d438eSPaul Traina 			if (F_ISSET(c, CURS_ACQUIRE | CURS_AFTER | CURS_BEFORE))
101ef5d438eSPaul Traina 				return (RET_SPECIAL);
102ef5d438eSPaul Traina 			if ((h = mpool_get(t->bt_mp, c->pg.pgno, 0)) == NULL)
103ef5d438eSPaul Traina 				return (RET_ERROR);
104ef5d438eSPaul Traina 
105ef5d438eSPaul Traina 			/*
106ef5d438eSPaul Traina 			 * If the page is about to be emptied, we'll need to
107ef5d438eSPaul Traina 			 * delete it, which means we have to acquire a stack.
108ef5d438eSPaul Traina 			 */
109ef5d438eSPaul Traina 			if (NEXTINDEX(h) == 1)
110ef5d438eSPaul Traina 				if (__bt_stkacq(t, &h, &t->bt_cursor))
111ef5d438eSPaul Traina 					return (RET_ERROR);
112ef5d438eSPaul Traina 
113ef5d438eSPaul Traina 			status = __bt_dleaf(t, NULL, h, c->pg.index);
114ef5d438eSPaul Traina 
115ef5d438eSPaul Traina 			if (NEXTINDEX(h) == 0 && status == RET_SUCCESS) {
116ef5d438eSPaul Traina 				if (__bt_pdelete(t, h))
117ef5d438eSPaul Traina 					return (RET_ERROR);
118ef5d438eSPaul Traina 			} else
119ef5d438eSPaul Traina 				mpool_put(t->bt_mp,
120ef5d438eSPaul Traina 				    h, status == RET_SUCCESS ? MPOOL_DIRTY : 0);
12158f0484fSRodney W. Grimes 			break;
122ef5d438eSPaul Traina 		}
123ef5d438eSPaul Traina 		/* FALLTHROUGH */
12458f0484fSRodney W. Grimes 	default:
125ef5d438eSPaul Traina 		errno = EINVAL;
12658f0484fSRodney W. Grimes 		return (RET_ERROR);
12758f0484fSRodney W. Grimes 	}
12858f0484fSRodney W. Grimes 	if (status == RET_SUCCESS)
129ef5d438eSPaul Traina 		F_SET(t, B_MODIFIED);
13058f0484fSRodney W. Grimes 	return (status);
13158f0484fSRodney W. Grimes }
13258f0484fSRodney W. Grimes 
13358f0484fSRodney W. Grimes /*
134ef5d438eSPaul Traina  * __bt_stkacq --
135ef5d438eSPaul Traina  *	Acquire a stack so we can delete a cursor entry.
13658f0484fSRodney W. Grimes  *
13758f0484fSRodney W. Grimes  * Parameters:
138ef5d438eSPaul Traina  *	  t:	tree
139ef5d438eSPaul Traina  *	 hp:	pointer to current, pinned PAGE pointer
140ef5d438eSPaul Traina  *	  c:	pointer to the cursor
141ef5d438eSPaul Traina  *
142ef5d438eSPaul Traina  * Returns:
143ef5d438eSPaul Traina  *	0 on success, 1 on failure
144ef5d438eSPaul Traina  */
145ef5d438eSPaul Traina static int
146ef5d438eSPaul Traina __bt_stkacq(t, hp, c)
147ef5d438eSPaul Traina 	BTREE *t;
148ef5d438eSPaul Traina 	PAGE **hp;
149ef5d438eSPaul Traina 	CURSOR *c;
150ef5d438eSPaul Traina {
151ef5d438eSPaul Traina 	BINTERNAL *bi;
152ef5d438eSPaul Traina 	EPG *e;
153ef5d438eSPaul Traina 	EPGNO *parent;
154ef5d438eSPaul Traina 	PAGE *h;
155ef5d438eSPaul Traina 	indx_t index;
156ef5d438eSPaul Traina 	pgno_t pgno;
157ef5d438eSPaul Traina 	recno_t nextpg, prevpg;
158ef5d438eSPaul Traina 	int exact, level;
159ef5d438eSPaul Traina 
160ef5d438eSPaul Traina 	/*
161ef5d438eSPaul Traina 	 * Find the first occurrence of the key in the tree.  Toss the
162ef5d438eSPaul Traina 	 * currently locked page so we don't hit an already-locked page.
163ef5d438eSPaul Traina 	 */
164ef5d438eSPaul Traina 	h = *hp;
165ef5d438eSPaul Traina 	mpool_put(t->bt_mp, h, 0);
166ef5d438eSPaul Traina 	if ((e = __bt_search(t, &c->key, &exact)) == NULL)
167ef5d438eSPaul Traina 		return (1);
168ef5d438eSPaul Traina 	h = e->page;
169ef5d438eSPaul Traina 
170ef5d438eSPaul Traina 	/* See if we got it in one shot. */
171ef5d438eSPaul Traina 	if (h->pgno == c->pg.pgno)
172ef5d438eSPaul Traina 		goto ret;
173ef5d438eSPaul Traina 
174ef5d438eSPaul Traina 	/*
175ef5d438eSPaul Traina 	 * Move right, looking for the page.  At each move we have to move
176ef5d438eSPaul Traina 	 * up the stack until we don't have to move to the next page.  If
177ef5d438eSPaul Traina 	 * we have to change pages at an internal level, we have to fix the
178ef5d438eSPaul Traina 	 * stack back up.
179ef5d438eSPaul Traina 	 */
180ef5d438eSPaul Traina 	while (h->pgno != c->pg.pgno) {
181ef5d438eSPaul Traina 		if ((nextpg = h->nextpg) == P_INVALID)
182ef5d438eSPaul Traina 			break;
183ef5d438eSPaul Traina 		mpool_put(t->bt_mp, h, 0);
184ef5d438eSPaul Traina 
185ef5d438eSPaul Traina 		/* Move up the stack. */
186ef5d438eSPaul Traina 		for (level = 0; (parent = BT_POP(t)) != NULL; ++level) {
187ef5d438eSPaul Traina 			/* Get the parent page. */
188ef5d438eSPaul Traina 			if ((h = mpool_get(t->bt_mp, parent->pgno, 0)) == NULL)
189ef5d438eSPaul Traina 				return (1);
190ef5d438eSPaul Traina 
191ef5d438eSPaul Traina 			/* Move to the next index. */
192ef5d438eSPaul Traina 			if (parent->index != NEXTINDEX(h) - 1) {
193ef5d438eSPaul Traina 				index = parent->index + 1;
194ef5d438eSPaul Traina 				BT_PUSH(t, h->pgno, index);
195ef5d438eSPaul Traina 				break;
196ef5d438eSPaul Traina 			}
197ef5d438eSPaul Traina 			mpool_put(t->bt_mp, h, 0);
198ef5d438eSPaul Traina 		}
199ef5d438eSPaul Traina 
200ef5d438eSPaul Traina 		/* Restore the stack. */
201ef5d438eSPaul Traina 		while (level--) {
202ef5d438eSPaul Traina 			/* Push the next level down onto the stack. */
203ef5d438eSPaul Traina 			bi = GETBINTERNAL(h, index);
204ef5d438eSPaul Traina 			pgno = bi->pgno;
205ef5d438eSPaul Traina 			BT_PUSH(t, pgno, 0);
206ef5d438eSPaul Traina 
207ef5d438eSPaul Traina 			/* Lose the currently pinned page. */
208ef5d438eSPaul Traina 			mpool_put(t->bt_mp, h, 0);
209ef5d438eSPaul Traina 
210ef5d438eSPaul Traina 			/* Get the next level down. */
211ef5d438eSPaul Traina 			if ((h = mpool_get(t->bt_mp, pgno, 0)) == NULL)
212ef5d438eSPaul Traina 				return (1);
213ef5d438eSPaul Traina 			index = 0;
214ef5d438eSPaul Traina 		}
215ef5d438eSPaul Traina 		mpool_put(t->bt_mp, h, 0);
216ef5d438eSPaul Traina 		if ((h = mpool_get(t->bt_mp, nextpg, 0)) == NULL)
217ef5d438eSPaul Traina 			return (1);
218ef5d438eSPaul Traina 	}
219ef5d438eSPaul Traina 
220ef5d438eSPaul Traina 	if (h->pgno == c->pg.pgno)
221ef5d438eSPaul Traina 		goto ret;
222ef5d438eSPaul Traina 
223ef5d438eSPaul Traina 	/* Reacquire the original stack. */
224ef5d438eSPaul Traina 	mpool_put(t->bt_mp, h, 0);
225ef5d438eSPaul Traina 	if ((e = __bt_search(t, &c->key, &exact)) == NULL)
226ef5d438eSPaul Traina 		return (1);
227ef5d438eSPaul Traina 	h = e->page;
228ef5d438eSPaul Traina 
229ef5d438eSPaul Traina 	/*
230ef5d438eSPaul Traina 	 * Move left, looking for the page.  At each move we have to move
231ef5d438eSPaul Traina 	 * up the stack until we don't have to change pages to move to the
232ef5d438eSPaul Traina 	 * next page.  If we have to change pages at an internal level, we
233ef5d438eSPaul Traina 	 * have to fix the stack back up.
234ef5d438eSPaul Traina 	 */
235ef5d438eSPaul Traina 	while (h->pgno != c->pg.pgno) {
236ef5d438eSPaul Traina 		if ((prevpg = h->prevpg) == P_INVALID)
237ef5d438eSPaul Traina 			break;
238ef5d438eSPaul Traina 		mpool_put(t->bt_mp, h, 0);
239ef5d438eSPaul Traina 
240ef5d438eSPaul Traina 		/* Move up the stack. */
241ef5d438eSPaul Traina 		for (level = 0; (parent = BT_POP(t)) != NULL; ++level) {
242ef5d438eSPaul Traina 			/* Get the parent page. */
243ef5d438eSPaul Traina 			if ((h = mpool_get(t->bt_mp, parent->pgno, 0)) == NULL)
244ef5d438eSPaul Traina 				return (1);
245ef5d438eSPaul Traina 
246ef5d438eSPaul Traina 			/* Move to the next index. */
247ef5d438eSPaul Traina 			if (parent->index != 0) {
248ef5d438eSPaul Traina 				index = parent->index - 1;
249ef5d438eSPaul Traina 				BT_PUSH(t, h->pgno, index);
250ef5d438eSPaul Traina 				break;
251ef5d438eSPaul Traina 			}
252ef5d438eSPaul Traina 			mpool_put(t->bt_mp, h, 0);
253ef5d438eSPaul Traina 		}
254ef5d438eSPaul Traina 
255ef5d438eSPaul Traina 		/* Restore the stack. */
256ef5d438eSPaul Traina 		while (level--) {
257ef5d438eSPaul Traina 			/* Push the next level down onto the stack. */
258ef5d438eSPaul Traina 			bi = GETBINTERNAL(h, index);
259ef5d438eSPaul Traina 			pgno = bi->pgno;
260ef5d438eSPaul Traina 
261ef5d438eSPaul Traina 			/* Lose the currently pinned page. */
262ef5d438eSPaul Traina 			mpool_put(t->bt_mp, h, 0);
263ef5d438eSPaul Traina 
264ef5d438eSPaul Traina 			/* Get the next level down. */
265ef5d438eSPaul Traina 			if ((h = mpool_get(t->bt_mp, pgno, 0)) == NULL)
266ef5d438eSPaul Traina 				return (1);
267ef5d438eSPaul Traina 
268ef5d438eSPaul Traina 			index = NEXTINDEX(h) - 1;
269ef5d438eSPaul Traina 			BT_PUSH(t, pgno, index);
270ef5d438eSPaul Traina 		}
271ef5d438eSPaul Traina 		mpool_put(t->bt_mp, h, 0);
272ef5d438eSPaul Traina 		if ((h = mpool_get(t->bt_mp, prevpg, 0)) == NULL)
273ef5d438eSPaul Traina 			return (1);
274ef5d438eSPaul Traina 	}
275ef5d438eSPaul Traina 
276ef5d438eSPaul Traina 
277ef5d438eSPaul Traina ret:	mpool_put(t->bt_mp, h, 0);
278ef5d438eSPaul Traina 	return ((*hp = mpool_get(t->bt_mp, c->pg.pgno, 0)) == NULL);
279ef5d438eSPaul Traina }
280ef5d438eSPaul Traina 
281ef5d438eSPaul Traina /*
282ef5d438eSPaul Traina  * __bt_bdelete --
283ef5d438eSPaul Traina  *	Delete all key/data pairs matching the specified key.
284ef5d438eSPaul Traina  *
285ef5d438eSPaul Traina  * Parameters:
286ef5d438eSPaul Traina  *	  t:	tree
28758f0484fSRodney W. Grimes  *	key:	key to delete
28858f0484fSRodney W. Grimes  *
28958f0484fSRodney W. Grimes  * Returns:
29058f0484fSRodney W. Grimes  *	RET_ERROR, RET_SUCCESS and RET_SPECIAL if the key not found.
29158f0484fSRodney W. Grimes  */
29258f0484fSRodney W. Grimes static int
293ef5d438eSPaul Traina __bt_bdelete(t, key)
29458f0484fSRodney W. Grimes 	BTREE *t;
29558f0484fSRodney W. Grimes 	const DBT *key;
29658f0484fSRodney W. Grimes {
297ef5d438eSPaul Traina 	EPG *e;
29858f0484fSRodney W. Grimes 	PAGE *h;
299ef5d438eSPaul Traina 	int deleted, exact, redo;
300ef5d438eSPaul Traina 
301ef5d438eSPaul Traina 	deleted = 0;
30258f0484fSRodney W. Grimes 
30358f0484fSRodney W. Grimes 	/* Find any matching record; __bt_search pins the page. */
304ef5d438eSPaul Traina loop:	if ((e = __bt_search(t, key, &exact)) == NULL)
305ef5d438eSPaul Traina 		return (deleted ? RET_SUCCESS : RET_ERROR);
30658f0484fSRodney W. Grimes 	if (!exact) {
30758f0484fSRodney W. Grimes 		mpool_put(t->bt_mp, e->page, 0);
30858f0484fSRodney W. Grimes 		return (deleted ? RET_SUCCESS : RET_SPECIAL);
30958f0484fSRodney W. Grimes 	}
31058f0484fSRodney W. Grimes 
31158f0484fSRodney W. Grimes 	/*
312ef5d438eSPaul Traina 	 * Delete forward, then delete backward, from the found key.  If
313ef5d438eSPaul Traina 	 * there are duplicates and we reach either side of the page, do
314ef5d438eSPaul Traina 	 * the key search again, so that we get them all.
315ef5d438eSPaul Traina 	 */
316ef5d438eSPaul Traina 	redo = 0;
317ef5d438eSPaul Traina 	h = e->page;
318ef5d438eSPaul Traina 	do {
319ef5d438eSPaul Traina 		if (__bt_dleaf(t, key, h, e->index)) {
320ef5d438eSPaul Traina 			mpool_put(t->bt_mp, h, 0);
321ef5d438eSPaul Traina 			return (RET_ERROR);
322ef5d438eSPaul Traina 		}
323ef5d438eSPaul Traina 		if (F_ISSET(t, B_NODUPS)) {
324ef5d438eSPaul Traina 			if (NEXTINDEX(h) == 0) {
325ef5d438eSPaul Traina 				if (__bt_pdelete(t, h))
326ef5d438eSPaul Traina 					return (RET_ERROR);
327ef5d438eSPaul Traina 			} else
328ef5d438eSPaul Traina 				mpool_put(t->bt_mp, h, MPOOL_DIRTY);
329ef5d438eSPaul Traina 			return (RET_SUCCESS);
330ef5d438eSPaul Traina 		}
331ef5d438eSPaul Traina 		deleted = 1;
332ef5d438eSPaul Traina 	} while (e->index < NEXTINDEX(h) && __bt_cmp(t, key, e) == 0);
333ef5d438eSPaul Traina 
334ef5d438eSPaul Traina 	/* Check for right-hand edge of the page. */
335ef5d438eSPaul Traina 	if (e->index == NEXTINDEX(h))
336ef5d438eSPaul Traina 		redo = 1;
337ef5d438eSPaul Traina 
338ef5d438eSPaul Traina 	/* Delete from the key to the beginning of the page. */
339ef5d438eSPaul Traina 	while (e->index-- > 0) {
340ef5d438eSPaul Traina 		if (__bt_cmp(t, key, e) != 0)
341ef5d438eSPaul Traina 			break;
342ef5d438eSPaul Traina 		if (__bt_dleaf(t, key, h, e->index) == RET_ERROR) {
343ef5d438eSPaul Traina 			mpool_put(t->bt_mp, h, 0);
344ef5d438eSPaul Traina 			return (RET_ERROR);
345ef5d438eSPaul Traina 		}
346ef5d438eSPaul Traina 		if (e->index == 0)
347ef5d438eSPaul Traina 			redo = 1;
348ef5d438eSPaul Traina 	}
349ef5d438eSPaul Traina 
350ef5d438eSPaul Traina 	/* Check for an empty page. */
351ef5d438eSPaul Traina 	if (NEXTINDEX(h) == 0) {
352ef5d438eSPaul Traina 		if (__bt_pdelete(t, h))
353ef5d438eSPaul Traina 			return (RET_ERROR);
354ef5d438eSPaul Traina 		goto loop;
355ef5d438eSPaul Traina 	}
356ef5d438eSPaul Traina 
357ef5d438eSPaul Traina 	/* Put the page. */
358ef5d438eSPaul Traina 	mpool_put(t->bt_mp, h, MPOOL_DIRTY);
359ef5d438eSPaul Traina 
360ef5d438eSPaul Traina 	if (redo)
361ef5d438eSPaul Traina 		goto loop;
362ef5d438eSPaul Traina 	return (RET_SUCCESS);
363ef5d438eSPaul Traina }
364ef5d438eSPaul Traina 
365ef5d438eSPaul Traina /*
366ef5d438eSPaul Traina  * __bt_pdelete --
367ef5d438eSPaul Traina  *	Delete a single page from the tree.
36858f0484fSRodney W. Grimes  *
36958f0484fSRodney W. Grimes  * Parameters:
37058f0484fSRodney W. Grimes  *	t:	tree
371ef5d438eSPaul Traina  *	h:	leaf page
372ef5d438eSPaul Traina  *
373ef5d438eSPaul Traina  * Returns:
374ef5d438eSPaul Traina  *	RET_SUCCESS, RET_ERROR.
375ef5d438eSPaul Traina  *
376ef5d438eSPaul Traina  * Side-effects:
377ef5d438eSPaul Traina  *	mpool_put's the page
378ef5d438eSPaul Traina  */
379ef5d438eSPaul Traina static int
380ef5d438eSPaul Traina __bt_pdelete(t, h)
381ef5d438eSPaul Traina 	BTREE *t;
382ef5d438eSPaul Traina 	PAGE *h;
383ef5d438eSPaul Traina {
384ef5d438eSPaul Traina 	BINTERNAL *bi;
385ef5d438eSPaul Traina 	PAGE *pg;
386ef5d438eSPaul Traina 	EPGNO *parent;
387ef5d438eSPaul Traina 	indx_t cnt, index, *ip, offset;
388ef5d438eSPaul Traina 	u_int32_t nksize;
389ef5d438eSPaul Traina 	char *from;
390ef5d438eSPaul Traina 
391ef5d438eSPaul Traina 	/*
392ef5d438eSPaul Traina 	 * Walk the parent page stack -- a LIFO stack of the pages that were
393ef5d438eSPaul Traina 	 * traversed when we searched for the page where the delete occurred.
394ef5d438eSPaul Traina 	 * Each stack entry is a page number and a page index offset.  The
395ef5d438eSPaul Traina 	 * offset is for the page traversed on the search.  We've just deleted
396ef5d438eSPaul Traina 	 * a page, so we have to delete the key from the parent page.
397ef5d438eSPaul Traina 	 *
398ef5d438eSPaul Traina 	 * If the delete from the parent page makes it empty, this process may
399ef5d438eSPaul Traina 	 * continue all the way up the tree.  We stop if we reach the root page
400ef5d438eSPaul Traina 	 * (which is never deleted, it's just not worth the effort) or if the
401ef5d438eSPaul Traina 	 * delete does not empty the page.
402ef5d438eSPaul Traina 	 */
403ef5d438eSPaul Traina 	while ((parent = BT_POP(t)) != NULL) {
404ef5d438eSPaul Traina 		/* Get the parent page. */
405ef5d438eSPaul Traina 		if ((pg = mpool_get(t->bt_mp, parent->pgno, 0)) == NULL)
406ef5d438eSPaul Traina 			return (RET_ERROR);
407ef5d438eSPaul Traina 
408ef5d438eSPaul Traina 		index = parent->index;
409ef5d438eSPaul Traina 		bi = GETBINTERNAL(pg, index);
410ef5d438eSPaul Traina 
411ef5d438eSPaul Traina 		/* Free any overflow pages. */
412ef5d438eSPaul Traina 		if (bi->flags & P_BIGKEY &&
413ef5d438eSPaul Traina 		    __ovfl_delete(t, bi->bytes) == RET_ERROR) {
414ef5d438eSPaul Traina 			mpool_put(t->bt_mp, pg, 0);
415ef5d438eSPaul Traina 			return (RET_ERROR);
416ef5d438eSPaul Traina 		}
417ef5d438eSPaul Traina 
418ef5d438eSPaul Traina 		/*
419ef5d438eSPaul Traina 		 * Free the parent if it has only the one key and it's not the
420ef5d438eSPaul Traina 		 * root page. If it's the rootpage, turn it back into an empty
421ef5d438eSPaul Traina 		 * leaf page.
422ef5d438eSPaul Traina 		 */
423ef5d438eSPaul Traina 		if (NEXTINDEX(pg) == 1)
424ef5d438eSPaul Traina 			if (pg->pgno == P_ROOT) {
425ef5d438eSPaul Traina 				pg->lower = BTDATAOFF;
426ef5d438eSPaul Traina 				pg->upper = t->bt_psize;
427ef5d438eSPaul Traina 				pg->flags = P_BLEAF;
428ef5d438eSPaul Traina 			} else {
429ef5d438eSPaul Traina 				if (__bt_relink(t, pg) || __bt_free(t, pg))
430ef5d438eSPaul Traina 					return (RET_ERROR);
431ef5d438eSPaul Traina 				continue;
432ef5d438eSPaul Traina 			}
433ef5d438eSPaul Traina 		else {
434ef5d438eSPaul Traina 			/* Pack remaining key items at the end of the page. */
435ef5d438eSPaul Traina 			nksize = NBINTERNAL(bi->ksize);
436ef5d438eSPaul Traina 			from = (char *)pg + pg->upper;
437ef5d438eSPaul Traina 			memmove(from + nksize, from, (char *)bi - from);
438ef5d438eSPaul Traina 			pg->upper += nksize;
439ef5d438eSPaul Traina 
440ef5d438eSPaul Traina 			/* Adjust indices' offsets, shift the indices down. */
441ef5d438eSPaul Traina 			offset = pg->linp[index];
442ef5d438eSPaul Traina 			for (cnt = index, ip = &pg->linp[0]; cnt--; ++ip)
443ef5d438eSPaul Traina 				if (ip[0] < offset)
444ef5d438eSPaul Traina 					ip[0] += nksize;
445ef5d438eSPaul Traina 			for (cnt = NEXTINDEX(pg) - index; --cnt; ++ip)
446ef5d438eSPaul Traina 				ip[0] = ip[1] < offset ? ip[1] + nksize : ip[1];
447ef5d438eSPaul Traina 			pg->lower -= sizeof(indx_t);
448ef5d438eSPaul Traina 		}
449ef5d438eSPaul Traina 
450ef5d438eSPaul Traina 		mpool_put(t->bt_mp, pg, MPOOL_DIRTY);
451ef5d438eSPaul Traina 		break;
452ef5d438eSPaul Traina 	}
453ef5d438eSPaul Traina 
454ef5d438eSPaul Traina 	/* Free the leaf page, as long as it wasn't the root. */
455ef5d438eSPaul Traina 	if (h->pgno == P_ROOT) {
456ef5d438eSPaul Traina 		mpool_put(t->bt_mp, h, MPOOL_DIRTY);
457ef5d438eSPaul Traina 		return (RET_SUCCESS);
458ef5d438eSPaul Traina 	}
459ef5d438eSPaul Traina 	return (__bt_relink(t, h) || __bt_free(t, h));
460ef5d438eSPaul Traina }
461ef5d438eSPaul Traina 
462ef5d438eSPaul Traina /*
463ef5d438eSPaul Traina  * __bt_dleaf --
464ef5d438eSPaul Traina  *	Delete a single record from a leaf page.
465ef5d438eSPaul Traina  *
466ef5d438eSPaul Traina  * Parameters:
467ef5d438eSPaul Traina  *	t:	tree
468ef5d438eSPaul Traina  *    key:	referenced key
469ef5d438eSPaul Traina  *	h:	page
470ef5d438eSPaul Traina  *	index:	index on page to delete
47158f0484fSRodney W. Grimes  *
47258f0484fSRodney W. Grimes  * Returns:
47358f0484fSRodney W. Grimes  *	RET_SUCCESS, RET_ERROR.
47458f0484fSRodney W. Grimes  */
47558f0484fSRodney W. Grimes int
476ef5d438eSPaul Traina __bt_dleaf(t, key, h, index)
47758f0484fSRodney W. Grimes 	BTREE *t;
478ef5d438eSPaul Traina 	const DBT *key;
47958f0484fSRodney W. Grimes 	PAGE *h;
480ef5d438eSPaul Traina 	u_int index;
48158f0484fSRodney W. Grimes {
482ef5d438eSPaul Traina 	BLEAF *bl;
483ef5d438eSPaul Traina 	indx_t cnt, *ip, offset;
484ef5d438eSPaul Traina 	u_int32_t nbytes;
48558f0484fSRodney W. Grimes 	void *to;
486ef5d438eSPaul Traina 	char *from;
48758f0484fSRodney W. Grimes 
488ef5d438eSPaul Traina 	/* If this record is referenced by the cursor, delete the cursor. */
489ef5d438eSPaul Traina 	if (F_ISSET(&t->bt_cursor, CURS_INIT) &&
490ef5d438eSPaul Traina 	    !F_ISSET(&t->bt_cursor, CURS_ACQUIRE) &&
491ef5d438eSPaul Traina 	    t->bt_cursor.pg.pgno == h->pgno && t->bt_cursor.pg.index == index &&
492ef5d438eSPaul Traina 	    __bt_curdel(t, key, h, index))
493ef5d438eSPaul Traina 		return (RET_ERROR);
494ef5d438eSPaul Traina 
495ef5d438eSPaul Traina 	/* If the entry uses overflow pages, make them available for reuse. */
49658f0484fSRodney W. Grimes 	to = bl = GETBLEAF(h, index);
49758f0484fSRodney W. Grimes 	if (bl->flags & P_BIGKEY && __ovfl_delete(t, bl->bytes) == RET_ERROR)
49858f0484fSRodney W. Grimes 		return (RET_ERROR);
49958f0484fSRodney W. Grimes 	if (bl->flags & P_BIGDATA &&
50058f0484fSRodney W. Grimes 	    __ovfl_delete(t, bl->bytes + bl->ksize) == RET_ERROR)
50158f0484fSRodney W. Grimes 		return (RET_ERROR);
50258f0484fSRodney W. Grimes 
503ef5d438eSPaul Traina 	/* Pack the remaining key/data items at the end of the page. */
504ef5d438eSPaul Traina 	nbytes = NBLEAF(bl);
50558f0484fSRodney W. Grimes 	from = (char *)h + h->upper;
50658f0484fSRodney W. Grimes 	memmove(from + nbytes, from, (char *)to - from);
50758f0484fSRodney W. Grimes 	h->upper += nbytes;
50858f0484fSRodney W. Grimes 
509ef5d438eSPaul Traina 	/* Adjust the indices' offsets, shift the indices down. */
51058f0484fSRodney W. Grimes 	offset = h->linp[index];
51158f0484fSRodney W. Grimes 	for (cnt = index, ip = &h->linp[0]; cnt--; ++ip)
51258f0484fSRodney W. Grimes 		if (ip[0] < offset)
51358f0484fSRodney W. Grimes 			ip[0] += nbytes;
51458f0484fSRodney W. Grimes 	for (cnt = NEXTINDEX(h) - index; --cnt; ++ip)
51558f0484fSRodney W. Grimes 		ip[0] = ip[1] < offset ? ip[1] + nbytes : ip[1];
51658f0484fSRodney W. Grimes 	h->lower -= sizeof(indx_t);
517ef5d438eSPaul Traina 
518ef5d438eSPaul Traina 	/* If the cursor is on this page, adjust it as necessary. */
519ef5d438eSPaul Traina 	if (F_ISSET(&t->bt_cursor, CURS_INIT) &&
520ef5d438eSPaul Traina 	    !F_ISSET(&t->bt_cursor, CURS_ACQUIRE) &&
521ef5d438eSPaul Traina 	    t->bt_cursor.pg.pgno == h->pgno && t->bt_cursor.pg.index > index)
522ef5d438eSPaul Traina 		--t->bt_cursor.pg.index;
523ef5d438eSPaul Traina 
52458f0484fSRodney W. Grimes 	return (RET_SUCCESS);
52558f0484fSRodney W. Grimes }
526ef5d438eSPaul Traina 
527ef5d438eSPaul Traina /*
528ef5d438eSPaul Traina  * __bt_curdel --
529ef5d438eSPaul Traina  *	Delete the cursor.
530ef5d438eSPaul Traina  *
531ef5d438eSPaul Traina  * Parameters:
532ef5d438eSPaul Traina  *	t:	tree
533ef5d438eSPaul Traina  *    key:	referenced key (or NULL)
534ef5d438eSPaul Traina  *	h:	page
535ef5d438eSPaul Traina  *  index:	index on page to delete
536ef5d438eSPaul Traina  *
537ef5d438eSPaul Traina  * Returns:
538ef5d438eSPaul Traina  *	RET_SUCCESS, RET_ERROR.
539ef5d438eSPaul Traina  */
540ef5d438eSPaul Traina static int
541ef5d438eSPaul Traina __bt_curdel(t, key, h, index)
542ef5d438eSPaul Traina 	BTREE *t;
543ef5d438eSPaul Traina 	const DBT *key;
544ef5d438eSPaul Traina 	PAGE *h;
545ef5d438eSPaul Traina 	u_int index;
546ef5d438eSPaul Traina {
547ef5d438eSPaul Traina 	CURSOR *c;
548ef5d438eSPaul Traina 	EPG e;
549ef5d438eSPaul Traina 	PAGE *pg;
550ef5d438eSPaul Traina 	int curcopy, status;
551ef5d438eSPaul Traina 
552ef5d438eSPaul Traina 	/*
553ef5d438eSPaul Traina 	 * If there are duplicates, move forward or backward to one.
554ef5d438eSPaul Traina 	 * Otherwise, copy the key into the cursor area.
555ef5d438eSPaul Traina 	 */
556ef5d438eSPaul Traina 	c = &t->bt_cursor;
557ef5d438eSPaul Traina 	F_CLR(c, CURS_AFTER | CURS_BEFORE | CURS_ACQUIRE);
558ef5d438eSPaul Traina 
559ef5d438eSPaul Traina 	curcopy = 0;
560ef5d438eSPaul Traina 	if (!F_ISSET(t, B_NODUPS)) {
561ef5d438eSPaul Traina 		/*
562ef5d438eSPaul Traina 		 * We're going to have to do comparisons.  If we weren't
563ef5d438eSPaul Traina 		 * provided a copy of the key, i.e. the user is deleting
564ef5d438eSPaul Traina 		 * the current cursor position, get one.
565ef5d438eSPaul Traina 		 */
566ef5d438eSPaul Traina 		if (key == NULL) {
567ef5d438eSPaul Traina 			e.page = h;
568ef5d438eSPaul Traina 			e.index = index;
569ef5d438eSPaul Traina 			if ((status = __bt_ret(t, &e,
570ef5d438eSPaul Traina 			    &c->key, &c->key, NULL, NULL, 1)) != RET_SUCCESS)
571ef5d438eSPaul Traina 				return (status);
572ef5d438eSPaul Traina 			curcopy = 1;
573ef5d438eSPaul Traina 			key = &c->key;
574ef5d438eSPaul Traina 		}
575ef5d438eSPaul Traina 		/* Check previous key, if not at the beginning of the page. */
576ef5d438eSPaul Traina 		if (index > 0) {
577ef5d438eSPaul Traina 			e.page = h;
578ef5d438eSPaul Traina 			e.index = index - 1;
579ef5d438eSPaul Traina 			if (__bt_cmp(t, key, &e) == 0) {
580ef5d438eSPaul Traina 				F_SET(c, CURS_BEFORE);
581ef5d438eSPaul Traina 				goto dup2;
582ef5d438eSPaul Traina 			}
583ef5d438eSPaul Traina 		}
584ef5d438eSPaul Traina 		/* Check next key, if not at the end of the page. */
585ef5d438eSPaul Traina 		if (index < NEXTINDEX(h) - 1) {
586ef5d438eSPaul Traina 			e.page = h;
587ef5d438eSPaul Traina 			e.index = index + 1;
588ef5d438eSPaul Traina 			if (__bt_cmp(t, key, &e) == 0) {
589ef5d438eSPaul Traina 				F_SET(c, CURS_AFTER);
590ef5d438eSPaul Traina 				goto dup2;
591ef5d438eSPaul Traina 			}
592ef5d438eSPaul Traina 		}
593ef5d438eSPaul Traina 		/* Check previous key if at the beginning of the page. */
594ef5d438eSPaul Traina 		if (index == 0 && h->prevpg != P_INVALID) {
595ef5d438eSPaul Traina 			if ((pg = mpool_get(t->bt_mp, h->prevpg, 0)) == NULL)
596ef5d438eSPaul Traina 				return (RET_ERROR);
597ef5d438eSPaul Traina 			e.page = pg;
598ef5d438eSPaul Traina 			e.index = NEXTINDEX(pg) - 1;
599ef5d438eSPaul Traina 			if (__bt_cmp(t, key, &e) == 0) {
600ef5d438eSPaul Traina 				F_SET(c, CURS_BEFORE);
601ef5d438eSPaul Traina 				goto dup1;
602ef5d438eSPaul Traina 			}
603ef5d438eSPaul Traina 			mpool_put(t->bt_mp, pg, 0);
604ef5d438eSPaul Traina 		}
605ef5d438eSPaul Traina 		/* Check next key if at the end of the page. */
606ef5d438eSPaul Traina 		if (index == NEXTINDEX(h) - 1 && h->nextpg != P_INVALID) {
607ef5d438eSPaul Traina 			if ((pg = mpool_get(t->bt_mp, h->nextpg, 0)) == NULL)
608ef5d438eSPaul Traina 				return (RET_ERROR);
609ef5d438eSPaul Traina 			e.page = pg;
610ef5d438eSPaul Traina 			e.index = 0;
611ef5d438eSPaul Traina 			if (__bt_cmp(t, key, &e) == 0) {
612ef5d438eSPaul Traina 				F_SET(c, CURS_AFTER);
613ef5d438eSPaul Traina dup1:				mpool_put(t->bt_mp, pg, 0);
614ef5d438eSPaul Traina dup2:				c->pg.pgno = e.page->pgno;
615ef5d438eSPaul Traina 				c->pg.index = e.index;
616ef5d438eSPaul Traina 				return (RET_SUCCESS);
617ef5d438eSPaul Traina 			}
618ef5d438eSPaul Traina 			mpool_put(t->bt_mp, pg, 0);
619ef5d438eSPaul Traina 		}
620ef5d438eSPaul Traina 	}
621ef5d438eSPaul Traina 	e.page = h;
622ef5d438eSPaul Traina 	e.index = index;
623ef5d438eSPaul Traina 	if (curcopy || (status =
624ef5d438eSPaul Traina 	    __bt_ret(t, &e, &c->key, &c->key, NULL, NULL, 1)) == RET_SUCCESS) {
625ef5d438eSPaul Traina 		F_SET(c, CURS_ACQUIRE);
626ef5d438eSPaul Traina 		return (RET_SUCCESS);
627ef5d438eSPaul Traina 	}
628ef5d438eSPaul Traina 	return (status);
629ef5d438eSPaul Traina }
630ef5d438eSPaul Traina 
631ef5d438eSPaul Traina /*
632ef5d438eSPaul Traina  * __bt_relink --
633ef5d438eSPaul Traina  *	Link around a deleted page.
634ef5d438eSPaul Traina  *
635ef5d438eSPaul Traina  * Parameters:
636ef5d438eSPaul Traina  *	t:	tree
637ef5d438eSPaul Traina  *	h:	page to be deleted
638ef5d438eSPaul Traina  */
639ef5d438eSPaul Traina static int
640ef5d438eSPaul Traina __bt_relink(t, h)
641ef5d438eSPaul Traina 	BTREE *t;
642ef5d438eSPaul Traina 	PAGE *h;
643ef5d438eSPaul Traina {
644ef5d438eSPaul Traina 	PAGE *pg;
645ef5d438eSPaul Traina 
646ef5d438eSPaul Traina 	if (h->nextpg != P_INVALID) {
647ef5d438eSPaul Traina 		if ((pg = mpool_get(t->bt_mp, h->nextpg, 0)) == NULL)
648ef5d438eSPaul Traina 			return (RET_ERROR);
649ef5d438eSPaul Traina 		pg->prevpg = h->prevpg;
650ef5d438eSPaul Traina 		mpool_put(t->bt_mp, pg, MPOOL_DIRTY);
651ef5d438eSPaul Traina 	}
652ef5d438eSPaul Traina 	if (h->prevpg != P_INVALID) {
653ef5d438eSPaul Traina 		if ((pg = mpool_get(t->bt_mp, h->prevpg, 0)) == NULL)
654ef5d438eSPaul Traina 			return (RET_ERROR);
655ef5d438eSPaul Traina 		pg->nextpg = h->nextpg;
656ef5d438eSPaul Traina 		mpool_put(t->bt_mp, pg, MPOOL_DIRTY);
657ef5d438eSPaul Traina 	}
658ef5d438eSPaul Traina 	return (0);
659ef5d438eSPaul Traina }
660