Home | History | Annotate | Line # | Download | only in recno
rec_utils.c revision 1.12.6.2
      1  1.12.6.2  joerg /*	$NetBSD: rec_utils.c,v 1.12.6.2 2008/09/10 17:52:37 joerg Exp $	*/
      2  1.12.6.2  joerg 
      3  1.12.6.2  joerg /*-
      4  1.12.6.2  joerg  * Copyright (c) 1990, 1993, 1994
      5  1.12.6.2  joerg  *	The Regents of the University of California.  All rights reserved.
      6  1.12.6.2  joerg  *
      7  1.12.6.2  joerg  * Redistribution and use in source and binary forms, with or without
      8  1.12.6.2  joerg  * modification, are permitted provided that the following conditions
      9  1.12.6.2  joerg  * are met:
     10  1.12.6.2  joerg  * 1. Redistributions of source code must retain the above copyright
     11  1.12.6.2  joerg  *    notice, this list of conditions and the following disclaimer.
     12  1.12.6.2  joerg  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.12.6.2  joerg  *    notice, this list of conditions and the following disclaimer in the
     14  1.12.6.2  joerg  *    documentation and/or other materials provided with the distribution.
     15  1.12.6.2  joerg  * 3. Neither the name of the University nor the names of its contributors
     16  1.12.6.2  joerg  *    may be used to endorse or promote products derived from this software
     17  1.12.6.2  joerg  *    without specific prior written permission.
     18  1.12.6.2  joerg  *
     19  1.12.6.2  joerg  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  1.12.6.2  joerg  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  1.12.6.2  joerg  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  1.12.6.2  joerg  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  1.12.6.2  joerg  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  1.12.6.2  joerg  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  1.12.6.2  joerg  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  1.12.6.2  joerg  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  1.12.6.2  joerg  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  1.12.6.2  joerg  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  1.12.6.2  joerg  * SUCH DAMAGE.
     30  1.12.6.2  joerg  */
     31  1.12.6.2  joerg 
     32  1.12.6.2  joerg #if HAVE_NBTOOL_CONFIG_H
     33  1.12.6.2  joerg #include "nbtool_config.h"
     34  1.12.6.2  joerg #endif
     35  1.12.6.2  joerg 
     36  1.12.6.2  joerg #include <sys/cdefs.h>
     37  1.12.6.2  joerg __RCSID("$NetBSD: rec_utils.c,v 1.12.6.2 2008/09/10 17:52:37 joerg Exp $");
     38  1.12.6.2  joerg 
     39  1.12.6.2  joerg #include <sys/param.h>
     40  1.12.6.2  joerg 
     41  1.12.6.2  joerg #include <assert.h>
     42  1.12.6.2  joerg #include <stdio.h>
     43  1.12.6.2  joerg #include <stdlib.h>
     44  1.12.6.2  joerg #include <string.h>
     45  1.12.6.2  joerg 
     46  1.12.6.2  joerg #include <db.h>
     47  1.12.6.2  joerg #include "recno.h"
     48  1.12.6.2  joerg 
     49  1.12.6.2  joerg /*
     50  1.12.6.2  joerg  * __rec_ret --
     51  1.12.6.2  joerg  *	Build return data.
     52  1.12.6.2  joerg  *
     53  1.12.6.2  joerg  * Parameters:
     54  1.12.6.2  joerg  *	t:	tree
     55  1.12.6.2  joerg  *	e:	key/data pair to be returned
     56  1.12.6.2  joerg  *   nrec:	record number
     57  1.12.6.2  joerg  *    key:	user's key structure
     58  1.12.6.2  joerg  *	data:	user's data structure
     59  1.12.6.2  joerg  *
     60  1.12.6.2  joerg  * Returns:
     61  1.12.6.2  joerg  *	RET_SUCCESS, RET_ERROR.
     62  1.12.6.2  joerg  */
     63  1.12.6.2  joerg int
     64  1.12.6.2  joerg __rec_ret(BTREE *t, EPG *e, recno_t nrec, DBT *key, DBT *data)
     65  1.12.6.2  joerg {
     66  1.12.6.2  joerg 	RLEAF *rl;
     67  1.12.6.2  joerg 	void *p;
     68  1.12.6.2  joerg 
     69  1.12.6.2  joerg 	if (key == NULL)
     70  1.12.6.2  joerg 		goto dataonly;
     71  1.12.6.2  joerg 
     72  1.12.6.2  joerg 	/* We have to copy the key, it's not on the page. */
     73  1.12.6.2  joerg 	if (sizeof(recno_t) > t->bt_rkey.size) {
     74  1.12.6.2  joerg 		p = (void *)(t->bt_rkey.data == NULL ?
     75  1.12.6.2  joerg 		    malloc(sizeof(recno_t)) :
     76  1.12.6.2  joerg 		    realloc(t->bt_rkey.data, sizeof(recno_t)));
     77  1.12.6.2  joerg 		if (p == NULL)
     78  1.12.6.2  joerg 			return (RET_ERROR);
     79  1.12.6.2  joerg 		t->bt_rkey.data = p;
     80  1.12.6.2  joerg 		t->bt_rkey.size = sizeof(recno_t);
     81  1.12.6.2  joerg 	}
     82  1.12.6.2  joerg 	memmove(t->bt_rkey.data, &nrec, sizeof(recno_t));
     83  1.12.6.2  joerg 	key->size = sizeof(recno_t);
     84  1.12.6.2  joerg 	key->data = t->bt_rkey.data;
     85  1.12.6.2  joerg 
     86  1.12.6.2  joerg dataonly:
     87  1.12.6.2  joerg 	if (data == NULL)
     88  1.12.6.2  joerg 		return (RET_SUCCESS);
     89  1.12.6.2  joerg 
     90  1.12.6.2  joerg 	/*
     91  1.12.6.2  joerg 	 * We must copy big keys/data to make them contigous.  Otherwise,
     92  1.12.6.2  joerg 	 * leave the page pinned and don't copy unless the user specified
     93  1.12.6.2  joerg 	 * concurrent access.
     94  1.12.6.2  joerg 	 */
     95  1.12.6.2  joerg 	rl = GETRLEAF(e->page, e->index);
     96  1.12.6.2  joerg 	if (rl->flags & P_BIGDATA) {
     97  1.12.6.2  joerg 		if (__ovfl_get(t, rl->bytes,
     98  1.12.6.2  joerg 		    &data->size, &t->bt_rdata.data, &t->bt_rdata.size))
     99  1.12.6.2  joerg 			return (RET_ERROR);
    100  1.12.6.2  joerg 		data->data = t->bt_rdata.data;
    101  1.12.6.2  joerg 	} else if (F_ISSET(t, B_DB_LOCK)) {
    102  1.12.6.2  joerg 		/* Use +1 in case the first record retrieved is 0 length. */
    103  1.12.6.2  joerg 		if (rl->dsize + 1 > t->bt_rdata.size) {
    104  1.12.6.2  joerg 			p = (void *)(t->bt_rdata.data == NULL ?
    105  1.12.6.2  joerg 			    malloc(rl->dsize + 1) :
    106  1.12.6.2  joerg 			    realloc(t->bt_rdata.data, rl->dsize + 1));
    107  1.12.6.2  joerg 			if (p == NULL)
    108  1.12.6.2  joerg 				return (RET_ERROR);
    109  1.12.6.2  joerg 			t->bt_rdata.data = p;
    110  1.12.6.2  joerg 			t->bt_rdata.size = rl->dsize + 1;
    111  1.12.6.2  joerg 		}
    112  1.12.6.2  joerg 		memmove(t->bt_rdata.data, rl->bytes, rl->dsize);
    113  1.12.6.2  joerg 		data->size = rl->dsize;
    114  1.12.6.2  joerg 		data->data = t->bt_rdata.data;
    115  1.12.6.2  joerg 	} else {
    116  1.12.6.2  joerg 		data->size = rl->dsize;
    117  1.12.6.2  joerg 		data->data = rl->bytes;
    118  1.12.6.2  joerg 	}
    119  1.12.6.2  joerg 	return (RET_SUCCESS);
    120  1.12.6.2  joerg }
    121