1 /*- 2 * Copyright (c) 1990, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34 #if defined(LIBC_SCCS) && !defined(lint) 35 /* from: static char sccsid[] = "@(#)rec_get.c 8.2 (Berkeley) 9/7/93"; */ 36 static char *rcsid = "$Id: rec_get.c,v 1.4 1993/09/09 02:42:23 cgd Exp $"; 37 #endif /* LIBC_SCCS and not lint */ 38 39 #include <sys/types.h> 40 41 #include <errno.h> 42 #include <stddef.h> 43 #include <stdio.h> 44 #include <stdlib.h> 45 #include <string.h> 46 #include <unistd.h> 47 48 #include <db.h> 49 #include "recno.h" 50 51 /* 52 * __REC_GET -- Get a record from the btree. 53 * 54 * Parameters: 55 * dbp: pointer to access method 56 * key: key to find 57 * data: data to return 58 * flag: currently unused 59 * 60 * Returns: 61 * RET_ERROR, RET_SUCCESS and RET_SPECIAL if the key not found. 62 */ 63 int 64 __rec_get(dbp, key, data, flags) 65 const DB *dbp; 66 const DBT *key; 67 DBT *data; 68 u_int flags; 69 { 70 BTREE *t; 71 EPG *e; 72 recno_t nrec; 73 int status; 74 75 t = dbp->internal; 76 77 /* Toss any page pinned across calls. */ 78 if (t->bt_pinned != NULL) { 79 mpool_put(t->bt_mp, t->bt_pinned, 0); 80 t->bt_pinned = NULL; 81 } 82 83 /* Get currently doesn't take any flags, and keys of 0 are illegal. */ 84 if (flags || (nrec = *(recno_t *)key->data) == 0) { 85 errno = EINVAL; 86 return (RET_ERROR); 87 } 88 89 /* 90 * If we haven't seen this record yet, try to find it in the 91 * original file. 92 */ 93 if (nrec > t->bt_nrecs) { 94 if (ISSET(t, R_EOF | R_INMEM)) 95 return (RET_SPECIAL); 96 if ((status = t->bt_irec(t, nrec)) != RET_SUCCESS) 97 return (status); 98 } 99 100 --nrec; 101 if ((e = __rec_search(t, nrec, SEARCH)) == NULL) 102 return (RET_ERROR); 103 104 status = __rec_ret(t, e, 0, NULL, data); 105 if (ISSET(t, B_DB_LOCK)) 106 mpool_put(t->bt_mp, e->page, 0); 107 else 108 t->bt_pinned = e->page; 109 return (status); 110 } 111 112 /* 113 * __REC_FPIPE -- Get fixed length records from a pipe. 114 * 115 * Parameters: 116 * t: tree 117 * cnt: records to read 118 * 119 * Returns: 120 * RET_ERROR, RET_SUCCESS 121 */ 122 int 123 __rec_fpipe(t, top) 124 BTREE *t; 125 recno_t top; 126 { 127 DBT data; 128 recno_t nrec; 129 size_t len; 130 int ch; 131 char *p; 132 133 data.data = t->bt_dbuf; 134 data.size = t->bt_reclen; 135 136 if (t->bt_dbufsz < t->bt_reclen) { 137 if ((t->bt_dbuf = realloc(t->bt_dbuf, t->bt_reclen)) == NULL) 138 return (RET_ERROR); 139 t->bt_dbufsz = t->bt_reclen; 140 } 141 for (nrec = t->bt_nrecs; nrec < top; ++nrec) { 142 len = t->bt_reclen; 143 for (p = t->bt_dbuf;; *p++ = ch) 144 if ((ch = getc(t->bt_rfp)) == EOF || !len--) { 145 if (__rec_iput(t, nrec, &data, 0) 146 != RET_SUCCESS) 147 return (RET_ERROR); 148 break; 149 } 150 if (ch == EOF) 151 break; 152 } 153 if (nrec < top) { 154 SET(t, R_EOF); 155 return (RET_SPECIAL); 156 } 157 return (RET_SUCCESS); 158 } 159 160 /* 161 * __REC_VPIPE -- Get variable length records from a pipe. 162 * 163 * Parameters: 164 * t: tree 165 * cnt: records to read 166 * 167 * Returns: 168 * RET_ERROR, RET_SUCCESS 169 */ 170 int 171 __rec_vpipe(t, top) 172 BTREE *t; 173 recno_t top; 174 { 175 DBT data; 176 recno_t nrec; 177 indx_t len; 178 size_t sz; 179 int bval, ch; 180 char *p; 181 182 bval = t->bt_bval; 183 for (nrec = t->bt_nrecs; nrec < top; ++nrec) { 184 for (p = t->bt_dbuf, sz = t->bt_dbufsz;; *p++ = ch, --sz) { 185 if ((ch = getc(t->bt_rfp)) == EOF || ch == bval) { 186 data.data = t->bt_dbuf; 187 data.size = p - t->bt_dbuf; 188 if (ch == EOF && data.size == 0) 189 break; 190 if (__rec_iput(t, nrec, &data, 0) 191 != RET_SUCCESS) 192 return (RET_ERROR); 193 break; 194 } 195 if (sz == 0) { 196 len = p - t->bt_dbuf; 197 t->bt_dbufsz += (sz = 256); 198 if ((t->bt_dbuf = 199 realloc(t->bt_dbuf, t->bt_dbufsz)) == NULL) 200 return (RET_ERROR); 201 p = t->bt_dbuf + len; 202 } 203 } 204 if (ch == EOF) 205 break; 206 } 207 if (nrec < top) { 208 SET(t, R_EOF); 209 return (RET_SPECIAL); 210 } 211 return (RET_SUCCESS); 212 } 213 214 /* 215 * __REC_FMAP -- Get fixed length records from a file. 216 * 217 * Parameters: 218 * t: tree 219 * cnt: records to read 220 * 221 * Returns: 222 * RET_ERROR, RET_SUCCESS 223 */ 224 int 225 __rec_fmap(t, top) 226 BTREE *t; 227 recno_t top; 228 { 229 DBT data; 230 recno_t nrec; 231 caddr_t sp, ep; 232 size_t len; 233 char *p; 234 235 sp = t->bt_cmap; 236 ep = t->bt_emap; 237 data.data = t->bt_dbuf; 238 data.size = t->bt_reclen; 239 240 if (t->bt_dbufsz < t->bt_reclen) { 241 if ((t->bt_dbuf = realloc(t->bt_dbuf, t->bt_reclen)) == NULL) 242 return (RET_ERROR); 243 t->bt_dbufsz = t->bt_reclen; 244 } 245 for (nrec = t->bt_nrecs; nrec < top; ++nrec) { 246 if (sp >= ep) { 247 SET(t, R_EOF); 248 return (RET_SPECIAL); 249 } 250 len = t->bt_reclen; 251 for (p = t->bt_dbuf; sp < ep && len--; *p++ = *sp++); 252 memset(p, t->bt_bval, len); 253 if (__rec_iput(t, nrec, &data, 0) != RET_SUCCESS) 254 return (RET_ERROR); 255 } 256 t->bt_cmap = sp; 257 return (RET_SUCCESS); 258 } 259 260 /* 261 * __REC_VMAP -- Get variable length records from a file. 262 * 263 * Parameters: 264 * t: tree 265 * cnt: records to read 266 * 267 * Returns: 268 * RET_ERROR, RET_SUCCESS 269 */ 270 int 271 __rec_vmap(t, top) 272 BTREE *t; 273 recno_t top; 274 { 275 DBT data; 276 caddr_t sp, ep; 277 recno_t nrec; 278 int bval; 279 280 sp = t->bt_cmap; 281 ep = t->bt_emap; 282 bval = t->bt_bval; 283 284 for (nrec = t->bt_nrecs; nrec < top; ++nrec) { 285 if (sp >= ep) { 286 SET(t, R_EOF); 287 return (RET_SPECIAL); 288 } 289 for (data.data = sp; sp < ep && *sp != bval; ++sp); 290 data.size = sp - (caddr_t)data.data; 291 if (__rec_iput(t, nrec, &data, 0) != RET_SUCCESS) 292 return (RET_ERROR); 293 ++sp; 294 } 295 t->bt_cmap = sp; 296 return (RET_SUCCESS); 297 } 298