]>
git.saurik.com Git - apple/libc.git/blob - db/btree/bt_open.c
0eb86d3ec8a88182cd2faa45dadd52490ad6c844
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 * Copyright (c) 1990, 1993
24 * The Regents of the University of California. All rights reserved.
26 * This code is derived from software contributed to Berkeley by
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
32 * 1. Redistributions of source code must retain the above copyright
33 * notice, this list of conditions and the following disclaimer.
34 * 2. Redistributions in binary form must reproduce the above copyright
35 * notice, this list of conditions and the following disclaimer in the
36 * documentation and/or other materials provided with the distribution.
37 * 3. All advertising materials mentioning features or use of this software
38 * must display the following acknowledgement:
39 * This product includes software developed by the University of
40 * California, Berkeley and its contributors.
41 * 4. Neither the name of the University nor the names of its contributors
42 * may be used to endorse or promote products derived from this software
43 * without specific prior written permission.
45 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60 * Implementation of btree access method for 4.4BSD.
62 * The design here was originally based on that of the btree access method
63 * used in the Postgres database system at UC Berkeley. This implementation
64 * is wholly independent of the Postgres code.
67 #include <sys/param.h>
82 static int byteorder
__P((void));
83 static int nroot
__P((BTREE
*));
84 static int tmp
__P((void));
87 * __BT_OPEN -- Open a btree.
89 * Creates and fills a DB struct, and calls the routine that actually
93 * fname: filename (NULL for in-memory trees)
94 * flags: open flag bits
95 * mode: open permission bits
96 * b: BTREEINFO pointer
99 * NULL on failure, pointer to DB on success.
103 __bt_open(fname
, flags
, mode
, openinfo
, dflags
)
105 int flags
, mode
, dflags
;
106 const BTREEINFO
*openinfo
;
120 * Intention is to make sure all of the user's selections are okay
121 * here and then use them without checking. Can't be complete, since
122 * we don't know the right page size, lorder or flags until the backing
123 * file is opened. Also, the file's page size can cause the cachesize
126 machine_lorder
= byteorder();
131 if (b
.flags
& ~(R_DUP
))
135 * Page size must be indx_t aligned and >= MINPSIZE. Default
136 * page size is set farther on, based on the underlying file
140 (b
.psize
< MINPSIZE
|| b
.psize
> MAX_PAGE_OFFSET
+ 1 ||
141 b
.psize
& sizeof(indx_t
) - 1))
144 /* Minimum number of keys per page; absolute minimum is 2. */
146 if (b
.minkeypage
< 2)
149 b
.minkeypage
= DEFMINKEYPAGE
;
151 /* If no comparison, use default comparison and prefix. */
152 if (b
.compare
== NULL
) {
153 b
.compare
= __bt_defcmp
;
154 if (b
.prefix
== NULL
)
155 b
.prefix
= __bt_defpfx
;
159 b
.lorder
= machine_lorder
;
161 b
.compare
= __bt_defcmp
;
164 b
.lorder
= machine_lorder
;
165 b
.minkeypage
= DEFMINKEYPAGE
;
166 b
.prefix
= __bt_defpfx
;
170 /* Check for the ubiquitous PDP-11. */
171 if (b
.lorder
!= BIG_ENDIAN
&& b
.lorder
!= LITTLE_ENDIAN
)
174 /* Allocate and initialize DB and BTREE structures. */
175 if ((t
= (BTREE
*)malloc(sizeof(BTREE
))) == NULL
)
177 memset(t
, 0, sizeof(BTREE
));
178 t
->bt_bcursor
.pgno
= P_INVALID
;
179 t
->bt_fd
= -1; /* Don't close unopened fd on error. */
180 t
->bt_lorder
= b
.lorder
;
182 t
->bt_cmp
= b
.compare
;
183 t
->bt_pfx
= b
.prefix
;
186 if ((t
->bt_dbp
= dbp
= (DB
*)malloc(sizeof(DB
))) == NULL
)
189 if (t
->bt_lorder
!= machine_lorder
)
192 dbp
->type
= DB_BTREE
;
194 dbp
->close
= __bt_close
;
195 dbp
->del
= __bt_delete
;
200 dbp
->sync
= __bt_sync
;
203 * If no file name was supplied, this is an in-memory btree and we
204 * open a backing temporary file. Otherwise, it's a disk-based tree.
207 switch(flags
& O_ACCMODE
) {
218 if ((t
->bt_fd
= open(fname
, flags
, mode
)) < 0)
222 if ((flags
& O_ACCMODE
) != O_RDWR
)
224 if ((t
->bt_fd
= tmp()) == -1)
229 if (fcntl(t
->bt_fd
, F_SETFD
, 1) == -1)
232 if (fstat(t
->bt_fd
, &sb
))
235 nr
= read(t
->bt_fd
, &m
, sizeof(BTMETA
));
238 if (nr
!= sizeof(BTMETA
))
242 * Read in the meta-data. This can change the notion of what
243 * the lorder, page size and flags are, and, when the page size
244 * changes, the cachesize value can change too. If the user
245 * specified the wrong byte order for an existing database, we
246 * don't bother to return an error, we just clear the NEEDSWAP
249 if (m
.m_magic
== BTREEMAGIC
)
253 M_32_SWAP(m
.m_magic
);
254 M_32_SWAP(m
.m_version
);
255 M_32_SWAP(m
.m_psize
);
257 M_32_SWAP(m
.m_nrecs
);
258 M_32_SWAP(m
.m_flags
);
260 if (m
.m_magic
!= BTREEMAGIC
|| m
.m_version
!= BTREEVERSION
)
262 if (m
.m_psize
< MINPSIZE
|| m
.m_psize
> MAX_PAGE_OFFSET
+ 1 ||
263 m
.m_psize
& sizeof(indx_t
) - 1)
265 if (m
.m_flags
& ~SAVEMETA
)
268 t
->bt_flags
|= m
.m_flags
;
269 t
->bt_free
= m
.m_free
;
270 t
->bt_nrecs
= m
.m_nrecs
;
273 * Set the page size to the best value for I/O to this file.
274 * Don't overflow the page offset type.
277 b
.psize
= sb
.st_blksize
;
278 if (b
.psize
< MINPSIZE
)
280 if (b
.psize
> MAX_PAGE_OFFSET
+ 1)
281 b
.psize
= MAX_PAGE_OFFSET
+ 1;
284 /* Set flag if duplicates permitted. */
285 if (!(b
.flags
& R_DUP
))
288 t
->bt_free
= P_INVALID
;
293 t
->bt_psize
= b
.psize
;
295 /* Set the cache size; must be a multiple of the page size. */
296 if (b
.cachesize
&& b
.cachesize
& b
.psize
- 1)
297 b
.cachesize
+= (~b
.cachesize
& b
.psize
- 1) + 1;
298 if (b
.cachesize
< b
.psize
* MINCACHE
)
299 b
.cachesize
= b
.psize
* MINCACHE
;
301 /* Calculate number of pages to cache. */
302 ncache
= (b
.cachesize
+ t
->bt_psize
- 1) / t
->bt_psize
;
305 * The btree data structure requires that at least two keys can fit on
306 * a page, but other than that there's no fixed requirement. The user
307 * specified a minimum number per page, and we translated that into the
308 * number of bytes a key/data pair can use before being placed on an
309 * overflow page. This calculation includes the page header, the size
310 * of the index referencing the leaf item and the size of the leaf item
311 * structure. Also, don't let the user specify a minkeypage such that
312 * a key/data pair won't fit even if both key and data are on overflow
315 t
->bt_ovflsize
= (t
->bt_psize
- BTDATAOFF
) / b
.minkeypage
-
316 (sizeof(indx_t
) + NBLEAFDBT(0, 0));
317 if (t
->bt_ovflsize
< NBLEAFDBT(NOVFLSIZE
, NOVFLSIZE
) + sizeof(indx_t
))
319 NBLEAFDBT(NOVFLSIZE
, NOVFLSIZE
) + sizeof(indx_t
);
321 /* Initialize the buffer pool. */
323 mpool_open(NULL
, t
->bt_fd
, t
->bt_psize
, ncache
)) == NULL
)
325 if (!ISSET(t
, B_INMEM
))
326 mpool_filter(t
->bt_mp
, __bt_pgin
, __bt_pgout
, t
);
328 /* Create a root page if new tree. */
329 if (nroot(t
) == RET_ERROR
)
333 if (dflags
& DB_LOCK
)
335 if (dflags
& DB_SHMEM
)
342 einval
: errno
= EINVAL
;
345 eftype
: errno
= EFTYPE
;
352 (void)close(t
->bt_fd
);
359 * NROOT -- Create the root of a new tree.
365 * RET_ERROR, RET_SUCCESS
374 if ((meta
= mpool_get(t
->bt_mp
, 0, 0)) != NULL
) {
375 mpool_put(t
->bt_mp
, meta
, 0);
376 return (RET_SUCCESS
);
381 if ((meta
= mpool_new(t
->bt_mp
, &npg
)) == NULL
)
384 if ((root
= mpool_new(t
->bt_mp
, &npg
)) == NULL
)
390 root
->prevpg
= root
->nextpg
= P_INVALID
;
391 root
->lower
= BTDATAOFF
;
392 root
->upper
= t
->bt_psize
;
393 root
->flags
= P_BLEAF
;
394 memset(meta
, 0, t
->bt_psize
);
395 mpool_put(t
->bt_mp
, meta
, MPOOL_DIRTY
);
396 mpool_put(t
->bt_mp
, root
, MPOOL_DIRTY
);
397 return (RET_SUCCESS
);
406 char path
[MAXPATHLEN
];
408 envtmp
= getenv("TMPDIR");
410 sizeof(path
), "%s/bt.XXXXXX", envtmp
? envtmp
: "/tmp");
412 (void)sigfillset(&set
);
413 (void)sigprocmask(SIG_BLOCK
, &set
, &oset
);
414 if ((fd
= mkstemp(path
)) != -1)
416 (void)sigprocmask(SIG_SETMASK
, &oset
, NULL
);
432 return (LITTLE_ENDIAN
);
446 /* Toss any page pinned across calls. */
447 if (t
->bt_pinned
!= NULL
) {
448 mpool_put(t
->bt_mp
, t
->bt_pinned
, 0);
452 /* In-memory database can't have a file descriptor. */
453 if (ISSET(t
, B_INMEM
)) {