summaryrefslogtreecommitdiff
path: root/db2/btree/bt_open.c
blob: a89cfccb97e9f4fcfa47025bb28943721f5b59a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/*-
 * See the file LICENSE for redistribution information.
 *
 * Copyright (c) 1996, 1997, 1998
 *	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. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by the University of
 *	California, Berkeley and its contributors.
 * 4. 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 "config.h"

#ifndef lint
static const char sccsid[] = "@(#)bt_open.c	10.39 (Sleepycat) 11/21/98";
#endif /* not lint */

#ifndef NO_SYSTEM_INCLUDES
#include <sys/types.h>

#include <errno.h>
#include <limits.h>
#include <string.h>
#endif

#include "db_int.h"
#include "db_page.h"
#include "btree.h"

/*
 * __bam_open --
 *	Open a btree.
 *
 * PUBLIC: int __bam_open __P((DB *, DB_INFO *));
 */
int
__bam_open(dbp, dbinfo)
	DB *dbp;
	DB_INFO *dbinfo;
{
	BTREE *t;
	int ret;

	/* Allocate and initialize the private btree structure. */
	if ((ret = __os_calloc(1, sizeof(BTREE), &t)) != 0)
		return (ret);
	dbp->internal = t;

	/*
	 * Intention is to make sure all of the user's selections are okay
	 * here and then use them without checking.
	 */
	if (dbinfo == NULL) {
		t->bt_minkey = DEFMINKEYPAGE;
		t->bt_compare = __bam_defcmp;
		t->bt_prefix = __bam_defpfx;
	} else {
		/* Minimum number of keys per page. */
		if (dbinfo->bt_minkey == 0)
			t->bt_minkey = DEFMINKEYPAGE;
		else {
			if (dbinfo->bt_minkey < 2)
				goto einval;
			t->bt_minkey = dbinfo->bt_minkey;
		}

		/* Maximum number of keys per page. */
		if (dbinfo->bt_maxkey == 0)
			t->bt_maxkey = 0;
		else {
			if (dbinfo->bt_maxkey < 1)
				goto einval;
			t->bt_maxkey = dbinfo->bt_maxkey;
		}

		/*
		 * If no comparison, use default comparison.  If no comparison
		 * and no prefix, use default prefix.  (We can't default the
		 * prefix if the user supplies a comparison routine; shortening
		 * the keys may break their comparison algorithm.  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 = dbinfo->bt_compare) == NULL) {
			if (dbinfo->bt_prefix != NULL)
				goto einval;
			t->bt_compare = __bam_defcmp;
			t->bt_prefix = __bam_defpfx;
		} else
			t->bt_prefix = dbinfo->bt_prefix;
	}

	/* Initialize the remaining fields/methods of the DB. */
	dbp->am_close = __bam_close;
	dbp->del = __bam_delete;
	dbp->stat = __bam_stat;

	/* Start up the tree. */
	if ((ret = __bam_read_root(dbp)) != 0)
		goto err;

	/* Set the overflow page size. */
	__bam_setovflsize(dbp);

	return (0);

einval:	ret = EINVAL;

err:	__os_free(t, sizeof(BTREE));
	return (ret);
}

/*
 * __bam_close --
 *	Close a btree.
 *
 * PUBLIC: int __bam_close __P((DB *));
 */
int
__bam_close(dbp)
	DB *dbp;
{
	__os_free(dbp->internal, sizeof(BTREE));
	dbp->internal = NULL;

	return (0);
}

/*
 * __bam_setovflsize --
 *
 * PUBLIC: void __bam_setovflsize __P((DB *));
 */
void
__bam_setovflsize(dbp)
	DB *dbp;
{
	BTREE *t;

	t = dbp->internal;

	/*
	 * !!!
	 * Correction for recno, which doesn't know anything about minimum
	 * keys per page.
	 */
	if (t->bt_minkey == 0)
		t->bt_minkey = DEFMINKEYPAGE;

	/*
	 * The btree data structure requires that at least two key/data pairs
	 * can fit on a page, but other than that there's no fixed requirement.
	 * Translate the minimum number of items into the bytes a key/data pair
	 * can use before being placed on an overflow page.  We calculate for
	 * the worst possible alignment by assuming every item requires the
	 * maximum alignment for padding.
	 *
	 * Recno uses the btree bt_ovflsize value -- it's close enough.
	 */
	t->bt_ovflsize = (dbp->pgsize - P_OVERHEAD) / (t->bt_minkey * P_INDX)
	    - (BKEYDATA_PSIZE(0) + ALIGN(1, 4));
}

/*
 * __bam_read_root --
 *	Check (and optionally create) a tree.
 *
 * PUBLIC: int __bam_read_root __P((DB *));
 */
int
__bam_read_root(dbp)
	DB *dbp;
{
	BTMETA *meta;
	BTREE *t;
	DBC *dbc;
	DB_LOCK metalock, rootlock;
	PAGE *root;
	db_pgno_t pgno;
	int ret, t_ret;

	ret = 0;
	t = dbp->internal;

	/* Get a cursor. */
	if ((ret = dbp->cursor(dbp, NULL, &dbc, 0)) != 0)
		return (ret);

	/* Get, and optionally create the metadata page. */
	pgno = PGNO_METADATA;
	if ((ret =
	    __bam_lget(dbc, 0, PGNO_METADATA, DB_LOCK_WRITE, &metalock)) != 0)
		goto err;
	if ((ret =
	    memp_fget(dbp->mpf, &pgno, DB_MPOOL_CREATE, (PAGE **)&meta)) != 0) {
		(void)__BT_LPUT(dbc, metalock);
		goto err;
	}

	/*
	 * If the magic number is correct, we're not creating the tree.
	 * Correct any fields that may not be right.  Note, all of the
	 * local flags were set by db_open(3).
	 */
	if (meta->magic != 0) {
		t->bt_maxkey = meta->maxkey;
		t->bt_minkey = meta->minkey;

		(void)memp_fput(dbp->mpf, (PAGE *)meta, 0);
		(void)__BT_LPUT(dbc, metalock);
		goto done;
	}

	/* Initialize the tree structure metadata information. */
	memset(meta, 0, sizeof(BTMETA));
	ZERO_LSN(meta->lsn);
	meta->pgno = PGNO_METADATA;
	meta->magic = DB_BTREEMAGIC;
	meta->version = DB_BTREEVERSION;
	meta->pagesize = dbp->pgsize;
	meta->maxkey = t->bt_maxkey;
	meta->minkey = t->bt_minkey;
	meta->free = PGNO_INVALID;
	if (dbp->type == DB_RECNO)
		F_SET(meta, BTM_RECNO);
	if (F_ISSET(dbp, DB_AM_DUP))
		F_SET(meta, BTM_DUP);
	if (F_ISSET(dbp, DB_RE_FIXEDLEN))
		F_SET(meta, BTM_FIXEDLEN);
	if (F_ISSET(dbp, DB_BT_RECNUM))
		F_SET(meta, BTM_RECNUM);
	if (F_ISSET(dbp, DB_RE_RENUMBER))
		F_SET(meta, BTM_RENUMBER);
	memcpy(meta->uid, dbp->fileid, DB_FILE_ID_LEN);

	/* Create and initialize a root page. */
	pgno = PGNO_ROOT;
	if ((ret =
	    __bam_lget(dbc, 0, PGNO_ROOT, DB_LOCK_WRITE, &rootlock)) != 0)
		goto err;
	if ((ret = memp_fget(dbp->mpf, &pgno, DB_MPOOL_CREATE, &root)) != 0) {
		(void)__BT_LPUT(dbc, rootlock);
		goto err;
	}
	P_INIT(root, dbp->pgsize, PGNO_ROOT, PGNO_INVALID,
	    PGNO_INVALID, 1, dbp->type == DB_RECNO ? P_LRECNO : P_LBTREE);
	ZERO_LSN(root->lsn);

	/* Release the metadata and root pages. */
	if ((ret = memp_fput(dbp->mpf, (PAGE *)meta, DB_MPOOL_DIRTY)) != 0)
		goto err;
	if ((ret = memp_fput(dbp->mpf, root, DB_MPOOL_DIRTY)) != 0)
		goto err;

	/*
	 * Flush the metadata and root pages to disk -- since the user can't
	 * transaction protect open, the pages have to exist during recovery.
	 *
	 * XXX
	 * It's not useful to return not-yet-flushed here -- convert it to
	 * an error.
	 */
	if ((ret = memp_fsync(dbp->mpf)) == DB_INCOMPLETE)
		ret = EINVAL;

	/* Release the locks. */
	(void)__BT_LPUT(dbc, metalock);
	(void)__BT_LPUT(dbc, rootlock);

err:
done:	if ((t_ret = dbc->c_close(dbc)) != 0 && ret == 0)
		ret = t_ret;
	return (ret);
}