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.4 1993/09/09 02:42:26 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 tdata.data = NULL; 124 tdata.size = 0; 125 while (nrec > t->bt_nrecs + 1) 126 if (__rec_iput(t, 127 t->bt_nrecs, &tdata, 0) != RET_SUCCESS) 128 return (RET_ERROR); 129 } 130 } 131 132 if ((status = __rec_iput(t, nrec - 1, data, flags)) != RET_SUCCESS) 133 return (status); 134 135 if (flags == R_SETCURSOR) 136 t->bt_rcursor = nrec; 137 138 SET(t, R_MODIFIED); 139 return (__rec_ret(t, NULL, nrec, key, NULL)); 140 } 141 142 /* 143 * __REC_IPUT -- Add a recno item to the tree. 144 * 145 * Parameters: 146 * t: tree 147 * nrec: record number 148 * data: data 149 * 150 * Returns: 151 * RET_ERROR, RET_SUCCESS 152 */ 153 int 154 __rec_iput(t, nrec, data, flags) 155 BTREE *t; 156 recno_t nrec; 157 const DBT *data; 158 u_int flags; 159 { 160 DBT tdata; 161 EPG *e; 162 PAGE *h; 163 indx_t index, nxtindex; 164 pgno_t pg; 165 size_t nbytes; 166 int dflags, status; 167 char *dest, db[NOVFLSIZE]; 168 169 /* 170 * If the data won't fit on a page, store it on indirect pages. 171 * 172 * XXX 173 * If the insert fails later on, these pages aren't recovered. 174 */ 175 if (data->size > t->bt_ovflsize) { 176 if (__ovfl_put(t, data, &pg) == RET_ERROR) 177 return (RET_ERROR); 178 tdata.data = db; 179 tdata.size = NOVFLSIZE; 180 *(pgno_t *)db = pg; 181 *(size_t *)(db + sizeof(pgno_t)) = data->size; 182 dflags = P_BIGDATA; 183 data = &tdata; 184 } else 185 dflags = 0; 186 187 /* __rec_search pins the returned page. */ 188 if ((e = __rec_search(t, nrec, 189 nrec > t->bt_nrecs || flags == R_IAFTER || flags == R_IBEFORE ? 190 SINSERT : SEARCH)) == NULL) 191 return (RET_ERROR); 192 193 h = e->page; 194 index = e->index; 195 196 /* 197 * Add the specified key/data pair to the tree. The R_IAFTER and 198 * R_IBEFORE flags insert the key after/before the specified key. 199 * 200 * Pages are split as required. 201 */ 202 switch (flags) { 203 case R_IAFTER: 204 ++index; 205 break; 206 case R_IBEFORE: 207 break; 208 default: 209 if (nrec < t->bt_nrecs && 210 __rec_dleaf(t, h, index) == RET_ERROR) { 211 mpool_put(t->bt_mp, h, 0); 212 return (RET_ERROR); 213 } 214 break; 215 } 216 217 /* 218 * If not enough room, split the page. The split code will insert 219 * the key and data and unpin the current page. If inserting into 220 * the offset array, shift the pointers up. 221 */ 222 nbytes = NRLEAFDBT(data->size); 223 if (h->upper - h->lower < nbytes + sizeof(indx_t)) { 224 status = __bt_split(t, h, NULL, data, dflags, nbytes, index); 225 if (status == RET_SUCCESS) 226 ++t->bt_nrecs; 227 return (status); 228 } 229 230 if (index < (nxtindex = NEXTINDEX(h))) 231 memmove(h->linp + index + 1, h->linp + index, 232 (nxtindex - index) * sizeof(indx_t)); 233 h->lower += sizeof(indx_t); 234 235 h->linp[index] = h->upper -= nbytes; 236 dest = (char *)h + h->upper; 237 WR_RLEAF(dest, data, dflags); 238 239 ++t->bt_nrecs; 240 SET(t, B_MODIFIED); 241 mpool_put(t->bt_mp, h, MPOOL_DIRTY); 242 243 return (RET_SUCCESS); 244 } 245