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