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. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * @(#)bt_delete.c 8.13 (Berkeley) 7/28/94
33 * $DragonFly: src/lib/libc/db/btree/bt_delete.c,v 1.8 2005/11/12 23:01:54 swildner Exp $
36 #include <sys/types.h>
45 static int __bt_bdelete (BTREE *, const DBT *);
46 static int __bt_curdel (BTREE *, const DBT *, PAGE *, u_int);
47 static int __bt_pdelete (BTREE *, PAGE *);
48 static int __bt_relink (BTREE *, PAGE *);
49 static int __bt_stkacq (BTREE *, PAGE **, CURSOR *);
53 * Delete the item(s) referenced by a key.
55 * Return RET_SPECIAL if the key is not found.
58 __bt_delete(const DB *dbp, const DBT *key, u_int flags)
67 /* Toss any page pinned across calls. */
68 if (t->bt_pinned != NULL) {
69 mpool_put(t->bt_mp, t->bt_pinned, 0);
73 /* Check for change to a read-only tree. */
74 if (F_ISSET(t, B_RDONLY)) {
81 status = __bt_bdelete(t, key);
85 * If flags is R_CURSOR, delete the cursor. Must already
86 * have started a scan and not have already deleted it.
89 if (F_ISSET(c, CURS_INIT)) {
90 if (F_ISSET(c, CURS_ACQUIRE | CURS_AFTER | CURS_BEFORE))
92 if ((h = mpool_get(t->bt_mp, c->pg.pgno, 0)) == NULL)
96 * If the page is about to be emptied, we'll need to
97 * delete it, which means we have to acquire a stack.
99 if (NEXTINDEX(h) == 1)
100 if (__bt_stkacq(t, &h, &t->bt_cursor))
103 status = __bt_dleaf(t, NULL, h, c->pg.index);
105 if (NEXTINDEX(h) == 0 && status == RET_SUCCESS) {
106 if (__bt_pdelete(t, h))
110 h, status == RET_SUCCESS ? MPOOL_DIRTY : 0);
118 if (status == RET_SUCCESS)
119 F_SET(t, B_MODIFIED);
125 * Acquire a stack so we can delete a cursor entry.
129 * hp: pointer to current, pinned PAGE pointer
130 * c: pointer to the cursor
133 * 0 on success, 1 on failure
136 __bt_stkacq(BTREE *t, PAGE **hp, CURSOR *c)
144 recno_t nextpg, prevpg;
148 * Find the first occurrence of the key in the tree. Toss the
149 * currently locked page so we don't hit an already-locked page.
152 mpool_put(t->bt_mp, h, 0);
153 if ((e = __bt_search(t, &c->key, &exact)) == NULL)
157 /* See if we got it in one shot. */
158 if (h->pgno == c->pg.pgno)
162 * Move right, looking for the page. At each move we have to move
163 * up the stack until we don't have to move to the next page. If
164 * we have to change pages at an internal level, we have to fix the
167 while (h->pgno != c->pg.pgno) {
168 if ((nextpg = h->nextpg) == P_INVALID)
170 mpool_put(t->bt_mp, h, 0);
172 /* Move up the stack. */
173 for (level = 0; (parent = BT_POP(t)) != NULL; ++level) {
174 /* Get the parent page. */
175 if ((h = mpool_get(t->bt_mp, parent->pgno, 0)) == NULL)
178 /* Move to the next index. */
179 if (parent->index != NEXTINDEX(h) - 1) {
180 idx = parent->index + 1;
181 BT_PUSH(t, h->pgno, idx);
184 mpool_put(t->bt_mp, h, 0);
187 /* Restore the stack. */
189 /* Push the next level down onto the stack. */
190 bi = GETBINTERNAL(h, idx);
194 /* Lose the currently pinned page. */
195 mpool_put(t->bt_mp, h, 0);
197 /* Get the next level down. */
198 if ((h = mpool_get(t->bt_mp, pgno, 0)) == NULL)
202 mpool_put(t->bt_mp, h, 0);
203 if ((h = mpool_get(t->bt_mp, nextpg, 0)) == NULL)
207 if (h->pgno == c->pg.pgno)
210 /* Reacquire the original stack. */
211 mpool_put(t->bt_mp, h, 0);
212 if ((e = __bt_search(t, &c->key, &exact)) == NULL)
217 * Move left, looking for the page. At each move we have to move
218 * up the stack until we don't have to change pages to move to the
219 * next page. If we have to change pages at an internal level, we
220 * have to fix the stack back up.
222 while (h->pgno != c->pg.pgno) {
223 if ((prevpg = h->prevpg) == P_INVALID)
225 mpool_put(t->bt_mp, h, 0);
227 /* Move up the stack. */
228 for (level = 0; (parent = BT_POP(t)) != NULL; ++level) {
229 /* Get the parent page. */
230 if ((h = mpool_get(t->bt_mp, parent->pgno, 0)) == NULL)
233 /* Move to the next index. */
234 if (parent->index != 0) {
235 idx = parent->index - 1;
236 BT_PUSH(t, h->pgno, idx);
239 mpool_put(t->bt_mp, h, 0);
242 /* Restore the stack. */
244 /* Push the next level down onto the stack. */
245 bi = GETBINTERNAL(h, idx);
248 /* Lose the currently pinned page. */
249 mpool_put(t->bt_mp, h, 0);
251 /* Get the next level down. */
252 if ((h = mpool_get(t->bt_mp, pgno, 0)) == NULL)
255 idx = NEXTINDEX(h) - 1;
256 BT_PUSH(t, pgno, idx);
258 mpool_put(t->bt_mp, h, 0);
259 if ((h = mpool_get(t->bt_mp, prevpg, 0)) == NULL)
264 ret: mpool_put(t->bt_mp, h, 0);
265 return ((*hp = mpool_get(t->bt_mp, c->pg.pgno, 0)) == NULL);
270 * Delete all key/data pairs matching the specified key.
277 * RET_ERROR, RET_SUCCESS and RET_SPECIAL if the key not found.
280 __bt_bdelete(BTREE *t, const DBT *key)
284 int deleted, exact, redo;
288 /* Find any matching record; __bt_search pins the page. */
289 loop: if ((e = __bt_search(t, key, &exact)) == NULL)
290 return (deleted ? RET_SUCCESS : RET_ERROR);
292 mpool_put(t->bt_mp, e->page, 0);
293 return (deleted ? RET_SUCCESS : RET_SPECIAL);
297 * Delete forward, then delete backward, from the found key. If
298 * there are duplicates and we reach either side of the page, do
299 * the key search again, so that we get them all.
304 if (__bt_dleaf(t, key, h, e->index)) {
305 mpool_put(t->bt_mp, h, 0);
308 if (F_ISSET(t, B_NODUPS)) {
309 if (NEXTINDEX(h) == 0) {
310 if (__bt_pdelete(t, h))
313 mpool_put(t->bt_mp, h, MPOOL_DIRTY);
314 return (RET_SUCCESS);
317 } while (e->index < NEXTINDEX(h) && __bt_cmp(t, key, e) == 0);
319 /* Check for right-hand edge of the page. */
320 if (e->index == NEXTINDEX(h))
323 /* Delete from the key to the beginning of the page. */
324 while (e->index-- > 0) {
325 if (__bt_cmp(t, key, e) != 0)
327 if (__bt_dleaf(t, key, h, e->index) == RET_ERROR) {
328 mpool_put(t->bt_mp, h, 0);
335 /* Check for an empty page. */
336 if (NEXTINDEX(h) == 0) {
337 if (__bt_pdelete(t, h))
343 mpool_put(t->bt_mp, h, MPOOL_DIRTY);
347 return (RET_SUCCESS);
352 * Delete a single page from the tree.
359 * RET_SUCCESS, RET_ERROR.
362 * mpool_put's the page
365 __bt_pdelete(BTREE *t, PAGE *h)
370 indx_t cnt, idx, *ip, offset;
375 * Walk the parent page stack -- a LIFO stack of the pages that were
376 * traversed when we searched for the page where the delete occurred.
377 * Each stack entry is a page number and a page index offset. The
378 * offset is for the page traversed on the search. We've just deleted
379 * a page, so we have to delete the key from the parent page.
381 * If the delete from the parent page makes it empty, this process may
382 * continue all the way up the tree. We stop if we reach the root page
383 * (which is never deleted, it's just not worth the effort) or if the
384 * delete does not empty the page.
386 while ((parent = BT_POP(t)) != NULL) {
387 /* Get the parent page. */
388 if ((pg = mpool_get(t->bt_mp, parent->pgno, 0)) == NULL)
392 bi = GETBINTERNAL(pg, idx);
394 /* Free any overflow pages. */
395 if (bi->flags & P_BIGKEY &&
396 __ovfl_delete(t, bi->bytes) == RET_ERROR) {
397 mpool_put(t->bt_mp, pg, 0);
402 * Free the parent if it has only the one key and it's not the
403 * root page. If it's the rootpage, turn it back into an empty
406 if (NEXTINDEX(pg) == 1)
407 if (pg->pgno == P_ROOT) {
408 pg->lower = BTDATAOFF;
409 pg->upper = t->bt_psize;
412 if (__bt_relink(t, pg) || __bt_free(t, pg))
417 /* Pack remaining key items at the end of the page. */
418 nksize = NBINTERNAL(bi->ksize);
419 from = (char *)pg + pg->upper;
420 memmove(from + nksize, from, (char *)bi - from);
423 /* Adjust indices' offsets, shift the indices down. */
424 offset = pg->linp[idx];
425 for (cnt = idx, ip = &pg->linp[0]; cnt--; ++ip)
428 for (cnt = NEXTINDEX(pg) - idx; --cnt; ++ip)
429 ip[0] = ip[1] < offset ? ip[1] + nksize : ip[1];
430 pg->lower -= sizeof(indx_t);
433 mpool_put(t->bt_mp, pg, MPOOL_DIRTY);
437 /* Free the leaf page, as long as it wasn't the root. */
438 if (h->pgno == P_ROOT) {
439 mpool_put(t->bt_mp, h, MPOOL_DIRTY);
440 return (RET_SUCCESS);
442 return (__bt_relink(t, h) || __bt_free(t, h));
447 * Delete a single record from a leaf page.
451 * key: referenced key
453 * idx: index on page to delete
456 * RET_SUCCESS, RET_ERROR.
459 __bt_dleaf(BTREE *t, const DBT *key, PAGE *h, u_int idx)
462 indx_t cnt, *ip, offset;
467 /* If this record is referenced by the cursor, delete the cursor. */
468 if (F_ISSET(&t->bt_cursor, CURS_INIT) &&
469 !F_ISSET(&t->bt_cursor, CURS_ACQUIRE) &&
470 t->bt_cursor.pg.pgno == h->pgno && t->bt_cursor.pg.index == idx &&
471 __bt_curdel(t, key, h, idx))
474 /* If the entry uses overflow pages, make them available for reuse. */
475 to = bl = GETBLEAF(h, idx);
476 if (bl->flags & P_BIGKEY && __ovfl_delete(t, bl->bytes) == RET_ERROR)
478 if (bl->flags & P_BIGDATA &&
479 __ovfl_delete(t, bl->bytes + bl->ksize) == RET_ERROR)
482 /* Pack the remaining key/data items at the end of the page. */
484 from = (char *)h + h->upper;
485 memmove(from + nbytes, from, (char *)to - from);
488 /* Adjust the indices' offsets, shift the indices down. */
489 offset = h->linp[idx];
490 for (cnt = idx, ip = &h->linp[0]; cnt--; ++ip)
493 for (cnt = NEXTINDEX(h) - idx; --cnt; ++ip)
494 ip[0] = ip[1] < offset ? ip[1] + nbytes : ip[1];
495 h->lower -= sizeof(indx_t);
497 /* If the cursor is on this page, adjust it as necessary. */
498 if (F_ISSET(&t->bt_cursor, CURS_INIT) &&
499 !F_ISSET(&t->bt_cursor, CURS_ACQUIRE) &&
500 t->bt_cursor.pg.pgno == h->pgno && t->bt_cursor.pg.index > idx)
501 --t->bt_cursor.pg.index;
503 return (RET_SUCCESS);
512 * key: referenced key (or NULL)
514 * idx: index on page to delete
517 * RET_SUCCESS, RET_ERROR.
520 __bt_curdel(BTREE *t, const DBT *key, PAGE *h, u_int idx)
528 * If there are duplicates, move forward or backward to one.
529 * Otherwise, copy the key into the cursor area.
532 F_CLR(c, CURS_AFTER | CURS_BEFORE | CURS_ACQUIRE);
535 if (!F_ISSET(t, B_NODUPS)) {
537 * We're going to have to do comparisons. If we weren't
538 * provided a copy of the key, i.e. the user is deleting
539 * the current cursor position, get one.
544 if ((status = __bt_ret(t, &e,
545 &c->key, &c->key, NULL, NULL, 1)) != RET_SUCCESS)
550 /* Check previous key, if not at the beginning of the page. */
554 if (__bt_cmp(t, key, &e) == 0) {
555 F_SET(c, CURS_BEFORE);
559 /* Check next key, if not at the end of the page. */
560 if (idx < NEXTINDEX(h) - 1) {
563 if (__bt_cmp(t, key, &e) == 0) {
564 F_SET(c, CURS_AFTER);
568 /* Check previous key if at the beginning of the page. */
569 if (idx == 0 && h->prevpg != P_INVALID) {
570 if ((pg = mpool_get(t->bt_mp, h->prevpg, 0)) == NULL)
573 e.index = NEXTINDEX(pg) - 1;
574 if (__bt_cmp(t, key, &e) == 0) {
575 F_SET(c, CURS_BEFORE);
578 mpool_put(t->bt_mp, pg, 0);
580 /* Check next key if at the end of the page. */
581 if (idx == NEXTINDEX(h) - 1 && h->nextpg != P_INVALID) {
582 if ((pg = mpool_get(t->bt_mp, h->nextpg, 0)) == NULL)
586 if (__bt_cmp(t, key, &e) == 0) {
587 F_SET(c, CURS_AFTER);
588 dup1: mpool_put(t->bt_mp, pg, 0);
589 dup2: c->pg.pgno = e.page->pgno;
590 c->pg.index = e.index;
591 return (RET_SUCCESS);
593 mpool_put(t->bt_mp, pg, 0);
598 if (curcopy || (status =
599 __bt_ret(t, &e, &c->key, &c->key, NULL, NULL, 1)) == RET_SUCCESS) {
600 F_SET(c, CURS_ACQUIRE);
601 return (RET_SUCCESS);
608 * Link around a deleted page.
612 * h: page to be deleted
615 __bt_relink(BTREE *t, PAGE *h)
619 if (h->nextpg != P_INVALID) {
620 if ((pg = mpool_get(t->bt_mp, h->nextpg, 0)) == NULL)
622 pg->prevpg = h->prevpg;
623 mpool_put(t->bt_mp, pg, MPOOL_DIRTY);
625 if (h->prevpg != P_INVALID) {
626 if ((pg = mpool_get(t->bt_mp, h->prevpg, 0)) == NULL)
628 pg->nextpg = h->nextpg;
629 mpool_put(t->bt_mp, pg, MPOOL_DIRTY);