📄 bt_open.c
字号:
/*- * See the file LICENSE for redistribution information. * * Copyright (c) 1996-2002 * Sleepycat Software. All rights reserved. *//* * Copyright (c) 1990, 1993, 1994, 1995, 1996 * Keith Bostic. All rights reserved. *//* * Copyright (c) 1990, 1993, 1994, 1995 * The Regents of the University of California. All rights reserved. * * This code is derived from software contributed to Berkeley by * Mike Olson. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */#include "db_config.h"#ifndef lintstatic const char revid[] = "$Id: bt_open.c,v 11.76 2002/09/04 19:06:42 margo Exp $";#endif /* not lint */#ifndef NO_SYSTEM_INCLUDES#include <sys/types.h>#include <limits.h>#include <string.h>#endif#include "db_int.h"#include "dbinc/crypto.h"#include "dbinc/db_page.h"#include "dbinc/db_swap.h"#include "dbinc/btree.h"#include "dbinc/db_shash.h"#include "dbinc/lock.h"#include "dbinc/log.h"#include "dbinc/fop.h"static void __bam_init_meta __P((DB *, BTMETA *, db_pgno_t, DB_LSN *));/* * __bam_open -- * Open a btree. * * PUBLIC: int __bam_open __P((DB *, * PUBLIC: DB_TXN *, const char *, db_pgno_t, u_int32_t)); */int__bam_open(dbp, txn, name, base_pgno, flags) DB *dbp; DB_TXN *txn; const char *name; db_pgno_t base_pgno; u_int32_t flags;{ BTREE *t; COMPQUIET(name, NULL); t = dbp->bt_internal; /* Initialize the remaining fields/methods of the DB. */ dbp->key_range = __bam_key_range; dbp->stat = __bam_stat; /* * We don't permit the user to specify a prefix routine if they didn't * also specify a comparison routine, they can't know enough about our * comparison routine to get it right. */ if (t->bt_compare == __bam_defcmp && t->bt_prefix != __bam_defpfx) { __db_err(dbp->dbenv,"prefix comparison may not be specified for default comparison routine"); return (EINVAL); } /* * Verify that the bt_minkey value specified won't cause the * calculation of ovflsize to underflow [#2406] for this pagesize. */ if (B_MINKEY_TO_OVFLSIZE(dbp, t->bt_minkey, dbp->pgsize) > B_MINKEY_TO_OVFLSIZE(dbp, DEFMINKEYPAGE, dbp->pgsize)) { __db_err(dbp->dbenv, "bt_minkey value of %lu too high for page size of %lu", (u_long)t->bt_minkey, (u_long)dbp->pgsize); return (EINVAL); } /* Start up the tree. */ return (__bam_read_root(dbp, txn, base_pgno, flags));}/* * __bam_metachk -- * * PUBLIC: int __bam_metachk __P((DB *, const char *, BTMETA *)); */int__bam_metachk(dbp, name, btm) DB *dbp; const char *name; BTMETA *btm;{ DB_ENV *dbenv; u_int32_t vers; int ret; dbenv = dbp->dbenv; /* * At this point, all we know is that the magic number is for a Btree. * Check the version, the database may be out of date. */ vers = btm->dbmeta.version; if (F_ISSET(dbp, DB_AM_SWAP)) M_32_SWAP(vers); switch (vers) { case 6: case 7: __db_err(dbenv, "%s: btree version %lu requires a version upgrade", name, (u_long)vers); return (DB_OLD_VERSION); case 8: case 9: break; default: __db_err(dbenv, "%s: unsupported btree version: %lu", name, (u_long)vers); return (EINVAL); } /* Swap the page if we need to. */ if (F_ISSET(dbp, DB_AM_SWAP) && (ret = __bam_mswap((PAGE *)btm)) != 0) return (ret); /* * Check application info against metadata info, and set info, flags, * and type based on metadata info. */ if ((ret = __db_fchk(dbenv, "DB->open", btm->dbmeta.flags, BTM_MASK)) != 0) return (ret); if (F_ISSET(&btm->dbmeta, BTM_RECNO)) { if (dbp->type == DB_BTREE) goto wrong_type; dbp->type = DB_RECNO; DB_ILLEGAL_METHOD(dbp, DB_OK_RECNO); } else { if (dbp->type == DB_RECNO) goto wrong_type; dbp->type = DB_BTREE; DB_ILLEGAL_METHOD(dbp, DB_OK_BTREE); } if (F_ISSET(&btm->dbmeta, BTM_DUP)) F_SET(dbp, DB_AM_DUP); else if (F_ISSET(dbp, DB_AM_DUP)) { __db_err(dbenv, "%s: DB_DUP specified to open method but not set in database", name); return (EINVAL); } if (F_ISSET(&btm->dbmeta, BTM_RECNUM)) { if (dbp->type != DB_BTREE) goto wrong_type; F_SET(dbp, DB_AM_RECNUM); if ((ret = __db_fcchk(dbenv, "DB->open", dbp->flags, DB_AM_DUP, DB_AM_RECNUM)) != 0) return (ret); } else if (F_ISSET(dbp, DB_AM_RECNUM)) { __db_err(dbenv, "%s: DB_RECNUM specified to open method but not set in database", name); return (EINVAL); } if (F_ISSET(&btm->dbmeta, BTM_FIXEDLEN)) { if (dbp->type != DB_RECNO) goto wrong_type; F_SET(dbp, DB_AM_FIXEDLEN); } else if (F_ISSET(dbp, DB_AM_FIXEDLEN)) { __db_err(dbenv, "%s: DB_FIXEDLEN specified to open method but not set in database", name); return (EINVAL); } if (F_ISSET(&btm->dbmeta, BTM_RENUMBER)) { if (dbp->type != DB_RECNO) goto wrong_type; F_SET(dbp, DB_AM_RENUMBER); } else if (F_ISSET(dbp, DB_AM_RENUMBER)) { __db_err(dbenv, "%s: DB_RENUMBER specified to open method but not set in database", name); return (EINVAL); } if (F_ISSET(&btm->dbmeta, BTM_SUBDB)) F_SET(dbp, DB_AM_SUBDB); else if (F_ISSET(dbp, DB_AM_SUBDB)) { __db_err(dbenv, "%s: multiple databases specified but not supported by file", name); return (EINVAL); } if (F_ISSET(&btm->dbmeta, BTM_DUPSORT)) { if (dbp->dup_compare == NULL) dbp->dup_compare = __bam_defcmp; F_SET(dbp, DB_AM_DUPSORT); } else if (dbp->dup_compare != NULL) { __db_err(dbenv, "%s: duplicate sort specified but not supported in database", name); return (EINVAL); } /* Set the page size. */ dbp->pgsize = btm->dbmeta.pagesize; /* Copy the file's ID. */ memcpy(dbp->fileid, btm->dbmeta.uid, DB_FILE_ID_LEN); return (0);wrong_type: if (dbp->type == DB_BTREE) __db_err(dbenv, "open method type is Btree, database type is Recno"); else __db_err(dbenv, "open method type is Recno, database type is Btree"); return (EINVAL);}/* * __bam_read_root -- * Read the root page and check a tree. * * PUBLIC: int __bam_read_root __P((DB *, DB_TXN *, db_pgno_t, u_int32_t)); */int__bam_read_root(dbp, txn, base_pgno, flags) DB *dbp; DB_TXN *txn; db_pgno_t base_pgno; u_int32_t flags;{ BTMETA *meta; BTREE *t; DBC *dbc; DB_LOCK metalock; DB_MPOOLFILE *mpf; int ret, t_ret; meta = NULL; t = dbp->bt_internal; LOCK_INIT(metalock); mpf = dbp->mpf; ret = 0; /* Get a cursor. */ if ((ret = dbp->cursor(dbp, txn, &dbc, 0)) != 0) return (ret); /* Get the metadata page. */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -