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