rec_delete.c revision 1.9 1 /* $NetBSD: rec_delete.c,v 1.9 1997/07/13 18:52:11 christos Exp $ */
2
3 /*-
4 * Copyright (c) 1990, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Mike Olson.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 */
38
39 #include <sys/cdefs.h>
40 #if defined(LIBC_SCCS) && !defined(lint)
41 #if 0
42 static char sccsid[] = "@(#)rec_delete.c 8.7 (Berkeley) 7/14/94";
43 #else
44 __RCSID("$NetBSD: rec_delete.c,v 1.9 1997/07/13 18:52:11 christos Exp $");
45 #endif
46 #endif /* LIBC_SCCS and not lint */
47
48 #include <sys/types.h>
49
50 #include <errno.h>
51 #include <stdio.h>
52 #include <string.h>
53
54 #include <db.h>
55 #include "recno.h"
56
57 static int rec_rdelete __P((BTREE *, recno_t));
58
59 /*
60 * __REC_DELETE -- Delete the item(s) referenced by a key.
61 *
62 * Parameters:
63 * dbp: pointer to access method
64 * key: key to delete
65 * flags: R_CURSOR if deleting what the cursor references
66 *
67 * Returns:
68 * RET_ERROR, RET_SUCCESS and RET_SPECIAL if the key not found.
69 */
70 int
71 __rec_delete(dbp, key, flags)
72 const DB *dbp;
73 const DBT *key;
74 u_int flags;
75 {
76 BTREE *t;
77 recno_t nrec;
78 int status;
79
80 t = dbp->internal;
81
82 /* Toss any page pinned across calls. */
83 if (t->bt_pinned != NULL) {
84 mpool_put(t->bt_mp, t->bt_pinned, 0);
85 t->bt_pinned = NULL;
86 }
87
88 switch(flags) {
89 case 0:
90 if ((nrec = *(recno_t *)key->data) == 0)
91 goto einval;
92 if (nrec > t->bt_nrecs)
93 return (RET_SPECIAL);
94 --nrec;
95 status = rec_rdelete(t, nrec);
96 break;
97 case R_CURSOR:
98 if (!F_ISSET(&t->bt_cursor, CURS_INIT))
99 goto einval;
100 if (t->bt_nrecs == 0)
101 return (RET_SPECIAL);
102 status = rec_rdelete(t, t->bt_cursor.rcursor - 1);
103 if (status == RET_SUCCESS)
104 --t->bt_cursor.rcursor;
105 break;
106 default:
107 einval: errno = EINVAL;
108 return (RET_ERROR);
109 }
110
111 if (status == RET_SUCCESS)
112 F_SET(t, B_MODIFIED | R_MODIFIED);
113 return (status);
114 }
115
116 /*
117 * REC_RDELETE -- Delete the data matching the specified key.
118 *
119 * Parameters:
120 * tree: tree
121 * nrec: record to delete
122 *
123 * Returns:
124 * RET_ERROR, RET_SUCCESS and RET_SPECIAL if the key not found.
125 */
126 static int
127 rec_rdelete(t, nrec)
128 BTREE *t;
129 recno_t nrec;
130 {
131 EPG *e;
132 PAGE *h;
133 int status;
134
135 /* Find the record; __rec_search pins the page. */
136 if ((e = __rec_search(t, nrec, SDELETE)) == NULL)
137 return (RET_ERROR);
138
139 /* Delete the record. */
140 h = e->page;
141 status = __rec_dleaf(t, h, e->index);
142 if (status != RET_SUCCESS) {
143 mpool_put(t->bt_mp, h, 0);
144 return (status);
145 }
146 mpool_put(t->bt_mp, h, MPOOL_DIRTY);
147 return (RET_SUCCESS);
148 }
149
150 /*
151 * __REC_DLEAF -- Delete a single record from a recno leaf page.
152 *
153 * Parameters:
154 * t: tree
155 * index: index on current page to delete
156 *
157 * Returns:
158 * RET_SUCCESS, RET_ERROR.
159 */
160 int
161 __rec_dleaf(t, h, index)
162 BTREE *t;
163 PAGE *h;
164 u_int32_t index;
165 {
166 RLEAF *rl;
167 indx_t *ip, cnt, offset;
168 u_int32_t nbytes;
169 char *from;
170 void *to;
171
172 /*
173 * Delete a record from a recno leaf page. Internal records are never
174 * deleted from internal pages, regardless of the records that caused
175 * them to be added being deleted. Pages made empty by deletion are
176 * not reclaimed. They are, however, made available for reuse.
177 *
178 * Pack the remaining entries at the end of the page, shift the indices
179 * down, overwriting the deleted record and its index. If the record
180 * uses overflow pages, make them available for reuse.
181 */
182 to = rl = GETRLEAF(h, index);
183 if (rl->flags & P_BIGDATA && __ovfl_delete(t, rl->bytes) == RET_ERROR)
184 return (RET_ERROR);
185 nbytes = NRLEAF(rl);
186
187 /*
188 * Compress the key/data pairs. Compress and adjust the [BR]LEAF
189 * offsets. Reset the headers.
190 */
191 from = (char *)h + h->upper;
192 memmove(from + nbytes, from, (char *)to - from);
193 h->upper += nbytes;
194
195 offset = h->linp[index];
196 for (cnt = &h->linp[index] - (ip = &h->linp[0]); cnt--; ++ip)
197 if (ip[0] < offset)
198 ip[0] += nbytes;
199 for (cnt = &h->linp[NEXTINDEX(h)] - ip; --cnt; ++ip)
200 ip[0] = ip[1] < offset ? ip[1] + nbytes : ip[1];
201 h->lower -= sizeof(indx_t);
202 --t->bt_nrecs;
203 return (RET_SUCCESS);
204 }
205