xref: /csrg-svn/lib/libc/db/btree/bt_search.c (revision 51105)
146132Smao /*-
246132Smao  * Copyright (c) 1990 The Regents of the University of California.
346132Smao  * All rights reserved.
446132Smao  *
546132Smao  * This code is derived from software contributed to Berkeley by
646132Smao  * Mike Olson.
746132Smao  *
846132Smao  * %sccs.include.redist.c%
946132Smao  */
1046132Smao 
1146132Smao #if defined(LIBC_SCCS) && !defined(lint)
12*51105Sbostic static char sccsid[] = "@(#)bt_search.c	5.4 (Berkeley) 09/12/91";
1346132Smao #endif /* LIBC_SCCS and not lint */
1446132Smao 
1546132Smao #include <sys/types.h>
1646132Smao #include <db.h>
1750989Sbostic #include <stdio.h>
1846132Smao #include "btree.h"
1946132Smao 
2046132Smao /*
2150989Sbostic  * __BT_SEARCH -- Search a btree for a key.
2246132Smao  *
2350989Sbostic  * Parameters:
2450989Sbostic  *	t:	tree to search
2550989Sbostic  *	key:	key to find
2650989Sbostic  *	exactp:	pointer to exact match flag
2746132Smao  *
2850989Sbostic  * Returns:
2950989Sbostic  *	EPG for matching record, if any, or the EPG for the location of the
3050989Sbostic  *	key, if it were inserted into the tree.
3146132Smao  *
3250989Sbostic  * Warnings:
3350989Sbostic  *	The EPG returned is in static memory, and will be overwritten by the
3450989Sbostic  *	next search of any kind in any tree.
3546132Smao  */
3650989Sbostic EPG *
3750989Sbostic __bt_search(t, key, exactp)
3850989Sbostic 	BTREE *t;
3950989Sbostic 	const DBT *key;
4050989Sbostic 	int *exactp;
4146132Smao {
4250989Sbostic 	register index_t index;
4350989Sbostic 	register int base, cmp, lim;
4450989Sbostic 	register PAGE *h;
4550989Sbostic 	pgno_t pg;
4650989Sbostic 	static EPG e;
4746132Smao 
4850989Sbostic 	for (pg = P_ROOT;;) {
4950989Sbostic 		if ((h = mpool_get(t->bt_mp, pg, 0)) == NULL)
5050989Sbostic 			return (NULL);
5146132Smao 
5250989Sbostic 		/* Do a binary search on the current page. */
5350989Sbostic 		e.page = h;
5450989Sbostic 		for (base = 0, lim = NEXTINDEX(h); lim; lim >>= 1) {
5550989Sbostic 			e.index = index = base + (lim >> 1);
5650989Sbostic 			if ((cmp = __bt_cmp(t, key, &e)) == 0) {
5750989Sbostic 				if (h->flags & P_BLEAF) {
5850989Sbostic 					*exactp = 1;
5950989Sbostic 					return (&e);
6050989Sbostic 				}
6150989Sbostic 				goto next;
6246132Smao 			}
6350989Sbostic 			if (cmp > 0) {
6450989Sbostic 				base = index + 1;
6550989Sbostic 				--lim;
6646132Smao 			}
6746132Smao 		}
6846132Smao 
6950989Sbostic 		/*
7050989Sbostic 		 * No match found.  Base is the smallest index greater than
7150989Sbostic 		 * key but may be an illegal index.  Use base if it's a leaf
7250989Sbostic 		 * page, decrement it by one if it's an internal page.  This
7350989Sbostic 		 * is safe because internal pages can't be empty.
7450989Sbostic 		 */
7550989Sbostic 		index = h->flags & P_BLEAF ? base : base - 1;
7650989Sbostic 
7750989Sbostic 		/* If it's a leaf page, we're done. */
7850989Sbostic 		if (h->flags & P_BLEAF) {
7950989Sbostic 			e.index = index;
8050989Sbostic 			*exactp = 0;
8150989Sbostic 			return (&e);
8246132Smao 		}
8346132Smao 
84*51105Sbostic next:		if (__bt_push(t, h->pgno, index) == RET_ERROR)
8550989Sbostic 			return (NULL);
8650989Sbostic 		pg = GETBINTERNAL(h, index)->pgno;
8750989Sbostic 		mpool_put(t->bt_mp, h, 0);
8846132Smao 	}
8946132Smao }
90