Lines Matching +full:page +full:- +full:based
1 /*-
2 * SPDX-License-Identifier: BSD-3-Clause
38 * The design here was originally based on that of the btree access method
55 #include "un-namespace.h"
71 * __BT_OPEN -- Open a btree.
77 * fname: filename (NULL for in-memory trees)
103 * we don't know the right page size, lorder or flags until the backing in __bt_open()
104 * file is opened. Also, the file's page size can cause the cachesize in __bt_open()
116 * Page size must be indx_t aligned and >= MINPSIZE. Default in __bt_open()
117 * page size is set farther on, based on the underlying file in __bt_open()
122 b.psize & (sizeof(indx_t) - 1) )) in __bt_open()
125 /* Minimum number of keys per page; absolute minimum is 2. */ in __bt_open()
151 /* Check for the ubiquitous PDP-11. */ in __bt_open()
158 t->bt_fd = -1; /* Don't close unopened fd on error. */ in __bt_open()
159 t->bt_lorder = b.lorder; in __bt_open()
160 t->bt_order = NOT; in __bt_open()
161 t->bt_cmp = b.compare; in __bt_open()
162 t->bt_pfx = b.prefix; in __bt_open()
163 t->bt_rfd = -1; in __bt_open()
165 if ((t->bt_dbp = dbp = (DB *)calloc(1, sizeof(DB))) == NULL) in __bt_open()
167 if (t->bt_lorder != machine_lorder) in __bt_open()
170 dbp->type = DB_BTREE; in __bt_open()
171 dbp->internal = t; in __bt_open()
172 dbp->close = __bt_close; in __bt_open()
173 dbp->del = __bt_delete; in __bt_open()
174 dbp->fd = __bt_fd; in __bt_open()
175 dbp->get = __bt_get; in __bt_open()
176 dbp->put = __bt_put; in __bt_open()
177 dbp->seq = __bt_seq; in __bt_open()
178 dbp->sync = __bt_sync; in __bt_open()
181 * If no file name was supplied, this is an in-memory btree and we in __bt_open()
182 * open a backing temporary file. Otherwise, it's a disk-based tree. in __bt_open()
196 if ((t->bt_fd = _open(fname, flags | O_CLOEXEC, mode)) < 0) in __bt_open()
202 if ((t->bt_fd = tmp()) == -1) in __bt_open()
207 if (_fstat(t->bt_fd, &sb)) in __bt_open()
210 if ((nr = _read(t->bt_fd, &m, sizeof(BTMETA))) < 0) in __bt_open()
216 * Read in the meta-data. This can change the notion of what in __bt_open()
217 * the lorder, page size and flags are, and, when the page size in __bt_open()
237 m.psize & (sizeof(indx_t) - 1) ) in __bt_open()
243 t->bt_free = m.free; in __bt_open()
244 t->bt_nrecs = m.nrecs; in __bt_open()
247 * Set the page size to the best value for I/O to this file. in __bt_open()
248 * Don't overflow the page offset type. in __bt_open()
262 t->bt_free = P_INVALID; in __bt_open()
263 t->bt_nrecs = 0; in __bt_open()
267 t->bt_psize = b.psize; in __bt_open()
269 /* Set the cache size; must be a multiple of the page size. */ in __bt_open()
270 if (b.cachesize && b.cachesize & (b.psize - 1) ) in __bt_open()
271 b.cachesize += (~b.cachesize & (b.psize - 1) ) + 1; in __bt_open()
276 ncache = howmany(b.cachesize, t->bt_psize); in __bt_open()
280 * a page, but other than that there's no fixed requirement. The user in __bt_open()
281 * specified a minimum number per page, and we translated that into the in __bt_open()
283 * overflow page. This calculation includes the page header, the size in __bt_open()
289 t->bt_ovflsize = (t->bt_psize - BTDATAOFF) / b.minkeypage - in __bt_open()
291 if (t->bt_ovflsize < NBLEAFDBT(NOVFLSIZE, NOVFLSIZE) + sizeof(indx_t)) in __bt_open()
292 t->bt_ovflsize = in __bt_open()
296 if ((t->bt_mp = in __bt_open()
297 mpool_open(NULL, t->bt_fd, t->bt_psize, ncache)) == NULL) in __bt_open()
300 mpool_filter(t->bt_mp, __bt_pgin, __bt_pgout, t); in __bt_open()
302 /* Create a root page if new tree. */ in __bt_open()
324 if (t->bt_dbp) in __bt_open()
325 free(t->bt_dbp); in __bt_open()
326 if (t->bt_fd != -1) in __bt_open()
327 (void)_close(t->bt_fd); in __bt_open()
335 * NROOT -- Create the root of a new tree.
346 PAGE *meta, *root; in nroot()
349 if ((root = mpool_get(t->bt_mp, 1, 0)) != NULL) { in nroot()
350 if (root->lower == 0 && in nroot()
351 root->pgno == 0 && in nroot()
352 root->linp[0] == 0) { in nroot()
353 mpool_delete(t->bt_mp, root); in nroot()
356 mpool_put(t->bt_mp, root, 0); in nroot()
364 if ((meta = mpool_new(t->bt_mp, &npg, MPOOL_PAGE_NEXT)) == NULL) in nroot()
367 if ((root = mpool_new(t->bt_mp, &npg, MPOOL_PAGE_NEXT)) == NULL) in nroot()
372 root->pgno = npg; in nroot()
373 root->prevpg = root->nextpg = P_INVALID; in nroot()
374 root->lower = BTDATAOFF; in nroot()
375 root->upper = t->bt_psize; in nroot()
376 root->flags = P_BLEAF; in nroot()
377 memset(meta, 0, t->bt_psize); in nroot()
378 mpool_put(t->bt_mp, meta, MPOOL_DIRTY); in nroot()
379 mpool_put(t->bt_mp, root, MPOOL_DIRTY); in nroot()
396 return(-1); in tmp()
401 if ((fd = mkostemp(path, O_CLOEXEC)) != -1) in tmp()
430 t = dbp->internal; in __bt_fd()
432 /* Toss any page pinned across calls. */ in __bt_fd()
433 if (t->bt_pinned != NULL) { in __bt_fd()
434 mpool_put(t->bt_mp, t->bt_pinned, 0); in __bt_fd()
435 t->bt_pinned = NULL; in __bt_fd()
438 /* In-memory database can't have a file descriptor. */ in __bt_fd()
441 return (-1); in __bt_fd()
443 return (t->bt_fd); in __bt_fd()