xref: /freebsd/lib/libc/db/btree/bt_search.c (revision 58f0484fa251c266ede97b591b499fe3dd4f578e)
158f0484fSRodney W. Grimes /*-
258f0484fSRodney W. Grimes  * Copyright (c) 1990, 1993
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  * 3. All advertising materials mentioning features or use of this software
1758f0484fSRodney W. Grimes  *    must display the following acknowledgement:
1858f0484fSRodney W. Grimes  *	This product includes software developed by the University of
1958f0484fSRodney W. Grimes  *	California, Berkeley and its contributors.
2058f0484fSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
2158f0484fSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
2258f0484fSRodney W. Grimes  *    without specific prior written permission.
2358f0484fSRodney W. Grimes  *
2458f0484fSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2558f0484fSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2658f0484fSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2758f0484fSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2858f0484fSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2958f0484fSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3058f0484fSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3158f0484fSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3258f0484fSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3358f0484fSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3458f0484fSRodney W. Grimes  * SUCH DAMAGE.
3558f0484fSRodney W. Grimes  */
3658f0484fSRodney W. Grimes 
3758f0484fSRodney W. Grimes #if defined(LIBC_SCCS) && !defined(lint)
3858f0484fSRodney W. Grimes static char sccsid[] = "@(#)bt_search.c	8.6 (Berkeley) 3/15/94";
3958f0484fSRodney W. Grimes #endif /* LIBC_SCCS and not lint */
4058f0484fSRodney W. Grimes 
4158f0484fSRodney W. Grimes #include <sys/types.h>
4258f0484fSRodney W. Grimes 
4358f0484fSRodney W. Grimes #include <stdio.h>
4458f0484fSRodney W. Grimes 
4558f0484fSRodney W. Grimes #include <db.h>
4658f0484fSRodney W. Grimes #include "btree.h"
4758f0484fSRodney W. Grimes 
4858f0484fSRodney W. Grimes static int bt_snext __P((BTREE *, PAGE *, const DBT *, int *));
4958f0484fSRodney W. Grimes static int bt_sprev __P((BTREE *, PAGE *, const DBT *, int *));
5058f0484fSRodney W. Grimes 
5158f0484fSRodney W. Grimes /*
5258f0484fSRodney W. Grimes  * __BT_SEARCH -- Search a btree for a key.
5358f0484fSRodney W. Grimes  *
5458f0484fSRodney W. Grimes  * Parameters:
5558f0484fSRodney W. Grimes  *	t:	tree to search
5658f0484fSRodney W. Grimes  *	key:	key to find
5758f0484fSRodney W. Grimes  *	exactp:	pointer to exact match flag
5858f0484fSRodney W. Grimes  *
5958f0484fSRodney W. Grimes  * Returns:
6058f0484fSRodney W. Grimes  *	The EPG for matching record, if any, or the EPG for the location
6158f0484fSRodney W. Grimes  *	of the key, if it were inserted into the tree, is entered into
6258f0484fSRodney W. Grimes  *	the bt_cur field of the tree.  A pointer to the field is returned.
6358f0484fSRodney W. Grimes  */
6458f0484fSRodney W. Grimes EPG *
6558f0484fSRodney W. Grimes __bt_search(t, key, exactp)
6658f0484fSRodney W. Grimes 	BTREE *t;
6758f0484fSRodney W. Grimes 	const DBT *key;
6858f0484fSRodney W. Grimes 	int *exactp;
6958f0484fSRodney W. Grimes {
7058f0484fSRodney W. Grimes 	PAGE *h;
7158f0484fSRodney W. Grimes 	indx_t base, index, lim;
7258f0484fSRodney W. Grimes 	pgno_t pg;
7358f0484fSRodney W. Grimes 	int cmp;
7458f0484fSRodney W. Grimes 
7558f0484fSRodney W. Grimes 	BT_CLR(t);
7658f0484fSRodney W. Grimes 	for (pg = P_ROOT;;) {
7758f0484fSRodney W. Grimes 		if ((h = mpool_get(t->bt_mp, pg, 0)) == NULL)
7858f0484fSRodney W. Grimes 			return (NULL);
7958f0484fSRodney W. Grimes 
8058f0484fSRodney W. Grimes 		/* Do a binary search on the current page. */
8158f0484fSRodney W. Grimes 		t->bt_cur.page = h;
8258f0484fSRodney W. Grimes 		for (base = 0, lim = NEXTINDEX(h); lim; lim >>= 1) {
8358f0484fSRodney W. Grimes 			t->bt_cur.index = index = base + (lim >> 1);
8458f0484fSRodney W. Grimes 			if ((cmp = __bt_cmp(t, key, &t->bt_cur)) == 0) {
8558f0484fSRodney W. Grimes 				if (h->flags & P_BLEAF) {
8658f0484fSRodney W. Grimes 					*exactp = 1;
8758f0484fSRodney W. Grimes 					return (&t->bt_cur);
8858f0484fSRodney W. Grimes 				}
8958f0484fSRodney W. Grimes 				goto next;
9058f0484fSRodney W. Grimes 			}
9158f0484fSRodney W. Grimes 			if (cmp > 0) {
9258f0484fSRodney W. Grimes 				base = index + 1;
9358f0484fSRodney W. Grimes 				--lim;
9458f0484fSRodney W. Grimes 			}
9558f0484fSRodney W. Grimes 		}
9658f0484fSRodney W. Grimes 
9758f0484fSRodney W. Grimes 		/*
9858f0484fSRodney W. Grimes 		 * If it's a leaf page, and duplicates aren't allowed, we're
9958f0484fSRodney W. Grimes 		 * done.  If duplicates are allowed, it's possible that there
10058f0484fSRodney W. Grimes 		 * were duplicate keys on duplicate pages, and they were later
10158f0484fSRodney W. Grimes 		 * deleted, so we could be on a page with no matches while
10258f0484fSRodney W. Grimes 		 * there are matches on other pages.  If we're at the start or
10358f0484fSRodney W. Grimes 		 * end of a page, check on both sides.
10458f0484fSRodney W. Grimes 		 */
10558f0484fSRodney W. Grimes 		if (h->flags & P_BLEAF) {
10658f0484fSRodney W. Grimes 			t->bt_cur.index = base;
10758f0484fSRodney W. Grimes 			*exactp = 0;
10858f0484fSRodney W. Grimes 			if (!ISSET(t, B_NODUPS)) {
10958f0484fSRodney W. Grimes 				if (base == 0 &&
11058f0484fSRodney W. Grimes 				    bt_sprev(t, h, key, exactp))
11158f0484fSRodney W. Grimes 					return (&t->bt_cur);
11258f0484fSRodney W. Grimes 				if (base == NEXTINDEX(h) &&
11358f0484fSRodney W. Grimes 				    bt_snext(t, h, key, exactp))
11458f0484fSRodney W. Grimes 					return (&t->bt_cur);
11558f0484fSRodney W. Grimes 			}
11658f0484fSRodney W. Grimes 			return (&t->bt_cur);
11758f0484fSRodney W. Grimes 		}
11858f0484fSRodney W. Grimes 
11958f0484fSRodney W. Grimes 		/*
12058f0484fSRodney W. Grimes 		 * No match found.  Base is the smallest index greater than
12158f0484fSRodney W. Grimes 		 * key and may be zero or a last + 1 index.  If it's non-zero,
12258f0484fSRodney W. Grimes 		 * decrement by one, and record the internal page which should
12358f0484fSRodney W. Grimes 		 * be a parent page for the key.  If a split later occurs, the
12458f0484fSRodney W. Grimes 		 * inserted page will be to the right of the saved page.
12558f0484fSRodney W. Grimes 		 */
12658f0484fSRodney W. Grimes 		index = base ? base - 1 : base;
12758f0484fSRodney W. Grimes 
12858f0484fSRodney W. Grimes next:		if (__bt_push(t, h->pgno, index) == RET_ERROR)
12958f0484fSRodney W. Grimes 			return (NULL);
13058f0484fSRodney W. Grimes 		pg = GETBINTERNAL(h, index)->pgno;
13158f0484fSRodney W. Grimes 		mpool_put(t->bt_mp, h, 0);
13258f0484fSRodney W. Grimes 	}
13358f0484fSRodney W. Grimes }
13458f0484fSRodney W. Grimes 
13558f0484fSRodney W. Grimes /*
13658f0484fSRodney W. Grimes  * BT_SNEXT -- Check for an exact match after the key.
13758f0484fSRodney W. Grimes  *
13858f0484fSRodney W. Grimes  * Parameters:
13958f0484fSRodney W. Grimes  *	t:	tree to search
14058f0484fSRodney W. Grimes  *	h:	current page.
14158f0484fSRodney W. Grimes  *	key:	key to find
14258f0484fSRodney W. Grimes  *	exactp:	pointer to exact match flag
14358f0484fSRodney W. Grimes  *
14458f0484fSRodney W. Grimes  * Returns:
14558f0484fSRodney W. Grimes  *	If an exact match found.
14658f0484fSRodney W. Grimes  */
14758f0484fSRodney W. Grimes static int
14858f0484fSRodney W. Grimes bt_snext(t, h, key, exactp)
14958f0484fSRodney W. Grimes 	BTREE *t;
15058f0484fSRodney W. Grimes 	PAGE *h;
15158f0484fSRodney W. Grimes 	const DBT *key;
15258f0484fSRodney W. Grimes 	int *exactp;
15358f0484fSRodney W. Grimes {
15458f0484fSRodney W. Grimes 	EPG e;
15558f0484fSRodney W. Grimes 	PAGE *tp;
15658f0484fSRodney W. Grimes 	pgno_t pg;
15758f0484fSRodney W. Grimes 
15858f0484fSRodney W. Grimes 	/* Skip until reach the end of the tree or a key. */
15958f0484fSRodney W. Grimes 	for (pg = h->nextpg; pg != P_INVALID;) {
16058f0484fSRodney W. Grimes 		if ((tp = mpool_get(t->bt_mp, pg, 0)) == NULL) {
16158f0484fSRodney W. Grimes 			mpool_put(t->bt_mp, h, 0);
16258f0484fSRodney W. Grimes 			return (NULL);
16358f0484fSRodney W. Grimes 		}
16458f0484fSRodney W. Grimes 		if (NEXTINDEX(tp) != 0)
16558f0484fSRodney W. Grimes 			break;
16658f0484fSRodney W. Grimes 		pg = tp->prevpg;
16758f0484fSRodney W. Grimes 		mpool_put(t->bt_mp, tp, 0);
16858f0484fSRodney W. Grimes 	}
16958f0484fSRodney W. Grimes 	/*
17058f0484fSRodney W. Grimes 	 * The key is either an exact match, or not as good as
17158f0484fSRodney W. Grimes 	 * the one we already have.
17258f0484fSRodney W. Grimes 	 */
17358f0484fSRodney W. Grimes 	if (pg != P_INVALID) {
17458f0484fSRodney W. Grimes 		e.page = tp;
17558f0484fSRodney W. Grimes 		e.index = NEXTINDEX(tp) - 1;
17658f0484fSRodney W. Grimes 		if (__bt_cmp(t, key, &e) == 0) {
17758f0484fSRodney W. Grimes 			mpool_put(t->bt_mp, h, 0);
17858f0484fSRodney W. Grimes 			t->bt_cur = e;
17958f0484fSRodney W. Grimes 			*exactp = 1;
18058f0484fSRodney W. Grimes 			return (1);
18158f0484fSRodney W. Grimes 		}
18258f0484fSRodney W. Grimes 	}
18358f0484fSRodney W. Grimes 	return (0);
18458f0484fSRodney W. Grimes }
18558f0484fSRodney W. Grimes 
18658f0484fSRodney W. Grimes /*
18758f0484fSRodney W. Grimes  * BT_SPREV -- Check for an exact match before the key.
18858f0484fSRodney W. Grimes  *
18958f0484fSRodney W. Grimes  * Parameters:
19058f0484fSRodney W. Grimes  *	t:	tree to search
19158f0484fSRodney W. Grimes  *	h:	current page.
19258f0484fSRodney W. Grimes  *	key:	key to find
19358f0484fSRodney W. Grimes  *	exactp:	pointer to exact match flag
19458f0484fSRodney W. Grimes  *
19558f0484fSRodney W. Grimes  * Returns:
19658f0484fSRodney W. Grimes  *	If an exact match found.
19758f0484fSRodney W. Grimes  */
19858f0484fSRodney W. Grimes static int
19958f0484fSRodney W. Grimes bt_sprev(t, h, key, exactp)
20058f0484fSRodney W. Grimes 	BTREE *t;
20158f0484fSRodney W. Grimes 	PAGE *h;
20258f0484fSRodney W. Grimes 	const DBT *key;
20358f0484fSRodney W. Grimes 	int *exactp;
20458f0484fSRodney W. Grimes {
20558f0484fSRodney W. Grimes 	EPG e;
20658f0484fSRodney W. Grimes 	PAGE *tp;
20758f0484fSRodney W. Grimes 	pgno_t pg;
20858f0484fSRodney W. Grimes 
20958f0484fSRodney W. Grimes 	/* Skip until reach the beginning of the tree or a key. */
21058f0484fSRodney W. Grimes 	for (pg = h->prevpg; pg != P_INVALID;) {
21158f0484fSRodney W. Grimes 		if ((tp = mpool_get(t->bt_mp, pg, 0)) == NULL) {
21258f0484fSRodney W. Grimes 			mpool_put(t->bt_mp, h, 0);
21358f0484fSRodney W. Grimes 			return (NULL);
21458f0484fSRodney W. Grimes 		}
21558f0484fSRodney W. Grimes 		if (NEXTINDEX(tp) != 0)
21658f0484fSRodney W. Grimes 			break;
21758f0484fSRodney W. Grimes 		pg = tp->prevpg;
21858f0484fSRodney W. Grimes 		mpool_put(t->bt_mp, tp, 0);
21958f0484fSRodney W. Grimes 	}
22058f0484fSRodney W. Grimes 	/*
22158f0484fSRodney W. Grimes 	 * The key is either an exact match, or not as good as
22258f0484fSRodney W. Grimes 	 * the one we already have.
22358f0484fSRodney W. Grimes 	 */
22458f0484fSRodney W. Grimes 	if (pg != P_INVALID) {
22558f0484fSRodney W. Grimes 		e.page = tp;
22658f0484fSRodney W. Grimes 		e.index = NEXTINDEX(tp) - 1;
22758f0484fSRodney W. Grimes 		if (__bt_cmp(t, key, &e) == 0) {
22858f0484fSRodney W. Grimes 			mpool_put(t->bt_mp, h, 0);
22958f0484fSRodney W. Grimes 			t->bt_cur = e;
23058f0484fSRodney W. Grimes 			*exactp = 1;
23158f0484fSRodney W. Grimes 			return (1);
23258f0484fSRodney W. Grimes 		}
23358f0484fSRodney W. Grimes 	}
23458f0484fSRodney W. Grimes 	return (0);
23558f0484fSRodney W. Grimes }
236