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