Expand PMF_FN_* macros.
[netbsd-mini2440.git] / lib / libc / db / recno / rec_put.c
blob0609d15913537a3e7fca033af3a8d5d13f8cfe72
1 /* $NetBSD: rec_put.c,v 1.16 2008/09/10 17:52:36 joerg Exp $ */
3 /*-
4 * Copyright (c) 1990, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
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.
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.
32 #if HAVE_NBTOOL_CONFIG_H
33 #include "nbtool_config.h"
34 #endif
36 #include <sys/cdefs.h>
37 __RCSID("$NetBSD: rec_put.c,v 1.16 2008/09/10 17:52:36 joerg Exp $");
39 #include "namespace.h"
40 #include <sys/types.h>
42 #include <assert.h>
43 #include <errno.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
48 #include <db.h>
49 #include "recno.h"
52 * __REC_PUT -- Add a recno item to the tree.
54 * Parameters:
55 * dbp: pointer to access method
56 * key: key
57 * data: data
58 * flag: R_CURSOR, R_IAFTER, R_IBEFORE, R_NOOVERWRITE
60 * Returns:
61 * RET_ERROR, RET_SUCCESS and RET_SPECIAL if the key is
62 * already in the tree and R_NOOVERWRITE specified.
64 int
65 __rec_put(const DB *dbp, DBT *key, const DBT *data, u_int flags)
67 BTREE *t;
68 DBT fdata, tdata;
69 recno_t nrec;
70 int status;
72 t = dbp->internal;
74 /* Toss any page pinned across calls. */
75 if (t->bt_pinned != NULL) {
76 mpool_put(t->bt_mp, t->bt_pinned, 0);
77 t->bt_pinned = NULL;
81 * If using fixed-length records, and the record is long, return
82 * EINVAL. If it's short, pad it out. Use the record data return
83 * memory, it's only short-term.
85 if (F_ISSET(t, R_FIXLEN) && data->size != t->bt_reclen) {
86 if (data->size > t->bt_reclen)
87 goto einval;
89 if (t->bt_rdata.size < t->bt_reclen) {
90 t->bt_rdata.data = t->bt_rdata.data == NULL ?
91 malloc(t->bt_reclen) :
92 realloc(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;
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;
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;
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);
140 * Make sure that records up to and including the put record are
141 * already in the database. If skipping records, create empty ones.
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;
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);
167 if ((status = __rec_iput(t, nrec - 1, &fdata, flags)) != RET_SUCCESS)
168 return (status);
170 if (flags == R_SETCURSOR)
171 t->bt_cursor.rcursor = nrec;
173 F_SET(t, R_MODIFIED);
174 return (__rec_ret(t, NULL, nrec, key, NULL));
178 * __REC_IPUT -- Add a recno item to the tree.
180 * Parameters:
181 * t: tree
182 * nrec: record number
183 * data: data
185 * Returns:
186 * RET_ERROR, RET_SUCCESS
189 __rec_iput(BTREE *t, recno_t nrec, const DBT *data, u_int flags)
191 DBT tdata;
192 EPG *e;
193 PAGE *h;
194 indx_t idx, nxtindex;
195 pgno_t pg;
196 uint32_t nbytes;
197 int dflags, status;
198 char *dest, db[NOVFLSIZE];
201 * If the data won't fit on a page, store it on indirect pages.
203 * XXX
204 * If the insert fails later on, these pages aren't recovered.
206 if (data->size > t->bt_ovflsize) {
207 if (__ovfl_put(t, data, &pg) == RET_ERROR)
208 return (RET_ERROR);
209 tdata.data = db;
210 tdata.size = NOVFLSIZE;
211 *(pgno_t *)(void *)db = pg;
212 _DBFIT(data->size, uint32_t);
213 *(uint32_t *)(void *)(db + sizeof(pgno_t)) =
214 (uint32_t)data->size;
215 dflags = P_BIGDATA;
216 data = &tdata;
217 } else
218 dflags = 0;
220 /* __rec_search pins the returned page. */
221 if ((e = __rec_search(t, nrec,
222 nrec > t->bt_nrecs || flags == R_IAFTER || flags == R_IBEFORE ?
223 SINSERT : SEARCH)) == NULL)
224 return (RET_ERROR);
226 h = e->page;
227 idx = e->index;
230 * Add the specified key/data pair to the tree. The R_IAFTER and
231 * R_IBEFORE flags insert the key after/before the specified key.
233 * Pages are split as required.
235 switch (flags) {
236 case R_IAFTER:
237 ++idx;
238 break;
239 case R_IBEFORE:
240 break;
241 default:
242 if (nrec < t->bt_nrecs &&
243 __rec_dleaf(t, h, (uint32_t)idx) == RET_ERROR) {
244 mpool_put(t->bt_mp, h, 0);
245 return (RET_ERROR);
247 break;
251 * If not enough room, split the page. The split code will insert
252 * the key and data and unpin the current page. If inserting into
253 * the offset array, shift the pointers up.
255 nbytes = NRLEAFDBT(data->size);
256 if ((uint32_t) (h->upper - h->lower) < nbytes + sizeof(indx_t)) {
257 status = __bt_split(t, h, NULL, data, dflags, nbytes,
258 (uint32_t)idx);
259 if (status == RET_SUCCESS)
260 ++t->bt_nrecs;
261 return (status);
264 if (idx < (nxtindex = NEXTINDEX(h)))
265 memmove(h->linp + idx + 1, h->linp + idx,
266 (nxtindex - idx) * sizeof(indx_t));
267 h->lower += sizeof(indx_t);
269 h->linp[idx] = h->upper -= nbytes;
270 dest = (char *)(void *)h + h->upper;
271 WR_RLEAF(dest, data, dflags);
273 ++t->bt_nrecs;
274 F_SET(t, B_MODIFIED);
275 mpool_put(t->bt_mp, h, MPOOL_DIRTY);
277 return (RET_SUCCESS);