1*bec2d00aSderaadt /* $NetBSD: bt_delete.c,v 1.7 1996/05/03 21:50:44 cgd Exp $ */ 2df930be7Sderaadt 3df930be7Sderaadt /*- 4df930be7Sderaadt * Copyright (c) 1990, 1993, 1994 5df930be7Sderaadt * The Regents of the University of California. All rights reserved. 6df930be7Sderaadt * 7df930be7Sderaadt * This code is derived from software contributed to Berkeley by 8df930be7Sderaadt * Mike Olson. 9df930be7Sderaadt * 10df930be7Sderaadt * Redistribution and use in source and binary forms, with or without 11df930be7Sderaadt * modification, are permitted provided that the following conditions 12df930be7Sderaadt * are met: 13df930be7Sderaadt * 1. Redistributions of source code must retain the above copyright 14df930be7Sderaadt * notice, this list of conditions and the following disclaimer. 15df930be7Sderaadt * 2. Redistributions in binary form must reproduce the above copyright 16df930be7Sderaadt * notice, this list of conditions and the following disclaimer in the 17df930be7Sderaadt * documentation and/or other materials provided with the distribution. 18df930be7Sderaadt * 3. All advertising materials mentioning features or use of this software 19df930be7Sderaadt * must display the following acknowledgement: 20df930be7Sderaadt * This product includes software developed by the University of 21df930be7Sderaadt * California, Berkeley and its contributors. 22df930be7Sderaadt * 4. Neither the name of the University nor the names of its contributors 23df930be7Sderaadt * may be used to endorse or promote products derived from this software 24df930be7Sderaadt * without specific prior written permission. 25df930be7Sderaadt * 26df930be7Sderaadt * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 27df930be7Sderaadt * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28df930be7Sderaadt * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29df930be7Sderaadt * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30df930be7Sderaadt * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31df930be7Sderaadt * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32df930be7Sderaadt * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33df930be7Sderaadt * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34df930be7Sderaadt * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35df930be7Sderaadt * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36df930be7Sderaadt * SUCH DAMAGE. 37df930be7Sderaadt */ 38df930be7Sderaadt 39df930be7Sderaadt #if defined(LIBC_SCCS) && !defined(lint) 40df930be7Sderaadt #if 0 41*bec2d00aSderaadt static char sccsid[] = "@(#)bt_delete.c 8.13 (Berkeley) 7/28/94"; 42df930be7Sderaadt #else 43*bec2d00aSderaadt static char rcsid[] = "$NetBSD: bt_delete.c,v 1.7 1996/05/03 21:50:44 cgd Exp $"; 44df930be7Sderaadt #endif 45df930be7Sderaadt #endif /* LIBC_SCCS and not lint */ 46df930be7Sderaadt 47df930be7Sderaadt #include <sys/types.h> 48df930be7Sderaadt 49df930be7Sderaadt #include <errno.h> 50df930be7Sderaadt #include <stdio.h> 51df930be7Sderaadt #include <string.h> 52df930be7Sderaadt 53df930be7Sderaadt #include <db.h> 54df930be7Sderaadt #include "btree.h" 55df930be7Sderaadt 56*bec2d00aSderaadt static int __bt_bdelete __P((BTREE *, const DBT *)); 57*bec2d00aSderaadt static int __bt_curdel __P((BTREE *, const DBT *, PAGE *, u_int)); 58*bec2d00aSderaadt static int __bt_pdelete __P((BTREE *, PAGE *)); 59*bec2d00aSderaadt static int __bt_relink __P((BTREE *, PAGE *)); 60*bec2d00aSderaadt static int __bt_stkacq __P((BTREE *, PAGE **, CURSOR *)); 61df930be7Sderaadt 62df930be7Sderaadt /* 63*bec2d00aSderaadt * __bt_delete 64*bec2d00aSderaadt * Delete the item(s) referenced by a key. 65df930be7Sderaadt * 66*bec2d00aSderaadt * Return RET_SPECIAL if the key is not found. 67df930be7Sderaadt */ 68df930be7Sderaadt int 69df930be7Sderaadt __bt_delete(dbp, key, flags) 70df930be7Sderaadt const DB *dbp; 71df930be7Sderaadt const DBT *key; 72df930be7Sderaadt u_int flags; 73df930be7Sderaadt { 74df930be7Sderaadt BTREE *t; 75*bec2d00aSderaadt CURSOR *c; 76*bec2d00aSderaadt PAGE *h; 77df930be7Sderaadt int status; 78df930be7Sderaadt 79df930be7Sderaadt t = dbp->internal; 80df930be7Sderaadt 81df930be7Sderaadt /* Toss any page pinned across calls. */ 82df930be7Sderaadt if (t->bt_pinned != NULL) { 83df930be7Sderaadt mpool_put(t->bt_mp, t->bt_pinned, 0); 84df930be7Sderaadt t->bt_pinned = NULL; 85df930be7Sderaadt } 86df930be7Sderaadt 87*bec2d00aSderaadt /* Check for change to a read-only tree. */ 88*bec2d00aSderaadt if (F_ISSET(t, B_RDONLY)) { 89df930be7Sderaadt errno = EPERM; 90df930be7Sderaadt return (RET_ERROR); 91df930be7Sderaadt } 92df930be7Sderaadt 93df930be7Sderaadt switch (flags) { 94df930be7Sderaadt case 0: 95*bec2d00aSderaadt status = __bt_bdelete(t, key); 96df930be7Sderaadt break; 97df930be7Sderaadt case R_CURSOR: 98df930be7Sderaadt /* 99*bec2d00aSderaadt * If flags is R_CURSOR, delete the cursor. Must already 100*bec2d00aSderaadt * have started a scan and not have already deleted it. 101df930be7Sderaadt */ 102*bec2d00aSderaadt c = &t->bt_cursor; 103*bec2d00aSderaadt if (F_ISSET(c, CURS_INIT)) { 104*bec2d00aSderaadt if (F_ISSET(c, CURS_ACQUIRE | CURS_AFTER | CURS_BEFORE)) 105*bec2d00aSderaadt return (RET_SPECIAL); 106*bec2d00aSderaadt if ((h = mpool_get(t->bt_mp, c->pg.pgno, 0)) == NULL) 107*bec2d00aSderaadt return (RET_ERROR); 108*bec2d00aSderaadt 109*bec2d00aSderaadt /* 110*bec2d00aSderaadt * If the page is about to be emptied, we'll need to 111*bec2d00aSderaadt * delete it, which means we have to acquire a stack. 112*bec2d00aSderaadt */ 113*bec2d00aSderaadt if (NEXTINDEX(h) == 1) 114*bec2d00aSderaadt if (__bt_stkacq(t, &h, &t->bt_cursor)) 115*bec2d00aSderaadt return (RET_ERROR); 116*bec2d00aSderaadt 117*bec2d00aSderaadt status = __bt_dleaf(t, NULL, h, c->pg.index); 118*bec2d00aSderaadt 119*bec2d00aSderaadt if (NEXTINDEX(h) == 0 && status == RET_SUCCESS) { 120*bec2d00aSderaadt if (__bt_pdelete(t, h)) 121*bec2d00aSderaadt return (RET_ERROR); 122*bec2d00aSderaadt } else 123*bec2d00aSderaadt mpool_put(t->bt_mp, 124*bec2d00aSderaadt h, status == RET_SUCCESS ? MPOOL_DIRTY : 0); 125df930be7Sderaadt break; 126*bec2d00aSderaadt } 127*bec2d00aSderaadt /* FALLTHROUGH */ 128df930be7Sderaadt default: 129*bec2d00aSderaadt errno = EINVAL; 130df930be7Sderaadt return (RET_ERROR); 131df930be7Sderaadt } 132df930be7Sderaadt if (status == RET_SUCCESS) 133*bec2d00aSderaadt F_SET(t, B_MODIFIED); 134df930be7Sderaadt return (status); 135df930be7Sderaadt } 136df930be7Sderaadt 137df930be7Sderaadt /* 138*bec2d00aSderaadt * __bt_stkacq -- 139*bec2d00aSderaadt * Acquire a stack so we can delete a cursor entry. 140df930be7Sderaadt * 141df930be7Sderaadt * Parameters: 142*bec2d00aSderaadt * t: tree 143*bec2d00aSderaadt * hp: pointer to current, pinned PAGE pointer 144*bec2d00aSderaadt * c: pointer to the cursor 145*bec2d00aSderaadt * 146*bec2d00aSderaadt * Returns: 147*bec2d00aSderaadt * 0 on success, 1 on failure 148*bec2d00aSderaadt */ 149*bec2d00aSderaadt static int 150*bec2d00aSderaadt __bt_stkacq(t, hp, c) 151*bec2d00aSderaadt BTREE *t; 152*bec2d00aSderaadt PAGE **hp; 153*bec2d00aSderaadt CURSOR *c; 154*bec2d00aSderaadt { 155*bec2d00aSderaadt BINTERNAL *bi; 156*bec2d00aSderaadt EPG *e; 157*bec2d00aSderaadt EPGNO *parent; 158*bec2d00aSderaadt PAGE *h; 159*bec2d00aSderaadt indx_t index; 160*bec2d00aSderaadt pgno_t pgno; 161*bec2d00aSderaadt recno_t nextpg, prevpg; 162*bec2d00aSderaadt int exact, level; 163*bec2d00aSderaadt 164*bec2d00aSderaadt /* 165*bec2d00aSderaadt * Find the first occurrence of the key in the tree. Toss the 166*bec2d00aSderaadt * currently locked page so we don't hit an already-locked page. 167*bec2d00aSderaadt */ 168*bec2d00aSderaadt h = *hp; 169*bec2d00aSderaadt mpool_put(t->bt_mp, h, 0); 170*bec2d00aSderaadt if ((e = __bt_search(t, &c->key, &exact)) == NULL) 171*bec2d00aSderaadt return (1); 172*bec2d00aSderaadt h = e->page; 173*bec2d00aSderaadt 174*bec2d00aSderaadt /* See if we got it in one shot. */ 175*bec2d00aSderaadt if (h->pgno == c->pg.pgno) 176*bec2d00aSderaadt goto ret; 177*bec2d00aSderaadt 178*bec2d00aSderaadt /* 179*bec2d00aSderaadt * Move right, looking for the page. At each move we have to move 180*bec2d00aSderaadt * up the stack until we don't have to move to the next page. If 181*bec2d00aSderaadt * we have to change pages at an internal level, we have to fix the 182*bec2d00aSderaadt * stack back up. 183*bec2d00aSderaadt */ 184*bec2d00aSderaadt while (h->pgno != c->pg.pgno) { 185*bec2d00aSderaadt if ((nextpg = h->nextpg) == P_INVALID) 186*bec2d00aSderaadt break; 187*bec2d00aSderaadt mpool_put(t->bt_mp, h, 0); 188*bec2d00aSderaadt 189*bec2d00aSderaadt /* Move up the stack. */ 190*bec2d00aSderaadt for (level = 0; (parent = BT_POP(t)) != NULL; ++level) { 191*bec2d00aSderaadt /* Get the parent page. */ 192*bec2d00aSderaadt if ((h = mpool_get(t->bt_mp, parent->pgno, 0)) == NULL) 193*bec2d00aSderaadt return (1); 194*bec2d00aSderaadt 195*bec2d00aSderaadt /* Move to the next index. */ 196*bec2d00aSderaadt if (parent->index != NEXTINDEX(h) - 1) { 197*bec2d00aSderaadt index = parent->index + 1; 198*bec2d00aSderaadt BT_PUSH(t, h->pgno, index); 199*bec2d00aSderaadt break; 200*bec2d00aSderaadt } 201*bec2d00aSderaadt mpool_put(t->bt_mp, h, 0); 202*bec2d00aSderaadt } 203*bec2d00aSderaadt 204*bec2d00aSderaadt /* Restore the stack. */ 205*bec2d00aSderaadt while (level--) { 206*bec2d00aSderaadt /* Push the next level down onto the stack. */ 207*bec2d00aSderaadt bi = GETBINTERNAL(h, index); 208*bec2d00aSderaadt pgno = bi->pgno; 209*bec2d00aSderaadt BT_PUSH(t, pgno, 0); 210*bec2d00aSderaadt 211*bec2d00aSderaadt /* Lose the currently pinned page. */ 212*bec2d00aSderaadt mpool_put(t->bt_mp, h, 0); 213*bec2d00aSderaadt 214*bec2d00aSderaadt /* Get the next level down. */ 215*bec2d00aSderaadt if ((h = mpool_get(t->bt_mp, pgno, 0)) == NULL) 216*bec2d00aSderaadt return (1); 217*bec2d00aSderaadt index = 0; 218*bec2d00aSderaadt } 219*bec2d00aSderaadt mpool_put(t->bt_mp, h, 0); 220*bec2d00aSderaadt if ((h = mpool_get(t->bt_mp, nextpg, 0)) == NULL) 221*bec2d00aSderaadt return (1); 222*bec2d00aSderaadt } 223*bec2d00aSderaadt 224*bec2d00aSderaadt if (h->pgno == c->pg.pgno) 225*bec2d00aSderaadt goto ret; 226*bec2d00aSderaadt 227*bec2d00aSderaadt /* Reacquire the original stack. */ 228*bec2d00aSderaadt mpool_put(t->bt_mp, h, 0); 229*bec2d00aSderaadt if ((e = __bt_search(t, &c->key, &exact)) == NULL) 230*bec2d00aSderaadt return (1); 231*bec2d00aSderaadt h = e->page; 232*bec2d00aSderaadt 233*bec2d00aSderaadt /* 234*bec2d00aSderaadt * Move left, looking for the page. At each move we have to move 235*bec2d00aSderaadt * up the stack until we don't have to change pages to move to the 236*bec2d00aSderaadt * next page. If we have to change pages at an internal level, we 237*bec2d00aSderaadt * have to fix the stack back up. 238*bec2d00aSderaadt */ 239*bec2d00aSderaadt while (h->pgno != c->pg.pgno) { 240*bec2d00aSderaadt if ((prevpg = h->prevpg) == P_INVALID) 241*bec2d00aSderaadt break; 242*bec2d00aSderaadt mpool_put(t->bt_mp, h, 0); 243*bec2d00aSderaadt 244*bec2d00aSderaadt /* Move up the stack. */ 245*bec2d00aSderaadt for (level = 0; (parent = BT_POP(t)) != NULL; ++level) { 246*bec2d00aSderaadt /* Get the parent page. */ 247*bec2d00aSderaadt if ((h = mpool_get(t->bt_mp, parent->pgno, 0)) == NULL) 248*bec2d00aSderaadt return (1); 249*bec2d00aSderaadt 250*bec2d00aSderaadt /* Move to the next index. */ 251*bec2d00aSderaadt if (parent->index != 0) { 252*bec2d00aSderaadt index = parent->index - 1; 253*bec2d00aSderaadt BT_PUSH(t, h->pgno, index); 254*bec2d00aSderaadt break; 255*bec2d00aSderaadt } 256*bec2d00aSderaadt mpool_put(t->bt_mp, h, 0); 257*bec2d00aSderaadt } 258*bec2d00aSderaadt 259*bec2d00aSderaadt /* Restore the stack. */ 260*bec2d00aSderaadt while (level--) { 261*bec2d00aSderaadt /* Push the next level down onto the stack. */ 262*bec2d00aSderaadt bi = GETBINTERNAL(h, index); 263*bec2d00aSderaadt pgno = bi->pgno; 264*bec2d00aSderaadt 265*bec2d00aSderaadt /* Lose the currently pinned page. */ 266*bec2d00aSderaadt mpool_put(t->bt_mp, h, 0); 267*bec2d00aSderaadt 268*bec2d00aSderaadt /* Get the next level down. */ 269*bec2d00aSderaadt if ((h = mpool_get(t->bt_mp, pgno, 0)) == NULL) 270*bec2d00aSderaadt return (1); 271*bec2d00aSderaadt 272*bec2d00aSderaadt index = NEXTINDEX(h) - 1; 273*bec2d00aSderaadt BT_PUSH(t, pgno, index); 274*bec2d00aSderaadt } 275*bec2d00aSderaadt mpool_put(t->bt_mp, h, 0); 276*bec2d00aSderaadt if ((h = mpool_get(t->bt_mp, prevpg, 0)) == NULL) 277*bec2d00aSderaadt return (1); 278*bec2d00aSderaadt } 279*bec2d00aSderaadt 280*bec2d00aSderaadt 281*bec2d00aSderaadt ret: mpool_put(t->bt_mp, h, 0); 282*bec2d00aSderaadt return ((*hp = mpool_get(t->bt_mp, c->pg.pgno, 0)) == NULL); 283*bec2d00aSderaadt } 284*bec2d00aSderaadt 285*bec2d00aSderaadt /* 286*bec2d00aSderaadt * __bt_bdelete -- 287*bec2d00aSderaadt * Delete all key/data pairs matching the specified key. 288*bec2d00aSderaadt * 289*bec2d00aSderaadt * Parameters: 290*bec2d00aSderaadt * t: tree 291df930be7Sderaadt * key: key to delete 292df930be7Sderaadt * 293df930be7Sderaadt * Returns: 294df930be7Sderaadt * RET_ERROR, RET_SUCCESS and RET_SPECIAL if the key not found. 295df930be7Sderaadt */ 296df930be7Sderaadt static int 297*bec2d00aSderaadt __bt_bdelete(t, key) 298df930be7Sderaadt BTREE *t; 299df930be7Sderaadt const DBT *key; 300df930be7Sderaadt { 301*bec2d00aSderaadt EPG *e; 302df930be7Sderaadt PAGE *h; 303*bec2d00aSderaadt int deleted, exact, redo; 304*bec2d00aSderaadt 305*bec2d00aSderaadt deleted = 0; 306df930be7Sderaadt 307df930be7Sderaadt /* Find any matching record; __bt_search pins the page. */ 308*bec2d00aSderaadt loop: if ((e = __bt_search(t, key, &exact)) == NULL) 309*bec2d00aSderaadt return (deleted ? RET_SUCCESS : RET_ERROR); 310df930be7Sderaadt if (!exact) { 311df930be7Sderaadt mpool_put(t->bt_mp, e->page, 0); 312df930be7Sderaadt return (deleted ? RET_SUCCESS : RET_SPECIAL); 313df930be7Sderaadt } 314df930be7Sderaadt 315df930be7Sderaadt /* 316*bec2d00aSderaadt * Delete forward, then delete backward, from the found key. If 317*bec2d00aSderaadt * there are duplicates and we reach either side of the page, do 318*bec2d00aSderaadt * the key search again, so that we get them all. 319*bec2d00aSderaadt */ 320*bec2d00aSderaadt redo = 0; 321*bec2d00aSderaadt h = e->page; 322*bec2d00aSderaadt do { 323*bec2d00aSderaadt if (__bt_dleaf(t, key, h, e->index)) { 324*bec2d00aSderaadt mpool_put(t->bt_mp, h, 0); 325*bec2d00aSderaadt return (RET_ERROR); 326*bec2d00aSderaadt } 327*bec2d00aSderaadt if (F_ISSET(t, B_NODUPS)) { 328*bec2d00aSderaadt if (NEXTINDEX(h) == 0) { 329*bec2d00aSderaadt if (__bt_pdelete(t, h)) 330*bec2d00aSderaadt return (RET_ERROR); 331*bec2d00aSderaadt } else 332*bec2d00aSderaadt mpool_put(t->bt_mp, h, MPOOL_DIRTY); 333*bec2d00aSderaadt return (RET_SUCCESS); 334*bec2d00aSderaadt } 335*bec2d00aSderaadt deleted = 1; 336*bec2d00aSderaadt } while (e->index < NEXTINDEX(h) && __bt_cmp(t, key, e) == 0); 337*bec2d00aSderaadt 338*bec2d00aSderaadt /* Check for right-hand edge of the page. */ 339*bec2d00aSderaadt if (e->index == NEXTINDEX(h)) 340*bec2d00aSderaadt redo = 1; 341*bec2d00aSderaadt 342*bec2d00aSderaadt /* Delete from the key to the beginning of the page. */ 343*bec2d00aSderaadt while (e->index-- > 0) { 344*bec2d00aSderaadt if (__bt_cmp(t, key, e) != 0) 345*bec2d00aSderaadt break; 346*bec2d00aSderaadt if (__bt_dleaf(t, key, h, e->index) == RET_ERROR) { 347*bec2d00aSderaadt mpool_put(t->bt_mp, h, 0); 348*bec2d00aSderaadt return (RET_ERROR); 349*bec2d00aSderaadt } 350*bec2d00aSderaadt if (e->index == 0) 351*bec2d00aSderaadt redo = 1; 352*bec2d00aSderaadt } 353*bec2d00aSderaadt 354*bec2d00aSderaadt /* Check for an empty page. */ 355*bec2d00aSderaadt if (NEXTINDEX(h) == 0) { 356*bec2d00aSderaadt if (__bt_pdelete(t, h)) 357*bec2d00aSderaadt return (RET_ERROR); 358*bec2d00aSderaadt goto loop; 359*bec2d00aSderaadt } 360*bec2d00aSderaadt 361*bec2d00aSderaadt /* Put the page. */ 362*bec2d00aSderaadt mpool_put(t->bt_mp, h, MPOOL_DIRTY); 363*bec2d00aSderaadt 364*bec2d00aSderaadt if (redo) 365*bec2d00aSderaadt goto loop; 366*bec2d00aSderaadt return (RET_SUCCESS); 367*bec2d00aSderaadt } 368*bec2d00aSderaadt 369*bec2d00aSderaadt /* 370*bec2d00aSderaadt * __bt_pdelete -- 371*bec2d00aSderaadt * Delete a single page from the tree. 372df930be7Sderaadt * 373df930be7Sderaadt * Parameters: 374df930be7Sderaadt * t: tree 375*bec2d00aSderaadt * h: leaf page 376*bec2d00aSderaadt * 377*bec2d00aSderaadt * Returns: 378*bec2d00aSderaadt * RET_SUCCESS, RET_ERROR. 379*bec2d00aSderaadt * 380*bec2d00aSderaadt * Side-effects: 381*bec2d00aSderaadt * mpool_put's the page 382*bec2d00aSderaadt */ 383*bec2d00aSderaadt static int 384*bec2d00aSderaadt __bt_pdelete(t, h) 385*bec2d00aSderaadt BTREE *t; 386*bec2d00aSderaadt PAGE *h; 387*bec2d00aSderaadt { 388*bec2d00aSderaadt BINTERNAL *bi; 389*bec2d00aSderaadt PAGE *pg; 390*bec2d00aSderaadt EPGNO *parent; 391*bec2d00aSderaadt indx_t cnt, index, *ip, offset; 392*bec2d00aSderaadt u_int32_t nksize; 393*bec2d00aSderaadt char *from; 394*bec2d00aSderaadt 395*bec2d00aSderaadt /* 396*bec2d00aSderaadt * Walk the parent page stack -- a LIFO stack of the pages that were 397*bec2d00aSderaadt * traversed when we searched for the page where the delete occurred. 398*bec2d00aSderaadt * Each stack entry is a page number and a page index offset. The 399*bec2d00aSderaadt * offset is for the page traversed on the search. We've just deleted 400*bec2d00aSderaadt * a page, so we have to delete the key from the parent page. 401*bec2d00aSderaadt * 402*bec2d00aSderaadt * If the delete from the parent page makes it empty, this process may 403*bec2d00aSderaadt * continue all the way up the tree. We stop if we reach the root page 404*bec2d00aSderaadt * (which is never deleted, it's just not worth the effort) or if the 405*bec2d00aSderaadt * delete does not empty the page. 406*bec2d00aSderaadt */ 407*bec2d00aSderaadt while ((parent = BT_POP(t)) != NULL) { 408*bec2d00aSderaadt /* Get the parent page. */ 409*bec2d00aSderaadt if ((pg = mpool_get(t->bt_mp, parent->pgno, 0)) == NULL) 410*bec2d00aSderaadt return (RET_ERROR); 411*bec2d00aSderaadt 412*bec2d00aSderaadt index = parent->index; 413*bec2d00aSderaadt bi = GETBINTERNAL(pg, index); 414*bec2d00aSderaadt 415*bec2d00aSderaadt /* Free any overflow pages. */ 416*bec2d00aSderaadt if (bi->flags & P_BIGKEY && 417*bec2d00aSderaadt __ovfl_delete(t, bi->bytes) == RET_ERROR) { 418*bec2d00aSderaadt mpool_put(t->bt_mp, pg, 0); 419*bec2d00aSderaadt return (RET_ERROR); 420*bec2d00aSderaadt } 421*bec2d00aSderaadt 422*bec2d00aSderaadt /* 423*bec2d00aSderaadt * Free the parent if it has only the one key and it's not the 424*bec2d00aSderaadt * root page. If it's the rootpage, turn it back into an empty 425*bec2d00aSderaadt * leaf page. 426*bec2d00aSderaadt */ 427*bec2d00aSderaadt if (NEXTINDEX(pg) == 1) 428*bec2d00aSderaadt if (pg->pgno == P_ROOT) { 429*bec2d00aSderaadt pg->lower = BTDATAOFF; 430*bec2d00aSderaadt pg->upper = t->bt_psize; 431*bec2d00aSderaadt pg->flags = P_BLEAF; 432*bec2d00aSderaadt } else { 433*bec2d00aSderaadt if (__bt_relink(t, pg) || __bt_free(t, pg)) 434*bec2d00aSderaadt return (RET_ERROR); 435*bec2d00aSderaadt continue; 436*bec2d00aSderaadt } 437*bec2d00aSderaadt else { 438*bec2d00aSderaadt /* Pack remaining key items at the end of the page. */ 439*bec2d00aSderaadt nksize = NBINTERNAL(bi->ksize); 440*bec2d00aSderaadt from = (char *)pg + pg->upper; 441*bec2d00aSderaadt memmove(from + nksize, from, (char *)bi - from); 442*bec2d00aSderaadt pg->upper += nksize; 443*bec2d00aSderaadt 444*bec2d00aSderaadt /* Adjust indices' offsets, shift the indices down. */ 445*bec2d00aSderaadt offset = pg->linp[index]; 446*bec2d00aSderaadt for (cnt = index, ip = &pg->linp[0]; cnt--; ++ip) 447*bec2d00aSderaadt if (ip[0] < offset) 448*bec2d00aSderaadt ip[0] += nksize; 449*bec2d00aSderaadt for (cnt = NEXTINDEX(pg) - index; --cnt; ++ip) 450*bec2d00aSderaadt ip[0] = ip[1] < offset ? ip[1] + nksize : ip[1]; 451*bec2d00aSderaadt pg->lower -= sizeof(indx_t); 452*bec2d00aSderaadt } 453*bec2d00aSderaadt 454*bec2d00aSderaadt mpool_put(t->bt_mp, pg, MPOOL_DIRTY); 455*bec2d00aSderaadt break; 456*bec2d00aSderaadt } 457*bec2d00aSderaadt 458*bec2d00aSderaadt /* Free the leaf page, as long as it wasn't the root. */ 459*bec2d00aSderaadt if (h->pgno == P_ROOT) { 460*bec2d00aSderaadt mpool_put(t->bt_mp, h, MPOOL_DIRTY); 461*bec2d00aSderaadt return (RET_SUCCESS); 462*bec2d00aSderaadt } 463*bec2d00aSderaadt return (__bt_relink(t, h) || __bt_free(t, h)); 464*bec2d00aSderaadt } 465*bec2d00aSderaadt 466*bec2d00aSderaadt /* 467*bec2d00aSderaadt * __bt_dleaf -- 468*bec2d00aSderaadt * Delete a single record from a leaf page. 469*bec2d00aSderaadt * 470*bec2d00aSderaadt * Parameters: 471*bec2d00aSderaadt * t: tree 472*bec2d00aSderaadt * key: referenced key 473*bec2d00aSderaadt * h: page 474*bec2d00aSderaadt * index: index on page to delete 475df930be7Sderaadt * 476df930be7Sderaadt * Returns: 477df930be7Sderaadt * RET_SUCCESS, RET_ERROR. 478df930be7Sderaadt */ 479df930be7Sderaadt int 480*bec2d00aSderaadt __bt_dleaf(t, key, h, index) 481df930be7Sderaadt BTREE *t; 482*bec2d00aSderaadt const DBT *key; 483df930be7Sderaadt PAGE *h; 484*bec2d00aSderaadt u_int index; 485df930be7Sderaadt { 486*bec2d00aSderaadt BLEAF *bl; 487*bec2d00aSderaadt indx_t cnt, *ip, offset; 488*bec2d00aSderaadt u_int32_t nbytes; 489df930be7Sderaadt void *to; 490*bec2d00aSderaadt char *from; 491df930be7Sderaadt 492*bec2d00aSderaadt /* If this record is referenced by the cursor, delete the cursor. */ 493*bec2d00aSderaadt if (F_ISSET(&t->bt_cursor, CURS_INIT) && 494*bec2d00aSderaadt !F_ISSET(&t->bt_cursor, CURS_ACQUIRE) && 495*bec2d00aSderaadt t->bt_cursor.pg.pgno == h->pgno && t->bt_cursor.pg.index == index && 496*bec2d00aSderaadt __bt_curdel(t, key, h, index)) 497*bec2d00aSderaadt return (RET_ERROR); 498*bec2d00aSderaadt 499*bec2d00aSderaadt /* If the entry uses overflow pages, make them available for reuse. */ 500df930be7Sderaadt to = bl = GETBLEAF(h, index); 501df930be7Sderaadt if (bl->flags & P_BIGKEY && __ovfl_delete(t, bl->bytes) == RET_ERROR) 502df930be7Sderaadt return (RET_ERROR); 503df930be7Sderaadt if (bl->flags & P_BIGDATA && 504df930be7Sderaadt __ovfl_delete(t, bl->bytes + bl->ksize) == RET_ERROR) 505df930be7Sderaadt return (RET_ERROR); 506df930be7Sderaadt 507*bec2d00aSderaadt /* Pack the remaining key/data items at the end of the page. */ 508*bec2d00aSderaadt nbytes = NBLEAF(bl); 509df930be7Sderaadt from = (char *)h + h->upper; 510df930be7Sderaadt memmove(from + nbytes, from, (char *)to - from); 511df930be7Sderaadt h->upper += nbytes; 512df930be7Sderaadt 513*bec2d00aSderaadt /* Adjust the indices' offsets, shift the indices down. */ 514df930be7Sderaadt offset = h->linp[index]; 515df930be7Sderaadt for (cnt = index, ip = &h->linp[0]; cnt--; ++ip) 516df930be7Sderaadt if (ip[0] < offset) 517df930be7Sderaadt ip[0] += nbytes; 518df930be7Sderaadt for (cnt = NEXTINDEX(h) - index; --cnt; ++ip) 519df930be7Sderaadt ip[0] = ip[1] < offset ? ip[1] + nbytes : ip[1]; 520df930be7Sderaadt h->lower -= sizeof(indx_t); 521*bec2d00aSderaadt 522*bec2d00aSderaadt /* If the cursor is on this page, adjust it as necessary. */ 523*bec2d00aSderaadt if (F_ISSET(&t->bt_cursor, CURS_INIT) && 524*bec2d00aSderaadt !F_ISSET(&t->bt_cursor, CURS_ACQUIRE) && 525*bec2d00aSderaadt t->bt_cursor.pg.pgno == h->pgno && t->bt_cursor.pg.index > index) 526*bec2d00aSderaadt --t->bt_cursor.pg.index; 527*bec2d00aSderaadt 528df930be7Sderaadt return (RET_SUCCESS); 529df930be7Sderaadt } 530*bec2d00aSderaadt 531*bec2d00aSderaadt /* 532*bec2d00aSderaadt * __bt_curdel -- 533*bec2d00aSderaadt * Delete the cursor. 534*bec2d00aSderaadt * 535*bec2d00aSderaadt * Parameters: 536*bec2d00aSderaadt * t: tree 537*bec2d00aSderaadt * key: referenced key (or NULL) 538*bec2d00aSderaadt * h: page 539*bec2d00aSderaadt * index: index on page to delete 540*bec2d00aSderaadt * 541*bec2d00aSderaadt * Returns: 542*bec2d00aSderaadt * RET_SUCCESS, RET_ERROR. 543*bec2d00aSderaadt */ 544*bec2d00aSderaadt static int 545*bec2d00aSderaadt __bt_curdel(t, key, h, index) 546*bec2d00aSderaadt BTREE *t; 547*bec2d00aSderaadt const DBT *key; 548*bec2d00aSderaadt PAGE *h; 549*bec2d00aSderaadt u_int index; 550*bec2d00aSderaadt { 551*bec2d00aSderaadt CURSOR *c; 552*bec2d00aSderaadt EPG e; 553*bec2d00aSderaadt PAGE *pg; 554*bec2d00aSderaadt int curcopy, status; 555*bec2d00aSderaadt 556*bec2d00aSderaadt /* 557*bec2d00aSderaadt * If there are duplicates, move forward or backward to one. 558*bec2d00aSderaadt * Otherwise, copy the key into the cursor area. 559*bec2d00aSderaadt */ 560*bec2d00aSderaadt c = &t->bt_cursor; 561*bec2d00aSderaadt F_CLR(c, CURS_AFTER | CURS_BEFORE | CURS_ACQUIRE); 562*bec2d00aSderaadt 563*bec2d00aSderaadt curcopy = 0; 564*bec2d00aSderaadt if (!F_ISSET(t, B_NODUPS)) { 565*bec2d00aSderaadt /* 566*bec2d00aSderaadt * We're going to have to do comparisons. If we weren't 567*bec2d00aSderaadt * provided a copy of the key, i.e. the user is deleting 568*bec2d00aSderaadt * the current cursor position, get one. 569*bec2d00aSderaadt */ 570*bec2d00aSderaadt if (key == NULL) { 571*bec2d00aSderaadt e.page = h; 572*bec2d00aSderaadt e.index = index; 573*bec2d00aSderaadt if ((status = __bt_ret(t, &e, 574*bec2d00aSderaadt &c->key, &c->key, NULL, NULL, 1)) != RET_SUCCESS) 575*bec2d00aSderaadt return (status); 576*bec2d00aSderaadt curcopy = 1; 577*bec2d00aSderaadt key = &c->key; 578*bec2d00aSderaadt } 579*bec2d00aSderaadt /* Check previous key, if not at the beginning of the page. */ 580*bec2d00aSderaadt if (index > 0) { 581*bec2d00aSderaadt e.page = h; 582*bec2d00aSderaadt e.index = index - 1; 583*bec2d00aSderaadt if (__bt_cmp(t, key, &e) == 0) { 584*bec2d00aSderaadt F_SET(c, CURS_BEFORE); 585*bec2d00aSderaadt goto dup2; 586*bec2d00aSderaadt } 587*bec2d00aSderaadt } 588*bec2d00aSderaadt /* Check next key, if not at the end of the page. */ 589*bec2d00aSderaadt if (index < NEXTINDEX(h) - 1) { 590*bec2d00aSderaadt e.page = h; 591*bec2d00aSderaadt e.index = index + 1; 592*bec2d00aSderaadt if (__bt_cmp(t, key, &e) == 0) { 593*bec2d00aSderaadt F_SET(c, CURS_AFTER); 594*bec2d00aSderaadt goto dup2; 595*bec2d00aSderaadt } 596*bec2d00aSderaadt } 597*bec2d00aSderaadt /* Check previous key if at the beginning of the page. */ 598*bec2d00aSderaadt if (index == 0 && h->prevpg != P_INVALID) { 599*bec2d00aSderaadt if ((pg = mpool_get(t->bt_mp, h->prevpg, 0)) == NULL) 600*bec2d00aSderaadt return (RET_ERROR); 601*bec2d00aSderaadt e.page = pg; 602*bec2d00aSderaadt e.index = NEXTINDEX(pg) - 1; 603*bec2d00aSderaadt if (__bt_cmp(t, key, &e) == 0) { 604*bec2d00aSderaadt F_SET(c, CURS_BEFORE); 605*bec2d00aSderaadt goto dup1; 606*bec2d00aSderaadt } 607*bec2d00aSderaadt mpool_put(t->bt_mp, pg, 0); 608*bec2d00aSderaadt } 609*bec2d00aSderaadt /* Check next key if at the end of the page. */ 610*bec2d00aSderaadt if (index == NEXTINDEX(h) - 1 && h->nextpg != P_INVALID) { 611*bec2d00aSderaadt if ((pg = mpool_get(t->bt_mp, h->nextpg, 0)) == NULL) 612*bec2d00aSderaadt return (RET_ERROR); 613*bec2d00aSderaadt e.page = pg; 614*bec2d00aSderaadt e.index = 0; 615*bec2d00aSderaadt if (__bt_cmp(t, key, &e) == 0) { 616*bec2d00aSderaadt F_SET(c, CURS_AFTER); 617*bec2d00aSderaadt dup1: mpool_put(t->bt_mp, pg, 0); 618*bec2d00aSderaadt dup2: c->pg.pgno = e.page->pgno; 619*bec2d00aSderaadt c->pg.index = e.index; 620*bec2d00aSderaadt return (RET_SUCCESS); 621*bec2d00aSderaadt } 622*bec2d00aSderaadt mpool_put(t->bt_mp, pg, 0); 623*bec2d00aSderaadt } 624*bec2d00aSderaadt } 625*bec2d00aSderaadt e.page = h; 626*bec2d00aSderaadt e.index = index; 627*bec2d00aSderaadt if (curcopy || (status = 628*bec2d00aSderaadt __bt_ret(t, &e, &c->key, &c->key, NULL, NULL, 1)) == RET_SUCCESS) { 629*bec2d00aSderaadt F_SET(c, CURS_ACQUIRE); 630*bec2d00aSderaadt return (RET_SUCCESS); 631*bec2d00aSderaadt } 632*bec2d00aSderaadt return (status); 633*bec2d00aSderaadt } 634*bec2d00aSderaadt 635*bec2d00aSderaadt /* 636*bec2d00aSderaadt * __bt_relink -- 637*bec2d00aSderaadt * Link around a deleted page. 638*bec2d00aSderaadt * 639*bec2d00aSderaadt * Parameters: 640*bec2d00aSderaadt * t: tree 641*bec2d00aSderaadt * h: page to be deleted 642*bec2d00aSderaadt */ 643*bec2d00aSderaadt static int 644*bec2d00aSderaadt __bt_relink(t, h) 645*bec2d00aSderaadt BTREE *t; 646*bec2d00aSderaadt PAGE *h; 647*bec2d00aSderaadt { 648*bec2d00aSderaadt PAGE *pg; 649*bec2d00aSderaadt 650*bec2d00aSderaadt if (h->nextpg != P_INVALID) { 651*bec2d00aSderaadt if ((pg = mpool_get(t->bt_mp, h->nextpg, 0)) == NULL) 652*bec2d00aSderaadt return (RET_ERROR); 653*bec2d00aSderaadt pg->prevpg = h->prevpg; 654*bec2d00aSderaadt mpool_put(t->bt_mp, pg, MPOOL_DIRTY); 655*bec2d00aSderaadt } 656*bec2d00aSderaadt if (h->prevpg != P_INVALID) { 657*bec2d00aSderaadt if ((pg = mpool_get(t->bt_mp, h->prevpg, 0)) == NULL) 658*bec2d00aSderaadt return (RET_ERROR); 659*bec2d00aSderaadt pg->nextpg = h->nextpg; 660*bec2d00aSderaadt mpool_put(t->bt_mp, pg, MPOOL_DIRTY); 661*bec2d00aSderaadt } 662*bec2d00aSderaadt return (0); 663*bec2d00aSderaadt } 664