1 /*- 2 * Copyright (c) 1990 The Regents of the University of California. 3 * All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * Mike Olson. 7 * 8 * %sccs.include.redist.c% 9 */ 10 11 #if defined(LIBC_SCCS) && !defined(lint) 12 static char sccsid[] = "@(#)bt_put.c 5.8 (Berkeley) 11/13/92"; 13 #endif /* LIBC_SCCS and not lint */ 14 15 #include <sys/types.h> 16 17 #include <db.h> 18 #include <errno.h> 19 #include <stdio.h> 20 #include <stdlib.h> 21 #include <string.h> 22 23 #include "btree.h" 24 25 static EPG *bt_fast __P((BTREE *, const DBT *, const DBT *, int *)); 26 27 /* 28 * __BT_PUT -- Add a btree item to the tree. 29 * 30 * Parameters: 31 * dbp: pointer to access method 32 * key: key 33 * data: data 34 * flag: R_NOOVERWRITE 35 * 36 * Returns: 37 * RET_ERROR, RET_SUCCESS and RET_SPECIAL if the key is already in the 38 * tree and R_NOOVERWRITE specified. 39 */ 40 int 41 __bt_put(dbp, key, data, flags) 42 const DB *dbp; 43 DBT *key; 44 const DBT *data; 45 u_int flags; 46 { 47 BTREE *t; 48 DBT tkey, tdata; 49 EPG *e; 50 PAGE *h; 51 index_t index, nxtindex; 52 pgno_t pg; 53 size_t nbytes; 54 int dflags, exact, status; 55 char *dest, db[NOVFLSIZE], kb[NOVFLSIZE]; 56 57 t = dbp->internal; 58 59 switch (flags) { 60 case R_CURSOR: 61 if (ISSET(t, BTF_DELCRSR)) { 62 errno = EINVAL; 63 return (RET_ERROR); 64 } 65 break; 66 case 0: 67 case R_NOOVERWRITE: 68 break; 69 default: 70 errno = EINVAL; 71 return (RET_ERROR); 72 } 73 74 if (ISSET(t, BTF_RDONLY)) { 75 errno = EPERM; 76 return (RET_ERROR); 77 } 78 79 /* 80 * If the key/data won't fit on a page, store it on indirect pages. 81 * Only store the key on the overflow page if it's too big after the 82 * data is on an overflow page. 83 * 84 * XXX 85 * If the insert fails later on, these pages aren't recovered. 86 */ 87 dflags = 0; 88 if (key->size + data->size > t->bt_ovflsize) { 89 if (key->size > t->bt_ovflsize) { 90 storekey: if (__ovfl_put(t, key, &pg) == RET_ERROR) 91 return (RET_ERROR); 92 tkey.data = kb; 93 tkey.size = NOVFLSIZE; 94 bcopy(&pg, kb, sizeof(pgno_t)); 95 bcopy(&key->size, kb + sizeof(pgno_t), sizeof(size_t)); 96 dflags |= P_BIGKEY; 97 key = &tkey; 98 } 99 if (key->size + data->size > t->bt_ovflsize) { 100 if (__ovfl_put(t, data, &pg) == RET_ERROR) 101 return (RET_ERROR); 102 tdata.data = db; 103 tdata.size = NOVFLSIZE; 104 bcopy(&pg, db, sizeof(pgno_t)); 105 bcopy(&data->size, db + sizeof(pgno_t), sizeof(size_t)); 106 dflags |= P_BIGDATA; 107 data = &tdata; 108 } 109 if (key->size + data->size > t->bt_ovflsize) 110 goto storekey; 111 } 112 113 /* Replace the cursor. */ 114 if (flags == R_CURSOR) { 115 if ((h = mpool_get(t->bt_mp, t->bt_bcursor.pgno, 0)) == NULL) 116 return (RET_ERROR); 117 index = t->bt_bcursor.index; 118 goto delete; 119 } 120 121 /* 122 * Find the key to delete, or, the location at which to insert. Bt_fast 123 * and __bt_search pin the returned page. 124 */ 125 if (t->bt_order == NOT || (e = bt_fast(t, key, data, &exact)) == NULL) 126 if ((e = __bt_search(t, key, &exact)) == NULL) 127 return (RET_ERROR); 128 h = e->page; 129 index = e->index; 130 131 /* 132 * Add the specified key/data pair to the tree. If an identical key 133 * is already in the tree, and R_NOOVERWRITE is set, an error is 134 * returned. If R_NOOVERWRITE is not set, the key is either added (if 135 * duplicates are permitted) or an error is returned. 136 * 137 * Pages are split as required. 138 */ 139 switch (flags) { 140 case R_NOOVERWRITE: 141 if (!exact) 142 break; 143 /* 144 * One special case is if the cursor references the record and 145 * it's been flagged for deletion. Then, we delete the record, 146 * leaving the cursor there -- this means that the inserted 147 * record will not be seen in a cursor scan. 148 */ 149 if (ISSET(t, BTF_DELCRSR) && t->bt_bcursor.pgno == h->pgno && 150 t->bt_bcursor.index == index) { 151 CLR(t, BTF_DELCRSR); 152 goto delete; 153 } 154 BT_CLR(t); 155 mpool_put(t->bt_mp, h, 0); 156 return (RET_SPECIAL); 157 default: 158 if (!exact || !ISSET(t, BTF_NODUPS)) 159 break; 160 delete: if (__bt_dleaf(t, h, index) == RET_ERROR) { 161 BT_CLR(t); 162 mpool_put(t->bt_mp, h, 0); 163 return (RET_ERROR); 164 } 165 break; 166 } 167 168 /* 169 * If not enough room, or the user has put a ceiling on the number of 170 * keys permitted in the page, split the page. The split code will 171 * insert the key and data and unpin the current page. If inserting 172 * into the offset array, shift the pointers up. 173 */ 174 nbytes = NBLEAFDBT(key->size, data->size); 175 if (h->upper - h->lower < nbytes + sizeof(index_t)) { 176 if ((status = __bt_split(t, h, key, 177 data, dflags, nbytes, index)) != RET_SUCCESS) 178 return (status); 179 goto success; 180 } 181 182 if (index < (nxtindex = NEXTINDEX(h))) 183 bcopy(h->linp + index, h->linp + index + 1, 184 (nxtindex - index) * sizeof(index_t)); 185 h->lower += sizeof(index_t); 186 187 h->linp[index] = h->upper -= nbytes; 188 dest = (char *)h + h->upper; 189 WR_BLEAF(dest, key, data, dflags); 190 191 if (t->bt_order == NOT) 192 if (h->nextpg == P_INVALID) { 193 if (index == NEXTINDEX(h) - 1) { 194 t->bt_order = FORWARD; 195 t->bt_last.index = index; 196 t->bt_last.pgno = h->pgno; 197 } 198 } else if (h->prevpg == P_INVALID) { 199 if (index == 0) { 200 t->bt_order = BACK; 201 t->bt_last.index = 0; 202 t->bt_last.pgno = h->pgno; 203 } 204 } 205 206 mpool_put(t->bt_mp, h, MPOOL_DIRTY); 207 BT_CLR(t); 208 209 success: 210 if (flags == R_SETCURSOR) { 211 t->bt_bcursor.pgno = e->page->pgno; 212 t->bt_bcursor.index = e->index; 213 } 214 SET(t, BTF_MODIFIED); 215 return (RET_SUCCESS); 216 } 217 218 #ifdef STATISTICS 219 u_long bt_cache_hit, bt_cache_miss; 220 #endif 221 222 /* 223 * BT_FAST -- Do a quick check for sorted data. 224 * 225 * Parameters: 226 * t: tree 227 * key: key to insert 228 * 229 * Returns: 230 * EPG for new record or NULL if not found. 231 */ 232 static EPG * 233 bt_fast(t, key, data, exactp) 234 BTREE *t; 235 const DBT *key, *data; 236 int *exactp; 237 { 238 EPG e; 239 PAGE *h; 240 size_t nbytes; 241 int cmp; 242 243 if ((h = mpool_get(t->bt_mp, t->bt_last.pgno, 0)) == NULL) { 244 t->bt_order = NOT; 245 return (NULL); 246 } 247 e.page = h; 248 e.index = t->bt_last.index; 249 250 /* 251 * If won't fit in this page or have too many keys in this page, have 252 * to search to get split stack. 253 */ 254 nbytes = NBLEAFDBT(key->size, data->size); 255 if (h->upper - h->lower < nbytes + sizeof(index_t)) 256 goto miss; 257 258 if (t->bt_order == FORWARD) { 259 if (e.page->nextpg != P_INVALID) 260 goto miss; 261 if (e.index != NEXTINDEX(h) - 1) 262 goto miss; 263 if ((cmp = __bt_cmp(t, key, &e)) < 0) 264 goto miss; 265 t->bt_last.index = cmp ? ++e.index : e.index; 266 } else { 267 if (e.page->prevpg != P_INVALID) 268 goto miss; 269 if (e.index != 0) 270 goto miss; 271 if ((cmp = __bt_cmp(t, key, &e)) > 0) 272 goto miss; 273 t->bt_last.index = 0; 274 } 275 *exactp = cmp == 0; 276 #ifdef STATISTICS 277 ++bt_cache_hit; 278 #endif 279 return (&e); 280 281 miss: 282 #ifdef STATISTICS 283 ++bt_cache_miss; 284 #endif 285 t->bt_order = NOT; 286 mpool_put(t->bt_mp, h, 0); 287 return (NULL); 288 } 289