📄 bt_open.c
字号:
if ((ret = __db_lget(dbc, 0, base_pgno, DB_LOCK_READ, 0, &metalock)) != 0) goto err; if ((ret = mpf->get(mpf, &base_pgno, 0, (PAGE **)&meta)) != 0) goto err; /* * If the magic number is set, the tree has been created. Correct * any fields that may not be right. Note, all of the local flags * were set by DB->open. * * Otherwise, we'd better be in recovery or abort, in which case the * metadata page will be created/initialized elsewhere. */ DB_ASSERT(meta->dbmeta.magic != 0 || IS_RECOVERING(dbp->dbenv) || F_ISSET(dbp, DB_AM_RECOVER)); t->bt_maxkey = meta->maxkey; t->bt_minkey = meta->minkey; t->re_pad = meta->re_pad; t->re_len = meta->re_len; t->bt_meta = base_pgno; t->bt_root = meta->root; /* * !!! * If creating a subdatabase, we've already done an insert when * we put the subdatabase's entry into the master database, so * our last-page-inserted value is wrongly initialized for the * master database, not the subdatabase we're creating. I'm not * sure where the *right* place to clear this value is, it's not * intuitively obvious that it belongs here. */ t->bt_lpgno = PGNO_INVALID; /* We must initialize last_pgno, it could be stale. */ if (!LF_ISSET(DB_RDONLY) && dbp->meta_pgno == PGNO_BASE_MD) { mpf->last_pgno(mpf, &meta->dbmeta.last_pgno); ret = mpf->put(mpf, meta, DB_MPOOL_DIRTY); } else ret = mpf->put(mpf, meta, 0); meta = NULL;err: /* Put the metadata page back. */ if (meta != NULL && (t_ret = mpf->put(mpf, meta, 0)) != 0 && ret == 0) ret = t_ret; if ((t_ret = __LPUT(dbc, metalock)) != 0 && ret == 0) ret = t_ret; if ((t_ret = dbc->c_close(dbc)) != 0 && ret == 0) ret = t_ret; return (ret);}/* * __bam_init_meta -- * * Initialize a btree meta-data page. The following fields may need * to be updated later: last_pgno, root. */static void__bam_init_meta(dbp, meta, pgno, lsnp) DB *dbp; BTMETA *meta; db_pgno_t pgno; DB_LSN *lsnp;{ BTREE *t; memset(meta, 0, sizeof(BTMETA)); meta->dbmeta.lsn = *lsnp; meta->dbmeta.pgno = pgno; meta->dbmeta.magic = DB_BTREEMAGIC; meta->dbmeta.version = DB_BTREEVERSION; meta->dbmeta.pagesize = dbp->pgsize; if (F_ISSET(dbp, DB_AM_CHKSUM)) FLD_SET(meta->dbmeta.metaflags, DBMETA_CHKSUM); if (F_ISSET(dbp, DB_AM_ENCRYPT)) { meta->dbmeta.encrypt_alg = ((DB_CIPHER *)dbp->dbenv->crypto_handle)->alg; DB_ASSERT(meta->dbmeta.encrypt_alg != 0); meta->crypto_magic = meta->dbmeta.magic; } meta->dbmeta.type = P_BTREEMETA; meta->dbmeta.free = PGNO_INVALID; meta->dbmeta.last_pgno = pgno; if (F_ISSET(dbp, DB_AM_DUP)) F_SET(&meta->dbmeta, BTM_DUP); if (F_ISSET(dbp, DB_AM_FIXEDLEN)) F_SET(&meta->dbmeta, BTM_FIXEDLEN); if (F_ISSET(dbp, DB_AM_RECNUM)) F_SET(&meta->dbmeta, BTM_RECNUM); if (F_ISSET(dbp, DB_AM_RENUMBER)) F_SET(&meta->dbmeta, BTM_RENUMBER); if (F_ISSET(dbp, DB_AM_SUBDB)) F_SET(&meta->dbmeta, BTM_SUBDB); if (dbp->dup_compare != NULL) F_SET(&meta->dbmeta, BTM_DUPSORT); if (dbp->type == DB_RECNO) F_SET(&meta->dbmeta, BTM_RECNO); memcpy(meta->dbmeta.uid, dbp->fileid, DB_FILE_ID_LEN); t = dbp->bt_internal; meta->maxkey = t->bt_maxkey; meta->minkey = t->bt_minkey; meta->re_len = t->re_len; meta->re_pad = t->re_pad;}/* * __bam_new_file -- * Create the necessary pages to begin a new database file. * * This code appears more complex than it is because of the two cases (named * and unnamed). The way to read the code is that for each page being created, * there are three parts: 1) a "get page" chunk (which either uses malloc'd * memory or calls mpf->get), 2) the initialization, and 3) the "put page" * chunk which either does a fop write or an mpf->put. * * PUBLIC: int __bam_new_file __P((DB *, DB_TXN *, DB_FH *, const char *)); */int__bam_new_file(dbp, txn, fhp, name) DB *dbp; DB_TXN *txn; DB_FH *fhp; const char *name;{ BTMETA *meta; DB_ENV *dbenv; DB_LSN lsn; DB_MPOOLFILE *mpf; DB_PGINFO pginfo; DBT pdbt; PAGE *root; db_pgno_t pgno; int ret; void *buf; dbenv = dbp->dbenv; mpf = dbp->mpf; root = NULL; meta = NULL; memset(&pdbt, 0, sizeof(pdbt)); /* Build meta-data page. */ if (name == NULL) { pgno = PGNO_BASE_MD; ret = mpf->get(mpf, &pgno, DB_MPOOL_CREATE, &meta); } else { pginfo.db_pagesize = dbp->pgsize; pginfo.flags = F_ISSET(dbp, (DB_AM_CHKSUM | DB_AM_ENCRYPT | DB_AM_SWAP)); pginfo.type = dbp->type; pdbt.data = &pginfo; pdbt.size = sizeof(pginfo); ret = __os_calloc(dbp->dbenv, 1, dbp->pgsize, &buf); meta = (BTMETA *)buf; } if (ret != 0) return (ret); LSN_NOT_LOGGED(lsn); __bam_init_meta(dbp, meta, PGNO_BASE_MD, &lsn); meta->root = 1; meta->dbmeta.last_pgno = 1; if (name == NULL) ret = mpf->put(mpf, meta, DB_MPOOL_DIRTY); else { if ((ret = __db_pgout(dbenv, PGNO_BASE_MD, meta, &pdbt)) != 0) goto err; ret = __fop_write(dbenv, txn, name, DB_APP_DATA, fhp, 0, buf, dbp->pgsize, 1); } if (ret != 0) goto err; meta = NULL; /* Now build root page. */ if (name == NULL) { pgno = 1; if ((ret = mpf->get(mpf, &pgno, DB_MPOOL_CREATE, &root)) != 0) goto err; } else {#ifdef DIAGNOSTIC memset(buf, 0, dbp->pgsize);#endif root = (PAGE *)buf; } P_INIT(root, dbp->pgsize, 1, PGNO_INVALID, PGNO_INVALID, LEAFLEVEL, dbp->type == DB_RECNO ? P_LRECNO : P_LBTREE); LSN_NOT_LOGGED(root->lsn); if (name == NULL) ret = mpf->put(mpf, root, DB_MPOOL_DIRTY); else { if ((ret = __db_pgout(dbenv, root->pgno, root, &pdbt)) != 0) goto err; ret = __fop_write(dbenv, txn, name, DB_APP_DATA, fhp, dbp->pgsize, buf, dbp->pgsize, 1); } if (ret != 0) goto err; root = NULL;err: if (name != NULL) __os_free(dbenv, buf); else { if (meta != NULL) (void)mpf->put(mpf, meta, 0); if (root != NULL) (void)mpf->put(mpf, root, 0); } return (ret);}/* * __bam_new_subdb -- * Create a metadata page and a root page for a new btree. * * PUBLIC: int __bam_new_subdb __P((DB *, DB *, DB_TXN *)); */int__bam_new_subdb(mdbp, dbp, txn) DB *mdbp, *dbp; DB_TXN *txn;{ BTMETA *meta; DBC *dbc; DB_ENV *dbenv; DB_LOCK metalock; DB_LSN lsn; DB_MPOOLFILE *mpf; PAGE *root; int ret, t_ret; dbenv = mdbp->dbenv; mpf = mdbp->mpf; dbc = NULL; meta = NULL; root = NULL; if ((ret = mdbp->cursor(mdbp, txn, &dbc, CDB_LOCKING(dbenv) ? DB_WRITECURSOR : 0)) != 0) return (ret); /* Get, and optionally create the metadata page. */ if ((ret = __db_lget(dbc, 0, dbp->meta_pgno, DB_LOCK_WRITE, 0, &metalock)) != 0) goto err; if ((ret = mpf->get(mpf, &dbp->meta_pgno, DB_MPOOL_CREATE, &meta)) != 0) goto err; /* Build meta-data page. */ lsn = meta->dbmeta.lsn; __bam_init_meta(dbp, meta, dbp->meta_pgno, &lsn); if ((ret = __db_log_page(mdbp, txn, &meta->dbmeta.lsn, dbp->meta_pgno, (PAGE *)meta)) != 0) goto err; /* Create and initialize a root page. */ if ((ret = __db_new(dbc, dbp->type == DB_RECNO ? P_LRECNO : P_LBTREE, &root)) != 0) goto err; root->level = LEAFLEVEL; if (DBENV_LOGGING(dbenv) && (ret = __bam_root_log(mdbp, txn, &meta->dbmeta.lsn, 0, meta->dbmeta.pgno, root->pgno, &meta->dbmeta.lsn)) != 0) goto err; meta->root = root->pgno; if ((ret = __db_log_page(mdbp, txn, &root->lsn, root->pgno, root)) != 0) goto err; /* Release the metadata and root pages. */ if ((ret = mpf->put(mpf, meta, DB_MPOOL_DIRTY)) != 0) goto err; meta = NULL; if ((ret = mpf->put(mpf, root, DB_MPOOL_DIRTY)) != 0) goto err; root = NULL;err: if (meta != NULL) if ((t_ret = mpf->put(mpf, meta, 0)) != 0 && ret == 0) ret = t_ret; if (root != NULL) if ((t_ret = mpf->put(mpf, root, 0)) != 0 && ret == 0) ret = t_ret; if (LOCK_ISSET(metalock)) if ((t_ret = __LPUT(dbc, metalock)) != 0 && ret == 0) ret = t_ret; if (dbc != NULL) if ((t_ret = dbc->c_close(dbc)) != 0 && ret == 0) ret = t_ret; return (ret);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -