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