bt_delete.c revision 1.3 1 1.1 cgd /*-
2 1.1 cgd * Copyright (c) 1990, 1993
3 1.1 cgd * The Regents of the University of California. All rights reserved.
4 1.1 cgd *
5 1.1 cgd * This code is derived from software contributed to Berkeley by
6 1.1 cgd * Mike Olson.
7 1.1 cgd *
8 1.1 cgd * Redistribution and use in source and binary forms, with or without
9 1.1 cgd * modification, are permitted provided that the following conditions
10 1.1 cgd * are met:
11 1.1 cgd * 1. Redistributions of source code must retain the above copyright
12 1.1 cgd * notice, this list of conditions and the following disclaimer.
13 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 cgd * notice, this list of conditions and the following disclaimer in the
15 1.1 cgd * documentation and/or other materials provided with the distribution.
16 1.1 cgd * 3. All advertising materials mentioning features or use of this software
17 1.1 cgd * must display the following acknowledgement:
18 1.1 cgd * This product includes software developed by the University of
19 1.1 cgd * California, Berkeley and its contributors.
20 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
21 1.1 cgd * may be used to endorse or promote products derived from this software
22 1.1 cgd * without specific prior written permission.
23 1.1 cgd *
24 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 1.1 cgd * SUCH DAMAGE.
35 1.1 cgd */
36 1.1 cgd
37 1.1 cgd #if defined(LIBC_SCCS) && !defined(lint)
38 1.3 jtc /*static char *sccsid = "from: @(#)bt_delete.c 8.1 (Berkeley) 6/4/93";*/
39 1.3 jtc static char *rcsid = "$Id: bt_delete.c,v 1.3 1993/08/26 00:43:19 jtc Exp $";
40 1.1 cgd #endif /* LIBC_SCCS and not lint */
41 1.1 cgd
42 1.1 cgd #include <sys/types.h>
43 1.1 cgd
44 1.1 cgd #include <errno.h>
45 1.1 cgd #include <stdio.h>
46 1.1 cgd #include <string.h>
47 1.1 cgd
48 1.1 cgd #include <db.h>
49 1.1 cgd #include "btree.h"
50 1.1 cgd
51 1.1 cgd static int bt_bdelete __P((BTREE *, const DBT *));
52 1.1 cgd
53 1.1 cgd /*
54 1.1 cgd * __BT_DELETE -- Delete the item(s) referenced by a key.
55 1.1 cgd *
56 1.1 cgd * Parameters:
57 1.1 cgd * dbp: pointer to access method
58 1.1 cgd * key: key to delete
59 1.1 cgd * flags: R_CURSOR if deleting what the cursor references
60 1.1 cgd *
61 1.1 cgd * Returns:
62 1.1 cgd * RET_ERROR, RET_SUCCESS and RET_SPECIAL if the key not found.
63 1.1 cgd */
64 1.1 cgd int
65 1.1 cgd __bt_delete(dbp, key, flags)
66 1.1 cgd const DB *dbp;
67 1.1 cgd const DBT *key;
68 1.1 cgd u_int flags;
69 1.1 cgd {
70 1.1 cgd BTREE *t;
71 1.1 cgd int status;
72 1.1 cgd
73 1.1 cgd t = dbp->internal;
74 1.1 cgd if (ISSET(t, B_RDONLY)) {
75 1.1 cgd errno = EPERM;
76 1.1 cgd return (RET_ERROR);
77 1.1 cgd }
78 1.1 cgd switch(flags) {
79 1.1 cgd case 0:
80 1.1 cgd status = bt_bdelete(t, key);
81 1.1 cgd break;
82 1.1 cgd case R_CURSOR:
83 1.1 cgd /*
84 1.1 cgd * If flags is R_CURSOR, delete the cursor; must already have
85 1.1 cgd * started a scan and not have already deleted the record. For
86 1.1 cgd * the delete cursor bit to have been set requires that the
87 1.1 cgd * scan be initialized, so no reason to check.
88 1.1 cgd */
89 1.1 cgd if (!ISSET(t, B_SEQINIT))
90 1.1 cgd goto einval;
91 1.1 cgd status = ISSET(t, B_DELCRSR) ?
92 1.1 cgd RET_SPECIAL : __bt_crsrdel(t, &t->bt_bcursor);
93 1.1 cgd break;
94 1.1 cgd default:
95 1.1 cgd einval: errno = EINVAL;
96 1.1 cgd return (RET_ERROR);
97 1.1 cgd }
98 1.1 cgd if (status == RET_SUCCESS)
99 1.1 cgd SET(t, B_MODIFIED);
100 1.1 cgd return (status);
101 1.1 cgd }
102 1.1 cgd
103 1.1 cgd /*
104 1.1 cgd * BT_BDELETE -- Delete all key/data pairs matching the specified key.
105 1.1 cgd *
106 1.1 cgd * Parameters:
107 1.1 cgd * tree: tree
108 1.1 cgd * key: key to delete
109 1.1 cgd *
110 1.1 cgd * Returns:
111 1.1 cgd * RET_ERROR, RET_SUCCESS and RET_SPECIAL if the key not found.
112 1.1 cgd */
113 1.1 cgd static int
114 1.1 cgd bt_bdelete(t, key)
115 1.1 cgd BTREE *t;
116 1.1 cgd const DBT *key;
117 1.1 cgd {
118 1.1 cgd EPG *e, save;
119 1.1 cgd PAGE *h;
120 1.1 cgd pgno_t cpgno, pg;
121 1.1 cgd indx_t cindex;
122 1.1 cgd int deleted, dirty1, dirty2, exact;
123 1.1 cgd
124 1.1 cgd /* Find any matching record; __bt_search pins the page. */
125 1.1 cgd if ((e = __bt_search(t, key, &exact)) == NULL)
126 1.1 cgd return (RET_ERROR);
127 1.1 cgd if (!exact) {
128 1.1 cgd mpool_put(t->bt_mp, e->page, 0);
129 1.1 cgd return (RET_SPECIAL);
130 1.1 cgd }
131 1.1 cgd
132 1.1 cgd /*
133 1.1 cgd * Delete forward, then delete backward, from the found key. The
134 1.1 cgd * ordering is so that the deletions don't mess up the page refs.
135 1.1 cgd * The first loop deletes the key from the original page, the second
136 1.1 cgd * unpins the original page. In the first loop, dirty1 is set if
137 1.1 cgd * the original page is modified, and dirty2 is set if any subsequent
138 1.1 cgd * pages are modified. In the second loop, dirty1 starts off set if
139 1.1 cgd * the original page has been modified, and is set if any subsequent
140 1.1 cgd * pages are modified.
141 1.1 cgd *
142 1.1 cgd * If find the key referenced by the cursor, don't delete it, just
143 1.1 cgd * flag it for future deletion. The cursor page number is P_INVALID
144 1.1 cgd * unless the sequential scan is initialized, so no reason to check.
145 1.1 cgd * A special case is when the already deleted cursor record was the
146 1.1 cgd * only record found. If so, then the delete opertion fails as no
147 1.1 cgd * records were deleted.
148 1.1 cgd *
149 1.1 cgd * Cycle in place in the current page until the current record doesn't
150 1.1 cgd * match the key or the page is empty. If the latter, walk forward,
151 1.1 cgd * skipping empty pages and repeating until a record doesn't match
152 1.1 cgd * the key or the end of the tree is reached.
153 1.1 cgd */
154 1.1 cgd cpgno = t->bt_bcursor.pgno;
155 1.1 cgd cindex = t->bt_bcursor.index;
156 1.1 cgd save = *e;
157 1.1 cgd dirty1 = 0;
158 1.1 cgd for (h = e->page, deleted = 0;;) {
159 1.1 cgd dirty2 = 0;
160 1.1 cgd do {
161 1.1 cgd if (h->pgno == cpgno && e->index == cindex) {
162 1.1 cgd if (!ISSET(t, B_DELCRSR)) {
163 1.1 cgd SET(t, B_DELCRSR);
164 1.1 cgd deleted = 1;
165 1.1 cgd }
166 1.1 cgd ++e->index;
167 1.1 cgd } else {
168 1.1 cgd if (__bt_dleaf(t, h, e->index)) {
169 1.1 cgd if (h->pgno != save.page->pgno)
170 1.1 cgd mpool_put(t->bt_mp, h, dirty2);
171 1.1 cgd mpool_put(t->bt_mp, save.page, dirty1);
172 1.1 cgd return (RET_ERROR);
173 1.1 cgd }
174 1.1 cgd if (h->pgno == save.page->pgno)
175 1.1 cgd dirty1 = MPOOL_DIRTY;
176 1.1 cgd else
177 1.1 cgd dirty2 = MPOOL_DIRTY;
178 1.1 cgd deleted = 1;
179 1.1 cgd }
180 1.1 cgd } while (e->index < NEXTINDEX(h) && __bt_cmp(t, key, e) == 0);
181 1.1 cgd
182 1.1 cgd /*
183 1.1 cgd * Quit if didn't find a match, no next page, or first key on
184 1.1 cgd * the next page doesn't match. Don't unpin the original page
185 1.1 cgd * unless an error occurs.
186 1.1 cgd */
187 1.1 cgd if (e->index < NEXTINDEX(h))
188 1.1 cgd break;
189 1.1 cgd for (;;) {
190 1.1 cgd if ((pg = h->nextpg) == P_INVALID)
191 1.1 cgd goto done1;
192 1.1 cgd if (h->pgno != save.page->pgno)
193 1.1 cgd mpool_put(t->bt_mp, h, dirty2);
194 1.1 cgd if ((h = mpool_get(t->bt_mp, pg, 0)) == NULL) {
195 1.1 cgd mpool_put(t->bt_mp, save.page, dirty1);
196 1.1 cgd return (RET_ERROR);
197 1.1 cgd }
198 1.1 cgd if (NEXTINDEX(h) != 0) {
199 1.1 cgd e->page = h;
200 1.1 cgd e->index = 0;
201 1.1 cgd break;
202 1.1 cgd }
203 1.1 cgd }
204 1.1 cgd
205 1.1 cgd if (__bt_cmp(t, key, e) != 0)
206 1.1 cgd break;
207 1.1 cgd }
208 1.1 cgd
209 1.1 cgd /*
210 1.1 cgd * Reach here with the original page and the last page referenced
211 1.1 cgd * pinned (they may be the same). Release it if not the original.
212 1.1 cgd */
213 1.1 cgd done1: if (h->pgno != save.page->pgno)
214 1.1 cgd mpool_put(t->bt_mp, h, dirty2);
215 1.1 cgd
216 1.1 cgd /*
217 1.1 cgd * Walk backwards from the record previous to the record returned by
218 1.1 cgd * __bt_search, skipping empty pages, until a record doesn't match
219 1.1 cgd * the key or reach the beginning of the tree.
220 1.1 cgd */
221 1.1 cgd *e = save;
222 1.1 cgd for (;;) {
223 1.1 cgd if (e->index)
224 1.1 cgd --e->index;
225 1.1 cgd for (h = e->page; e->index; --e->index) {
226 1.1 cgd if (__bt_cmp(t, key, e) != 0)
227 1.1 cgd goto done2;
228 1.1 cgd if (h->pgno == cpgno && e->index == cindex) {
229 1.1 cgd if (!ISSET(t, B_DELCRSR)) {
230 1.1 cgd SET(t, B_DELCRSR);
231 1.1 cgd deleted = 1;
232 1.1 cgd }
233 1.1 cgd } else {
234 1.1 cgd if (__bt_dleaf(t, h, e->index) == RET_ERROR) {
235 1.1 cgd mpool_put(t->bt_mp, h, dirty1);
236 1.1 cgd return (RET_ERROR);
237 1.1 cgd }
238 1.1 cgd if (h->pgno == save.page->pgno)
239 1.1 cgd dirty1 = MPOOL_DIRTY;
240 1.1 cgd deleted = 1;
241 1.1 cgd }
242 1.1 cgd }
243 1.1 cgd
244 1.1 cgd if ((pg = h->prevpg) == P_INVALID)
245 1.1 cgd goto done2;
246 1.1 cgd mpool_put(t->bt_mp, h, dirty1);
247 1.1 cgd dirty1 = 0;
248 1.1 cgd if ((e->page = mpool_get(t->bt_mp, pg, 0)) == NULL)
249 1.1 cgd return (RET_ERROR);
250 1.1 cgd e->index = NEXTINDEX(e->page);
251 1.1 cgd }
252 1.1 cgd
253 1.1 cgd /*
254 1.1 cgd * Reach here with the last page that was looked at pinned. Release
255 1.1 cgd * it.
256 1.1 cgd */
257 1.1 cgd done2: mpool_put(t->bt_mp, h, dirty1);
258 1.1 cgd return (deleted ? RET_SUCCESS : RET_SPECIAL);
259 1.1 cgd }
260 1.1 cgd
261 1.1 cgd /*
262 1.1 cgd * __BT_DLEAF -- Delete a single record from a leaf page.
263 1.1 cgd *
264 1.1 cgd * Parameters:
265 1.1 cgd * t: tree
266 1.1 cgd * index: index on current page to delete
267 1.1 cgd *
268 1.1 cgd * Returns:
269 1.1 cgd * RET_SUCCESS, RET_ERROR.
270 1.1 cgd */
271 1.1 cgd int
272 1.1 cgd __bt_dleaf(t, h, index)
273 1.1 cgd BTREE *t;
274 1.1 cgd PAGE *h;
275 1.1 cgd int index;
276 1.1 cgd {
277 1.1 cgd register BLEAF *bl;
278 1.1 cgd register indx_t *ip, offset;
279 1.1 cgd register size_t nbytes;
280 1.1 cgd register int cnt;
281 1.1 cgd char *from;
282 1.1 cgd void *to;
283 1.1 cgd
284 1.1 cgd /*
285 1.1 cgd * Delete a record from a btree leaf page. Internal records are never
286 1.1 cgd * deleted from internal pages, regardless of the records that caused
287 1.1 cgd * them to be added being deleted. Pages made empty by deletion are
288 1.1 cgd * not reclaimed. They are, however, made available for reuse.
289 1.1 cgd *
290 1.1 cgd * Pack the remaining entries at the end of the page, shift the indices
291 1.1 cgd * down, overwriting the deleted record and its index. If the record
292 1.1 cgd * uses overflow pages, make them available for reuse.
293 1.1 cgd */
294 1.1 cgd to = bl = GETBLEAF(h, index);
295 1.1 cgd if (bl->flags & P_BIGKEY && __ovfl_delete(t, bl->bytes) == RET_ERROR)
296 1.1 cgd return (RET_ERROR);
297 1.1 cgd if (bl->flags & P_BIGDATA &&
298 1.1 cgd __ovfl_delete(t, bl->bytes + bl->ksize) == RET_ERROR)
299 1.1 cgd return (RET_ERROR);
300 1.1 cgd nbytes = NBLEAF(bl);
301 1.1 cgd
302 1.1 cgd /*
303 1.1 cgd * Compress the key/data pairs. Compress and adjust the [BR]LEAF
304 1.1 cgd * offsets. Reset the headers.
305 1.1 cgd */
306 1.1 cgd from = (char *)h + h->upper;
307 1.1 cgd memmove(from + nbytes, from, (char *)to - from);
308 1.1 cgd h->upper += nbytes;
309 1.1 cgd
310 1.1 cgd offset = h->linp[index];
311 1.1 cgd for (cnt = index, ip = &h->linp[0]; cnt--; ++ip)
312 1.1 cgd if (ip[0] < offset)
313 1.1 cgd ip[0] += nbytes;
314 1.1 cgd for (cnt = NEXTINDEX(h) - index; --cnt; ++ip)
315 1.1 cgd ip[0] = ip[1] < offset ? ip[1] + nbytes : ip[1];
316 1.1 cgd h->lower -= sizeof(indx_t);
317 1.1 cgd return (RET_SUCCESS);
318 1.1 cgd }
319