158f0484fSRodney W. Grimes /*- 2ef5d438eSPaul Traina * Copyright (c) 1990, 1993, 1994 358f0484fSRodney W. Grimes * The Regents of the University of California. All rights reserved. 458f0484fSRodney W. Grimes * 558f0484fSRodney W. Grimes * This code is derived from software contributed to Berkeley by 658f0484fSRodney W. Grimes * Mike Olson. 758f0484fSRodney W. Grimes * 858f0484fSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 958f0484fSRodney W. Grimes * modification, are permitted provided that the following conditions 1058f0484fSRodney W. Grimes * are met: 1158f0484fSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 1258f0484fSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 1358f0484fSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 1458f0484fSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 1558f0484fSRodney W. Grimes * documentation and/or other materials provided with the distribution. 1658f0484fSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 1758f0484fSRodney W. Grimes * may be used to endorse or promote products derived from this software 1858f0484fSRodney W. Grimes * without specific prior written permission. 1958f0484fSRodney W. Grimes * 2058f0484fSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2158f0484fSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2258f0484fSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2358f0484fSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2458f0484fSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2558f0484fSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2658f0484fSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2758f0484fSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2858f0484fSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2958f0484fSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3058f0484fSRodney W. Grimes * SUCH DAMAGE. 3158f0484fSRodney W. Grimes */ 3258f0484fSRodney W. Grimes 3358f0484fSRodney W. Grimes #if defined(LIBC_SCCS) && !defined(lint) 34ef5d438eSPaul Traina static char sccsid[] = "@(#)bt_search.c 8.8 (Berkeley) 7/31/94"; 3558f0484fSRodney W. Grimes #endif /* LIBC_SCCS and not lint */ 36c05ac53bSDavid E. O'Brien #include <sys/cdefs.h> 37c05ac53bSDavid E. O'Brien __FBSDID("$FreeBSD$"); 3858f0484fSRodney W. Grimes 3958f0484fSRodney W. Grimes #include <sys/types.h> 4058f0484fSRodney W. Grimes 4158f0484fSRodney W. Grimes #include <stdio.h> 4258f0484fSRodney W. Grimes 4358f0484fSRodney W. Grimes #include <db.h> 4458f0484fSRodney W. Grimes #include "btree.h" 4558f0484fSRodney W. Grimes 46c05ac53bSDavid E. O'Brien static int __bt_snext(BTREE *, PAGE *, const DBT *, int *); 47c05ac53bSDavid E. O'Brien static int __bt_sprev(BTREE *, PAGE *, const DBT *, int *); 4858f0484fSRodney W. Grimes 4958f0484fSRodney W. Grimes /* 50ef5d438eSPaul Traina * __bt_search -- 51ef5d438eSPaul Traina * Search a btree for a key. 5258f0484fSRodney W. Grimes * 5358f0484fSRodney W. Grimes * Parameters: 5458f0484fSRodney W. Grimes * t: tree to search 5558f0484fSRodney W. Grimes * key: key to find 5658f0484fSRodney W. Grimes * exactp: pointer to exact match flag 5758f0484fSRodney W. Grimes * 5858f0484fSRodney W. Grimes * Returns: 5958f0484fSRodney W. Grimes * The EPG for matching record, if any, or the EPG for the location 6058f0484fSRodney W. Grimes * of the key, if it were inserted into the tree, is entered into 6158f0484fSRodney W. Grimes * the bt_cur field of the tree. A pointer to the field is returned. 6258f0484fSRodney W. Grimes */ 6358f0484fSRodney W. Grimes EPG * 640ac22237SXin LI __bt_search(BTREE *t, const DBT *key, int *exactp) 6558f0484fSRodney W. Grimes { 6658f0484fSRodney W. Grimes PAGE *h; 674c66e4b6SXin LI indx_t base, idx, lim; 6858f0484fSRodney W. Grimes pgno_t pg; 6958f0484fSRodney W. Grimes int cmp; 7058f0484fSRodney W. Grimes 7158f0484fSRodney W. Grimes BT_CLR(t); 7258f0484fSRodney W. Grimes for (pg = P_ROOT;;) { 7358f0484fSRodney W. Grimes if ((h = mpool_get(t->bt_mp, pg, 0)) == NULL) 7458f0484fSRodney W. Grimes return (NULL); 7558f0484fSRodney W. Grimes 7658f0484fSRodney W. Grimes /* Do a binary search on the current page. */ 7758f0484fSRodney W. Grimes t->bt_cur.page = h; 7858f0484fSRodney W. Grimes for (base = 0, lim = NEXTINDEX(h); lim; lim >>= 1) { 794c66e4b6SXin LI t->bt_cur.index = idx = base + (lim >> 1); 8058f0484fSRodney W. Grimes if ((cmp = __bt_cmp(t, key, &t->bt_cur)) == 0) { 8158f0484fSRodney W. Grimes if (h->flags & P_BLEAF) { 8258f0484fSRodney W. Grimes *exactp = 1; 8358f0484fSRodney W. Grimes return (&t->bt_cur); 8458f0484fSRodney W. Grimes } 8558f0484fSRodney W. Grimes goto next; 8658f0484fSRodney W. Grimes } 8758f0484fSRodney W. Grimes if (cmp > 0) { 884c66e4b6SXin LI base = idx + 1; 8958f0484fSRodney W. Grimes --lim; 9058f0484fSRodney W. Grimes } 9158f0484fSRodney W. Grimes } 9258f0484fSRodney W. Grimes 9358f0484fSRodney W. Grimes /* 94ef5d438eSPaul Traina * If it's a leaf page, we're almost done. If no duplicates 95ef5d438eSPaul Traina * are allowed, or we have an exact match, we're done. Else, 96ef5d438eSPaul Traina * it's possible that there were matching keys on this page, 97ef5d438eSPaul Traina * which later deleted, and we're on a page with no matches 98ef5d438eSPaul Traina * while there are matches on other pages. If at the start or 99ef5d438eSPaul Traina * end of a page, check the adjacent page. 10058f0484fSRodney W. Grimes */ 10158f0484fSRodney W. Grimes if (h->flags & P_BLEAF) { 102ef5d438eSPaul Traina if (!F_ISSET(t, B_NODUPS)) { 10358f0484fSRodney W. Grimes if (base == 0 && 104ef5d438eSPaul Traina h->prevpg != P_INVALID && 105ef5d438eSPaul Traina __bt_sprev(t, h, key, exactp)) 10658f0484fSRodney W. Grimes return (&t->bt_cur); 10758f0484fSRodney W. Grimes if (base == NEXTINDEX(h) && 108ef5d438eSPaul Traina h->nextpg != P_INVALID && 109ef5d438eSPaul Traina __bt_snext(t, h, key, exactp)) 11058f0484fSRodney W. Grimes return (&t->bt_cur); 11158f0484fSRodney W. Grimes } 112ef5d438eSPaul Traina *exactp = 0; 113ef5d438eSPaul Traina t->bt_cur.index = base; 11458f0484fSRodney W. Grimes return (&t->bt_cur); 11558f0484fSRodney W. Grimes } 11658f0484fSRodney W. Grimes 11758f0484fSRodney W. Grimes /* 11858f0484fSRodney W. Grimes * No match found. Base is the smallest index greater than 11958f0484fSRodney W. Grimes * key and may be zero or a last + 1 index. If it's non-zero, 12058f0484fSRodney W. Grimes * decrement by one, and record the internal page which should 12158f0484fSRodney W. Grimes * be a parent page for the key. If a split later occurs, the 12258f0484fSRodney W. Grimes * inserted page will be to the right of the saved page. 12358f0484fSRodney W. Grimes */ 1244c66e4b6SXin LI idx = base ? base - 1 : base; 12558f0484fSRodney W. Grimes 1264c66e4b6SXin LI next: BT_PUSH(t, h->pgno, idx); 1274c66e4b6SXin LI pg = GETBINTERNAL(h, idx)->pgno; 12858f0484fSRodney W. Grimes mpool_put(t->bt_mp, h, 0); 12958f0484fSRodney W. Grimes } 13058f0484fSRodney W. Grimes } 13158f0484fSRodney W. Grimes 13258f0484fSRodney W. Grimes /* 133ef5d438eSPaul Traina * __bt_snext -- 134ef5d438eSPaul Traina * Check for an exact match after the key. 13558f0484fSRodney W. Grimes * 13658f0484fSRodney W. Grimes * Parameters: 137ef5d438eSPaul Traina * t: tree 138ef5d438eSPaul Traina * h: current page 139ef5d438eSPaul Traina * key: key 14058f0484fSRodney W. Grimes * exactp: pointer to exact match flag 14158f0484fSRodney W. Grimes * 14258f0484fSRodney W. Grimes * Returns: 14358f0484fSRodney W. Grimes * If an exact match found. 14458f0484fSRodney W. Grimes */ 14558f0484fSRodney W. Grimes static int 1460ac22237SXin LI __bt_snext(BTREE *t, PAGE *h, const DBT *key, int *exactp) 14758f0484fSRodney W. Grimes { 14858f0484fSRodney W. Grimes EPG e; 14958f0484fSRodney W. Grimes 15058f0484fSRodney W. Grimes /* 151ef5d438eSPaul Traina * Get the next page. The key is either an exact 152ef5d438eSPaul Traina * match, or not as good as the one we already have. 15358f0484fSRodney W. Grimes */ 154ef5d438eSPaul Traina if ((e.page = mpool_get(t->bt_mp, h->nextpg, 0)) == NULL) 155ef5d438eSPaul Traina return (0); 156ef5d438eSPaul Traina e.index = 0; 15758f0484fSRodney W. Grimes if (__bt_cmp(t, key, &e) == 0) { 15858f0484fSRodney W. Grimes mpool_put(t->bt_mp, h, 0); 15958f0484fSRodney W. Grimes t->bt_cur = e; 16058f0484fSRodney W. Grimes *exactp = 1; 16158f0484fSRodney W. Grimes return (1); 16258f0484fSRodney W. Grimes } 163ef5d438eSPaul Traina mpool_put(t->bt_mp, e.page, 0); 16458f0484fSRodney W. Grimes return (0); 16558f0484fSRodney W. Grimes } 16658f0484fSRodney W. Grimes 16758f0484fSRodney W. Grimes /* 168ef5d438eSPaul Traina * __bt_sprev -- 169ef5d438eSPaul Traina * Check for an exact match before the key. 17058f0484fSRodney W. Grimes * 17158f0484fSRodney W. Grimes * Parameters: 172ef5d438eSPaul Traina * t: tree 173ef5d438eSPaul Traina * h: current page 174ef5d438eSPaul Traina * key: key 17558f0484fSRodney W. Grimes * exactp: pointer to exact match flag 17658f0484fSRodney W. Grimes * 17758f0484fSRodney W. Grimes * Returns: 17858f0484fSRodney W. Grimes * If an exact match found. 17958f0484fSRodney W. Grimes */ 18058f0484fSRodney W. Grimes static int 1810ac22237SXin LI __bt_sprev(BTREE *t, PAGE *h, const DBT *key, int *exactp) 18258f0484fSRodney W. Grimes { 18358f0484fSRodney W. Grimes EPG e; 18458f0484fSRodney W. Grimes 18558f0484fSRodney W. Grimes /* 186ef5d438eSPaul Traina * Get the previous page. The key is either an exact 187ef5d438eSPaul Traina * match, or not as good as the one we already have. 18858f0484fSRodney W. Grimes */ 189ef5d438eSPaul Traina if ((e.page = mpool_get(t->bt_mp, h->prevpg, 0)) == NULL) 190ef5d438eSPaul Traina return (0); 191ef5d438eSPaul Traina e.index = NEXTINDEX(e.page) - 1; 19258f0484fSRodney W. Grimes if (__bt_cmp(t, key, &e) == 0) { 19358f0484fSRodney W. Grimes mpool_put(t->bt_mp, h, 0); 19458f0484fSRodney W. Grimes t->bt_cur = e; 19558f0484fSRodney W. Grimes *exactp = 1; 19658f0484fSRodney W. Grimes return (1); 19758f0484fSRodney W. Grimes } 198ef5d438eSPaul Traina mpool_put(t->bt_mp, e.page, 0); 19958f0484fSRodney W. Grimes return (0); 20058f0484fSRodney W. Grimes } 201