2 * See the file LICENSE for redistribution information.
4 * Copyright (c) 1996, 1997
5 * Sleepycat Software. All rights reserved.
8 * Copyright (c) 1990, 1993, 1994, 1995, 1996
9 * Keith Bostic. All rights reserved.
12 * Copyright (c) 1990, 1993, 1994, 1995
13 * The Regents of the University of California. All rights reserved.
15 * This code is derived from software contributed to Berkeley by
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions
21 * 1. Redistributions of source code must retain the above copyright
22 * notice, this list of conditions and the following disclaimer.
23 * 2. Redistributions in binary form must reproduce the above copyright
24 * notice, this list of conditions and the following disclaimer in the
25 * documentation and/or other materials provided with the distribution.
26 * 3. All advertising materials mentioning features or use of this software
27 * must display the following acknowledgement:
28 * This product includes software developed by the University of
29 * California, Berkeley and its contributors.
30 * 4. Neither the name of the University nor the names of its contributors
31 * may be used to endorse or promote products derived from this software
32 * without specific prior written permission.
34 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
35 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
36 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
37 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
38 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
39 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
40 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
41 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
42 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
43 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 static const char sccsid[] = "@(#)bt_open.c 10.21 (Sleepycat) 10/25/97";
54 * Implementation of btree access method for 4.4BSD.
56 * The design here was originally based on that of the btree access method
57 * used in the Postgres database system at UC Berkeley. This implementation
58 * is wholly independent of the Postgres code.
61 #ifndef NO_SYSTEM_INCLUDES
62 #include <sys/types.h>
77 #include "common_ext.h"
79 static int __bam_keyalloc __P((BTREE *));
80 static int __bam_setmeta __P((DB *, BTREE *));
86 * PUBLIC: int __bam_open __P((DB *, DBTYPE, DB_INFO *));
89 __bam_open(dbp, type, dbinfo)
97 /* Allocate the btree internal structure. */
98 if ((t = (BTREE *)__db_calloc(1, sizeof(BTREE))) == NULL)
101 t->bt_sp = t->bt_csp = t->bt_stack;
102 t->bt_esp = t->bt_stack + sizeof(t->bt_stack) / sizeof(t->bt_stack[0]);
104 if ((type == DB_RECNO || F_ISSET(dbp, DB_BT_RECNUM)) &&
105 (ret = __bam_keyalloc(t)) != 0)
109 * Intention is to make sure all of the user's selections are okay
110 * here and then use them without checking.
112 if (dbinfo != NULL) {
113 /* Minimum number of keys per page. */
114 if (dbinfo->bt_minkey == 0)
115 t->bt_minkey = DEFMINKEYPAGE;
117 if (dbinfo->bt_minkey < 2)
119 t->bt_minkey = dbinfo->bt_minkey;
122 /* Maximum number of keys per page. */
123 if (dbinfo->bt_maxkey == 0)
126 if (dbinfo->bt_maxkey < 1)
128 t->bt_maxkey = dbinfo->bt_maxkey;
132 * If no comparison, use default comparison. If no comparison
133 * and no prefix, use default prefix. (We can't default the
134 * prefix if the user supplies a comparison routine; shortening
135 * the keys may break their comparison algorithm.)
137 t->bt_compare = dbinfo->bt_compare == NULL ?
138 __bam_defcmp : dbinfo->bt_compare;
139 t->bt_prefix = dbinfo->bt_prefix == NULL ?
140 (dbinfo->bt_compare == NULL ?
141 __bam_defpfx : NULL) : dbinfo->bt_prefix;
143 t->bt_minkey = DEFMINKEYPAGE;
144 t->bt_compare = __bam_defcmp;
145 t->bt_prefix = __bam_defpfx;
148 /* Initialize the remaining fields of the DB. */
151 dbp->cursor = __bam_cursor;
152 dbp->del = __bam_delete;
153 dbp->get = __bam_get;
154 dbp->put = __bam_put;
155 dbp->stat = __bam_stat;
156 dbp->sync = __bam_sync;
159 * The btree data structure requires that at least two key/data pairs
160 * can fit on a page, but other than that there's no fixed requirement.
161 * Translate the minimum number of items into the bytes a key/data pair
162 * can use before being placed on an overflow page. We calculate for
163 * the worst possible alignment by assuming every item requires the
164 * maximum alignment for padding.
166 * Recno uses the btree bt_ovflsize value -- it's close enough.
168 t->bt_ovflsize = (dbp->pgsize - P_OVERHEAD) / (t->bt_minkey * P_INDX)
169 - (BKEYDATA_PSIZE(0) + ALIGN(1, 4));
171 /* Create a root page if new tree. */
172 if ((ret = __bam_setmeta(dbp, t)) != 0)
177 einval: ret = EINVAL;
179 err: if (t != NULL) {
180 /* If we allocated room for key/data return, discard it. */
181 if (t->bt_rkey.data != NULL)
182 __db_free(t->bt_rkey.data);
184 FREE(t, sizeof(BTREE));
191 * Create a BTREE handle for a threaded DB handle.
193 * PUBLIC: int __bam_bdup __P((DB *, DB *));
196 __bam_bdup(orig, new)
204 if ((t = (BTREE *)__db_calloc(1, sizeof(*t))) == NULL)
209 * Ignore the cursor queue, only the first DB has attached cursors.
212 t->bt_sp = t->bt_csp = t->bt_stack;
213 t->bt_esp = t->bt_stack + sizeof(t->bt_stack) / sizeof(t->bt_stack[0]);
215 if ((orig->type == DB_RECNO || F_ISSET(orig, DB_BT_RECNUM)) &&
216 (ret = __bam_keyalloc(t)) != 0) {
221 t->bt_maxkey = ot->bt_maxkey;
222 t->bt_minkey = ot->bt_minkey;
223 t->bt_compare = ot->bt_compare;
224 t->bt_prefix = ot->bt_prefix;
225 t->bt_ovflsize = ot->bt_ovflsize;
229 * The entire RECNO structure is shared. If it breaks, the application
230 * was misusing it to start with.
232 t->bt_recno = ot->bt_recno;
241 * Allocate return memory for recno keys.
248 * Recno keys are always the same size, and we don't want to have
249 * to check for space on each return. Allocate it now.
251 if ((t->bt_rkey.data = (void *)__db_malloc(sizeof(db_recno_t))) == NULL)
253 t->bt_rkey.ulen = sizeof(db_recno_t);
259 * Check (and optionally create) a tree.
262 __bam_setmeta(dbp, t)
268 DB_LOCK mlock, rlock;
272 /* Get, and optionally create the metadata page. */
273 pgno = PGNO_METADATA;
275 __bam_lget(dbp, 0, PGNO_METADATA, DB_LOCK_WRITE, &mlock)) != 0)
278 __bam_pget(dbp, (PAGE **)&meta, &pgno, DB_MPOOL_CREATE)) != 0) {
279 (void)__BT_LPUT(dbp, mlock);
284 * If the magic number is correct, we're not creating the tree.
285 * Correct any fields that may not be right. Note, all of the
286 * local flags were set by db_open(3).
288 if (meta->magic != 0) {
289 t->bt_maxkey = meta->maxkey;
290 t->bt_minkey = meta->minkey;
292 (void)memp_fput(dbp->mpf, (PAGE *)meta, 0);
293 (void)__BT_LPUT(dbp, mlock);
297 /* Initialize the tree structure metadata information. */
299 meta->pgno = PGNO_METADATA;
300 meta->magic = DB_BTREEMAGIC;
301 meta->version = DB_BTREEVERSION;
302 meta->pagesize = dbp->pgsize;
303 meta->maxkey = t->bt_maxkey;
304 meta->minkey = t->bt_minkey;
305 meta->free = PGNO_INVALID;
307 if (dbp->type == DB_RECNO)
308 F_SET(meta, BTM_RECNO);
309 if (F_ISSET(dbp, DB_AM_DUP))
310 F_SET(meta, BTM_DUP);
311 if (F_ISSET(dbp, DB_RE_FIXEDLEN))
312 F_SET(meta, BTM_FIXEDLEN);
313 if (F_ISSET(dbp, DB_BT_RECNUM))
314 F_SET(meta, BTM_RECNUM);
315 if (F_ISSET(dbp, DB_RE_RENUMBER))
316 F_SET(meta, BTM_RENUMBER);
319 memcpy(meta->uid, dbp->lock.fileid, DB_FILE_ID_LEN);
321 /* Create and initialize a root page. */
323 if ((ret = __bam_lget(dbp, 0, PGNO_ROOT, DB_LOCK_WRITE, &rlock)) != 0)
325 if ((ret = __bam_pget(dbp, &root, &pgno, DB_MPOOL_CREATE)) != 0) {
326 (void)__BT_LPUT(dbp, rlock);
329 P_INIT(root, dbp->pgsize, PGNO_ROOT, PGNO_INVALID,
330 PGNO_INVALID, 1, dbp->type == DB_RECNO ? P_LRECNO : P_LBTREE);
333 /* Release the metadata and root pages. */
334 if ((ret = memp_fput(dbp->mpf, (PAGE *)meta, DB_MPOOL_DIRTY)) != 0)
336 if ((ret = memp_fput(dbp->mpf, root, DB_MPOOL_DIRTY)) != 0)
340 * Flush the metadata and root pages to disk -- since the user can't
341 * transaction protect open, the pages have to exist during recovery.
344 * It's not useful to return not-yet-flushed here -- convert it to
347 if ((ret = memp_fsync(dbp->mpf)) == DB_INCOMPLETE)
350 /* Release the locks. */
351 (void)__BT_LPUT(dbp, mlock);
352 (void)__BT_LPUT(dbp, rlock);