1N/A/*-
1N/A * See the file LICENSE for redistribution information.
1N/A *
1N/A * Copyright (c) 1996, 1997, 1998
1N/A * Sleepycat Software. All rights reserved.
1N/A */
1N/A/*
1N/A * Copyright (c) 1990, 1993, 1994, 1995, 1996
1N/A * Keith Bostic. All rights reserved.
1N/A */
1N/A/*
1N/A * Copyright (c) 1990, 1993, 1994, 1995
1N/A * The Regents of the University of California. All rights reserved.
1N/A *
1N/A * This code is derived from software contributed to Berkeley by
1N/A * Mike Olson.
1N/A *
1N/A * Redistribution and use in source and binary forms, with or without
1N/A * modification, are permitted provided that the following conditions
1N/A * are met:
1N/A * 1. Redistributions of source code must retain the above copyright
1N/A * notice, this list of conditions and the following disclaimer.
1N/A * 2. Redistributions in binary form must reproduce the above copyright
1N/A * notice, this list of conditions and the following disclaimer in the
1N/A * documentation and/or other materials provided with the distribution.
1N/A * 3. All advertising materials mentioning features or use of this software
1N/A * must display the following acknowledgement:
1N/A * This product includes software developed by the University of
1N/A * California, Berkeley and its contributors.
1N/A * 4. Neither the name of the University nor the names of its contributors
1N/A * may be used to endorse or promote products derived from this software
1N/A * without specific prior written permission.
1N/A *
1N/A * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1N/A * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1N/A * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1N/A * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
1N/A * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1N/A * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1N/A * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1N/A * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
1N/A * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
1N/A * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
1N/A * SUCH DAMAGE.
1N/A */
1N/A
1N/A#include "config.h"
1N/A
1N/A#ifndef lint
1N/Astatic const char sccsid[] = "@(#)bt_open.c 10.39 (Sleepycat) 11/21/98";
1N/A#endif /* not lint */
1N/A
1N/A#ifndef NO_SYSTEM_INCLUDES
1N/A#include <sys/types.h>
1N/A
1N/A#include <errno.h>
1N/A#include <limits.h>
1N/A#include <string.h>
1N/A#endif
1N/A
1N/A#include "db_int.h"
1N/A#include "db_page.h"
1N/A#include "btree.h"
1N/A
1N/A/*
1N/A * __bam_open --
1N/A * Open a btree.
1N/A *
1N/A * PUBLIC: int __bam_open __P((DB *, DB_INFO *));
1N/A */
1N/Aint
1N/A__bam_open(dbp, dbinfo)
1N/A DB *dbp;
1N/A DB_INFO *dbinfo;
1N/A{
1N/A BTREE *t;
1N/A int ret;
1N/A
1N/A /* Allocate and initialize the private btree structure. */
1N/A if ((ret = __os_calloc(1, sizeof(BTREE), &t)) != 0)
1N/A return (ret);
1N/A dbp->internal = t;
1N/A
1N/A /*
1N/A * Intention is to make sure all of the user's selections are okay
1N/A * here and then use them without checking.
1N/A */
1N/A if (dbinfo == NULL) {
1N/A t->bt_minkey = DEFMINKEYPAGE;
1N/A t->bt_compare = __bam_defcmp;
1N/A t->bt_prefix = __bam_defpfx;
1N/A } else {
1N/A /* Minimum number of keys per page. */
1N/A if (dbinfo->bt_minkey == 0)
1N/A t->bt_minkey = DEFMINKEYPAGE;
1N/A else {
1N/A if (dbinfo->bt_minkey < 2)
1N/A goto einval;
1N/A t->bt_minkey = dbinfo->bt_minkey;
1N/A }
1N/A
1N/A /* Maximum number of keys per page. */
1N/A if (dbinfo->bt_maxkey == 0)
1N/A t->bt_maxkey = 0;
1N/A else {
1N/A if (dbinfo->bt_maxkey < 1)
1N/A goto einval;
1N/A t->bt_maxkey = dbinfo->bt_maxkey;
1N/A }
1N/A
1N/A /*
1N/A * If no comparison, use default comparison. If no comparison
1N/A * and no prefix, use default prefix. (We can't default the
1N/A * prefix if the user supplies a comparison routine; shortening
1N/A * the keys may break their comparison algorithm. We don't
1N/A * permit the user to specify a prefix routine if they didn't
1N/A * also specify a comparison routine, they can't know enough
1N/A * about our comparison routine to get it right.)
1N/A */
1N/A if ((t->bt_compare = dbinfo->bt_compare) == NULL) {
1N/A if (dbinfo->bt_prefix != NULL)
1N/A goto einval;
1N/A t->bt_compare = __bam_defcmp;
1N/A t->bt_prefix = __bam_defpfx;
1N/A } else
1N/A t->bt_prefix = dbinfo->bt_prefix;
1N/A }
1N/A
1N/A /* Initialize the remaining fields/methods of the DB. */
1N/A dbp->am_close = __bam_close;
1N/A dbp->del = __bam_delete;
1N/A dbp->stat = __bam_stat;
1N/A
1N/A /* Start up the tree. */
1N/A if ((ret = __bam_read_root(dbp)) != 0)
1N/A goto err;
1N/A
1N/A /* Set the overflow page size. */
1N/A __bam_setovflsize(dbp);
1N/A
1N/A return (0);
1N/A
1N/Aeinval: ret = EINVAL;
1N/A
1N/Aerr: __os_free(t, sizeof(BTREE));
1N/A return (ret);
1N/A}
1N/A
1N/A/*
1N/A * __bam_close --
1N/A * Close a btree.
1N/A *
1N/A * PUBLIC: int __bam_close __P((DB *));
1N/A */
1N/Aint
1N/A__bam_close(dbp)
1N/A DB *dbp;
1N/A{
1N/A __os_free(dbp->internal, sizeof(BTREE));
1N/A dbp->internal = NULL;
1N/A
1N/A return (0);
1N/A}
1N/A
1N/A/*
1N/A * __bam_setovflsize --
1N/A *
1N/A * PUBLIC: void __bam_setovflsize __P((DB *));
1N/A */
1N/Avoid
1N/A__bam_setovflsize(dbp)
1N/A DB *dbp;
1N/A{
1N/A BTREE *t;
1N/A
1N/A t = dbp->internal;
1N/A
1N/A /*
1N/A * !!!
1N/A * Correction for recno, which doesn't know anything about minimum
1N/A * keys per page.
1N/A */
1N/A if (t->bt_minkey == 0)
1N/A t->bt_minkey = DEFMINKEYPAGE;
1N/A
1N/A /*
1N/A * The btree data structure requires that at least two key/data pairs
1N/A * can fit on a page, but other than that there's no fixed requirement.
1N/A * Translate the minimum number of items into the bytes a key/data pair
1N/A * can use before being placed on an overflow page. We calculate for
1N/A * the worst possible alignment by assuming every item requires the
1N/A * maximum alignment for padding.
1N/A *
1N/A * Recno uses the btree bt_ovflsize value -- it's close enough.
1N/A */
1N/A t->bt_ovflsize = (dbp->pgsize - P_OVERHEAD) / (t->bt_minkey * P_INDX)
1N/A - (BKEYDATA_PSIZE(0) + ALIGN(1, 4));
1N/A}
1N/A
1N/A/*
1N/A * __bam_read_root --
1N/A * Check (and optionally create) a tree.
1N/A *
1N/A * PUBLIC: int __bam_read_root __P((DB *));
1N/A */
1N/Aint
1N/A__bam_read_root(dbp)
1N/A DB *dbp;
1N/A{
1N/A BTMETA *meta;
1N/A BTREE *t;
1N/A DBC *dbc;
1N/A DB_LOCK metalock, rootlock;
1N/A PAGE *root;
1N/A db_pgno_t pgno;
1N/A int ret, t_ret;
1N/A
1N/A ret = 0;
1N/A t = dbp->internal;
1N/A
1N/A /* Get a cursor. */
1N/A if ((ret = dbp->cursor(dbp, NULL, &dbc, 0)) != 0)
1N/A return (ret);
1N/A
1N/A /* Get, and optionally create the metadata page. */
1N/A pgno = PGNO_METADATA;
1N/A if ((ret =
1N/A __bam_lget(dbc, 0, PGNO_METADATA, DB_LOCK_WRITE, &metalock)) != 0)
1N/A goto err;
1N/A if ((ret =
1N/A memp_fget(dbp->mpf, &pgno, DB_MPOOL_CREATE, (PAGE **)&meta)) != 0) {
1N/A (void)__BT_LPUT(dbc, metalock);
1N/A goto err;
1N/A }
1N/A
1N/A /*
1N/A * If the magic number is correct, we're not creating the tree.
1N/A * Correct any fields that may not be right. Note, all of the
1N/A * local flags were set by db_open(3).
1N/A */
1N/A if (meta->magic != 0) {
1N/A t->bt_maxkey = meta->maxkey;
1N/A t->bt_minkey = meta->minkey;
1N/A
1N/A (void)memp_fput(dbp->mpf, (PAGE *)meta, 0);
1N/A (void)__BT_LPUT(dbc, metalock);
1N/A goto done;
1N/A }
1N/A
1N/A /* Initialize the tree structure metadata information. */
1N/A memset(meta, 0, sizeof(BTMETA));
1N/A ZERO_LSN(meta->lsn);
1N/A meta->pgno = PGNO_METADATA;
1N/A meta->magic = DB_BTREEMAGIC;
1N/A meta->version = DB_BTREEVERSION;
1N/A meta->pagesize = dbp->pgsize;
1N/A meta->maxkey = t->bt_maxkey;
1N/A meta->minkey = t->bt_minkey;
1N/A meta->free = PGNO_INVALID;
1N/A if (dbp->type == DB_RECNO)
1N/A F_SET(meta, BTM_RECNO);
1N/A if (F_ISSET(dbp, DB_AM_DUP))
1N/A F_SET(meta, BTM_DUP);
1N/A if (F_ISSET(dbp, DB_RE_FIXEDLEN))
1N/A F_SET(meta, BTM_FIXEDLEN);
1N/A if (F_ISSET(dbp, DB_BT_RECNUM))
1N/A F_SET(meta, BTM_RECNUM);
1N/A if (F_ISSET(dbp, DB_RE_RENUMBER))
1N/A F_SET(meta, BTM_RENUMBER);
1N/A memcpy(meta->uid, dbp->fileid, DB_FILE_ID_LEN);
1N/A
1N/A /* Create and initialize a root page. */
1N/A pgno = PGNO_ROOT;
1N/A if ((ret =
1N/A __bam_lget(dbc, 0, PGNO_ROOT, DB_LOCK_WRITE, &rootlock)) != 0)
1N/A goto err;
1N/A if ((ret = memp_fget(dbp->mpf, &pgno, DB_MPOOL_CREATE, &root)) != 0) {
1N/A (void)__BT_LPUT(dbc, rootlock);
1N/A goto err;
1N/A }
1N/A P_INIT(root, dbp->pgsize, PGNO_ROOT, PGNO_INVALID,
1N/A PGNO_INVALID, 1, dbp->type == DB_RECNO ? P_LRECNO : P_LBTREE);
1N/A ZERO_LSN(root->lsn);
1N/A
1N/A /* Release the metadata and root pages. */
1N/A if ((ret = memp_fput(dbp->mpf, (PAGE *)meta, DB_MPOOL_DIRTY)) != 0)
1N/A goto err;
1N/A if ((ret = memp_fput(dbp->mpf, root, DB_MPOOL_DIRTY)) != 0)
1N/A goto err;
1N/A
1N/A /*
1N/A * Flush the metadata and root pages to disk -- since the user can't
1N/A * transaction protect open, the pages have to exist during recovery.
1N/A *
1N/A * XXX
1N/A * It's not useful to return not-yet-flushed here -- convert it to
1N/A * an error.
1N/A */
1N/A if ((ret = memp_fsync(dbp->mpf)) == DB_INCOMPLETE)
1N/A ret = EINVAL;
1N/A
1N/A /* Release the locks. */
1N/A (void)__BT_LPUT(dbc, metalock);
1N/A (void)__BT_LPUT(dbc, rootlock);
1N/A
1N/Aerr:
1N/Adone: if ((t_ret = dbc->c_close(dbc)) != 0 && ret == 0)
1N/A ret = t_ret;
1N/A return (ret);
1N/A}