xref: /dflybsd-src/lib/libc/db/recno/rec_put.c (revision ee61f228c1c38342493a01fe774b83cffc0f4be9)
1 /*-
2  * Copyright (c) 1990, 1993, 1994
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * @(#)rec_put.c	8.7 (Berkeley) 8/18/94
30  * $FreeBSD: src/lib/libc/db/recno/rec_put.c,v 1.4.6.1 2001/01/02 05:13:25 peter Exp $
31  * $DragonFly: src/lib/libc/db/recno/rec_put.c,v 1.3 2005/09/19 09:20:37 asmodai Exp $
32  */
33 
34 #include <sys/types.h>
35 
36 #include <errno.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 
41 #include <db.h>
42 #include "recno.h"
43 
44 /*
45  * __REC_PUT -- Add a recno item to the tree.
46  *
47  * Parameters:
48  *	dbp:	pointer to access method
49  *	key:	key
50  *	data:	data
51  *	flag:	R_CURSOR, R_IAFTER, R_IBEFORE, R_NOOVERWRITE
52  *
53  * Returns:
54  *	RET_ERROR, RET_SUCCESS and RET_SPECIAL if the key is
55  *	already in the tree and R_NOOVERWRITE specified.
56  */
57 int
58 __rec_put(dbp, key, data, flags)
59 	const DB *dbp;
60 	DBT *key;
61 	const DBT *data;
62 	u_int flags;
63 {
64 	BTREE *t;
65 	DBT fdata, tdata;
66 	recno_t nrec;
67 	int status;
68 
69 	t = dbp->internal;
70 
71 	/* Toss any page pinned across calls. */
72 	if (t->bt_pinned != NULL) {
73 		mpool_put(t->bt_mp, t->bt_pinned, 0);
74 		t->bt_pinned = NULL;
75 	}
76 
77 	/*
78 	 * If using fixed-length records, and the record is long, return
79 	 * EINVAL.  If it's short, pad it out.  Use the record data return
80 	 * memory, it's only short-term.
81 	 */
82 	if (F_ISSET(t, R_FIXLEN) && data->size != t->bt_reclen) {
83 		if (data->size > t->bt_reclen)
84 			goto einval;
85 
86 		if (t->bt_rdata.size < t->bt_reclen) {
87 			t->bt_rdata.data =
88 			    reallocf(t->bt_rdata.data, t->bt_reclen);
89 			if (t->bt_rdata.data == NULL)
90 				return (RET_ERROR);
91 			t->bt_rdata.size = t->bt_reclen;
92 		}
93 		memmove(t->bt_rdata.data, data->data, data->size);
94 		memset((char *)t->bt_rdata.data + data->size,
95 		    t->bt_bval, t->bt_reclen - data->size);
96 		fdata.data = t->bt_rdata.data;
97 		fdata.size = t->bt_reclen;
98 	} else {
99 		fdata.data = data->data;
100 		fdata.size = data->size;
101 	}
102 
103 	switch (flags) {
104 	case R_CURSOR:
105 		if (!F_ISSET(&t->bt_cursor, CURS_INIT))
106 			goto einval;
107 		nrec = t->bt_cursor.rcursor;
108 		break;
109 	case R_SETCURSOR:
110 		if ((nrec = *(recno_t *)key->data) == 0)
111 			goto einval;
112 		break;
113 	case R_IAFTER:
114 		if ((nrec = *(recno_t *)key->data) == 0) {
115 			nrec = 1;
116 			flags = R_IBEFORE;
117 		}
118 		break;
119 	case 0:
120 	case R_IBEFORE:
121 		if ((nrec = *(recno_t *)key->data) == 0)
122 			goto einval;
123 		break;
124 	case R_NOOVERWRITE:
125 		if ((nrec = *(recno_t *)key->data) == 0)
126 			goto einval;
127 		if (nrec <= t->bt_nrecs)
128 			return (RET_SPECIAL);
129 		break;
130 	default:
131 einval:		errno = EINVAL;
132 		return (RET_ERROR);
133 	}
134 
135 	/*
136 	 * Make sure that records up to and including the put record are
137 	 * already in the database.  If skipping records, create empty ones.
138 	 */
139 	if (nrec > t->bt_nrecs) {
140 		if (!F_ISSET(t, R_EOF | R_INMEM) &&
141 		    t->bt_irec(t, nrec) == RET_ERROR)
142 			return (RET_ERROR);
143 		if (nrec > t->bt_nrecs + 1) {
144 			if (F_ISSET(t, R_FIXLEN)) {
145 				if ((tdata.data =
146 				    (void *)malloc(t->bt_reclen)) == NULL)
147 					return (RET_ERROR);
148 				tdata.size = t->bt_reclen;
149 				memset(tdata.data, t->bt_bval, tdata.size);
150 			} else {
151 				tdata.data = NULL;
152 				tdata.size = 0;
153 			}
154 			while (nrec > t->bt_nrecs + 1)
155 				if (__rec_iput(t,
156 				    t->bt_nrecs, &tdata, 0) != RET_SUCCESS)
157 					return (RET_ERROR);
158 			if (F_ISSET(t, R_FIXLEN))
159 				free(tdata.data);
160 		}
161 	}
162 
163 	if ((status = __rec_iput(t, nrec - 1, &fdata, flags)) != RET_SUCCESS)
164 		return (status);
165 
166 	switch (flags) {
167 	case R_IAFTER:
168 		nrec++;
169 		break;
170 	case R_SETCURSOR:
171 		t->bt_cursor.rcursor = nrec;
172 		break;
173 	}
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(t, nrec, data, flags)
192 	BTREE *t;
193 	recno_t nrec;
194 	const DBT *data;
195 	u_int flags;
196 {
197 	DBT tdata;
198 	EPG *e;
199 	PAGE *h;
200 	indx_t index, nxtindex;
201 	pgno_t pg;
202 	u_int32_t nbytes;
203 	int dflags, status;
204 	char *dest, db[NOVFLSIZE];
205 
206 	/*
207 	 * If the data won't fit on a page, store it on indirect pages.
208 	 *
209 	 * XXX
210 	 * If the insert fails later on, these pages aren't recovered.
211 	 */
212 	if (data->size > t->bt_ovflsize) {
213 		if (__ovfl_put(t, data, &pg) == RET_ERROR)
214 			return (RET_ERROR);
215 		tdata.data = db;
216 		tdata.size = NOVFLSIZE;
217 		*(pgno_t *)db = pg;
218 		*(u_int32_t *)(db + sizeof(pgno_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 	index = 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 		++index;
242 		break;
243 	case R_IBEFORE:
244 		break;
245 	default:
246 		if (nrec < t->bt_nrecs &&
247 		    __rec_dleaf(t, h, index) == 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 (h->upper - h->lower < nbytes + sizeof(indx_t)) {
261 		status = __bt_split(t, h, NULL, data, dflags, nbytes, index);
262 		if (status == RET_SUCCESS)
263 			++t->bt_nrecs;
264 		return (status);
265 	}
266 
267 	if (index < (nxtindex = NEXTINDEX(h)))
268 		memmove(h->linp + index + 1, h->linp + index,
269 		    (nxtindex - index) * sizeof(indx_t));
270 	h->lower += sizeof(indx_t);
271 
272 	h->linp[index] = h->upper -= nbytes;
273 	dest = (char *)h + h->upper;
274 	WR_RLEAF(dest, data, dflags);
275 
276 	++t->bt_nrecs;
277 	F_SET(t, B_MODIFIED);
278 	mpool_put(t->bt_mp, h, MPOOL_DIRTY);
279 
280 	return (RET_SUCCESS);
281 }
282