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