1 /* $NetBSD: bt_delete.c,v 1.17 2009/01/29 02:02:36 lukem Exp $ */
4 * Copyright (c) 1990, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #if HAVE_NBTOOL_CONFIG_H
36 #include "nbtool_config.h"
39 #include <sys/cdefs.h>
40 __RCSID("$NetBSD: bt_delete.c,v 1.17 2009/01/29 02:02:36 lukem Exp $");
42 #include "namespace.h"
43 #include <sys/types.h>
53 static int __bt_bdelete(BTREE
*, const DBT
*);
54 static int __bt_curdel(BTREE
*, const DBT
*, PAGE
*, u_int
);
55 static int __bt_pdelete(BTREE
*, PAGE
*);
56 static int __bt_relink(BTREE
*, PAGE
*);
57 static int __bt_stkacq(BTREE
*, PAGE
**, CURSOR
*);
61 * Delete the item(s) referenced by a key.
63 * Return RET_SPECIAL if the key is not found.
66 __bt_delete(const DB
*dbp
, const DBT
*key
, u_int flags
)
75 /* Toss any page pinned across calls. */
76 if (t
->bt_pinned
!= NULL
) {
77 mpool_put(t
->bt_mp
, t
->bt_pinned
, 0);
81 /* Check for change to a read-only tree. */
82 if (F_ISSET(t
, B_RDONLY
)) {
89 status
= __bt_bdelete(t
, key
);
93 * If flags is R_CURSOR, delete the cursor. Must already
94 * have started a scan and not have already deleted it.
97 if (F_ISSET(c
, CURS_INIT
)) {
98 if (F_ISSET(c
, CURS_ACQUIRE
| CURS_AFTER
| CURS_BEFORE
))
100 if ((h
= mpool_get(t
->bt_mp
, c
->pg
.pgno
, 0)) == NULL
)
104 * If the page is about to be emptied, we'll need to
105 * delete it, which means we have to acquire a stack.
107 if (NEXTINDEX(h
) == 1)
108 if (__bt_stkacq(t
, &h
, &t
->bt_cursor
))
111 status
= __bt_dleaf(t
, NULL
, h
, (u_int
)c
->pg
.index
);
113 if (NEXTINDEX(h
) == 0 && status
== RET_SUCCESS
) {
114 if (__bt_pdelete(t
, h
))
117 mpool_put(t
->bt_mp
, h
,
118 (u_int
)(status
== RET_SUCCESS
?
127 if (status
== RET_SUCCESS
)
128 F_SET(t
, B_MODIFIED
);
134 * Acquire a stack so we can delete a cursor entry.
138 * hp: pointer to current, pinned PAGE pointer
139 * c: pointer to the cursor
142 * 0 on success, 1 on failure
145 __bt_stkacq(BTREE
*t
, PAGE
**hp
, CURSOR
*c
)
151 indx_t idx
= 0; /* Pacify gcc */
153 recno_t nextpg
, prevpg
;
157 * Find the first occurrence of the key in the tree. Toss the
158 * currently locked page so we don't hit an already-locked page.
161 mpool_put(t
->bt_mp
, h
, 0);
162 if ((e
= __bt_search(t
, &c
->key
, &exact
)) == NULL
)
166 /* See if we got it in one shot. */
167 if (h
->pgno
== c
->pg
.pgno
)
171 * Move right, looking for the page. At each move we have to move
172 * up the stack until we don't have to move to the next page. If
173 * we have to change pages at an internal level, we have to fix the
176 while (h
->pgno
!= c
->pg
.pgno
) {
177 if ((nextpg
= h
->nextpg
) == P_INVALID
)
179 mpool_put(t
->bt_mp
, h
, 0);
181 /* Move up the stack. */
182 for (level
= 0; (parent
= BT_POP(t
)) != NULL
; ++level
) {
183 /* Get the parent page. */
184 if ((h
= mpool_get(t
->bt_mp
, parent
->pgno
, 0)) == NULL
)
187 /* Move to the next index. */
188 if (parent
->index
!= NEXTINDEX(h
) - 1) {
189 idx
= parent
->index
+ 1;
190 BT_PUSH(t
, h
->pgno
, idx
);
193 mpool_put(t
->bt_mp
, h
, 0);
196 /* Restore the stack. */
198 /* Push the next level down onto the stack. */
199 bi
= GETBINTERNAL(h
, idx
);
203 /* Lose the currently pinned page. */
204 mpool_put(t
->bt_mp
, h
, 0);
206 /* Get the next level down. */
207 if ((h
= mpool_get(t
->bt_mp
, pgno
, 0)) == NULL
)
211 mpool_put(t
->bt_mp
, h
, 0);
212 if ((h
= mpool_get(t
->bt_mp
, nextpg
, 0)) == NULL
)
216 if (h
->pgno
== c
->pg
.pgno
)
219 /* Reacquire the original stack. */
220 mpool_put(t
->bt_mp
, h
, 0);
221 if ((e
= __bt_search(t
, &c
->key
, &exact
)) == NULL
)
226 * Move left, looking for the page. At each move we have to move
227 * up the stack until we don't have to change pages to move to the
228 * next page. If we have to change pages at an internal level, we
229 * have to fix the stack back up.
231 while (h
->pgno
!= c
->pg
.pgno
) {
232 if ((prevpg
= h
->prevpg
) == P_INVALID
)
234 mpool_put(t
->bt_mp
, h
, 0);
236 /* Move up the stack. */
237 for (level
= 0; (parent
= BT_POP(t
)) != NULL
; ++level
) {
238 /* Get the parent page. */
239 if ((h
= mpool_get(t
->bt_mp
, parent
->pgno
, 0)) == NULL
)
242 /* Move to the next index. */
243 if (parent
->index
!= 0) {
244 idx
= parent
->index
- 1;
245 BT_PUSH(t
, h
->pgno
, idx
);
248 mpool_put(t
->bt_mp
, h
, 0);
251 /* Restore the stack. */
253 /* Push the next level down onto the stack. */
254 bi
= GETBINTERNAL(h
, idx
);
257 /* Lose the currently pinned page. */
258 mpool_put(t
->bt_mp
, h
, 0);
260 /* Get the next level down. */
261 if ((h
= mpool_get(t
->bt_mp
, pgno
, 0)) == NULL
)
264 idx
= NEXTINDEX(h
) - 1;
265 BT_PUSH(t
, pgno
, idx
);
267 mpool_put(t
->bt_mp
, h
, 0);
268 if ((h
= mpool_get(t
->bt_mp
, prevpg
, 0)) == NULL
)
273 ret
: mpool_put(t
->bt_mp
, h
, 0);
274 return ((*hp
= mpool_get(t
->bt_mp
, c
->pg
.pgno
, 0)) == NULL
);
279 * Delete all key/data pairs matching the specified key.
286 * RET_ERROR, RET_SUCCESS and RET_SPECIAL if the key not found.
289 __bt_bdelete(BTREE
*t
, const DBT
*key
)
293 int deleted
, exact
, redo
;
297 /* Find any matching record; __bt_search pins the page. */
298 loop
: if ((e
= __bt_search(t
, key
, &exact
)) == NULL
)
299 return (deleted
? RET_SUCCESS
: RET_ERROR
);
301 mpool_put(t
->bt_mp
, e
->page
, 0);
302 return (deleted
? RET_SUCCESS
: RET_SPECIAL
);
306 * Delete forward, then delete backward, from the found key. If
307 * there are duplicates and we reach either side of the page, do
308 * the key search again, so that we get them all.
313 if (__bt_dleaf(t
, key
, h
, (u_int
)e
->index
)) {
314 mpool_put(t
->bt_mp
, h
, 0);
317 if (F_ISSET(t
, B_NODUPS
)) {
318 if (NEXTINDEX(h
) == 0) {
319 if (__bt_pdelete(t
, h
))
322 mpool_put(t
->bt_mp
, h
, MPOOL_DIRTY
);
323 return (RET_SUCCESS
);
326 } while (e
->index
< NEXTINDEX(h
) && __bt_cmp(t
, key
, e
) == 0);
328 /* Check for right-hand edge of the page. */
329 if (e
->index
== NEXTINDEX(h
))
332 /* Delete from the key to the beginning of the page. */
333 while (e
->index
-- > 0) {
334 if (__bt_cmp(t
, key
, e
) != 0)
336 if (__bt_dleaf(t
, key
, h
, (u_int
)e
->index
) == RET_ERROR
) {
337 mpool_put(t
->bt_mp
, h
, 0);
344 /* Check for an empty page. */
345 if (NEXTINDEX(h
) == 0) {
346 if (__bt_pdelete(t
, h
))
352 mpool_put(t
->bt_mp
, h
, MPOOL_DIRTY
);
356 return (RET_SUCCESS
);
361 * Delete a single page from the tree.
368 * RET_SUCCESS, RET_ERROR.
371 * mpool_put's the page
374 __bt_pdelete(BTREE
*t
, PAGE
*h
)
379 indx_t cnt
, idx
, *ip
, offset
;
384 * Walk the parent page stack -- a LIFO stack of the pages that were
385 * traversed when we searched for the page where the delete occurred.
386 * Each stack entry is a page number and a page index offset. The
387 * offset is for the page traversed on the search. We've just deleted
388 * a page, so we have to delete the key from the parent page.
390 * If the delete from the parent page makes it empty, this process may
391 * continue all the way up the tree. We stop if we reach the root page
392 * (which is never deleted, it's just not worth the effort) or if the
393 * delete does not empty the page.
395 while ((parent
= BT_POP(t
)) != NULL
) {
396 /* Get the parent page. */
397 if ((pg
= mpool_get(t
->bt_mp
, parent
->pgno
, 0)) == NULL
)
401 bi
= GETBINTERNAL(pg
, idx
);
403 /* Free any overflow pages. */
404 if (bi
->flags
& P_BIGKEY
&&
405 __ovfl_delete(t
, bi
->bytes
) == RET_ERROR
) {
406 mpool_put(t
->bt_mp
, pg
, 0);
411 * Free the parent if it has only the one key and it's not the
412 * root page. If it's the rootpage, turn it back into an empty
415 if (NEXTINDEX(pg
) == 1) {
416 if (pg
->pgno
== P_ROOT
) {
417 pg
->lower
= BTDATAOFF
;
418 pg
->upper
= t
->bt_psize
;
421 if (__bt_relink(t
, pg
) || __bt_free(t
, pg
))
426 /* Pack remaining key items at the end of the page. */
427 nksize
= NBINTERNAL(bi
->ksize
);
428 from
= (char *)(void *)pg
+ pg
->upper
;
429 memmove(from
+ nksize
, from
,
430 (size_t)((char *)(void *)bi
- from
));
433 /* Adjust indices' offsets, shift the indices down. */
434 offset
= pg
->linp
[idx
];
435 for (cnt
= idx
, ip
= &pg
->linp
[0]; cnt
--; ++ip
)
438 for (cnt
= NEXTINDEX(pg
) - idx
; --cnt
; ++ip
)
439 ip
[0] = ip
[1] < offset
? ip
[1] + nksize
: ip
[1];
440 pg
->lower
-= sizeof(indx_t
);
443 mpool_put(t
->bt_mp
, pg
, MPOOL_DIRTY
);
447 /* Free the leaf page, as long as it wasn't the root. */
448 if (h
->pgno
== P_ROOT
) {
449 mpool_put(t
->bt_mp
, h
, MPOOL_DIRTY
);
450 return (RET_SUCCESS
);
452 return (__bt_relink(t
, h
) || __bt_free(t
, h
));
457 * Delete a single record from a leaf page.
461 * key: referenced key
463 * idx: index on page to delete
466 * RET_SUCCESS, RET_ERROR.
469 __bt_dleaf(BTREE
*t
, const DBT
*key
, PAGE
*h
, u_int idx
)
472 indx_t cnt
, *ip
, offset
;
477 /* If this record is referenced by the cursor, delete the cursor. */
478 if (F_ISSET(&t
->bt_cursor
, CURS_INIT
) &&
479 !F_ISSET(&t
->bt_cursor
, CURS_ACQUIRE
) &&
480 t
->bt_cursor
.pg
.pgno
== h
->pgno
&& t
->bt_cursor
.pg
.index
== idx
&&
481 __bt_curdel(t
, key
, h
, idx
))
484 /* If the entry uses overflow pages, make them available for reuse. */
485 to
= bl
= GETBLEAF(h
, idx
);
486 if (bl
->flags
& P_BIGKEY
&& __ovfl_delete(t
, bl
->bytes
) == RET_ERROR
)
488 if (bl
->flags
& P_BIGDATA
&&
489 __ovfl_delete(t
, bl
->bytes
+ bl
->ksize
) == RET_ERROR
)
492 /* Pack the remaining key/data items at the end of the page. */
494 from
= (char *)(void *)h
+ h
->upper
;
495 memmove(from
+ nbytes
, from
, (size_t)((char *)(void *)to
- from
));
498 /* Adjust the indices' offsets, shift the indices down. */
499 offset
= h
->linp
[idx
];
500 for (cnt
= idx
, ip
= &h
->linp
[0]; cnt
--; ++ip
)
503 for (cnt
= NEXTINDEX(h
) - idx
; --cnt
; ++ip
)
504 ip
[0] = ip
[1] < offset
? ip
[1] + nbytes
: ip
[1];
505 h
->lower
-= sizeof(indx_t
);
507 /* If the cursor is on this page, adjust it as necessary. */
508 if (F_ISSET(&t
->bt_cursor
, CURS_INIT
) &&
509 !F_ISSET(&t
->bt_cursor
, CURS_ACQUIRE
) &&
510 t
->bt_cursor
.pg
.pgno
== h
->pgno
&& t
->bt_cursor
.pg
.index
> idx
)
511 --t
->bt_cursor
.pg
.index
;
513 return (RET_SUCCESS
);
522 * key: referenced key (or NULL)
524 * idx: index on page to delete
527 * RET_SUCCESS, RET_ERROR.
530 __bt_curdel(BTREE
*t
, const DBT
*key
, PAGE
*h
, u_int idx
)
538 * If there are duplicates, move forward or backward to one.
539 * Otherwise, copy the key into the cursor area.
542 F_CLR(c
, CURS_AFTER
| CURS_BEFORE
| CURS_ACQUIRE
);
545 if (!F_ISSET(t
, B_NODUPS
)) {
547 * We're going to have to do comparisons. If we weren't
548 * provided a copy of the key, i.e. the user is deleting
549 * the current cursor position, get one.
554 if ((status
= __bt_ret(t
, &e
,
555 &c
->key
, &c
->key
, NULL
, NULL
, 1)) != RET_SUCCESS
)
560 /* Check previous key, if not at the beginning of the page. */
564 if (__bt_cmp(t
, key
, &e
) == 0) {
565 F_SET(c
, CURS_BEFORE
);
569 /* Check next key, if not at the end of the page. */
570 if (idx
< (unsigned)(NEXTINDEX(h
) - 1)) {
573 if (__bt_cmp(t
, key
, &e
) == 0) {
574 F_SET(c
, CURS_AFTER
);
578 /* Check previous key if at the beginning of the page. */
579 if (idx
== 0 && h
->prevpg
!= P_INVALID
) {
580 if ((pg
= mpool_get(t
->bt_mp
, h
->prevpg
, 0)) == NULL
)
583 e
.index
= NEXTINDEX(pg
) - 1;
584 if (__bt_cmp(t
, key
, &e
) == 0) {
585 F_SET(c
, CURS_BEFORE
);
588 mpool_put(t
->bt_mp
, pg
, 0);
590 /* Check next key if at the end of the page. */
591 if (idx
== (unsigned)(NEXTINDEX(h
) - 1) && h
->nextpg
!= P_INVALID
) {
592 if ((pg
= mpool_get(t
->bt_mp
, h
->nextpg
, 0)) == NULL
)
596 if (__bt_cmp(t
, key
, &e
) == 0) {
597 F_SET(c
, CURS_AFTER
);
598 dup1
: mpool_put(t
->bt_mp
, pg
, 0);
599 dup2
: c
->pg
.pgno
= e
.page
->pgno
;
600 c
->pg
.index
= e
.index
;
601 return (RET_SUCCESS
);
603 mpool_put(t
->bt_mp
, pg
, 0);
608 if (curcopy
|| (status
=
609 __bt_ret(t
, &e
, &c
->key
, &c
->key
, NULL
, NULL
, 1)) == RET_SUCCESS
) {
610 F_SET(c
, CURS_ACQUIRE
);
611 return (RET_SUCCESS
);
618 * Link around a deleted page.
622 * h: page to be deleted
625 __bt_relink(BTREE
*t
, PAGE
*h
)
629 if (h
->nextpg
!= P_INVALID
) {
630 if ((pg
= mpool_get(t
->bt_mp
, h
->nextpg
, 0)) == NULL
)
632 pg
->prevpg
= h
->prevpg
;
633 mpool_put(t
->bt_mp
, pg
, MPOOL_DIRTY
);
635 if (h
->prevpg
!= P_INVALID
) {
636 if ((pg
= mpool_get(t
->bt_mp
, h
->prevpg
, 0)) == NULL
)
638 pg
->nextpg
= h
->nextpg
;
639 mpool_put(t
->bt_mp
, pg
, MPOOL_DIRTY
);