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