Home | History | Annotate | Line # | Download | only in btree
bt_get.c revision 1.13.6.2
      1  1.13.6.2  joerg /*	$NetBSD: bt_get.c,v 1.13.6.2 2008/09/11 12:58:01 joerg Exp $	*/
      2  1.13.6.2  joerg 
      3  1.13.6.2  joerg /*-
      4  1.13.6.2  joerg  * Copyright (c) 1990, 1993, 1994
      5  1.13.6.2  joerg  *	The Regents of the University of California.  All rights reserved.
      6  1.13.6.2  joerg  *
      7  1.13.6.2  joerg  * This code is derived from software contributed to Berkeley by
      8  1.13.6.2  joerg  * Mike Olson.
      9  1.13.6.2  joerg  *
     10  1.13.6.2  joerg  * Redistribution and use in source and binary forms, with or without
     11  1.13.6.2  joerg  * modification, are permitted provided that the following conditions
     12  1.13.6.2  joerg  * are met:
     13  1.13.6.2  joerg  * 1. Redistributions of source code must retain the above copyright
     14  1.13.6.2  joerg  *    notice, this list of conditions and the following disclaimer.
     15  1.13.6.2  joerg  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.13.6.2  joerg  *    notice, this list of conditions and the following disclaimer in the
     17  1.13.6.2  joerg  *    documentation and/or other materials provided with the distribution.
     18  1.13.6.2  joerg  * 3. Neither the name of the University nor the names of its contributors
     19  1.13.6.2  joerg  *    may be used to endorse or promote products derived from this software
     20  1.13.6.2  joerg  *    without specific prior written permission.
     21  1.13.6.2  joerg  *
     22  1.13.6.2  joerg  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23  1.13.6.2  joerg  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  1.13.6.2  joerg  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  1.13.6.2  joerg  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26  1.13.6.2  joerg  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27  1.13.6.2  joerg  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28  1.13.6.2  joerg  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29  1.13.6.2  joerg  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30  1.13.6.2  joerg  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  1.13.6.2  joerg  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  1.13.6.2  joerg  * SUCH DAMAGE.
     33  1.13.6.2  joerg  */
     34  1.13.6.2  joerg 
     35  1.13.6.2  joerg #if HAVE_NBTOOL_CONFIG_H
     36  1.13.6.2  joerg #include "nbtool_config.h"
     37  1.13.6.2  joerg #endif
     38  1.13.6.2  joerg 
     39  1.13.6.2  joerg #include <sys/cdefs.h>
     40  1.13.6.2  joerg __RCSID("$NetBSD: bt_get.c,v 1.13.6.2 2008/09/11 12:58:01 joerg Exp $");
     41  1.13.6.2  joerg 
     42  1.13.6.2  joerg #include "namespace.h"
     43  1.13.6.2  joerg #include <sys/types.h>
     44  1.13.6.2  joerg 
     45  1.13.6.2  joerg #include <assert.h>
     46  1.13.6.2  joerg #include <errno.h>
     47  1.13.6.2  joerg #include <stddef.h>
     48  1.13.6.2  joerg #include <stdio.h>
     49  1.13.6.2  joerg 
     50  1.13.6.2  joerg #include <db.h>
     51  1.13.6.2  joerg #include "btree.h"
     52  1.13.6.2  joerg 
     53  1.13.6.2  joerg /*
     54  1.13.6.2  joerg  * __BT_GET -- Get a record from the btree.
     55  1.13.6.2  joerg  *
     56  1.13.6.2  joerg  * Parameters:
     57  1.13.6.2  joerg  *	dbp:	pointer to access method
     58  1.13.6.2  joerg  *	key:	key to find
     59  1.13.6.2  joerg  *	data:	data to return
     60  1.13.6.2  joerg  *	flag:	currently unused
     61  1.13.6.2  joerg  *
     62  1.13.6.2  joerg  * Returns:
     63  1.13.6.2  joerg  *	RET_ERROR, RET_SUCCESS and RET_SPECIAL if the key not found.
     64  1.13.6.2  joerg  */
     65  1.13.6.2  joerg int
     66  1.13.6.2  joerg __bt_get(const DB *dbp, const DBT *key, DBT *data, u_int flags)
     67  1.13.6.2  joerg {
     68  1.13.6.2  joerg 	BTREE *t;
     69  1.13.6.2  joerg 	EPG *e;
     70  1.13.6.2  joerg 	int exact, status;
     71  1.13.6.2  joerg 
     72  1.13.6.2  joerg 	t = dbp->internal;
     73  1.13.6.2  joerg 
     74  1.13.6.2  joerg 	/* Toss any page pinned across calls. */
     75  1.13.6.2  joerg 	if (t->bt_pinned != NULL) {
     76  1.13.6.2  joerg 		mpool_put(t->bt_mp, t->bt_pinned, 0);
     77  1.13.6.2  joerg 		t->bt_pinned = NULL;
     78  1.13.6.2  joerg 	}
     79  1.13.6.2  joerg 
     80  1.13.6.2  joerg 	/* Get currently doesn't take any flags. */
     81  1.13.6.2  joerg 	if (flags) {
     82  1.13.6.2  joerg 		errno = EINVAL;
     83  1.13.6.2  joerg 		return (RET_ERROR);
     84  1.13.6.2  joerg 	}
     85  1.13.6.2  joerg 
     86  1.13.6.2  joerg 	if ((e = __bt_search(t, key, &exact)) == NULL)
     87  1.13.6.2  joerg 		return (RET_ERROR);
     88  1.13.6.2  joerg 	if (!exact) {
     89  1.13.6.2  joerg 		mpool_put(t->bt_mp, e->page, 0);
     90  1.13.6.2  joerg 		return (RET_SPECIAL);
     91  1.13.6.2  joerg 	}
     92  1.13.6.2  joerg 
     93  1.13.6.2  joerg 	status = __bt_ret(t, e, NULL, NULL, data, &t->bt_rdata, 0);
     94  1.13.6.2  joerg 
     95  1.13.6.2  joerg 	/*
     96  1.13.6.2  joerg 	 * If the user is doing concurrent access, we copied the
     97  1.13.6.2  joerg 	 * key/data, toss the page.
     98  1.13.6.2  joerg 	 */
     99  1.13.6.2  joerg 	if (F_ISSET(t, B_DB_LOCK))
    100  1.13.6.2  joerg 		mpool_put(t->bt_mp, e->page, 0);
    101  1.13.6.2  joerg 	else
    102  1.13.6.2  joerg 		t->bt_pinned = e->page;
    103  1.13.6.2  joerg 	return (status);
    104  1.13.6.2  joerg }
    105