2 * SPDX-License-Identifier: BSD-3-Clause
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 defined(LIBC_SCCS) && !defined(lint)
36 static char sccsid[] = "@(#)bt_delete.c 8.13 (Berkeley) 7/28/94";
37 #endif /* LIBC_SCCS and not lint */
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
41 #include <sys/types.h>
50 static int __bt_bdelete(BTREE *, const DBT *);
51 static int __bt_curdel(BTREE *, const DBT *, PAGE *, u_int);
52 static int __bt_pdelete(BTREE *, PAGE *);
53 static int __bt_relink(BTREE *, PAGE *);
54 static int __bt_stkacq(BTREE *, PAGE **, CURSOR *);
58 * Delete the item(s) referenced by a key.
60 * Return RET_SPECIAL if the key is not found.
63 __bt_delete(const DB *dbp, const DBT *key, u_int flags)
72 /* Toss any page pinned across calls. */
73 if (t->bt_pinned != NULL) {
74 mpool_put(t->bt_mp, t->bt_pinned, 0);
78 /* Check for change to a read-only tree. */
79 if (F_ISSET(t, B_RDONLY)) {
86 status = __bt_bdelete(t, key);
90 * If flags is R_CURSOR, delete the cursor. Must already
91 * have started a scan and not have already deleted it.
94 if (F_ISSET(c, CURS_INIT)) {
95 if (F_ISSET(c, CURS_ACQUIRE | CURS_AFTER | CURS_BEFORE))
97 if ((h = mpool_get(t->bt_mp, c->pg.pgno, 0)) == NULL)
101 * If the page is about to be emptied, we'll need to
102 * delete it, which means we have to acquire a stack.
104 if (NEXTINDEX(h) == 1)
105 if (__bt_stkacq(t, &h, &t->bt_cursor))
108 status = __bt_dleaf(t, NULL, h, c->pg.index);
110 if (NEXTINDEX(h) == 0 && status == RET_SUCCESS) {
111 if (__bt_pdelete(t, h))
115 h, status == RET_SUCCESS ? MPOOL_DIRTY : 0);
123 if (status == RET_SUCCESS)
124 F_SET(t, B_MODIFIED);
130 * Acquire a stack so we can delete a cursor entry.
134 * hp: pointer to current, pinned PAGE pointer
135 * c: pointer to the cursor
138 * 0 on success, 1 on failure
141 __bt_stkacq(BTREE *t, PAGE **hp, CURSOR *c)
149 recno_t nextpg, prevpg;
153 * Find the first occurrence of the key in the tree. Toss the
154 * currently locked page so we don't hit an already-locked page.
157 mpool_put(t->bt_mp, h, 0);
158 if ((e = __bt_search(t, &c->key, &exact)) == NULL)
162 /* See if we got it in one shot. */
163 if (h->pgno == c->pg.pgno)
167 * Move right, looking for the page. At each move we have to move
168 * up the stack until we don't have to move to the next page. If
169 * we have to change pages at an internal level, we have to fix the
172 while (h->pgno != c->pg.pgno) {
173 if ((nextpg = h->nextpg) == P_INVALID)
175 mpool_put(t->bt_mp, h, 0);
177 /* Move up the stack. */
178 for (level = 0; (parent = BT_POP(t)) != NULL; ++level) {
179 /* Get the parent page. */
180 if ((h = mpool_get(t->bt_mp, parent->pgno, 0)) == NULL)
183 /* Move to the next index. */
184 if (parent->index != NEXTINDEX(h) - 1) {
185 idx = parent->index + 1;
186 BT_PUSH(t, h->pgno, idx);
189 mpool_put(t->bt_mp, h, 0);
192 /* Restore the stack. */
194 /* Push the next level down onto the stack. */
195 bi = GETBINTERNAL(h, idx);
199 /* Lose the currently pinned page. */
200 mpool_put(t->bt_mp, h, 0);
202 /* Get the next level down. */
203 if ((h = mpool_get(t->bt_mp, pgno, 0)) == NULL)
207 mpool_put(t->bt_mp, h, 0);
208 if ((h = mpool_get(t->bt_mp, nextpg, 0)) == NULL)
212 if (h->pgno == c->pg.pgno)
215 /* Reacquire the original stack. */
216 mpool_put(t->bt_mp, h, 0);
217 if ((e = __bt_search(t, &c->key, &exact)) == NULL)
222 * Move left, looking for the page. At each move we have to move
223 * up the stack until we don't have to change pages to move to the
224 * next page. If we have to change pages at an internal level, we
225 * have to fix the stack back up.
227 while (h->pgno != c->pg.pgno) {
228 if ((prevpg = h->prevpg) == P_INVALID)
230 mpool_put(t->bt_mp, h, 0);
232 /* Move up the stack. */
233 for (level = 0; (parent = BT_POP(t)) != NULL; ++level) {
234 /* Get the parent page. */
235 if ((h = mpool_get(t->bt_mp, parent->pgno, 0)) == NULL)
238 /* Move to the next index. */
239 if (parent->index != 0) {
240 idx = parent->index - 1;
241 BT_PUSH(t, h->pgno, idx);
244 mpool_put(t->bt_mp, h, 0);
247 /* Restore the stack. */
249 /* Push the next level down onto the stack. */
250 bi = GETBINTERNAL(h, idx);
253 /* Lose the currently pinned page. */
254 mpool_put(t->bt_mp, h, 0);
256 /* Get the next level down. */
257 if ((h = mpool_get(t->bt_mp, pgno, 0)) == NULL)
260 idx = NEXTINDEX(h) - 1;
261 BT_PUSH(t, pgno, idx);
263 mpool_put(t->bt_mp, h, 0);
264 if ((h = mpool_get(t->bt_mp, prevpg, 0)) == NULL)
269 ret: mpool_put(t->bt_mp, h, 0);
270 return ((*hp = mpool_get(t->bt_mp, c->pg.pgno, 0)) == NULL);
275 * Delete all key/data pairs matching the specified key.
282 * RET_ERROR, RET_SUCCESS and RET_SPECIAL if the key not found.
285 __bt_bdelete(BTREE *t, const DBT *key)
289 int deleted, exact, redo;
293 /* Find any matching record; __bt_search pins the page. */
294 loop: if ((e = __bt_search(t, key, &exact)) == NULL)
295 return (deleted ? RET_SUCCESS : RET_ERROR);
297 mpool_put(t->bt_mp, e->page, 0);
298 return (deleted ? RET_SUCCESS : RET_SPECIAL);
302 * Delete forward, then delete backward, from the found key. If
303 * there are duplicates and we reach either side of the page, do
304 * the key search again, so that we get them all.
309 if (__bt_dleaf(t, key, h, e->index)) {
310 mpool_put(t->bt_mp, h, 0);
313 if (F_ISSET(t, B_NODUPS)) {
314 if (NEXTINDEX(h) == 0) {
315 if (__bt_pdelete(t, h))
318 mpool_put(t->bt_mp, h, MPOOL_DIRTY);
319 return (RET_SUCCESS);
322 } while (e->index < NEXTINDEX(h) && __bt_cmp(t, key, e) == 0);
324 /* Check for right-hand edge of the page. */
325 if (e->index == NEXTINDEX(h))
328 /* Delete from the key to the beginning of the page. */
329 while (e->index-- > 0) {
330 if (__bt_cmp(t, key, e) != 0)
332 if (__bt_dleaf(t, key, h, e->index) == RET_ERROR) {
333 mpool_put(t->bt_mp, h, 0);
340 /* Check for an empty page. */
341 if (NEXTINDEX(h) == 0) {
342 if (__bt_pdelete(t, h))
348 mpool_put(t->bt_mp, h, MPOOL_DIRTY);
352 return (RET_SUCCESS);
357 * Delete a single page from the tree.
364 * RET_SUCCESS, RET_ERROR.
367 * mpool_put's the page
370 __bt_pdelete(BTREE *t, PAGE *h)
375 indx_t cnt, idx, *ip, offset;
380 * Walk the parent page stack -- a LIFO stack of the pages that were
381 * traversed when we searched for the page where the delete occurred.
382 * Each stack entry is a page number and a page index offset. The
383 * offset is for the page traversed on the search. We've just deleted
384 * a page, so we have to delete the key from the parent page.
386 * If the delete from the parent page makes it empty, this process may
387 * continue all the way up the tree. We stop if we reach the root page
388 * (which is never deleted, it's just not worth the effort) or if the
389 * delete does not empty the page.
391 while ((parent = BT_POP(t)) != NULL) {
392 /* Get the parent page. */
393 if ((pg = mpool_get(t->bt_mp, parent->pgno, 0)) == NULL)
397 bi = GETBINTERNAL(pg, idx);
399 /* Free any overflow pages. */
400 if (bi->flags & P_BIGKEY &&
401 __ovfl_delete(t, bi->bytes) == RET_ERROR) {
402 mpool_put(t->bt_mp, pg, 0);
407 * Free the parent if it has only the one key and it's not the
408 * root page. If it's the rootpage, turn it back into an empty
411 if (NEXTINDEX(pg) == 1) {
412 if (pg->pgno == P_ROOT) {
413 pg->lower = BTDATAOFF;
414 pg->upper = t->bt_psize;
417 if (__bt_relink(t, pg) || __bt_free(t, pg))
422 /* Pack remaining key items at the end of the page. */
423 nksize = NBINTERNAL(bi->ksize);
424 from = (char *)pg + pg->upper;
425 memmove(from + nksize, from, (char *)bi - from);
428 /* Adjust indices' offsets, shift the indices down. */
429 offset = pg->linp[idx];
430 for (cnt = idx, ip = &pg->linp[0]; cnt--; ++ip)
433 for (cnt = NEXTINDEX(pg) - idx; --cnt; ++ip)
434 ip[0] = ip[1] < offset ? ip[1] + nksize : ip[1];
435 pg->lower -= sizeof(indx_t);
438 mpool_put(t->bt_mp, pg, MPOOL_DIRTY);
442 /* Free the leaf page, as long as it wasn't the root. */
443 if (h->pgno == P_ROOT) {
444 mpool_put(t->bt_mp, h, MPOOL_DIRTY);
445 return (RET_SUCCESS);
447 return (__bt_relink(t, h) || __bt_free(t, h));
452 * Delete a single record from a leaf page.
456 * key: referenced key
458 * idx: index on page to delete
461 * RET_SUCCESS, RET_ERROR.
464 __bt_dleaf(BTREE *t, const DBT *key, PAGE *h, u_int idx)
467 indx_t cnt, *ip, offset;
472 /* If this record is referenced by the cursor, delete the cursor. */
473 if (F_ISSET(&t->bt_cursor, CURS_INIT) &&
474 !F_ISSET(&t->bt_cursor, CURS_ACQUIRE) &&
475 t->bt_cursor.pg.pgno == h->pgno && t->bt_cursor.pg.index == idx &&
476 __bt_curdel(t, key, h, idx))
479 /* If the entry uses overflow pages, make them available for reuse. */
480 to = bl = GETBLEAF(h, idx);
481 if (bl->flags & P_BIGKEY && __ovfl_delete(t, bl->bytes) == RET_ERROR)
483 if (bl->flags & P_BIGDATA &&
484 __ovfl_delete(t, bl->bytes + bl->ksize) == RET_ERROR)
487 /* Pack the remaining key/data items at the end of the page. */
489 from = (char *)h + h->upper;
490 memmove(from + nbytes, from, (char *)to - from);
493 /* Adjust the indices' offsets, shift the indices down. */
494 offset = h->linp[idx];
495 for (cnt = idx, ip = &h->linp[0]; cnt--; ++ip)
498 for (cnt = NEXTINDEX(h) - idx; --cnt; ++ip)
499 ip[0] = ip[1] < offset ? ip[1] + nbytes : ip[1];
500 h->lower -= sizeof(indx_t);
502 /* If the cursor is on this page, adjust it as necessary. */
503 if (F_ISSET(&t->bt_cursor, CURS_INIT) &&
504 !F_ISSET(&t->bt_cursor, CURS_ACQUIRE) &&
505 t->bt_cursor.pg.pgno == h->pgno && t->bt_cursor.pg.index > idx)
506 --t->bt_cursor.pg.index;
508 return (RET_SUCCESS);
517 * key: referenced key (or NULL)
519 * idx: index on page to delete
522 * RET_SUCCESS, RET_ERROR.
525 __bt_curdel(BTREE *t, const DBT *key, PAGE *h, u_int idx)
533 * If there are duplicates, move forward or backward to one.
534 * Otherwise, copy the key into the cursor area.
537 F_CLR(c, CURS_AFTER | CURS_BEFORE | CURS_ACQUIRE);
540 if (!F_ISSET(t, B_NODUPS)) {
542 * We're going to have to do comparisons. If we weren't
543 * provided a copy of the key, i.e. the user is deleting
544 * the current cursor position, get one.
549 if ((status = __bt_ret(t, &e,
550 &c->key, &c->key, NULL, NULL, 1)) != RET_SUCCESS)
555 /* Check previous key, if not at the beginning of the page. */
559 if (__bt_cmp(t, key, &e) == 0) {
560 F_SET(c, CURS_BEFORE);
564 /* Check next key, if not at the end of the page. */
565 if (idx < NEXTINDEX(h) - 1) {
568 if (__bt_cmp(t, key, &e) == 0) {
569 F_SET(c, CURS_AFTER);
573 /* Check previous key if at the beginning of the page. */
574 if (idx == 0 && h->prevpg != P_INVALID) {
575 if ((pg = mpool_get(t->bt_mp, h->prevpg, 0)) == NULL)
578 e.index = NEXTINDEX(pg) - 1;
579 if (__bt_cmp(t, key, &e) == 0) {
580 F_SET(c, CURS_BEFORE);
583 mpool_put(t->bt_mp, pg, 0);
585 /* Check next key if at the end of the page. */
586 if (idx == NEXTINDEX(h) - 1 && h->nextpg != P_INVALID) {
587 if ((pg = mpool_get(t->bt_mp, h->nextpg, 0)) == NULL)
591 if (__bt_cmp(t, key, &e) == 0) {
592 F_SET(c, CURS_AFTER);
593 dup1: mpool_put(t->bt_mp, pg, 0);
594 dup2: c->pg.pgno = e.page->pgno;
595 c->pg.index = e.index;
596 return (RET_SUCCESS);
598 mpool_put(t->bt_mp, pg, 0);
603 if (curcopy || (status =
604 __bt_ret(t, &e, &c->key, &c->key, NULL, NULL, 1)) == RET_SUCCESS) {
605 F_SET(c, CURS_ACQUIRE);
606 return (RET_SUCCESS);
613 * Link around a deleted page.
617 * h: page to be deleted
620 __bt_relink(BTREE *t, PAGE *h)
624 if (h->nextpg != P_INVALID) {
625 if ((pg = mpool_get(t->bt_mp, h->nextpg, 0)) == NULL)
627 pg->prevpg = h->prevpg;
628 mpool_put(t->bt_mp, pg, MPOOL_DIRTY);
630 if (h->prevpg != P_INVALID) {
631 if ((pg = mpool_get(t->bt_mp, h->prevpg, 0)) == NULL)
633 pg->nextpg = h->nextpg;
634 mpool_put(t->bt_mp, pg, MPOOL_DIRTY);