1 /*- 2 * Copyright (c) 1990, 1993, 1994 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 4. Neither the name of the University nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 30 #if defined(LIBC_SCCS) && !defined(lint) 31 static char sccsid[] = "@(#)rec_put.c 8.7 (Berkeley) 8/18/94"; 32 #endif /* LIBC_SCCS and not lint */ 33 #include <sys/cdefs.h> 34 __FBSDID("$FreeBSD$"); 35 36 #include <sys/types.h> 37 38 #include <errno.h> 39 #include <stdio.h> 40 #include <stdlib.h> 41 #include <string.h> 42 43 #include <db.h> 44 #include "recno.h" 45 46 /* 47 * __REC_PUT -- Add a recno item to the tree. 48 * 49 * Parameters: 50 * dbp: pointer to access method 51 * key: key 52 * data: data 53 * flag: R_CURSOR, R_IAFTER, R_IBEFORE, R_NOOVERWRITE 54 * 55 * Returns: 56 * RET_ERROR, RET_SUCCESS and RET_SPECIAL if the key is 57 * already in the tree and R_NOOVERWRITE specified. 58 */ 59 int 60 __rec_put(dbp, key, data, flags) 61 const DB *dbp; 62 DBT *key; 63 const DBT *data; 64 u_int flags; 65 { 66 BTREE *t; 67 DBT fdata, tdata; 68 recno_t nrec; 69 int status; 70 71 t = dbp->internal; 72 73 /* Toss any page pinned across calls. */ 74 if (t->bt_pinned != NULL) { 75 mpool_put(t->bt_mp, t->bt_pinned, 0); 76 t->bt_pinned = NULL; 77 } 78 79 /* 80 * If using fixed-length records, and the record is long, return 81 * EINVAL. If it's short, pad it out. Use the record data return 82 * memory, it's only short-term. 83 */ 84 if (F_ISSET(t, R_FIXLEN) && data->size != t->bt_reclen) { 85 if (data->size > t->bt_reclen) 86 goto einval; 87 88 if (t->bt_rdata.size < t->bt_reclen) { 89 t->bt_rdata.data = 90 reallocf(t->bt_rdata.data, t->bt_reclen); 91 if (t->bt_rdata.data == NULL) 92 return (RET_ERROR); 93 t->bt_rdata.size = t->bt_reclen; 94 } 95 memmove(t->bt_rdata.data, data->data, data->size); 96 memset((char *)t->bt_rdata.data + data->size, 97 t->bt_bval, t->bt_reclen - data->size); 98 fdata.data = t->bt_rdata.data; 99 fdata.size = t->bt_reclen; 100 } else { 101 fdata.data = data->data; 102 fdata.size = data->size; 103 } 104 105 switch (flags) { 106 case R_CURSOR: 107 if (!F_ISSET(&t->bt_cursor, CURS_INIT)) 108 goto einval; 109 nrec = t->bt_cursor.rcursor; 110 break; 111 case R_SETCURSOR: 112 if ((nrec = *(recno_t *)key->data) == 0) 113 goto einval; 114 break; 115 case R_IAFTER: 116 if ((nrec = *(recno_t *)key->data) == 0) { 117 nrec = 1; 118 flags = R_IBEFORE; 119 } 120 break; 121 case 0: 122 case R_IBEFORE: 123 if ((nrec = *(recno_t *)key->data) == 0) 124 goto einval; 125 break; 126 case R_NOOVERWRITE: 127 if ((nrec = *(recno_t *)key->data) == 0) 128 goto einval; 129 if (nrec <= t->bt_nrecs) 130 return (RET_SPECIAL); 131 break; 132 default: 133 einval: errno = EINVAL; 134 return (RET_ERROR); 135 } 136 137 /* 138 * Make sure that records up to and including the put record are 139 * already in the database. If skipping records, create empty ones. 140 */ 141 if (nrec > t->bt_nrecs) { 142 if (!F_ISSET(t, R_EOF | R_INMEM) && 143 t->bt_irec(t, nrec) == RET_ERROR) 144 return (RET_ERROR); 145 if (nrec > t->bt_nrecs + 1) { 146 if (F_ISSET(t, R_FIXLEN)) { 147 if ((tdata.data = 148 (void *)malloc(t->bt_reclen)) == NULL) 149 return (RET_ERROR); 150 tdata.size = t->bt_reclen; 151 memset(tdata.data, t->bt_bval, tdata.size); 152 } else { 153 tdata.data = NULL; 154 tdata.size = 0; 155 } 156 while (nrec > t->bt_nrecs + 1) 157 if (__rec_iput(t, 158 t->bt_nrecs, &tdata, 0) != RET_SUCCESS) 159 return (RET_ERROR); 160 if (F_ISSET(t, R_FIXLEN)) 161 free(tdata.data); 162 } 163 } 164 165 if ((status = __rec_iput(t, nrec - 1, &fdata, flags)) != RET_SUCCESS) 166 return (status); 167 168 switch (flags) { 169 case R_IAFTER: 170 nrec++; 171 break; 172 case R_SETCURSOR: 173 t->bt_cursor.rcursor = nrec; 174 break; 175 } 176 177 F_SET(t, R_MODIFIED); 178 return (__rec_ret(t, NULL, nrec, key, NULL)); 179 } 180 181 /* 182 * __REC_IPUT -- Add a recno item to the tree. 183 * 184 * Parameters: 185 * t: tree 186 * nrec: record number 187 * data: data 188 * 189 * Returns: 190 * RET_ERROR, RET_SUCCESS 191 */ 192 int 193 __rec_iput(t, nrec, data, flags) 194 BTREE *t; 195 recno_t nrec; 196 const DBT *data; 197 u_int flags; 198 { 199 DBT tdata; 200 EPG *e; 201 PAGE *h; 202 indx_t index, nxtindex; 203 pgno_t pg; 204 u_int32_t nbytes; 205 int dflags, status; 206 char *dest, db[NOVFLSIZE]; 207 208 /* 209 * If the data won't fit on a page, store it on indirect pages. 210 * 211 * XXX 212 * If the insert fails later on, these pages aren't recovered. 213 */ 214 if (data->size > t->bt_ovflsize) { 215 if (__ovfl_put(t, data, &pg) == RET_ERROR) 216 return (RET_ERROR); 217 tdata.data = db; 218 tdata.size = NOVFLSIZE; 219 *(pgno_t *)db = pg; 220 *(u_int32_t *)(db + sizeof(pgno_t)) = data->size; 221 dflags = P_BIGDATA; 222 data = &tdata; 223 } else 224 dflags = 0; 225 226 /* __rec_search pins the returned page. */ 227 if ((e = __rec_search(t, nrec, 228 nrec > t->bt_nrecs || flags == R_IAFTER || flags == R_IBEFORE ? 229 SINSERT : SEARCH)) == NULL) 230 return (RET_ERROR); 231 232 h = e->page; 233 index = e->index; 234 235 /* 236 * Add the specified key/data pair to the tree. The R_IAFTER and 237 * R_IBEFORE flags insert the key after/before the specified key. 238 * 239 * Pages are split as required. 240 */ 241 switch (flags) { 242 case R_IAFTER: 243 ++index; 244 break; 245 case R_IBEFORE: 246 break; 247 default: 248 if (nrec < t->bt_nrecs && 249 __rec_dleaf(t, h, index) == RET_ERROR) { 250 mpool_put(t->bt_mp, h, 0); 251 return (RET_ERROR); 252 } 253 break; 254 } 255 256 /* 257 * If not enough room, split the page. The split code will insert 258 * the key and data and unpin the current page. If inserting into 259 * the offset array, shift the pointers up. 260 */ 261 nbytes = NRLEAFDBT(data->size); 262 if (h->upper - h->lower < nbytes + sizeof(indx_t)) { 263 status = __bt_split(t, h, NULL, data, dflags, nbytes, index); 264 if (status == RET_SUCCESS) 265 ++t->bt_nrecs; 266 return (status); 267 } 268 269 if (index < (nxtindex = NEXTINDEX(h))) 270 memmove(h->linp + index + 1, h->linp + index, 271 (nxtindex - index) * sizeof(indx_t)); 272 h->lower += sizeof(indx_t); 273 274 h->linp[index] = h->upper -= nbytes; 275 dest = (char *)h + h->upper; 276 WR_RLEAF(dest, data, dflags); 277 278 ++t->bt_nrecs; 279 F_SET(t, B_MODIFIED); 280 mpool_put(t->bt_mp, h, MPOOL_DIRTY); 281 282 return (RET_SUCCESS); 283 } 284