xref: /netbsd-src/lib/libc/db/recno/rec_put.c (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1 /*	$NetBSD: rec_put.c,v 1.14 2007/02/03 23:46:09 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 #include <sys/cdefs.h>
33 #if defined(LIBC_SCCS) && !defined(lint)
34 #if 0
35 static char sccsid[] = "@(#)rec_put.c	8.7 (Berkeley) 8/18/94";
36 #else
37 __RCSID("$NetBSD: rec_put.c,v 1.14 2007/02/03 23:46:09 christos Exp $");
38 #endif
39 #endif /* LIBC_SCCS and not lint */
40 
41 #include "namespace.h"
42 #include <sys/types.h>
43 
44 #include <assert.h>
45 #include <errno.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49 
50 #include <db.h>
51 #include "recno.h"
52 
53 /*
54  * __REC_PUT -- Add a recno item to the tree.
55  *
56  * Parameters:
57  *	dbp:	pointer to access method
58  *	key:	key
59  *	data:	data
60  *	flag:	R_CURSOR, R_IAFTER, R_IBEFORE, R_NOOVERWRITE
61  *
62  * Returns:
63  *	RET_ERROR, RET_SUCCESS and RET_SPECIAL if the key is
64  *	already in the tree and R_NOOVERWRITE specified.
65  */
66 int
67 __rec_put(const DB *dbp, DBT *key, const DBT *data, u_int flags)
68 {
69 	BTREE *t;
70 	DBT fdata, tdata;
71 	recno_t nrec;
72 	int status;
73 
74 	t = dbp->internal;
75 
76 	/* Toss any page pinned across calls. */
77 	if (t->bt_pinned != NULL) {
78 		mpool_put(t->bt_mp, t->bt_pinned, 0);
79 		t->bt_pinned = NULL;
80 	}
81 
82 	/*
83 	 * If using fixed-length records, and the record is long, return
84 	 * EINVAL.  If it's short, pad it out.  Use the record data return
85 	 * memory, it's only short-term.
86 	 */
87 	if (F_ISSET(t, R_FIXLEN) && data->size != t->bt_reclen) {
88 		if (data->size > t->bt_reclen)
89 			goto einval;
90 
91 		if (t->bt_rdata.size < t->bt_reclen) {
92 			t->bt_rdata.data = t->bt_rdata.data == NULL ?
93 			    malloc(t->bt_reclen) :
94 			    realloc(t->bt_rdata.data, t->bt_reclen);
95 			if (t->bt_rdata.data == NULL)
96 				return (RET_ERROR);
97 			t->bt_rdata.size = t->bt_reclen;
98 		}
99 		memmove(t->bt_rdata.data, data->data, data->size);
100 		memset((char *)t->bt_rdata.data + data->size,
101 		    t->bt_bval, t->bt_reclen - data->size);
102 		fdata.data = t->bt_rdata.data;
103 		fdata.size = t->bt_reclen;
104 	} else {
105 		fdata.data = data->data;
106 		fdata.size = data->size;
107 	}
108 
109 	switch (flags) {
110 	case R_CURSOR:
111 		if (!F_ISSET(&t->bt_cursor, CURS_INIT))
112 			goto einval;
113 		nrec = t->bt_cursor.rcursor;
114 		break;
115 	case R_SETCURSOR:
116 		if ((nrec = *(recno_t *)key->data) == 0)
117 			goto einval;
118 		break;
119 	case R_IAFTER:
120 		if ((nrec = *(recno_t *)key->data) == 0) {
121 			nrec = 1;
122 			flags = R_IBEFORE;
123 		}
124 		break;
125 	case 0:
126 	case R_IBEFORE:
127 		if ((nrec = *(recno_t *)key->data) == 0)
128 			goto einval;
129 		break;
130 	case R_NOOVERWRITE:
131 		if ((nrec = *(recno_t *)key->data) == 0)
132 			goto einval;
133 		if (nrec <= t->bt_nrecs)
134 			return (RET_SPECIAL);
135 		break;
136 	default:
137 einval:		errno = EINVAL;
138 		return (RET_ERROR);
139 	}
140 
141 	/*
142 	 * Make sure that records up to and including the put record are
143 	 * already in the database.  If skipping records, create empty ones.
144 	 */
145 	if (nrec > t->bt_nrecs) {
146 		if (!F_ISSET(t, R_EOF | R_INMEM) &&
147 		    t->bt_irec(t, nrec) == RET_ERROR)
148 			return (RET_ERROR);
149 		if (nrec > t->bt_nrecs + 1) {
150 			if (F_ISSET(t, R_FIXLEN)) {
151 				if ((tdata.data =
152 				    (void *)malloc(t->bt_reclen)) == NULL)
153 					return (RET_ERROR);
154 				tdata.size = t->bt_reclen;
155 				memset(tdata.data, t->bt_bval, tdata.size);
156 			} else {
157 				tdata.data = NULL;
158 				tdata.size = 0;
159 			}
160 			while (nrec > t->bt_nrecs + 1)
161 				if (__rec_iput(t,
162 				    t->bt_nrecs, &tdata, 0) != RET_SUCCESS)
163 					return (RET_ERROR);
164 			if (F_ISSET(t, R_FIXLEN))
165 				free(tdata.data);
166 		}
167 	}
168 
169 	if ((status = __rec_iput(t, nrec - 1, &fdata, flags)) != RET_SUCCESS)
170 		return (status);
171 
172 	if (flags == R_SETCURSOR)
173 		t->bt_cursor.rcursor = nrec;
174 
175 	F_SET(t, R_MODIFIED);
176 	return (__rec_ret(t, NULL, nrec, key, NULL));
177 }
178 
179 /*
180  * __REC_IPUT -- Add a recno item to the tree.
181  *
182  * Parameters:
183  *	t:	tree
184  *	nrec:	record number
185  *	data:	data
186  *
187  * Returns:
188  *	RET_ERROR, RET_SUCCESS
189  */
190 int
191 __rec_iput(BTREE *t, recno_t nrec, const DBT *data, u_int flags)
192 {
193 	DBT tdata;
194 	EPG *e;
195 	PAGE *h;
196 	indx_t idx, nxtindex;
197 	pgno_t pg;
198 	u_int32_t nbytes;
199 	int dflags, status;
200 	char *dest, db[NOVFLSIZE];
201 
202 	/*
203 	 * If the data won't fit on a page, store it on indirect pages.
204 	 *
205 	 * XXX
206 	 * If the insert fails later on, these pages aren't recovered.
207 	 */
208 	if (data->size > t->bt_ovflsize) {
209 		if (__ovfl_put(t, data, &pg) == RET_ERROR)
210 			return (RET_ERROR);
211 		tdata.data = db;
212 		tdata.size = NOVFLSIZE;
213 		*(pgno_t *)(void *)db = pg;
214 		_DBFIT(data->size, u_int32_t);
215 		*(u_int32_t *)(void *)(db + sizeof(pgno_t)) =
216 		    (u_int32_t)data->size;
217 		dflags = P_BIGDATA;
218 		data = &tdata;
219 	} else
220 		dflags = 0;
221 
222 	/* __rec_search pins the returned page. */
223 	if ((e = __rec_search(t, nrec,
224 	    nrec > t->bt_nrecs || flags == R_IAFTER || flags == R_IBEFORE ?
225 	    SINSERT : SEARCH)) == NULL)
226 		return (RET_ERROR);
227 
228 	h = e->page;
229 	idx = e->index;
230 
231 	/*
232 	 * Add the specified key/data pair to the tree.  The R_IAFTER and
233 	 * R_IBEFORE flags insert the key after/before the specified key.
234 	 *
235 	 * Pages are split as required.
236 	 */
237 	switch (flags) {
238 	case R_IAFTER:
239 		++idx;
240 		break;
241 	case R_IBEFORE:
242 		break;
243 	default:
244 		if (nrec < t->bt_nrecs &&
245 		    __rec_dleaf(t, h, (u_int32_t)idx) == RET_ERROR) {
246 			mpool_put(t->bt_mp, h, 0);
247 			return (RET_ERROR);
248 		}
249 		break;
250 	}
251 
252 	/*
253 	 * If not enough room, split the page.  The split code will insert
254 	 * the key and data and unpin the current page.  If inserting into
255 	 * the offset array, shift the pointers up.
256 	 */
257 	nbytes = NRLEAFDBT(data->size);
258 	if ((u_int32_t) (h->upper - h->lower) < nbytes + sizeof(indx_t)) {
259 		status = __bt_split(t, h, NULL, data, dflags, nbytes,
260 		    (u_int32_t)idx);
261 		if (status == RET_SUCCESS)
262 			++t->bt_nrecs;
263 		return (status);
264 	}
265 
266 	if (idx < (nxtindex = NEXTINDEX(h)))
267 		memmove(h->linp + idx + 1, h->linp + idx,
268 		    (nxtindex - idx) * sizeof(indx_t));
269 	h->lower += sizeof(indx_t);
270 
271 	h->linp[idx] = h->upper -= nbytes;
272 	dest = (char *)(void *)h + h->upper;
273 	WR_RLEAF(dest, data, dflags);
274 
275 	++t->bt_nrecs;
276 	F_SET(t, B_MODIFIED);
277 	mpool_put(t->bt_mp, h, MPOOL_DIRTY);
278 
279 	return (RET_SUCCESS);
280 }
281