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