]>
git.saurik.com Git - apple/libc.git/blob - db/btree/bt_open.c
6ceba3ecafb6569722b556218164891a9c1fcfce
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
26 * Copyright (c) 1990, 1993
27 * The Regents of the University of California. All rights reserved.
29 * This code is derived from software contributed to Berkeley by
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
40 * 3. All advertising materials mentioning features or use of this software
41 * must display the following acknowledgement:
42 * This product includes software developed by the University of
43 * California, Berkeley and its contributors.
44 * 4. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63 * Implementation of btree access method for 4.4BSD.
65 * The design here was originally based on that of the btree access method
66 * used in the Postgres database system at UC Berkeley. This implementation
67 * is wholly independent of the Postgres code.
70 #include <sys/param.h>
85 static int byteorder
__P((void));
86 static int nroot
__P((BTREE
*));
87 static int tmp
__P((void));
90 * __BT_OPEN -- Open a btree.
92 * Creates and fills a DB struct, and calls the routine that actually
96 * fname: filename (NULL for in-memory trees)
97 * flags: open flag bits
98 * mode: open permission bits
99 * b: BTREEINFO pointer
102 * NULL on failure, pointer to DB on success.
106 __bt_open(fname
, flags
, mode
, openinfo
, dflags
)
108 int flags
, mode
, dflags
;
109 const BTREEINFO
*openinfo
;
123 * Intention is to make sure all of the user's selections are okay
124 * here and then use them without checking. Can't be complete, since
125 * we don't know the right page size, lorder or flags until the backing
126 * file is opened. Also, the file's page size can cause the cachesize
129 machine_lorder
= byteorder();
134 if (b
.flags
& ~(R_DUP
))
138 * Page size must be indx_t aligned and >= MINPSIZE. Default
139 * page size is set farther on, based on the underlying file
143 (b
.psize
< MINPSIZE
|| b
.psize
> MAX_PAGE_OFFSET
+ 1 ||
144 b
.psize
& sizeof(indx_t
) - 1))
147 /* Minimum number of keys per page; absolute minimum is 2. */
149 if (b
.minkeypage
< 2)
152 b
.minkeypage
= DEFMINKEYPAGE
;
154 /* If no comparison, use default comparison and prefix. */
155 if (b
.compare
== NULL
) {
156 b
.compare
= __bt_defcmp
;
157 if (b
.prefix
== NULL
)
158 b
.prefix
= __bt_defpfx
;
162 b
.lorder
= machine_lorder
;
164 b
.compare
= __bt_defcmp
;
167 b
.lorder
= machine_lorder
;
168 b
.minkeypage
= DEFMINKEYPAGE
;
169 b
.prefix
= __bt_defpfx
;
173 /* Check for the ubiquitous PDP-11. */
174 if (b
.lorder
!= BIG_ENDIAN
&& b
.lorder
!= LITTLE_ENDIAN
)
177 /* Allocate and initialize DB and BTREE structures. */
178 if ((t
= (BTREE
*)malloc(sizeof(BTREE
))) == NULL
)
180 memset(t
, 0, sizeof(BTREE
));
181 t
->bt_bcursor
.pgno
= P_INVALID
;
182 t
->bt_fd
= -1; /* Don't close unopened fd on error. */
183 t
->bt_lorder
= b
.lorder
;
185 t
->bt_cmp
= b
.compare
;
186 t
->bt_pfx
= b
.prefix
;
189 if ((t
->bt_dbp
= dbp
= (DB
*)malloc(sizeof(DB
))) == NULL
)
192 if (t
->bt_lorder
!= machine_lorder
)
195 dbp
->type
= DB_BTREE
;
197 dbp
->close
= __bt_close
;
198 dbp
->del
= __bt_delete
;
203 dbp
->sync
= __bt_sync
;
206 * If no file name was supplied, this is an in-memory btree and we
207 * open a backing temporary file. Otherwise, it's a disk-based tree.
210 switch(flags
& O_ACCMODE
) {
221 if ((t
->bt_fd
= open(fname
, flags
, mode
)) < 0)
225 if ((flags
& O_ACCMODE
) != O_RDWR
)
227 if ((t
->bt_fd
= tmp()) == -1)
232 if (fcntl(t
->bt_fd
, F_SETFD
, 1) == -1)
235 if (fstat(t
->bt_fd
, &sb
))
238 nr
= read(t
->bt_fd
, &m
, sizeof(BTMETA
));
241 if (nr
!= sizeof(BTMETA
))
245 * Read in the meta-data. This can change the notion of what
246 * the lorder, page size and flags are, and, when the page size
247 * changes, the cachesize value can change too. If the user
248 * specified the wrong byte order for an existing database, we
249 * don't bother to return an error, we just clear the NEEDSWAP
252 if (m
.m_magic
== BTREEMAGIC
)
256 M_32_SWAP(m
.m_magic
);
257 M_32_SWAP(m
.m_version
);
258 M_32_SWAP(m
.m_psize
);
260 M_32_SWAP(m
.m_nrecs
);
261 M_32_SWAP(m
.m_flags
);
263 if (m
.m_magic
!= BTREEMAGIC
|| m
.m_version
!= BTREEVERSION
)
265 if (m
.m_psize
< MINPSIZE
|| m
.m_psize
> MAX_PAGE_OFFSET
+ 1 ||
266 m
.m_psize
& sizeof(indx_t
) - 1)
268 if (m
.m_flags
& ~SAVEMETA
)
271 t
->bt_flags
|= m
.m_flags
;
272 t
->bt_free
= m
.m_free
;
273 t
->bt_nrecs
= m
.m_nrecs
;
276 * Set the page size to the best value for I/O to this file.
277 * Don't overflow the page offset type.
280 b
.psize
= sb
.st_blksize
;
281 if (b
.psize
< MINPSIZE
)
283 if (b
.psize
> MAX_PAGE_OFFSET
+ 1)
284 b
.psize
= MAX_PAGE_OFFSET
+ 1;
287 /* Set flag if duplicates permitted. */
288 if (!(b
.flags
& R_DUP
))
291 t
->bt_free
= P_INVALID
;
296 t
->bt_psize
= b
.psize
;
298 /* Set the cache size; must be a multiple of the page size. */
299 if (b
.cachesize
&& b
.cachesize
& b
.psize
- 1)
300 b
.cachesize
+= (~b
.cachesize
& b
.psize
- 1) + 1;
301 if (b
.cachesize
< b
.psize
* MINCACHE
)
302 b
.cachesize
= b
.psize
* MINCACHE
;
304 /* Calculate number of pages to cache. */
305 ncache
= (b
.cachesize
+ t
->bt_psize
- 1) / t
->bt_psize
;
308 * The btree data structure requires that at least two keys can fit on
309 * a page, but other than that there's no fixed requirement. The user
310 * specified a minimum number per page, and we translated that into the
311 * number of bytes a key/data pair can use before being placed on an
312 * overflow page. This calculation includes the page header, the size
313 * of the index referencing the leaf item and the size of the leaf item
314 * structure. Also, don't let the user specify a minkeypage such that
315 * a key/data pair won't fit even if both key and data are on overflow
318 t
->bt_ovflsize
= (t
->bt_psize
- BTDATAOFF
) / b
.minkeypage
-
319 (sizeof(indx_t
) + NBLEAFDBT(0, 0));
320 if (t
->bt_ovflsize
< NBLEAFDBT(NOVFLSIZE
, NOVFLSIZE
) + sizeof(indx_t
))
322 NBLEAFDBT(NOVFLSIZE
, NOVFLSIZE
) + sizeof(indx_t
);
324 /* Initialize the buffer pool. */
326 mpool_open(NULL
, t
->bt_fd
, t
->bt_psize
, ncache
)) == NULL
)
328 if (!ISSET(t
, B_INMEM
))
329 mpool_filter(t
->bt_mp
, __bt_pgin
, __bt_pgout
, t
);
331 /* Create a root page if new tree. */
332 if (nroot(t
) == RET_ERROR
)
336 if (dflags
& DB_LOCK
)
338 if (dflags
& DB_SHMEM
)
345 einval
: errno
= EINVAL
;
348 eftype
: errno
= EFTYPE
;
355 (void)close(t
->bt_fd
);
362 * NROOT -- Create the root of a new tree.
368 * RET_ERROR, RET_SUCCESS
377 if ((meta
= mpool_get(t
->bt_mp
, 0, 0)) != NULL
) {
378 mpool_put(t
->bt_mp
, meta
, 0);
379 return (RET_SUCCESS
);
384 if ((meta
= mpool_new(t
->bt_mp
, &npg
)) == NULL
)
387 if ((root
= mpool_new(t
->bt_mp
, &npg
)) == NULL
)
393 root
->prevpg
= root
->nextpg
= P_INVALID
;
394 root
->lower
= BTDATAOFF
;
395 root
->upper
= t
->bt_psize
;
396 root
->flags
= P_BLEAF
;
397 memset(meta
, 0, t
->bt_psize
);
398 mpool_put(t
->bt_mp
, meta
, MPOOL_DIRTY
);
399 mpool_put(t
->bt_mp
, root
, MPOOL_DIRTY
);
400 return (RET_SUCCESS
);
409 char path
[MAXPATHLEN
];
411 envtmp
= getenv("TMPDIR");
413 sizeof(path
), "%s/bt.XXXXXX", envtmp
? envtmp
: "/tmp");
415 (void)sigfillset(&set
);
416 (void)sigprocmask(SIG_BLOCK
, &set
, &oset
);
417 if ((fd
= mkstemp(path
)) != -1)
419 (void)sigprocmask(SIG_SETMASK
, &oset
, NULL
);
435 return (LITTLE_ENDIAN
);
449 /* Toss any page pinned across calls. */
450 if (t
->bt_pinned
!= NULL
) {
451 mpool_put(t
->bt_mp
, t
->bt_pinned
, 0);
455 /* In-memory database can't have a file descriptor. */
456 if (ISSET(t
, B_INMEM
)) {