xref: /netbsd-src/lib/libc/db/recno/rec_search.c (revision aae80e6be7bf8e6ad43f8b63d296d2d3923c4ee5)
1*aae80e6bSchristos /*	$NetBSD: rec_search.c,v 1.16 2016/09/24 21:31:25 christos Exp $	*/
22c84ad3aScgd 
39f0aa214Scgd /*-
49f0aa214Scgd  * Copyright (c) 1990, 1993
59f0aa214Scgd  *	The Regents of the University of California.  All rights reserved.
69f0aa214Scgd  *
79f0aa214Scgd  * Redistribution and use in source and binary forms, with or without
89f0aa214Scgd  * modification, are permitted provided that the following conditions
99f0aa214Scgd  * are met:
109f0aa214Scgd  * 1. Redistributions of source code must retain the above copyright
119f0aa214Scgd  *    notice, this list of conditions and the following disclaimer.
129f0aa214Scgd  * 2. Redistributions in binary form must reproduce the above copyright
139f0aa214Scgd  *    notice, this list of conditions and the following disclaimer in the
149f0aa214Scgd  *    documentation and/or other materials provided with the distribution.
15eb7c1594Sagc  * 3. Neither the name of the University nor the names of its contributors
169f0aa214Scgd  *    may be used to endorse or promote products derived from this software
179f0aa214Scgd  *    without specific prior written permission.
189f0aa214Scgd  *
199f0aa214Scgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
209f0aa214Scgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
219f0aa214Scgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
229f0aa214Scgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
239f0aa214Scgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
249f0aa214Scgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
259f0aa214Scgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
269f0aa214Scgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
279f0aa214Scgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
289f0aa214Scgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
299f0aa214Scgd  * SUCH DAMAGE.
309f0aa214Scgd  */
319f0aa214Scgd 
32d3595ddfSjoerg #if HAVE_NBTOOL_CONFIG_H
33d3595ddfSjoerg #include "nbtool_config.h"
34d3595ddfSjoerg #endif
35d3595ddfSjoerg 
3600ae392dSchristos #include <sys/cdefs.h>
37*aae80e6bSchristos __RCSID("$NetBSD: rec_search.c,v 1.16 2016/09/24 21:31:25 christos Exp $");
389f0aa214Scgd 
3943fa6fe3Sjtc #include "namespace.h"
409f0aa214Scgd #include <sys/types.h>
419f0aa214Scgd 
42cb9daf8fSchristos #include <assert.h>
439f0aa214Scgd #include <errno.h>
449f0aa214Scgd #include <stdio.h>
459f0aa214Scgd 
469f0aa214Scgd #include <db.h>
479f0aa214Scgd #include "recno.h"
489f0aa214Scgd 
499f0aa214Scgd /*
509f0aa214Scgd  * __REC_SEARCH -- Search a btree for a key.
519f0aa214Scgd  *
529f0aa214Scgd  * Parameters:
539f0aa214Scgd  *	t:	tree to search
549f0aa214Scgd  *	recno:	key to find
559f0aa214Scgd  *	op: 	search operation
569f0aa214Scgd  *
579f0aa214Scgd  * Returns:
589f0aa214Scgd  *	EPG for matching record, if any, or the EPG for the location of the
599f0aa214Scgd  *	key, if it were inserted into the tree.
609f0aa214Scgd  *
6165aeeefbScgd  * Returns:
6265aeeefbScgd  *	The EPG for matching record, if any, or the EPG for the location
6365aeeefbScgd  *	of the key, if it were inserted into the tree, is entered into
6465aeeefbScgd  *	the bt_cur field of the tree.  A pointer to the field is returned.
659f0aa214Scgd  */
669f0aa214Scgd EPG *
__rec_search(BTREE * t,recno_t recno,enum SRCHOP op)67cb9daf8fSchristos __rec_search(BTREE *t, recno_t recno, enum SRCHOP op)
689f0aa214Scgd {
69cb9daf8fSchristos 	indx_t idx;
70cb9daf8fSchristos 	PAGE *h;
719f0aa214Scgd 	EPGNO *parent;
729f0aa214Scgd 	RINTERNAL *r;
739f0aa214Scgd 	pgno_t pg;
749f0aa214Scgd 	indx_t top;
759f0aa214Scgd 	recno_t total;
76a6d14e36Scgd 	int sverrno;
779f0aa214Scgd 
789f0aa214Scgd 	BT_CLR(t);
799f0aa214Scgd 	for (pg = P_ROOT, total = 0;;) {
80*aae80e6bSchristos 		if ((h = mpool_get(t->bt_mp, pg, 0)) == NULL)
819f0aa214Scgd 			goto err;
829f0aa214Scgd 		if (h->flags & P_RLEAF) {
8365aeeefbScgd 			t->bt_cur.page = h;
8465aeeefbScgd 			t->bt_cur.index = recno - total;
8565aeeefbScgd 			return (&t->bt_cur);
869f0aa214Scgd 		}
87e23f3d91Sthorpej 		for (idx = 0, top = NEXTINDEX(h);;) {
88e23f3d91Sthorpej 			r = GETRINTERNAL(h, idx);
89e23f3d91Sthorpej 			if (++idx == top || total + r->nrecs > recno)
909f0aa214Scgd 				break;
919f0aa214Scgd 			total += r->nrecs;
929f0aa214Scgd 		}
939f0aa214Scgd 
94e23f3d91Sthorpej 		BT_PUSH(t, pg, idx - 1);
959f0aa214Scgd 
969f0aa214Scgd 		pg = r->pgno;
979f0aa214Scgd 		switch (op) {
989f0aa214Scgd 		case SDELETE:
99e23f3d91Sthorpej 			--GETRINTERNAL(h, (idx - 1))->nrecs;
1009f0aa214Scgd 			mpool_put(t->bt_mp, h, MPOOL_DIRTY);
1019f0aa214Scgd 			break;
1029f0aa214Scgd 		case SINSERT:
103e23f3d91Sthorpej 			++GETRINTERNAL(h, (idx - 1))->nrecs;
1049f0aa214Scgd 			mpool_put(t->bt_mp, h, MPOOL_DIRTY);
1059f0aa214Scgd 			break;
1069f0aa214Scgd 		case SEARCH:
1079f0aa214Scgd 			mpool_put(t->bt_mp, h, 0);
1089f0aa214Scgd 			break;
1099f0aa214Scgd 		}
1109f0aa214Scgd 
1119f0aa214Scgd 	}
1129f0aa214Scgd 	/* Try and recover the tree. */
113a6d14e36Scgd err:	sverrno = errno;
1149f0aa214Scgd 	if (op != SEARCH)
1159f0aa214Scgd 		while  ((parent = BT_POP(t)) != NULL) {
116*aae80e6bSchristos 			if ((h = mpool_get(t->bt_mp, parent->pgno, 0)) == NULL)
1179f0aa214Scgd 				break;
1189f0aa214Scgd 			if (op == SINSERT)
1199f0aa214Scgd 				--GETRINTERNAL(h, parent->index)->nrecs;
1209f0aa214Scgd 			else
1219f0aa214Scgd 				++GETRINTERNAL(h, parent->index)->nrecs;
1229f0aa214Scgd                         mpool_put(t->bt_mp, h, MPOOL_DIRTY);
1239f0aa214Scgd                 }
124a6d14e36Scgd 	errno = sverrno;
1259f0aa214Scgd 	return (NULL);
1269f0aa214Scgd }
127