* Copyright (c) 1990, 1993
* The Regents of the University of California. All rights reserved.
* This code is derived from software contributed to Berkeley by
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
#if defined(LIBC_SCCS) && !defined(lint)
static char sccsid
[] = "@(#)bt_delete.c 8.2 (Berkeley) 9/7/93";
#endif /* LIBC_SCCS and not lint */
static int bt_bdelete
__P((BTREE
*, const DBT
*));
* __BT_DELETE -- Delete the item(s) referenced by a key.
* dbp: pointer to access method
* flags: R_CURSOR if deleting what the cursor references
* RET_ERROR, RET_SUCCESS and RET_SPECIAL if the key not found.
__bt_delete(dbp
, key
, flags
)
/* Toss any page pinned across calls. */
if (t
->bt_pinned
!= NULL
) {
mpool_put(t
->bt_mp
, t
->bt_pinned
, 0);
if (ISSET(t
, B_RDONLY
)) {
status
= bt_bdelete(t
, key
);
* If flags is R_CURSOR, delete the cursor; must already have
* started a scan and not have already deleted the record. For
* the delete cursor bit to have been set requires that the
* scan be initialized, so no reason to check.
if (!ISSET(t
, B_SEQINIT
))
status
= ISSET(t
, B_DELCRSR
) ?
RET_SPECIAL
: __bt_crsrdel(t
, &t
->bt_bcursor
);
if (status
== RET_SUCCESS
)
* BT_BDELETE -- Delete all key/data pairs matching the specified key.
* RET_ERROR, RET_SUCCESS and RET_SPECIAL if the key not found.
int deleted
, dirty1
, dirty2
, exact
;
/* Find any matching record; __bt_search pins the page. */
if ((e
= __bt_search(t
, key
, &exact
)) == NULL
)
mpool_put(t
->bt_mp
, e
->page
, 0);
* Delete forward, then delete backward, from the found key. The
* ordering is so that the deletions don't mess up the page refs.
* The first loop deletes the key from the original page, the second
* unpins the original page. In the first loop, dirty1 is set if
* the original page is modified, and dirty2 is set if any subsequent
* pages are modified. In the second loop, dirty1 starts off set if
* the original page has been modified, and is set if any subsequent
* If find the key referenced by the cursor, don't delete it, just
* flag it for future deletion. The cursor page number is P_INVALID
* unless the sequential scan is initialized, so no reason to check.
* A special case is when the already deleted cursor record was the
* only record found. If so, then the delete opertion fails as no
* Cycle in place in the current page until the current record doesn't
* match the key or the page is empty. If the latter, walk forward,
* skipping empty pages and repeating until a record doesn't match
* the key or the end of the tree is reached.
cpgno
= t
->bt_bcursor
.pgno
;
cindex
= t
->bt_bcursor
.index
;
for (h
= e
->page
, deleted
= 0;;) {
if (h
->pgno
== cpgno
&& e
->index
== cindex
) {
if (!ISSET(t
, B_DELCRSR
)) {
if (__bt_dleaf(t
, h
, e
->index
)) {
if (h
->pgno
!= save
.page
->pgno
)
mpool_put(t
->bt_mp
, h
, dirty2
);
mpool_put(t
->bt_mp
, save
.page
, dirty1
);
if (h
->pgno
== save
.page
->pgno
)
} while (e
->index
< NEXTINDEX(h
) && __bt_cmp(t
, key
, e
) == 0);
* Quit if didn't find a match, no next page, or first key on
* the next page doesn't match. Don't unpin the original page
* unless an error occurs.
if (e
->index
< NEXTINDEX(h
))
if ((pg
= h
->nextpg
) == P_INVALID
)
if (h
->pgno
!= save
.page
->pgno
)
mpool_put(t
->bt_mp
, h
, dirty2
);
if ((h
= mpool_get(t
->bt_mp
, pg
, 0)) == NULL
) {
mpool_put(t
->bt_mp
, save
.page
, dirty1
);
if (__bt_cmp(t
, key
, e
) != 0)
* Reach here with the original page and the last page referenced
* pinned (they may be the same). Release it if not the original.
done1
: if (h
->pgno
!= save
.page
->pgno
)
mpool_put(t
->bt_mp
, h
, dirty2
);
* Walk backwards from the record previous to the record returned by
* __bt_search, skipping empty pages, until a record doesn't match
* the key or reach the beginning of the tree.
for (h
= e
->page
; e
->index
; --e
->index
) {
if (__bt_cmp(t
, key
, e
) != 0)
if (h
->pgno
== cpgno
&& e
->index
== cindex
) {
if (!ISSET(t
, B_DELCRSR
)) {
if (__bt_dleaf(t
, h
, e
->index
) == RET_ERROR
) {
mpool_put(t
->bt_mp
, h
, dirty1
);
if (h
->pgno
== save
.page
->pgno
)
if ((pg
= h
->prevpg
) == P_INVALID
)
mpool_put(t
->bt_mp
, h
, dirty1
);
if ((e
->page
= mpool_get(t
->bt_mp
, pg
, 0)) == NULL
)
e
->index
= NEXTINDEX(e
->page
);
* Reach here with the last page that was looked at pinned. Release
done2
: mpool_put(t
->bt_mp
, h
, dirty1
);
return (deleted
? RET_SUCCESS
: RET_SPECIAL
);
* __BT_DLEAF -- Delete a single record from a leaf page.
* index: index on current page to delete
* RET_SUCCESS, RET_ERROR.
register indx_t
*ip
, offset
;
* Delete a record from a btree leaf page. Internal records are never
* deleted from internal pages, regardless of the records that caused
* them to be added being deleted. Pages made empty by deletion are
* not reclaimed. They are, however, made available for reuse.
* Pack the remaining entries at the end of the page, shift the indices
* down, overwriting the deleted record and its index. If the record
* uses overflow pages, make them available for reuse.
to
= bl
= GETBLEAF(h
, index
);
if (bl
->flags
& P_BIGKEY
&& __ovfl_delete(t
, bl
->bytes
) == RET_ERROR
)
if (bl
->flags
& P_BIGDATA
&&
__ovfl_delete(t
, bl
->bytes
+ bl
->ksize
) == RET_ERROR
)
* Compress the key/data pairs. Compress and adjust the [BR]LEAF
* offsets. Reset the headers.
from
= (char *)h
+ h
->upper
;
memmove(from
+ nbytes
, from
, (char *)to
- from
);
for (cnt
= index
, ip
= &h
->linp
[0]; cnt
--; ++ip
)
for (cnt
= NEXTINDEX(h
) - index
; --cnt
; ++ip
)
ip
[0] = ip
[1] < offset
? ip
[1] + nbytes
: ip
[1];
h
->lower
-= sizeof(indx_t
);