Main Page | Class Hierarchy | Data Structures | Directories | File List | Data Fields | Related Pages

bt_put.c

00001 /*-
00002  * See the file LICENSE for redistribution information.
00003  *
00004  * Copyright (c) 1996-2005
00005  *      Sleepycat Software.  All rights reserved.
00006  */
00007 /*
00008  * Copyright (c) 1990, 1993, 1994, 1995, 1996
00009  *      Keith Bostic.  All rights reserved.
00010  */
00011 /*
00012  * Copyright (c) 1990, 1993, 1994, 1995
00013  *      The Regents of the University of California.  All rights reserved.
00014  *
00015  * This code is derived from software contributed to Berkeley by
00016  * Mike Olson.
00017  *
00018  * Redistribution and use in source and binary forms, with or without
00019  * modification, are permitted provided that the following conditions
00020  * are met:
00021  * 1. Redistributions of source code must retain the above copyright
00022  *    notice, this list of conditions and the following disclaimer.
00023  * 2. Redistributions in binary form must reproduce the above copyright
00024  *    notice, this list of conditions and the following disclaimer in the
00025  *    documentation and/or other materials provided with the distribution.
00026  * 3. Neither the name of the University nor the names of its contributors
00027  *    may be used to endorse or promote products derived from this software
00028  *    without specific prior written permission.
00029  *
00030  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
00031  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00032  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00033  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
00034  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00035  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00036  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00037  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00038  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00039  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00040  * SUCH DAMAGE.
00041  *
00042  * $Id: bt_put.c,v 12.10 2005/10/20 18:57:00 bostic Exp $
00043  */
00044 
00045 #include "db_config.h"
00046 
00047 #ifndef NO_SYSTEM_INCLUDES
00048 #include <sys/types.h>
00049 
00050 #include <string.h>
00051 #endif
00052 
00053 #include "db_int.h"
00054 #include "dbinc/db_page.h"
00055 #include "dbinc/db_shash.h"
00056 #include "dbinc/btree.h"
00057 #include "dbinc/mp.h"
00058 
00059 static int __bam_build
00060                __P((DBC *, u_int32_t, DBT *, PAGE *, u_int32_t, u_int32_t));
00061 static int __bam_dup_check __P((DBC *, u_int32_t,
00062                 PAGE *, u_int32_t, u_int32_t, db_indx_t *));
00063 static int __bam_dup_convert __P((DBC *, PAGE *, u_int32_t, u_int32_t));
00064 static int __bam_ovput
00065                __P((DBC *, u_int32_t, db_pgno_t, PAGE *, u_int32_t, DBT *));
00066 static u_int32_t
00067            __bam_partsize __P((DB *, u_int32_t, DBT *, PAGE *, u_int32_t));
00068 
00069 /*
00070  * __bam_iitem --
00071  *      Insert an item into the tree.
00072  *
00073  * PUBLIC: int __bam_iitem __P((DBC *, DBT *, DBT *, u_int32_t, u_int32_t));
00074  */
00075 int
00076 __bam_iitem(dbc, key, data, op, flags)
00077         DBC *dbc;
00078         DBT *key, *data;
00079         u_int32_t op, flags;
00080 {
00081         DB_ENV *dbenv;
00082         BKEYDATA *bk, bk_tmp;
00083         BTREE *t;
00084         BTREE_CURSOR *cp;
00085         DB *dbp;
00086         DBT bk_hdr, tdbt;
00087         DB_MPOOLFILE *mpf;
00088         PAGE *h;
00089         db_indx_t cnt, indx;
00090         u_int32_t data_size, have_bytes, need_bytes, needed, pages, pagespace;
00091         int cmp, bigkey, bigdata, dupadjust, padrec, replace, ret, was_deleted;
00092 
00093         COMPQUIET(bk, NULL);
00094         COMPQUIET(cnt, 0);
00095 
00096         dbp = dbc->dbp;
00097         dbenv = dbp->dbenv;
00098         mpf = dbp->mpf;
00099         cp = (BTREE_CURSOR *)dbc->internal;
00100         t = dbp->bt_internal;
00101         h = cp->page;
00102         indx = cp->indx;
00103         dupadjust = replace = was_deleted = 0;
00104 
00105         /*
00106          * Fixed-length records with partial puts: it's an error to specify
00107          * anything other simple overwrite.
00108          */
00109         if (F_ISSET(dbp, DB_AM_FIXEDLEN) &&
00110             F_ISSET(data, DB_DBT_PARTIAL) && data->size != data->dlen)
00111                 return (__db_rec_repl(dbenv, data->size, data->dlen));
00112 
00113         /*
00114          * Figure out how much space the data will take, including if it's a
00115          * partial record.
00116          *
00117          * Fixed-length records: it's an error to specify a record that's
00118          * longer than the fixed-length, and we never require less than
00119          * the fixed-length record size.
00120          */
00121         data_size = F_ISSET(data, DB_DBT_PARTIAL) ?
00122             __bam_partsize(dbp, op, data, h, indx) : data->size;
00123         padrec = 0;
00124         if (F_ISSET(dbp, DB_AM_FIXEDLEN)) {
00125                 if (data_size > t->re_len)
00126                         return (__db_rec_toobig(dbenv, data_size, t->re_len));
00127 
00128                 /* Records that are deleted anyway needn't be padded out. */
00129                 if (!LF_ISSET(BI_DELETED) && data_size < t->re_len) {
00130                         padrec = 1;
00131                         data_size = t->re_len;
00132                 }
00133         }
00134 
00135         /*
00136          * Handle partial puts or short fixed-length records: build the
00137          * real record.
00138          */
00139         if (padrec || F_ISSET(data, DB_DBT_PARTIAL)) {
00140                 tdbt = *data;
00141                 if ((ret =
00142                     __bam_build(dbc, op, &tdbt, h, indx, data_size)) != 0)
00143                         return (ret);
00144                 data = &tdbt;
00145         }
00146 
00147         /*
00148          * If the user has specified a duplicate comparison function, return
00149          * an error if DB_CURRENT was specified and the replacement data
00150          * doesn't compare equal to the current data.  This stops apps from
00151          * screwing up the duplicate sort order.  We have to do this after
00152          * we build the real record so that we're comparing the real items.
00153          */
00154         if (op == DB_CURRENT && dbp->dup_compare != NULL) {
00155                 if ((ret = __bam_cmp(dbp, data, h,
00156                     indx + (TYPE(h) == P_LBTREE ? O_INDX : 0),
00157                     dbp->dup_compare, &cmp)) != 0)
00158                         return (ret);
00159                 if (cmp != 0) {
00160                         __db_err(dbenv,
00161                 "Existing data sorts differently from put data");
00162                         return (EINVAL);
00163                 }
00164         }
00165 
00166         /*
00167          * If the key or data item won't fit on a page, we'll have to store
00168          * them on overflow pages.
00169          */
00170         needed = 0;
00171         bigdata = data_size > cp->ovflsize;
00172         switch (op) {
00173         case DB_KEYFIRST:
00174                 /* We're adding a new key and data pair. */
00175                 bigkey = key->size > cp->ovflsize;
00176                 if (bigkey)
00177                         needed += BOVERFLOW_PSIZE;
00178                 else
00179                         needed += BKEYDATA_PSIZE(key->size);
00180                 if (bigdata)
00181                         needed += BOVERFLOW_PSIZE;
00182                 else
00183                         needed += BKEYDATA_PSIZE(data_size);
00184                 break;
00185         case DB_AFTER:
00186         case DB_BEFORE:
00187         case DB_CURRENT:
00188                 /*
00189                  * We're either overwriting the data item of a key/data pair
00190                  * or we're creating a new on-page duplicate and only adding
00191                  * a data item.
00192                  *
00193                  * !!!
00194                  * We're not currently correcting for space reclaimed from
00195                  * already deleted items, but I don't think it's worth the
00196                  * complexity.
00197                  */
00198                 bigkey = 0;
00199                 if (op == DB_CURRENT) {
00200                         bk = GET_BKEYDATA(dbp, h,
00201                             indx + (TYPE(h) == P_LBTREE ? O_INDX : 0));
00202                         if (B_TYPE(bk->type) == B_KEYDATA)
00203                                 have_bytes = BKEYDATA_PSIZE(bk->len);
00204                         else
00205                                 have_bytes = BOVERFLOW_PSIZE;
00206                         need_bytes = 0;
00207                 } else {
00208                         have_bytes = 0;
00209                         need_bytes = sizeof(db_indx_t);
00210                 }
00211                 if (bigdata)
00212                         need_bytes += BOVERFLOW_PSIZE;
00213                 else
00214                         need_bytes += BKEYDATA_PSIZE(data_size);
00215 
00216                 if (have_bytes < need_bytes)
00217                         needed += need_bytes - have_bytes;
00218                 break;
00219         default:
00220                 return (__db_unknown_flag(dbenv, "DB->put", op));
00221         }
00222 
00223         /* Split the page if there's not enough room. */
00224         if (P_FREESPACE(dbp, h) < needed)
00225                 return (DB_NEEDSPLIT);
00226 
00227         /*
00228          * Check to see if we will convert to off page duplicates -- if
00229          * so, we'll need a page.
00230          */
00231         if (F_ISSET(dbp, DB_AM_DUP) &&
00232             TYPE(h) == P_LBTREE && op != DB_KEYFIRST &&
00233             P_FREESPACE(dbp, h) - needed <= dbp->pgsize / 2 &&
00234             __bam_dup_check(dbc, op, h, indx, needed, &cnt)) {
00235                 pages = 1;
00236                 dupadjust = 1;
00237         } else
00238                 pages = 0;
00239 
00240         /*
00241          * If we are not using transactions and there is a page limit
00242          * set on the file, then figure out if things will fit before
00243          * taking action.
00244          */
00245         if (dbc->txn == NULL && dbp->mpf->mfp->maxpgno != 0) {
00246                 pagespace = P_MAXSPACE(dbp, dbp->pgsize);
00247                 if (bigdata)
00248                         pages += ((data_size - 1) / pagespace) + 1;
00249                 if (bigkey)
00250                         pages += ((key->size - 1) / pagespace) + 1;
00251 
00252                 if (pages > (dbp->mpf->mfp->maxpgno - dbp->mpf->mfp->last_pgno))
00253                         return (__db_space_err(dbp));
00254         }
00255 
00256         /*
00257          * The code breaks it up into five cases:
00258          *
00259          * 1. Insert a new key/data pair.
00260          * 2. Append a new data item (a new duplicate).
00261          * 3. Insert a new data item (a new duplicate).
00262          * 4. Delete and re-add the data item (overflow item).
00263          * 5. Overwrite the data item.
00264          */
00265         switch (op) {
00266         case DB_KEYFIRST:               /* 1. Insert a new key/data pair. */
00267                 if (bigkey) {
00268                         if ((ret = __bam_ovput(dbc,
00269                             B_OVERFLOW, PGNO_INVALID, h, indx, key)) != 0)
00270                                 return (ret);
00271                 } else
00272                         if ((ret = __db_pitem(dbc, h, indx,
00273                             BKEYDATA_SIZE(key->size), NULL, key)) != 0)
00274                                 return (ret);
00275 
00276                 if ((ret = __bam_ca_di(dbc, PGNO(h), indx, 1)) != 0)
00277                         return (ret);
00278                 ++indx;
00279                 break;
00280         case DB_AFTER:                  /* 2. Append a new data item. */
00281                 if (TYPE(h) == P_LBTREE) {
00282                         /* Copy the key for the duplicate and adjust cursors. */
00283                         if ((ret =
00284                             __bam_adjindx(dbc, h, indx + P_INDX, indx, 1)) != 0)
00285                                 return (ret);
00286                         if ((ret =
00287                             __bam_ca_di(dbc, PGNO(h), indx + P_INDX, 1)) != 0)
00288                                 return (ret);
00289 
00290                         indx += 3;
00291 
00292                         cp->indx += 2;
00293                 } else {
00294                         ++indx;
00295                         cp->indx += 1;
00296                 }
00297                 break;
00298         case DB_BEFORE:                 /* 3. Insert a new data item. */
00299                 if (TYPE(h) == P_LBTREE) {
00300                         /* Copy the key for the duplicate and adjust cursors. */
00301                         if ((ret = __bam_adjindx(dbc, h, indx, indx, 1)) != 0)
00302                                 return (ret);
00303                         if ((ret = __bam_ca_di(dbc, PGNO(h), indx, 1)) != 0)
00304                                 return (ret);
00305 
00306                         ++indx;
00307                 }
00308                 break;
00309         case DB_CURRENT:
00310                  /*
00311                   * Clear the cursor's deleted flag.  The problem is that if
00312                   * we deadlock or fail while deleting the overflow item or
00313                   * replacing the non-overflow item, a subsequent cursor close
00314                   * will try and remove the item because the cursor's delete
00315                   * flag is set.
00316                   */
00317                 if ((ret = __bam_ca_delete(dbp, PGNO(h), indx, 0, NULL)) != 0)
00318                         return (ret);
00319 
00320                 if (TYPE(h) == P_LBTREE) {
00321                         ++indx;
00322                 }
00323 
00324                 /*
00325                  * In a Btree deleted records aren't counted (deleted records
00326                  * are counted in a Recno because all accesses are based on
00327                  * record number).  If it's a Btree and it's a DB_CURRENT
00328                  * operation overwriting a previously deleted record, increment
00329                  * the record count.
00330                  */
00331                 if (TYPE(h) == P_LBTREE || TYPE(h) == P_LDUP)
00332                         was_deleted = B_DISSET(bk->type);
00333 
00334                 /*
00335                  * 4. Delete and re-add the data item.
00336                  *
00337                  * If we're changing the type of the on-page structure, or we
00338                  * are referencing offpage items, we have to delete and then
00339                  * re-add the item.  We do not do any cursor adjustments here
00340                  * because we're going to immediately re-add the item into the
00341                  * same slot.
00342                  */
00343                 if (bigdata || B_TYPE(bk->type) != B_KEYDATA) {
00344                         if ((ret = __bam_ditem(dbc, h, indx)) != 0)
00345                                 return (ret);
00346                         break;
00347                 }
00348 
00349                 /* 5. Overwrite the data item. */
00350                 replace = 1;
00351                 break;
00352         default:
00353                 return (__db_unknown_flag(dbenv, "DB->put", op));
00354         }
00355 
00356         /* Add the data. */
00357         if (bigdata) {
00358                 /*
00359                  * We do not have to handle deleted (BI_DELETED) records
00360                  * in this case; the actual records should never be created.
00361                  */
00362                 DB_ASSERT(!LF_ISSET(BI_DELETED));
00363                 if ((ret = __bam_ovput(dbc,
00364                     B_OVERFLOW, PGNO_INVALID, h, indx, data)) != 0)
00365                         return (ret);
00366         } else {
00367                 if (LF_ISSET(BI_DELETED)) {
00368                         B_TSET(bk_tmp.type, B_KEYDATA, 1);
00369                         bk_tmp.len = data->size;
00370                         bk_hdr.data = &bk_tmp;
00371                         bk_hdr.size = SSZA(BKEYDATA, data);
00372                         ret = __db_pitem(dbc, h, indx,
00373                             BKEYDATA_SIZE(data->size), &bk_hdr, data);
00374                 } else if (replace)
00375                         ret = __bam_ritem(dbc, h, indx, data);
00376                 else
00377                         ret = __db_pitem(dbc, h, indx,
00378                             BKEYDATA_SIZE(data->size), NULL, data);
00379                 if (ret != 0)
00380                         return (ret);
00381         }
00382         if ((ret = __memp_fset(mpf, h, DB_MPOOL_DIRTY)) != 0)
00383                 return (ret);
00384 
00385         /*
00386          * Re-position the cursors if necessary and reset the current cursor
00387          * to point to the new item.
00388          */
00389         if (op != DB_CURRENT) {
00390                 if ((ret = __bam_ca_di(dbc, PGNO(h), indx, 1)) != 0)
00391                         return (ret);
00392                 cp->indx = TYPE(h) == P_LBTREE ? indx - O_INDX : indx;
00393         }
00394 
00395         /*
00396          * If we've changed the record count, update the tree.  There's no
00397          * need to adjust the count if the operation not performed on the
00398          * current record or when the current record was previously deleted.
00399          */
00400         if (F_ISSET(cp, C_RECNUM) && (op != DB_CURRENT || was_deleted))
00401                 if ((ret = __bam_adjust(dbc, 1)) != 0)
00402                         return (ret);
00403 
00404         /*
00405          * If a Btree leaf page is at least 50% full and we may have added or
00406          * modified a duplicate data item, see if the set of duplicates takes
00407          * up at least 25% of the space on the page.  If it does, move it onto
00408          * its own page.
00409          */
00410         if (dupadjust &&
00411             (ret = __bam_dup_convert(dbc, h, indx - O_INDX, cnt)) != 0)
00412                 return (ret);
00413 
00414         /* If we've modified a recno file, set the flag. */
00415         if (dbc->dbtype == DB_RECNO)
00416                 t->re_modified = 1;
00417 
00418         return (ret);
00419 }
00420 
00421 /*
00422  * __bam_partsize --
00423  *      Figure out how much space a partial data item is in total.
00424  */
00425 static u_int32_t
00426 __bam_partsize(dbp, op, data, h, indx)
00427         DB *dbp;
00428         u_int32_t op, indx;
00429         DBT *data;
00430         PAGE *h;
00431 {
00432         BKEYDATA *bk;
00433         u_int32_t nbytes;
00434 
00435         /*
00436          * If the record doesn't already exist, it's simply the data we're
00437          * provided.
00438          */
00439         if (op != DB_CURRENT)
00440                 return (data->doff + data->size);
00441 
00442         /*
00443          * Otherwise, it's the data provided plus any already existing data
00444          * that we're not replacing.
00445          */
00446         bk = GET_BKEYDATA(dbp, h, indx + (TYPE(h) == P_LBTREE ? O_INDX : 0));
00447         nbytes =
00448             B_TYPE(bk->type) == B_OVERFLOW ? ((BOVERFLOW *)bk)->tlen : bk->len;
00449 
00450         return (__db_partsize(nbytes, data));
00451 }
00452 
00453 /*
00454  * __bam_build --
00455  *      Build the real record for a partial put, or short fixed-length record.
00456  */
00457 static int
00458 __bam_build(dbc, op, dbt, h, indx, nbytes)
00459         DBC *dbc;
00460         u_int32_t op, indx, nbytes;
00461         DBT *dbt;
00462         PAGE *h;
00463 {
00464         BKEYDATA *bk, tbk;
00465         BOVERFLOW *bo;
00466         BTREE *t;
00467         DB *dbp;
00468         DBT copy, *rdata;
00469         u_int32_t len, tlen;
00470         u_int8_t *p;
00471         int ret;
00472 
00473         COMPQUIET(bo, NULL);
00474 
00475         dbp = dbc->dbp;
00476         t = dbp->bt_internal;
00477 
00478         /* We use the record data return memory, it's only a short-term use. */
00479         rdata = &dbc->my_rdata;
00480         if (rdata->ulen < nbytes) {
00481                 if ((ret = __os_realloc(dbp->dbenv,
00482                     nbytes, &rdata->data)) != 0) {
00483                         rdata->ulen = 0;
00484                         rdata->data = NULL;
00485                         return (ret);
00486                 }
00487                 rdata->ulen = nbytes;
00488         }
00489 
00490         /*
00491          * We use nul or pad bytes for any part of the record that isn't
00492          * specified; get it over with.
00493          */
00494         memset(rdata->data,
00495            F_ISSET(dbp, DB_AM_FIXEDLEN) ? t->re_pad : 0, nbytes);
00496 
00497         /*
00498          * In the next clauses, we need to do three things: a) set p to point
00499          * to the place at which to copy the user's data, b) set tlen to the
00500          * total length of the record, not including the bytes contributed by
00501          * the user, and c) copy any valid data from an existing record.  If
00502          * it's not a partial put (this code is called for both partial puts
00503          * and fixed-length record padding) or it's a new key, we can cut to
00504          * the chase.
00505          */
00506         if (!F_ISSET(dbt, DB_DBT_PARTIAL) || op != DB_CURRENT) {
00507                 p = (u_int8_t *)rdata->data + dbt->doff;
00508                 tlen = dbt->doff;
00509                 goto user_copy;
00510         }
00511 
00512         /* Find the current record. */
00513         if (indx < NUM_ENT(h)) {
00514                 bk = GET_BKEYDATA(dbp, h, indx + (TYPE(h) == P_LBTREE ?
00515                     O_INDX : 0));
00516                 bo = (BOVERFLOW *)bk;
00517         } else {
00518                 bk = &tbk;
00519                 B_TSET(bk->type, B_KEYDATA, 0);
00520                 bk->len = 0;
00521         }
00522         if (B_TYPE(bk->type) == B_OVERFLOW) {
00523                 /*
00524                  * In the case of an overflow record, we shift things around
00525                  * in the current record rather than allocate a separate copy.
00526                  */
00527                 memset(&copy, 0, sizeof(copy));
00528                 if ((ret = __db_goff(dbp, &copy, bo->tlen,
00529                     bo->pgno, &rdata->data, &rdata->ulen)) != 0)
00530                         return (ret);
00531 
00532                 /* Skip any leading data from the original record. */
00533                 tlen = dbt->doff;
00534                 p = (u_int8_t *)rdata->data + dbt->doff;
00535 
00536                 /*
00537                  * Copy in any trailing data from the original record.
00538                  *
00539                  * If the original record was larger than the original offset
00540                  * plus the bytes being deleted, there is trailing data in the
00541                  * original record we need to preserve.  If we aren't deleting
00542                  * the same number of bytes as we're inserting, copy it up or
00543                  * down, into place.
00544                  *
00545                  * Use memmove(), the regions may overlap.
00546                  */
00547                 if (bo->tlen > dbt->doff + dbt->dlen) {
00548                         len = bo->tlen - (dbt->doff + dbt->dlen);
00549                         if (dbt->dlen != dbt->size)
00550                                 memmove(p + dbt->size, p + dbt->dlen, len);
00551                         tlen += len;
00552                 }
00553         } else {
00554                 /* Copy in any leading data from the original record. */
00555                 memcpy(rdata->data,
00556                     bk->data, dbt->doff > bk->len ? bk->len : dbt->doff);
00557                 tlen = dbt->doff;
00558                 p = (u_int8_t *)rdata->data + dbt->doff;
00559 
00560                 /* Copy in any trailing data from the original record. */
00561                 len = dbt->doff + dbt->dlen;
00562                 if (bk->len > len) {
00563                         memcpy(p + dbt->size, bk->data + len, bk->len - len);
00564                         tlen += bk->len - len;
00565                 }
00566         }
00567 
00568 user_copy:
00569         /*
00570          * Copy in the application provided data -- p and tlen must have been
00571          * initialized above.
00572          */
00573         memcpy(p, dbt->data, dbt->size);
00574         tlen += dbt->size;
00575 
00576         /* Set the DBT to reference our new record. */
00577         rdata->size = F_ISSET(dbp, DB_AM_FIXEDLEN) ? t->re_len : tlen;
00578         rdata->dlen = 0;
00579         rdata->doff = 0;
00580         rdata->flags = 0;
00581         *dbt = *rdata;
00582         return (0);
00583 }
00584 
00585 /*
00586  * __bam_ritem --
00587  *      Replace an item on a page.
00588  *
00589  * PUBLIC: int __bam_ritem __P((DBC *, PAGE *, u_int32_t, DBT *));
00590  */
00591 int
00592 __bam_ritem(dbc, h, indx, data)
00593         DBC *dbc;
00594         PAGE *h;
00595         u_int32_t indx;
00596         DBT *data;
00597 {
00598         BKEYDATA *bk;
00599         DB *dbp;
00600         DBT orig, repl;
00601         db_indx_t cnt, lo, ln, min, off, prefix, suffix;
00602         int32_t nbytes;
00603         int ret;
00604         db_indx_t *inp;
00605         u_int8_t *p, *t;
00606 
00607         dbp = dbc->dbp;
00608 
00609         /*
00610          * Replace a single item onto a page.  The logic figuring out where
00611          * to insert and whether it fits is handled in the caller.  All we do
00612          * here is manage the page shuffling.
00613          */
00614         bk = GET_BKEYDATA(dbp, h, indx);
00615 
00616         /* Log the change. */
00617         if (DBC_LOGGING(dbc)) {
00618                 /*
00619                  * We might as well check to see if the two data items share
00620                  * a common prefix and suffix -- it can save us a lot of log
00621                  * message if they're large.
00622                  */
00623                 min = data->size < bk->len ? data->size : bk->len;
00624                 for (prefix = 0,
00625                     p = bk->data, t = data->data;
00626                     prefix < min && *p == *t; ++prefix, ++p, ++t)
00627                         ;
00628 
00629                 min -= prefix;
00630                 for (suffix = 0,
00631                     p = (u_int8_t *)bk->data + bk->len - 1,
00632                     t = (u_int8_t *)data->data + data->size - 1;
00633                     suffix < min && *p == *t; ++suffix, --p, --t)
00634                         ;
00635 
00636                 /* We only log the parts of the keys that have changed. */
00637                 orig.data = (u_int8_t *)bk->data + prefix;
00638                 orig.size = bk->len - (prefix + suffix);
00639                 repl.data = (u_int8_t *)data->data + prefix;
00640                 repl.size = data->size - (prefix + suffix);
00641                 if ((ret = __bam_repl_log(dbp, dbc->txn, &LSN(h), 0, PGNO(h),
00642                     &LSN(h), (u_int32_t)indx, (u_int32_t)B_DISSET(bk->type),
00643                     &orig, &repl, (u_int32_t)prefix, (u_int32_t)suffix)) != 0)
00644                         return (ret);
00645         } else
00646                 LSN_NOT_LOGGED(LSN(h));
00647 
00648         /*
00649          * Set references to the first in-use byte on the page and the
00650          * first byte of the item being replaced.
00651          */
00652         inp = P_INP(dbp, h);
00653         p = (u_int8_t *)h + HOFFSET(h);
00654         t = (u_int8_t *)bk;
00655 
00656         /*
00657          * If the entry is growing in size, shift the beginning of the data
00658          * part of the page down.  If the entry is shrinking in size, shift
00659          * the beginning of the data part of the page up.  Use memmove(3),
00660          * the regions overlap.
00661          */
00662         lo = BKEYDATA_SIZE(bk->len);
00663         ln = (db_indx_t)BKEYDATA_SIZE(data->size);
00664         if (lo != ln) {
00665                 nbytes = lo - ln;               /* Signed difference. */
00666                 if (p == t)                     /* First index is fast. */
00667                         inp[indx] += nbytes;
00668                 else {                          /* Else, shift the page. */
00669                         memmove(p + nbytes, p, (size_t)(t - p));
00670 
00671                         /* Adjust the indices' offsets. */
00672                         off = inp[indx];
00673                         for (cnt = 0; cnt < NUM_ENT(h); ++cnt)
00674                                 if (inp[cnt] <= off)
00675                                         inp[cnt] += nbytes;
00676                 }
00677 
00678                 /* Clean up the page and adjust the item's reference. */
00679                 HOFFSET(h) += nbytes;
00680                 t += nbytes;
00681         }
00682 
00683         /* Copy the new item onto the page. */
00684         bk = (BKEYDATA *)t;
00685         B_TSET(bk->type, B_KEYDATA, 0);
00686         bk->len = data->size;
00687         memcpy(bk->data, data->data, data->size);
00688 
00689         return (0);
00690 }
00691 
00692 /*
00693  * __bam_dup_check --
00694  *      Check to see if the duplicate set at indx should have its own page.
00695  */
00696 static int
00697 __bam_dup_check(dbc, op, h, indx, sz, cntp)
00698         DBC *dbc;
00699         u_int32_t op;
00700         PAGE *h;
00701         u_int32_t indx, sz;
00702         db_indx_t *cntp;
00703 {
00704         BKEYDATA *bk;
00705         DB *dbp;
00706         db_indx_t cnt, first, *inp;
00707 
00708         dbp = dbc->dbp;
00709         inp = P_INP(dbp, h);
00710 
00711         /*
00712          * Count the duplicate records and calculate how much room they're
00713          * using on the page.
00714          */
00715         while (indx > 0 && inp[indx] == inp[indx - P_INDX])
00716                 indx -= P_INDX;
00717 
00718         /* Count the key once. */
00719         bk = GET_BKEYDATA(dbp, h, indx);
00720         sz += B_TYPE(bk->type) == B_KEYDATA ?
00721             BKEYDATA_PSIZE(bk->len) : BOVERFLOW_PSIZE;
00722 
00723         /* Sum up all the data items. */
00724         first = indx;
00725 
00726         /*
00727          * Account for the record being inserted.  If we are replacing it,
00728          * don't count it twice.
00729          *
00730          * We execute the loop with first == indx to get the size of the
00731          * first record.
00732          */
00733         cnt = op == DB_CURRENT ? 0 : 1;
00734         for (first = indx;
00735             indx < NUM_ENT(h) && inp[first] == inp[indx];
00736             ++cnt, indx += P_INDX) {
00737                 bk = GET_BKEYDATA(dbp, h, indx + O_INDX);
00738                 sz += B_TYPE(bk->type) == B_KEYDATA ?
00739                     BKEYDATA_PSIZE(bk->len) : BOVERFLOW_PSIZE;
00740         }
00741 
00742         /*
00743          * We have to do these checks when the user is replacing the cursor's
00744          * data item -- if the application replaces a duplicate item with a
00745          * larger data item, it can increase the amount of space used by the
00746          * duplicates, requiring this check.  But that means we may have done
00747          * this check when it wasn't a duplicate item after all.
00748          */
00749         if (cnt == 1)
00750                 return (0);
00751 
00752         /*
00753          * If this set of duplicates is using more than 25% of the page, move
00754          * them off.  The choice of 25% is a WAG, but the value must be small
00755          * enough that we can always split a page without putting duplicates
00756          * on two different pages.
00757          */
00758         if (sz < dbp->pgsize / 4)
00759                 return (0);
00760 
00761         *cntp = cnt;
00762         return (1);
00763 }
00764 
00765 /*
00766  * __bam_dup_convert --
00767  *      Move a set of duplicates off-page and into their own tree.
00768  */
00769 static int
00770 __bam_dup_convert(dbc, h, indx, cnt)
00771         DBC *dbc;
00772         PAGE *h;
00773         u_int32_t indx, cnt;
00774 {
00775         BKEYDATA *bk;
00776         DB *dbp;
00777         DBT hdr;
00778         DB_MPOOLFILE *mpf;
00779         PAGE *dp;
00780         db_indx_t cpindx, dindx, first, *inp;
00781         int ret;
00782 
00783         dbp = dbc->dbp;
00784         mpf = dbp->mpf;
00785         inp = P_INP(dbp, h);
00786 
00787         /* Move to the beginning of the dup set. */
00788         while (indx > 0 && inp[indx] == inp[indx - P_INDX])
00789                 indx -= P_INDX;
00790 
00791         /* Get a new page. */
00792         if ((ret = __db_new(dbc,
00793             dbp->dup_compare == NULL ? P_LRECNO : P_LDUP, &dp)) != 0)
00794                 return (ret);
00795         P_INIT(dp, dbp->pgsize, dp->pgno,
00796             PGNO_INVALID, PGNO_INVALID, LEAFLEVEL, TYPE(dp));
00797 
00798         /*
00799          * Move this set of duplicates off the page.  First points to the first
00800          * key of the first duplicate key/data pair, cnt is the number of pairs
00801          * we're dealing with.
00802          */
00803         memset(&hdr, 0, sizeof(hdr));
00804         first = indx;
00805         dindx = indx;
00806         cpindx = 0;
00807         do {
00808                 /* Move cursors referencing the old entry to the new entry. */
00809                 if ((ret = __bam_ca_dup(dbc, first,
00810                     PGNO(h), indx, PGNO(dp), cpindx)) != 0)
00811                         goto err;
00812 
00813                 /*
00814                  * Copy the entry to the new page.  If the off-duplicate page
00815                  * If the off-duplicate page is a Btree page (i.e. dup_compare
00816                  * will be non-NULL, we use Btree pages for sorted dups,
00817                  * and Recno pages for unsorted dups), move all entries
00818                  * normally, even deleted ones.  If it's a Recno page,
00819                  * deleted entries are discarded (if the deleted entry is
00820                  * overflow, then free up those pages).
00821                  */
00822                 bk = GET_BKEYDATA(dbp, h, dindx + 1);
00823                 hdr.data = bk;
00824                 hdr.size = B_TYPE(bk->type) == B_KEYDATA ?
00825                     BKEYDATA_SIZE(bk->len) : BOVERFLOW_SIZE;
00826                 if (dbp->dup_compare == NULL && B_DISSET(bk->type)) {
00827                         /*
00828                          * Unsorted dups, i.e. recno page, and we have
00829                          * a deleted entry, don't move it, but if it was
00830                          * an overflow entry, we need to free those pages.
00831                          */
00832                         if (B_TYPE(bk->type) == B_OVERFLOW &&
00833                             (ret = __db_doff(dbc,
00834                             (GET_BOVERFLOW(dbp, h, dindx + 1))->pgno)) != 0)
00835                                 goto err;
00836                 } else {
00837                         if ((ret = __db_pitem(
00838                             dbc, dp, cpindx, hdr.size, &hdr, NULL)) != 0)
00839                                 goto err;
00840                         ++cpindx;
00841                 }
00842                 /* Delete all but the last reference to the key. */
00843                 if (cnt != 1) {
00844                         if ((ret = __bam_adjindx(dbc,
00845                             h, dindx, first + 1, 0)) != 0)
00846                                 goto err;
00847                 } else
00848                         dindx++;
00849 
00850                 /* Delete the data item. */
00851                 if ((ret = __db_ditem(dbc, h, dindx, hdr.size)) != 0)
00852                         goto err;
00853                 indx += P_INDX;
00854         } while (--cnt);
00855 
00856         /* Put in a new data item that points to the duplicates page. */
00857         if ((ret = __bam_ovput(dbc,
00858             B_DUPLICATE, dp->pgno, h, first + 1, NULL)) != 0)
00859                 goto err;
00860 
00861         /* Adjust cursors for all the above movements. */
00862         if ((ret = __bam_ca_di(dbc,
00863             PGNO(h), first + P_INDX, (int)(first + P_INDX - indx))) != 0)
00864                 goto err;
00865 
00866         return (__memp_fput(mpf, dp, DB_MPOOL_DIRTY));
00867 
00868 err:    (void)__memp_fput(mpf, dp, 0);
00869         return (ret);
00870 }
00871 
00872 /*
00873  * __bam_ovput --
00874  *      Build an item for an off-page duplicates page or overflow page and
00875  *      insert it on the page.
00876  */
00877 static int
00878 __bam_ovput(dbc, type, pgno, h, indx, item)
00879         DBC *dbc;
00880         u_int32_t type, indx;
00881         db_pgno_t pgno;
00882         PAGE *h;
00883         DBT *item;
00884 {
00885         BOVERFLOW bo;
00886         DBT hdr;
00887         int ret;
00888 
00889         UMRW_SET(bo.unused1);
00890         B_TSET(bo.type, type, 0);
00891         UMRW_SET(bo.unused2);
00892 
00893         /*
00894          * If we're creating an overflow item, do so and acquire the page
00895          * number for it.  If we're creating an off-page duplicates tree,
00896          * we are giving the page number as an argument.
00897          */
00898         if (type == B_OVERFLOW) {
00899                 if ((ret = __db_poff(dbc, item, &bo.pgno)) != 0)
00900                         return (ret);
00901                 bo.tlen = item->size;
00902         } else {
00903                 bo.pgno = pgno;
00904                 bo.tlen = 0;
00905         }
00906 
00907         /* Store the new record on the page. */
00908         memset(&hdr, 0, sizeof(hdr));
00909         hdr.data = &bo;
00910         hdr.size = BOVERFLOW_SIZE;
00911         return (__db_pitem(dbc, h, indx, BOVERFLOW_SIZE, &hdr, NULL));
00912 }

Generated on Sun Dec 25 12:14:12 2005 for Berkeley DB 4.4.16 by  doxygen 1.4.2