Home | History | Annotate | Line # | Download | only in recno
      1 /*	$NetBSD: rec_put.c,v 1.21 2013/12/14 18:04:56 christos 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. Neither the name of the University nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  */
     31 
     32 #if HAVE_NBTOOL_CONFIG_H
     33 #include "nbtool_config.h"
     34 #endif
     35 
     36 #include <sys/cdefs.h>
     37 __RCSID("$NetBSD: rec_put.c,v 1.21 2013/12/14 18:04:56 christos Exp $");
     38 
     39 #include "namespace.h"
     40 #include <sys/types.h>
     41 
     42 #include <assert.h>
     43 #include <errno.h>
     44 #include <stdio.h>
     45 #include <stdlib.h>
     46 #include <string.h>
     47 
     48 #include <db.h>
     49 #include "recno.h"
     50 
     51 /*
     52  * __REC_PUT -- Add a recno item to the tree.
     53  *
     54  * Parameters:
     55  *	dbp:	pointer to access method
     56  *	key:	key
     57  *	data:	data
     58  *	flag:	R_CURSOR, R_IAFTER, R_IBEFORE, R_NOOVERWRITE
     59  *
     60  * Returns:
     61  *	RET_ERROR, RET_SUCCESS and RET_SPECIAL if the key is
     62  *	already in the tree and R_NOOVERWRITE specified.
     63  */
     64 int
     65 __rec_put(const DB *dbp, DBT *key, const DBT *data, u_int flags)
     66 {
     67 	BTREE *t;
     68 	DBT fdata, tdata;
     69 	recno_t nrec;
     70 	int status;
     71 
     72 	t = dbp->internal;
     73 
     74 	/* Toss any page pinned across calls. */
     75 	if (t->bt_pinned != NULL) {
     76 		mpool_put(t->bt_mp, t->bt_pinned, 0);
     77 		t->bt_pinned = NULL;
     78 	}
     79 
     80 	/*
     81 	 * If using fixed-length records, and the record is long, return
     82 	 * EINVAL.  If it's short, pad it out.  Use the record data return
     83 	 * memory, it's only short-term.
     84 	 */
     85 	if (F_ISSET(t, R_FIXLEN) && data->size != t->bt_reclen) {
     86 		if (data->size > t->bt_reclen)
     87 			goto einval;
     88 
     89 		if (t->bt_rdata.size < t->bt_reclen) {
     90 			void *np = realloc(t->bt_rdata.data, t->bt_reclen);
     91 			if (np == NULL)
     92 				return (RET_ERROR);
     93 			t->bt_rdata.data = np;
     94 			t->bt_rdata.size = t->bt_reclen;
     95 		}
     96 		memmove(t->bt_rdata.data, data->data, data->size);
     97 		memset((char *)t->bt_rdata.data + data->size,
     98 		    t->bt_bval, t->bt_reclen - data->size);
     99 		fdata.data = t->bt_rdata.data;
    100 		fdata.size = t->bt_reclen;
    101 	} else {
    102 		fdata.data = data->data;
    103 		fdata.size = data->size;
    104 	}
    105 
    106 	switch (flags) {
    107 	case R_CURSOR:
    108 		if (!F_ISSET(&t->bt_cursor, CURS_INIT))
    109 			goto einval;
    110 		nrec = t->bt_cursor.rcursor;
    111 		break;
    112 	case R_SETCURSOR:
    113 		if ((nrec = *(recno_t *)key->data) == 0)
    114 			goto einval;
    115 		break;
    116 	case R_IAFTER:
    117 		if ((nrec = *(recno_t *)key->data) == 0) {
    118 			nrec = 1;
    119 			flags = R_IBEFORE;
    120 		}
    121 		break;
    122 	case 0:
    123 	case R_IBEFORE:
    124 		if ((nrec = *(recno_t *)key->data) == 0)
    125 			goto einval;
    126 		break;
    127 	case R_NOOVERWRITE:
    128 		if ((nrec = *(recno_t *)key->data) == 0)
    129 			goto einval;
    130 		if (nrec <= t->bt_nrecs)
    131 			return (RET_SPECIAL);
    132 		break;
    133 	default:
    134 einval:		errno = EINVAL;
    135 		return (RET_ERROR);
    136 	}
    137 
    138 	/*
    139 	 * Make sure that records up to and including the put record are
    140 	 * already in the database.  If skipping records, create empty ones.
    141 	 */
    142 	if (nrec > t->bt_nrecs) {
    143 		if (!F_ISSET(t, R_EOF | R_INMEM) &&
    144 		    t->bt_irec(t, nrec) == RET_ERROR)
    145 			return (RET_ERROR);
    146 		if (nrec > t->bt_nrecs + 1) {
    147 			if (F_ISSET(t, R_FIXLEN)) {
    148 				if ((tdata.data = malloc(t->bt_reclen)) == NULL)
    149 					return (RET_ERROR);
    150 				tdata.size = t->bt_reclen;
    151 				memset(tdata.data, t->bt_bval, tdata.size);
    152 			} else {
    153 				tdata.data = NULL;
    154 				tdata.size = 0;
    155 			}
    156 			while (nrec > t->bt_nrecs + 1)
    157 				if (__rec_iput(t,
    158 				    t->bt_nrecs, &tdata, 0) != RET_SUCCESS)
    159 					return (RET_ERROR);
    160 			if (F_ISSET(t, R_FIXLEN))
    161 				free(tdata.data);
    162 		}
    163 	}
    164 
    165 	if ((status = __rec_iput(t, nrec - 1, &fdata, flags)) != RET_SUCCESS)
    166 		return (status);
    167 
    168 	switch (flags) {
    169 	case R_IAFTER:
    170 		nrec++;
    171 		break;
    172 	case R_SETCURSOR:
    173 		t->bt_cursor.rcursor = nrec;
    174 		break;
    175 	}
    176 
    177 	F_SET(t, R_MODIFIED);
    178 	return (__rec_ret(t, NULL, nrec, key, NULL));
    179 }
    180 
    181 /*
    182  * __REC_IPUT -- Add a recno item to the tree.
    183  *
    184  * Parameters:
    185  *	t:	tree
    186  *	nrec:	record number
    187  *	data:	data
    188  *
    189  * Returns:
    190  *	RET_ERROR, RET_SUCCESS
    191  */
    192 int
    193 __rec_iput(BTREE *t, recno_t nrec, const DBT *data, u_int flags)
    194 {
    195 	DBT tdata;
    196 	EPG *e;
    197 	PAGE *h;
    198 	indx_t idx, nxtindex;
    199 	pgno_t pg;
    200 	uint32_t nbytes;
    201 	int dflags, status;
    202 	char *dest, db[NOVFLSIZE];
    203 
    204 	/*
    205 	 * If the data won't fit on a page, store it on indirect pages.
    206 	 *
    207 	 * XXX
    208 	 * If the insert fails later on, these pages aren't recovered.
    209 	 */
    210 	if (data->size > t->bt_ovflsize) {
    211 		if (__ovfl_put(t, data, &pg) == RET_ERROR)
    212 			return (RET_ERROR);
    213 		tdata.data = db;
    214 		tdata.size = NOVFLSIZE;
    215 		memcpy(db, &pg, sizeof(pg));
    216 		_DBFIT(data->size, uint32_t);
    217 		*(uint32_t *)(void *)(db + sizeof(pgno_t)) =
    218 		    (uint32_t)data->size;
    219 		dflags = P_BIGDATA;
    220 		data = &tdata;
    221 	} else
    222 		dflags = 0;
    223 
    224 	/* __rec_search pins the returned page. */
    225 	if ((e = __rec_search(t, nrec,
    226 	    nrec > t->bt_nrecs || flags == R_IAFTER || flags == R_IBEFORE ?
    227 	    SINSERT : SEARCH)) == NULL)
    228 		return (RET_ERROR);
    229 
    230 	h = e->page;
    231 	idx = e->index;
    232 
    233 	/*
    234 	 * Add the specified key/data pair to the tree.  The R_IAFTER and
    235 	 * R_IBEFORE flags insert the key after/before the specified key.
    236 	 *
    237 	 * Pages are split as required.
    238 	 */
    239 	switch (flags) {
    240 	case R_IAFTER:
    241 		++idx;
    242 		break;
    243 	case R_IBEFORE:
    244 		break;
    245 	default:
    246 		if (nrec < t->bt_nrecs &&
    247 		    __rec_dleaf(t, h, (uint32_t)idx) == RET_ERROR) {
    248 			mpool_put(t->bt_mp, h, 0);
    249 			return (RET_ERROR);
    250 		}
    251 		break;
    252 	}
    253 
    254 	/*
    255 	 * If not enough room, split the page.  The split code will insert
    256 	 * the key and data and unpin the current page.  If inserting into
    257 	 * the offset array, shift the pointers up.
    258 	 */
    259 	nbytes = NRLEAFDBT(data->size);
    260 	if ((uint32_t) (h->upper - h->lower) < nbytes + sizeof(indx_t)) {
    261 		status = __bt_split(t, h, NULL, data, dflags, nbytes,
    262 		    (uint32_t)idx);
    263 		if (status == RET_SUCCESS)
    264 			++t->bt_nrecs;
    265 		return (status);
    266 	}
    267 
    268 	if (idx < (nxtindex = NEXTINDEX(h)))
    269 		memmove(h->linp + idx + 1, h->linp + idx,
    270 		    (nxtindex - idx) * sizeof(indx_t));
    271 	h->lower += sizeof(indx_t);
    272 
    273 	h->linp[idx] = h->upper -= nbytes;
    274 	dest = (char *)(void *)h + h->upper;
    275 	WR_RLEAF(dest, data, dflags);
    276 
    277 	++t->bt_nrecs;
    278 	F_SET(t, B_MODIFIED);
    279 	mpool_put(t->bt_mp, h, MPOOL_DIRTY);
    280 
    281 	return (RET_SUCCESS);
    282 }
    283