2N/A/*-
2N/A * Copyright (c) 1990, 1993, 1994
2N/A * The Regents of the University of California. All rights reserved.
2N/A *
2N/A * Redistribution and use in source and binary forms, with or without
2N/A * modification, are permitted provided that the following conditions
2N/A * are met:
2N/A * 1. Redistributions of source code must retain the above copyright
2N/A * notice, this list of conditions and the following disclaimer.
2N/A * 2. Redistributions in binary form must reproduce the above copyright
2N/A * notice, this list of conditions and the following disclaimer in the
2N/A * documentation and/or other materials provided with the distribution.
2N/A * 3. All advertising materials mentioning features or use of this software
2N/A * must display the following acknowledgement:
2N/A * This product includes software developed by the University of
2N/A * California, Berkeley and its contributors.
2N/A * 4. Neither the name of the University nor the names of its contributors
2N/A * may be used to endorse or promote products derived from this software
2N/A * without specific prior written permission.
2N/A *
2N/A * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2N/A * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2N/A * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2N/A * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2N/A * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2N/A * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2N/A * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2N/A * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2N/A * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2N/A * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2N/A * SUCH DAMAGE.
2N/A */
2N/A
2N/A#if defined(LIBC_SCCS) && !defined(lint)
2N/Astatic char sccsid[] = "@(#)rec_get.c 8.9 (Berkeley) 8/18/94";
2N/A#endif /* LIBC_SCCS and not lint */
2N/A
2N/A#include <sys/types.h>
2N/A
2N/A#include <errno.h>
2N/A#include <stddef.h>
2N/A#include <stdio.h>
2N/A#include <stdlib.h>
2N/A#include <string.h>
2N/A#include <unistd.h>
2N/A
2N/A#include "db-int.h"
2N/A#include "recno.h"
2N/A
2N/A/*
2N/A * __REC_GET -- Get a record from the btree.
2N/A *
2N/A * Parameters:
2N/A * dbp: pointer to access method
2N/A * key: key to find
2N/A * data: data to return
2N/A * flag: currently unused
2N/A *
2N/A * Returns:
2N/A * RET_ERROR, RET_SUCCESS and RET_SPECIAL if the key not found.
2N/A */
2N/Aint
2N/A__rec_get(dbp, key, data, flags)
2N/A const DB *dbp;
2N/A const DBT *key;
2N/A DBT *data;
2N/A u_int flags;
2N/A{
2N/A BTREE *t;
2N/A EPG *e;
2N/A recno_t nrec;
2N/A int status;
2N/A
2N/A t = dbp->internal;
2N/A
2N/A /* Toss any page pinned across calls. */
2N/A if (t->bt_pinned != NULL) {
2N/A mpool_put(t->bt_mp, t->bt_pinned, 0);
2N/A t->bt_pinned = NULL;
2N/A }
2N/A
2N/A /* Get currently doesn't take any flags, and keys of 0 are illegal. */
2N/A if (flags || (nrec = *(recno_t *)key->data) == 0) {
2N/A errno = EINVAL;
2N/A return (RET_ERROR);
2N/A }
2N/A
2N/A /*
2N/A * If we haven't seen this record yet, try to find it in the
2N/A * original file.
2N/A */
2N/A if (nrec > t->bt_nrecs) {
2N/A if (F_ISSET(t, R_EOF | R_INMEM))
2N/A return (RET_SPECIAL);
2N/A if ((status = t->bt_irec(t, nrec)) != RET_SUCCESS)
2N/A return (status);
2N/A }
2N/A
2N/A --nrec;
2N/A if ((e = __rec_search(t, nrec, SEARCH)) == NULL)
2N/A return (RET_ERROR);
2N/A
2N/A status = __rec_ret(t, e, 0, NULL, data);
2N/A if (F_ISSET(t, B_DB_LOCK))
2N/A mpool_put(t->bt_mp, e->page, 0);
2N/A else
2N/A t->bt_pinned = e->page;
2N/A return (status);
2N/A}
2N/A
2N/A/*
2N/A * __REC_FPIPE -- Get fixed length records from a pipe.
2N/A *
2N/A * Parameters:
2N/A * t: tree
2N/A * cnt: records to read
2N/A *
2N/A * Returns:
2N/A * RET_ERROR, RET_SUCCESS
2N/A */
2N/Aint
2N/A__rec_fpipe(t, top)
2N/A BTREE *t;
2N/A recno_t top;
2N/A{
2N/A DBT data;
2N/A recno_t nrec;
2N/A size_t len;
2N/A int ch;
2N/A u_char *p;
2N/A
2N/A if (t->bt_rdata.size < t->bt_reclen) {
2N/A t->bt_rdata.data = t->bt_rdata.data == NULL ?
2N/A malloc(t->bt_reclen) :
2N/A realloc(t->bt_rdata.data, t->bt_reclen);
2N/A if (t->bt_rdata.data == NULL)
2N/A return (RET_ERROR);
2N/A t->bt_rdata.size = t->bt_reclen;
2N/A }
2N/A data.data = t->bt_rdata.data;
2N/A data.size = t->bt_reclen;
2N/A
2N/A for (nrec = t->bt_nrecs; nrec < top;) {
2N/A len = t->bt_reclen;
2N/A for (p = t->bt_rdata.data;; *p++ = ch)
2N/A if ((ch = getc(t->bt_rfp)) == EOF || !--len) {
2N/A if (ch != EOF)
2N/A *p = ch;
2N/A if (len != 0)
2N/A memset(p, t->bt_bval, len);
2N/A if (__rec_iput(t,
2N/A nrec, &data, 0) != RET_SUCCESS)
2N/A return (RET_ERROR);
2N/A ++nrec;
2N/A break;
2N/A }
2N/A if (ch == EOF)
2N/A break;
2N/A }
2N/A if (nrec < top) {
2N/A F_SET(t, R_EOF);
2N/A return (RET_SPECIAL);
2N/A }
2N/A return (RET_SUCCESS);
2N/A}
2N/A
2N/A/*
2N/A * __REC_VPIPE -- Get variable length records from a pipe.
2N/A *
2N/A * Parameters:
2N/A * t: tree
2N/A * cnt: records to read
2N/A *
2N/A * Returns:
2N/A * RET_ERROR, RET_SUCCESS
2N/A */
2N/Aint
2N/A__rec_vpipe(t, top)
2N/A BTREE *t;
2N/A recno_t top;
2N/A{
2N/A DBT data;
2N/A recno_t nrec;
2N/A indx_t len;
2N/A size_t sz;
2N/A int bval, ch;
2N/A u_char *p;
2N/A
2N/A bval = t->bt_bval;
2N/A for (nrec = t->bt_nrecs; nrec < top; ++nrec) {
2N/A for (p = t->bt_rdata.data,
2N/A sz = t->bt_rdata.size;; *p++ = ch, --sz) {
2N/A if ((ch = getc(t->bt_rfp)) == EOF || ch == bval) {
2N/A data.data = t->bt_rdata.data;
2N/A data.size = p - (u_char *)t->bt_rdata.data;
2N/A if (ch == EOF && data.size == 0)
2N/A break;
2N/A if (__rec_iput(t, nrec, &data, 0)
2N/A != RET_SUCCESS)
2N/A return (RET_ERROR);
2N/A break;
2N/A }
2N/A if (sz == 0) {
2N/A len = p - (u_char *)t->bt_rdata.data;
2N/A t->bt_rdata.size += (sz = 256);
2N/A t->bt_rdata.data = t->bt_rdata.data == NULL ?
2N/A malloc(t->bt_rdata.size) :
2N/A realloc(t->bt_rdata.data, t->bt_rdata.size);
2N/A if (t->bt_rdata.data == NULL)
2N/A return (RET_ERROR);
2N/A p = (u_char *)t->bt_rdata.data + len;
2N/A }
2N/A }
2N/A if (ch == EOF)
2N/A break;
2N/A }
2N/A if (nrec < top) {
2N/A F_SET(t, R_EOF);
2N/A return (RET_SPECIAL);
2N/A }
2N/A return (RET_SUCCESS);
2N/A}
2N/A
2N/A/*
2N/A * __REC_FMAP -- Get fixed length records from a file.
2N/A *
2N/A * Parameters:
2N/A * t: tree
2N/A * cnt: records to read
2N/A *
2N/A * Returns:
2N/A * RET_ERROR, RET_SUCCESS
2N/A */
2N/Aint
2N/A__rec_fmap(t, top)
2N/A BTREE *t;
2N/A recno_t top;
2N/A{
2N/A DBT data;
2N/A recno_t nrec;
2N/A u_char *sp, *ep, *p;
2N/A size_t len;
2N/A
2N/A if (t->bt_rdata.size < t->bt_reclen) {
2N/A t->bt_rdata.data = t->bt_rdata.data == NULL ?
2N/A malloc(t->bt_reclen) :
2N/A realloc(t->bt_rdata.data, t->bt_reclen);
2N/A if (t->bt_rdata.data == NULL)
2N/A return (RET_ERROR);
2N/A t->bt_rdata.size = t->bt_reclen;
2N/A }
2N/A data.data = t->bt_rdata.data;
2N/A data.size = t->bt_reclen;
2N/A
2N/A sp = (u_char *)t->bt_cmap;
2N/A ep = (u_char *)t->bt_emap;
2N/A for (nrec = t->bt_nrecs; nrec < top; ++nrec) {
2N/A if (sp >= ep) {
2N/A F_SET(t, R_EOF);
2N/A return (RET_SPECIAL);
2N/A }
2N/A len = t->bt_reclen;
2N/A for (p = t->bt_rdata.data;
2N/A sp < ep && len > 0; *p++ = *sp++, --len);
2N/A if (len != 0)
2N/A memset(p, t->bt_bval, len);
2N/A if (__rec_iput(t, nrec, &data, 0) != RET_SUCCESS)
2N/A return (RET_ERROR);
2N/A }
2N/A t->bt_cmap = (caddr_t)sp;
2N/A return (RET_SUCCESS);
2N/A}
2N/A
2N/A/*
2N/A * __REC_VMAP -- Get variable length records from a file.
2N/A *
2N/A * Parameters:
2N/A * t: tree
2N/A * cnt: records to read
2N/A *
2N/A * Returns:
2N/A * RET_ERROR, RET_SUCCESS
2N/A */
2N/Aint
2N/A__rec_vmap(t, top)
2N/A BTREE *t;
2N/A recno_t top;
2N/A{
2N/A DBT data;
2N/A u_char *sp, *ep;
2N/A recno_t nrec;
2N/A int bval;
2N/A
2N/A sp = (u_char *)t->bt_cmap;
2N/A ep = (u_char *)t->bt_emap;
2N/A bval = t->bt_bval;
2N/A
2N/A for (nrec = t->bt_nrecs; nrec < top; ++nrec) {
2N/A if (sp >= ep) {
2N/A F_SET(t, R_EOF);
2N/A return (RET_SPECIAL);
2N/A }
2N/A for (data.data = sp; sp < ep && *sp != bval; ++sp);
2N/A data.size = sp - (u_char *)data.data;
2N/A if (__rec_iput(t, nrec, &data, 0) != RET_SUCCESS)
2N/A return (RET_ERROR);
2N/A ++sp;
2N/A }
2N/A t->bt_cmap = (caddr_t)sp;
2N/A return (RET_SUCCESS);
2N/A}