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