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