bt_get.c revision 302408
1189251Ssam/*-
2189251Ssam * Copyright (c) 1990, 1993, 1994
3189251Ssam *	The Regents of the University of California.  All rights reserved.
4189251Ssam *
5252726Srpaulo * This code is derived from software contributed to Berkeley by
6252726Srpaulo * Mike Olson.
7189251Ssam *
8189251Ssam * Redistribution and use in source and binary forms, with or without
9189251Ssam * modification, are permitted provided that the following conditions
10189251Ssam * are met:
11189251Ssam * 1. Redistributions of source code must retain the above copyright
12189251Ssam *    notice, this list of conditions and the following disclaimer.
13189251Ssam * 2. Redistributions in binary form must reproduce the above copyright
14189251Ssam *    notice, this list of conditions and the following disclaimer in the
15189251Ssam *    documentation and/or other materials provided with the distribution.
16189251Ssam * 4. Neither the name of the University nor the names of its contributors
17189251Ssam *    may be used to endorse or promote products derived from this software
18189251Ssam *    without specific prior written permission.
19189251Ssam *
20189251Ssam * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21189251Ssam * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22189251Ssam * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23189251Ssam * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24189251Ssam * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25189251Ssam * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26189251Ssam * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27189251Ssam * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28189251Ssam * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29189251Ssam * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30189251Ssam * SUCH DAMAGE.
31189251Ssam */
32189251Ssam
33189251Ssam#if defined(LIBC_SCCS) && !defined(lint)
34189251Ssamstatic char sccsid[] = "@(#)bt_get.c	8.6 (Berkeley) 7/20/94";
35189251Ssam#endif /* LIBC_SCCS and not lint */
36189251Ssam#include <sys/cdefs.h>
37189251Ssam__FBSDID("$FreeBSD: stable/11/lib/libc/db/btree/bt_get.c 189291 2009-03-02 23:47:18Z delphij $");
38189251Ssam
39189251Ssam#include <sys/types.h>
40189251Ssam
41189251Ssam#include <errno.h>
42189251Ssam#include <stddef.h>
43189251Ssam#include <stdio.h>
44189251Ssam
45189251Ssam#include <db.h>
46189251Ssam#include "btree.h"
47189251Ssam
48189251Ssam/*
49189251Ssam * __BT_GET -- Get a record from the btree.
50189251Ssam *
51189251Ssam * Parameters:
52189251Ssam *	dbp:	pointer to access method
53189251Ssam *	key:	key to find
54189251Ssam *	data:	data to return
55189251Ssam *	flag:	currently unused
56189251Ssam *
57189251Ssam * Returns:
58189251Ssam *	RET_ERROR, RET_SUCCESS and RET_SPECIAL if the key not found.
59189251Ssam */
60189251Ssamint
61189251Ssam__bt_get(const DB *dbp, const DBT *key, DBT *data, u_int flags)
62189251Ssam{
63189251Ssam	BTREE *t;
64189251Ssam	EPG *e;
65189251Ssam	int exact, status;
66189251Ssam
67189251Ssam	t = dbp->internal;
68189251Ssam
69189251Ssam	/* Toss any page pinned across calls. */
70189251Ssam	if (t->bt_pinned != NULL) {
71189251Ssam		mpool_put(t->bt_mp, t->bt_pinned, 0);
72189251Ssam		t->bt_pinned = NULL;
73189251Ssam	}
74189251Ssam
75189251Ssam	/* Get currently doesn't take any flags. */
76189251Ssam	if (flags) {
77189251Ssam		errno = EINVAL;
78189251Ssam		return (RET_ERROR);
79189251Ssam	}
80189251Ssam
81189251Ssam	if ((e = __bt_search(t, key, &exact)) == NULL)
82189251Ssam		return (RET_ERROR);
83189251Ssam	if (!exact) {
84189251Ssam		mpool_put(t->bt_mp, e->page, 0);
85189251Ssam		return (RET_SPECIAL);
86189251Ssam	}
87189251Ssam
88189251Ssam	status = __bt_ret(t, e, NULL, NULL, data, &t->bt_rdata, 0);
89189251Ssam
90189251Ssam	/*
91189251Ssam	 * If the user is doing concurrent access, we copied the
92189251Ssam	 * key/data, toss the page.
93189251Ssam	 */
94189251Ssam	if (F_ISSET(t, B_DB_LOCK))
95189251Ssam		mpool_put(t->bt_mp, e->page, 0);
96189251Ssam	else
97189251Ssam		t->bt_pinned = e->page;
98189251Ssam	return (status);
99189251Ssam}
100189251Ssam