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