xref: /csrg-svn/lib/libc/db/btree/bt_search.c (revision 57932)
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*57932Sbostic static char sccsid[] = "@(#)bt_search.c	5.8 (Berkeley) 02/11/93";
1346132Smao #endif /* LIBC_SCCS and not lint */
1446132Smao 
1546132Smao #include <sys/types.h>
1656739Sbostic 
1750989Sbostic #include <stdio.h>
1856739Sbostic 
19*57932Sbostic #include <db.h>
2046132Smao #include "btree.h"
2146132Smao 
2246132Smao /*
2350989Sbostic  * __BT_SEARCH -- Search a btree for a key.
2446132Smao  *
2550989Sbostic  * Parameters:
2650989Sbostic  *	t:	tree to search
2750989Sbostic  *	key:	key to find
2850989Sbostic  *	exactp:	pointer to exact match flag
2946132Smao  *
3050989Sbostic  * Returns:
3150989Sbostic  *	EPG for matching record, if any, or the EPG for the location of the
3250989Sbostic  *	key, if it were inserted into the tree.
3346132Smao  *
3450989Sbostic  * Warnings:
3550989Sbostic  *	The EPG returned is in static memory, and will be overwritten by the
3650989Sbostic  *	next search of any kind in any tree.
3746132Smao  */
3850989Sbostic EPG *
3950989Sbostic __bt_search(t, key, exactp)
4050989Sbostic 	BTREE *t;
4150989Sbostic 	const DBT *key;
4250989Sbostic 	int *exactp;
4346132Smao {
4450989Sbostic 	register index_t index;
4550989Sbostic 	register int base, cmp, lim;
4650989Sbostic 	register PAGE *h;
4750989Sbostic 	pgno_t pg;
4850989Sbostic 	static EPG e;
4946132Smao 
5057449Sbostic 	BT_CLR(t);
5150989Sbostic 	for (pg = P_ROOT;;) {
5250989Sbostic 		if ((h = mpool_get(t->bt_mp, pg, 0)) == NULL)
5350989Sbostic 			return (NULL);
5446132Smao 
5550989Sbostic 		/* Do a binary search on the current page. */
5650989Sbostic 		e.page = h;
5750989Sbostic 		for (base = 0, lim = NEXTINDEX(h); lim; lim >>= 1) {
5850989Sbostic 			e.index = index = base + (lim >> 1);
5950989Sbostic 			if ((cmp = __bt_cmp(t, key, &e)) == 0) {
6050989Sbostic 				if (h->flags & P_BLEAF) {
6150989Sbostic 					*exactp = 1;
6250989Sbostic 					return (&e);
6350989Sbostic 				}
6450989Sbostic 				goto next;
6546132Smao 			}
6650989Sbostic 			if (cmp > 0) {
6750989Sbostic 				base = index + 1;
6850989Sbostic 				--lim;
6946132Smao 			}
7046132Smao 		}
7146132Smao 
7250989Sbostic 		/* If it's a leaf page, we're done. */
7350989Sbostic 		if (h->flags & P_BLEAF) {
7457448Sbostic 			e.index = base;
7550989Sbostic 			*exactp = 0;
7650989Sbostic 			return (&e);
7746132Smao 		}
7846132Smao 
7957448Sbostic 		/*
8057448Sbostic 		 * No match found.  Base is the smallest index greater than
8157448Sbostic 		 * key and may be zero or a last + 1 index.  If it's non-zero,
8257448Sbostic 		 * decrement by one, and record the internal page which should
8357448Sbostic 		 * be a parent page for the key.  If a split later occurs, the
8457448Sbostic 		 * inserted page will be to the right of the saved page.
8557448Sbostic 		 */
8657448Sbostic 		index = base ? base - 1 : base;
8757448Sbostic 
8851105Sbostic next:		if (__bt_push(t, h->pgno, index) == RET_ERROR)
8950989Sbostic 			return (NULL);
9050989Sbostic 		pg = GETBINTERNAL(h, index)->pgno;
9150989Sbostic 		mpool_put(t->bt_mp, h, 0);
9246132Smao 	}
9346132Smao }
94