1 /*- 2 * Copyright (c) 1990, 1993, 1994 3 * The Regents of the University of California. All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * Mike Olson. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by the University of 19 * California, Berkeley and its contributors. 20 * 4. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 * 36 * @(#)bt_put.c 8.8 (Berkeley) 7/26/94 37 */ 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 "btree.h" 48 49 static EPG *bt_fast __P((BTREE *, const DBT *, const DBT *, int *)); 50 51 /* 52 * __BT_PUT -- Add a btree item to the tree. 53 * 54 * Parameters: 55 * dbp: pointer to access method 56 * key: key 57 * data: data 58 * flag: R_NOOVERWRITE 59 * 60 * Returns: 61 * RET_ERROR, RET_SUCCESS and RET_SPECIAL if the key is already in the 62 * tree and R_NOOVERWRITE specified. 63 */ 64 int 65 __bt_put(dbp, key, data, flags) 66 const DB *dbp; 67 DBT *key; 68 const DBT *data; 69 u_int flags; 70 { 71 BTREE *t; 72 DBT tkey, tdata; 73 EPG *e; 74 PAGE *h; 75 indx_t index, nxtindex; 76 pgno_t pg; 77 u_int32_t nbytes; 78 int dflags, exact, status; 79 char *dest, db[NOVFLSIZE], kb[NOVFLSIZE]; 80 81 t = dbp->internal; 82 83 /* Toss any page pinned across calls. */ 84 if (t->bt_pinned != NULL) { 85 mpool_put(t->bt_mp, t->bt_pinned, 0); 86 t->bt_pinned = NULL; 87 } 88 89 /* Check for change to a read-only tree. */ 90 if (F_ISSET(t, B_RDONLY)) { 91 errno = EPERM; 92 return (RET_ERROR); 93 } 94 95 switch (flags) { 96 case 0: 97 case R_NOOVERWRITE: 98 break; 99 case R_CURSOR: 100 /* 101 * If flags is R_CURSOR, put the cursor. Must already 102 * have started a scan and not have already deleted it. 103 */ 104 if (F_ISSET(&t->bt_cursor, CURS_INIT) && 105 !F_ISSET(&t->bt_cursor, 106 CURS_ACQUIRE | CURS_AFTER | CURS_BEFORE)) 107 break; 108 /* FALLTHROUGH */ 109 default: 110 errno = EINVAL; 111 return (RET_ERROR); 112 } 113 114 /* 115 * If the key/data pair won't fit on a page, store it on overflow 116 * pages. Only put the key on the overflow page if the pair are 117 * still too big after moving the data to an overflow page. 118 * 119 * XXX 120 * If the insert fails later on, the overflow pages aren't recovered. 121 */ 122 dflags = 0; 123 if (key->size + data->size > t->bt_ovflsize) { 124 if (key->size > t->bt_ovflsize) { 125 storekey: if (__ovfl_put(t, key, &pg) == RET_ERROR) 126 return (RET_ERROR); 127 tkey.data = kb; 128 tkey.size = NOVFLSIZE; 129 memmove(kb, &pg, sizeof(pgno_t)); 130 memmove(kb + sizeof(pgno_t), 131 &key->size, sizeof(u_int32_t)); 132 dflags |= P_BIGKEY; 133 key = &tkey; 134 } 135 if (key->size + data->size > t->bt_ovflsize) { 136 if (__ovfl_put(t, data, &pg) == RET_ERROR) 137 return (RET_ERROR); 138 tdata.data = db; 139 tdata.size = NOVFLSIZE; 140 memmove(db, &pg, sizeof(pgno_t)); 141 memmove(db + sizeof(pgno_t), 142 &data->size, sizeof(u_int32_t)); 143 dflags |= P_BIGDATA; 144 data = &tdata; 145 } 146 if (key->size + data->size > t->bt_ovflsize) 147 goto storekey; 148 } 149 150 /* Replace the cursor. */ 151 if (flags == R_CURSOR) { 152 if ((h = mpool_get(t->bt_mp, t->bt_cursor.pg.pgno, 0)) == NULL) 153 return (RET_ERROR); 154 index = t->bt_cursor.pg.index; 155 goto delete; 156 } 157 158 /* 159 * Find the key to delete, or, the location at which to insert. 160 * Bt_fast and __bt_search both pin the returned page. 161 */ 162 if (t->bt_order == NOT || (e = bt_fast(t, key, data, &exact)) == NULL) 163 if ((e = __bt_search(t, key, &exact)) == NULL) 164 return (RET_ERROR); 165 h = e->page; 166 index = e->index; 167 168 /* 169 * Add the key/data pair to the tree. If an identical key is already 170 * in the tree, and R_NOOVERWRITE is set, an error is returned. If 171 * R_NOOVERWRITE is not set, the key is either added (if duplicates are 172 * permitted) or an error is returned. 173 */ 174 switch (flags) { 175 case R_NOOVERWRITE: 176 if (!exact) 177 break; 178 mpool_put(t->bt_mp, h, 0); 179 return (RET_SPECIAL); 180 default: 181 if (!exact || !F_ISSET(t, B_NODUPS)) 182 break; 183 /* 184 * !!! 185 * Note, the delete may empty the page, so we need to put a 186 * new entry into the page immediately. 187 */ 188 delete: if (__bt_dleaf(t, key, h, index) == RET_ERROR) { 189 mpool_put(t->bt_mp, h, 0); 190 return (RET_ERROR); 191 } 192 break; 193 } 194 195 /* 196 * If not enough room, or the user has put a ceiling on the number of 197 * keys permitted in the page, split the page. The split code will 198 * insert the key and data and unpin the current page. If inserting 199 * into the offset array, shift the pointers up. 200 */ 201 nbytes = NBLEAFDBT(key->size, data->size); 202 if (h->upper - h->lower < nbytes + sizeof(indx_t)) { 203 if ((status = __bt_split(t, h, key, 204 data, dflags, nbytes, index)) != RET_SUCCESS) 205 return (status); 206 goto success; 207 } 208 209 if (index < (nxtindex = NEXTINDEX(h))) 210 memmove(h->linp + index + 1, h->linp + index, 211 (nxtindex - index) * sizeof(indx_t)); 212 h->lower += sizeof(indx_t); 213 214 h->linp[index] = h->upper -= nbytes; 215 dest = (char *)h + h->upper; 216 WR_BLEAF(dest, key, data, dflags); 217 218 /* If the cursor is on this page, adjust it as necessary. */ 219 if (F_ISSET(&t->bt_cursor, CURS_INIT) && 220 !F_ISSET(&t->bt_cursor, CURS_ACQUIRE) && 221 t->bt_cursor.pg.pgno == h->pgno && t->bt_cursor.pg.index >= index) 222 ++t->bt_cursor.pg.index; 223 224 if (t->bt_order == NOT) 225 if (h->nextpg == P_INVALID) { 226 if (index == NEXTINDEX(h) - 1) { 227 t->bt_order = FORWARD; 228 t->bt_last.index = index; 229 t->bt_last.pgno = h->pgno; 230 } 231 } else if (h->prevpg == P_INVALID) { 232 if (index == 0) { 233 t->bt_order = BACK; 234 t->bt_last.index = 0; 235 t->bt_last.pgno = h->pgno; 236 } 237 } 238 239 mpool_put(t->bt_mp, h, MPOOL_DIRTY); 240 241 success: 242 if (flags == R_SETCURSOR) 243 __bt_setcur(t, e->page->pgno, e->index); 244 245 F_SET(t, B_MODIFIED); 246 return (RET_SUCCESS); 247 } 248 249 #ifdef STATISTICS 250 u_long bt_cache_hit, bt_cache_miss; 251 #endif 252 253 /* 254 * BT_FAST -- Do a quick check for sorted data. 255 * 256 * Parameters: 257 * t: tree 258 * key: key to insert 259 * 260 * Returns: 261 * EPG for new record or NULL if not found. 262 */ 263 static EPG * 264 bt_fast(t, key, data, exactp) 265 BTREE *t; 266 const DBT *key, *data; 267 int *exactp; 268 { 269 PAGE *h; 270 u_int32_t nbytes; 271 int cmp; 272 273 if ((h = mpool_get(t->bt_mp, t->bt_last.pgno, 0)) == NULL) { 274 t->bt_order = NOT; 275 return (NULL); 276 } 277 t->bt_cur.page = h; 278 t->bt_cur.index = t->bt_last.index; 279 280 /* 281 * If won't fit in this page or have too many keys in this page, 282 * have to search to get split stack. 283 */ 284 nbytes = NBLEAFDBT(key->size, data->size); 285 if (h->upper - h->lower < nbytes + sizeof(indx_t)) 286 goto miss; 287 288 if (t->bt_order == FORWARD) { 289 if (t->bt_cur.page->nextpg != P_INVALID) 290 goto miss; 291 if (t->bt_cur.index != NEXTINDEX(h) - 1) 292 goto miss; 293 if ((cmp = __bt_cmp(t, key, &t->bt_cur)) < 0) 294 goto miss; 295 t->bt_last.index = cmp ? ++t->bt_cur.index : t->bt_cur.index; 296 } else { 297 if (t->bt_cur.page->prevpg != P_INVALID) 298 goto miss; 299 if (t->bt_cur.index != 0) 300 goto miss; 301 if ((cmp = __bt_cmp(t, key, &t->bt_cur)) > 0) 302 goto miss; 303 t->bt_last.index = 0; 304 } 305 *exactp = cmp == 0; 306 #ifdef STATISTICS 307 ++bt_cache_hit; 308 #endif 309 return (&t->bt_cur); 310 311 miss: 312 #ifdef STATISTICS 313 ++bt_cache_miss; 314 #endif 315 t->bt_order = NOT; 316 mpool_put(t->bt_mp, h, 0); 317 return (NULL); 318 } 319