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