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