/*-
* See the file LICENSE for redistribution information.
*
* Copyright (c) 1996, 1997, 1998
* Sleepycat Software. All rights reserved.
*/
/*
* Copyright (c) 1990, 1993, 1994, 1995, 1996
* Keith Bostic. All rights reserved.
*/
/*
* Copyright (c) 1990, 1993, 1994, 1995
* The Regents of the University of California. All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* Mike Olson.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 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
* 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
* SUCH DAMAGE.
*/
#include "config.h"
#ifndef lint
#endif /* not lint */
#ifndef NO_SYSTEM_INCLUDES
#include <string.h>
#endif
#include "db_int.h"
#include "db_page.h"
#include "btree.h"
/*
* __bam_delete --
* Delete the items referenced by a key.
*
* PUBLIC: int __bam_delete __P((DB *, DB_TXN *, DBT *, u_int32_t));
*/
int
{
/* Check for invalid flags. */
if ((ret =
return (ret);
/* Allocate a cursor. */
return (ret);
/*
* the DB_DBT_USERMEM flag, as this might be a threaded application
* and the flags checking will catch us. We don't actually want the
* keys or data, so request a partial of length 0.
*/
/* If locking, set read-modify-write flag. */
}
goto err;
for (;;) {
goto err;
if (ret == DB_NOTFOUND) {
ret = 0;
break;
}
goto err;
}
}
err: /* Discard the cursor. */
return (ret);
}
/*
* __bam_ditem --
* Delete one or more entries from a page.
*
* PUBLIC: int __bam_ditem __P((DBC *, PAGE *, u_int32_t));
*/
int
PAGE *h;
{
int ret;
switch (TYPE(h)) {
case P_IBTREE:
case B_DUPLICATE:
case B_OVERFLOW:
goto offpage;
case B_KEYDATA:
break;
default:
}
break;
case P_IRECNO:
break;
case P_LBTREE:
/*
* If it's a duplicate key, discard the index and don't touch
* the actual page item.
*
* XXX
* This works because no data item can have an index matching
* any other index so even if the data item is in a key "slot",
* it won't match any other index.
*/
if ((indx % 2) == 0) {
/*
* Check for a duplicate after us on the page. NOTE:
* we have to delete the key item before deleting the
* data item, otherwise the "indx + P_INDX" calculation
* won't work!
*/
return (__bam_adjindx(dbc,
/*
* Check for a duplicate before us on the page. It
* doesn't matter if we delete the key item before or
* after the data item for the purposes of this one.
*/
return (__bam_adjindx(dbc,
}
/* FALLTHROUGH */
case P_LRECNO:
case B_DUPLICATE:
case B_OVERFLOW:
if ((ret =
return (ret);
} else
if ((ret =
return (ret);
break;
case B_KEYDATA:
break;
default:
}
break;
default:
}
/* Delete the item. */
return (ret);
/* Mark the page dirty. */
}
/*
* __bam_adjindx --
* Adjust an index on the page.
*
* PUBLIC: int __bam_adjindx __P((DBC *, PAGE *, u_int32_t, u_int32_t, int));
*/
int
PAGE *h;
int is_insert;
{
int ret;
/* Log the change. */
if (DB_LOGGING(dbc) &&
return (ret);
if (is_insert) {
++NUM_ENT(h);
} else {
--NUM_ENT(h);
}
/* Mark the page dirty. */
/* Adjust the cursors. */
return (0);
}
/*
* __bam_dpage --
* Delete a page from the tree.
*
* PUBLIC: int __bam_dpage __P((DBC *, const DBT *));
*/
int
{
PAGE *h;
ret = 0;
/*
* The locking protocol is that we acquire locks by walking down the
* tree, to avoid the obvious deadlocks.
*
* Call __bam_search to reacquire the empty leaf page, but this time
* get both the leaf page and it's parent, locked. Walk back up the
* tree, until we have the top pair of pages that we want to delete.
* Once we have the top page that we want to delete locked, lock the
* underlying pages and check to make sure they're still empty. If
* they are, delete them.
*/
/* Acquire a page and its parent, locked. */
if ((ret =
return (ret);
/*
* If we reach the root or the page isn't going to be empty
* when we delete one record, quit.
*/
break;
/* Release the two locked pages. */
}
/*
* Leave the stack pointer one after the last entry, we may be about
* to push more items on the stack.
*/
/*
* cp->csp[-2].page is the top page, which we're not going to delete,
* and cp->csp[-1].page is the first page we are going to delete.
*
* Walk down the chain, acquiring the rest of the pages until we've
* retrieved the leaf page. If we find any pages that aren't going
* to be emptied by the delete, someone else added something while we
* were walking the tree, and we discontinue the delete.
*/
if (ISLEAF(h)) {
if (NUM_ENT(h) != 0)
goto release;
break;
} else
if (NUM_ENT(h) != 1)
goto release;
/*
* Get the next page, write lock it and push it onto the stack.
* We know it's index 0, because it can only have one element.
*/
goto release;
goto release;
}
/* Adjust back to reference the last page on the stack. */
BT_STK_POP(cp);
/* Delete the pages. */
return (__bam_dpages(dbc));
/* Adjust back to reference the last page on the stack. */
BT_STK_POP(cp);
/* Discard any locked pages and return. */
__bam_stkrel(dbc, 0);
return (ret);
}
/*
* __bam_dpages --
* Delete a set of locked pages.
*
* PUBLIC: int __bam_dpages __P((DBC *));
*/
int
{
DBT a, b;
/*
* !!!
* There is an interesting deadlock situation here. We have to relink
* the leaf page chain around the leaf page being deleted. Consider
* a cursor walking through the leaf pages, that has the previous page
* read-locked and is waiting on a lock for the page we're deleting.
* It will deadlock here. This is a problem, because if our process is
* selected to resolve the deadlock, we'll leave an empty leaf page
* that we can never again access by walking down the tree. So, before
* we unlink the subtree, we relink the leaf page chain.
*/
goto release;
/*
* We have the entire stack of deletable pages locked.
*
* Delete the highest page in the tree's reference to the underlying
* stack of pages. Then, release that page, letting the rest of the
* tree get back to business.
*/
return (ret);
}
/*
* Free the rest of the stack of pages.
*
* !!!
* Don't bother checking for errors. We've unlinked the subtree from
* the tree, and there's no possibility of recovery outside of doing
* TXN rollback.
*/
/*
* Delete page entries so they will be restored as part of
* recovery.
*/
}
BT_STK_CLR(cp);
/*
* Try and collapse the tree a level -- this is only applicable
* if we've deleted the next-to-last element from the root page.
*
* There are two cases when collapsing a tree.
*
* If we've just deleted the last item from the root page, there is no
* further work to be done. The code above has emptied the root page
* and freed all pages below it.
*/
return (0);
/*
* If we just deleted the next-to-last item from the root page, the
* tree can collapse one or more levels. While there remains only a
* single item on the root page, write lock the last page referenced
* by the root page and copy it over the root page. If we can't get a
* write lock, that's okay, the tree just stays deeper than we'd like.
*/
/* Initialize. */
/* Lock the root. */
if ((ret =
goto stop;
goto stop;
goto stop;
/* Lock the child page. */
if ((ret =
goto stop;
goto stop;
/* Log the change. */
if (DB_LOGGING(dbc)) {
memset(&a, 0, sizeof(a));
memset(&b, 0, sizeof(b));
}
/*
* Make the switch.
*
* One fixup -- if the tree has record numbers and we're not
* converting to a leaf page, we have to preserve the total
* record count. Note that we are about to overwrite everything
* on the parent, including its LSN. This is actually OK,
* because the above log message, which describes this update,
* stores its LSN on the child page. When the child is copied
* to the parent, the correct LSN is going to copied into
* place in the parent.
*/
/* Mark the pages dirty. */
/* Adjust the cursors. */
/*
* Free the page copied onto the root page and discard its
* lock. (The call to __bam_free() discards our reference
* to the page.)
*/
if (0) {
}
if (p_lock != LOCK_INVALID)
if (c_lock != LOCK_INVALID)
}
return (0);
}