rec_delete.c revision 1.10 1 /* $NetBSD: rec_delete.c,v 1.10 1997/07/21 14:06:43 jtc 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.10 1997/07/21 14:06:43 jtc Exp $");
45 #endif
46 #endif /* LIBC_SCCS and not lint */
47
48 #include "namespace.h"
49 #include <sys/types.h>
50
51 #include <errno.h>
52 #include <stdio.h>
53 #include <string.h>
54
55 #include <db.h>
56 #include "recno.h"
57
58 static int rec_rdelete __P((BTREE *, recno_t));
59
60 /*
61 * __REC_DELETE -- Delete the item(s) referenced by a key.
62 *
63 * Parameters:
64 * dbp: pointer to access method
65 * key: key to delete
66 * flags: R_CURSOR if deleting what the cursor references
67 *
68 * Returns:
69 * RET_ERROR, RET_SUCCESS and RET_SPECIAL if the key not found.
70 */
71 int
72 __rec_delete(dbp, key, flags)
73 const DB *dbp;
74 const DBT *key;
75 u_int flags;
76 {
77 BTREE *t;
78 recno_t nrec;
79 int status;
80
81 t = dbp->internal;
82
83 /* Toss any page pinned across calls. */
84 if (t->bt_pinned != NULL) {
85 mpool_put(t->bt_mp, t->bt_pinned, 0);
86 t->bt_pinned = NULL;
87 }
88
89 switch(flags) {
90 case 0:
91 if ((nrec = *(recno_t *)key->data) == 0)
92 goto einval;
93 if (nrec > t->bt_nrecs)
94 return (RET_SPECIAL);
95 --nrec;
96 status = rec_rdelete(t, nrec);
97 break;
98 case R_CURSOR:
99 if (!F_ISSET(&t->bt_cursor, CURS_INIT))
100 goto einval;
101 if (t->bt_nrecs == 0)
102 return (RET_SPECIAL);
103 status = rec_rdelete(t, t->bt_cursor.rcursor - 1);
104 if (status == RET_SUCCESS)
105 --t->bt_cursor.rcursor;
106 break;
107 default:
108 einval: errno = EINVAL;
109 return (RET_ERROR);
110 }
111
112 if (status == RET_SUCCESS)
113 F_SET(t, B_MODIFIED | R_MODIFIED);
114 return (status);
115 }
116
117 /*
118 * REC_RDELETE -- Delete the data matching the specified key.
119 *
120 * Parameters:
121 * tree: tree
122 * nrec: record to delete
123 *
124 * Returns:
125 * RET_ERROR, RET_SUCCESS and RET_SPECIAL if the key not found.
126 */
127 static int
128 rec_rdelete(t, nrec)
129 BTREE *t;
130 recno_t nrec;
131 {
132 EPG *e;
133 PAGE *h;
134 int status;
135
136 /* Find the record; __rec_search pins the page. */
137 if ((e = __rec_search(t, nrec, SDELETE)) == NULL)
138 return (RET_ERROR);
139
140 /* Delete the record. */
141 h = e->page;
142 status = __rec_dleaf(t, h, e->index);
143 if (status != RET_SUCCESS) {
144 mpool_put(t->bt_mp, h, 0);
145 return (status);
146 }
147 mpool_put(t->bt_mp, h, MPOOL_DIRTY);
148 return (RET_SUCCESS);
149 }
150
151 /*
152 * __REC_DLEAF -- Delete a single record from a recno leaf page.
153 *
154 * Parameters:
155 * t: tree
156 * index: index on current page to delete
157 *
158 * Returns:
159 * RET_SUCCESS, RET_ERROR.
160 */
161 int
162 __rec_dleaf(t, h, index)
163 BTREE *t;
164 PAGE *h;
165 u_int32_t index;
166 {
167 RLEAF *rl;
168 indx_t *ip, cnt, offset;
169 u_int32_t nbytes;
170 char *from;
171 void *to;
172
173 /*
174 * Delete a record from a recno leaf page. Internal records are never
175 * deleted from internal pages, regardless of the records that caused
176 * them to be added being deleted. Pages made empty by deletion are
177 * not reclaimed. They are, however, made available for reuse.
178 *
179 * Pack the remaining entries at the end of the page, shift the indices
180 * down, overwriting the deleted record and its index. If the record
181 * uses overflow pages, make them available for reuse.
182 */
183 to = rl = GETRLEAF(h, index);
184 if (rl->flags & P_BIGDATA && __ovfl_delete(t, rl->bytes) == RET_ERROR)
185 return (RET_ERROR);
186 nbytes = NRLEAF(rl);
187
188 /*
189 * Compress the key/data pairs. Compress and adjust the [BR]LEAF
190 * offsets. Reset the headers.
191 */
192 from = (char *)h + h->upper;
193 memmove(from + nbytes, from, (char *)to - from);
194 h->upper += nbytes;
195
196 offset = h->linp[index];
197 for (cnt = &h->linp[index] - (ip = &h->linp[0]); cnt--; ++ip)
198 if (ip[0] < offset)
199 ip[0] += nbytes;
200 for (cnt = &h->linp[NEXTINDEX(h)] - ip; --cnt; ++ip)
201 ip[0] = ip[1] < offset ? ip[1] + nbytes : ip[1];
202 h->lower -= sizeof(indx_t);
203 --t->bt_nrecs;
204 return (RET_SUCCESS);
205 }
206