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