bt_put.c revision 189292
11573Srgrimes/*- 214272Spst * Copyright (c) 1990, 1993, 1994 31573Srgrimes * The Regents of the University of California. All rights reserved. 41573Srgrimes * 51573Srgrimes * This code is derived from software contributed to Berkeley by 61573Srgrimes * Mike Olson. 71573Srgrimes * 81573Srgrimes * Redistribution and use in source and binary forms, with or without 91573Srgrimes * modification, are permitted provided that the following conditions 101573Srgrimes * are met: 111573Srgrimes * 1. Redistributions of source code must retain the above copyright 121573Srgrimes * notice, this list of conditions and the following disclaimer. 131573Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 141573Srgrimes * notice, this list of conditions and the following disclaimer in the 151573Srgrimes * documentation and/or other materials provided with the distribution. 161573Srgrimes * 4. Neither the name of the University nor the names of its contributors 171573Srgrimes * may be used to endorse or promote products derived from this software 181573Srgrimes * without specific prior written permission. 191573Srgrimes * 201573Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 211573Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 221573Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 231573Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 241573Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 251573Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 261573Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 271573Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 281573Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 291573Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 301573Srgrimes * SUCH DAMAGE. 311573Srgrimes */ 321573Srgrimes 331573Srgrimes#if defined(LIBC_SCCS) && !defined(lint) 3414272Spststatic char sccsid[] = "@(#)bt_put.c 8.8 (Berkeley) 7/26/94"; 351573Srgrimes#endif /* LIBC_SCCS and not lint */ 3692905Sobrien#include <sys/cdefs.h> 3792905Sobrien__FBSDID("$FreeBSD: head/lib/libc/db/btree/bt_put.c 189292 2009-03-03 02:16:12Z delphij $"); 381573Srgrimes 391573Srgrimes#include <sys/types.h> 401573Srgrimes 411573Srgrimes#include <errno.h> 421573Srgrimes#include <stdio.h> 431573Srgrimes#include <stdlib.h> 441573Srgrimes#include <string.h> 451573Srgrimes 461573Srgrimes#include <db.h> 471573Srgrimes#include "btree.h" 481573Srgrimes 4992905Sobrienstatic EPG *bt_fast(BTREE *, const DBT *, const DBT *, int *); 501573Srgrimes 511573Srgrimes/* 521573Srgrimes * __BT_PUT -- Add a btree item to the tree. 531573Srgrimes * 541573Srgrimes * Parameters: 551573Srgrimes * dbp: pointer to access method 561573Srgrimes * key: key 571573Srgrimes * data: data 581573Srgrimes * flag: R_NOOVERWRITE 591573Srgrimes * 601573Srgrimes * Returns: 611573Srgrimes * RET_ERROR, RET_SUCCESS and RET_SPECIAL if the key is already in the 621573Srgrimes * tree and R_NOOVERWRITE specified. 631573Srgrimes */ 641573Srgrimesint 65189291Sdelphij__bt_put(const DB *dbp, DBT *key, const DBT *data, u_int flags) 661573Srgrimes{ 671573Srgrimes BTREE *t; 681573Srgrimes DBT tkey, tdata; 691573Srgrimes EPG *e; 701573Srgrimes PAGE *h; 71189292Sdelphij indx_t idx, nxtindex; 721573Srgrimes pgno_t pg; 73115411Stmm u_int32_t nbytes, tmp; 741573Srgrimes int dflags, exact, status; 751573Srgrimes char *dest, db[NOVFLSIZE], kb[NOVFLSIZE]; 761573Srgrimes 771573Srgrimes t = dbp->internal; 781573Srgrimes 791573Srgrimes /* Toss any page pinned across calls. */ 801573Srgrimes if (t->bt_pinned != NULL) { 811573Srgrimes mpool_put(t->bt_mp, t->bt_pinned, 0); 821573Srgrimes t->bt_pinned = NULL; 831573Srgrimes } 841573Srgrimes 8514272Spst /* Check for change to a read-only tree. */ 8614272Spst if (F_ISSET(t, B_RDONLY)) { 8714272Spst errno = EPERM; 8814272Spst return (RET_ERROR); 8914272Spst } 9014272Spst 911573Srgrimes switch (flags) { 921573Srgrimes case 0: 931573Srgrimes case R_NOOVERWRITE: 941573Srgrimes break; 9514272Spst case R_CURSOR: 9614272Spst /* 9714272Spst * If flags is R_CURSOR, put the cursor. Must already 9814272Spst * have started a scan and not have already deleted it. 9914272Spst */ 10014272Spst if (F_ISSET(&t->bt_cursor, CURS_INIT) && 10114272Spst !F_ISSET(&t->bt_cursor, 10214272Spst CURS_ACQUIRE | CURS_AFTER | CURS_BEFORE)) 10314272Spst break; 10414272Spst /* FALLTHROUGH */ 1051573Srgrimes default: 10614272Spst errno = EINVAL; 1071573Srgrimes return (RET_ERROR); 1081573Srgrimes } 1091573Srgrimes 1101573Srgrimes /* 11114272Spst * If the key/data pair won't fit on a page, store it on overflow 11214272Spst * pages. Only put the key on the overflow page if the pair are 11314272Spst * still too big after moving the data to an overflow page. 1141573Srgrimes * 1151573Srgrimes * XXX 11614272Spst * If the insert fails later on, the overflow pages aren't recovered. 1171573Srgrimes */ 1181573Srgrimes dflags = 0; 1191573Srgrimes if (key->size + data->size > t->bt_ovflsize) { 1201573Srgrimes if (key->size > t->bt_ovflsize) { 1211573Srgrimesstorekey: if (__ovfl_put(t, key, &pg) == RET_ERROR) 1221573Srgrimes return (RET_ERROR); 1231573Srgrimes tkey.data = kb; 1241573Srgrimes tkey.size = NOVFLSIZE; 1251573Srgrimes memmove(kb, &pg, sizeof(pgno_t)); 126115411Stmm tmp = key->size; 1271573Srgrimes memmove(kb + sizeof(pgno_t), 128115411Stmm &tmp, sizeof(u_int32_t)); 1291573Srgrimes dflags |= P_BIGKEY; 1301573Srgrimes key = &tkey; 1311573Srgrimes } 1321573Srgrimes if (key->size + data->size > t->bt_ovflsize) { 1331573Srgrimes if (__ovfl_put(t, data, &pg) == RET_ERROR) 1341573Srgrimes return (RET_ERROR); 1351573Srgrimes tdata.data = db; 1361573Srgrimes tdata.size = NOVFLSIZE; 1371573Srgrimes memmove(db, &pg, sizeof(pgno_t)); 138115411Stmm tmp = data->size; 1391573Srgrimes memmove(db + sizeof(pgno_t), 140115411Stmm &tmp, sizeof(u_int32_t)); 1411573Srgrimes dflags |= P_BIGDATA; 1421573Srgrimes data = &tdata; 1431573Srgrimes } 1441573Srgrimes if (key->size + data->size > t->bt_ovflsize) 1451573Srgrimes goto storekey; 1461573Srgrimes } 1471573Srgrimes 1481573Srgrimes /* Replace the cursor. */ 1491573Srgrimes if (flags == R_CURSOR) { 15014272Spst if ((h = mpool_get(t->bt_mp, t->bt_cursor.pg.pgno, 0)) == NULL) 1511573Srgrimes return (RET_ERROR); 152189292Sdelphij idx = t->bt_cursor.pg.index; 1531573Srgrimes goto delete; 1541573Srgrimes } 1551573Srgrimes 1561573Srgrimes /* 15714272Spst * Find the key to delete, or, the location at which to insert. 15814272Spst * Bt_fast and __bt_search both pin the returned page. 1591573Srgrimes */ 1601573Srgrimes if (t->bt_order == NOT || (e = bt_fast(t, key, data, &exact)) == NULL) 1611573Srgrimes if ((e = __bt_search(t, key, &exact)) == NULL) 1621573Srgrimes return (RET_ERROR); 1631573Srgrimes h = e->page; 164189292Sdelphij idx = e->index; 1651573Srgrimes 1661573Srgrimes /* 16714272Spst * Add the key/data pair to the tree. If an identical key is already 16814272Spst * in the tree, and R_NOOVERWRITE is set, an error is returned. If 16914272Spst * R_NOOVERWRITE is not set, the key is either added (if duplicates are 17014272Spst * permitted) or an error is returned. 1711573Srgrimes */ 1721573Srgrimes switch (flags) { 1731573Srgrimes case R_NOOVERWRITE: 1741573Srgrimes if (!exact) 1751573Srgrimes break; 1761573Srgrimes mpool_put(t->bt_mp, h, 0); 1771573Srgrimes return (RET_SPECIAL); 1781573Srgrimes default: 17914272Spst if (!exact || !F_ISSET(t, B_NODUPS)) 1801573Srgrimes break; 18114272Spst /* 18214272Spst * !!! 18314272Spst * Note, the delete may empty the page, so we need to put a 18414272Spst * new entry into the page immediately. 18514272Spst */ 186189292Sdelphijdelete: if (__bt_dleaf(t, key, h, idx) == RET_ERROR) { 1871573Srgrimes mpool_put(t->bt_mp, h, 0); 1881573Srgrimes return (RET_ERROR); 1891573Srgrimes } 1901573Srgrimes break; 1911573Srgrimes } 1921573Srgrimes 1931573Srgrimes /* 1941573Srgrimes * If not enough room, or the user has put a ceiling on the number of 1951573Srgrimes * keys permitted in the page, split the page. The split code will 1961573Srgrimes * insert the key and data and unpin the current page. If inserting 1971573Srgrimes * into the offset array, shift the pointers up. 1981573Srgrimes */ 1991573Srgrimes nbytes = NBLEAFDBT(key->size, data->size); 2001573Srgrimes if (h->upper - h->lower < nbytes + sizeof(indx_t)) { 2011573Srgrimes if ((status = __bt_split(t, h, key, 202189292Sdelphij data, dflags, nbytes, idx)) != RET_SUCCESS) 2031573Srgrimes return (status); 2041573Srgrimes goto success; 2051573Srgrimes } 2061573Srgrimes 207189292Sdelphij if (idx < (nxtindex = NEXTINDEX(h))) 208189292Sdelphij memmove(h->linp + idx + 1, h->linp + idx, 209189292Sdelphij (nxtindex - idx) * sizeof(indx_t)); 2101573Srgrimes h->lower += sizeof(indx_t); 2111573Srgrimes 212189292Sdelphij h->linp[idx] = h->upper -= nbytes; 2131573Srgrimes dest = (char *)h + h->upper; 2141573Srgrimes WR_BLEAF(dest, key, data, dflags); 2151573Srgrimes 21614272Spst /* If the cursor is on this page, adjust it as necessary. */ 21714272Spst if (F_ISSET(&t->bt_cursor, CURS_INIT) && 21814272Spst !F_ISSET(&t->bt_cursor, CURS_ACQUIRE) && 219189292Sdelphij t->bt_cursor.pg.pgno == h->pgno && t->bt_cursor.pg.index >= idx) 22014272Spst ++t->bt_cursor.pg.index; 22114272Spst 222111010Snectar if (t->bt_order == NOT) { 2231573Srgrimes if (h->nextpg == P_INVALID) { 224189292Sdelphij if (idx == NEXTINDEX(h) - 1) { 2251573Srgrimes t->bt_order = FORWARD; 226189292Sdelphij t->bt_last.index = idx; 2271573Srgrimes t->bt_last.pgno = h->pgno; 2281573Srgrimes } 2291573Srgrimes } else if (h->prevpg == P_INVALID) { 230189292Sdelphij if (idx == 0) { 2311573Srgrimes t->bt_order = BACK; 2321573Srgrimes t->bt_last.index = 0; 2331573Srgrimes t->bt_last.pgno = h->pgno; 2341573Srgrimes } 2351573Srgrimes } 236111010Snectar } 2371573Srgrimes 2381573Srgrimes mpool_put(t->bt_mp, h, MPOOL_DIRTY); 2391573Srgrimes 2401573Srgrimessuccess: 24114272Spst if (flags == R_SETCURSOR) 24214272Spst __bt_setcur(t, e->page->pgno, e->index); 24314272Spst 24414272Spst F_SET(t, B_MODIFIED); 2451573Srgrimes return (RET_SUCCESS); 2461573Srgrimes} 2471573Srgrimes 2481573Srgrimes#ifdef STATISTICS 2491573Srgrimesu_long bt_cache_hit, bt_cache_miss; 2501573Srgrimes#endif 2511573Srgrimes 2521573Srgrimes/* 2531573Srgrimes * BT_FAST -- Do a quick check for sorted data. 2541573Srgrimes * 2551573Srgrimes * Parameters: 2561573Srgrimes * t: tree 2571573Srgrimes * key: key to insert 2581573Srgrimes * 2591573Srgrimes * Returns: 2601573Srgrimes * EPG for new record or NULL if not found. 2611573Srgrimes */ 2621573Srgrimesstatic EPG * 263189291Sdelphijbt_fast(BTREE *t, const DBT *key, const DBT *data, int *exactp) 2641573Srgrimes{ 2651573Srgrimes PAGE *h; 26614272Spst u_int32_t nbytes; 2671573Srgrimes int cmp; 2681573Srgrimes 2691573Srgrimes if ((h = mpool_get(t->bt_mp, t->bt_last.pgno, 0)) == NULL) { 2701573Srgrimes t->bt_order = NOT; 2711573Srgrimes return (NULL); 2721573Srgrimes } 2731573Srgrimes t->bt_cur.page = h; 2741573Srgrimes t->bt_cur.index = t->bt_last.index; 2751573Srgrimes 2761573Srgrimes /* 27714272Spst * If won't fit in this page or have too many keys in this page, 27814272Spst * have to search to get split stack. 2791573Srgrimes */ 2801573Srgrimes nbytes = NBLEAFDBT(key->size, data->size); 2811573Srgrimes if (h->upper - h->lower < nbytes + sizeof(indx_t)) 2821573Srgrimes goto miss; 2831573Srgrimes 2841573Srgrimes if (t->bt_order == FORWARD) { 2851573Srgrimes if (t->bt_cur.page->nextpg != P_INVALID) 2861573Srgrimes goto miss; 2871573Srgrimes if (t->bt_cur.index != NEXTINDEX(h) - 1) 2881573Srgrimes goto miss; 2891573Srgrimes if ((cmp = __bt_cmp(t, key, &t->bt_cur)) < 0) 2901573Srgrimes goto miss; 2911573Srgrimes t->bt_last.index = cmp ? ++t->bt_cur.index : t->bt_cur.index; 2921573Srgrimes } else { 2931573Srgrimes if (t->bt_cur.page->prevpg != P_INVALID) 2941573Srgrimes goto miss; 2951573Srgrimes if (t->bt_cur.index != 0) 2961573Srgrimes goto miss; 2971573Srgrimes if ((cmp = __bt_cmp(t, key, &t->bt_cur)) > 0) 2981573Srgrimes goto miss; 2991573Srgrimes t->bt_last.index = 0; 3001573Srgrimes } 3011573Srgrimes *exactp = cmp == 0; 3021573Srgrimes#ifdef STATISTICS 3031573Srgrimes ++bt_cache_hit; 3041573Srgrimes#endif 3051573Srgrimes return (&t->bt_cur); 3061573Srgrimes 3071573Srgrimesmiss: 3081573Srgrimes#ifdef STATISTICS 3091573Srgrimes ++bt_cache_miss; 3101573Srgrimes#endif 3111573Srgrimes t->bt_order = NOT; 3121573Srgrimes mpool_put(t->bt_mp, h, 0); 3131573Srgrimes return (NULL); 3141573Srgrimes} 315