1*0Sstevel@tonic-gate /*-
2*0Sstevel@tonic-gate * See the file LICENSE for redistribution information.
3*0Sstevel@tonic-gate *
4*0Sstevel@tonic-gate * Copyright (c) 1996, 1997, 1998
5*0Sstevel@tonic-gate * Sleepycat Software. All rights reserved.
6*0Sstevel@tonic-gate */
7*0Sstevel@tonic-gate /*
8*0Sstevel@tonic-gate * Copyright (c) 1990, 1993, 1994, 1995, 1996
9*0Sstevel@tonic-gate * Keith Bostic. All rights reserved.
10*0Sstevel@tonic-gate */
11*0Sstevel@tonic-gate /*
12*0Sstevel@tonic-gate * Copyright (c) 1990, 1993, 1994, 1995
13*0Sstevel@tonic-gate * The Regents of the University of California. All rights reserved.
14*0Sstevel@tonic-gate *
15*0Sstevel@tonic-gate * This code is derived from software contributed to Berkeley by
16*0Sstevel@tonic-gate * Mike Olson.
17*0Sstevel@tonic-gate *
18*0Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without
19*0Sstevel@tonic-gate * modification, are permitted provided that the following conditions
20*0Sstevel@tonic-gate * are met:
21*0Sstevel@tonic-gate * 1. Redistributions of source code must retain the above copyright
22*0Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer.
23*0Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright
24*0Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in the
25*0Sstevel@tonic-gate * documentation and/or other materials provided with the distribution.
26*0Sstevel@tonic-gate * 3. All advertising materials mentioning features or use of this software
27*0Sstevel@tonic-gate * must display the following acknowledgement:
28*0Sstevel@tonic-gate * This product includes software developed by the University of
29*0Sstevel@tonic-gate * California, Berkeley and its contributors.
30*0Sstevel@tonic-gate * 4. Neither the name of the University nor the names of its contributors
31*0Sstevel@tonic-gate * may be used to endorse or promote products derived from this software
32*0Sstevel@tonic-gate * without specific prior written permission.
33*0Sstevel@tonic-gate *
34*0Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
35*0Sstevel@tonic-gate * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
36*0Sstevel@tonic-gate * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
37*0Sstevel@tonic-gate * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
38*0Sstevel@tonic-gate * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
39*0Sstevel@tonic-gate * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
40*0Sstevel@tonic-gate * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
41*0Sstevel@tonic-gate * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
42*0Sstevel@tonic-gate * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
43*0Sstevel@tonic-gate * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
44*0Sstevel@tonic-gate * SUCH DAMAGE.
45*0Sstevel@tonic-gate */
46*0Sstevel@tonic-gate
47*0Sstevel@tonic-gate #include "config.h"
48*0Sstevel@tonic-gate
49*0Sstevel@tonic-gate #ifndef lint
50*0Sstevel@tonic-gate static const char sccsid[] = "@(#)bt_delete.c 10.43 (Sleepycat) 12/7/98";
51*0Sstevel@tonic-gate #endif /* not lint */
52*0Sstevel@tonic-gate
53*0Sstevel@tonic-gate #ifndef NO_SYSTEM_INCLUDES
54*0Sstevel@tonic-gate #include <sys/types.h>
55*0Sstevel@tonic-gate
56*0Sstevel@tonic-gate #include <string.h>
57*0Sstevel@tonic-gate #endif
58*0Sstevel@tonic-gate
59*0Sstevel@tonic-gate #include "db_int.h"
60*0Sstevel@tonic-gate #include "db_page.h"
61*0Sstevel@tonic-gate #include "btree.h"
62*0Sstevel@tonic-gate
63*0Sstevel@tonic-gate /*
64*0Sstevel@tonic-gate * __bam_delete --
65*0Sstevel@tonic-gate * Delete the items referenced by a key.
66*0Sstevel@tonic-gate *
67*0Sstevel@tonic-gate * PUBLIC: int __bam_delete __P((DB *, DB_TXN *, DBT *, u_int32_t));
68*0Sstevel@tonic-gate */
69*0Sstevel@tonic-gate int
__bam_delete(dbp,txn,key,flags)70*0Sstevel@tonic-gate __bam_delete(dbp, txn, key, flags)
71*0Sstevel@tonic-gate DB *dbp;
72*0Sstevel@tonic-gate DB_TXN *txn;
73*0Sstevel@tonic-gate DBT *key;
74*0Sstevel@tonic-gate u_int32_t flags;
75*0Sstevel@tonic-gate {
76*0Sstevel@tonic-gate DBC *dbc;
77*0Sstevel@tonic-gate DBT data;
78*0Sstevel@tonic-gate u_int32_t f_init, f_next;
79*0Sstevel@tonic-gate int ret, t_ret;
80*0Sstevel@tonic-gate
81*0Sstevel@tonic-gate DB_PANIC_CHECK(dbp);
82*0Sstevel@tonic-gate
83*0Sstevel@tonic-gate /* Check for invalid flags. */
84*0Sstevel@tonic-gate if ((ret =
85*0Sstevel@tonic-gate __db_delchk(dbp, key, flags, F_ISSET(dbp, DB_AM_RDONLY))) != 0)
86*0Sstevel@tonic-gate return (ret);
87*0Sstevel@tonic-gate
88*0Sstevel@tonic-gate /* Allocate a cursor. */
89*0Sstevel@tonic-gate if ((ret = dbp->cursor(dbp, txn, &dbc, DB_WRITELOCK)) != 0)
90*0Sstevel@tonic-gate return (ret);
91*0Sstevel@tonic-gate
92*0Sstevel@tonic-gate DEBUG_LWRITE(dbc, txn, "bam_delete", key, NULL, flags);
93*0Sstevel@tonic-gate
94*0Sstevel@tonic-gate /*
95*0Sstevel@tonic-gate * Walk a cursor through the key/data pairs, deleting as we go. Set
96*0Sstevel@tonic-gate * the DB_DBT_USERMEM flag, as this might be a threaded application
97*0Sstevel@tonic-gate * and the flags checking will catch us. We don't actually want the
98*0Sstevel@tonic-gate * keys or data, so request a partial of length 0.
99*0Sstevel@tonic-gate */
100*0Sstevel@tonic-gate memset(&data, 0, sizeof(data));
101*0Sstevel@tonic-gate F_SET(&data, DB_DBT_USERMEM | DB_DBT_PARTIAL);
102*0Sstevel@tonic-gate
103*0Sstevel@tonic-gate /* If locking, set read-modify-write flag. */
104*0Sstevel@tonic-gate f_init = DB_SET;
105*0Sstevel@tonic-gate f_next = DB_NEXT_DUP;
106*0Sstevel@tonic-gate if (dbp->dbenv != NULL && dbp->dbenv->lk_info != NULL) {
107*0Sstevel@tonic-gate f_init |= DB_RMW;
108*0Sstevel@tonic-gate f_next |= DB_RMW;
109*0Sstevel@tonic-gate }
110*0Sstevel@tonic-gate
111*0Sstevel@tonic-gate /* Walk through the set of key/data pairs, deleting as we go. */
112*0Sstevel@tonic-gate if ((ret = dbc->c_get(dbc, key, &data, f_init)) != 0)
113*0Sstevel@tonic-gate goto err;
114*0Sstevel@tonic-gate for (;;) {
115*0Sstevel@tonic-gate if ((ret = dbc->c_del(dbc, 0)) != 0)
116*0Sstevel@tonic-gate goto err;
117*0Sstevel@tonic-gate if ((ret = dbc->c_get(dbc, key, &data, f_next)) != 0) {
118*0Sstevel@tonic-gate if (ret == DB_NOTFOUND) {
119*0Sstevel@tonic-gate ret = 0;
120*0Sstevel@tonic-gate break;
121*0Sstevel@tonic-gate }
122*0Sstevel@tonic-gate goto err;
123*0Sstevel@tonic-gate }
124*0Sstevel@tonic-gate }
125*0Sstevel@tonic-gate
126*0Sstevel@tonic-gate err: /* Discard the cursor. */
127*0Sstevel@tonic-gate if ((t_ret = dbc->c_close(dbc)) != 0 &&
128*0Sstevel@tonic-gate (ret == 0 || ret == DB_NOTFOUND))
129*0Sstevel@tonic-gate ret = t_ret;
130*0Sstevel@tonic-gate
131*0Sstevel@tonic-gate return (ret);
132*0Sstevel@tonic-gate }
133*0Sstevel@tonic-gate
134*0Sstevel@tonic-gate /*
135*0Sstevel@tonic-gate * __bam_ditem --
136*0Sstevel@tonic-gate * Delete one or more entries from a page.
137*0Sstevel@tonic-gate *
138*0Sstevel@tonic-gate * PUBLIC: int __bam_ditem __P((DBC *, PAGE *, u_int32_t));
139*0Sstevel@tonic-gate */
140*0Sstevel@tonic-gate int
__bam_ditem(dbc,h,indx)141*0Sstevel@tonic-gate __bam_ditem(dbc, h, indx)
142*0Sstevel@tonic-gate DBC *dbc;
143*0Sstevel@tonic-gate PAGE *h;
144*0Sstevel@tonic-gate u_int32_t indx;
145*0Sstevel@tonic-gate {
146*0Sstevel@tonic-gate BINTERNAL *bi;
147*0Sstevel@tonic-gate BKEYDATA *bk;
148*0Sstevel@tonic-gate BOVERFLOW *bo;
149*0Sstevel@tonic-gate DB *dbp;
150*0Sstevel@tonic-gate u_int32_t nbytes;
151*0Sstevel@tonic-gate int ret;
152*0Sstevel@tonic-gate
153*0Sstevel@tonic-gate dbp = dbc->dbp;
154*0Sstevel@tonic-gate
155*0Sstevel@tonic-gate switch (TYPE(h)) {
156*0Sstevel@tonic-gate case P_IBTREE:
157*0Sstevel@tonic-gate bi = GET_BINTERNAL(h, indx);
158*0Sstevel@tonic-gate switch (B_TYPE(bi->type)) {
159*0Sstevel@tonic-gate case B_DUPLICATE:
160*0Sstevel@tonic-gate case B_OVERFLOW:
161*0Sstevel@tonic-gate nbytes = BINTERNAL_SIZE(bi->len);
162*0Sstevel@tonic-gate bo = (BOVERFLOW *)bi->data;
163*0Sstevel@tonic-gate goto offpage;
164*0Sstevel@tonic-gate case B_KEYDATA:
165*0Sstevel@tonic-gate nbytes = BINTERNAL_SIZE(bi->len);
166*0Sstevel@tonic-gate break;
167*0Sstevel@tonic-gate default:
168*0Sstevel@tonic-gate return (__db_pgfmt(dbp, h->pgno));
169*0Sstevel@tonic-gate }
170*0Sstevel@tonic-gate break;
171*0Sstevel@tonic-gate case P_IRECNO:
172*0Sstevel@tonic-gate nbytes = RINTERNAL_SIZE;
173*0Sstevel@tonic-gate break;
174*0Sstevel@tonic-gate case P_LBTREE:
175*0Sstevel@tonic-gate /*
176*0Sstevel@tonic-gate * If it's a duplicate key, discard the index and don't touch
177*0Sstevel@tonic-gate * the actual page item.
178*0Sstevel@tonic-gate *
179*0Sstevel@tonic-gate * XXX
180*0Sstevel@tonic-gate * This works because no data item can have an index matching
181*0Sstevel@tonic-gate * any other index so even if the data item is in a key "slot",
182*0Sstevel@tonic-gate * it won't match any other index.
183*0Sstevel@tonic-gate */
184*0Sstevel@tonic-gate if ((indx % 2) == 0) {
185*0Sstevel@tonic-gate /*
186*0Sstevel@tonic-gate * Check for a duplicate after us on the page. NOTE:
187*0Sstevel@tonic-gate * we have to delete the key item before deleting the
188*0Sstevel@tonic-gate * data item, otherwise the "indx + P_INDX" calculation
189*0Sstevel@tonic-gate * won't work!
190*0Sstevel@tonic-gate */
191*0Sstevel@tonic-gate if (indx + P_INDX < (u_int32_t)NUM_ENT(h) &&
192*0Sstevel@tonic-gate h->inp[indx] == h->inp[indx + P_INDX])
193*0Sstevel@tonic-gate return (__bam_adjindx(dbc,
194*0Sstevel@tonic-gate h, indx, indx + O_INDX, 0));
195*0Sstevel@tonic-gate /*
196*0Sstevel@tonic-gate * Check for a duplicate before us on the page. It
197*0Sstevel@tonic-gate * doesn't matter if we delete the key item before or
198*0Sstevel@tonic-gate * after the data item for the purposes of this one.
199*0Sstevel@tonic-gate */
200*0Sstevel@tonic-gate if (indx > 0 && h->inp[indx] == h->inp[indx - P_INDX])
201*0Sstevel@tonic-gate return (__bam_adjindx(dbc,
202*0Sstevel@tonic-gate h, indx, indx - P_INDX, 0));
203*0Sstevel@tonic-gate }
204*0Sstevel@tonic-gate /* FALLTHROUGH */
205*0Sstevel@tonic-gate case P_LRECNO:
206*0Sstevel@tonic-gate bk = GET_BKEYDATA(h, indx);
207*0Sstevel@tonic-gate switch (B_TYPE(bk->type)) {
208*0Sstevel@tonic-gate case B_DUPLICATE:
209*0Sstevel@tonic-gate case B_OVERFLOW:
210*0Sstevel@tonic-gate nbytes = BOVERFLOW_SIZE;
211*0Sstevel@tonic-gate bo = GET_BOVERFLOW(h, indx);
212*0Sstevel@tonic-gate
213*0Sstevel@tonic-gate offpage: /* Delete duplicate/offpage chains. */
214*0Sstevel@tonic-gate if (B_TYPE(bo->type) == B_DUPLICATE) {
215*0Sstevel@tonic-gate if ((ret =
216*0Sstevel@tonic-gate __db_ddup(dbc, bo->pgno, __bam_free)) != 0)
217*0Sstevel@tonic-gate return (ret);
218*0Sstevel@tonic-gate } else
219*0Sstevel@tonic-gate if ((ret =
220*0Sstevel@tonic-gate __db_doff(dbc, bo->pgno, __bam_free)) != 0)
221*0Sstevel@tonic-gate return (ret);
222*0Sstevel@tonic-gate break;
223*0Sstevel@tonic-gate case B_KEYDATA:
224*0Sstevel@tonic-gate nbytes = BKEYDATA_SIZE(bk->len);
225*0Sstevel@tonic-gate break;
226*0Sstevel@tonic-gate default:
227*0Sstevel@tonic-gate return (__db_pgfmt(dbp, h->pgno));
228*0Sstevel@tonic-gate }
229*0Sstevel@tonic-gate break;
230*0Sstevel@tonic-gate default:
231*0Sstevel@tonic-gate return (__db_pgfmt(dbp, h->pgno));
232*0Sstevel@tonic-gate }
233*0Sstevel@tonic-gate
234*0Sstevel@tonic-gate /* Delete the item. */
235*0Sstevel@tonic-gate if ((ret = __db_ditem(dbc, h, indx, nbytes)) != 0)
236*0Sstevel@tonic-gate return (ret);
237*0Sstevel@tonic-gate
238*0Sstevel@tonic-gate /* Mark the page dirty. */
239*0Sstevel@tonic-gate return (memp_fset(dbp->mpf, h, DB_MPOOL_DIRTY));
240*0Sstevel@tonic-gate }
241*0Sstevel@tonic-gate
242*0Sstevel@tonic-gate /*
243*0Sstevel@tonic-gate * __bam_adjindx --
244*0Sstevel@tonic-gate * Adjust an index on the page.
245*0Sstevel@tonic-gate *
246*0Sstevel@tonic-gate * PUBLIC: int __bam_adjindx __P((DBC *, PAGE *, u_int32_t, u_int32_t, int));
247*0Sstevel@tonic-gate */
248*0Sstevel@tonic-gate int
__bam_adjindx(dbc,h,indx,indx_copy,is_insert)249*0Sstevel@tonic-gate __bam_adjindx(dbc, h, indx, indx_copy, is_insert)
250*0Sstevel@tonic-gate DBC *dbc;
251*0Sstevel@tonic-gate PAGE *h;
252*0Sstevel@tonic-gate u_int32_t indx, indx_copy;
253*0Sstevel@tonic-gate int is_insert;
254*0Sstevel@tonic-gate {
255*0Sstevel@tonic-gate DB *dbp;
256*0Sstevel@tonic-gate db_indx_t copy;
257*0Sstevel@tonic-gate int ret;
258*0Sstevel@tonic-gate
259*0Sstevel@tonic-gate dbp = dbc->dbp;
260*0Sstevel@tonic-gate
261*0Sstevel@tonic-gate /* Log the change. */
262*0Sstevel@tonic-gate if (DB_LOGGING(dbc) &&
263*0Sstevel@tonic-gate (ret = __bam_adj_log(dbp->dbenv->lg_info, dbc->txn, &LSN(h),
264*0Sstevel@tonic-gate 0, dbp->log_fileid, PGNO(h), &LSN(h), indx, indx_copy,
265*0Sstevel@tonic-gate (u_int32_t)is_insert)) != 0)
266*0Sstevel@tonic-gate return (ret);
267*0Sstevel@tonic-gate
268*0Sstevel@tonic-gate if (is_insert) {
269*0Sstevel@tonic-gate copy = h->inp[indx_copy];
270*0Sstevel@tonic-gate if (indx != NUM_ENT(h))
271*0Sstevel@tonic-gate memmove(&h->inp[indx + O_INDX], &h->inp[indx],
272*0Sstevel@tonic-gate sizeof(db_indx_t) * (NUM_ENT(h) - indx));
273*0Sstevel@tonic-gate h->inp[indx] = copy;
274*0Sstevel@tonic-gate ++NUM_ENT(h);
275*0Sstevel@tonic-gate } else {
276*0Sstevel@tonic-gate --NUM_ENT(h);
277*0Sstevel@tonic-gate if (indx != NUM_ENT(h))
278*0Sstevel@tonic-gate memmove(&h->inp[indx], &h->inp[indx + O_INDX],
279*0Sstevel@tonic-gate sizeof(db_indx_t) * (NUM_ENT(h) - indx));
280*0Sstevel@tonic-gate }
281*0Sstevel@tonic-gate
282*0Sstevel@tonic-gate /* Mark the page dirty. */
283*0Sstevel@tonic-gate ret = memp_fset(dbp->mpf, h, DB_MPOOL_DIRTY);
284*0Sstevel@tonic-gate
285*0Sstevel@tonic-gate /* Adjust the cursors. */
286*0Sstevel@tonic-gate __bam_ca_di(dbp, h->pgno, indx, is_insert ? 1 : -1);
287*0Sstevel@tonic-gate return (0);
288*0Sstevel@tonic-gate }
289*0Sstevel@tonic-gate
290*0Sstevel@tonic-gate /*
291*0Sstevel@tonic-gate * __bam_dpage --
292*0Sstevel@tonic-gate * Delete a page from the tree.
293*0Sstevel@tonic-gate *
294*0Sstevel@tonic-gate * PUBLIC: int __bam_dpage __P((DBC *, const DBT *));
295*0Sstevel@tonic-gate */
296*0Sstevel@tonic-gate int
__bam_dpage(dbc,key)297*0Sstevel@tonic-gate __bam_dpage(dbc, key)
298*0Sstevel@tonic-gate DBC *dbc;
299*0Sstevel@tonic-gate const DBT *key;
300*0Sstevel@tonic-gate {
301*0Sstevel@tonic-gate CURSOR *cp;
302*0Sstevel@tonic-gate DB *dbp;
303*0Sstevel@tonic-gate DB_LOCK lock;
304*0Sstevel@tonic-gate PAGE *h;
305*0Sstevel@tonic-gate db_pgno_t pgno;
306*0Sstevel@tonic-gate int level; /* !!!: has to hold number of tree levels. */
307*0Sstevel@tonic-gate int exact, ret;
308*0Sstevel@tonic-gate
309*0Sstevel@tonic-gate dbp = dbc->dbp;
310*0Sstevel@tonic-gate cp = dbc->internal;
311*0Sstevel@tonic-gate ret = 0;
312*0Sstevel@tonic-gate
313*0Sstevel@tonic-gate /*
314*0Sstevel@tonic-gate * The locking protocol is that we acquire locks by walking down the
315*0Sstevel@tonic-gate * tree, to avoid the obvious deadlocks.
316*0Sstevel@tonic-gate *
317*0Sstevel@tonic-gate * Call __bam_search to reacquire the empty leaf page, but this time
318*0Sstevel@tonic-gate * get both the leaf page and it's parent, locked. Walk back up the
319*0Sstevel@tonic-gate * tree, until we have the top pair of pages that we want to delete.
320*0Sstevel@tonic-gate * Once we have the top page that we want to delete locked, lock the
321*0Sstevel@tonic-gate * underlying pages and check to make sure they're still empty. If
322*0Sstevel@tonic-gate * they are, delete them.
323*0Sstevel@tonic-gate */
324*0Sstevel@tonic-gate for (level = LEAFLEVEL;; ++level) {
325*0Sstevel@tonic-gate /* Acquire a page and its parent, locked. */
326*0Sstevel@tonic-gate if ((ret =
327*0Sstevel@tonic-gate __bam_search(dbc, key, S_WRPAIR, level, NULL, &exact)) != 0)
328*0Sstevel@tonic-gate return (ret);
329*0Sstevel@tonic-gate
330*0Sstevel@tonic-gate /*
331*0Sstevel@tonic-gate * If we reach the root or the page isn't going to be empty
332*0Sstevel@tonic-gate * when we delete one record, quit.
333*0Sstevel@tonic-gate */
334*0Sstevel@tonic-gate h = cp->csp[-1].page;
335*0Sstevel@tonic-gate if (h->pgno == PGNO_ROOT || NUM_ENT(h) != 1)
336*0Sstevel@tonic-gate break;
337*0Sstevel@tonic-gate
338*0Sstevel@tonic-gate /* Release the two locked pages. */
339*0Sstevel@tonic-gate (void)memp_fput(dbp->mpf, cp->csp[-1].page, 0);
340*0Sstevel@tonic-gate (void)__BT_TLPUT(dbc, cp->csp[-1].lock);
341*0Sstevel@tonic-gate (void)memp_fput(dbp->mpf, cp->csp[0].page, 0);
342*0Sstevel@tonic-gate (void)__BT_TLPUT(dbc, cp->csp[0].lock);
343*0Sstevel@tonic-gate }
344*0Sstevel@tonic-gate
345*0Sstevel@tonic-gate /*
346*0Sstevel@tonic-gate * Leave the stack pointer one after the last entry, we may be about
347*0Sstevel@tonic-gate * to push more items on the stack.
348*0Sstevel@tonic-gate */
349*0Sstevel@tonic-gate ++cp->csp;
350*0Sstevel@tonic-gate
351*0Sstevel@tonic-gate /*
352*0Sstevel@tonic-gate * cp->csp[-2].page is the top page, which we're not going to delete,
353*0Sstevel@tonic-gate * and cp->csp[-1].page is the first page we are going to delete.
354*0Sstevel@tonic-gate *
355*0Sstevel@tonic-gate * Walk down the chain, acquiring the rest of the pages until we've
356*0Sstevel@tonic-gate * retrieved the leaf page. If we find any pages that aren't going
357*0Sstevel@tonic-gate * to be emptied by the delete, someone else added something while we
358*0Sstevel@tonic-gate * were walking the tree, and we discontinue the delete.
359*0Sstevel@tonic-gate */
360*0Sstevel@tonic-gate for (h = cp->csp[-1].page;;) {
361*0Sstevel@tonic-gate if (ISLEAF(h)) {
362*0Sstevel@tonic-gate if (NUM_ENT(h) != 0)
363*0Sstevel@tonic-gate goto release;
364*0Sstevel@tonic-gate break;
365*0Sstevel@tonic-gate } else
366*0Sstevel@tonic-gate if (NUM_ENT(h) != 1)
367*0Sstevel@tonic-gate goto release;
368*0Sstevel@tonic-gate
369*0Sstevel@tonic-gate /*
370*0Sstevel@tonic-gate * Get the next page, write lock it and push it onto the stack.
371*0Sstevel@tonic-gate * We know it's index 0, because it can only have one element.
372*0Sstevel@tonic-gate */
373*0Sstevel@tonic-gate pgno = TYPE(h) == P_IBTREE ?
374*0Sstevel@tonic-gate GET_BINTERNAL(h, 0)->pgno : GET_RINTERNAL(h, 0)->pgno;
375*0Sstevel@tonic-gate
376*0Sstevel@tonic-gate if ((ret = __bam_lget(dbc, 0, pgno, DB_LOCK_WRITE, &lock)) != 0)
377*0Sstevel@tonic-gate goto release;
378*0Sstevel@tonic-gate if ((ret = memp_fget(dbp->mpf, &pgno, 0, &h)) != 0)
379*0Sstevel@tonic-gate goto release;
380*0Sstevel@tonic-gate BT_STK_PUSH(cp, h, 0, lock, ret);
381*0Sstevel@tonic-gate }
382*0Sstevel@tonic-gate
383*0Sstevel@tonic-gate /* Adjust back to reference the last page on the stack. */
384*0Sstevel@tonic-gate BT_STK_POP(cp);
385*0Sstevel@tonic-gate
386*0Sstevel@tonic-gate /* Delete the pages. */
387*0Sstevel@tonic-gate return (__bam_dpages(dbc));
388*0Sstevel@tonic-gate
389*0Sstevel@tonic-gate release:
390*0Sstevel@tonic-gate /* Adjust back to reference the last page on the stack. */
391*0Sstevel@tonic-gate BT_STK_POP(cp);
392*0Sstevel@tonic-gate
393*0Sstevel@tonic-gate /* Discard any locked pages and return. */
394*0Sstevel@tonic-gate __bam_stkrel(dbc, 0);
395*0Sstevel@tonic-gate
396*0Sstevel@tonic-gate return (ret);
397*0Sstevel@tonic-gate }
398*0Sstevel@tonic-gate
399*0Sstevel@tonic-gate /*
400*0Sstevel@tonic-gate * __bam_dpages --
401*0Sstevel@tonic-gate * Delete a set of locked pages.
402*0Sstevel@tonic-gate *
403*0Sstevel@tonic-gate * PUBLIC: int __bam_dpages __P((DBC *));
404*0Sstevel@tonic-gate */
405*0Sstevel@tonic-gate int
__bam_dpages(dbc)406*0Sstevel@tonic-gate __bam_dpages(dbc)
407*0Sstevel@tonic-gate DBC *dbc;
408*0Sstevel@tonic-gate {
409*0Sstevel@tonic-gate CURSOR *cp;
410*0Sstevel@tonic-gate DB *dbp;
411*0Sstevel@tonic-gate DBT a, b;
412*0Sstevel@tonic-gate DB_LOCK c_lock, p_lock;
413*0Sstevel@tonic-gate EPG *epg;
414*0Sstevel@tonic-gate PAGE *child, *parent;
415*0Sstevel@tonic-gate db_indx_t nitems;
416*0Sstevel@tonic-gate db_pgno_t pgno;
417*0Sstevel@tonic-gate db_recno_t rcnt;
418*0Sstevel@tonic-gate int done, ret;
419*0Sstevel@tonic-gate
420*0Sstevel@tonic-gate dbp = dbc->dbp;
421*0Sstevel@tonic-gate cp = dbc->internal;
422*0Sstevel@tonic-gate epg = cp->sp;
423*0Sstevel@tonic-gate
424*0Sstevel@tonic-gate /*
425*0Sstevel@tonic-gate * !!!
426*0Sstevel@tonic-gate * There is an interesting deadlock situation here. We have to relink
427*0Sstevel@tonic-gate * the leaf page chain around the leaf page being deleted. Consider
428*0Sstevel@tonic-gate * a cursor walking through the leaf pages, that has the previous page
429*0Sstevel@tonic-gate * read-locked and is waiting on a lock for the page we're deleting.
430*0Sstevel@tonic-gate * It will deadlock here. This is a problem, because if our process is
431*0Sstevel@tonic-gate * selected to resolve the deadlock, we'll leave an empty leaf page
432*0Sstevel@tonic-gate * that we can never again access by walking down the tree. So, before
433*0Sstevel@tonic-gate * we unlink the subtree, we relink the leaf page chain.
434*0Sstevel@tonic-gate */
435*0Sstevel@tonic-gate if ((ret = __db_relink(dbc, DB_REM_PAGE, cp->csp->page, NULL, 1)) != 0)
436*0Sstevel@tonic-gate goto release;
437*0Sstevel@tonic-gate
438*0Sstevel@tonic-gate /*
439*0Sstevel@tonic-gate * We have the entire stack of deletable pages locked.
440*0Sstevel@tonic-gate *
441*0Sstevel@tonic-gate * Delete the highest page in the tree's reference to the underlying
442*0Sstevel@tonic-gate * stack of pages. Then, release that page, letting the rest of the
443*0Sstevel@tonic-gate * tree get back to business.
444*0Sstevel@tonic-gate */
445*0Sstevel@tonic-gate if ((ret = __bam_ditem(dbc, epg->page, epg->indx)) != 0) {
446*0Sstevel@tonic-gate release: (void)__bam_stkrel(dbc, 0);
447*0Sstevel@tonic-gate return (ret);
448*0Sstevel@tonic-gate }
449*0Sstevel@tonic-gate
450*0Sstevel@tonic-gate pgno = epg->page->pgno;
451*0Sstevel@tonic-gate nitems = NUM_ENT(epg->page);
452*0Sstevel@tonic-gate
453*0Sstevel@tonic-gate (void)memp_fput(dbp->mpf, epg->page, 0);
454*0Sstevel@tonic-gate (void)__BT_TLPUT(dbc, epg->lock);
455*0Sstevel@tonic-gate
456*0Sstevel@tonic-gate /*
457*0Sstevel@tonic-gate * Free the rest of the stack of pages.
458*0Sstevel@tonic-gate *
459*0Sstevel@tonic-gate * !!!
460*0Sstevel@tonic-gate * Don't bother checking for errors. We've unlinked the subtree from
461*0Sstevel@tonic-gate * the tree, and there's no possibility of recovery outside of doing
462*0Sstevel@tonic-gate * TXN rollback.
463*0Sstevel@tonic-gate */
464*0Sstevel@tonic-gate while (++epg <= cp->csp) {
465*0Sstevel@tonic-gate /*
466*0Sstevel@tonic-gate * Delete page entries so they will be restored as part of
467*0Sstevel@tonic-gate * recovery.
468*0Sstevel@tonic-gate */
469*0Sstevel@tonic-gate if (NUM_ENT(epg->page) != 0)
470*0Sstevel@tonic-gate (void)__bam_ditem(dbc, epg->page, epg->indx);
471*0Sstevel@tonic-gate
472*0Sstevel@tonic-gate (void)__bam_free(dbc, epg->page);
473*0Sstevel@tonic-gate (void)__BT_TLPUT(dbc, epg->lock);
474*0Sstevel@tonic-gate }
475*0Sstevel@tonic-gate BT_STK_CLR(cp);
476*0Sstevel@tonic-gate
477*0Sstevel@tonic-gate /*
478*0Sstevel@tonic-gate * Try and collapse the tree a level -- this is only applicable
479*0Sstevel@tonic-gate * if we've deleted the next-to-last element from the root page.
480*0Sstevel@tonic-gate *
481*0Sstevel@tonic-gate * There are two cases when collapsing a tree.
482*0Sstevel@tonic-gate *
483*0Sstevel@tonic-gate * If we've just deleted the last item from the root page, there is no
484*0Sstevel@tonic-gate * further work to be done. The code above has emptied the root page
485*0Sstevel@tonic-gate * and freed all pages below it.
486*0Sstevel@tonic-gate */
487*0Sstevel@tonic-gate if (pgno != PGNO_ROOT || nitems != 1)
488*0Sstevel@tonic-gate return (0);
489*0Sstevel@tonic-gate
490*0Sstevel@tonic-gate /*
491*0Sstevel@tonic-gate * If we just deleted the next-to-last item from the root page, the
492*0Sstevel@tonic-gate * tree can collapse one or more levels. While there remains only a
493*0Sstevel@tonic-gate * single item on the root page, write lock the last page referenced
494*0Sstevel@tonic-gate * by the root page and copy it over the root page. If we can't get a
495*0Sstevel@tonic-gate * write lock, that's okay, the tree just stays deeper than we'd like.
496*0Sstevel@tonic-gate */
497*0Sstevel@tonic-gate for (done = 0; !done;) {
498*0Sstevel@tonic-gate /* Initialize. */
499*0Sstevel@tonic-gate parent = child = NULL;
500*0Sstevel@tonic-gate p_lock = c_lock = LOCK_INVALID;
501*0Sstevel@tonic-gate
502*0Sstevel@tonic-gate /* Lock the root. */
503*0Sstevel@tonic-gate pgno = PGNO_ROOT;
504*0Sstevel@tonic-gate if ((ret =
505*0Sstevel@tonic-gate __bam_lget(dbc, 0, pgno, DB_LOCK_WRITE, &p_lock)) != 0)
506*0Sstevel@tonic-gate goto stop;
507*0Sstevel@tonic-gate if ((ret = memp_fget(dbp->mpf, &pgno, 0, &parent)) != 0)
508*0Sstevel@tonic-gate goto stop;
509*0Sstevel@tonic-gate
510*0Sstevel@tonic-gate if (NUM_ENT(parent) != 1 ||
511*0Sstevel@tonic-gate (TYPE(parent) != P_IBTREE && TYPE(parent) != P_IRECNO))
512*0Sstevel@tonic-gate goto stop;
513*0Sstevel@tonic-gate
514*0Sstevel@tonic-gate pgno = TYPE(parent) == P_IBTREE ?
515*0Sstevel@tonic-gate GET_BINTERNAL(parent, 0)->pgno :
516*0Sstevel@tonic-gate GET_RINTERNAL(parent, 0)->pgno;
517*0Sstevel@tonic-gate
518*0Sstevel@tonic-gate /* Lock the child page. */
519*0Sstevel@tonic-gate if ((ret =
520*0Sstevel@tonic-gate __bam_lget(dbc, 0, pgno, DB_LOCK_WRITE, &c_lock)) != 0)
521*0Sstevel@tonic-gate goto stop;
522*0Sstevel@tonic-gate if ((ret = memp_fget(dbp->mpf, &pgno, 0, &child)) != 0)
523*0Sstevel@tonic-gate goto stop;
524*0Sstevel@tonic-gate
525*0Sstevel@tonic-gate /* Log the change. */
526*0Sstevel@tonic-gate if (DB_LOGGING(dbc)) {
527*0Sstevel@tonic-gate memset(&a, 0, sizeof(a));
528*0Sstevel@tonic-gate a.data = child;
529*0Sstevel@tonic-gate a.size = dbp->pgsize;
530*0Sstevel@tonic-gate memset(&b, 0, sizeof(b));
531*0Sstevel@tonic-gate b.data = P_ENTRY(parent, 0);
532*0Sstevel@tonic-gate b.size = BINTERNAL_SIZE(((BINTERNAL *)b.data)->len);
533*0Sstevel@tonic-gate __bam_rsplit_log(dbp->dbenv->lg_info, dbc->txn,
534*0Sstevel@tonic-gate &child->lsn, 0, dbp->log_fileid, child->pgno, &a,
535*0Sstevel@tonic-gate RE_NREC(parent), &b, &parent->lsn);
536*0Sstevel@tonic-gate }
537*0Sstevel@tonic-gate
538*0Sstevel@tonic-gate /*
539*0Sstevel@tonic-gate * Make the switch.
540*0Sstevel@tonic-gate *
541*0Sstevel@tonic-gate * One fixup -- if the tree has record numbers and we're not
542*0Sstevel@tonic-gate * converting to a leaf page, we have to preserve the total
543*0Sstevel@tonic-gate * record count. Note that we are about to overwrite everything
544*0Sstevel@tonic-gate * on the parent, including its LSN. This is actually OK,
545*0Sstevel@tonic-gate * because the above log message, which describes this update,
546*0Sstevel@tonic-gate * stores its LSN on the child page. When the child is copied
547*0Sstevel@tonic-gate * to the parent, the correct LSN is going to copied into
548*0Sstevel@tonic-gate * place in the parent.
549*0Sstevel@tonic-gate */
550*0Sstevel@tonic-gate COMPQUIET(rcnt, 0);
551*0Sstevel@tonic-gate if (TYPE(child) == P_IRECNO ||
552*0Sstevel@tonic-gate (TYPE(child) == P_IBTREE && F_ISSET(dbp, DB_BT_RECNUM)))
553*0Sstevel@tonic-gate rcnt = RE_NREC(parent);
554*0Sstevel@tonic-gate memcpy(parent, child, dbp->pgsize);
555*0Sstevel@tonic-gate parent->pgno = PGNO_ROOT;
556*0Sstevel@tonic-gate if (TYPE(child) == P_IRECNO ||
557*0Sstevel@tonic-gate (TYPE(child) == P_IBTREE && F_ISSET(dbp, DB_BT_RECNUM)))
558*0Sstevel@tonic-gate RE_NREC_SET(parent, rcnt);
559*0Sstevel@tonic-gate
560*0Sstevel@tonic-gate /* Mark the pages dirty. */
561*0Sstevel@tonic-gate memp_fset(dbp->mpf, parent, DB_MPOOL_DIRTY);
562*0Sstevel@tonic-gate memp_fset(dbp->mpf, child, DB_MPOOL_DIRTY);
563*0Sstevel@tonic-gate
564*0Sstevel@tonic-gate /* Adjust the cursors. */
565*0Sstevel@tonic-gate __bam_ca_rsplit(dbp, child->pgno, PGNO_ROOT);
566*0Sstevel@tonic-gate
567*0Sstevel@tonic-gate /*
568*0Sstevel@tonic-gate * Free the page copied onto the root page and discard its
569*0Sstevel@tonic-gate * lock. (The call to __bam_free() discards our reference
570*0Sstevel@tonic-gate * to the page.)
571*0Sstevel@tonic-gate */
572*0Sstevel@tonic-gate (void)__bam_free(dbc, child);
573*0Sstevel@tonic-gate child = NULL;
574*0Sstevel@tonic-gate
575*0Sstevel@tonic-gate if (0) {
576*0Sstevel@tonic-gate stop: done = 1;
577*0Sstevel@tonic-gate }
578*0Sstevel@tonic-gate if (p_lock != LOCK_INVALID)
579*0Sstevel@tonic-gate (void)__BT_TLPUT(dbc, p_lock);
580*0Sstevel@tonic-gate if (parent != NULL)
581*0Sstevel@tonic-gate memp_fput(dbp->mpf, parent, 0);
582*0Sstevel@tonic-gate if (c_lock != LOCK_INVALID)
583*0Sstevel@tonic-gate (void)__BT_TLPUT(dbc, c_lock);
584*0Sstevel@tonic-gate if (child != NULL)
585*0Sstevel@tonic-gate memp_fput(dbp->mpf, child, 0);
586*0Sstevel@tonic-gate }
587*0Sstevel@tonic-gate
588*0Sstevel@tonic-gate return (0);
589*0Sstevel@tonic-gate }
590