xref: /freebsd/lib/libc/db/btree/bt_delete.c (revision dc36d6f9bb1753f3808552f3afd30eda9a7b206a)
158f0484fSRodney W. Grimes /*-
2*8a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
3*8a16b7a1SPedro F. Giffuni  *
4ef5d438eSPaul Traina  * Copyright (c) 1990, 1993, 1994
558f0484fSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
658f0484fSRodney W. Grimes  *
758f0484fSRodney W. Grimes  * This code is derived from software contributed to Berkeley by
858f0484fSRodney W. Grimes  * Mike Olson.
958f0484fSRodney W. Grimes  *
1058f0484fSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
1158f0484fSRodney W. Grimes  * modification, are permitted provided that the following conditions
1258f0484fSRodney W. Grimes  * are met:
1358f0484fSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
1458f0484fSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
1558f0484fSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
1658f0484fSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
1758f0484fSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
18fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
1958f0484fSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
2058f0484fSRodney W. Grimes  *    without specific prior written permission.
2158f0484fSRodney W. Grimes  *
2258f0484fSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2358f0484fSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2458f0484fSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2558f0484fSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2658f0484fSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2758f0484fSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2858f0484fSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2958f0484fSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3058f0484fSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3158f0484fSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3258f0484fSRodney W. Grimes  * SUCH DAMAGE.
3358f0484fSRodney W. Grimes  */
3458f0484fSRodney W. Grimes 
3558f0484fSRodney W. Grimes #include <sys/types.h>
3658f0484fSRodney W. Grimes 
3758f0484fSRodney W. Grimes #include <errno.h>
3858f0484fSRodney W. Grimes #include <stdio.h>
3958f0484fSRodney W. Grimes #include <string.h>
4058f0484fSRodney W. Grimes 
4158f0484fSRodney W. Grimes #include <db.h>
4258f0484fSRodney W. Grimes #include "btree.h"
4358f0484fSRodney W. Grimes 
44c05ac53bSDavid E. O'Brien static int __bt_bdelete(BTREE *, const DBT *);
45c05ac53bSDavid E. O'Brien static int __bt_curdel(BTREE *, const DBT *, PAGE *, u_int);
46c05ac53bSDavid E. O'Brien static int __bt_pdelete(BTREE *, PAGE *);
47c05ac53bSDavid E. O'Brien static int __bt_relink(BTREE *, PAGE *);
48c05ac53bSDavid E. O'Brien static int __bt_stkacq(BTREE *, PAGE **, CURSOR *);
4958f0484fSRodney W. Grimes 
5058f0484fSRodney W. Grimes /*
51ef5d438eSPaul Traina  * __bt_delete
52ef5d438eSPaul Traina  *	Delete the item(s) referenced by a key.
5358f0484fSRodney W. Grimes  *
54ef5d438eSPaul Traina  * Return RET_SPECIAL if the key is not found.
5558f0484fSRodney W. Grimes  */
5658f0484fSRodney W. Grimes int
__bt_delete(const DB * dbp,const DBT * key,u_int flags)570ac22237SXin LI __bt_delete(const DB *dbp, const DBT *key, u_int flags)
5858f0484fSRodney W. Grimes {
5958f0484fSRodney W. Grimes 	BTREE *t;
60ef5d438eSPaul Traina 	CURSOR *c;
61ef5d438eSPaul Traina 	PAGE *h;
6258f0484fSRodney W. Grimes 	int status;
6358f0484fSRodney W. Grimes 
6458f0484fSRodney W. Grimes 	t = dbp->internal;
6558f0484fSRodney W. Grimes 
6658f0484fSRodney W. Grimes 	/* Toss any page pinned across calls. */
6758f0484fSRodney W. Grimes 	if (t->bt_pinned != NULL) {
6858f0484fSRodney W. Grimes 		mpool_put(t->bt_mp, t->bt_pinned, 0);
6958f0484fSRodney W. Grimes 		t->bt_pinned = NULL;
7058f0484fSRodney W. Grimes 	}
7158f0484fSRodney W. Grimes 
72ef5d438eSPaul Traina 	/* Check for change to a read-only tree. */
73ef5d438eSPaul Traina 	if (F_ISSET(t, B_RDONLY)) {
7458f0484fSRodney W. Grimes 		errno = EPERM;
7558f0484fSRodney W. Grimes 		return (RET_ERROR);
7658f0484fSRodney W. Grimes 	}
7758f0484fSRodney W. Grimes 
7858f0484fSRodney W. Grimes 	switch (flags) {
7958f0484fSRodney W. Grimes 	case 0:
80ef5d438eSPaul Traina 		status = __bt_bdelete(t, key);
8158f0484fSRodney W. Grimes 		break;
8258f0484fSRodney W. Grimes 	case R_CURSOR:
8358f0484fSRodney W. Grimes 		/*
84ef5d438eSPaul Traina 		 * If flags is R_CURSOR, delete the cursor.  Must already
85ef5d438eSPaul Traina 		 * have started a scan and not have already deleted it.
8658f0484fSRodney W. Grimes 		 */
87ef5d438eSPaul Traina 		c = &t->bt_cursor;
88ef5d438eSPaul Traina 		if (F_ISSET(c, CURS_INIT)) {
89ef5d438eSPaul Traina 			if (F_ISSET(c, CURS_ACQUIRE | CURS_AFTER | CURS_BEFORE))
90ef5d438eSPaul Traina 				return (RET_SPECIAL);
91ef5d438eSPaul Traina 			if ((h = mpool_get(t->bt_mp, c->pg.pgno, 0)) == NULL)
92ef5d438eSPaul Traina 				return (RET_ERROR);
93ef5d438eSPaul Traina 
94ef5d438eSPaul Traina 			/*
95ef5d438eSPaul Traina 			 * If the page is about to be emptied, we'll need to
96ef5d438eSPaul Traina 			 * delete it, which means we have to acquire a stack.
97ef5d438eSPaul Traina 			 */
98ef5d438eSPaul Traina 			if (NEXTINDEX(h) == 1)
99ef5d438eSPaul Traina 				if (__bt_stkacq(t, &h, &t->bt_cursor))
100ef5d438eSPaul Traina 					return (RET_ERROR);
101ef5d438eSPaul Traina 
102ef5d438eSPaul Traina 			status = __bt_dleaf(t, NULL, h, c->pg.index);
103ef5d438eSPaul Traina 
104ef5d438eSPaul Traina 			if (NEXTINDEX(h) == 0 && status == RET_SUCCESS) {
105ef5d438eSPaul Traina 				if (__bt_pdelete(t, h))
106ef5d438eSPaul Traina 					return (RET_ERROR);
107ef5d438eSPaul Traina 			} else
108ef5d438eSPaul Traina 				mpool_put(t->bt_mp,
109ef5d438eSPaul Traina 				    h, status == RET_SUCCESS ? MPOOL_DIRTY : 0);
11058f0484fSRodney W. Grimes 			break;
111ef5d438eSPaul Traina 		}
112ef5d438eSPaul Traina 		/* FALLTHROUGH */
11358f0484fSRodney W. Grimes 	default:
114ef5d438eSPaul Traina 		errno = EINVAL;
11558f0484fSRodney W. Grimes 		return (RET_ERROR);
11658f0484fSRodney W. Grimes 	}
11758f0484fSRodney W. Grimes 	if (status == RET_SUCCESS)
118ef5d438eSPaul Traina 		F_SET(t, B_MODIFIED);
11958f0484fSRodney W. Grimes 	return (status);
12058f0484fSRodney W. Grimes }
12158f0484fSRodney W. Grimes 
12258f0484fSRodney W. Grimes /*
123ef5d438eSPaul Traina  * __bt_stkacq --
124ef5d438eSPaul Traina  *	Acquire a stack so we can delete a cursor entry.
12558f0484fSRodney W. Grimes  *
12658f0484fSRodney W. Grimes  * Parameters:
127ef5d438eSPaul Traina  *	  t:	tree
128ef5d438eSPaul Traina  *	 hp:	pointer to current, pinned PAGE pointer
129ef5d438eSPaul Traina  *	  c:	pointer to the cursor
130ef5d438eSPaul Traina  *
131ef5d438eSPaul Traina  * Returns:
132ef5d438eSPaul Traina  *	0 on success, 1 on failure
133ef5d438eSPaul Traina  */
134ef5d438eSPaul Traina static int
__bt_stkacq(BTREE * t,PAGE ** hp,CURSOR * c)1350ac22237SXin LI __bt_stkacq(BTREE *t, PAGE **hp, CURSOR *c)
136ef5d438eSPaul Traina {
137ef5d438eSPaul Traina 	BINTERNAL *bi;
138ef5d438eSPaul Traina 	EPG *e;
139ef5d438eSPaul Traina 	EPGNO *parent;
140ef5d438eSPaul Traina 	PAGE *h;
1414c66e4b6SXin LI 	indx_t idx;
142ef5d438eSPaul Traina 	pgno_t pgno;
143ef5d438eSPaul Traina 	recno_t nextpg, prevpg;
144ef5d438eSPaul Traina 	int exact, level;
145ef5d438eSPaul Traina 
146ef5d438eSPaul Traina 	/*
147ef5d438eSPaul Traina 	 * Find the first occurrence of the key in the tree.  Toss the
148ef5d438eSPaul Traina 	 * currently locked page so we don't hit an already-locked page.
149ef5d438eSPaul Traina 	 */
150ef5d438eSPaul Traina 	h = *hp;
151ef5d438eSPaul Traina 	mpool_put(t->bt_mp, h, 0);
152ef5d438eSPaul Traina 	if ((e = __bt_search(t, &c->key, &exact)) == NULL)
153ef5d438eSPaul Traina 		return (1);
154ef5d438eSPaul Traina 	h = e->page;
155ef5d438eSPaul Traina 
156ef5d438eSPaul Traina 	/* See if we got it in one shot. */
157ef5d438eSPaul Traina 	if (h->pgno == c->pg.pgno)
158ef5d438eSPaul Traina 		goto ret;
159ef5d438eSPaul Traina 
160ef5d438eSPaul Traina 	/*
161ef5d438eSPaul Traina 	 * Move right, looking for the page.  At each move we have to move
162ef5d438eSPaul Traina 	 * up the stack until we don't have to move to the next page.  If
163ef5d438eSPaul Traina 	 * we have to change pages at an internal level, we have to fix the
164ef5d438eSPaul Traina 	 * stack back up.
165ef5d438eSPaul Traina 	 */
166ef5d438eSPaul Traina 	while (h->pgno != c->pg.pgno) {
167ef5d438eSPaul Traina 		if ((nextpg = h->nextpg) == P_INVALID)
168ef5d438eSPaul Traina 			break;
169ef5d438eSPaul Traina 		mpool_put(t->bt_mp, h, 0);
170ef5d438eSPaul Traina 
171ef5d438eSPaul Traina 		/* Move up the stack. */
172ef5d438eSPaul Traina 		for (level = 0; (parent = BT_POP(t)) != NULL; ++level) {
173ef5d438eSPaul Traina 			/* Get the parent page. */
174ef5d438eSPaul Traina 			if ((h = mpool_get(t->bt_mp, parent->pgno, 0)) == NULL)
175ef5d438eSPaul Traina 				return (1);
176ef5d438eSPaul Traina 
177ef5d438eSPaul Traina 			/* Move to the next index. */
178ef5d438eSPaul Traina 			if (parent->index != NEXTINDEX(h) - 1) {
1794c66e4b6SXin LI 				idx = parent->index + 1;
1804c66e4b6SXin LI 				BT_PUSH(t, h->pgno, idx);
181ef5d438eSPaul Traina 				break;
182ef5d438eSPaul Traina 			}
183ef5d438eSPaul Traina 			mpool_put(t->bt_mp, h, 0);
184ef5d438eSPaul Traina 		}
185ef5d438eSPaul Traina 
186ef5d438eSPaul Traina 		/* Restore the stack. */
187ef5d438eSPaul Traina 		while (level--) {
188ef5d438eSPaul Traina 			/* Push the next level down onto the stack. */
1894c66e4b6SXin LI 			bi = GETBINTERNAL(h, idx);
190ef5d438eSPaul Traina 			pgno = bi->pgno;
191ef5d438eSPaul Traina 			BT_PUSH(t, pgno, 0);
192ef5d438eSPaul Traina 
193ef5d438eSPaul Traina 			/* Lose the currently pinned page. */
194ef5d438eSPaul Traina 			mpool_put(t->bt_mp, h, 0);
195ef5d438eSPaul Traina 
196ef5d438eSPaul Traina 			/* Get the next level down. */
197ef5d438eSPaul Traina 			if ((h = mpool_get(t->bt_mp, pgno, 0)) == NULL)
198ef5d438eSPaul Traina 				return (1);
1994c66e4b6SXin LI 			idx = 0;
200ef5d438eSPaul Traina 		}
201ef5d438eSPaul Traina 		mpool_put(t->bt_mp, h, 0);
202ef5d438eSPaul Traina 		if ((h = mpool_get(t->bt_mp, nextpg, 0)) == NULL)
203ef5d438eSPaul Traina 			return (1);
204ef5d438eSPaul Traina 	}
205ef5d438eSPaul Traina 
206ef5d438eSPaul Traina 	if (h->pgno == c->pg.pgno)
207ef5d438eSPaul Traina 		goto ret;
208ef5d438eSPaul Traina 
209ef5d438eSPaul Traina 	/* Reacquire the original stack. */
210ef5d438eSPaul Traina 	mpool_put(t->bt_mp, h, 0);
211ef5d438eSPaul Traina 	if ((e = __bt_search(t, &c->key, &exact)) == NULL)
212ef5d438eSPaul Traina 		return (1);
213ef5d438eSPaul Traina 	h = e->page;
214ef5d438eSPaul Traina 
215ef5d438eSPaul Traina 	/*
216ef5d438eSPaul Traina 	 * Move left, looking for the page.  At each move we have to move
217ef5d438eSPaul Traina 	 * up the stack until we don't have to change pages to move to the
218ef5d438eSPaul Traina 	 * next page.  If we have to change pages at an internal level, we
219ef5d438eSPaul Traina 	 * have to fix the stack back up.
220ef5d438eSPaul Traina 	 */
221ef5d438eSPaul Traina 	while (h->pgno != c->pg.pgno) {
222ef5d438eSPaul Traina 		if ((prevpg = h->prevpg) == P_INVALID)
223ef5d438eSPaul Traina 			break;
224ef5d438eSPaul Traina 		mpool_put(t->bt_mp, h, 0);
225ef5d438eSPaul Traina 
226ef5d438eSPaul Traina 		/* Move up the stack. */
227ef5d438eSPaul Traina 		for (level = 0; (parent = BT_POP(t)) != NULL; ++level) {
228ef5d438eSPaul Traina 			/* Get the parent page. */
229ef5d438eSPaul Traina 			if ((h = mpool_get(t->bt_mp, parent->pgno, 0)) == NULL)
230ef5d438eSPaul Traina 				return (1);
231ef5d438eSPaul Traina 
232ef5d438eSPaul Traina 			/* Move to the next index. */
233ef5d438eSPaul Traina 			if (parent->index != 0) {
2344c66e4b6SXin LI 				idx = parent->index - 1;
2354c66e4b6SXin LI 				BT_PUSH(t, h->pgno, idx);
236ef5d438eSPaul Traina 				break;
237ef5d438eSPaul Traina 			}
238ef5d438eSPaul Traina 			mpool_put(t->bt_mp, h, 0);
239ef5d438eSPaul Traina 		}
240ef5d438eSPaul Traina 
241ef5d438eSPaul Traina 		/* Restore the stack. */
242ef5d438eSPaul Traina 		while (level--) {
243ef5d438eSPaul Traina 			/* Push the next level down onto the stack. */
2444c66e4b6SXin LI 			bi = GETBINTERNAL(h, idx);
245ef5d438eSPaul Traina 			pgno = bi->pgno;
246ef5d438eSPaul Traina 
247ef5d438eSPaul Traina 			/* Lose the currently pinned page. */
248ef5d438eSPaul Traina 			mpool_put(t->bt_mp, h, 0);
249ef5d438eSPaul Traina 
250ef5d438eSPaul Traina 			/* Get the next level down. */
251ef5d438eSPaul Traina 			if ((h = mpool_get(t->bt_mp, pgno, 0)) == NULL)
252ef5d438eSPaul Traina 				return (1);
253ef5d438eSPaul Traina 
2544c66e4b6SXin LI 			idx = NEXTINDEX(h) - 1;
2554c66e4b6SXin LI 			BT_PUSH(t, pgno, idx);
256ef5d438eSPaul Traina 		}
257ef5d438eSPaul Traina 		mpool_put(t->bt_mp, h, 0);
258ef5d438eSPaul Traina 		if ((h = mpool_get(t->bt_mp, prevpg, 0)) == NULL)
259ef5d438eSPaul Traina 			return (1);
260ef5d438eSPaul Traina 	}
261ef5d438eSPaul Traina 
262ef5d438eSPaul Traina 
263ef5d438eSPaul Traina ret:	mpool_put(t->bt_mp, h, 0);
264ef5d438eSPaul Traina 	return ((*hp = mpool_get(t->bt_mp, c->pg.pgno, 0)) == NULL);
265ef5d438eSPaul Traina }
266ef5d438eSPaul Traina 
267ef5d438eSPaul Traina /*
268ef5d438eSPaul Traina  * __bt_bdelete --
269ef5d438eSPaul Traina  *	Delete all key/data pairs matching the specified key.
270ef5d438eSPaul Traina  *
271ef5d438eSPaul Traina  * Parameters:
272ef5d438eSPaul Traina  *	  t:	tree
27358f0484fSRodney W. Grimes  *	key:	key to delete
27458f0484fSRodney W. Grimes  *
27558f0484fSRodney W. Grimes  * Returns:
27658f0484fSRodney W. Grimes  *	RET_ERROR, RET_SUCCESS and RET_SPECIAL if the key not found.
27758f0484fSRodney W. Grimes  */
27858f0484fSRodney W. Grimes static int
__bt_bdelete(BTREE * t,const DBT * key)2790ac22237SXin LI __bt_bdelete(BTREE *t, const DBT *key)
28058f0484fSRodney W. Grimes {
281ef5d438eSPaul Traina 	EPG *e;
28258f0484fSRodney W. Grimes 	PAGE *h;
283ef5d438eSPaul Traina 	int deleted, exact, redo;
284ef5d438eSPaul Traina 
285ef5d438eSPaul Traina 	deleted = 0;
28658f0484fSRodney W. Grimes 
28758f0484fSRodney W. Grimes 	/* Find any matching record; __bt_search pins the page. */
288ef5d438eSPaul Traina loop:	if ((e = __bt_search(t, key, &exact)) == NULL)
289ef5d438eSPaul Traina 		return (deleted ? RET_SUCCESS : RET_ERROR);
29058f0484fSRodney W. Grimes 	if (!exact) {
29158f0484fSRodney W. Grimes 		mpool_put(t->bt_mp, e->page, 0);
29258f0484fSRodney W. Grimes 		return (deleted ? RET_SUCCESS : RET_SPECIAL);
29358f0484fSRodney W. Grimes 	}
29458f0484fSRodney W. Grimes 
29558f0484fSRodney W. Grimes 	/*
296ef5d438eSPaul Traina 	 * Delete forward, then delete backward, from the found key.  If
297ef5d438eSPaul Traina 	 * there are duplicates and we reach either side of the page, do
298ef5d438eSPaul Traina 	 * the key search again, so that we get them all.
299ef5d438eSPaul Traina 	 */
300ef5d438eSPaul Traina 	redo = 0;
301ef5d438eSPaul Traina 	h = e->page;
302ef5d438eSPaul Traina 	do {
303ef5d438eSPaul Traina 		if (__bt_dleaf(t, key, h, e->index)) {
304ef5d438eSPaul Traina 			mpool_put(t->bt_mp, h, 0);
305ef5d438eSPaul Traina 			return (RET_ERROR);
306ef5d438eSPaul Traina 		}
307ef5d438eSPaul Traina 		if (F_ISSET(t, B_NODUPS)) {
308ef5d438eSPaul Traina 			if (NEXTINDEX(h) == 0) {
309ef5d438eSPaul Traina 				if (__bt_pdelete(t, h))
310ef5d438eSPaul Traina 					return (RET_ERROR);
311ef5d438eSPaul Traina 			} else
312ef5d438eSPaul Traina 				mpool_put(t->bt_mp, h, MPOOL_DIRTY);
313ef5d438eSPaul Traina 			return (RET_SUCCESS);
314ef5d438eSPaul Traina 		}
315ef5d438eSPaul Traina 		deleted = 1;
316ef5d438eSPaul Traina 	} while (e->index < NEXTINDEX(h) && __bt_cmp(t, key, e) == 0);
317ef5d438eSPaul Traina 
318ef5d438eSPaul Traina 	/* Check for right-hand edge of the page. */
319ef5d438eSPaul Traina 	if (e->index == NEXTINDEX(h))
320ef5d438eSPaul Traina 		redo = 1;
321ef5d438eSPaul Traina 
322ef5d438eSPaul Traina 	/* Delete from the key to the beginning of the page. */
323ef5d438eSPaul Traina 	while (e->index-- > 0) {
324ef5d438eSPaul Traina 		if (__bt_cmp(t, key, e) != 0)
325ef5d438eSPaul Traina 			break;
326ef5d438eSPaul Traina 		if (__bt_dleaf(t, key, h, e->index) == RET_ERROR) {
327ef5d438eSPaul Traina 			mpool_put(t->bt_mp, h, 0);
328ef5d438eSPaul Traina 			return (RET_ERROR);
329ef5d438eSPaul Traina 		}
330ef5d438eSPaul Traina 		if (e->index == 0)
331ef5d438eSPaul Traina 			redo = 1;
332ef5d438eSPaul Traina 	}
333ef5d438eSPaul Traina 
334ef5d438eSPaul Traina 	/* Check for an empty page. */
335ef5d438eSPaul Traina 	if (NEXTINDEX(h) == 0) {
336ef5d438eSPaul Traina 		if (__bt_pdelete(t, h))
337ef5d438eSPaul Traina 			return (RET_ERROR);
338ef5d438eSPaul Traina 		goto loop;
339ef5d438eSPaul Traina 	}
340ef5d438eSPaul Traina 
341ef5d438eSPaul Traina 	/* Put the page. */
342ef5d438eSPaul Traina 	mpool_put(t->bt_mp, h, MPOOL_DIRTY);
343ef5d438eSPaul Traina 
344ef5d438eSPaul Traina 	if (redo)
345ef5d438eSPaul Traina 		goto loop;
346ef5d438eSPaul Traina 	return (RET_SUCCESS);
347ef5d438eSPaul Traina }
348ef5d438eSPaul Traina 
349ef5d438eSPaul Traina /*
350ef5d438eSPaul Traina  * __bt_pdelete --
351ef5d438eSPaul Traina  *	Delete a single page from the tree.
35258f0484fSRodney W. Grimes  *
35358f0484fSRodney W. Grimes  * Parameters:
35458f0484fSRodney W. Grimes  *	t:	tree
355ef5d438eSPaul Traina  *	h:	leaf page
356ef5d438eSPaul Traina  *
357ef5d438eSPaul Traina  * Returns:
358ef5d438eSPaul Traina  *	RET_SUCCESS, RET_ERROR.
359ef5d438eSPaul Traina  *
360ef5d438eSPaul Traina  * Side-effects:
361ef5d438eSPaul Traina  *	mpool_put's the page
362ef5d438eSPaul Traina  */
363ef5d438eSPaul Traina static int
__bt_pdelete(BTREE * t,PAGE * h)3640ac22237SXin LI __bt_pdelete(BTREE *t, PAGE *h)
365ef5d438eSPaul Traina {
366ef5d438eSPaul Traina 	BINTERNAL *bi;
367ef5d438eSPaul Traina 	PAGE *pg;
368ef5d438eSPaul Traina 	EPGNO *parent;
3694c66e4b6SXin LI 	indx_t cnt, idx, *ip, offset;
370ef5d438eSPaul Traina 	u_int32_t nksize;
371ef5d438eSPaul Traina 	char *from;
372ef5d438eSPaul Traina 
373ef5d438eSPaul Traina 	/*
374ef5d438eSPaul Traina 	 * Walk the parent page stack -- a LIFO stack of the pages that were
375ef5d438eSPaul Traina 	 * traversed when we searched for the page where the delete occurred.
376ef5d438eSPaul Traina 	 * Each stack entry is a page number and a page index offset.  The
377ef5d438eSPaul Traina 	 * offset is for the page traversed on the search.  We've just deleted
378ef5d438eSPaul Traina 	 * a page, so we have to delete the key from the parent page.
379ef5d438eSPaul Traina 	 *
380ef5d438eSPaul Traina 	 * If the delete from the parent page makes it empty, this process may
381ef5d438eSPaul Traina 	 * continue all the way up the tree.  We stop if we reach the root page
382ef5d438eSPaul Traina 	 * (which is never deleted, it's just not worth the effort) or if the
383ef5d438eSPaul Traina 	 * delete does not empty the page.
384ef5d438eSPaul Traina 	 */
385ef5d438eSPaul Traina 	while ((parent = BT_POP(t)) != NULL) {
386ef5d438eSPaul Traina 		/* Get the parent page. */
387ef5d438eSPaul Traina 		if ((pg = mpool_get(t->bt_mp, parent->pgno, 0)) == NULL)
388ef5d438eSPaul Traina 			return (RET_ERROR);
389ef5d438eSPaul Traina 
3904c66e4b6SXin LI 		idx = parent->index;
3914c66e4b6SXin LI 		bi = GETBINTERNAL(pg, idx);
392ef5d438eSPaul Traina 
393ef5d438eSPaul Traina 		/* Free any overflow pages. */
394ef5d438eSPaul Traina 		if (bi->flags & P_BIGKEY &&
395ef5d438eSPaul Traina 		    __ovfl_delete(t, bi->bytes) == RET_ERROR) {
396ef5d438eSPaul Traina 			mpool_put(t->bt_mp, pg, 0);
397ef5d438eSPaul Traina 			return (RET_ERROR);
398ef5d438eSPaul Traina 		}
399ef5d438eSPaul Traina 
400ef5d438eSPaul Traina 		/*
401ef5d438eSPaul Traina 		 * Free the parent if it has only the one key and it's not the
402ef5d438eSPaul Traina 		 * root page. If it's the rootpage, turn it back into an empty
403ef5d438eSPaul Traina 		 * leaf page.
404ef5d438eSPaul Traina 		 */
4053df1d934SXin LI 		if (NEXTINDEX(pg) == 1) {
406ef5d438eSPaul Traina 			if (pg->pgno == P_ROOT) {
407ef5d438eSPaul Traina 				pg->lower = BTDATAOFF;
408ef5d438eSPaul Traina 				pg->upper = t->bt_psize;
409ef5d438eSPaul Traina 				pg->flags = P_BLEAF;
410ef5d438eSPaul Traina 			} else {
411ef5d438eSPaul Traina 				if (__bt_relink(t, pg) || __bt_free(t, pg))
412ef5d438eSPaul Traina 					return (RET_ERROR);
413ef5d438eSPaul Traina 				continue;
414ef5d438eSPaul Traina 			}
4153df1d934SXin LI 		} else {
416ef5d438eSPaul Traina 			/* Pack remaining key items at the end of the page. */
417ef5d438eSPaul Traina 			nksize = NBINTERNAL(bi->ksize);
418ef5d438eSPaul Traina 			from = (char *)pg + pg->upper;
419ef5d438eSPaul Traina 			memmove(from + nksize, from, (char *)bi - from);
420ef5d438eSPaul Traina 			pg->upper += nksize;
421ef5d438eSPaul Traina 
422ef5d438eSPaul Traina 			/* Adjust indices' offsets, shift the indices down. */
4234c66e4b6SXin LI 			offset = pg->linp[idx];
4244c66e4b6SXin LI 			for (cnt = idx, ip = &pg->linp[0]; cnt--; ++ip)
425ef5d438eSPaul Traina 				if (ip[0] < offset)
426ef5d438eSPaul Traina 					ip[0] += nksize;
4274c66e4b6SXin LI 			for (cnt = NEXTINDEX(pg) - idx; --cnt; ++ip)
428ef5d438eSPaul Traina 				ip[0] = ip[1] < offset ? ip[1] + nksize : ip[1];
429ef5d438eSPaul Traina 			pg->lower -= sizeof(indx_t);
430ef5d438eSPaul Traina 		}
431ef5d438eSPaul Traina 
432ef5d438eSPaul Traina 		mpool_put(t->bt_mp, pg, MPOOL_DIRTY);
433ef5d438eSPaul Traina 		break;
434ef5d438eSPaul Traina 	}
435ef5d438eSPaul Traina 
436ef5d438eSPaul Traina 	/* Free the leaf page, as long as it wasn't the root. */
437ef5d438eSPaul Traina 	if (h->pgno == P_ROOT) {
438ef5d438eSPaul Traina 		mpool_put(t->bt_mp, h, MPOOL_DIRTY);
439ef5d438eSPaul Traina 		return (RET_SUCCESS);
440ef5d438eSPaul Traina 	}
441ef5d438eSPaul Traina 	return (__bt_relink(t, h) || __bt_free(t, h));
442ef5d438eSPaul Traina }
443ef5d438eSPaul Traina 
444ef5d438eSPaul Traina /*
445ef5d438eSPaul Traina  * __bt_dleaf --
446ef5d438eSPaul Traina  *	Delete a single record from a leaf page.
447ef5d438eSPaul Traina  *
448ef5d438eSPaul Traina  * Parameters:
449ef5d438eSPaul Traina  *	t:	tree
450ef5d438eSPaul Traina  *    key:	referenced key
451ef5d438eSPaul Traina  *	h:	page
4524c66e4b6SXin LI  *	idx:	index on page to delete
45358f0484fSRodney W. Grimes  *
45458f0484fSRodney W. Grimes  * Returns:
45558f0484fSRodney W. Grimes  *	RET_SUCCESS, RET_ERROR.
45658f0484fSRodney W. Grimes  */
45758f0484fSRodney W. Grimes int
__bt_dleaf(BTREE * t,const DBT * key,PAGE * h,u_int idx)4584c66e4b6SXin LI __bt_dleaf(BTREE *t, const DBT *key, PAGE *h, u_int idx)
45958f0484fSRodney W. Grimes {
460ef5d438eSPaul Traina 	BLEAF *bl;
461ef5d438eSPaul Traina 	indx_t cnt, *ip, offset;
462ef5d438eSPaul Traina 	u_int32_t nbytes;
46358f0484fSRodney W. Grimes 	void *to;
464ef5d438eSPaul Traina 	char *from;
46558f0484fSRodney W. Grimes 
466ef5d438eSPaul Traina 	/* If this record is referenced by the cursor, delete the cursor. */
467ef5d438eSPaul Traina 	if (F_ISSET(&t->bt_cursor, CURS_INIT) &&
468ef5d438eSPaul Traina 	    !F_ISSET(&t->bt_cursor, CURS_ACQUIRE) &&
4694c66e4b6SXin LI 	    t->bt_cursor.pg.pgno == h->pgno && t->bt_cursor.pg.index == idx &&
4704c66e4b6SXin LI 	    __bt_curdel(t, key, h, idx))
471ef5d438eSPaul Traina 		return (RET_ERROR);
472ef5d438eSPaul Traina 
473ef5d438eSPaul Traina 	/* If the entry uses overflow pages, make them available for reuse. */
4744c66e4b6SXin LI 	to = bl = GETBLEAF(h, idx);
47558f0484fSRodney W. Grimes 	if (bl->flags & P_BIGKEY && __ovfl_delete(t, bl->bytes) == RET_ERROR)
47658f0484fSRodney W. Grimes 		return (RET_ERROR);
47758f0484fSRodney W. Grimes 	if (bl->flags & P_BIGDATA &&
47858f0484fSRodney W. Grimes 	    __ovfl_delete(t, bl->bytes + bl->ksize) == RET_ERROR)
47958f0484fSRodney W. Grimes 		return (RET_ERROR);
48058f0484fSRodney W. Grimes 
481ef5d438eSPaul Traina 	/* Pack the remaining key/data items at the end of the page. */
482ef5d438eSPaul Traina 	nbytes = NBLEAF(bl);
48358f0484fSRodney W. Grimes 	from = (char *)h + h->upper;
48458f0484fSRodney W. Grimes 	memmove(from + nbytes, from, (char *)to - from);
48558f0484fSRodney W. Grimes 	h->upper += nbytes;
48658f0484fSRodney W. Grimes 
487ef5d438eSPaul Traina 	/* Adjust the indices' offsets, shift the indices down. */
4884c66e4b6SXin LI 	offset = h->linp[idx];
4894c66e4b6SXin LI 	for (cnt = idx, ip = &h->linp[0]; cnt--; ++ip)
49058f0484fSRodney W. Grimes 		if (ip[0] < offset)
49158f0484fSRodney W. Grimes 			ip[0] += nbytes;
4924c66e4b6SXin LI 	for (cnt = NEXTINDEX(h) - idx; --cnt; ++ip)
49358f0484fSRodney W. Grimes 		ip[0] = ip[1] < offset ? ip[1] + nbytes : ip[1];
49458f0484fSRodney W. Grimes 	h->lower -= sizeof(indx_t);
495ef5d438eSPaul Traina 
496ef5d438eSPaul Traina 	/* If the cursor is on this page, adjust it as necessary. */
497ef5d438eSPaul Traina 	if (F_ISSET(&t->bt_cursor, CURS_INIT) &&
498ef5d438eSPaul Traina 	    !F_ISSET(&t->bt_cursor, CURS_ACQUIRE) &&
4994c66e4b6SXin LI 	    t->bt_cursor.pg.pgno == h->pgno && t->bt_cursor.pg.index > idx)
500ef5d438eSPaul Traina 		--t->bt_cursor.pg.index;
501ef5d438eSPaul Traina 
50258f0484fSRodney W. Grimes 	return (RET_SUCCESS);
50358f0484fSRodney W. Grimes }
504ef5d438eSPaul Traina 
505ef5d438eSPaul Traina /*
506ef5d438eSPaul Traina  * __bt_curdel --
507ef5d438eSPaul Traina  *	Delete the cursor.
508ef5d438eSPaul Traina  *
509ef5d438eSPaul Traina  * Parameters:
510ef5d438eSPaul Traina  *	t:	tree
511ef5d438eSPaul Traina  *    key:	referenced key (or NULL)
512ef5d438eSPaul Traina  *	h:	page
5134c66e4b6SXin LI  *    idx:	index on page to delete
514ef5d438eSPaul Traina  *
515ef5d438eSPaul Traina  * Returns:
516ef5d438eSPaul Traina  *	RET_SUCCESS, RET_ERROR.
517ef5d438eSPaul Traina  */
518ef5d438eSPaul Traina static int
__bt_curdel(BTREE * t,const DBT * key,PAGE * h,u_int idx)5194c66e4b6SXin LI __bt_curdel(BTREE *t, const DBT *key, PAGE *h, u_int idx)
520ef5d438eSPaul Traina {
521ef5d438eSPaul Traina 	CURSOR *c;
522ef5d438eSPaul Traina 	EPG e;
523ef5d438eSPaul Traina 	PAGE *pg;
524ef5d438eSPaul Traina 	int curcopy, status;
525ef5d438eSPaul Traina 
526ef5d438eSPaul Traina 	/*
527ef5d438eSPaul Traina 	 * If there are duplicates, move forward or backward to one.
528ef5d438eSPaul Traina 	 * Otherwise, copy the key into the cursor area.
529ef5d438eSPaul Traina 	 */
530ef5d438eSPaul Traina 	c = &t->bt_cursor;
531ef5d438eSPaul Traina 	F_CLR(c, CURS_AFTER | CURS_BEFORE | CURS_ACQUIRE);
532ef5d438eSPaul Traina 
533ef5d438eSPaul Traina 	curcopy = 0;
534ef5d438eSPaul Traina 	if (!F_ISSET(t, B_NODUPS)) {
535ef5d438eSPaul Traina 		/*
536ef5d438eSPaul Traina 		 * We're going to have to do comparisons.  If we weren't
537ef5d438eSPaul Traina 		 * provided a copy of the key, i.e. the user is deleting
538ef5d438eSPaul Traina 		 * the current cursor position, get one.
539ef5d438eSPaul Traina 		 */
540ef5d438eSPaul Traina 		if (key == NULL) {
541ef5d438eSPaul Traina 			e.page = h;
5424c66e4b6SXin LI 			e.index = idx;
543ef5d438eSPaul Traina 			if ((status = __bt_ret(t, &e,
544ef5d438eSPaul Traina 			    &c->key, &c->key, NULL, NULL, 1)) != RET_SUCCESS)
545ef5d438eSPaul Traina 				return (status);
546ef5d438eSPaul Traina 			curcopy = 1;
547ef5d438eSPaul Traina 			key = &c->key;
548ef5d438eSPaul Traina 		}
549ef5d438eSPaul Traina 		/* Check previous key, if not at the beginning of the page. */
5504c66e4b6SXin LI 		if (idx > 0) {
551ef5d438eSPaul Traina 			e.page = h;
5524c66e4b6SXin LI 			e.index = idx - 1;
553ef5d438eSPaul Traina 			if (__bt_cmp(t, key, &e) == 0) {
554ef5d438eSPaul Traina 				F_SET(c, CURS_BEFORE);
555ef5d438eSPaul Traina 				goto dup2;
556ef5d438eSPaul Traina 			}
557ef5d438eSPaul Traina 		}
558ef5d438eSPaul Traina 		/* Check next key, if not at the end of the page. */
5594c66e4b6SXin LI 		if (idx < NEXTINDEX(h) - 1) {
560ef5d438eSPaul Traina 			e.page = h;
5614c66e4b6SXin LI 			e.index = idx + 1;
562ef5d438eSPaul Traina 			if (__bt_cmp(t, key, &e) == 0) {
563ef5d438eSPaul Traina 				F_SET(c, CURS_AFTER);
564ef5d438eSPaul Traina 				goto dup2;
565ef5d438eSPaul Traina 			}
566ef5d438eSPaul Traina 		}
567ef5d438eSPaul Traina 		/* Check previous key if at the beginning of the page. */
5684c66e4b6SXin LI 		if (idx == 0 && h->prevpg != P_INVALID) {
569ef5d438eSPaul Traina 			if ((pg = mpool_get(t->bt_mp, h->prevpg, 0)) == NULL)
570ef5d438eSPaul Traina 				return (RET_ERROR);
571ef5d438eSPaul Traina 			e.page = pg;
572ef5d438eSPaul Traina 			e.index = NEXTINDEX(pg) - 1;
573ef5d438eSPaul Traina 			if (__bt_cmp(t, key, &e) == 0) {
574ef5d438eSPaul Traina 				F_SET(c, CURS_BEFORE);
575ef5d438eSPaul Traina 				goto dup1;
576ef5d438eSPaul Traina 			}
577ef5d438eSPaul Traina 			mpool_put(t->bt_mp, pg, 0);
578ef5d438eSPaul Traina 		}
579ef5d438eSPaul Traina 		/* Check next key if at the end of the page. */
5804c66e4b6SXin LI 		if (idx == NEXTINDEX(h) - 1 && h->nextpg != P_INVALID) {
581ef5d438eSPaul Traina 			if ((pg = mpool_get(t->bt_mp, h->nextpg, 0)) == NULL)
582ef5d438eSPaul Traina 				return (RET_ERROR);
583ef5d438eSPaul Traina 			e.page = pg;
584ef5d438eSPaul Traina 			e.index = 0;
585ef5d438eSPaul Traina 			if (__bt_cmp(t, key, &e) == 0) {
586ef5d438eSPaul Traina 				F_SET(c, CURS_AFTER);
587ef5d438eSPaul Traina dup1:				mpool_put(t->bt_mp, pg, 0);
588ef5d438eSPaul Traina dup2:				c->pg.pgno = e.page->pgno;
589ef5d438eSPaul Traina 				c->pg.index = e.index;
590ef5d438eSPaul Traina 				return (RET_SUCCESS);
591ef5d438eSPaul Traina 			}
592ef5d438eSPaul Traina 			mpool_put(t->bt_mp, pg, 0);
593ef5d438eSPaul Traina 		}
594ef5d438eSPaul Traina 	}
595ef5d438eSPaul Traina 	e.page = h;
5964c66e4b6SXin LI 	e.index = idx;
597ef5d438eSPaul Traina 	if (curcopy || (status =
598ef5d438eSPaul Traina 	    __bt_ret(t, &e, &c->key, &c->key, NULL, NULL, 1)) == RET_SUCCESS) {
599ef5d438eSPaul Traina 		F_SET(c, CURS_ACQUIRE);
600ef5d438eSPaul Traina 		return (RET_SUCCESS);
601ef5d438eSPaul Traina 	}
602ef5d438eSPaul Traina 	return (status);
603ef5d438eSPaul Traina }
604ef5d438eSPaul Traina 
605ef5d438eSPaul Traina /*
606ef5d438eSPaul Traina  * __bt_relink --
607ef5d438eSPaul Traina  *	Link around a deleted page.
608ef5d438eSPaul Traina  *
609ef5d438eSPaul Traina  * Parameters:
610ef5d438eSPaul Traina  *	t:	tree
611ef5d438eSPaul Traina  *	h:	page to be deleted
612ef5d438eSPaul Traina  */
613ef5d438eSPaul Traina static int
__bt_relink(BTREE * t,PAGE * h)6140ac22237SXin LI __bt_relink(BTREE *t, PAGE *h)
615ef5d438eSPaul Traina {
616ef5d438eSPaul Traina 	PAGE *pg;
617ef5d438eSPaul Traina 
618ef5d438eSPaul Traina 	if (h->nextpg != P_INVALID) {
619ef5d438eSPaul Traina 		if ((pg = mpool_get(t->bt_mp, h->nextpg, 0)) == NULL)
620ef5d438eSPaul Traina 			return (RET_ERROR);
621ef5d438eSPaul Traina 		pg->prevpg = h->prevpg;
622ef5d438eSPaul Traina 		mpool_put(t->bt_mp, pg, MPOOL_DIRTY);
623ef5d438eSPaul Traina 	}
624ef5d438eSPaul Traina 	if (h->prevpg != P_INVALID) {
625ef5d438eSPaul Traina 		if ((pg = mpool_get(t->bt_mp, h->prevpg, 0)) == NULL)
626ef5d438eSPaul Traina 			return (RET_ERROR);
627ef5d438eSPaul Traina 		pg->nextpg = h->nextpg;
628ef5d438eSPaul Traina 		mpool_put(t->bt_mp, pg, MPOOL_DIRTY);
629ef5d438eSPaul Traina 	}
630ef5d438eSPaul Traina 	return (0);
631ef5d438eSPaul Traina }
632