]>
git.saurik.com Git - apple/libc.git/blob - db/btree/FreeBSD/bt_delete.c
2 * Copyright (c) 1990, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 #if defined(LIBC_SCCS) && !defined(lint)
38 static char sccsid
[] = "@(#)bt_delete.c 8.13 (Berkeley) 7/28/94";
39 #endif /* LIBC_SCCS and not lint */
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD: src/lib/libc/db/btree/bt_delete.c,v 1.2 2002/03/21 22:46:25 obrien Exp $");
43 #include <sys/types.h>
52 static int __bt_bdelete(BTREE
*, const DBT
*);
53 static int __bt_curdel(BTREE
*, const DBT
*, PAGE
*, u_int
);
54 static int __bt_pdelete(BTREE
*, PAGE
*);
55 static int __bt_relink(BTREE
*, PAGE
*);
56 static int __bt_stkacq(BTREE
*, PAGE
**, CURSOR
*);
60 * Delete the item(s) referenced by a key.
62 * Return RET_SPECIAL if the key is not found.
65 __bt_delete(dbp
, key
, flags
)
77 /* Toss any page pinned across calls. */
78 if (t
->bt_pinned
!= NULL
) {
79 mpool_put(t
->bt_mp
, t
->bt_pinned
, 0);
83 /* Check for change to a read-only tree. */
84 if (F_ISSET(t
, B_RDONLY
)) {
91 status
= __bt_bdelete(t
, key
);
95 * If flags is R_CURSOR, delete the cursor. Must already
96 * have started a scan and not have already deleted it.
99 if (F_ISSET(c
, CURS_INIT
)) {
100 if (F_ISSET(c
, CURS_ACQUIRE
| CURS_AFTER
| CURS_BEFORE
))
101 return (RET_SPECIAL
);
102 if ((h
= mpool_get(t
->bt_mp
, c
->pg
.pgno
, 0)) == NULL
)
106 * If the page is about to be emptied, we'll need to
107 * delete it, which means we have to acquire a stack.
109 if (NEXTINDEX(h
) == 1)
110 if (__bt_stkacq(t
, &h
, &t
->bt_cursor
))
113 status
= __bt_dleaf(t
, NULL
, h
, c
->pg
.index
);
115 if (NEXTINDEX(h
) == 0 && status
== RET_SUCCESS
) {
116 if (__bt_pdelete(t
, h
))
120 h
, status
== RET_SUCCESS
? MPOOL_DIRTY
: 0);
128 if (status
== RET_SUCCESS
)
129 F_SET(t
, B_MODIFIED
);
135 * Acquire a stack so we can delete a cursor entry.
139 * hp: pointer to current, pinned PAGE pointer
140 * c: pointer to the cursor
143 * 0 on success, 1 on failure
146 __bt_stkacq(t
, hp
, c
)
157 recno_t nextpg
, prevpg
;
161 * Find the first occurrence of the key in the tree. Toss the
162 * currently locked page so we don't hit an already-locked page.
165 mpool_put(t
->bt_mp
, h
, 0);
166 if ((e
= __bt_search(t
, &c
->key
, &exact
)) == NULL
)
170 /* See if we got it in one shot. */
171 if (h
->pgno
== c
->pg
.pgno
)
175 * Move right, looking for the page. At each move we have to move
176 * up the stack until we don't have to move to the next page. If
177 * we have to change pages at an internal level, we have to fix the
180 while (h
->pgno
!= c
->pg
.pgno
) {
181 if ((nextpg
= h
->nextpg
) == P_INVALID
)
183 mpool_put(t
->bt_mp
, h
, 0);
185 /* Move up the stack. */
186 for (level
= 0; (parent
= BT_POP(t
)) != NULL
; ++level
) {
187 /* Get the parent page. */
188 if ((h
= mpool_get(t
->bt_mp
, parent
->pgno
, 0)) == NULL
)
191 /* Move to the next index. */
192 if (parent
->index
!= NEXTINDEX(h
) - 1) {
193 index
= parent
->index
+ 1;
194 BT_PUSH(t
, h
->pgno
, index
);
197 mpool_put(t
->bt_mp
, h
, 0);
200 /* Restore the stack. */
202 /* Push the next level down onto the stack. */
203 bi
= GETBINTERNAL(h
, index
);
207 /* Lose the currently pinned page. */
208 mpool_put(t
->bt_mp
, h
, 0);
210 /* Get the next level down. */
211 if ((h
= mpool_get(t
->bt_mp
, pgno
, 0)) == NULL
)
215 mpool_put(t
->bt_mp
, h
, 0);
216 if ((h
= mpool_get(t
->bt_mp
, nextpg
, 0)) == NULL
)
220 if (h
->pgno
== c
->pg
.pgno
)
223 /* Reacquire the original stack. */
224 mpool_put(t
->bt_mp
, h
, 0);
225 if ((e
= __bt_search(t
, &c
->key
, &exact
)) == NULL
)
230 * Move left, looking for the page. At each move we have to move
231 * up the stack until we don't have to change pages to move to the
232 * next page. If we have to change pages at an internal level, we
233 * have to fix the stack back up.
235 while (h
->pgno
!= c
->pg
.pgno
) {
236 if ((prevpg
= h
->prevpg
) == P_INVALID
)
238 mpool_put(t
->bt_mp
, h
, 0);
240 /* Move up the stack. */
241 for (level
= 0; (parent
= BT_POP(t
)) != NULL
; ++level
) {
242 /* Get the parent page. */
243 if ((h
= mpool_get(t
->bt_mp
, parent
->pgno
, 0)) == NULL
)
246 /* Move to the next index. */
247 if (parent
->index
!= 0) {
248 index
= parent
->index
- 1;
249 BT_PUSH(t
, h
->pgno
, index
);
252 mpool_put(t
->bt_mp
, h
, 0);
255 /* Restore the stack. */
257 /* Push the next level down onto the stack. */
258 bi
= GETBINTERNAL(h
, index
);
261 /* Lose the currently pinned page. */
262 mpool_put(t
->bt_mp
, h
, 0);
264 /* Get the next level down. */
265 if ((h
= mpool_get(t
->bt_mp
, pgno
, 0)) == NULL
)
268 index
= NEXTINDEX(h
) - 1;
269 BT_PUSH(t
, pgno
, index
);
271 mpool_put(t
->bt_mp
, h
, 0);
272 if ((h
= mpool_get(t
->bt_mp
, prevpg
, 0)) == NULL
)
277 ret
: mpool_put(t
->bt_mp
, h
, 0);
278 return ((*hp
= mpool_get(t
->bt_mp
, c
->pg
.pgno
, 0)) == NULL
);
283 * Delete all key/data pairs matching the specified key.
290 * RET_ERROR, RET_SUCCESS and RET_SPECIAL if the key not found.
299 int deleted
, exact
, redo
;
303 /* Find any matching record; __bt_search pins the page. */
304 loop
: if ((e
= __bt_search(t
, key
, &exact
)) == NULL
)
305 return (deleted
? RET_SUCCESS
: RET_ERROR
);
307 mpool_put(t
->bt_mp
, e
->page
, 0);
308 return (deleted
? RET_SUCCESS
: RET_SPECIAL
);
312 * Delete forward, then delete backward, from the found key. If
313 * there are duplicates and we reach either side of the page, do
314 * the key search again, so that we get them all.
319 if (__bt_dleaf(t
, key
, h
, e
->index
)) {
320 mpool_put(t
->bt_mp
, h
, 0);
323 if (F_ISSET(t
, B_NODUPS
)) {
324 if (NEXTINDEX(h
) == 0) {
325 if (__bt_pdelete(t
, h
))
328 mpool_put(t
->bt_mp
, h
, MPOOL_DIRTY
);
329 return (RET_SUCCESS
);
332 } while (e
->index
< NEXTINDEX(h
) && __bt_cmp(t
, key
, e
) == 0);
334 /* Check for right-hand edge of the page. */
335 if (e
->index
== NEXTINDEX(h
))
338 /* Delete from the key to the beginning of the page. */
339 while (e
->index
-- > 0) {
340 if (__bt_cmp(t
, key
, e
) != 0)
342 if (__bt_dleaf(t
, key
, h
, e
->index
) == RET_ERROR
) {
343 mpool_put(t
->bt_mp
, h
, 0);
350 /* Check for an empty page. */
351 if (NEXTINDEX(h
) == 0) {
352 if (__bt_pdelete(t
, h
))
358 mpool_put(t
->bt_mp
, h
, MPOOL_DIRTY
);
362 return (RET_SUCCESS
);
367 * Delete a single page from the tree.
374 * RET_SUCCESS, RET_ERROR.
377 * mpool_put's the page
387 indx_t cnt
, index
, *ip
, offset
;
392 * Walk the parent page stack -- a LIFO stack of the pages that were
393 * traversed when we searched for the page where the delete occurred.
394 * Each stack entry is a page number and a page index offset. The
395 * offset is for the page traversed on the search. We've just deleted
396 * a page, so we have to delete the key from the parent page.
398 * If the delete from the parent page makes it empty, this process may
399 * continue all the way up the tree. We stop if we reach the root page
400 * (which is never deleted, it's just not worth the effort) or if the
401 * delete does not empty the page.
403 while ((parent
= BT_POP(t
)) != NULL
) {
404 /* Get the parent page. */
405 if ((pg
= mpool_get(t
->bt_mp
, parent
->pgno
, 0)) == NULL
)
408 index
= parent
->index
;
409 bi
= GETBINTERNAL(pg
, index
);
411 /* Free any overflow pages. */
412 if (bi
->flags
& P_BIGKEY
&&
413 __ovfl_delete(t
, bi
->bytes
) == RET_ERROR
) {
414 mpool_put(t
->bt_mp
, pg
, 0);
419 * Free the parent if it has only the one key and it's not the
420 * root page. If it's the rootpage, turn it back into an empty
423 if (NEXTINDEX(pg
) == 1)
424 if (pg
->pgno
== P_ROOT
) {
425 pg
->lower
= BTDATAOFF
;
426 pg
->upper
= t
->bt_psize
;
429 if (__bt_relink(t
, pg
) || __bt_free(t
, pg
))
434 /* Pack remaining key items at the end of the page. */
435 nksize
= NBINTERNAL(bi
->ksize
);
436 from
= (char *)pg
+ pg
->upper
;
437 memmove(from
+ nksize
, from
, (char *)bi
- from
);
440 /* Adjust indices' offsets, shift the indices down. */
441 offset
= pg
->linp
[index
];
442 for (cnt
= index
, ip
= &pg
->linp
[0]; cnt
--; ++ip
)
445 for (cnt
= NEXTINDEX(pg
) - index
; --cnt
; ++ip
)
446 ip
[0] = ip
[1] < offset
? ip
[1] + nksize
: ip
[1];
447 pg
->lower
-= sizeof(indx_t
);
450 mpool_put(t
->bt_mp
, pg
, MPOOL_DIRTY
);
454 /* Free the leaf page, as long as it wasn't the root. */
455 if (h
->pgno
== P_ROOT
) {
456 mpool_put(t
->bt_mp
, h
, MPOOL_DIRTY
);
457 return (RET_SUCCESS
);
459 return (__bt_relink(t
, h
) || __bt_free(t
, h
));
464 * Delete a single record from a leaf page.
468 * key: referenced key
470 * index: index on page to delete
473 * RET_SUCCESS, RET_ERROR.
476 __bt_dleaf(t
, key
, h
, index
)
483 indx_t cnt
, *ip
, offset
;
488 /* If this record is referenced by the cursor, delete the cursor. */
489 if (F_ISSET(&t
->bt_cursor
, CURS_INIT
) &&
490 !F_ISSET(&t
->bt_cursor
, CURS_ACQUIRE
) &&
491 t
->bt_cursor
.pg
.pgno
== h
->pgno
&& t
->bt_cursor
.pg
.index
== index
&&
492 __bt_curdel(t
, key
, h
, index
))
495 /* If the entry uses overflow pages, make them available for reuse. */
496 to
= bl
= GETBLEAF(h
, index
);
497 if (bl
->flags
& P_BIGKEY
&& __ovfl_delete(t
, bl
->bytes
) == RET_ERROR
)
499 if (bl
->flags
& P_BIGDATA
&&
500 __ovfl_delete(t
, bl
->bytes
+ bl
->ksize
) == RET_ERROR
)
503 /* Pack the remaining key/data items at the end of the page. */
505 from
= (char *)h
+ h
->upper
;
506 memmove(from
+ nbytes
, from
, (char *)to
- from
);
509 /* Adjust the indices' offsets, shift the indices down. */
510 offset
= h
->linp
[index
];
511 for (cnt
= index
, ip
= &h
->linp
[0]; cnt
--; ++ip
)
514 for (cnt
= NEXTINDEX(h
) - index
; --cnt
; ++ip
)
515 ip
[0] = ip
[1] < offset
? ip
[1] + nbytes
: ip
[1];
516 h
->lower
-= sizeof(indx_t
);
518 /* If the cursor is on this page, adjust it as necessary. */
519 if (F_ISSET(&t
->bt_cursor
, CURS_INIT
) &&
520 !F_ISSET(&t
->bt_cursor
, CURS_ACQUIRE
) &&
521 t
->bt_cursor
.pg
.pgno
== h
->pgno
&& t
->bt_cursor
.pg
.index
> index
)
522 --t
->bt_cursor
.pg
.index
;
524 return (RET_SUCCESS
);
533 * key: referenced key (or NULL)
535 * index: index on page to delete
538 * RET_SUCCESS, RET_ERROR.
541 __bt_curdel(t
, key
, h
, index
)
553 * If there are duplicates, move forward or backward to one.
554 * Otherwise, copy the key into the cursor area.
557 F_CLR(c
, CURS_AFTER
| CURS_BEFORE
| CURS_ACQUIRE
);
560 if (!F_ISSET(t
, B_NODUPS
)) {
562 * We're going to have to do comparisons. If we weren't
563 * provided a copy of the key, i.e. the user is deleting
564 * the current cursor position, get one.
569 if ((status
= __bt_ret(t
, &e
,
570 &c
->key
, &c
->key
, NULL
, NULL
, 1)) != RET_SUCCESS
)
575 /* Check previous key, if not at the beginning of the page. */
579 if (__bt_cmp(t
, key
, &e
) == 0) {
580 F_SET(c
, CURS_BEFORE
);
584 /* Check next key, if not at the end of the page. */
585 if (index
< NEXTINDEX(h
) - 1) {
588 if (__bt_cmp(t
, key
, &e
) == 0) {
589 F_SET(c
, CURS_AFTER
);
593 /* Check previous key if at the beginning of the page. */
594 if (index
== 0 && h
->prevpg
!= P_INVALID
) {
595 if ((pg
= mpool_get(t
->bt_mp
, h
->prevpg
, 0)) == NULL
)
598 e
.index
= NEXTINDEX(pg
) - 1;
599 if (__bt_cmp(t
, key
, &e
) == 0) {
600 F_SET(c
, CURS_BEFORE
);
603 mpool_put(t
->bt_mp
, pg
, 0);
605 /* Check next key if at the end of the page. */
606 if (index
== NEXTINDEX(h
) - 1 && h
->nextpg
!= P_INVALID
) {
607 if ((pg
= mpool_get(t
->bt_mp
, h
->nextpg
, 0)) == NULL
)
611 if (__bt_cmp(t
, key
, &e
) == 0) {
612 F_SET(c
, CURS_AFTER
);
613 dup1
: mpool_put(t
->bt_mp
, pg
, 0);
614 dup2
: c
->pg
.pgno
= e
.page
->pgno
;
615 c
->pg
.index
= e
.index
;
616 return (RET_SUCCESS
);
618 mpool_put(t
->bt_mp
, pg
, 0);
623 if (curcopy
|| (status
=
624 __bt_ret(t
, &e
, &c
->key
, &c
->key
, NULL
, NULL
, 1)) == RET_SUCCESS
) {
625 F_SET(c
, CURS_ACQUIRE
);
626 return (RET_SUCCESS
);
633 * Link around a deleted page.
637 * h: page to be deleted
646 if (h
->nextpg
!= P_INVALID
) {
647 if ((pg
= mpool_get(t
->bt_mp
, h
->nextpg
, 0)) == NULL
)
649 pg
->prevpg
= h
->prevpg
;
650 mpool_put(t
->bt_mp
, pg
, MPOOL_DIRTY
);
652 if (h
->prevpg
!= P_INVALID
) {
653 if ((pg
= mpool_get(t
->bt_mp
, h
->prevpg
, 0)) == NULL
)
655 pg
->nextpg
= h
->nextpg
;
656 mpool_put(t
->bt_mp
, pg
, MPOOL_DIRTY
);