xref: /minix3/lib/libc/db/btree/bt_delete.c (revision 2fe8fb192fe7e8720e3e7a77f928da545e872a6a)
1*2639ae9bSBen Gras /*	$NetBSD: bt_delete.c,v 1.17 2009/01/29 02:02:36 lukem Exp $	*/
2*2639ae9bSBen Gras 
3*2639ae9bSBen Gras /*-
4*2639ae9bSBen Gras  * Copyright (c) 1990, 1993, 1994
5*2639ae9bSBen Gras  *	The Regents of the University of California.  All rights reserved.
6*2639ae9bSBen Gras  *
7*2639ae9bSBen Gras  * This code is derived from software contributed to Berkeley by
8*2639ae9bSBen Gras  * Mike Olson.
9*2639ae9bSBen Gras  *
10*2639ae9bSBen Gras  * Redistribution and use in source and binary forms, with or without
11*2639ae9bSBen Gras  * modification, are permitted provided that the following conditions
12*2639ae9bSBen Gras  * are met:
13*2639ae9bSBen Gras  * 1. Redistributions of source code must retain the above copyright
14*2639ae9bSBen Gras  *    notice, this list of conditions and the following disclaimer.
15*2639ae9bSBen Gras  * 2. Redistributions in binary form must reproduce the above copyright
16*2639ae9bSBen Gras  *    notice, this list of conditions and the following disclaimer in the
17*2639ae9bSBen Gras  *    documentation and/or other materials provided with the distribution.
18*2639ae9bSBen Gras  * 3. Neither the name of the University nor the names of its contributors
19*2639ae9bSBen Gras  *    may be used to endorse or promote products derived from this software
20*2639ae9bSBen Gras  *    without specific prior written permission.
21*2639ae9bSBen Gras  *
22*2639ae9bSBen Gras  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23*2639ae9bSBen Gras  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24*2639ae9bSBen Gras  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25*2639ae9bSBen Gras  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26*2639ae9bSBen Gras  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27*2639ae9bSBen Gras  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28*2639ae9bSBen Gras  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29*2639ae9bSBen Gras  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30*2639ae9bSBen Gras  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31*2639ae9bSBen Gras  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32*2639ae9bSBen Gras  * SUCH DAMAGE.
33*2639ae9bSBen Gras  */
34*2639ae9bSBen Gras 
35*2639ae9bSBen Gras #if HAVE_NBTOOL_CONFIG_H
36*2639ae9bSBen Gras #include "nbtool_config.h"
37*2639ae9bSBen Gras #endif
38*2639ae9bSBen Gras 
39*2639ae9bSBen Gras #include <sys/cdefs.h>
40*2639ae9bSBen Gras __RCSID("$NetBSD: bt_delete.c,v 1.17 2009/01/29 02:02:36 lukem Exp $");
41*2639ae9bSBen Gras 
42*2639ae9bSBen Gras #include "namespace.h"
43*2639ae9bSBen Gras #include <sys/types.h>
44*2639ae9bSBen Gras 
45*2639ae9bSBen Gras #include <assert.h>
46*2639ae9bSBen Gras #include <errno.h>
47*2639ae9bSBen Gras #include <stdio.h>
48*2639ae9bSBen Gras #include <string.h>
49*2639ae9bSBen Gras 
50*2639ae9bSBen Gras #include <db.h>
51*2639ae9bSBen Gras #include "btree.h"
52*2639ae9bSBen Gras 
53*2639ae9bSBen Gras static int __bt_bdelete(BTREE *, const DBT *);
54*2639ae9bSBen Gras static int __bt_curdel(BTREE *, const DBT *, PAGE *, u_int);
55*2639ae9bSBen Gras static int __bt_pdelete(BTREE *, PAGE *);
56*2639ae9bSBen Gras static int __bt_relink(BTREE *, PAGE *);
57*2639ae9bSBen Gras static int __bt_stkacq(BTREE *, PAGE **, CURSOR *);
58*2639ae9bSBen Gras 
59*2639ae9bSBen Gras /*
60*2639ae9bSBen Gras  * __bt_delete
61*2639ae9bSBen Gras  *	Delete the item(s) referenced by a key.
62*2639ae9bSBen Gras  *
63*2639ae9bSBen Gras  * Return RET_SPECIAL if the key is not found.
64*2639ae9bSBen Gras  */
65*2639ae9bSBen Gras int
__bt_delete(const DB * dbp,const DBT * key,u_int flags)66*2639ae9bSBen Gras __bt_delete(const DB *dbp, const DBT *key, u_int flags)
67*2639ae9bSBen Gras {
68*2639ae9bSBen Gras 	BTREE *t;
69*2639ae9bSBen Gras 	CURSOR *c;
70*2639ae9bSBen Gras 	PAGE *h;
71*2639ae9bSBen Gras 	int status;
72*2639ae9bSBen Gras 
73*2639ae9bSBen Gras 	t = dbp->internal;
74*2639ae9bSBen Gras 
75*2639ae9bSBen Gras 	/* Toss any page pinned across calls. */
76*2639ae9bSBen Gras 	if (t->bt_pinned != NULL) {
77*2639ae9bSBen Gras 		mpool_put(t->bt_mp, t->bt_pinned, 0);
78*2639ae9bSBen Gras 		t->bt_pinned = NULL;
79*2639ae9bSBen Gras 	}
80*2639ae9bSBen Gras 
81*2639ae9bSBen Gras 	/* Check for change to a read-only tree. */
82*2639ae9bSBen Gras 	if (F_ISSET(t, B_RDONLY)) {
83*2639ae9bSBen Gras 		errno = EPERM;
84*2639ae9bSBen Gras 		return (RET_ERROR);
85*2639ae9bSBen Gras 	}
86*2639ae9bSBen Gras 
87*2639ae9bSBen Gras 	switch (flags) {
88*2639ae9bSBen Gras 	case 0:
89*2639ae9bSBen Gras 		status = __bt_bdelete(t, key);
90*2639ae9bSBen Gras 		break;
91*2639ae9bSBen Gras 	case R_CURSOR:
92*2639ae9bSBen Gras 		/*
93*2639ae9bSBen Gras 		 * If flags is R_CURSOR, delete the cursor.  Must already
94*2639ae9bSBen Gras 		 * have started a scan and not have already deleted it.
95*2639ae9bSBen Gras 		 */
96*2639ae9bSBen Gras 		c = &t->bt_cursor;
97*2639ae9bSBen Gras 		if (F_ISSET(c, CURS_INIT)) {
98*2639ae9bSBen Gras 			if (F_ISSET(c, CURS_ACQUIRE | CURS_AFTER | CURS_BEFORE))
99*2639ae9bSBen Gras 				return (RET_SPECIAL);
100*2639ae9bSBen Gras 			if ((h = mpool_get(t->bt_mp, c->pg.pgno, 0)) == NULL)
101*2639ae9bSBen Gras 				return (RET_ERROR);
102*2639ae9bSBen Gras 
103*2639ae9bSBen Gras 			/*
104*2639ae9bSBen Gras 			 * If the page is about to be emptied, we'll need to
105*2639ae9bSBen Gras 			 * delete it, which means we have to acquire a stack.
106*2639ae9bSBen Gras 			 */
107*2639ae9bSBen Gras 			if (NEXTINDEX(h) == 1)
108*2639ae9bSBen Gras 				if (__bt_stkacq(t, &h, &t->bt_cursor))
109*2639ae9bSBen Gras 					return (RET_ERROR);
110*2639ae9bSBen Gras 
111*2639ae9bSBen Gras 			status = __bt_dleaf(t, NULL, h, (u_int)c->pg.index);
112*2639ae9bSBen Gras 
113*2639ae9bSBen Gras 			if (NEXTINDEX(h) == 0 && status == RET_SUCCESS) {
114*2639ae9bSBen Gras 				if (__bt_pdelete(t, h))
115*2639ae9bSBen Gras 					return (RET_ERROR);
116*2639ae9bSBen Gras 			} else
117*2639ae9bSBen Gras 				mpool_put(t->bt_mp, h,
118*2639ae9bSBen Gras 				    (u_int)(status == RET_SUCCESS ?
119*2639ae9bSBen Gras 				    MPOOL_DIRTY : 0));
120*2639ae9bSBen Gras 			break;
121*2639ae9bSBen Gras 		}
122*2639ae9bSBen Gras 		/* FALLTHROUGH */
123*2639ae9bSBen Gras 	default:
124*2639ae9bSBen Gras 		errno = EINVAL;
125*2639ae9bSBen Gras 		return (RET_ERROR);
126*2639ae9bSBen Gras 	}
127*2639ae9bSBen Gras 	if (status == RET_SUCCESS)
128*2639ae9bSBen Gras 		F_SET(t, B_MODIFIED);
129*2639ae9bSBen Gras 	return (status);
130*2639ae9bSBen Gras }
131*2639ae9bSBen Gras 
132*2639ae9bSBen Gras /*
133*2639ae9bSBen Gras  * __bt_stkacq --
134*2639ae9bSBen Gras  *	Acquire a stack so we can delete a cursor entry.
135*2639ae9bSBen Gras  *
136*2639ae9bSBen Gras  * Parameters:
137*2639ae9bSBen Gras  *	  t:	tree
138*2639ae9bSBen Gras  *	 hp:	pointer to current, pinned PAGE pointer
139*2639ae9bSBen Gras  *	  c:	pointer to the cursor
140*2639ae9bSBen Gras  *
141*2639ae9bSBen Gras  * Returns:
142*2639ae9bSBen Gras  *	0 on success, 1 on failure
143*2639ae9bSBen Gras  */
144*2639ae9bSBen Gras static int
__bt_stkacq(BTREE * t,PAGE ** hp,CURSOR * c)145*2639ae9bSBen Gras __bt_stkacq(BTREE *t, PAGE **hp, CURSOR *c)
146*2639ae9bSBen Gras {
147*2639ae9bSBen Gras 	BINTERNAL *bi;
148*2639ae9bSBen Gras 	EPG *e;
149*2639ae9bSBen Gras 	EPGNO *parent;
150*2639ae9bSBen Gras 	PAGE *h;
151*2639ae9bSBen Gras 	indx_t idx = 0;	/* Pacify gcc */
152*2639ae9bSBen Gras 	pgno_t pgno;
153*2639ae9bSBen Gras 	recno_t nextpg, prevpg;
154*2639ae9bSBen Gras 	int exact, level;
155*2639ae9bSBen Gras 
156*2639ae9bSBen Gras 	/*
157*2639ae9bSBen Gras 	 * Find the first occurrence of the key in the tree.  Toss the
158*2639ae9bSBen Gras 	 * currently locked page so we don't hit an already-locked page.
159*2639ae9bSBen Gras 	 */
160*2639ae9bSBen Gras 	h = *hp;
161*2639ae9bSBen Gras 	mpool_put(t->bt_mp, h, 0);
162*2639ae9bSBen Gras 	if ((e = __bt_search(t, &c->key, &exact)) == NULL)
163*2639ae9bSBen Gras 		return (1);
164*2639ae9bSBen Gras 	h = e->page;
165*2639ae9bSBen Gras 
166*2639ae9bSBen Gras 	/* See if we got it in one shot. */
167*2639ae9bSBen Gras 	if (h->pgno == c->pg.pgno)
168*2639ae9bSBen Gras 		goto ret;
169*2639ae9bSBen Gras 
170*2639ae9bSBen Gras 	/*
171*2639ae9bSBen Gras 	 * Move right, looking for the page.  At each move we have to move
172*2639ae9bSBen Gras 	 * up the stack until we don't have to move to the next page.  If
173*2639ae9bSBen Gras 	 * we have to change pages at an internal level, we have to fix the
174*2639ae9bSBen Gras 	 * stack back up.
175*2639ae9bSBen Gras 	 */
176*2639ae9bSBen Gras 	while (h->pgno != c->pg.pgno) {
177*2639ae9bSBen Gras 		if ((nextpg = h->nextpg) == P_INVALID)
178*2639ae9bSBen Gras 			break;
179*2639ae9bSBen Gras 		mpool_put(t->bt_mp, h, 0);
180*2639ae9bSBen Gras 
181*2639ae9bSBen Gras 		/* Move up the stack. */
182*2639ae9bSBen Gras 		for (level = 0; (parent = BT_POP(t)) != NULL; ++level) {
183*2639ae9bSBen Gras 			/* Get the parent page. */
184*2639ae9bSBen Gras 			if ((h = mpool_get(t->bt_mp, parent->pgno, 0)) == NULL)
185*2639ae9bSBen Gras 				return (1);
186*2639ae9bSBen Gras 
187*2639ae9bSBen Gras 			/* Move to the next index. */
188*2639ae9bSBen Gras 			if (parent->index != NEXTINDEX(h) - 1) {
189*2639ae9bSBen Gras 				idx = parent->index + 1;
190*2639ae9bSBen Gras 				BT_PUSH(t, h->pgno, idx);
191*2639ae9bSBen Gras 				break;
192*2639ae9bSBen Gras 			}
193*2639ae9bSBen Gras 			mpool_put(t->bt_mp, h, 0);
194*2639ae9bSBen Gras 		}
195*2639ae9bSBen Gras 
196*2639ae9bSBen Gras 		/* Restore the stack. */
197*2639ae9bSBen Gras 		while (level--) {
198*2639ae9bSBen Gras 			/* Push the next level down onto the stack. */
199*2639ae9bSBen Gras 			bi = GETBINTERNAL(h, idx);
200*2639ae9bSBen Gras 			pgno = bi->pgno;
201*2639ae9bSBen Gras 			BT_PUSH(t, pgno, 0);
202*2639ae9bSBen Gras 
203*2639ae9bSBen Gras 			/* Lose the currently pinned page. */
204*2639ae9bSBen Gras 			mpool_put(t->bt_mp, h, 0);
205*2639ae9bSBen Gras 
206*2639ae9bSBen Gras 			/* Get the next level down. */
207*2639ae9bSBen Gras 			if ((h = mpool_get(t->bt_mp, pgno, 0)) == NULL)
208*2639ae9bSBen Gras 				return (1);
209*2639ae9bSBen Gras 			idx = 0;
210*2639ae9bSBen Gras 		}
211*2639ae9bSBen Gras 		mpool_put(t->bt_mp, h, 0);
212*2639ae9bSBen Gras 		if ((h = mpool_get(t->bt_mp, nextpg, 0)) == NULL)
213*2639ae9bSBen Gras 			return (1);
214*2639ae9bSBen Gras 	}
215*2639ae9bSBen Gras 
216*2639ae9bSBen Gras 	if (h->pgno == c->pg.pgno)
217*2639ae9bSBen Gras 		goto ret;
218*2639ae9bSBen Gras 
219*2639ae9bSBen Gras 	/* Reacquire the original stack. */
220*2639ae9bSBen Gras 	mpool_put(t->bt_mp, h, 0);
221*2639ae9bSBen Gras 	if ((e = __bt_search(t, &c->key, &exact)) == NULL)
222*2639ae9bSBen Gras 		return (1);
223*2639ae9bSBen Gras 	h = e->page;
224*2639ae9bSBen Gras 
225*2639ae9bSBen Gras 	/*
226*2639ae9bSBen Gras 	 * Move left, looking for the page.  At each move we have to move
227*2639ae9bSBen Gras 	 * up the stack until we don't have to change pages to move to the
228*2639ae9bSBen Gras 	 * next page.  If we have to change pages at an internal level, we
229*2639ae9bSBen Gras 	 * have to fix the stack back up.
230*2639ae9bSBen Gras 	 */
231*2639ae9bSBen Gras 	while (h->pgno != c->pg.pgno) {
232*2639ae9bSBen Gras 		if ((prevpg = h->prevpg) == P_INVALID)
233*2639ae9bSBen Gras 			break;
234*2639ae9bSBen Gras 		mpool_put(t->bt_mp, h, 0);
235*2639ae9bSBen Gras 
236*2639ae9bSBen Gras 		/* Move up the stack. */
237*2639ae9bSBen Gras 		for (level = 0; (parent = BT_POP(t)) != NULL; ++level) {
238*2639ae9bSBen Gras 			/* Get the parent page. */
239*2639ae9bSBen Gras 			if ((h = mpool_get(t->bt_mp, parent->pgno, 0)) == NULL)
240*2639ae9bSBen Gras 				return (1);
241*2639ae9bSBen Gras 
242*2639ae9bSBen Gras 			/* Move to the next index. */
243*2639ae9bSBen Gras 			if (parent->index != 0) {
244*2639ae9bSBen Gras 				idx = parent->index - 1;
245*2639ae9bSBen Gras 				BT_PUSH(t, h->pgno, idx);
246*2639ae9bSBen Gras 				break;
247*2639ae9bSBen Gras 			}
248*2639ae9bSBen Gras 			mpool_put(t->bt_mp, h, 0);
249*2639ae9bSBen Gras 		}
250*2639ae9bSBen Gras 
251*2639ae9bSBen Gras 		/* Restore the stack. */
252*2639ae9bSBen Gras 		while (level--) {
253*2639ae9bSBen Gras 			/* Push the next level down onto the stack. */
254*2639ae9bSBen Gras 			bi = GETBINTERNAL(h, idx);
255*2639ae9bSBen Gras 			pgno = bi->pgno;
256*2639ae9bSBen Gras 
257*2639ae9bSBen Gras 			/* Lose the currently pinned page. */
258*2639ae9bSBen Gras 			mpool_put(t->bt_mp, h, 0);
259*2639ae9bSBen Gras 
260*2639ae9bSBen Gras 			/* Get the next level down. */
261*2639ae9bSBen Gras 			if ((h = mpool_get(t->bt_mp, pgno, 0)) == NULL)
262*2639ae9bSBen Gras 				return (1);
263*2639ae9bSBen Gras 
264*2639ae9bSBen Gras 			idx = NEXTINDEX(h) - 1;
265*2639ae9bSBen Gras 			BT_PUSH(t, pgno, idx);
266*2639ae9bSBen Gras 		}
267*2639ae9bSBen Gras 		mpool_put(t->bt_mp, h, 0);
268*2639ae9bSBen Gras 		if ((h = mpool_get(t->bt_mp, prevpg, 0)) == NULL)
269*2639ae9bSBen Gras 			return (1);
270*2639ae9bSBen Gras 	}
271*2639ae9bSBen Gras 
272*2639ae9bSBen Gras 
273*2639ae9bSBen Gras ret:	mpool_put(t->bt_mp, h, 0);
274*2639ae9bSBen Gras 	return ((*hp = mpool_get(t->bt_mp, c->pg.pgno, 0)) == NULL);
275*2639ae9bSBen Gras }
276*2639ae9bSBen Gras 
277*2639ae9bSBen Gras /*
278*2639ae9bSBen Gras  * __bt_bdelete --
279*2639ae9bSBen Gras  *	Delete all key/data pairs matching the specified key.
280*2639ae9bSBen Gras  *
281*2639ae9bSBen Gras  * Parameters:
282*2639ae9bSBen Gras  *	  t:	tree
283*2639ae9bSBen Gras  *	key:	key to delete
284*2639ae9bSBen Gras  *
285*2639ae9bSBen Gras  * Returns:
286*2639ae9bSBen Gras  *	RET_ERROR, RET_SUCCESS and RET_SPECIAL if the key not found.
287*2639ae9bSBen Gras  */
288*2639ae9bSBen Gras static int
__bt_bdelete(BTREE * t,const DBT * key)289*2639ae9bSBen Gras __bt_bdelete(BTREE *t, const DBT *key)
290*2639ae9bSBen Gras {
291*2639ae9bSBen Gras 	EPG *e;
292*2639ae9bSBen Gras 	PAGE *h;
293*2639ae9bSBen Gras 	int deleted, exact, redo;
294*2639ae9bSBen Gras 
295*2639ae9bSBen Gras 	deleted = 0;
296*2639ae9bSBen Gras 
297*2639ae9bSBen Gras 	/* Find any matching record; __bt_search pins the page. */
298*2639ae9bSBen Gras loop:	if ((e = __bt_search(t, key, &exact)) == NULL)
299*2639ae9bSBen Gras 		return (deleted ? RET_SUCCESS : RET_ERROR);
300*2639ae9bSBen Gras 	if (!exact) {
301*2639ae9bSBen Gras 		mpool_put(t->bt_mp, e->page, 0);
302*2639ae9bSBen Gras 		return (deleted ? RET_SUCCESS : RET_SPECIAL);
303*2639ae9bSBen Gras 	}
304*2639ae9bSBen Gras 
305*2639ae9bSBen Gras 	/*
306*2639ae9bSBen Gras 	 * Delete forward, then delete backward, from the found key.  If
307*2639ae9bSBen Gras 	 * there are duplicates and we reach either side of the page, do
308*2639ae9bSBen Gras 	 * the key search again, so that we get them all.
309*2639ae9bSBen Gras 	 */
310*2639ae9bSBen Gras 	redo = 0;
311*2639ae9bSBen Gras 	h = e->page;
312*2639ae9bSBen Gras 	do {
313*2639ae9bSBen Gras 		if (__bt_dleaf(t, key, h, (u_int)e->index)) {
314*2639ae9bSBen Gras 			mpool_put(t->bt_mp, h, 0);
315*2639ae9bSBen Gras 			return (RET_ERROR);
316*2639ae9bSBen Gras 		}
317*2639ae9bSBen Gras 		if (F_ISSET(t, B_NODUPS)) {
318*2639ae9bSBen Gras 			if (NEXTINDEX(h) == 0) {
319*2639ae9bSBen Gras 				if (__bt_pdelete(t, h))
320*2639ae9bSBen Gras 					return (RET_ERROR);
321*2639ae9bSBen Gras 			} else
322*2639ae9bSBen Gras 				mpool_put(t->bt_mp, h, MPOOL_DIRTY);
323*2639ae9bSBen Gras 			return (RET_SUCCESS);
324*2639ae9bSBen Gras 		}
325*2639ae9bSBen Gras 		deleted = 1;
326*2639ae9bSBen Gras 	} while (e->index < NEXTINDEX(h) && __bt_cmp(t, key, e) == 0);
327*2639ae9bSBen Gras 
328*2639ae9bSBen Gras 	/* Check for right-hand edge of the page. */
329*2639ae9bSBen Gras 	if (e->index == NEXTINDEX(h))
330*2639ae9bSBen Gras 		redo = 1;
331*2639ae9bSBen Gras 
332*2639ae9bSBen Gras 	/* Delete from the key to the beginning of the page. */
333*2639ae9bSBen Gras 	while (e->index-- > 0) {
334*2639ae9bSBen Gras 		if (__bt_cmp(t, key, e) != 0)
335*2639ae9bSBen Gras 			break;
336*2639ae9bSBen Gras 		if (__bt_dleaf(t, key, h, (u_int)e->index) == RET_ERROR) {
337*2639ae9bSBen Gras 			mpool_put(t->bt_mp, h, 0);
338*2639ae9bSBen Gras 			return (RET_ERROR);
339*2639ae9bSBen Gras 		}
340*2639ae9bSBen Gras 		if (e->index == 0)
341*2639ae9bSBen Gras 			redo = 1;
342*2639ae9bSBen Gras 	}
343*2639ae9bSBen Gras 
344*2639ae9bSBen Gras 	/* Check for an empty page. */
345*2639ae9bSBen Gras 	if (NEXTINDEX(h) == 0) {
346*2639ae9bSBen Gras 		if (__bt_pdelete(t, h))
347*2639ae9bSBen Gras 			return (RET_ERROR);
348*2639ae9bSBen Gras 		goto loop;
349*2639ae9bSBen Gras 	}
350*2639ae9bSBen Gras 
351*2639ae9bSBen Gras 	/* Put the page. */
352*2639ae9bSBen Gras 	mpool_put(t->bt_mp, h, MPOOL_DIRTY);
353*2639ae9bSBen Gras 
354*2639ae9bSBen Gras 	if (redo)
355*2639ae9bSBen Gras 		goto loop;
356*2639ae9bSBen Gras 	return (RET_SUCCESS);
357*2639ae9bSBen Gras }
358*2639ae9bSBen Gras 
359*2639ae9bSBen Gras /*
360*2639ae9bSBen Gras  * __bt_pdelete --
361*2639ae9bSBen Gras  *	Delete a single page from the tree.
362*2639ae9bSBen Gras  *
363*2639ae9bSBen Gras  * Parameters:
364*2639ae9bSBen Gras  *	t:	tree
365*2639ae9bSBen Gras  *	h:	leaf page
366*2639ae9bSBen Gras  *
367*2639ae9bSBen Gras  * Returns:
368*2639ae9bSBen Gras  *	RET_SUCCESS, RET_ERROR.
369*2639ae9bSBen Gras  *
370*2639ae9bSBen Gras  * Side-effects:
371*2639ae9bSBen Gras  *	mpool_put's the page
372*2639ae9bSBen Gras  */
373*2639ae9bSBen Gras static int
__bt_pdelete(BTREE * t,PAGE * h)374*2639ae9bSBen Gras __bt_pdelete(BTREE *t, PAGE *h)
375*2639ae9bSBen Gras {
376*2639ae9bSBen Gras 	BINTERNAL *bi;
377*2639ae9bSBen Gras 	PAGE *pg;
378*2639ae9bSBen Gras 	EPGNO *parent;
379*2639ae9bSBen Gras 	indx_t cnt, idx, *ip, offset;
380*2639ae9bSBen Gras 	uint32_t nksize;
381*2639ae9bSBen Gras 	char *from;
382*2639ae9bSBen Gras 
383*2639ae9bSBen Gras 	/*
384*2639ae9bSBen Gras 	 * Walk the parent page stack -- a LIFO stack of the pages that were
385*2639ae9bSBen Gras 	 * traversed when we searched for the page where the delete occurred.
386*2639ae9bSBen Gras 	 * Each stack entry is a page number and a page index offset.  The
387*2639ae9bSBen Gras 	 * offset is for the page traversed on the search.  We've just deleted
388*2639ae9bSBen Gras 	 * a page, so we have to delete the key from the parent page.
389*2639ae9bSBen Gras 	 *
390*2639ae9bSBen Gras 	 * If the delete from the parent page makes it empty, this process may
391*2639ae9bSBen Gras 	 * continue all the way up the tree.  We stop if we reach the root page
392*2639ae9bSBen Gras 	 * (which is never deleted, it's just not worth the effort) or if the
393*2639ae9bSBen Gras 	 * delete does not empty the page.
394*2639ae9bSBen Gras 	 */
395*2639ae9bSBen Gras 	while ((parent = BT_POP(t)) != NULL) {
396*2639ae9bSBen Gras 		/* Get the parent page. */
397*2639ae9bSBen Gras 		if ((pg = mpool_get(t->bt_mp, parent->pgno, 0)) == NULL)
398*2639ae9bSBen Gras 			return (RET_ERROR);
399*2639ae9bSBen Gras 
400*2639ae9bSBen Gras 		idx = parent->index;
401*2639ae9bSBen Gras 		bi = GETBINTERNAL(pg, idx);
402*2639ae9bSBen Gras 
403*2639ae9bSBen Gras 		/* Free any overflow pages. */
404*2639ae9bSBen Gras 		if (bi->flags & P_BIGKEY &&
405*2639ae9bSBen Gras 		    __ovfl_delete(t, bi->bytes) == RET_ERROR) {
406*2639ae9bSBen Gras 			mpool_put(t->bt_mp, pg, 0);
407*2639ae9bSBen Gras 			return (RET_ERROR);
408*2639ae9bSBen Gras 		}
409*2639ae9bSBen Gras 
410*2639ae9bSBen Gras 		/*
411*2639ae9bSBen Gras 		 * Free the parent if it has only the one key and it's not the
412*2639ae9bSBen Gras 		 * root page. If it's the rootpage, turn it back into an empty
413*2639ae9bSBen Gras 		 * leaf page.
414*2639ae9bSBen Gras 		 */
415*2639ae9bSBen Gras 		if (NEXTINDEX(pg) == 1) {
416*2639ae9bSBen Gras 			if (pg->pgno == P_ROOT) {
417*2639ae9bSBen Gras 				pg->lower = BTDATAOFF;
418*2639ae9bSBen Gras 				pg->upper = t->bt_psize;
419*2639ae9bSBen Gras 				pg->flags = P_BLEAF;
420*2639ae9bSBen Gras 			} else {
421*2639ae9bSBen Gras 				if (__bt_relink(t, pg) || __bt_free(t, pg))
422*2639ae9bSBen Gras 					return (RET_ERROR);
423*2639ae9bSBen Gras 				continue;
424*2639ae9bSBen Gras 			}
425*2639ae9bSBen Gras 		} else {
426*2639ae9bSBen Gras 			/* Pack remaining key items at the end of the page. */
427*2639ae9bSBen Gras 			nksize = NBINTERNAL(bi->ksize);
428*2639ae9bSBen Gras 			from = (char *)(void *)pg + pg->upper;
429*2639ae9bSBen Gras 			memmove(from + nksize, from,
430*2639ae9bSBen Gras 			(size_t)((char *)(void *)bi - from));
431*2639ae9bSBen Gras 			pg->upper += nksize;
432*2639ae9bSBen Gras 
433*2639ae9bSBen Gras 			/* Adjust indices' offsets, shift the indices down. */
434*2639ae9bSBen Gras 			offset = pg->linp[idx];
435*2639ae9bSBen Gras 			for (cnt = idx, ip = &pg->linp[0]; cnt--; ++ip)
436*2639ae9bSBen Gras 				if (ip[0] < offset)
437*2639ae9bSBen Gras 					ip[0] += nksize;
438*2639ae9bSBen Gras 			for (cnt = NEXTINDEX(pg) - idx; --cnt; ++ip)
439*2639ae9bSBen Gras 				ip[0] = ip[1] < offset ? ip[1] + nksize : ip[1];
440*2639ae9bSBen Gras 			pg->lower -= sizeof(indx_t);
441*2639ae9bSBen Gras 		}
442*2639ae9bSBen Gras 
443*2639ae9bSBen Gras 		mpool_put(t->bt_mp, pg, MPOOL_DIRTY);
444*2639ae9bSBen Gras 		break;
445*2639ae9bSBen Gras 	}
446*2639ae9bSBen Gras 
447*2639ae9bSBen Gras 	/* Free the leaf page, as long as it wasn't the root. */
448*2639ae9bSBen Gras 	if (h->pgno == P_ROOT) {
449*2639ae9bSBen Gras 		mpool_put(t->bt_mp, h, MPOOL_DIRTY);
450*2639ae9bSBen Gras 		return (RET_SUCCESS);
451*2639ae9bSBen Gras 	}
452*2639ae9bSBen Gras 	return (__bt_relink(t, h) || __bt_free(t, h));
453*2639ae9bSBen Gras }
454*2639ae9bSBen Gras 
455*2639ae9bSBen Gras /*
456*2639ae9bSBen Gras  * __bt_dleaf --
457*2639ae9bSBen Gras  *	Delete a single record from a leaf page.
458*2639ae9bSBen Gras  *
459*2639ae9bSBen Gras  * Parameters:
460*2639ae9bSBen Gras  *	t:	tree
461*2639ae9bSBen Gras  *    key:	referenced key
462*2639ae9bSBen Gras  *	h:	page
463*2639ae9bSBen Gras  *	idx:	index on page to delete
464*2639ae9bSBen Gras  *
465*2639ae9bSBen Gras  * Returns:
466*2639ae9bSBen Gras  *	RET_SUCCESS, RET_ERROR.
467*2639ae9bSBen Gras  */
468*2639ae9bSBen Gras int
__bt_dleaf(BTREE * t,const DBT * key,PAGE * h,u_int idx)469*2639ae9bSBen Gras __bt_dleaf(BTREE *t, const DBT *key, PAGE *h, u_int idx)
470*2639ae9bSBen Gras {
471*2639ae9bSBen Gras 	BLEAF *bl;
472*2639ae9bSBen Gras 	indx_t cnt, *ip, offset;
473*2639ae9bSBen Gras 	uint32_t nbytes;
474*2639ae9bSBen Gras 	void *to;
475*2639ae9bSBen Gras 	char *from;
476*2639ae9bSBen Gras 
477*2639ae9bSBen Gras 	/* If this record is referenced by the cursor, delete the cursor. */
478*2639ae9bSBen Gras 	if (F_ISSET(&t->bt_cursor, CURS_INIT) &&
479*2639ae9bSBen Gras 	    !F_ISSET(&t->bt_cursor, CURS_ACQUIRE) &&
480*2639ae9bSBen Gras 	    t->bt_cursor.pg.pgno == h->pgno && t->bt_cursor.pg.index == idx &&
481*2639ae9bSBen Gras 	    __bt_curdel(t, key, h, idx))
482*2639ae9bSBen Gras 		return (RET_ERROR);
483*2639ae9bSBen Gras 
484*2639ae9bSBen Gras 	/* If the entry uses overflow pages, make them available for reuse. */
485*2639ae9bSBen Gras 	to = bl = GETBLEAF(h, idx);
486*2639ae9bSBen Gras 	if (bl->flags & P_BIGKEY && __ovfl_delete(t, bl->bytes) == RET_ERROR)
487*2639ae9bSBen Gras 		return (RET_ERROR);
488*2639ae9bSBen Gras 	if (bl->flags & P_BIGDATA &&
489*2639ae9bSBen Gras 	    __ovfl_delete(t, bl->bytes + bl->ksize) == RET_ERROR)
490*2639ae9bSBen Gras 		return (RET_ERROR);
491*2639ae9bSBen Gras 
492*2639ae9bSBen Gras 	/* Pack the remaining key/data items at the end of the page. */
493*2639ae9bSBen Gras 	nbytes = NBLEAF(bl);
494*2639ae9bSBen Gras 	from = (char *)(void *)h + h->upper;
495*2639ae9bSBen Gras 	memmove(from + nbytes, from, (size_t)((char *)(void *)to - from));
496*2639ae9bSBen Gras 	h->upper += nbytes;
497*2639ae9bSBen Gras 
498*2639ae9bSBen Gras 	/* Adjust the indices' offsets, shift the indices down. */
499*2639ae9bSBen Gras 	offset = h->linp[idx];
500*2639ae9bSBen Gras 	for (cnt = idx, ip = &h->linp[0]; cnt--; ++ip)
501*2639ae9bSBen Gras 		if (ip[0] < offset)
502*2639ae9bSBen Gras 			ip[0] += nbytes;
503*2639ae9bSBen Gras 	for (cnt = NEXTINDEX(h) - idx; --cnt; ++ip)
504*2639ae9bSBen Gras 		ip[0] = ip[1] < offset ? ip[1] + nbytes : ip[1];
505*2639ae9bSBen Gras 	h->lower -= sizeof(indx_t);
506*2639ae9bSBen Gras 
507*2639ae9bSBen Gras 	/* If the cursor is on this page, adjust it as necessary. */
508*2639ae9bSBen Gras 	if (F_ISSET(&t->bt_cursor, CURS_INIT) &&
509*2639ae9bSBen Gras 	    !F_ISSET(&t->bt_cursor, CURS_ACQUIRE) &&
510*2639ae9bSBen Gras 	    t->bt_cursor.pg.pgno == h->pgno && t->bt_cursor.pg.index > idx)
511*2639ae9bSBen Gras 		--t->bt_cursor.pg.index;
512*2639ae9bSBen Gras 
513*2639ae9bSBen Gras 	return (RET_SUCCESS);
514*2639ae9bSBen Gras }
515*2639ae9bSBen Gras 
516*2639ae9bSBen Gras /*
517*2639ae9bSBen Gras  * __bt_curdel --
518*2639ae9bSBen Gras  *	Delete the cursor.
519*2639ae9bSBen Gras  *
520*2639ae9bSBen Gras  * Parameters:
521*2639ae9bSBen Gras  *	t:	tree
522*2639ae9bSBen Gras  *    key:	referenced key (or NULL)
523*2639ae9bSBen Gras  *	h:	page
524*2639ae9bSBen Gras  *  idx:	index on page to delete
525*2639ae9bSBen Gras  *
526*2639ae9bSBen Gras  * Returns:
527*2639ae9bSBen Gras  *	RET_SUCCESS, RET_ERROR.
528*2639ae9bSBen Gras  */
529*2639ae9bSBen Gras static int
__bt_curdel(BTREE * t,const DBT * key,PAGE * h,u_int idx)530*2639ae9bSBen Gras __bt_curdel(BTREE *t, const DBT *key, PAGE *h, u_int idx)
531*2639ae9bSBen Gras {
532*2639ae9bSBen Gras 	CURSOR *c;
533*2639ae9bSBen Gras 	EPG e;
534*2639ae9bSBen Gras 	PAGE *pg;
535*2639ae9bSBen Gras 	int curcopy, status;
536*2639ae9bSBen Gras 
537*2639ae9bSBen Gras 	/*
538*2639ae9bSBen Gras 	 * If there are duplicates, move forward or backward to one.
539*2639ae9bSBen Gras 	 * Otherwise, copy the key into the cursor area.
540*2639ae9bSBen Gras 	 */
541*2639ae9bSBen Gras 	c = &t->bt_cursor;
542*2639ae9bSBen Gras 	F_CLR(c, CURS_AFTER | CURS_BEFORE | CURS_ACQUIRE);
543*2639ae9bSBen Gras 
544*2639ae9bSBen Gras 	curcopy = 0;
545*2639ae9bSBen Gras 	if (!F_ISSET(t, B_NODUPS)) {
546*2639ae9bSBen Gras 		/*
547*2639ae9bSBen Gras 		 * We're going to have to do comparisons.  If we weren't
548*2639ae9bSBen Gras 		 * provided a copy of the key, i.e. the user is deleting
549*2639ae9bSBen Gras 		 * the current cursor position, get one.
550*2639ae9bSBen Gras 		 */
551*2639ae9bSBen Gras 		if (key == NULL) {
552*2639ae9bSBen Gras 			e.page = h;
553*2639ae9bSBen Gras 			e.index = idx;
554*2639ae9bSBen Gras 			if ((status = __bt_ret(t, &e,
555*2639ae9bSBen Gras 			    &c->key, &c->key, NULL, NULL, 1)) != RET_SUCCESS)
556*2639ae9bSBen Gras 				return (status);
557*2639ae9bSBen Gras 			curcopy = 1;
558*2639ae9bSBen Gras 			key = &c->key;
559*2639ae9bSBen Gras 		}
560*2639ae9bSBen Gras 		/* Check previous key, if not at the beginning of the page. */
561*2639ae9bSBen Gras 		if (idx > 0) {
562*2639ae9bSBen Gras 			e.page = h;
563*2639ae9bSBen Gras 			e.index = idx - 1;
564*2639ae9bSBen Gras 			if (__bt_cmp(t, key, &e) == 0) {
565*2639ae9bSBen Gras 				F_SET(c, CURS_BEFORE);
566*2639ae9bSBen Gras 				goto dup2;
567*2639ae9bSBen Gras 			}
568*2639ae9bSBen Gras 		}
569*2639ae9bSBen Gras 		/* Check next key, if not at the end of the page. */
570*2639ae9bSBen Gras 		if (idx < (unsigned)(NEXTINDEX(h) - 1)) {
571*2639ae9bSBen Gras 			e.page = h;
572*2639ae9bSBen Gras 			e.index = idx + 1;
573*2639ae9bSBen Gras 			if (__bt_cmp(t, key, &e) == 0) {
574*2639ae9bSBen Gras 				F_SET(c, CURS_AFTER);
575*2639ae9bSBen Gras 				goto dup2;
576*2639ae9bSBen Gras 			}
577*2639ae9bSBen Gras 		}
578*2639ae9bSBen Gras 		/* Check previous key if at the beginning of the page. */
579*2639ae9bSBen Gras 		if (idx == 0 && h->prevpg != P_INVALID) {
580*2639ae9bSBen Gras 			if ((pg = mpool_get(t->bt_mp, h->prevpg, 0)) == NULL)
581*2639ae9bSBen Gras 				return (RET_ERROR);
582*2639ae9bSBen Gras 			e.page = pg;
583*2639ae9bSBen Gras 			e.index = NEXTINDEX(pg) - 1;
584*2639ae9bSBen Gras 			if (__bt_cmp(t, key, &e) == 0) {
585*2639ae9bSBen Gras 				F_SET(c, CURS_BEFORE);
586*2639ae9bSBen Gras 				goto dup1;
587*2639ae9bSBen Gras 			}
588*2639ae9bSBen Gras 			mpool_put(t->bt_mp, pg, 0);
589*2639ae9bSBen Gras 		}
590*2639ae9bSBen Gras 		/* Check next key if at the end of the page. */
591*2639ae9bSBen Gras 		if (idx == (unsigned)(NEXTINDEX(h) - 1) && h->nextpg != P_INVALID) {
592*2639ae9bSBen Gras 			if ((pg = mpool_get(t->bt_mp, h->nextpg, 0)) == NULL)
593*2639ae9bSBen Gras 				return (RET_ERROR);
594*2639ae9bSBen Gras 			e.page = pg;
595*2639ae9bSBen Gras 			e.index = 0;
596*2639ae9bSBen Gras 			if (__bt_cmp(t, key, &e) == 0) {
597*2639ae9bSBen Gras 				F_SET(c, CURS_AFTER);
598*2639ae9bSBen Gras dup1:				mpool_put(t->bt_mp, pg, 0);
599*2639ae9bSBen Gras dup2:				c->pg.pgno = e.page->pgno;
600*2639ae9bSBen Gras 				c->pg.index = e.index;
601*2639ae9bSBen Gras 				return (RET_SUCCESS);
602*2639ae9bSBen Gras 			}
603*2639ae9bSBen Gras 			mpool_put(t->bt_mp, pg, 0);
604*2639ae9bSBen Gras 		}
605*2639ae9bSBen Gras 	}
606*2639ae9bSBen Gras 	e.page = h;
607*2639ae9bSBen Gras 	e.index = idx;
608*2639ae9bSBen Gras 	if (curcopy || (status =
609*2639ae9bSBen Gras 	    __bt_ret(t, &e, &c->key, &c->key, NULL, NULL, 1)) == RET_SUCCESS) {
610*2639ae9bSBen Gras 		F_SET(c, CURS_ACQUIRE);
611*2639ae9bSBen Gras 		return (RET_SUCCESS);
612*2639ae9bSBen Gras 	}
613*2639ae9bSBen Gras 	return (status);
614*2639ae9bSBen Gras }
615*2639ae9bSBen Gras 
616*2639ae9bSBen Gras /*
617*2639ae9bSBen Gras  * __bt_relink --
618*2639ae9bSBen Gras  *	Link around a deleted page.
619*2639ae9bSBen Gras  *
620*2639ae9bSBen Gras  * Parameters:
621*2639ae9bSBen Gras  *	t:	tree
622*2639ae9bSBen Gras  *	h:	page to be deleted
623*2639ae9bSBen Gras  */
624*2639ae9bSBen Gras static int
__bt_relink(BTREE * t,PAGE * h)625*2639ae9bSBen Gras __bt_relink(BTREE *t, PAGE *h)
626*2639ae9bSBen Gras {
627*2639ae9bSBen Gras 	PAGE *pg;
628*2639ae9bSBen Gras 
629*2639ae9bSBen Gras 	if (h->nextpg != P_INVALID) {
630*2639ae9bSBen Gras 		if ((pg = mpool_get(t->bt_mp, h->nextpg, 0)) == NULL)
631*2639ae9bSBen Gras 			return (RET_ERROR);
632*2639ae9bSBen Gras 		pg->prevpg = h->prevpg;
633*2639ae9bSBen Gras 		mpool_put(t->bt_mp, pg, MPOOL_DIRTY);
634*2639ae9bSBen Gras 	}
635*2639ae9bSBen Gras 	if (h->prevpg != P_INVALID) {
636*2639ae9bSBen Gras 		if ((pg = mpool_get(t->bt_mp, h->prevpg, 0)) == NULL)
637*2639ae9bSBen Gras 			return (RET_ERROR);
638*2639ae9bSBen Gras 		pg->nextpg = h->nextpg;
639*2639ae9bSBen Gras 		mpool_put(t->bt_mp, pg, MPOOL_DIRTY);
640*2639ae9bSBen Gras 	}
641*2639ae9bSBen Gras 	return (0);
642*2639ae9bSBen Gras }
643