xref: /openbsd-src/lib/libc/db/btree/bt_search.c (revision b2ea75c1b17e1a9a339660e7ed45cd24946b230e)
1 /*	$OpenBSD: bt_search.c,v 1.4 1999/02/15 05:11:23 millert 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 #if defined(LIBC_SCCS) && !defined(lint)
40 #if 0
41 static char sccsid[] = "@(#)bt_search.c	8.8 (Berkeley) 7/31/94";
42 #else
43 static char rcsid[] = "$OpenBSD: bt_search.c,v 1.4 1999/02/15 05:11:23 millert Exp $";
44 #endif
45 #endif /* LIBC_SCCS and not lint */
46 
47 #include <sys/types.h>
48 
49 #include <stdio.h>
50 
51 #include <db.h>
52 #include "btree.h"
53 
54 static int __bt_snext __P((BTREE *, PAGE *, const DBT *, int *));
55 static int __bt_sprev __P((BTREE *, PAGE *, const DBT *, int *));
56 
57 /*
58  * __bt_search --
59  *	Search a btree for a key.
60  *
61  * Parameters:
62  *	t:	tree to search
63  *	key:	key to find
64  *	exactp:	pointer to exact match flag
65  *
66  * Returns:
67  *	The EPG for matching record, if any, or the EPG for the location
68  *	of the key, if it were inserted into the tree, is entered into
69  *	the bt_cur field of the tree.  A pointer to the field is returned.
70  */
71 EPG *
72 __bt_search(t, key, exactp)
73 	BTREE *t;
74 	const DBT *key;
75 	int *exactp;
76 {
77 	PAGE *h;
78 	indx_t base, index, lim;
79 	pgno_t pg;
80 	int cmp;
81 
82 	BT_CLR(t);
83 	for (pg = P_ROOT;;) {
84 		if ((h = mpool_get(t->bt_mp, pg, 0)) == NULL)
85 			return (NULL);
86 
87 		/* Do a binary search on the current page. */
88 		t->bt_cur.page = h;
89 		for (base = 0, lim = NEXTINDEX(h); lim; lim >>= 1) {
90 			t->bt_cur.index = index = base + (lim >> 1);
91 			if ((cmp = __bt_cmp(t, key, &t->bt_cur)) == 0) {
92 				if (h->flags & P_BLEAF) {
93 					*exactp = 1;
94 					return (&t->bt_cur);
95 				}
96 				goto next;
97 			}
98 			if (cmp > 0) {
99 				base = index + 1;
100 				--lim;
101 			}
102 		}
103 
104 		/*
105 		 * If it's a leaf page, we're almost done.  If no duplicates
106 		 * are allowed, or we have an exact match, we're done.  Else,
107 		 * it's possible that there were matching keys on this page,
108 		 * which later deleted, and we're on a page with no matches
109 		 * while there are matches on other pages.  If at the start or
110 		 * end of a page, check the adjacent page.
111 		 */
112 		if (h->flags & P_BLEAF) {
113 			if (!F_ISSET(t, B_NODUPS)) {
114 				if (base == 0 &&
115 				    h->prevpg != P_INVALID &&
116 				    __bt_sprev(t, h, key, exactp))
117 					return (&t->bt_cur);
118 				if (base == NEXTINDEX(h) &&
119 				    h->nextpg != P_INVALID &&
120 				    __bt_snext(t, h, key, exactp))
121 					return (&t->bt_cur);
122 			}
123 			*exactp = 0;
124 			t->bt_cur.index = base;
125 			return (&t->bt_cur);
126 		}
127 
128 		/*
129 		 * No match found.  Base is the smallest index greater than
130 		 * key and may be zero or a last + 1 index.  If it's non-zero,
131 		 * decrement by one, and record the internal page which should
132 		 * be a parent page for the key.  If a split later occurs, the
133 		 * inserted page will be to the right of the saved page.
134 		 */
135 		index = base ? base - 1 : base;
136 
137 next:		BT_PUSH(t, h->pgno, index);
138 		pg = GETBINTERNAL(h, index)->pgno;
139 		mpool_put(t->bt_mp, h, 0);
140 	}
141 }
142 
143 /*
144  * __bt_snext --
145  *	Check for an exact match after the key.
146  *
147  * Parameters:
148  *	t:	tree
149  *	h:	current page
150  *	key:	key
151  *	exactp:	pointer to exact match flag
152  *
153  * Returns:
154  *	If an exact match found.
155  */
156 static int
157 __bt_snext(t, h, key, exactp)
158 	BTREE *t;
159 	PAGE *h;
160 	const DBT *key;
161 	int *exactp;
162 {
163 	EPG e;
164 
165 	/*
166 	 * Get the next page.  The key is either an exact
167 	 * match, or not as good as the one we already have.
168 	 */
169 	if ((e.page = mpool_get(t->bt_mp, h->nextpg, 0)) == NULL)
170 		return (0);
171 	e.index = 0;
172 	if (__bt_cmp(t, key, &e) == 0) {
173 		mpool_put(t->bt_mp, h, 0);
174 		t->bt_cur = e;
175 		*exactp = 1;
176 		return (1);
177 	}
178 	mpool_put(t->bt_mp, e.page, 0);
179 	return (0);
180 }
181 
182 /*
183  * __bt_sprev --
184  *	Check for an exact match before the key.
185  *
186  * Parameters:
187  *	t:	tree
188  *	h:	current page
189  *	key:	key
190  *	exactp:	pointer to exact match flag
191  *
192  * Returns:
193  *	If an exact match found.
194  */
195 static int
196 __bt_sprev(t, h, key, exactp)
197 	BTREE *t;
198 	PAGE *h;
199 	const DBT *key;
200 	int *exactp;
201 {
202 	EPG e;
203 
204 	/*
205 	 * Get the previous page.  The key is either an exact
206 	 * match, or not as good as the one we already have.
207 	 */
208 	if ((e.page = mpool_get(t->bt_mp, h->prevpg, 0)) == NULL)
209 		return (0);
210 	e.index = NEXTINDEX(e.page) - 1;
211 	if (__bt_cmp(t, key, &e) == 0) {
212 		mpool_put(t->bt_mp, h, 0);
213 		t->bt_cur = e;
214 		*exactp = 1;
215 		return (1);
216 	}
217 	mpool_put(t->bt_mp, e.page, 0);
218 	return (0);
219 }
220