158f0484fSRodney W. Grimes /*- 2ef5d438eSPaul Traina * Copyright (c) 1990, 1993, 1994 358f0484fSRodney W. Grimes * The Regents of the University of California. All rights reserved. 458f0484fSRodney W. Grimes * 558f0484fSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 658f0484fSRodney W. Grimes * modification, are permitted provided that the following conditions 758f0484fSRodney W. Grimes * are met: 858f0484fSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 958f0484fSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 1058f0484fSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 1158f0484fSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 1258f0484fSRodney W. Grimes * documentation and/or other materials provided with the distribution. 1358f0484fSRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 1458f0484fSRodney W. Grimes * must display the following acknowledgement: 1558f0484fSRodney W. Grimes * This product includes software developed by the University of 1658f0484fSRodney W. Grimes * California, Berkeley and its contributors. 1758f0484fSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 1858f0484fSRodney W. Grimes * may be used to endorse or promote products derived from this software 1958f0484fSRodney W. Grimes * without specific prior written permission. 2058f0484fSRodney W. Grimes * 2158f0484fSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2258f0484fSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2358f0484fSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2458f0484fSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2558f0484fSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2658f0484fSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2758f0484fSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2858f0484fSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2958f0484fSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3058f0484fSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3158f0484fSRodney W. Grimes * SUCH DAMAGE. 3258f0484fSRodney W. Grimes */ 3358f0484fSRodney W. Grimes 3458f0484fSRodney W. Grimes #if defined(LIBC_SCCS) && !defined(lint) 35ef5d438eSPaul Traina static char sccsid[] = "@(#)rec_get.c 8.9 (Berkeley) 8/18/94"; 3658f0484fSRodney W. Grimes #endif /* LIBC_SCCS and not lint */ 3758f0484fSRodney W. Grimes 3858f0484fSRodney W. Grimes #include <sys/types.h> 3958f0484fSRodney W. Grimes 4058f0484fSRodney W. Grimes #include <errno.h> 4158f0484fSRodney W. Grimes #include <stddef.h> 4258f0484fSRodney W. Grimes #include <stdio.h> 4358f0484fSRodney W. Grimes #include <stdlib.h> 4458f0484fSRodney W. Grimes #include <string.h> 4558f0484fSRodney W. Grimes #include <unistd.h> 4658f0484fSRodney W. Grimes 4758f0484fSRodney W. Grimes #include <db.h> 4858f0484fSRodney W. Grimes #include "recno.h" 4958f0484fSRodney W. Grimes 5058f0484fSRodney W. Grimes /* 5158f0484fSRodney W. Grimes * __REC_GET -- Get a record from the btree. 5258f0484fSRodney W. Grimes * 5358f0484fSRodney W. Grimes * Parameters: 5458f0484fSRodney W. Grimes * dbp: pointer to access method 5558f0484fSRodney W. Grimes * key: key to find 5658f0484fSRodney W. Grimes * data: data to return 5758f0484fSRodney W. Grimes * flag: currently unused 5858f0484fSRodney W. Grimes * 5958f0484fSRodney W. Grimes * Returns: 6058f0484fSRodney W. Grimes * RET_ERROR, RET_SUCCESS and RET_SPECIAL if the key not found. 6158f0484fSRodney W. Grimes */ 6258f0484fSRodney W. Grimes int 6358f0484fSRodney W. Grimes __rec_get(dbp, key, data, flags) 6458f0484fSRodney W. Grimes const DB *dbp; 6558f0484fSRodney W. Grimes const DBT *key; 6658f0484fSRodney W. Grimes DBT *data; 6758f0484fSRodney W. Grimes u_int flags; 6858f0484fSRodney W. Grimes { 6958f0484fSRodney W. Grimes BTREE *t; 7058f0484fSRodney W. Grimes EPG *e; 7158f0484fSRodney W. Grimes recno_t nrec; 7258f0484fSRodney W. Grimes int status; 7358f0484fSRodney W. Grimes 7458f0484fSRodney W. Grimes t = dbp->internal; 7558f0484fSRodney W. Grimes 7658f0484fSRodney W. Grimes /* Toss any page pinned across calls. */ 7758f0484fSRodney W. Grimes if (t->bt_pinned != NULL) { 7858f0484fSRodney W. Grimes mpool_put(t->bt_mp, t->bt_pinned, 0); 7958f0484fSRodney W. Grimes t->bt_pinned = NULL; 8058f0484fSRodney W. Grimes } 8158f0484fSRodney W. Grimes 8258f0484fSRodney W. Grimes /* Get currently doesn't take any flags, and keys of 0 are illegal. */ 8358f0484fSRodney W. Grimes if (flags || (nrec = *(recno_t *)key->data) == 0) { 8458f0484fSRodney W. Grimes errno = EINVAL; 8558f0484fSRodney W. Grimes return (RET_ERROR); 8658f0484fSRodney W. Grimes } 8758f0484fSRodney W. Grimes 8858f0484fSRodney W. Grimes /* 8958f0484fSRodney W. Grimes * If we haven't seen this record yet, try to find it in the 9058f0484fSRodney W. Grimes * original file. 9158f0484fSRodney W. Grimes */ 9258f0484fSRodney W. Grimes if (nrec > t->bt_nrecs) { 93ef5d438eSPaul Traina if (F_ISSET(t, R_EOF | R_INMEM)) 9458f0484fSRodney W. Grimes return (RET_SPECIAL); 9558f0484fSRodney W. Grimes if ((status = t->bt_irec(t, nrec)) != RET_SUCCESS) 9658f0484fSRodney W. Grimes return (status); 9758f0484fSRodney W. Grimes } 9858f0484fSRodney W. Grimes 9958f0484fSRodney W. Grimes --nrec; 10058f0484fSRodney W. Grimes if ((e = __rec_search(t, nrec, SEARCH)) == NULL) 10158f0484fSRodney W. Grimes return (RET_ERROR); 10258f0484fSRodney W. Grimes 10358f0484fSRodney W. Grimes status = __rec_ret(t, e, 0, NULL, data); 104ef5d438eSPaul Traina if (F_ISSET(t, B_DB_LOCK)) 10558f0484fSRodney W. Grimes mpool_put(t->bt_mp, e->page, 0); 10658f0484fSRodney W. Grimes else 10758f0484fSRodney W. Grimes t->bt_pinned = e->page; 10858f0484fSRodney W. Grimes return (status); 10958f0484fSRodney W. Grimes } 11058f0484fSRodney W. Grimes 11158f0484fSRodney W. Grimes /* 11258f0484fSRodney W. Grimes * __REC_FPIPE -- Get fixed length records from a pipe. 11358f0484fSRodney W. Grimes * 11458f0484fSRodney W. Grimes * Parameters: 11558f0484fSRodney W. Grimes * t: tree 11658f0484fSRodney W. Grimes * cnt: records to read 11758f0484fSRodney W. Grimes * 11858f0484fSRodney W. Grimes * Returns: 11958f0484fSRodney W. Grimes * RET_ERROR, RET_SUCCESS 12058f0484fSRodney W. Grimes */ 12158f0484fSRodney W. Grimes int 12258f0484fSRodney W. Grimes __rec_fpipe(t, top) 12358f0484fSRodney W. Grimes BTREE *t; 12458f0484fSRodney W. Grimes recno_t top; 12558f0484fSRodney W. Grimes { 12658f0484fSRodney W. Grimes DBT data; 12758f0484fSRodney W. Grimes recno_t nrec; 12858f0484fSRodney W. Grimes size_t len; 12958f0484fSRodney W. Grimes int ch; 130ef5d438eSPaul Traina u_char *p; 13158f0484fSRodney W. Grimes 132ef5d438eSPaul Traina if (t->bt_rdata.size < t->bt_reclen) { 133ef5d438eSPaul Traina t->bt_rdata.data = t->bt_rdata.data == NULL ? 134ef5d438eSPaul Traina malloc(t->bt_reclen) : 135ef5d438eSPaul Traina realloc(t->bt_rdata.data, t->bt_reclen); 136ef5d438eSPaul Traina if (t->bt_rdata.data == NULL) 13758f0484fSRodney W. Grimes return (RET_ERROR); 138ef5d438eSPaul Traina t->bt_rdata.size = t->bt_reclen; 13958f0484fSRodney W. Grimes } 140ef5d438eSPaul Traina data.data = t->bt_rdata.data; 14158f0484fSRodney W. Grimes data.size = t->bt_reclen; 14258f0484fSRodney W. Grimes 143ef5d438eSPaul Traina for (nrec = t->bt_nrecs; nrec < top;) { 14458f0484fSRodney W. Grimes len = t->bt_reclen; 145ef5d438eSPaul Traina for (p = t->bt_rdata.data;; *p++ = ch) 146ef5d438eSPaul Traina if ((ch = getc(t->bt_rfp)) == EOF || !--len) { 147ef5d438eSPaul Traina if (ch != EOF) 148ef5d438eSPaul Traina *p = ch; 149ef5d438eSPaul Traina if (len != 0) 150ef5d438eSPaul Traina memset(p, t->bt_bval, len); 151ef5d438eSPaul Traina if (__rec_iput(t, 152ef5d438eSPaul Traina nrec, &data, 0) != RET_SUCCESS) 15358f0484fSRodney W. Grimes return (RET_ERROR); 154ef5d438eSPaul Traina ++nrec; 15558f0484fSRodney W. Grimes break; 15658f0484fSRodney W. Grimes } 15758f0484fSRodney W. Grimes if (ch == EOF) 15858f0484fSRodney W. Grimes break; 15958f0484fSRodney W. Grimes } 16058f0484fSRodney W. Grimes if (nrec < top) { 161ef5d438eSPaul Traina F_SET(t, R_EOF); 16258f0484fSRodney W. Grimes return (RET_SPECIAL); 16358f0484fSRodney W. Grimes } 16458f0484fSRodney W. Grimes return (RET_SUCCESS); 16558f0484fSRodney W. Grimes } 16658f0484fSRodney W. Grimes 16758f0484fSRodney W. Grimes /* 16858f0484fSRodney W. Grimes * __REC_VPIPE -- Get variable length records from a pipe. 16958f0484fSRodney W. Grimes * 17058f0484fSRodney W. Grimes * Parameters: 17158f0484fSRodney W. Grimes * t: tree 17258f0484fSRodney W. Grimes * cnt: records to read 17358f0484fSRodney W. Grimes * 17458f0484fSRodney W. Grimes * Returns: 17558f0484fSRodney W. Grimes * RET_ERROR, RET_SUCCESS 17658f0484fSRodney W. Grimes */ 17758f0484fSRodney W. Grimes int 17858f0484fSRodney W. Grimes __rec_vpipe(t, top) 17958f0484fSRodney W. Grimes BTREE *t; 18058f0484fSRodney W. Grimes recno_t top; 18158f0484fSRodney W. Grimes { 18258f0484fSRodney W. Grimes DBT data; 18358f0484fSRodney W. Grimes recno_t nrec; 18458f0484fSRodney W. Grimes indx_t len; 18558f0484fSRodney W. Grimes size_t sz; 18658f0484fSRodney W. Grimes int bval, ch; 187ef5d438eSPaul Traina u_char *p; 18858f0484fSRodney W. Grimes 18958f0484fSRodney W. Grimes bval = t->bt_bval; 19058f0484fSRodney W. Grimes for (nrec = t->bt_nrecs; nrec < top; ++nrec) { 191ef5d438eSPaul Traina for (p = t->bt_rdata.data, 192ef5d438eSPaul Traina sz = t->bt_rdata.size;; *p++ = ch, --sz) { 19358f0484fSRodney W. Grimes if ((ch = getc(t->bt_rfp)) == EOF || ch == bval) { 194ef5d438eSPaul Traina data.data = t->bt_rdata.data; 195ef5d438eSPaul Traina data.size = p - (u_char *)t->bt_rdata.data; 19658f0484fSRodney W. Grimes if (ch == EOF && data.size == 0) 19758f0484fSRodney W. Grimes break; 19858f0484fSRodney W. Grimes if (__rec_iput(t, nrec, &data, 0) 19958f0484fSRodney W. Grimes != RET_SUCCESS) 20058f0484fSRodney W. Grimes return (RET_ERROR); 20158f0484fSRodney W. Grimes break; 20258f0484fSRodney W. Grimes } 20358f0484fSRodney W. Grimes if (sz == 0) { 204ef5d438eSPaul Traina len = p - (u_char *)t->bt_rdata.data; 205ef5d438eSPaul Traina t->bt_rdata.size += (sz = 256); 206ef5d438eSPaul Traina t->bt_rdata.data = t->bt_rdata.data == NULL ? 207ef5d438eSPaul Traina malloc(t->bt_rdata.size) : 208ef5d438eSPaul Traina realloc(t->bt_rdata.data, t->bt_rdata.size); 209ef5d438eSPaul Traina if (t->bt_rdata.data == NULL) 21058f0484fSRodney W. Grimes return (RET_ERROR); 211ef5d438eSPaul Traina p = (u_char *)t->bt_rdata.data + len; 21258f0484fSRodney W. Grimes } 21358f0484fSRodney W. Grimes } 21458f0484fSRodney W. Grimes if (ch == EOF) 21558f0484fSRodney W. Grimes break; 21658f0484fSRodney W. Grimes } 21758f0484fSRodney W. Grimes if (nrec < top) { 218ef5d438eSPaul Traina F_SET(t, R_EOF); 21958f0484fSRodney W. Grimes return (RET_SPECIAL); 22058f0484fSRodney W. Grimes } 22158f0484fSRodney W. Grimes return (RET_SUCCESS); 22258f0484fSRodney W. Grimes } 22358f0484fSRodney W. Grimes 22458f0484fSRodney W. Grimes /* 22558f0484fSRodney W. Grimes * __REC_FMAP -- Get fixed length records from a file. 22658f0484fSRodney W. Grimes * 22758f0484fSRodney W. Grimes * Parameters: 22858f0484fSRodney W. Grimes * t: tree 22958f0484fSRodney W. Grimes * cnt: records to read 23058f0484fSRodney W. Grimes * 23158f0484fSRodney W. Grimes * Returns: 23258f0484fSRodney W. Grimes * RET_ERROR, RET_SUCCESS 23358f0484fSRodney W. Grimes */ 23458f0484fSRodney W. Grimes int 23558f0484fSRodney W. Grimes __rec_fmap(t, top) 23658f0484fSRodney W. Grimes BTREE *t; 23758f0484fSRodney W. Grimes recno_t top; 23858f0484fSRodney W. Grimes { 23958f0484fSRodney W. Grimes DBT data; 24058f0484fSRodney W. Grimes recno_t nrec; 241ef5d438eSPaul Traina u_char *sp, *ep, *p; 24258f0484fSRodney W. Grimes size_t len; 24358f0484fSRodney W. Grimes 244ef5d438eSPaul Traina if (t->bt_rdata.size < t->bt_reclen) { 245ef5d438eSPaul Traina t->bt_rdata.data = t->bt_rdata.data == NULL ? 246ef5d438eSPaul Traina malloc(t->bt_reclen) : 247ef5d438eSPaul Traina realloc(t->bt_rdata.data, t->bt_reclen); 248ef5d438eSPaul Traina if (t->bt_rdata.data == NULL) 24958f0484fSRodney W. Grimes return (RET_ERROR); 250ef5d438eSPaul Traina t->bt_rdata.size = t->bt_reclen; 25158f0484fSRodney W. Grimes } 252ef5d438eSPaul Traina data.data = t->bt_rdata.data; 25358f0484fSRodney W. Grimes data.size = t->bt_reclen; 25458f0484fSRodney W. Grimes 255ef5d438eSPaul Traina sp = (u_char *)t->bt_cmap; 256ef5d438eSPaul Traina ep = (u_char *)t->bt_emap; 25758f0484fSRodney W. Grimes for (nrec = t->bt_nrecs; nrec < top; ++nrec) { 25858f0484fSRodney W. Grimes if (sp >= ep) { 259ef5d438eSPaul Traina F_SET(t, R_EOF); 26058f0484fSRodney W. Grimes return (RET_SPECIAL); 26158f0484fSRodney W. Grimes } 26258f0484fSRodney W. Grimes len = t->bt_reclen; 263ef5d438eSPaul Traina for (p = t->bt_rdata.data; 264ef5d438eSPaul Traina sp < ep && len > 0; *p++ = *sp++, --len); 265ef5d438eSPaul Traina if (len != 0) 26658f0484fSRodney W. Grimes memset(p, t->bt_bval, len); 26758f0484fSRodney W. Grimes if (__rec_iput(t, nrec, &data, 0) != RET_SUCCESS) 26858f0484fSRodney W. Grimes return (RET_ERROR); 26958f0484fSRodney W. Grimes } 270ef5d438eSPaul Traina t->bt_cmap = (caddr_t)sp; 27158f0484fSRodney W. Grimes return (RET_SUCCESS); 27258f0484fSRodney W. Grimes } 27358f0484fSRodney W. Grimes 27458f0484fSRodney W. Grimes /* 27558f0484fSRodney W. Grimes * __REC_VMAP -- Get variable length records from a file. 27658f0484fSRodney W. Grimes * 27758f0484fSRodney W. Grimes * Parameters: 27858f0484fSRodney W. Grimes * t: tree 27958f0484fSRodney W. Grimes * cnt: records to read 28058f0484fSRodney W. Grimes * 28158f0484fSRodney W. Grimes * Returns: 28258f0484fSRodney W. Grimes * RET_ERROR, RET_SUCCESS 28358f0484fSRodney W. Grimes */ 28458f0484fSRodney W. Grimes int 28558f0484fSRodney W. Grimes __rec_vmap(t, top) 28658f0484fSRodney W. Grimes BTREE *t; 28758f0484fSRodney W. Grimes recno_t top; 28858f0484fSRodney W. Grimes { 28958f0484fSRodney W. Grimes DBT data; 290ef5d438eSPaul Traina u_char *sp, *ep; 29158f0484fSRodney W. Grimes recno_t nrec; 29258f0484fSRodney W. Grimes int bval; 29358f0484fSRodney W. Grimes 294ef5d438eSPaul Traina sp = (u_char *)t->bt_cmap; 295ef5d438eSPaul Traina ep = (u_char *)t->bt_emap; 29658f0484fSRodney W. Grimes bval = t->bt_bval; 29758f0484fSRodney W. Grimes 29858f0484fSRodney W. Grimes for (nrec = t->bt_nrecs; nrec < top; ++nrec) { 29958f0484fSRodney W. Grimes if (sp >= ep) { 300ef5d438eSPaul Traina F_SET(t, R_EOF); 30158f0484fSRodney W. Grimes return (RET_SPECIAL); 30258f0484fSRodney W. Grimes } 30358f0484fSRodney W. Grimes for (data.data = sp; sp < ep && *sp != bval; ++sp); 304ef5d438eSPaul Traina data.size = sp - (u_char *)data.data; 30558f0484fSRodney W. Grimes if (__rec_iput(t, nrec, &data, 0) != RET_SUCCESS) 30658f0484fSRodney W. Grimes return (RET_ERROR); 30758f0484fSRodney W. Grimes ++sp; 30858f0484fSRodney W. Grimes } 309ef5d438eSPaul Traina t->bt_cmap = (caddr_t)sp; 31058f0484fSRodney W. Grimes return (RET_SUCCESS); 31158f0484fSRodney W. Grimes } 312