root/libdb/bt_put.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. __bt_put
  2. bt_fast

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

/* [previous][next][first][last][top][bottom][index][help] */