1 | /*- |
---|
2 | * See the file LICENSE for redistribution information. |
---|
3 | * |
---|
4 | * Copyright (c) 1996, 1997, 1998, 1999, 2000 |
---|
5 | * Sleepycat Software. All rights reserved. |
---|
6 | */ |
---|
7 | /* |
---|
8 | * Copyright (c) 1990, 1993, 1994, 1995, 1996 |
---|
9 | * Keith Bostic. All rights reserved. |
---|
10 | */ |
---|
11 | /* |
---|
12 | * Copyright (c) 1990, 1993 |
---|
13 | * The Regents of the University of California. All rights reserved. |
---|
14 | * |
---|
15 | * Redistribution and use in source and binary forms, with or without |
---|
16 | * modification, are permitted provided that the following conditions |
---|
17 | * are met: |
---|
18 | * 1. Redistributions of source code must retain the above copyright |
---|
19 | * notice, this list of conditions and the following disclaimer. |
---|
20 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
21 | * notice, this list of conditions and the following disclaimer in the |
---|
22 | * documentation and/or other materials provided with the distribution. |
---|
23 | * 3. Neither the name of the University nor the names of its contributors |
---|
24 | * may be used to endorse or promote products derived from this software |
---|
25 | * without specific prior written permission. |
---|
26 | * |
---|
27 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
---|
28 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
29 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
30 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
---|
31 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
---|
32 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
---|
33 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
---|
34 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
---|
35 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
---|
36 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
---|
37 | * SUCH DAMAGE. |
---|
38 | */ |
---|
39 | |
---|
40 | #include "db_config.h" |
---|
41 | |
---|
42 | #ifndef lint |
---|
43 | static const char revid[] = "$Id: bt_rsearch.c,v 1.1.1.2 2002-02-11 16:25:51 ghudson Exp $"; |
---|
44 | #endif /* not lint */ |
---|
45 | |
---|
46 | #ifndef NO_SYSTEM_INCLUDES |
---|
47 | #include <sys/types.h> |
---|
48 | #endif |
---|
49 | |
---|
50 | #include "db_int.h" |
---|
51 | #include "db_page.h" |
---|
52 | #include "btree.h" |
---|
53 | #include "db_shash.h" |
---|
54 | #include "lock.h" |
---|
55 | |
---|
56 | /* |
---|
57 | * __bam_rsearch -- |
---|
58 | * Search a btree for a record number. |
---|
59 | * |
---|
60 | * PUBLIC: int __bam_rsearch __P((DBC *, db_recno_t *, u_int32_t, int, int *)); |
---|
61 | */ |
---|
62 | int |
---|
63 | __bam_rsearch(dbc, recnop, flags, stop, exactp) |
---|
64 | DBC *dbc; |
---|
65 | db_recno_t *recnop; |
---|
66 | u_int32_t flags; |
---|
67 | int stop, *exactp; |
---|
68 | { |
---|
69 | BINTERNAL *bi; |
---|
70 | BTREE_CURSOR *cp; |
---|
71 | DB *dbp; |
---|
72 | DB_LOCK lock; |
---|
73 | PAGE *h; |
---|
74 | RINTERNAL *ri; |
---|
75 | db_indx_t adjust, deloffset, indx, top; |
---|
76 | db_lockmode_t lock_mode; |
---|
77 | db_pgno_t pg; |
---|
78 | db_recno_t recno, t_recno, total; |
---|
79 | int ret, stack; |
---|
80 | |
---|
81 | dbp = dbc->dbp; |
---|
82 | cp = (BTREE_CURSOR *)dbc->internal; |
---|
83 | |
---|
84 | BT_STK_CLR(cp); |
---|
85 | |
---|
86 | /* |
---|
87 | * There are several ways we search a btree tree. The flags argument |
---|
88 | * specifies if we're acquiring read or write locks and if we are |
---|
89 | * locking pairs of pages. In addition, if we're adding or deleting |
---|
90 | * an item, we have to lock the entire tree, regardless. See btree.h |
---|
91 | * for more details. |
---|
92 | * |
---|
93 | * If write-locking pages, we need to know whether or not to acquire a |
---|
94 | * write lock on a page before getting it. This depends on how deep it |
---|
95 | * is in tree, which we don't know until we acquire the root page. So, |
---|
96 | * if we need to lock the root page we may have to upgrade it later, |
---|
97 | * because we won't get the correct lock initially. |
---|
98 | * |
---|
99 | * Retrieve the root page. |
---|
100 | */ |
---|
101 | pg = cp->root; |
---|
102 | stack = LF_ISSET(S_STACK); |
---|
103 | lock_mode = stack ? DB_LOCK_WRITE : DB_LOCK_READ; |
---|
104 | if ((ret = __db_lget(dbc, 0, pg, lock_mode, 0, &lock)) != 0) |
---|
105 | return (ret); |
---|
106 | if ((ret = memp_fget(dbp->mpf, &pg, 0, &h)) != 0) { |
---|
107 | /* Did not read it, so we can release the lock */ |
---|
108 | (void)__LPUT(dbc, lock); |
---|
109 | return (ret); |
---|
110 | } |
---|
111 | |
---|
112 | /* |
---|
113 | * Decide if we need to save this page; if we do, write lock it. |
---|
114 | * We deliberately don't lock-couple on this call. If the tree |
---|
115 | * is tiny, i.e., one page, and two threads are busily updating |
---|
116 | * the root page, we're almost guaranteed deadlocks galore, as |
---|
117 | * each one gets a read lock and then blocks the other's attempt |
---|
118 | * for a write lock. |
---|
119 | */ |
---|
120 | if (!stack && |
---|
121 | ((LF_ISSET(S_PARENT) && (u_int8_t)(stop + 1) >= h->level) || |
---|
122 | (LF_ISSET(S_WRITE) && h->level == LEAFLEVEL))) { |
---|
123 | (void)memp_fput(dbp->mpf, h, 0); |
---|
124 | (void)__LPUT(dbc, lock); |
---|
125 | lock_mode = DB_LOCK_WRITE; |
---|
126 | if ((ret = __db_lget(dbc, 0, pg, lock_mode, 0, &lock)) != 0) |
---|
127 | return (ret); |
---|
128 | if ((ret = memp_fget(dbp->mpf, &pg, 0, &h)) != 0) { |
---|
129 | /* Did not read it, so we can release the lock */ |
---|
130 | (void)__LPUT(dbc, lock); |
---|
131 | return (ret); |
---|
132 | } |
---|
133 | stack = 1; |
---|
134 | } |
---|
135 | |
---|
136 | /* |
---|
137 | * If appending to the tree, set the record number now -- we have the |
---|
138 | * root page locked. |
---|
139 | * |
---|
140 | * Delete only deletes exact matches, read only returns exact matches. |
---|
141 | * Note, this is different from __bam_search(), which returns non-exact |
---|
142 | * matches for read. |
---|
143 | * |
---|
144 | * The record may not exist. We can only return the correct location |
---|
145 | * for the record immediately after the last record in the tree, so do |
---|
146 | * a fast check now. |
---|
147 | */ |
---|
148 | total = RE_NREC(h); |
---|
149 | if (LF_ISSET(S_APPEND)) { |
---|
150 | *exactp = 0; |
---|
151 | *recnop = recno = total + 1; |
---|
152 | } else { |
---|
153 | recno = *recnop; |
---|
154 | if (recno <= total) |
---|
155 | *exactp = 1; |
---|
156 | else { |
---|
157 | *exactp = 0; |
---|
158 | if (!LF_ISSET(S_PAST_EOF) || recno > total + 1) { |
---|
159 | /* |
---|
160 | * Keep the page locked for serializability. |
---|
161 | * |
---|
162 | * XXX |
---|
163 | * This leaves the root page locked, which will |
---|
164 | * eliminate any concurrency. A possible fix |
---|
165 | * would be to lock the last leaf page instead. |
---|
166 | */ |
---|
167 | (void)memp_fput(dbp->mpf, h, 0); |
---|
168 | (void)__TLPUT(dbc, lock); |
---|
169 | return (DB_NOTFOUND); |
---|
170 | } |
---|
171 | } |
---|
172 | } |
---|
173 | |
---|
174 | /* |
---|
175 | * !!! |
---|
176 | * Record numbers in the tree are 0-based, but the recno is |
---|
177 | * 1-based. All of the calculations below have to take this |
---|
178 | * into account. |
---|
179 | */ |
---|
180 | for (total = 0;;) { |
---|
181 | switch (TYPE(h)) { |
---|
182 | case P_LBTREE: |
---|
183 | case P_LDUP: |
---|
184 | recno -= total; |
---|
185 | /* |
---|
186 | * There may be logically deleted records on the page. |
---|
187 | * If there are enough, the record may not exist. |
---|
188 | */ |
---|
189 | if (TYPE(h) == P_LBTREE) { |
---|
190 | adjust = P_INDX; |
---|
191 | deloffset = O_INDX; |
---|
192 | } else { |
---|
193 | adjust = O_INDX; |
---|
194 | deloffset = 0; |
---|
195 | } |
---|
196 | for (t_recno = 0, indx = 0;; indx += adjust) { |
---|
197 | if (indx >= NUM_ENT(h)) { |
---|
198 | *exactp = 0; |
---|
199 | if (!LF_ISSET(S_PAST_EOF) || |
---|
200 | recno > t_recno + 1) { |
---|
201 | ret = DB_NOTFOUND; |
---|
202 | goto err; |
---|
203 | } |
---|
204 | } |
---|
205 | if (!B_DISSET( |
---|
206 | GET_BKEYDATA(h, indx + deloffset)->type) && |
---|
207 | ++t_recno == recno) |
---|
208 | break; |
---|
209 | } |
---|
210 | |
---|
211 | /* Correct from 1-based to 0-based for a page offset. */ |
---|
212 | BT_STK_ENTER(dbp->dbenv, |
---|
213 | cp, h, indx, lock, lock_mode, ret); |
---|
214 | if (ret != 0) |
---|
215 | goto err; |
---|
216 | return (0); |
---|
217 | case P_IBTREE: |
---|
218 | for (indx = 0, top = NUM_ENT(h);;) { |
---|
219 | bi = GET_BINTERNAL(h, indx); |
---|
220 | if (++indx == top || total + bi->nrecs >= recno) |
---|
221 | break; |
---|
222 | total += bi->nrecs; |
---|
223 | } |
---|
224 | pg = bi->pgno; |
---|
225 | break; |
---|
226 | case P_LRECNO: |
---|
227 | recno -= total; |
---|
228 | |
---|
229 | /* Correct from 1-based to 0-based for a page offset. */ |
---|
230 | --recno; |
---|
231 | BT_STK_ENTER(dbp->dbenv, |
---|
232 | cp, h, recno, lock, lock_mode, ret); |
---|
233 | if (ret != 0) |
---|
234 | goto err; |
---|
235 | return (0); |
---|
236 | case P_IRECNO: |
---|
237 | for (indx = 0, top = NUM_ENT(h);;) { |
---|
238 | ri = GET_RINTERNAL(h, indx); |
---|
239 | if (++indx == top || total + ri->nrecs >= recno) |
---|
240 | break; |
---|
241 | total += ri->nrecs; |
---|
242 | } |
---|
243 | pg = ri->pgno; |
---|
244 | break; |
---|
245 | default: |
---|
246 | return (__db_pgfmt(dbp, h->pgno)); |
---|
247 | } |
---|
248 | --indx; |
---|
249 | |
---|
250 | if (stack) { |
---|
251 | /* Return if this is the lowest page wanted. */ |
---|
252 | if (LF_ISSET(S_PARENT) && stop == h->level) { |
---|
253 | BT_STK_ENTER(dbp->dbenv, |
---|
254 | cp, h, indx, lock, lock_mode, ret); |
---|
255 | if (ret != 0) |
---|
256 | goto err; |
---|
257 | return (0); |
---|
258 | } |
---|
259 | BT_STK_PUSH(dbp->dbenv, |
---|
260 | cp, h, indx, lock, lock_mode, ret); |
---|
261 | if (ret != 0) |
---|
262 | goto err; |
---|
263 | |
---|
264 | lock_mode = DB_LOCK_WRITE; |
---|
265 | if ((ret = |
---|
266 | __db_lget(dbc, 0, pg, lock_mode, 0, &lock)) != 0) |
---|
267 | goto err; |
---|
268 | } else { |
---|
269 | /* |
---|
270 | * Decide if we want to return a pointer to the next |
---|
271 | * page in the stack. If we do, write lock it and |
---|
272 | * never unlock it. |
---|
273 | */ |
---|
274 | if ((LF_ISSET(S_PARENT) && |
---|
275 | (u_int8_t)(stop + 1) >= (u_int8_t)(h->level - 1)) || |
---|
276 | (h->level - 1) == LEAFLEVEL) |
---|
277 | stack = 1; |
---|
278 | |
---|
279 | (void)memp_fput(dbp->mpf, h, 0); |
---|
280 | |
---|
281 | lock_mode = stack && |
---|
282 | LF_ISSET(S_WRITE) ? DB_LOCK_WRITE : DB_LOCK_READ; |
---|
283 | if ((ret = __db_lget(dbc, |
---|
284 | LCK_COUPLE, pg, lock_mode, 0, &lock)) != 0) { |
---|
285 | /* |
---|
286 | * If we fail, discard the lock we held. This |
---|
287 | * is OK because this only happens when we are |
---|
288 | * descending the tree holding read-locks. |
---|
289 | */ |
---|
290 | __LPUT(dbc, lock); |
---|
291 | goto err; |
---|
292 | } |
---|
293 | } |
---|
294 | |
---|
295 | if ((ret = memp_fget(dbp->mpf, &pg, 0, &h)) != 0) |
---|
296 | goto err; |
---|
297 | } |
---|
298 | /* NOTREACHED */ |
---|
299 | |
---|
300 | err: BT_STK_POP(cp); |
---|
301 | __bam_stkrel(dbc, 0); |
---|
302 | return (ret); |
---|
303 | } |
---|
304 | |
---|
305 | /* |
---|
306 | * __bam_adjust -- |
---|
307 | * Adjust the tree after adding or deleting a record. |
---|
308 | * |
---|
309 | * PUBLIC: int __bam_adjust __P((DBC *, int32_t)); |
---|
310 | */ |
---|
311 | int |
---|
312 | __bam_adjust(dbc, adjust) |
---|
313 | DBC *dbc; |
---|
314 | int32_t adjust; |
---|
315 | { |
---|
316 | BTREE_CURSOR *cp; |
---|
317 | DB *dbp; |
---|
318 | EPG *epg; |
---|
319 | PAGE *h; |
---|
320 | db_pgno_t root_pgno; |
---|
321 | int ret; |
---|
322 | |
---|
323 | dbp = dbc->dbp; |
---|
324 | cp = (BTREE_CURSOR *)dbc->internal; |
---|
325 | root_pgno = cp->root; |
---|
326 | |
---|
327 | /* Update the record counts for the tree. */ |
---|
328 | for (epg = cp->sp; epg <= cp->csp; ++epg) { |
---|
329 | h = epg->page; |
---|
330 | if (TYPE(h) == P_IBTREE || TYPE(h) == P_IRECNO) { |
---|
331 | if (DB_LOGGING(dbc) && |
---|
332 | (ret = __bam_cadjust_log(dbp->dbenv, |
---|
333 | dbc->txn, &LSN(h), 0, dbp->log_fileid, |
---|
334 | PGNO(h), &LSN(h), (u_int32_t)epg->indx, adjust, |
---|
335 | PGNO(h) == root_pgno ? CAD_UPDATEROOT : 0)) != 0) |
---|
336 | return (ret); |
---|
337 | |
---|
338 | if (TYPE(h) == P_IBTREE) |
---|
339 | GET_BINTERNAL(h, epg->indx)->nrecs += adjust; |
---|
340 | else |
---|
341 | GET_RINTERNAL(h, epg->indx)->nrecs += adjust; |
---|
342 | |
---|
343 | if (PGNO(h) == root_pgno) |
---|
344 | RE_NREC_ADJ(h, adjust); |
---|
345 | |
---|
346 | if ((ret = memp_fset(dbp->mpf, h, DB_MPOOL_DIRTY)) != 0) |
---|
347 | return (ret); |
---|
348 | } |
---|
349 | } |
---|
350 | return (0); |
---|
351 | } |
---|
352 | |
---|
353 | /* |
---|
354 | * __bam_nrecs -- |
---|
355 | * Return the number of records in the tree. |
---|
356 | * |
---|
357 | * PUBLIC: int __bam_nrecs __P((DBC *, db_recno_t *)); |
---|
358 | */ |
---|
359 | int |
---|
360 | __bam_nrecs(dbc, rep) |
---|
361 | DBC *dbc; |
---|
362 | db_recno_t *rep; |
---|
363 | { |
---|
364 | DB *dbp; |
---|
365 | DB_LOCK lock; |
---|
366 | PAGE *h; |
---|
367 | db_pgno_t pgno; |
---|
368 | int ret; |
---|
369 | |
---|
370 | dbp = dbc->dbp; |
---|
371 | |
---|
372 | pgno = dbc->internal->root; |
---|
373 | if ((ret = __db_lget(dbc, 0, pgno, DB_LOCK_READ, 0, &lock)) != 0) |
---|
374 | return (ret); |
---|
375 | if ((ret = memp_fget(dbp->mpf, &pgno, 0, &h)) != 0) |
---|
376 | return (ret); |
---|
377 | |
---|
378 | *rep = RE_NREC(h); |
---|
379 | |
---|
380 | (void)memp_fput(dbp->mpf, h, 0); |
---|
381 | (void)__TLPUT(dbc, lock); |
---|
382 | |
---|
383 | return (0); |
---|
384 | } |
---|
385 | |
---|
386 | /* |
---|
387 | * __bam_total -- |
---|
388 | * Return the number of records below a page. |
---|
389 | * |
---|
390 | * PUBLIC: db_recno_t __bam_total __P((PAGE *)); |
---|
391 | */ |
---|
392 | db_recno_t |
---|
393 | __bam_total(h) |
---|
394 | PAGE *h; |
---|
395 | { |
---|
396 | db_recno_t nrecs; |
---|
397 | db_indx_t indx, top; |
---|
398 | |
---|
399 | nrecs = 0; |
---|
400 | top = NUM_ENT(h); |
---|
401 | |
---|
402 | switch (TYPE(h)) { |
---|
403 | case P_LBTREE: |
---|
404 | /* Check for logically deleted records. */ |
---|
405 | for (indx = 0; indx < top; indx += P_INDX) |
---|
406 | if (!B_DISSET(GET_BKEYDATA(h, indx + O_INDX)->type)) |
---|
407 | ++nrecs; |
---|
408 | break; |
---|
409 | case P_LDUP: |
---|
410 | /* Check for logically deleted records. */ |
---|
411 | for (indx = 0; indx < top; indx += O_INDX) |
---|
412 | if (!B_DISSET(GET_BKEYDATA(h, indx)->type)) |
---|
413 | ++nrecs; |
---|
414 | break; |
---|
415 | case P_IBTREE: |
---|
416 | for (indx = 0; indx < top; indx += O_INDX) |
---|
417 | nrecs += GET_BINTERNAL(h, indx)->nrecs; |
---|
418 | break; |
---|
419 | case P_LRECNO: |
---|
420 | nrecs = NUM_ENT(h); |
---|
421 | break; |
---|
422 | case P_IRECNO: |
---|
423 | for (indx = 0; indx < top; indx += O_INDX) |
---|
424 | nrecs += GET_RINTERNAL(h, indx)->nrecs; |
---|
425 | break; |
---|
426 | } |
---|
427 | |
---|
428 | return (nrecs); |
---|
429 | } |
---|