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