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