xref: /netbsd/lib/libc/db/btree/bt_search.c (revision bf9ec67e)
1 /*	$NetBSD: bt_search.c,v 1.12 2001/12/30 19:18:23 thorpej Exp $	*/
2 
3 /*-
4  * Copyright (c) 1990, 1993, 1994
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Mike Olson.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed by the University of
21  *	California, Berkeley and its contributors.
22  * 4. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  */
38 
39 #include <sys/cdefs.h>
40 #if defined(LIBC_SCCS) && !defined(lint)
41 #if 0
42 static char sccsid[] = "@(#)bt_search.c	8.8 (Berkeley) 7/31/94";
43 #else
44 __RCSID("$NetBSD: bt_search.c,v 1.12 2001/12/30 19:18:23 thorpej Exp $");
45 #endif
46 #endif /* LIBC_SCCS and not lint */
47 
48 #include "namespace.h"
49 #include <sys/types.h>
50 
51 #include <stdio.h>
52 
53 #include <db.h>
54 #include "btree.h"
55 
56 static int __bt_snext __P((BTREE *, PAGE *, const DBT *, int *));
57 static int __bt_sprev __P((BTREE *, PAGE *, const DBT *, int *));
58 
59 /*
60  * __bt_search --
61  *	Search a btree for a key.
62  *
63  * Parameters:
64  *	t:	tree to search
65  *	key:	key to find
66  *	exactp:	pointer to exact match flag
67  *
68  * Returns:
69  *	The EPG for matching record, if any, or the EPG for the location
70  *	of the key, if it were inserted into the tree, is entered into
71  *	the bt_cur field of the tree.  A pointer to the field is returned.
72  */
73 EPG *
74 __bt_search(t, key, exactp)
75 	BTREE *t;
76 	const DBT *key;
77 	int *exactp;
78 {
79 	PAGE *h;
80 	indx_t base, idx, lim;
81 	pgno_t pg;
82 	int cmp;
83 
84 	BT_CLR(t);
85 	for (pg = P_ROOT;;) {
86 		if ((h = mpool_get(t->bt_mp, pg, 0)) == NULL)
87 			return (NULL);
88 
89 		/* Do a binary search on the current page. */
90 		t->bt_cur.page = h;
91 		for (base = 0, lim = NEXTINDEX(h); lim; lim >>= 1) {
92 			t->bt_cur.index = idx = base + ((u_int32_t)lim >> 1);
93 			if ((cmp = __bt_cmp(t, key, &t->bt_cur)) == 0) {
94 				if (h->flags & P_BLEAF) {
95 					*exactp = 1;
96 					return (&t->bt_cur);
97 				}
98 				goto next;
99 			}
100 			if (cmp > 0) {
101 				base = idx + 1;
102 				--lim;
103 			}
104 		}
105 
106 		/*
107 		 * If it's a leaf page, we're almost done.  If no duplicates
108 		 * are allowed, or we have an exact match, we're done.  Else,
109 		 * it's possible that there were matching keys on this page,
110 		 * which later deleted, and we're on a page with no matches
111 		 * while there are matches on other pages.  If at the start or
112 		 * end of a page, check the adjacent page.
113 		 */
114 		if (h->flags & P_BLEAF) {
115 			if (!F_ISSET(t, B_NODUPS)) {
116 				if (base == 0 &&
117 				    h->prevpg != P_INVALID &&
118 				    __bt_sprev(t, h, key, exactp))
119 					return (&t->bt_cur);
120 				if (base == NEXTINDEX(h) &&
121 				    h->nextpg != P_INVALID &&
122 				    __bt_snext(t, h, key, exactp))
123 					return (&t->bt_cur);
124 			}
125 			*exactp = 0;
126 			t->bt_cur.index = base;
127 			return (&t->bt_cur);
128 		}
129 
130 		/*
131 		 * No match found.  Base is the smallest index greater than
132 		 * key and may be zero or a last + 1 index.  If it's non-zero,
133 		 * decrement by one, and record the internal page which should
134 		 * be a parent page for the key.  If a split later occurs, the
135 		 * inserted page will be to the right of the saved page.
136 		 */
137 		idx = base ? base - 1 : base;
138 
139 next:		BT_PUSH(t, h->pgno, idx);
140 		pg = GETBINTERNAL(h, idx)->pgno;
141 		mpool_put(t->bt_mp, h, 0);
142 	}
143 }
144 
145 /*
146  * __bt_snext --
147  *	Check for an exact match after the key.
148  *
149  * Parameters:
150  *	t:	tree
151  *	h:	current page
152  *	key:	key
153  *	exactp:	pointer to exact match flag
154  *
155  * Returns:
156  *	If an exact match found.
157  */
158 static int
159 __bt_snext(t, h, key, exactp)
160 	BTREE *t;
161 	PAGE *h;
162 	const DBT *key;
163 	int *exactp;
164 {
165 	EPG e;
166 
167 	/*
168 	 * Get the next page.  The key is either an exact
169 	 * match, or not as good as the one we already have.
170 	 */
171 	if ((e.page = mpool_get(t->bt_mp, h->nextpg, 0)) == NULL)
172 		return (0);
173 	e.index = 0;
174 	if (__bt_cmp(t, key, &e) == 0) {
175 		mpool_put(t->bt_mp, h, 0);
176 		t->bt_cur = e;
177 		*exactp = 1;
178 		return (1);
179 	}
180 	mpool_put(t->bt_mp, e.page, 0);
181 	return (0);
182 }
183 
184 /*
185  * __bt_sprev --
186  *	Check for an exact match before the key.
187  *
188  * Parameters:
189  *	t:	tree
190  *	h:	current page
191  *	key:	key
192  *	exactp:	pointer to exact match flag
193  *
194  * Returns:
195  *	If an exact match found.
196  */
197 static int
198 __bt_sprev(t, h, key, exactp)
199 	BTREE *t;
200 	PAGE *h;
201 	const DBT *key;
202 	int *exactp;
203 {
204 	EPG e;
205 
206 	/*
207 	 * Get the previous page.  The key is either an exact
208 	 * match, or not as good as the one we already have.
209 	 */
210 	if ((e.page = mpool_get(t->bt_mp, h->prevpg, 0)) == NULL)
211 		return (0);
212 	e.index = NEXTINDEX(e.page) - 1;
213 	if (__bt_cmp(t, key, &e) == 0) {
214 		mpool_put(t->bt_mp, h, 0);
215 		t->bt_cur = e;
216 		*exactp = 1;
217 		return (1);
218 	}
219 	mpool_put(t->bt_mp, e.page, 0);
220 	return (0);
221 }
222