Deleted Added
full compact
bt_get.c (1573) bt_get.c (14272)
1/*-
1/*-
2 * Copyright (c) 1990, 1993
2 * Copyright (c) 1990, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Mike Olson.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:

--- 19 unchanged lines hidden (view full) ---

30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36
37#if defined(LIBC_SCCS) && !defined(lint)
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Mike Olson.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:

--- 19 unchanged lines hidden (view full) ---

30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36
37#if defined(LIBC_SCCS) && !defined(lint)
38static char sccsid[] = "@(#)bt_get.c 8.2 (Berkeley) 9/7/93";
38static char sccsid[] = "@(#)bt_get.c 8.6 (Berkeley) 7/20/94";
39#endif /* LIBC_SCCS and not lint */
40
41#include <sys/types.h>
42
43#include <errno.h>
44#include <stddef.h>
45#include <stdio.h>
46

--- 39 unchanged lines hidden (view full) ---

86
87 if ((e = __bt_search(t, key, &exact)) == NULL)
88 return (RET_ERROR);
89 if (!exact) {
90 mpool_put(t->bt_mp, e->page, 0);
91 return (RET_SPECIAL);
92 }
93
39#endif /* LIBC_SCCS and not lint */
40
41#include <sys/types.h>
42
43#include <errno.h>
44#include <stddef.h>
45#include <stdio.h>
46

--- 39 unchanged lines hidden (view full) ---

86
87 if ((e = __bt_search(t, key, &exact)) == NULL)
88 return (RET_ERROR);
89 if (!exact) {
90 mpool_put(t->bt_mp, e->page, 0);
91 return (RET_SPECIAL);
92 }
93
94 /*
95 * A special case is if we found the record but it's flagged for
96 * deletion. In this case, we want to find another record with the
97 * same key, if it exists. Rather than look around the tree we call
98 * __bt_first and have it redo the search, as __bt_first will not
99 * return keys marked for deletion. Slow, but should never happen.
100 */
101 if (ISSET(t, B_DELCRSR) && e->page->pgno == t->bt_bcursor.pgno &&
102 e->index == t->bt_bcursor.index) {
103 mpool_put(t->bt_mp, e->page, 0);
104 if ((e = __bt_first(t, key, &exact)) == NULL)
105 return (RET_ERROR);
106 if (!exact)
107 return (RET_SPECIAL);
108 }
94 status = __bt_ret(t, e, NULL, NULL, data, &t->bt_rdata, 0);
109
95
110 status = __bt_ret(t, e, NULL, data);
111 /*
112 * If the user is doing concurrent access, we copied the
113 * key/data, toss the page.
114 */
96 /*
97 * If the user is doing concurrent access, we copied the
98 * key/data, toss the page.
99 */
115 if (ISSET(t, B_DB_LOCK))
100 if (F_ISSET(t, B_DB_LOCK))
116 mpool_put(t->bt_mp, e->page, 0);
117 else
118 t->bt_pinned = e->page;
119 return (status);
120}
101 mpool_put(t->bt_mp, e->page, 0);
102 else
103 t->bt_pinned = e->page;
104 return (status);
105}
121
122/*
123 * __BT_FIRST -- Find the first entry.
124 *
125 * Parameters:
126 * t: the tree
127 * key: the key
128 *
129 * Returns:
130 * The first entry in the tree greater than or equal to key.
131 */
132EPG *
133__bt_first(t, key, exactp)
134 BTREE *t;
135 const DBT *key;
136 int *exactp;
137{
138 register PAGE *h;
139 register EPG *e;
140 EPG save;
141 pgno_t cpgno, pg;
142 indx_t cindex;
143 int found;
144
145 /*
146 * Find any matching record; __bt_search pins the page. Only exact
147 * matches are tricky, otherwise just return the location of the key
148 * if it were to be inserted into the tree.
149 */
150 if ((e = __bt_search(t, key, exactp)) == NULL)
151 return (NULL);
152 if (!*exactp)
153 return (e);
154
155 if (ISSET(t, B_DELCRSR)) {
156 cpgno = t->bt_bcursor.pgno;
157 cindex = t->bt_bcursor.index;
158 } else {
159 cpgno = P_INVALID;
160 cindex = 0; /* GCC thinks it's uninitialized. */
161 }
162
163 /*
164 * Walk backwards, skipping empty pages, as long as the entry matches
165 * and there are keys left in the tree. Save a copy of each match in
166 * case we go too far. A special case is that we don't return a match
167 * on records that the cursor references that have already been flagged
168 * for deletion.
169 */
170 save = *e;
171 h = e->page;
172 found = 0;
173 do {
174 if (cpgno != h->pgno || cindex != e->index) {
175 if (save.page->pgno != e->page->pgno) {
176 mpool_put(t->bt_mp, save.page, 0);
177 save = *e;
178 } else
179 save.index = e->index;
180 found = 1;
181 }
182 /*
183 * Make a special effort not to unpin the page the last (or
184 * original) match was on, but also make sure it's unpinned
185 * if an error occurs.
186 */
187 while (e->index == 0) {
188 if (h->prevpg == P_INVALID)
189 goto done1;
190 if (h->pgno != save.page->pgno)
191 mpool_put(t->bt_mp, h, 0);
192 if ((h = mpool_get(t->bt_mp, h->prevpg, 0)) == NULL) {
193 if (h->pgno == save.page->pgno)
194 mpool_put(t->bt_mp, save.page, 0);
195 return (NULL);
196 }
197 e->page = h;
198 e->index = NEXTINDEX(h);
199 }
200 --e->index;
201 } while (__bt_cmp(t, key, e) == 0);
202
203 /*
204 * Reach here with the last page that was looked at pinned, which may
205 * or may not be the same as the last (or original) match page. If
206 * it's not useful, release it.
207 */
208done1: if (h->pgno != save.page->pgno)
209 mpool_put(t->bt_mp, h, 0);
210
211 /*
212 * If still haven't found a record, the only possibility left is the
213 * next one. Move forward one slot, skipping empty pages and check.
214 */
215 if (!found) {
216 h = save.page;
217 if (++save.index == NEXTINDEX(h)) {
218 do {
219 pg = h->nextpg;
220 mpool_put(t->bt_mp, h, 0);
221 if (pg == P_INVALID) {
222 *exactp = 0;
223 return (e);
224 }
225 if ((h = mpool_get(t->bt_mp, pg, 0)) == NULL)
226 return (NULL);
227 } while ((save.index = NEXTINDEX(h)) == 0);
228 save.page = h;
229 }
230 if (__bt_cmp(t, key, &save) != 0) {
231 *exactp = 0;
232 return (e);
233 }
234 }
235 *e = save;
236 *exactp = 1;
237 return (e);
238}