158f0484fSRodney W. Grimes /*- 2ef5d438eSPaul Traina * Copyright (c) 1990, 1993, 1994 358f0484fSRodney W. Grimes * The Regents of the University of California. All rights reserved. 458f0484fSRodney W. Grimes * 558f0484fSRodney W. Grimes * This code is derived from software contributed to Berkeley by 658f0484fSRodney W. Grimes * Mike Olson. 758f0484fSRodney W. Grimes * 858f0484fSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 958f0484fSRodney W. Grimes * modification, are permitted provided that the following conditions 1058f0484fSRodney W. Grimes * are met: 1158f0484fSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 1258f0484fSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 1358f0484fSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 1458f0484fSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 1558f0484fSRodney W. Grimes * documentation and/or other materials provided with the distribution. 1658f0484fSRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 1758f0484fSRodney W. Grimes * must display the following acknowledgement: 1858f0484fSRodney W. Grimes * This product includes software developed by the University of 1958f0484fSRodney W. Grimes * California, Berkeley and its contributors. 2058f0484fSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 2158f0484fSRodney W. Grimes * may be used to endorse or promote products derived from this software 2258f0484fSRodney W. Grimes * without specific prior written permission. 2358f0484fSRodney W. Grimes * 2458f0484fSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2558f0484fSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2658f0484fSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2758f0484fSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2858f0484fSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2958f0484fSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 3058f0484fSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 3158f0484fSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 3258f0484fSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3358f0484fSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3458f0484fSRodney W. Grimes * SUCH DAMAGE. 3558f0484fSRodney W. Grimes */ 3658f0484fSRodney W. Grimes 3758f0484fSRodney W. Grimes #if defined(LIBC_SCCS) && !defined(lint) 38ef5d438eSPaul Traina static char sccsid[] = "@(#)bt_delete.c 8.13 (Berkeley) 7/28/94"; 3958f0484fSRodney W. Grimes #endif /* LIBC_SCCS and not lint */ 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 50ef5d438eSPaul Traina static int __bt_bdelete __P((BTREE *, const DBT *)); 51ef5d438eSPaul Traina static int __bt_curdel __P((BTREE *, const DBT *, PAGE *, u_int)); 52ef5d438eSPaul Traina static int __bt_pdelete __P((BTREE *, PAGE *)); 53ef5d438eSPaul Traina static int __bt_relink __P((BTREE *, PAGE *)); 54ef5d438eSPaul Traina static int __bt_stkacq __P((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 6358f0484fSRodney W. Grimes __bt_delete(dbp, key, flags) 6458f0484fSRodney W. Grimes const DB *dbp; 6558f0484fSRodney W. Grimes const DBT *key; 6658f0484fSRodney W. Grimes u_int flags; 6758f0484fSRodney W. Grimes { 6858f0484fSRodney W. Grimes BTREE *t; 69ef5d438eSPaul Traina CURSOR *c; 70ef5d438eSPaul Traina PAGE *h; 7158f0484fSRodney W. Grimes int status; 7258f0484fSRodney W. Grimes 7358f0484fSRodney W. Grimes t = dbp->internal; 7458f0484fSRodney W. Grimes 7558f0484fSRodney W. Grimes /* Toss any page pinned across calls. */ 7658f0484fSRodney W. Grimes if (t->bt_pinned != NULL) { 7758f0484fSRodney W. Grimes mpool_put(t->bt_mp, t->bt_pinned, 0); 7858f0484fSRodney W. Grimes t->bt_pinned = NULL; 7958f0484fSRodney W. Grimes } 8058f0484fSRodney W. Grimes 81ef5d438eSPaul Traina /* Check for change to a read-only tree. */ 82ef5d438eSPaul Traina if (F_ISSET(t, B_RDONLY)) { 8358f0484fSRodney W. Grimes errno = EPERM; 8458f0484fSRodney W. Grimes return (RET_ERROR); 8558f0484fSRodney W. Grimes } 8658f0484fSRodney W. Grimes 8758f0484fSRodney W. Grimes switch (flags) { 8858f0484fSRodney W. Grimes case 0: 89ef5d438eSPaul Traina status = __bt_bdelete(t, key); 9058f0484fSRodney W. Grimes break; 9158f0484fSRodney W. Grimes case R_CURSOR: 9258f0484fSRodney W. Grimes /* 93ef5d438eSPaul Traina * If flags is R_CURSOR, delete the cursor. Must already 94ef5d438eSPaul Traina * have started a scan and not have already deleted it. 9558f0484fSRodney W. Grimes */ 96ef5d438eSPaul Traina c = &t->bt_cursor; 97ef5d438eSPaul Traina if (F_ISSET(c, CURS_INIT)) { 98ef5d438eSPaul Traina if (F_ISSET(c, CURS_ACQUIRE | CURS_AFTER | CURS_BEFORE)) 99ef5d438eSPaul Traina return (RET_SPECIAL); 100ef5d438eSPaul Traina if ((h = mpool_get(t->bt_mp, c->pg.pgno, 0)) == NULL) 101ef5d438eSPaul Traina return (RET_ERROR); 102ef5d438eSPaul Traina 103ef5d438eSPaul Traina /* 104ef5d438eSPaul Traina * If the page is about to be emptied, we'll need to 105ef5d438eSPaul Traina * delete it, which means we have to acquire a stack. 106ef5d438eSPaul Traina */ 107ef5d438eSPaul Traina if (NEXTINDEX(h) == 1) 108ef5d438eSPaul Traina if (__bt_stkacq(t, &h, &t->bt_cursor)) 109ef5d438eSPaul Traina return (RET_ERROR); 110ef5d438eSPaul Traina 111ef5d438eSPaul Traina status = __bt_dleaf(t, NULL, h, c->pg.index); 112ef5d438eSPaul Traina 113ef5d438eSPaul Traina if (NEXTINDEX(h) == 0 && status == RET_SUCCESS) { 114ef5d438eSPaul Traina if (__bt_pdelete(t, h)) 115ef5d438eSPaul Traina return (RET_ERROR); 116ef5d438eSPaul Traina } else 117ef5d438eSPaul Traina mpool_put(t->bt_mp, 118ef5d438eSPaul Traina h, status == RET_SUCCESS ? MPOOL_DIRTY : 0); 11958f0484fSRodney W. Grimes break; 120ef5d438eSPaul Traina } 121ef5d438eSPaul Traina /* FALLTHROUGH */ 12258f0484fSRodney W. Grimes default: 123ef5d438eSPaul Traina errno = EINVAL; 12458f0484fSRodney W. Grimes return (RET_ERROR); 12558f0484fSRodney W. Grimes } 12658f0484fSRodney W. Grimes if (status == RET_SUCCESS) 127ef5d438eSPaul Traina F_SET(t, B_MODIFIED); 12858f0484fSRodney W. Grimes return (status); 12958f0484fSRodney W. Grimes } 13058f0484fSRodney W. Grimes 13158f0484fSRodney W. Grimes /* 132ef5d438eSPaul Traina * __bt_stkacq -- 133ef5d438eSPaul Traina * Acquire a stack so we can delete a cursor entry. 13458f0484fSRodney W. Grimes * 13558f0484fSRodney W. Grimes * Parameters: 136ef5d438eSPaul Traina * t: tree 137ef5d438eSPaul Traina * hp: pointer to current, pinned PAGE pointer 138ef5d438eSPaul Traina * c: pointer to the cursor 139ef5d438eSPaul Traina * 140ef5d438eSPaul Traina * Returns: 141ef5d438eSPaul Traina * 0 on success, 1 on failure 142ef5d438eSPaul Traina */ 143ef5d438eSPaul Traina static int 144ef5d438eSPaul Traina __bt_stkacq(t, hp, c) 145ef5d438eSPaul Traina BTREE *t; 146ef5d438eSPaul Traina PAGE **hp; 147ef5d438eSPaul Traina CURSOR *c; 148ef5d438eSPaul Traina { 149ef5d438eSPaul Traina BINTERNAL *bi; 150ef5d438eSPaul Traina EPG *e; 151ef5d438eSPaul Traina EPGNO *parent; 152ef5d438eSPaul Traina PAGE *h; 153ef5d438eSPaul Traina indx_t index; 154ef5d438eSPaul Traina pgno_t pgno; 155ef5d438eSPaul Traina recno_t nextpg, prevpg; 156ef5d438eSPaul Traina int exact, level; 157ef5d438eSPaul Traina 158ef5d438eSPaul Traina /* 159ef5d438eSPaul Traina * Find the first occurrence of the key in the tree. Toss the 160ef5d438eSPaul Traina * currently locked page so we don't hit an already-locked page. 161ef5d438eSPaul Traina */ 162ef5d438eSPaul Traina h = *hp; 163ef5d438eSPaul Traina mpool_put(t->bt_mp, h, 0); 164ef5d438eSPaul Traina if ((e = __bt_search(t, &c->key, &exact)) == NULL) 165ef5d438eSPaul Traina return (1); 166ef5d438eSPaul Traina h = e->page; 167ef5d438eSPaul Traina 168ef5d438eSPaul Traina /* See if we got it in one shot. */ 169ef5d438eSPaul Traina if (h->pgno == c->pg.pgno) 170ef5d438eSPaul Traina goto ret; 171ef5d438eSPaul Traina 172ef5d438eSPaul Traina /* 173ef5d438eSPaul Traina * Move right, looking for the page. At each move we have to move 174ef5d438eSPaul Traina * up the stack until we don't have to move to the next page. If 175ef5d438eSPaul Traina * we have to change pages at an internal level, we have to fix the 176ef5d438eSPaul Traina * stack back up. 177ef5d438eSPaul Traina */ 178ef5d438eSPaul Traina while (h->pgno != c->pg.pgno) { 179ef5d438eSPaul Traina if ((nextpg = h->nextpg) == P_INVALID) 180ef5d438eSPaul Traina break; 181ef5d438eSPaul Traina mpool_put(t->bt_mp, h, 0); 182ef5d438eSPaul Traina 183ef5d438eSPaul Traina /* Move up the stack. */ 184ef5d438eSPaul Traina for (level = 0; (parent = BT_POP(t)) != NULL; ++level) { 185ef5d438eSPaul Traina /* Get the parent page. */ 186ef5d438eSPaul Traina if ((h = mpool_get(t->bt_mp, parent->pgno, 0)) == NULL) 187ef5d438eSPaul Traina return (1); 188ef5d438eSPaul Traina 189ef5d438eSPaul Traina /* Move to the next index. */ 190ef5d438eSPaul Traina if (parent->index != NEXTINDEX(h) - 1) { 191ef5d438eSPaul Traina index = parent->index + 1; 192ef5d438eSPaul Traina BT_PUSH(t, h->pgno, index); 193ef5d438eSPaul Traina break; 194ef5d438eSPaul Traina } 195ef5d438eSPaul Traina mpool_put(t->bt_mp, h, 0); 196ef5d438eSPaul Traina } 197ef5d438eSPaul Traina 198ef5d438eSPaul Traina /* Restore the stack. */ 199ef5d438eSPaul Traina while (level--) { 200ef5d438eSPaul Traina /* Push the next level down onto the stack. */ 201ef5d438eSPaul Traina bi = GETBINTERNAL(h, index); 202ef5d438eSPaul Traina pgno = bi->pgno; 203ef5d438eSPaul Traina BT_PUSH(t, pgno, 0); 204ef5d438eSPaul Traina 205ef5d438eSPaul Traina /* Lose the currently pinned page. */ 206ef5d438eSPaul Traina mpool_put(t->bt_mp, h, 0); 207ef5d438eSPaul Traina 208ef5d438eSPaul Traina /* Get the next level down. */ 209ef5d438eSPaul Traina if ((h = mpool_get(t->bt_mp, pgno, 0)) == NULL) 210ef5d438eSPaul Traina return (1); 211ef5d438eSPaul Traina index = 0; 212ef5d438eSPaul Traina } 213ef5d438eSPaul Traina mpool_put(t->bt_mp, h, 0); 214ef5d438eSPaul Traina if ((h = mpool_get(t->bt_mp, nextpg, 0)) == NULL) 215ef5d438eSPaul Traina return (1); 216ef5d438eSPaul Traina } 217ef5d438eSPaul Traina 218ef5d438eSPaul Traina if (h->pgno == c->pg.pgno) 219ef5d438eSPaul Traina goto ret; 220ef5d438eSPaul Traina 221ef5d438eSPaul Traina /* Reacquire the original stack. */ 222ef5d438eSPaul Traina mpool_put(t->bt_mp, h, 0); 223ef5d438eSPaul Traina if ((e = __bt_search(t, &c->key, &exact)) == NULL) 224ef5d438eSPaul Traina return (1); 225ef5d438eSPaul Traina h = e->page; 226ef5d438eSPaul Traina 227ef5d438eSPaul Traina /* 228ef5d438eSPaul Traina * Move left, looking for the page. At each move we have to move 229ef5d438eSPaul Traina * up the stack until we don't have to change pages to move to the 230ef5d438eSPaul Traina * next page. If we have to change pages at an internal level, we 231ef5d438eSPaul Traina * have to fix the stack back up. 232ef5d438eSPaul Traina */ 233ef5d438eSPaul Traina while (h->pgno != c->pg.pgno) { 234ef5d438eSPaul Traina if ((prevpg = h->prevpg) == P_INVALID) 235ef5d438eSPaul Traina break; 236ef5d438eSPaul Traina mpool_put(t->bt_mp, h, 0); 237ef5d438eSPaul Traina 238ef5d438eSPaul Traina /* Move up the stack. */ 239ef5d438eSPaul Traina for (level = 0; (parent = BT_POP(t)) != NULL; ++level) { 240ef5d438eSPaul Traina /* Get the parent page. */ 241ef5d438eSPaul Traina if ((h = mpool_get(t->bt_mp, parent->pgno, 0)) == NULL) 242ef5d438eSPaul Traina return (1); 243ef5d438eSPaul Traina 244ef5d438eSPaul Traina /* Move to the next index. */ 245ef5d438eSPaul Traina if (parent->index != 0) { 246ef5d438eSPaul Traina index = parent->index - 1; 247ef5d438eSPaul Traina BT_PUSH(t, h->pgno, index); 248ef5d438eSPaul Traina break; 249ef5d438eSPaul Traina } 250ef5d438eSPaul Traina mpool_put(t->bt_mp, h, 0); 251ef5d438eSPaul Traina } 252ef5d438eSPaul Traina 253ef5d438eSPaul Traina /* Restore the stack. */ 254ef5d438eSPaul Traina while (level--) { 255ef5d438eSPaul Traina /* Push the next level down onto the stack. */ 256ef5d438eSPaul Traina bi = GETBINTERNAL(h, index); 257ef5d438eSPaul Traina pgno = bi->pgno; 258ef5d438eSPaul Traina 259ef5d438eSPaul Traina /* Lose the currently pinned page. */ 260ef5d438eSPaul Traina mpool_put(t->bt_mp, h, 0); 261ef5d438eSPaul Traina 262ef5d438eSPaul Traina /* Get the next level down. */ 263ef5d438eSPaul Traina if ((h = mpool_get(t->bt_mp, pgno, 0)) == NULL) 264ef5d438eSPaul Traina return (1); 265ef5d438eSPaul Traina 266ef5d438eSPaul Traina index = NEXTINDEX(h) - 1; 267ef5d438eSPaul Traina BT_PUSH(t, pgno, index); 268ef5d438eSPaul Traina } 269ef5d438eSPaul Traina mpool_put(t->bt_mp, h, 0); 270ef5d438eSPaul Traina if ((h = mpool_get(t->bt_mp, prevpg, 0)) == NULL) 271ef5d438eSPaul Traina return (1); 272ef5d438eSPaul Traina } 273ef5d438eSPaul Traina 274ef5d438eSPaul Traina 275ef5d438eSPaul Traina ret: mpool_put(t->bt_mp, h, 0); 276ef5d438eSPaul Traina return ((*hp = mpool_get(t->bt_mp, c->pg.pgno, 0)) == NULL); 277ef5d438eSPaul Traina } 278ef5d438eSPaul Traina 279ef5d438eSPaul Traina /* 280ef5d438eSPaul Traina * __bt_bdelete -- 281ef5d438eSPaul Traina * Delete all key/data pairs matching the specified key. 282ef5d438eSPaul Traina * 283ef5d438eSPaul Traina * Parameters: 284ef5d438eSPaul Traina * t: tree 28558f0484fSRodney W. Grimes * key: key to delete 28658f0484fSRodney W. Grimes * 28758f0484fSRodney W. Grimes * Returns: 28858f0484fSRodney W. Grimes * RET_ERROR, RET_SUCCESS and RET_SPECIAL if the key not found. 28958f0484fSRodney W. Grimes */ 29058f0484fSRodney W. Grimes static int 291ef5d438eSPaul Traina __bt_bdelete(t, key) 29258f0484fSRodney W. Grimes BTREE *t; 29358f0484fSRodney W. Grimes const DBT *key; 29458f0484fSRodney W. Grimes { 295ef5d438eSPaul Traina EPG *e; 29658f0484fSRodney W. Grimes PAGE *h; 297ef5d438eSPaul Traina int deleted, exact, redo; 298ef5d438eSPaul Traina 299ef5d438eSPaul Traina deleted = 0; 30058f0484fSRodney W. Grimes 30158f0484fSRodney W. Grimes /* Find any matching record; __bt_search pins the page. */ 302ef5d438eSPaul Traina loop: if ((e = __bt_search(t, key, &exact)) == NULL) 303ef5d438eSPaul Traina return (deleted ? RET_SUCCESS : RET_ERROR); 30458f0484fSRodney W. Grimes if (!exact) { 30558f0484fSRodney W. Grimes mpool_put(t->bt_mp, e->page, 0); 30658f0484fSRodney W. Grimes return (deleted ? RET_SUCCESS : RET_SPECIAL); 30758f0484fSRodney W. Grimes } 30858f0484fSRodney W. Grimes 30958f0484fSRodney W. Grimes /* 310ef5d438eSPaul Traina * Delete forward, then delete backward, from the found key. If 311ef5d438eSPaul Traina * there are duplicates and we reach either side of the page, do 312ef5d438eSPaul Traina * the key search again, so that we get them all. 313ef5d438eSPaul Traina */ 314ef5d438eSPaul Traina redo = 0; 315ef5d438eSPaul Traina h = e->page; 316ef5d438eSPaul Traina do { 317ef5d438eSPaul Traina if (__bt_dleaf(t, key, h, e->index)) { 318ef5d438eSPaul Traina mpool_put(t->bt_mp, h, 0); 319ef5d438eSPaul Traina return (RET_ERROR); 320ef5d438eSPaul Traina } 321ef5d438eSPaul Traina if (F_ISSET(t, B_NODUPS)) { 322ef5d438eSPaul Traina if (NEXTINDEX(h) == 0) { 323ef5d438eSPaul Traina if (__bt_pdelete(t, h)) 324ef5d438eSPaul Traina return (RET_ERROR); 325ef5d438eSPaul Traina } else 326ef5d438eSPaul Traina mpool_put(t->bt_mp, h, MPOOL_DIRTY); 327ef5d438eSPaul Traina return (RET_SUCCESS); 328ef5d438eSPaul Traina } 329ef5d438eSPaul Traina deleted = 1; 330ef5d438eSPaul Traina } while (e->index < NEXTINDEX(h) && __bt_cmp(t, key, e) == 0); 331ef5d438eSPaul Traina 332ef5d438eSPaul Traina /* Check for right-hand edge of the page. */ 333ef5d438eSPaul Traina if (e->index == NEXTINDEX(h)) 334ef5d438eSPaul Traina redo = 1; 335ef5d438eSPaul Traina 336ef5d438eSPaul Traina /* Delete from the key to the beginning of the page. */ 337ef5d438eSPaul Traina while (e->index-- > 0) { 338ef5d438eSPaul Traina if (__bt_cmp(t, key, e) != 0) 339ef5d438eSPaul Traina break; 340ef5d438eSPaul Traina if (__bt_dleaf(t, key, h, e->index) == RET_ERROR) { 341ef5d438eSPaul Traina mpool_put(t->bt_mp, h, 0); 342ef5d438eSPaul Traina return (RET_ERROR); 343ef5d438eSPaul Traina } 344ef5d438eSPaul Traina if (e->index == 0) 345ef5d438eSPaul Traina redo = 1; 346ef5d438eSPaul Traina } 347ef5d438eSPaul Traina 348ef5d438eSPaul Traina /* Check for an empty page. */ 349ef5d438eSPaul Traina if (NEXTINDEX(h) == 0) { 350ef5d438eSPaul Traina if (__bt_pdelete(t, h)) 351ef5d438eSPaul Traina return (RET_ERROR); 352ef5d438eSPaul Traina goto loop; 353ef5d438eSPaul Traina } 354ef5d438eSPaul Traina 355ef5d438eSPaul Traina /* Put the page. */ 356ef5d438eSPaul Traina mpool_put(t->bt_mp, h, MPOOL_DIRTY); 357ef5d438eSPaul Traina 358ef5d438eSPaul Traina if (redo) 359ef5d438eSPaul Traina goto loop; 360ef5d438eSPaul Traina return (RET_SUCCESS); 361ef5d438eSPaul Traina } 362ef5d438eSPaul Traina 363ef5d438eSPaul Traina /* 364ef5d438eSPaul Traina * __bt_pdelete -- 365ef5d438eSPaul Traina * Delete a single page from the tree. 36658f0484fSRodney W. Grimes * 36758f0484fSRodney W. Grimes * Parameters: 36858f0484fSRodney W. Grimes * t: tree 369ef5d438eSPaul Traina * h: leaf page 370ef5d438eSPaul Traina * 371ef5d438eSPaul Traina * Returns: 372ef5d438eSPaul Traina * RET_SUCCESS, RET_ERROR. 373ef5d438eSPaul Traina * 374ef5d438eSPaul Traina * Side-effects: 375ef5d438eSPaul Traina * mpool_put's the page 376ef5d438eSPaul Traina */ 377ef5d438eSPaul Traina static int 378ef5d438eSPaul Traina __bt_pdelete(t, h) 379ef5d438eSPaul Traina BTREE *t; 380ef5d438eSPaul Traina PAGE *h; 381ef5d438eSPaul Traina { 382ef5d438eSPaul Traina BINTERNAL *bi; 383ef5d438eSPaul Traina PAGE *pg; 384ef5d438eSPaul Traina EPGNO *parent; 385ef5d438eSPaul Traina indx_t cnt, index, *ip, offset; 386ef5d438eSPaul Traina u_int32_t nksize; 387ef5d438eSPaul Traina char *from; 388ef5d438eSPaul Traina 389ef5d438eSPaul Traina /* 390ef5d438eSPaul Traina * Walk the parent page stack -- a LIFO stack of the pages that were 391ef5d438eSPaul Traina * traversed when we searched for the page where the delete occurred. 392ef5d438eSPaul Traina * Each stack entry is a page number and a page index offset. The 393ef5d438eSPaul Traina * offset is for the page traversed on the search. We've just deleted 394ef5d438eSPaul Traina * a page, so we have to delete the key from the parent page. 395ef5d438eSPaul Traina * 396ef5d438eSPaul Traina * If the delete from the parent page makes it empty, this process may 397ef5d438eSPaul Traina * continue all the way up the tree. We stop if we reach the root page 398ef5d438eSPaul Traina * (which is never deleted, it's just not worth the effort) or if the 399ef5d438eSPaul Traina * delete does not empty the page. 400ef5d438eSPaul Traina */ 401ef5d438eSPaul Traina while ((parent = BT_POP(t)) != NULL) { 402ef5d438eSPaul Traina /* Get the parent page. */ 403ef5d438eSPaul Traina if ((pg = mpool_get(t->bt_mp, parent->pgno, 0)) == NULL) 404ef5d438eSPaul Traina return (RET_ERROR); 405ef5d438eSPaul Traina 406ef5d438eSPaul Traina index = parent->index; 407ef5d438eSPaul Traina bi = GETBINTERNAL(pg, index); 408ef5d438eSPaul Traina 409ef5d438eSPaul Traina /* Free any overflow pages. */ 410ef5d438eSPaul Traina if (bi->flags & P_BIGKEY && 411ef5d438eSPaul Traina __ovfl_delete(t, bi->bytes) == RET_ERROR) { 412ef5d438eSPaul Traina mpool_put(t->bt_mp, pg, 0); 413ef5d438eSPaul Traina return (RET_ERROR); 414ef5d438eSPaul Traina } 415ef5d438eSPaul Traina 416ef5d438eSPaul Traina /* 417ef5d438eSPaul Traina * Free the parent if it has only the one key and it's not the 418ef5d438eSPaul Traina * root page. If it's the rootpage, turn it back into an empty 419ef5d438eSPaul Traina * leaf page. 420ef5d438eSPaul Traina */ 421ef5d438eSPaul Traina if (NEXTINDEX(pg) == 1) 422ef5d438eSPaul Traina if (pg->pgno == P_ROOT) { 423ef5d438eSPaul Traina pg->lower = BTDATAOFF; 424ef5d438eSPaul Traina pg->upper = t->bt_psize; 425ef5d438eSPaul Traina pg->flags = P_BLEAF; 426ef5d438eSPaul Traina } else { 427ef5d438eSPaul Traina if (__bt_relink(t, pg) || __bt_free(t, pg)) 428ef5d438eSPaul Traina return (RET_ERROR); 429ef5d438eSPaul Traina continue; 430ef5d438eSPaul Traina } 431ef5d438eSPaul Traina else { 432ef5d438eSPaul Traina /* Pack remaining key items at the end of the page. */ 433ef5d438eSPaul Traina nksize = NBINTERNAL(bi->ksize); 434ef5d438eSPaul Traina from = (char *)pg + pg->upper; 435ef5d438eSPaul Traina memmove(from + nksize, from, (char *)bi - from); 436ef5d438eSPaul Traina pg->upper += nksize; 437ef5d438eSPaul Traina 438ef5d438eSPaul Traina /* Adjust indices' offsets, shift the indices down. */ 439ef5d438eSPaul Traina offset = pg->linp[index]; 440ef5d438eSPaul Traina for (cnt = index, ip = &pg->linp[0]; cnt--; ++ip) 441ef5d438eSPaul Traina if (ip[0] < offset) 442ef5d438eSPaul Traina ip[0] += nksize; 443ef5d438eSPaul Traina for (cnt = NEXTINDEX(pg) - index; --cnt; ++ip) 444ef5d438eSPaul Traina ip[0] = ip[1] < offset ? ip[1] + nksize : ip[1]; 445ef5d438eSPaul Traina pg->lower -= sizeof(indx_t); 446ef5d438eSPaul Traina } 447ef5d438eSPaul Traina 448ef5d438eSPaul Traina mpool_put(t->bt_mp, pg, MPOOL_DIRTY); 449ef5d438eSPaul Traina break; 450ef5d438eSPaul Traina } 451ef5d438eSPaul Traina 452ef5d438eSPaul Traina /* Free the leaf page, as long as it wasn't the root. */ 453ef5d438eSPaul Traina if (h->pgno == P_ROOT) { 454ef5d438eSPaul Traina mpool_put(t->bt_mp, h, MPOOL_DIRTY); 455ef5d438eSPaul Traina return (RET_SUCCESS); 456ef5d438eSPaul Traina } 457ef5d438eSPaul Traina return (__bt_relink(t, h) || __bt_free(t, h)); 458ef5d438eSPaul Traina } 459ef5d438eSPaul Traina 460ef5d438eSPaul Traina /* 461ef5d438eSPaul Traina * __bt_dleaf -- 462ef5d438eSPaul Traina * Delete a single record from a leaf page. 463ef5d438eSPaul Traina * 464ef5d438eSPaul Traina * Parameters: 465ef5d438eSPaul Traina * t: tree 466ef5d438eSPaul Traina * key: referenced key 467ef5d438eSPaul Traina * h: page 468ef5d438eSPaul Traina * index: index on page to delete 46958f0484fSRodney W. Grimes * 47058f0484fSRodney W. Grimes * Returns: 47158f0484fSRodney W. Grimes * RET_SUCCESS, RET_ERROR. 47258f0484fSRodney W. Grimes */ 47358f0484fSRodney W. Grimes int 474ef5d438eSPaul Traina __bt_dleaf(t, key, h, index) 47558f0484fSRodney W. Grimes BTREE *t; 476ef5d438eSPaul Traina const DBT *key; 47758f0484fSRodney W. Grimes PAGE *h; 478ef5d438eSPaul Traina u_int index; 47958f0484fSRodney W. Grimes { 480ef5d438eSPaul Traina BLEAF *bl; 481ef5d438eSPaul Traina indx_t cnt, *ip, offset; 482ef5d438eSPaul Traina u_int32_t nbytes; 48358f0484fSRodney W. Grimes void *to; 484ef5d438eSPaul Traina char *from; 48558f0484fSRodney W. Grimes 486ef5d438eSPaul Traina /* If this record is referenced by the cursor, delete the cursor. */ 487ef5d438eSPaul Traina if (F_ISSET(&t->bt_cursor, CURS_INIT) && 488ef5d438eSPaul Traina !F_ISSET(&t->bt_cursor, CURS_ACQUIRE) && 489ef5d438eSPaul Traina t->bt_cursor.pg.pgno == h->pgno && t->bt_cursor.pg.index == index && 490ef5d438eSPaul Traina __bt_curdel(t, key, h, index)) 491ef5d438eSPaul Traina return (RET_ERROR); 492ef5d438eSPaul Traina 493ef5d438eSPaul Traina /* If the entry uses overflow pages, make them available for reuse. */ 49458f0484fSRodney W. Grimes to = bl = GETBLEAF(h, index); 49558f0484fSRodney W. Grimes if (bl->flags & P_BIGKEY && __ovfl_delete(t, bl->bytes) == RET_ERROR) 49658f0484fSRodney W. Grimes return (RET_ERROR); 49758f0484fSRodney W. Grimes if (bl->flags & P_BIGDATA && 49858f0484fSRodney W. Grimes __ovfl_delete(t, bl->bytes + bl->ksize) == RET_ERROR) 49958f0484fSRodney W. Grimes return (RET_ERROR); 50058f0484fSRodney W. Grimes 501ef5d438eSPaul Traina /* Pack the remaining key/data items at the end of the page. */ 502ef5d438eSPaul Traina nbytes = NBLEAF(bl); 50358f0484fSRodney W. Grimes from = (char *)h + h->upper; 50458f0484fSRodney W. Grimes memmove(from + nbytes, from, (char *)to - from); 50558f0484fSRodney W. Grimes h->upper += nbytes; 50658f0484fSRodney W. Grimes 507ef5d438eSPaul Traina /* Adjust the indices' offsets, shift the indices down. */ 50858f0484fSRodney W. Grimes offset = h->linp[index]; 50958f0484fSRodney W. Grimes for (cnt = index, ip = &h->linp[0]; cnt--; ++ip) 51058f0484fSRodney W. Grimes if (ip[0] < offset) 51158f0484fSRodney W. Grimes ip[0] += nbytes; 51258f0484fSRodney W. Grimes for (cnt = NEXTINDEX(h) - index; --cnt; ++ip) 51358f0484fSRodney W. Grimes ip[0] = ip[1] < offset ? ip[1] + nbytes : ip[1]; 51458f0484fSRodney W. Grimes h->lower -= sizeof(indx_t); 515ef5d438eSPaul Traina 516ef5d438eSPaul Traina /* If the cursor is on this page, adjust it as necessary. */ 517ef5d438eSPaul Traina if (F_ISSET(&t->bt_cursor, CURS_INIT) && 518ef5d438eSPaul Traina !F_ISSET(&t->bt_cursor, CURS_ACQUIRE) && 519ef5d438eSPaul Traina t->bt_cursor.pg.pgno == h->pgno && t->bt_cursor.pg.index > index) 520ef5d438eSPaul Traina --t->bt_cursor.pg.index; 521ef5d438eSPaul Traina 52258f0484fSRodney W. Grimes return (RET_SUCCESS); 52358f0484fSRodney W. Grimes } 524ef5d438eSPaul Traina 525ef5d438eSPaul Traina /* 526ef5d438eSPaul Traina * __bt_curdel -- 527ef5d438eSPaul Traina * Delete the cursor. 528ef5d438eSPaul Traina * 529ef5d438eSPaul Traina * Parameters: 530ef5d438eSPaul Traina * t: tree 531ef5d438eSPaul Traina * key: referenced key (or NULL) 532ef5d438eSPaul Traina * h: page 533ef5d438eSPaul Traina * index: index on page to delete 534ef5d438eSPaul Traina * 535ef5d438eSPaul Traina * Returns: 536ef5d438eSPaul Traina * RET_SUCCESS, RET_ERROR. 537ef5d438eSPaul Traina */ 538ef5d438eSPaul Traina static int 539ef5d438eSPaul Traina __bt_curdel(t, key, h, index) 540ef5d438eSPaul Traina BTREE *t; 541ef5d438eSPaul Traina const DBT *key; 542ef5d438eSPaul Traina PAGE *h; 543ef5d438eSPaul Traina u_int index; 544ef5d438eSPaul Traina { 545ef5d438eSPaul Traina CURSOR *c; 546ef5d438eSPaul Traina EPG e; 547ef5d438eSPaul Traina PAGE *pg; 548ef5d438eSPaul Traina int curcopy, status; 549ef5d438eSPaul Traina 550ef5d438eSPaul Traina /* 551ef5d438eSPaul Traina * If there are duplicates, move forward or backward to one. 552ef5d438eSPaul Traina * Otherwise, copy the key into the cursor area. 553ef5d438eSPaul Traina */ 554ef5d438eSPaul Traina c = &t->bt_cursor; 555ef5d438eSPaul Traina F_CLR(c, CURS_AFTER | CURS_BEFORE | CURS_ACQUIRE); 556ef5d438eSPaul Traina 557ef5d438eSPaul Traina curcopy = 0; 558ef5d438eSPaul Traina if (!F_ISSET(t, B_NODUPS)) { 559ef5d438eSPaul Traina /* 560ef5d438eSPaul Traina * We're going to have to do comparisons. If we weren't 561ef5d438eSPaul Traina * provided a copy of the key, i.e. the user is deleting 562ef5d438eSPaul Traina * the current cursor position, get one. 563ef5d438eSPaul Traina */ 564ef5d438eSPaul Traina if (key == NULL) { 565ef5d438eSPaul Traina e.page = h; 566ef5d438eSPaul Traina e.index = index; 567ef5d438eSPaul Traina if ((status = __bt_ret(t, &e, 568ef5d438eSPaul Traina &c->key, &c->key, NULL, NULL, 1)) != RET_SUCCESS) 569ef5d438eSPaul Traina return (status); 570ef5d438eSPaul Traina curcopy = 1; 571ef5d438eSPaul Traina key = &c->key; 572ef5d438eSPaul Traina } 573ef5d438eSPaul Traina /* Check previous key, if not at the beginning of the page. */ 574ef5d438eSPaul Traina if (index > 0) { 575ef5d438eSPaul Traina e.page = h; 576ef5d438eSPaul Traina e.index = index - 1; 577ef5d438eSPaul Traina if (__bt_cmp(t, key, &e) == 0) { 578ef5d438eSPaul Traina F_SET(c, CURS_BEFORE); 579ef5d438eSPaul Traina goto dup2; 580ef5d438eSPaul Traina } 581ef5d438eSPaul Traina } 582ef5d438eSPaul Traina /* Check next key, if not at the end of the page. */ 583ef5d438eSPaul Traina if (index < NEXTINDEX(h) - 1) { 584ef5d438eSPaul Traina e.page = h; 585ef5d438eSPaul Traina e.index = index + 1; 586ef5d438eSPaul Traina if (__bt_cmp(t, key, &e) == 0) { 587ef5d438eSPaul Traina F_SET(c, CURS_AFTER); 588ef5d438eSPaul Traina goto dup2; 589ef5d438eSPaul Traina } 590ef5d438eSPaul Traina } 591ef5d438eSPaul Traina /* Check previous key if at the beginning of the page. */ 592ef5d438eSPaul Traina if (index == 0 && h->prevpg != P_INVALID) { 593ef5d438eSPaul Traina if ((pg = mpool_get(t->bt_mp, h->prevpg, 0)) == NULL) 594ef5d438eSPaul Traina return (RET_ERROR); 595ef5d438eSPaul Traina e.page = pg; 596ef5d438eSPaul Traina e.index = NEXTINDEX(pg) - 1; 597ef5d438eSPaul Traina if (__bt_cmp(t, key, &e) == 0) { 598ef5d438eSPaul Traina F_SET(c, CURS_BEFORE); 599ef5d438eSPaul Traina goto dup1; 600ef5d438eSPaul Traina } 601ef5d438eSPaul Traina mpool_put(t->bt_mp, pg, 0); 602ef5d438eSPaul Traina } 603ef5d438eSPaul Traina /* Check next key if at the end of the page. */ 604ef5d438eSPaul Traina if (index == NEXTINDEX(h) - 1 && h->nextpg != P_INVALID) { 605ef5d438eSPaul Traina if ((pg = mpool_get(t->bt_mp, h->nextpg, 0)) == NULL) 606ef5d438eSPaul Traina return (RET_ERROR); 607ef5d438eSPaul Traina e.page = pg; 608ef5d438eSPaul Traina e.index = 0; 609ef5d438eSPaul Traina if (__bt_cmp(t, key, &e) == 0) { 610ef5d438eSPaul Traina F_SET(c, CURS_AFTER); 611ef5d438eSPaul Traina dup1: mpool_put(t->bt_mp, pg, 0); 612ef5d438eSPaul Traina dup2: c->pg.pgno = e.page->pgno; 613ef5d438eSPaul Traina c->pg.index = e.index; 614ef5d438eSPaul Traina return (RET_SUCCESS); 615ef5d438eSPaul Traina } 616ef5d438eSPaul Traina mpool_put(t->bt_mp, pg, 0); 617ef5d438eSPaul Traina } 618ef5d438eSPaul Traina } 619ef5d438eSPaul Traina e.page = h; 620ef5d438eSPaul Traina e.index = index; 621ef5d438eSPaul Traina if (curcopy || (status = 622ef5d438eSPaul Traina __bt_ret(t, &e, &c->key, &c->key, NULL, NULL, 1)) == RET_SUCCESS) { 623ef5d438eSPaul Traina F_SET(c, CURS_ACQUIRE); 624ef5d438eSPaul Traina return (RET_SUCCESS); 625ef5d438eSPaul Traina } 626ef5d438eSPaul Traina return (status); 627ef5d438eSPaul Traina } 628ef5d438eSPaul Traina 629ef5d438eSPaul Traina /* 630ef5d438eSPaul Traina * __bt_relink -- 631ef5d438eSPaul Traina * Link around a deleted page. 632ef5d438eSPaul Traina * 633ef5d438eSPaul Traina * Parameters: 634ef5d438eSPaul Traina * t: tree 635ef5d438eSPaul Traina * h: page to be deleted 636ef5d438eSPaul Traina */ 637ef5d438eSPaul Traina static int 638ef5d438eSPaul Traina __bt_relink(t, h) 639ef5d438eSPaul Traina BTREE *t; 640ef5d438eSPaul Traina PAGE *h; 641ef5d438eSPaul Traina { 642ef5d438eSPaul Traina PAGE *pg; 643ef5d438eSPaul Traina 644ef5d438eSPaul Traina if (h->nextpg != P_INVALID) { 645ef5d438eSPaul Traina if ((pg = mpool_get(t->bt_mp, h->nextpg, 0)) == NULL) 646ef5d438eSPaul Traina return (RET_ERROR); 647ef5d438eSPaul Traina pg->prevpg = h->prevpg; 648ef5d438eSPaul Traina mpool_put(t->bt_mp, pg, MPOOL_DIRTY); 649ef5d438eSPaul Traina } 650ef5d438eSPaul Traina if (h->prevpg != P_INVALID) { 651ef5d438eSPaul Traina if ((pg = mpool_get(t->bt_mp, h->prevpg, 0)) == NULL) 652ef5d438eSPaul Traina return (RET_ERROR); 653ef5d438eSPaul Traina pg->nextpg = h->nextpg; 654ef5d438eSPaul Traina mpool_put(t->bt_mp, pg, MPOOL_DIRTY); 655ef5d438eSPaul Traina } 656ef5d438eSPaul Traina return (0); 657ef5d438eSPaul Traina } 658