xref: /netbsd/lib/libc/db/btree/bt_get.c (revision d3595ddf)
1*d3595ddfSjoerg /*	$NetBSD: bt_get.c,v 1.13 2008/09/11 12:58:00 joerg Exp $	*/
2a954b078Scgd 
39f0aa214Scgd /*-
424420c01Scgd  * Copyright (c) 1990, 1993, 1994
59f0aa214Scgd  *	The Regents of the University of California.  All rights reserved.
69f0aa214Scgd  *
79f0aa214Scgd  * This code is derived from software contributed to Berkeley by
89f0aa214Scgd  * Mike Olson.
99f0aa214Scgd  *
109f0aa214Scgd  * Redistribution and use in source and binary forms, with or without
119f0aa214Scgd  * modification, are permitted provided that the following conditions
129f0aa214Scgd  * are met:
139f0aa214Scgd  * 1. Redistributions of source code must retain the above copyright
149f0aa214Scgd  *    notice, this list of conditions and the following disclaimer.
159f0aa214Scgd  * 2. Redistributions in binary form must reproduce the above copyright
169f0aa214Scgd  *    notice, this list of conditions and the following disclaimer in the
179f0aa214Scgd  *    documentation and/or other materials provided with the distribution.
18eb7c1594Sagc  * 3. Neither the name of the University nor the names of its contributors
199f0aa214Scgd  *    may be used to endorse or promote products derived from this software
209f0aa214Scgd  *    without specific prior written permission.
219f0aa214Scgd  *
229f0aa214Scgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
239f0aa214Scgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
249f0aa214Scgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
259f0aa214Scgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
269f0aa214Scgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
279f0aa214Scgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
289f0aa214Scgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
299f0aa214Scgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
309f0aa214Scgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
319f0aa214Scgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
329f0aa214Scgd  * SUCH DAMAGE.
339f0aa214Scgd  */
349f0aa214Scgd 
35*d3595ddfSjoerg #if HAVE_NBTOOL_CONFIG_H
36*d3595ddfSjoerg #include "nbtool_config.h"
37*d3595ddfSjoerg #endif
38*d3595ddfSjoerg 
3900ae392dSchristos #include <sys/cdefs.h>
40*d3595ddfSjoerg __RCSID("$NetBSD: bt_get.c,v 1.13 2008/09/11 12:58:00 joerg Exp $");
419f0aa214Scgd 
4243fa6fe3Sjtc #include "namespace.h"
439f0aa214Scgd #include <sys/types.h>
449f0aa214Scgd 
45cb9daf8fSchristos #include <assert.h>
469f0aa214Scgd #include <errno.h>
479f0aa214Scgd #include <stddef.h>
489f0aa214Scgd #include <stdio.h>
499f0aa214Scgd 
509f0aa214Scgd #include <db.h>
519f0aa214Scgd #include "btree.h"
529f0aa214Scgd 
539f0aa214Scgd /*
549f0aa214Scgd  * __BT_GET -- Get a record from the btree.
559f0aa214Scgd  *
569f0aa214Scgd  * Parameters:
579f0aa214Scgd  *	dbp:	pointer to access method
589f0aa214Scgd  *	key:	key to find
599f0aa214Scgd  *	data:	data to return
609f0aa214Scgd  *	flag:	currently unused
619f0aa214Scgd  *
629f0aa214Scgd  * Returns:
639f0aa214Scgd  *	RET_ERROR, RET_SUCCESS and RET_SPECIAL if the key not found.
649f0aa214Scgd  */
659f0aa214Scgd int
__bt_get(const DB * dbp,const DBT * key,DBT * data,u_int flags)66cb9daf8fSchristos __bt_get(const DB *dbp, const DBT *key, DBT *data, u_int flags)
679f0aa214Scgd {
689f0aa214Scgd 	BTREE *t;
699f0aa214Scgd 	EPG *e;
709f0aa214Scgd 	int exact, status;
719f0aa214Scgd 
7245e27c80Scgd 	t = dbp->internal;
7345e27c80Scgd 
7445e27c80Scgd 	/* Toss any page pinned across calls. */
7545e27c80Scgd 	if (t->bt_pinned != NULL) {
7645e27c80Scgd 		mpool_put(t->bt_mp, t->bt_pinned, 0);
7745e27c80Scgd 		t->bt_pinned = NULL;
7845e27c80Scgd 	}
7945e27c80Scgd 
8045e27c80Scgd 	/* Get currently doesn't take any flags. */
819f0aa214Scgd 	if (flags) {
829f0aa214Scgd 		errno = EINVAL;
839f0aa214Scgd 		return (RET_ERROR);
849f0aa214Scgd 	}
8545e27c80Scgd 
869f0aa214Scgd 	if ((e = __bt_search(t, key, &exact)) == NULL)
879f0aa214Scgd 		return (RET_ERROR);
889f0aa214Scgd 	if (!exact) {
899f0aa214Scgd 		mpool_put(t->bt_mp, e->page, 0);
909f0aa214Scgd 		return (RET_SPECIAL);
919f0aa214Scgd 	}
929f0aa214Scgd 
9324420c01Scgd 	status = __bt_ret(t, e, NULL, NULL, data, &t->bt_rdata, 0);
949f0aa214Scgd 
9545e27c80Scgd 	/*
9645e27c80Scgd 	 * If the user is doing concurrent access, we copied the
9745e27c80Scgd 	 * key/data, toss the page.
9845e27c80Scgd 	 */
9924420c01Scgd 	if (F_ISSET(t, B_DB_LOCK))
1009f0aa214Scgd 		mpool_put(t->bt_mp, e->page, 0);
10145e27c80Scgd 	else
10245e27c80Scgd 		t->bt_pinned = e->page;
1039f0aa214Scgd 	return (status);
1049f0aa214Scgd }
105