158f0484fSRodney W. Grimes /*- 2*8a16b7a1SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause 3*8a16b7a1SPedro F. Giffuni * 4ef5d438eSPaul Traina * Copyright (c) 1990, 1993, 1994 558f0484fSRodney W. Grimes * The Regents of the University of California. All rights reserved. 658f0484fSRodney W. Grimes * 758f0484fSRodney W. Grimes * This code is derived from software contributed to Berkeley by 858f0484fSRodney W. Grimes * Mike Olson. 958f0484fSRodney W. Grimes * 1058f0484fSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 1158f0484fSRodney W. Grimes * modification, are permitted provided that the following conditions 1258f0484fSRodney W. Grimes * are met: 1358f0484fSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 1458f0484fSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 1558f0484fSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 1658f0484fSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 1758f0484fSRodney W. Grimes * documentation and/or other materials provided with the distribution. 18fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors 1958f0484fSRodney W. Grimes * may be used to endorse or promote products derived from this software 2058f0484fSRodney W. Grimes * without specific prior written permission. 2158f0484fSRodney W. Grimes * 2258f0484fSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2358f0484fSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2458f0484fSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2558f0484fSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2658f0484fSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2758f0484fSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2858f0484fSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2958f0484fSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 3058f0484fSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3158f0484fSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3258f0484fSRodney W. Grimes * SUCH DAMAGE. 3358f0484fSRodney W. Grimes */ 3458f0484fSRodney W. Grimes 3558f0484fSRodney W. Grimes #if defined(LIBC_SCCS) && !defined(lint) 36ef5d438eSPaul Traina static char sccsid[] = "@(#)bt_delete.c 8.13 (Berkeley) 7/28/94"; 3758f0484fSRodney W. Grimes #endif /* LIBC_SCCS and not lint */ 38c05ac53bSDavid E. O'Brien #include <sys/cdefs.h> 39c05ac53bSDavid E. O'Brien __FBSDID("$FreeBSD$"); 4058f0484fSRodney W. Grimes 4158f0484fSRodney W. Grimes #include <sys/types.h> 4258f0484fSRodney W. Grimes 4358f0484fSRodney W. Grimes #include <errno.h> 4458f0484fSRodney W. Grimes #include <stdio.h> 4558f0484fSRodney W. Grimes #include <string.h> 4658f0484fSRodney W. Grimes 4758f0484fSRodney W. Grimes #include <db.h> 4858f0484fSRodney W. Grimes #include "btree.h" 4958f0484fSRodney W. Grimes 50c05ac53bSDavid E. O'Brien static int __bt_bdelete(BTREE *, const DBT *); 51c05ac53bSDavid E. O'Brien static int __bt_curdel(BTREE *, const DBT *, PAGE *, u_int); 52c05ac53bSDavid E. O'Brien static int __bt_pdelete(BTREE *, PAGE *); 53c05ac53bSDavid E. O'Brien static int __bt_relink(BTREE *, PAGE *); 54c05ac53bSDavid E. O'Brien static int __bt_stkacq(BTREE *, PAGE **, CURSOR *); 5558f0484fSRodney W. Grimes 5658f0484fSRodney W. Grimes /* 57ef5d438eSPaul Traina * __bt_delete 58ef5d438eSPaul Traina * Delete the item(s) referenced by a key. 5958f0484fSRodney W. Grimes * 60ef5d438eSPaul Traina * Return RET_SPECIAL if the key is not found. 6158f0484fSRodney W. Grimes */ 6258f0484fSRodney W. Grimes int 630ac22237SXin LI __bt_delete(const DB *dbp, const DBT *key, u_int flags) 6458f0484fSRodney W. Grimes { 6558f0484fSRodney W. Grimes BTREE *t; 66ef5d438eSPaul Traina CURSOR *c; 67ef5d438eSPaul Traina PAGE *h; 6858f0484fSRodney W. Grimes int status; 6958f0484fSRodney W. Grimes 7058f0484fSRodney W. Grimes t = dbp->internal; 7158f0484fSRodney W. Grimes 7258f0484fSRodney W. Grimes /* Toss any page pinned across calls. */ 7358f0484fSRodney W. Grimes if (t->bt_pinned != NULL) { 7458f0484fSRodney W. Grimes mpool_put(t->bt_mp, t->bt_pinned, 0); 7558f0484fSRodney W. Grimes t->bt_pinned = NULL; 7658f0484fSRodney W. Grimes } 7758f0484fSRodney W. Grimes 78ef5d438eSPaul Traina /* Check for change to a read-only tree. */ 79ef5d438eSPaul Traina if (F_ISSET(t, B_RDONLY)) { 8058f0484fSRodney W. Grimes errno = EPERM; 8158f0484fSRodney W. Grimes return (RET_ERROR); 8258f0484fSRodney W. Grimes } 8358f0484fSRodney W. Grimes 8458f0484fSRodney W. Grimes switch (flags) { 8558f0484fSRodney W. Grimes case 0: 86ef5d438eSPaul Traina status = __bt_bdelete(t, key); 8758f0484fSRodney W. Grimes break; 8858f0484fSRodney W. Grimes case R_CURSOR: 8958f0484fSRodney W. Grimes /* 90ef5d438eSPaul Traina * If flags is R_CURSOR, delete the cursor. Must already 91ef5d438eSPaul Traina * have started a scan and not have already deleted it. 9258f0484fSRodney W. Grimes */ 93ef5d438eSPaul Traina c = &t->bt_cursor; 94ef5d438eSPaul Traina if (F_ISSET(c, CURS_INIT)) { 95ef5d438eSPaul Traina if (F_ISSET(c, CURS_ACQUIRE | CURS_AFTER | CURS_BEFORE)) 96ef5d438eSPaul Traina return (RET_SPECIAL); 97ef5d438eSPaul Traina if ((h = mpool_get(t->bt_mp, c->pg.pgno, 0)) == NULL) 98ef5d438eSPaul Traina return (RET_ERROR); 99ef5d438eSPaul Traina 100ef5d438eSPaul Traina /* 101ef5d438eSPaul Traina * If the page is about to be emptied, we'll need to 102ef5d438eSPaul Traina * delete it, which means we have to acquire a stack. 103ef5d438eSPaul Traina */ 104ef5d438eSPaul Traina if (NEXTINDEX(h) == 1) 105ef5d438eSPaul Traina if (__bt_stkacq(t, &h, &t->bt_cursor)) 106ef5d438eSPaul Traina return (RET_ERROR); 107ef5d438eSPaul Traina 108ef5d438eSPaul Traina status = __bt_dleaf(t, NULL, h, c->pg.index); 109ef5d438eSPaul Traina 110ef5d438eSPaul Traina if (NEXTINDEX(h) == 0 && status == RET_SUCCESS) { 111ef5d438eSPaul Traina if (__bt_pdelete(t, h)) 112ef5d438eSPaul Traina return (RET_ERROR); 113ef5d438eSPaul Traina } else 114ef5d438eSPaul Traina mpool_put(t->bt_mp, 115ef5d438eSPaul Traina h, status == RET_SUCCESS ? MPOOL_DIRTY : 0); 11658f0484fSRodney W. Grimes break; 117ef5d438eSPaul Traina } 118ef5d438eSPaul Traina /* FALLTHROUGH */ 11958f0484fSRodney W. Grimes default: 120ef5d438eSPaul Traina errno = EINVAL; 12158f0484fSRodney W. Grimes return (RET_ERROR); 12258f0484fSRodney W. Grimes } 12358f0484fSRodney W. Grimes if (status == RET_SUCCESS) 124ef5d438eSPaul Traina F_SET(t, B_MODIFIED); 12558f0484fSRodney W. Grimes return (status); 12658f0484fSRodney W. Grimes } 12758f0484fSRodney W. Grimes 12858f0484fSRodney W. Grimes /* 129ef5d438eSPaul Traina * __bt_stkacq -- 130ef5d438eSPaul Traina * Acquire a stack so we can delete a cursor entry. 13158f0484fSRodney W. Grimes * 13258f0484fSRodney W. Grimes * Parameters: 133ef5d438eSPaul Traina * t: tree 134ef5d438eSPaul Traina * hp: pointer to current, pinned PAGE pointer 135ef5d438eSPaul Traina * c: pointer to the cursor 136ef5d438eSPaul Traina * 137ef5d438eSPaul Traina * Returns: 138ef5d438eSPaul Traina * 0 on success, 1 on failure 139ef5d438eSPaul Traina */ 140ef5d438eSPaul Traina static int 1410ac22237SXin LI __bt_stkacq(BTREE *t, PAGE **hp, CURSOR *c) 142ef5d438eSPaul Traina { 143ef5d438eSPaul Traina BINTERNAL *bi; 144ef5d438eSPaul Traina EPG *e; 145ef5d438eSPaul Traina EPGNO *parent; 146ef5d438eSPaul Traina PAGE *h; 1474c66e4b6SXin LI indx_t idx; 148ef5d438eSPaul Traina pgno_t pgno; 149ef5d438eSPaul Traina recno_t nextpg, prevpg; 150ef5d438eSPaul Traina int exact, level; 151ef5d438eSPaul Traina 152ef5d438eSPaul Traina /* 153ef5d438eSPaul Traina * Find the first occurrence of the key in the tree. Toss the 154ef5d438eSPaul Traina * currently locked page so we don't hit an already-locked page. 155ef5d438eSPaul Traina */ 156ef5d438eSPaul Traina h = *hp; 157ef5d438eSPaul Traina mpool_put(t->bt_mp, h, 0); 158ef5d438eSPaul Traina if ((e = __bt_search(t, &c->key, &exact)) == NULL) 159ef5d438eSPaul Traina return (1); 160ef5d438eSPaul Traina h = e->page; 161ef5d438eSPaul Traina 162ef5d438eSPaul Traina /* See if we got it in one shot. */ 163ef5d438eSPaul Traina if (h->pgno == c->pg.pgno) 164ef5d438eSPaul Traina goto ret; 165ef5d438eSPaul Traina 166ef5d438eSPaul Traina /* 167ef5d438eSPaul Traina * Move right, looking for the page. At each move we have to move 168ef5d438eSPaul Traina * up the stack until we don't have to move to the next page. If 169ef5d438eSPaul Traina * we have to change pages at an internal level, we have to fix the 170ef5d438eSPaul Traina * stack back up. 171ef5d438eSPaul Traina */ 172ef5d438eSPaul Traina while (h->pgno != c->pg.pgno) { 173ef5d438eSPaul Traina if ((nextpg = h->nextpg) == P_INVALID) 174ef5d438eSPaul Traina break; 175ef5d438eSPaul Traina mpool_put(t->bt_mp, h, 0); 176ef5d438eSPaul Traina 177ef5d438eSPaul Traina /* Move up the stack. */ 178ef5d438eSPaul Traina for (level = 0; (parent = BT_POP(t)) != NULL; ++level) { 179ef5d438eSPaul Traina /* Get the parent page. */ 180ef5d438eSPaul Traina if ((h = mpool_get(t->bt_mp, parent->pgno, 0)) == NULL) 181ef5d438eSPaul Traina return (1); 182ef5d438eSPaul Traina 183ef5d438eSPaul Traina /* Move to the next index. */ 184ef5d438eSPaul Traina if (parent->index != NEXTINDEX(h) - 1) { 1854c66e4b6SXin LI idx = parent->index + 1; 1864c66e4b6SXin LI BT_PUSH(t, h->pgno, idx); 187ef5d438eSPaul Traina break; 188ef5d438eSPaul Traina } 189ef5d438eSPaul Traina mpool_put(t->bt_mp, h, 0); 190ef5d438eSPaul Traina } 191ef5d438eSPaul Traina 192ef5d438eSPaul Traina /* Restore the stack. */ 193ef5d438eSPaul Traina while (level--) { 194ef5d438eSPaul Traina /* Push the next level down onto the stack. */ 1954c66e4b6SXin LI bi = GETBINTERNAL(h, idx); 196ef5d438eSPaul Traina pgno = bi->pgno; 197ef5d438eSPaul Traina BT_PUSH(t, pgno, 0); 198ef5d438eSPaul Traina 199ef5d438eSPaul Traina /* Lose the currently pinned page. */ 200ef5d438eSPaul Traina mpool_put(t->bt_mp, h, 0); 201ef5d438eSPaul Traina 202ef5d438eSPaul Traina /* Get the next level down. */ 203ef5d438eSPaul Traina if ((h = mpool_get(t->bt_mp, pgno, 0)) == NULL) 204ef5d438eSPaul Traina return (1); 2054c66e4b6SXin LI idx = 0; 206ef5d438eSPaul Traina } 207ef5d438eSPaul Traina mpool_put(t->bt_mp, h, 0); 208ef5d438eSPaul Traina if ((h = mpool_get(t->bt_mp, nextpg, 0)) == NULL) 209ef5d438eSPaul Traina return (1); 210ef5d438eSPaul Traina } 211ef5d438eSPaul Traina 212ef5d438eSPaul Traina if (h->pgno == c->pg.pgno) 213ef5d438eSPaul Traina goto ret; 214ef5d438eSPaul Traina 215ef5d438eSPaul Traina /* Reacquire the original stack. */ 216ef5d438eSPaul Traina mpool_put(t->bt_mp, h, 0); 217ef5d438eSPaul Traina if ((e = __bt_search(t, &c->key, &exact)) == NULL) 218ef5d438eSPaul Traina return (1); 219ef5d438eSPaul Traina h = e->page; 220ef5d438eSPaul Traina 221ef5d438eSPaul Traina /* 222ef5d438eSPaul Traina * Move left, looking for the page. At each move we have to move 223ef5d438eSPaul Traina * up the stack until we don't have to change pages to move to the 224ef5d438eSPaul Traina * next page. If we have to change pages at an internal level, we 225ef5d438eSPaul Traina * have to fix the stack back up. 226ef5d438eSPaul Traina */ 227ef5d438eSPaul Traina while (h->pgno != c->pg.pgno) { 228ef5d438eSPaul Traina if ((prevpg = h->prevpg) == P_INVALID) 229ef5d438eSPaul Traina break; 230ef5d438eSPaul Traina mpool_put(t->bt_mp, h, 0); 231ef5d438eSPaul Traina 232ef5d438eSPaul Traina /* Move up the stack. */ 233ef5d438eSPaul Traina for (level = 0; (parent = BT_POP(t)) != NULL; ++level) { 234ef5d438eSPaul Traina /* Get the parent page. */ 235ef5d438eSPaul Traina if ((h = mpool_get(t->bt_mp, parent->pgno, 0)) == NULL) 236ef5d438eSPaul Traina return (1); 237ef5d438eSPaul Traina 238ef5d438eSPaul Traina /* Move to the next index. */ 239ef5d438eSPaul Traina if (parent->index != 0) { 2404c66e4b6SXin LI idx = parent->index - 1; 2414c66e4b6SXin LI BT_PUSH(t, h->pgno, idx); 242ef5d438eSPaul Traina break; 243ef5d438eSPaul Traina } 244ef5d438eSPaul Traina mpool_put(t->bt_mp, h, 0); 245ef5d438eSPaul Traina } 246ef5d438eSPaul Traina 247ef5d438eSPaul Traina /* Restore the stack. */ 248ef5d438eSPaul Traina while (level--) { 249ef5d438eSPaul Traina /* Push the next level down onto the stack. */ 2504c66e4b6SXin LI bi = GETBINTERNAL(h, idx); 251ef5d438eSPaul Traina pgno = bi->pgno; 252ef5d438eSPaul Traina 253ef5d438eSPaul Traina /* Lose the currently pinned page. */ 254ef5d438eSPaul Traina mpool_put(t->bt_mp, h, 0); 255ef5d438eSPaul Traina 256ef5d438eSPaul Traina /* Get the next level down. */ 257ef5d438eSPaul Traina if ((h = mpool_get(t->bt_mp, pgno, 0)) == NULL) 258ef5d438eSPaul Traina return (1); 259ef5d438eSPaul Traina 2604c66e4b6SXin LI idx = NEXTINDEX(h) - 1; 2614c66e4b6SXin LI BT_PUSH(t, pgno, idx); 262ef5d438eSPaul Traina } 263ef5d438eSPaul Traina mpool_put(t->bt_mp, h, 0); 264ef5d438eSPaul Traina if ((h = mpool_get(t->bt_mp, prevpg, 0)) == NULL) 265ef5d438eSPaul Traina return (1); 266ef5d438eSPaul Traina } 267ef5d438eSPaul Traina 268ef5d438eSPaul Traina 269ef5d438eSPaul Traina ret: mpool_put(t->bt_mp, h, 0); 270ef5d438eSPaul Traina return ((*hp = mpool_get(t->bt_mp, c->pg.pgno, 0)) == NULL); 271ef5d438eSPaul Traina } 272ef5d438eSPaul Traina 273ef5d438eSPaul Traina /* 274ef5d438eSPaul Traina * __bt_bdelete -- 275ef5d438eSPaul Traina * Delete all key/data pairs matching the specified key. 276ef5d438eSPaul Traina * 277ef5d438eSPaul Traina * Parameters: 278ef5d438eSPaul Traina * t: tree 27958f0484fSRodney W. Grimes * key: key to delete 28058f0484fSRodney W. Grimes * 28158f0484fSRodney W. Grimes * Returns: 28258f0484fSRodney W. Grimes * RET_ERROR, RET_SUCCESS and RET_SPECIAL if the key not found. 28358f0484fSRodney W. Grimes */ 28458f0484fSRodney W. Grimes static int 2850ac22237SXin LI __bt_bdelete(BTREE *t, const DBT *key) 28658f0484fSRodney W. Grimes { 287ef5d438eSPaul Traina EPG *e; 28858f0484fSRodney W. Grimes PAGE *h; 289ef5d438eSPaul Traina int deleted, exact, redo; 290ef5d438eSPaul Traina 291ef5d438eSPaul Traina deleted = 0; 29258f0484fSRodney W. Grimes 29358f0484fSRodney W. Grimes /* Find any matching record; __bt_search pins the page. */ 294ef5d438eSPaul Traina loop: if ((e = __bt_search(t, key, &exact)) == NULL) 295ef5d438eSPaul Traina return (deleted ? RET_SUCCESS : RET_ERROR); 29658f0484fSRodney W. Grimes if (!exact) { 29758f0484fSRodney W. Grimes mpool_put(t->bt_mp, e->page, 0); 29858f0484fSRodney W. Grimes return (deleted ? RET_SUCCESS : RET_SPECIAL); 29958f0484fSRodney W. Grimes } 30058f0484fSRodney W. Grimes 30158f0484fSRodney W. Grimes /* 302ef5d438eSPaul Traina * Delete forward, then delete backward, from the found key. If 303ef5d438eSPaul Traina * there are duplicates and we reach either side of the page, do 304ef5d438eSPaul Traina * the key search again, so that we get them all. 305ef5d438eSPaul Traina */ 306ef5d438eSPaul Traina redo = 0; 307ef5d438eSPaul Traina h = e->page; 308ef5d438eSPaul Traina do { 309ef5d438eSPaul Traina if (__bt_dleaf(t, key, h, e->index)) { 310ef5d438eSPaul Traina mpool_put(t->bt_mp, h, 0); 311ef5d438eSPaul Traina return (RET_ERROR); 312ef5d438eSPaul Traina } 313ef5d438eSPaul Traina if (F_ISSET(t, B_NODUPS)) { 314ef5d438eSPaul Traina if (NEXTINDEX(h) == 0) { 315ef5d438eSPaul Traina if (__bt_pdelete(t, h)) 316ef5d438eSPaul Traina return (RET_ERROR); 317ef5d438eSPaul Traina } else 318ef5d438eSPaul Traina mpool_put(t->bt_mp, h, MPOOL_DIRTY); 319ef5d438eSPaul Traina return (RET_SUCCESS); 320ef5d438eSPaul Traina } 321ef5d438eSPaul Traina deleted = 1; 322ef5d438eSPaul Traina } while (e->index < NEXTINDEX(h) && __bt_cmp(t, key, e) == 0); 323ef5d438eSPaul Traina 324ef5d438eSPaul Traina /* Check for right-hand edge of the page. */ 325ef5d438eSPaul Traina if (e->index == NEXTINDEX(h)) 326ef5d438eSPaul Traina redo = 1; 327ef5d438eSPaul Traina 328ef5d438eSPaul Traina /* Delete from the key to the beginning of the page. */ 329ef5d438eSPaul Traina while (e->index-- > 0) { 330ef5d438eSPaul Traina if (__bt_cmp(t, key, e) != 0) 331ef5d438eSPaul Traina break; 332ef5d438eSPaul Traina if (__bt_dleaf(t, key, h, e->index) == RET_ERROR) { 333ef5d438eSPaul Traina mpool_put(t->bt_mp, h, 0); 334ef5d438eSPaul Traina return (RET_ERROR); 335ef5d438eSPaul Traina } 336ef5d438eSPaul Traina if (e->index == 0) 337ef5d438eSPaul Traina redo = 1; 338ef5d438eSPaul Traina } 339ef5d438eSPaul Traina 340ef5d438eSPaul Traina /* Check for an empty page. */ 341ef5d438eSPaul Traina if (NEXTINDEX(h) == 0) { 342ef5d438eSPaul Traina if (__bt_pdelete(t, h)) 343ef5d438eSPaul Traina return (RET_ERROR); 344ef5d438eSPaul Traina goto loop; 345ef5d438eSPaul Traina } 346ef5d438eSPaul Traina 347ef5d438eSPaul Traina /* Put the page. */ 348ef5d438eSPaul Traina mpool_put(t->bt_mp, h, MPOOL_DIRTY); 349ef5d438eSPaul Traina 350ef5d438eSPaul Traina if (redo) 351ef5d438eSPaul Traina goto loop; 352ef5d438eSPaul Traina return (RET_SUCCESS); 353ef5d438eSPaul Traina } 354ef5d438eSPaul Traina 355ef5d438eSPaul Traina /* 356ef5d438eSPaul Traina * __bt_pdelete -- 357ef5d438eSPaul Traina * Delete a single page from the tree. 35858f0484fSRodney W. Grimes * 35958f0484fSRodney W. Grimes * Parameters: 36058f0484fSRodney W. Grimes * t: tree 361ef5d438eSPaul Traina * h: leaf page 362ef5d438eSPaul Traina * 363ef5d438eSPaul Traina * Returns: 364ef5d438eSPaul Traina * RET_SUCCESS, RET_ERROR. 365ef5d438eSPaul Traina * 366ef5d438eSPaul Traina * Side-effects: 367ef5d438eSPaul Traina * mpool_put's the page 368ef5d438eSPaul Traina */ 369ef5d438eSPaul Traina static int 3700ac22237SXin LI __bt_pdelete(BTREE *t, PAGE *h) 371ef5d438eSPaul Traina { 372ef5d438eSPaul Traina BINTERNAL *bi; 373ef5d438eSPaul Traina PAGE *pg; 374ef5d438eSPaul Traina EPGNO *parent; 3754c66e4b6SXin LI indx_t cnt, idx, *ip, offset; 376ef5d438eSPaul Traina u_int32_t nksize; 377ef5d438eSPaul Traina char *from; 378ef5d438eSPaul Traina 379ef5d438eSPaul Traina /* 380ef5d438eSPaul Traina * Walk the parent page stack -- a LIFO stack of the pages that were 381ef5d438eSPaul Traina * traversed when we searched for the page where the delete occurred. 382ef5d438eSPaul Traina * Each stack entry is a page number and a page index offset. The 383ef5d438eSPaul Traina * offset is for the page traversed on the search. We've just deleted 384ef5d438eSPaul Traina * a page, so we have to delete the key from the parent page. 385ef5d438eSPaul Traina * 386ef5d438eSPaul Traina * If the delete from the parent page makes it empty, this process may 387ef5d438eSPaul Traina * continue all the way up the tree. We stop if we reach the root page 388ef5d438eSPaul Traina * (which is never deleted, it's just not worth the effort) or if the 389ef5d438eSPaul Traina * delete does not empty the page. 390ef5d438eSPaul Traina */ 391ef5d438eSPaul Traina while ((parent = BT_POP(t)) != NULL) { 392ef5d438eSPaul Traina /* Get the parent page. */ 393ef5d438eSPaul Traina if ((pg = mpool_get(t->bt_mp, parent->pgno, 0)) == NULL) 394ef5d438eSPaul Traina return (RET_ERROR); 395ef5d438eSPaul Traina 3964c66e4b6SXin LI idx = parent->index; 3974c66e4b6SXin LI bi = GETBINTERNAL(pg, idx); 398ef5d438eSPaul Traina 399ef5d438eSPaul Traina /* Free any overflow pages. */ 400ef5d438eSPaul Traina if (bi->flags & P_BIGKEY && 401ef5d438eSPaul Traina __ovfl_delete(t, bi->bytes) == RET_ERROR) { 402ef5d438eSPaul Traina mpool_put(t->bt_mp, pg, 0); 403ef5d438eSPaul Traina return (RET_ERROR); 404ef5d438eSPaul Traina } 405ef5d438eSPaul Traina 406ef5d438eSPaul Traina /* 407ef5d438eSPaul Traina * Free the parent if it has only the one key and it's not the 408ef5d438eSPaul Traina * root page. If it's the rootpage, turn it back into an empty 409ef5d438eSPaul Traina * leaf page. 410ef5d438eSPaul Traina */ 4113df1d934SXin LI if (NEXTINDEX(pg) == 1) { 412ef5d438eSPaul Traina if (pg->pgno == P_ROOT) { 413ef5d438eSPaul Traina pg->lower = BTDATAOFF; 414ef5d438eSPaul Traina pg->upper = t->bt_psize; 415ef5d438eSPaul Traina pg->flags = P_BLEAF; 416ef5d438eSPaul Traina } else { 417ef5d438eSPaul Traina if (__bt_relink(t, pg) || __bt_free(t, pg)) 418ef5d438eSPaul Traina return (RET_ERROR); 419ef5d438eSPaul Traina continue; 420ef5d438eSPaul Traina } 4213df1d934SXin LI } else { 422ef5d438eSPaul Traina /* Pack remaining key items at the end of the page. */ 423ef5d438eSPaul Traina nksize = NBINTERNAL(bi->ksize); 424ef5d438eSPaul Traina from = (char *)pg + pg->upper; 425ef5d438eSPaul Traina memmove(from + nksize, from, (char *)bi - from); 426ef5d438eSPaul Traina pg->upper += nksize; 427ef5d438eSPaul Traina 428ef5d438eSPaul Traina /* Adjust indices' offsets, shift the indices down. */ 4294c66e4b6SXin LI offset = pg->linp[idx]; 4304c66e4b6SXin LI for (cnt = idx, ip = &pg->linp[0]; cnt--; ++ip) 431ef5d438eSPaul Traina if (ip[0] < offset) 432ef5d438eSPaul Traina ip[0] += nksize; 4334c66e4b6SXin LI for (cnt = NEXTINDEX(pg) - idx; --cnt; ++ip) 434ef5d438eSPaul Traina ip[0] = ip[1] < offset ? ip[1] + nksize : ip[1]; 435ef5d438eSPaul Traina pg->lower -= sizeof(indx_t); 436ef5d438eSPaul Traina } 437ef5d438eSPaul Traina 438ef5d438eSPaul Traina mpool_put(t->bt_mp, pg, MPOOL_DIRTY); 439ef5d438eSPaul Traina break; 440ef5d438eSPaul Traina } 441ef5d438eSPaul Traina 442ef5d438eSPaul Traina /* Free the leaf page, as long as it wasn't the root. */ 443ef5d438eSPaul Traina if (h->pgno == P_ROOT) { 444ef5d438eSPaul Traina mpool_put(t->bt_mp, h, MPOOL_DIRTY); 445ef5d438eSPaul Traina return (RET_SUCCESS); 446ef5d438eSPaul Traina } 447ef5d438eSPaul Traina return (__bt_relink(t, h) || __bt_free(t, h)); 448ef5d438eSPaul Traina } 449ef5d438eSPaul Traina 450ef5d438eSPaul Traina /* 451ef5d438eSPaul Traina * __bt_dleaf -- 452ef5d438eSPaul Traina * Delete a single record from a leaf page. 453ef5d438eSPaul Traina * 454ef5d438eSPaul Traina * Parameters: 455ef5d438eSPaul Traina * t: tree 456ef5d438eSPaul Traina * key: referenced key 457ef5d438eSPaul Traina * h: page 4584c66e4b6SXin LI * idx: index on page to delete 45958f0484fSRodney W. Grimes * 46058f0484fSRodney W. Grimes * Returns: 46158f0484fSRodney W. Grimes * RET_SUCCESS, RET_ERROR. 46258f0484fSRodney W. Grimes */ 46358f0484fSRodney W. Grimes int 4644c66e4b6SXin LI __bt_dleaf(BTREE *t, const DBT *key, PAGE *h, u_int idx) 46558f0484fSRodney W. Grimes { 466ef5d438eSPaul Traina BLEAF *bl; 467ef5d438eSPaul Traina indx_t cnt, *ip, offset; 468ef5d438eSPaul Traina u_int32_t nbytes; 46958f0484fSRodney W. Grimes void *to; 470ef5d438eSPaul Traina char *from; 47158f0484fSRodney W. Grimes 472ef5d438eSPaul Traina /* If this record is referenced by the cursor, delete the cursor. */ 473ef5d438eSPaul Traina if (F_ISSET(&t->bt_cursor, CURS_INIT) && 474ef5d438eSPaul Traina !F_ISSET(&t->bt_cursor, CURS_ACQUIRE) && 4754c66e4b6SXin LI t->bt_cursor.pg.pgno == h->pgno && t->bt_cursor.pg.index == idx && 4764c66e4b6SXin LI __bt_curdel(t, key, h, idx)) 477ef5d438eSPaul Traina return (RET_ERROR); 478ef5d438eSPaul Traina 479ef5d438eSPaul Traina /* If the entry uses overflow pages, make them available for reuse. */ 4804c66e4b6SXin LI to = bl = GETBLEAF(h, idx); 48158f0484fSRodney W. Grimes if (bl->flags & P_BIGKEY && __ovfl_delete(t, bl->bytes) == RET_ERROR) 48258f0484fSRodney W. Grimes return (RET_ERROR); 48358f0484fSRodney W. Grimes if (bl->flags & P_BIGDATA && 48458f0484fSRodney W. Grimes __ovfl_delete(t, bl->bytes + bl->ksize) == RET_ERROR) 48558f0484fSRodney W. Grimes return (RET_ERROR); 48658f0484fSRodney W. Grimes 487ef5d438eSPaul Traina /* Pack the remaining key/data items at the end of the page. */ 488ef5d438eSPaul Traina nbytes = NBLEAF(bl); 48958f0484fSRodney W. Grimes from = (char *)h + h->upper; 49058f0484fSRodney W. Grimes memmove(from + nbytes, from, (char *)to - from); 49158f0484fSRodney W. Grimes h->upper += nbytes; 49258f0484fSRodney W. Grimes 493ef5d438eSPaul Traina /* Adjust the indices' offsets, shift the indices down. */ 4944c66e4b6SXin LI offset = h->linp[idx]; 4954c66e4b6SXin LI for (cnt = idx, ip = &h->linp[0]; cnt--; ++ip) 49658f0484fSRodney W. Grimes if (ip[0] < offset) 49758f0484fSRodney W. Grimes ip[0] += nbytes; 4984c66e4b6SXin LI for (cnt = NEXTINDEX(h) - idx; --cnt; ++ip) 49958f0484fSRodney W. Grimes ip[0] = ip[1] < offset ? ip[1] + nbytes : ip[1]; 50058f0484fSRodney W. Grimes h->lower -= sizeof(indx_t); 501ef5d438eSPaul Traina 502ef5d438eSPaul Traina /* If the cursor is on this page, adjust it as necessary. */ 503ef5d438eSPaul Traina if (F_ISSET(&t->bt_cursor, CURS_INIT) && 504ef5d438eSPaul Traina !F_ISSET(&t->bt_cursor, CURS_ACQUIRE) && 5054c66e4b6SXin LI t->bt_cursor.pg.pgno == h->pgno && t->bt_cursor.pg.index > idx) 506ef5d438eSPaul Traina --t->bt_cursor.pg.index; 507ef5d438eSPaul Traina 50858f0484fSRodney W. Grimes return (RET_SUCCESS); 50958f0484fSRodney W. Grimes } 510ef5d438eSPaul Traina 511ef5d438eSPaul Traina /* 512ef5d438eSPaul Traina * __bt_curdel -- 513ef5d438eSPaul Traina * Delete the cursor. 514ef5d438eSPaul Traina * 515ef5d438eSPaul Traina * Parameters: 516ef5d438eSPaul Traina * t: tree 517ef5d438eSPaul Traina * key: referenced key (or NULL) 518ef5d438eSPaul Traina * h: page 5194c66e4b6SXin LI * idx: index on page to delete 520ef5d438eSPaul Traina * 521ef5d438eSPaul Traina * Returns: 522ef5d438eSPaul Traina * RET_SUCCESS, RET_ERROR. 523ef5d438eSPaul Traina */ 524ef5d438eSPaul Traina static int 5254c66e4b6SXin LI __bt_curdel(BTREE *t, const DBT *key, PAGE *h, u_int idx) 526ef5d438eSPaul Traina { 527ef5d438eSPaul Traina CURSOR *c; 528ef5d438eSPaul Traina EPG e; 529ef5d438eSPaul Traina PAGE *pg; 530ef5d438eSPaul Traina int curcopy, status; 531ef5d438eSPaul Traina 532ef5d438eSPaul Traina /* 533ef5d438eSPaul Traina * If there are duplicates, move forward or backward to one. 534ef5d438eSPaul Traina * Otherwise, copy the key into the cursor area. 535ef5d438eSPaul Traina */ 536ef5d438eSPaul Traina c = &t->bt_cursor; 537ef5d438eSPaul Traina F_CLR(c, CURS_AFTER | CURS_BEFORE | CURS_ACQUIRE); 538ef5d438eSPaul Traina 539ef5d438eSPaul Traina curcopy = 0; 540ef5d438eSPaul Traina if (!F_ISSET(t, B_NODUPS)) { 541ef5d438eSPaul Traina /* 542ef5d438eSPaul Traina * We're going to have to do comparisons. If we weren't 543ef5d438eSPaul Traina * provided a copy of the key, i.e. the user is deleting 544ef5d438eSPaul Traina * the current cursor position, get one. 545ef5d438eSPaul Traina */ 546ef5d438eSPaul Traina if (key == NULL) { 547ef5d438eSPaul Traina e.page = h; 5484c66e4b6SXin LI e.index = idx; 549ef5d438eSPaul Traina if ((status = __bt_ret(t, &e, 550ef5d438eSPaul Traina &c->key, &c->key, NULL, NULL, 1)) != RET_SUCCESS) 551ef5d438eSPaul Traina return (status); 552ef5d438eSPaul Traina curcopy = 1; 553ef5d438eSPaul Traina key = &c->key; 554ef5d438eSPaul Traina } 555ef5d438eSPaul Traina /* Check previous key, if not at the beginning of the page. */ 5564c66e4b6SXin LI if (idx > 0) { 557ef5d438eSPaul Traina e.page = h; 5584c66e4b6SXin LI e.index = idx - 1; 559ef5d438eSPaul Traina if (__bt_cmp(t, key, &e) == 0) { 560ef5d438eSPaul Traina F_SET(c, CURS_BEFORE); 561ef5d438eSPaul Traina goto dup2; 562ef5d438eSPaul Traina } 563ef5d438eSPaul Traina } 564ef5d438eSPaul Traina /* Check next key, if not at the end of the page. */ 5654c66e4b6SXin LI if (idx < NEXTINDEX(h) - 1) { 566ef5d438eSPaul Traina e.page = h; 5674c66e4b6SXin LI e.index = idx + 1; 568ef5d438eSPaul Traina if (__bt_cmp(t, key, &e) == 0) { 569ef5d438eSPaul Traina F_SET(c, CURS_AFTER); 570ef5d438eSPaul Traina goto dup2; 571ef5d438eSPaul Traina } 572ef5d438eSPaul Traina } 573ef5d438eSPaul Traina /* Check previous key if at the beginning of the page. */ 5744c66e4b6SXin LI if (idx == 0 && h->prevpg != P_INVALID) { 575ef5d438eSPaul Traina if ((pg = mpool_get(t->bt_mp, h->prevpg, 0)) == NULL) 576ef5d438eSPaul Traina return (RET_ERROR); 577ef5d438eSPaul Traina e.page = pg; 578ef5d438eSPaul Traina e.index = NEXTINDEX(pg) - 1; 579ef5d438eSPaul Traina if (__bt_cmp(t, key, &e) == 0) { 580ef5d438eSPaul Traina F_SET(c, CURS_BEFORE); 581ef5d438eSPaul Traina goto dup1; 582ef5d438eSPaul Traina } 583ef5d438eSPaul Traina mpool_put(t->bt_mp, pg, 0); 584ef5d438eSPaul Traina } 585ef5d438eSPaul Traina /* Check next key if at the end of the page. */ 5864c66e4b6SXin LI if (idx == NEXTINDEX(h) - 1 && h->nextpg != P_INVALID) { 587ef5d438eSPaul Traina if ((pg = mpool_get(t->bt_mp, h->nextpg, 0)) == NULL) 588ef5d438eSPaul Traina return (RET_ERROR); 589ef5d438eSPaul Traina e.page = pg; 590ef5d438eSPaul Traina e.index = 0; 591ef5d438eSPaul Traina if (__bt_cmp(t, key, &e) == 0) { 592ef5d438eSPaul Traina F_SET(c, CURS_AFTER); 593ef5d438eSPaul Traina dup1: mpool_put(t->bt_mp, pg, 0); 594ef5d438eSPaul Traina dup2: c->pg.pgno = e.page->pgno; 595ef5d438eSPaul Traina c->pg.index = e.index; 596ef5d438eSPaul Traina return (RET_SUCCESS); 597ef5d438eSPaul Traina } 598ef5d438eSPaul Traina mpool_put(t->bt_mp, pg, 0); 599ef5d438eSPaul Traina } 600ef5d438eSPaul Traina } 601ef5d438eSPaul Traina e.page = h; 6024c66e4b6SXin LI e.index = idx; 603ef5d438eSPaul Traina if (curcopy || (status = 604ef5d438eSPaul Traina __bt_ret(t, &e, &c->key, &c->key, NULL, NULL, 1)) == RET_SUCCESS) { 605ef5d438eSPaul Traina F_SET(c, CURS_ACQUIRE); 606ef5d438eSPaul Traina return (RET_SUCCESS); 607ef5d438eSPaul Traina } 608ef5d438eSPaul Traina return (status); 609ef5d438eSPaul Traina } 610ef5d438eSPaul Traina 611ef5d438eSPaul Traina /* 612ef5d438eSPaul Traina * __bt_relink -- 613ef5d438eSPaul Traina * Link around a deleted page. 614ef5d438eSPaul Traina * 615ef5d438eSPaul Traina * Parameters: 616ef5d438eSPaul Traina * t: tree 617ef5d438eSPaul Traina * h: page to be deleted 618ef5d438eSPaul Traina */ 619ef5d438eSPaul Traina static int 6200ac22237SXin LI __bt_relink(BTREE *t, PAGE *h) 621ef5d438eSPaul Traina { 622ef5d438eSPaul Traina PAGE *pg; 623ef5d438eSPaul Traina 624ef5d438eSPaul Traina if (h->nextpg != P_INVALID) { 625ef5d438eSPaul Traina if ((pg = mpool_get(t->bt_mp, h->nextpg, 0)) == NULL) 626ef5d438eSPaul Traina return (RET_ERROR); 627ef5d438eSPaul Traina pg->prevpg = h->prevpg; 628ef5d438eSPaul Traina mpool_put(t->bt_mp, pg, MPOOL_DIRTY); 629ef5d438eSPaul Traina } 630ef5d438eSPaul Traina if (h->prevpg != P_INVALID) { 631ef5d438eSPaul Traina if ((pg = mpool_get(t->bt_mp, h->prevpg, 0)) == NULL) 632ef5d438eSPaul Traina return (RET_ERROR); 633ef5d438eSPaul Traina pg->nextpg = h->nextpg; 634ef5d438eSPaul Traina mpool_put(t->bt_mp, pg, MPOOL_DIRTY); 635ef5d438eSPaul Traina } 636ef5d438eSPaul Traina return (0); 637ef5d438eSPaul Traina } 638