xref: /freebsd/lib/libc/db/btree/bt_search.c (revision dc36d6f9bb1753f3808552f3afd30eda9a7b206a)
158f0484fSRodney W. Grimes /*-
2*8a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
3*8a16b7a1SPedro F. Giffuni  *
4ef5d438eSPaul Traina  * Copyright (c) 1990, 1993, 1994
558f0484fSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
658f0484fSRodney W. Grimes  *
758f0484fSRodney W. Grimes  * This code is derived from software contributed to Berkeley by
858f0484fSRodney W. Grimes  * Mike Olson.
958f0484fSRodney W. Grimes  *
1058f0484fSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
1158f0484fSRodney W. Grimes  * modification, are permitted provided that the following conditions
1258f0484fSRodney W. Grimes  * are met:
1358f0484fSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
1458f0484fSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
1558f0484fSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
1658f0484fSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
1758f0484fSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
18fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
1958f0484fSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
2058f0484fSRodney W. Grimes  *    without specific prior written permission.
2158f0484fSRodney W. Grimes  *
2258f0484fSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2358f0484fSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2458f0484fSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2558f0484fSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2658f0484fSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2758f0484fSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2858f0484fSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2958f0484fSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3058f0484fSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3158f0484fSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3258f0484fSRodney W. Grimes  * SUCH DAMAGE.
3358f0484fSRodney W. Grimes  */
3458f0484fSRodney W. Grimes 
3558f0484fSRodney W. Grimes #include <sys/types.h>
3658f0484fSRodney W. Grimes 
3758f0484fSRodney W. Grimes #include <stdio.h>
3858f0484fSRodney W. Grimes 
3958f0484fSRodney W. Grimes #include <db.h>
4058f0484fSRodney W. Grimes #include "btree.h"
4158f0484fSRodney W. Grimes 
42c05ac53bSDavid E. O'Brien static int __bt_snext(BTREE *, PAGE *, const DBT *, int *);
43c05ac53bSDavid E. O'Brien static int __bt_sprev(BTREE *, PAGE *, const DBT *, int *);
4458f0484fSRodney W. Grimes 
4558f0484fSRodney W. Grimes /*
46ef5d438eSPaul Traina  * __bt_search --
47ef5d438eSPaul Traina  *	Search a btree for a key.
4858f0484fSRodney W. Grimes  *
4958f0484fSRodney W. Grimes  * Parameters:
5058f0484fSRodney W. Grimes  *	t:	tree to search
5158f0484fSRodney W. Grimes  *	key:	key to find
5258f0484fSRodney W. Grimes  *	exactp:	pointer to exact match flag
5358f0484fSRodney W. Grimes  *
5458f0484fSRodney W. Grimes  * Returns:
5558f0484fSRodney W. Grimes  *	The EPG for matching record, if any, or the EPG for the location
5658f0484fSRodney W. Grimes  *	of the key, if it were inserted into the tree, is entered into
5758f0484fSRodney W. Grimes  *	the bt_cur field of the tree.  A pointer to the field is returned.
5858f0484fSRodney W. Grimes  */
5958f0484fSRodney W. Grimes EPG *
__bt_search(BTREE * t,const DBT * key,int * exactp)600ac22237SXin LI __bt_search(BTREE *t, const DBT *key, int *exactp)
6158f0484fSRodney W. Grimes {
6258f0484fSRodney W. Grimes 	PAGE *h;
634c66e4b6SXin LI 	indx_t base, idx, lim;
6458f0484fSRodney W. Grimes 	pgno_t pg;
6558f0484fSRodney W. Grimes 	int cmp;
6658f0484fSRodney W. Grimes 
6758f0484fSRodney W. Grimes 	BT_CLR(t);
6858f0484fSRodney W. Grimes 	for (pg = P_ROOT;;) {
6958f0484fSRodney W. Grimes 		if ((h = mpool_get(t->bt_mp, pg, 0)) == NULL)
7058f0484fSRodney W. Grimes 			return (NULL);
7158f0484fSRodney W. Grimes 
7258f0484fSRodney W. Grimes 		/* Do a binary search on the current page. */
7358f0484fSRodney W. Grimes 		t->bt_cur.page = h;
7458f0484fSRodney W. Grimes 		for (base = 0, lim = NEXTINDEX(h); lim; lim >>= 1) {
754c66e4b6SXin LI 			t->bt_cur.index = idx = base + (lim >> 1);
7658f0484fSRodney W. Grimes 			if ((cmp = __bt_cmp(t, key, &t->bt_cur)) == 0) {
7758f0484fSRodney W. Grimes 				if (h->flags & P_BLEAF) {
7858f0484fSRodney W. Grimes 					*exactp = 1;
7958f0484fSRodney W. Grimes 					return (&t->bt_cur);
8058f0484fSRodney W. Grimes 				}
8158f0484fSRodney W. Grimes 				goto next;
8258f0484fSRodney W. Grimes 			}
8358f0484fSRodney W. Grimes 			if (cmp > 0) {
844c66e4b6SXin LI 				base = idx + 1;
8558f0484fSRodney W. Grimes 				--lim;
8658f0484fSRodney W. Grimes 			}
8758f0484fSRodney W. Grimes 		}
8858f0484fSRodney W. Grimes 
8958f0484fSRodney W. Grimes 		/*
90ef5d438eSPaul Traina 		 * If it's a leaf page, we're almost done.  If no duplicates
91ef5d438eSPaul Traina 		 * are allowed, or we have an exact match, we're done.  Else,
92ef5d438eSPaul Traina 		 * it's possible that there were matching keys on this page,
93ef5d438eSPaul Traina 		 * which later deleted, and we're on a page with no matches
94ef5d438eSPaul Traina 		 * while there are matches on other pages.  If at the start or
95ef5d438eSPaul Traina 		 * end of a page, check the adjacent page.
9658f0484fSRodney W. Grimes 		 */
9758f0484fSRodney W. Grimes 		if (h->flags & P_BLEAF) {
98ef5d438eSPaul Traina 			if (!F_ISSET(t, B_NODUPS)) {
9958f0484fSRodney W. Grimes 				if (base == 0 &&
100ef5d438eSPaul Traina 				    h->prevpg != P_INVALID &&
101ef5d438eSPaul Traina 				    __bt_sprev(t, h, key, exactp))
10258f0484fSRodney W. Grimes 					return (&t->bt_cur);
10358f0484fSRodney W. Grimes 				if (base == NEXTINDEX(h) &&
104ef5d438eSPaul Traina 				    h->nextpg != P_INVALID &&
105ef5d438eSPaul Traina 				    __bt_snext(t, h, key, exactp))
10658f0484fSRodney W. Grimes 					return (&t->bt_cur);
10758f0484fSRodney W. Grimes 			}
108ef5d438eSPaul Traina 			*exactp = 0;
109ef5d438eSPaul Traina 			t->bt_cur.index = base;
11058f0484fSRodney W. Grimes 			return (&t->bt_cur);
11158f0484fSRodney W. Grimes 		}
11258f0484fSRodney W. Grimes 
11358f0484fSRodney W. Grimes 		/*
11458f0484fSRodney W. Grimes 		 * No match found.  Base is the smallest index greater than
11558f0484fSRodney W. Grimes 		 * key and may be zero or a last + 1 index.  If it's non-zero,
11658f0484fSRodney W. Grimes 		 * decrement by one, and record the internal page which should
11758f0484fSRodney W. Grimes 		 * be a parent page for the key.  If a split later occurs, the
11858f0484fSRodney W. Grimes 		 * inserted page will be to the right of the saved page.
11958f0484fSRodney W. Grimes 		 */
1204c66e4b6SXin LI 		idx = base ? base - 1 : base;
12158f0484fSRodney W. Grimes 
1224c66e4b6SXin LI next:		BT_PUSH(t, h->pgno, idx);
1234c66e4b6SXin LI 		pg = GETBINTERNAL(h, idx)->pgno;
12458f0484fSRodney W. Grimes 		mpool_put(t->bt_mp, h, 0);
12558f0484fSRodney W. Grimes 	}
12658f0484fSRodney W. Grimes }
12758f0484fSRodney W. Grimes 
12858f0484fSRodney W. Grimes /*
129ef5d438eSPaul Traina  * __bt_snext --
130ef5d438eSPaul Traina  *	Check for an exact match after the key.
13158f0484fSRodney W. Grimes  *
13258f0484fSRodney W. Grimes  * Parameters:
133ef5d438eSPaul Traina  *	t:	tree
134ef5d438eSPaul Traina  *	h:	current page
135ef5d438eSPaul Traina  *	key:	key
13658f0484fSRodney W. Grimes  *	exactp:	pointer to exact match flag
13758f0484fSRodney W. Grimes  *
13858f0484fSRodney W. Grimes  * Returns:
13958f0484fSRodney W. Grimes  *	If an exact match found.
14058f0484fSRodney W. Grimes  */
14158f0484fSRodney W. Grimes static int
__bt_snext(BTREE * t,PAGE * h,const DBT * key,int * exactp)1420ac22237SXin LI __bt_snext(BTREE *t, PAGE *h, const DBT *key, int *exactp)
14358f0484fSRodney W. Grimes {
14458f0484fSRodney W. Grimes 	EPG e;
14558f0484fSRodney W. Grimes 
14658f0484fSRodney W. Grimes 	/*
147ef5d438eSPaul Traina 	 * Get the next page.  The key is either an exact
148ef5d438eSPaul Traina 	 * match, or not as good as the one we already have.
14958f0484fSRodney W. Grimes 	 */
150ef5d438eSPaul Traina 	if ((e.page = mpool_get(t->bt_mp, h->nextpg, 0)) == NULL)
151ef5d438eSPaul Traina 		return (0);
152ef5d438eSPaul Traina 	e.index = 0;
15358f0484fSRodney W. Grimes 	if (__bt_cmp(t, key, &e) == 0) {
15458f0484fSRodney W. Grimes 		mpool_put(t->bt_mp, h, 0);
15558f0484fSRodney W. Grimes 		t->bt_cur = e;
15658f0484fSRodney W. Grimes 		*exactp = 1;
15758f0484fSRodney W. Grimes 		return (1);
15858f0484fSRodney W. Grimes 	}
159ef5d438eSPaul Traina 	mpool_put(t->bt_mp, e.page, 0);
16058f0484fSRodney W. Grimes 	return (0);
16158f0484fSRodney W. Grimes }
16258f0484fSRodney W. Grimes 
16358f0484fSRodney W. Grimes /*
164ef5d438eSPaul Traina  * __bt_sprev --
165ef5d438eSPaul Traina  *	Check for an exact match before the key.
16658f0484fSRodney W. Grimes  *
16758f0484fSRodney W. Grimes  * Parameters:
168ef5d438eSPaul Traina  *	t:	tree
169ef5d438eSPaul Traina  *	h:	current page
170ef5d438eSPaul Traina  *	key:	key
17158f0484fSRodney W. Grimes  *	exactp:	pointer to exact match flag
17258f0484fSRodney W. Grimes  *
17358f0484fSRodney W. Grimes  * Returns:
17458f0484fSRodney W. Grimes  *	If an exact match found.
17558f0484fSRodney W. Grimes  */
17658f0484fSRodney W. Grimes static int
__bt_sprev(BTREE * t,PAGE * h,const DBT * key,int * exactp)1770ac22237SXin LI __bt_sprev(BTREE *t, PAGE *h, const DBT *key, int *exactp)
17858f0484fSRodney W. Grimes {
17958f0484fSRodney W. Grimes 	EPG e;
18058f0484fSRodney W. Grimes 
18158f0484fSRodney W. Grimes 	/*
182ef5d438eSPaul Traina 	 * Get the previous page.  The key is either an exact
183ef5d438eSPaul Traina 	 * match, or not as good as the one we already have.
18458f0484fSRodney W. Grimes 	 */
185ef5d438eSPaul Traina 	if ((e.page = mpool_get(t->bt_mp, h->prevpg, 0)) == NULL)
186ef5d438eSPaul Traina 		return (0);
187ef5d438eSPaul Traina 	e.index = NEXTINDEX(e.page) - 1;
18858f0484fSRodney W. Grimes 	if (__bt_cmp(t, key, &e) == 0) {
18958f0484fSRodney W. Grimes 		mpool_put(t->bt_mp, h, 0);
19058f0484fSRodney W. Grimes 		t->bt_cur = e;
19158f0484fSRodney W. Grimes 		*exactp = 1;
19258f0484fSRodney W. Grimes 		return (1);
19358f0484fSRodney W. Grimes 	}
194ef5d438eSPaul Traina 	mpool_put(t->bt_mp, e.page, 0);
19558f0484fSRodney W. Grimes 	return (0);
19658f0484fSRodney W. Grimes }
197