]> git.saurik.com Git - apple/libc.git/blame - db/btree/FreeBSD/bt_put.c
Libc-825.24.tar.gz
[apple/libc.git] / db / btree / FreeBSD / bt_put.c
CommitLineData
9385eb3d
A
1/*-
2 * Copyright (c) 1990, 1993, 1994
e9ce8d39
A
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Mike Olson.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
e9ce8d39
A
16 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
9385eb3d
A
33#if defined(LIBC_SCCS) && !defined(lint)
34static char sccsid[] = "@(#)bt_put.c 8.8 (Berkeley) 7/26/94";
35#endif /* LIBC_SCCS and not lint */
36#include <sys/cdefs.h>
1f2f436a 37__FBSDID("$FreeBSD: src/lib/libc/db/btree/bt_put.c,v 1.9 2009/03/28 05:45:29 delphij Exp $");
e9ce8d39
A
38
39#include <sys/types.h>
40
41#include <errno.h>
42#include <stdio.h>
43#include <stdlib.h>
44#include <string.h>
45
46#include <db.h>
47#include "btree.h"
48
9385eb3d 49static EPG *bt_fast(BTREE *, const DBT *, const DBT *, int *);
e9ce8d39
A
50
51/*
52 * __BT_PUT -- Add a btree item to the tree.
53 *
54 * Parameters:
55 * dbp: pointer to access method
56 * key: key
57 * data: data
58 * flag: R_NOOVERWRITE
59 *
60 * Returns:
61 * RET_ERROR, RET_SUCCESS and RET_SPECIAL if the key is already in the
62 * tree and R_NOOVERWRITE specified.
63 */
64int
1f2f436a 65__bt_put(const DB *dbp, DBT *key, const DBT *data, u_int flags)
e9ce8d39
A
66{
67 BTREE *t;
68 DBT tkey, tdata;
69 EPG *e;
70 PAGE *h;
1f2f436a 71 indx_t idx, nxtindex;
e9ce8d39 72 pgno_t pg;
59e0d9fe 73 u_int32_t nbytes, tmp;
e9ce8d39
A
74 int dflags, exact, status;
75 char *dest, db[NOVFLSIZE], kb[NOVFLSIZE];
76
77 t = dbp->internal;
78
79 /* Toss any page pinned across calls. */
80 if (t->bt_pinned != NULL) {
81 mpool_put(t->bt_mp, t->bt_pinned, 0);
82 t->bt_pinned = NULL;
83 }
84
9385eb3d
A
85 /* Check for change to a read-only tree. */
86 if (F_ISSET(t, B_RDONLY)) {
87 errno = EPERM;
88 return (RET_ERROR);
89 }
90
e9ce8d39 91 switch (flags) {
e9ce8d39
A
92 case 0:
93 case R_NOOVERWRITE:
94 break;
9385eb3d
A
95 case R_CURSOR:
96 /*
97 * If flags is R_CURSOR, put the cursor. Must already
98 * have started a scan and not have already deleted it.
99 */
100 if (F_ISSET(&t->bt_cursor, CURS_INIT) &&
101 !F_ISSET(&t->bt_cursor,
1f2f436a 102 CURS_ACQUIRE | CURS_AFTER | CURS_BEFORE))
9385eb3d
A
103 break;
104 /* FALLTHROUGH */
e9ce8d39 105 default:
9385eb3d 106 errno = EINVAL;
e9ce8d39
A
107 return (RET_ERROR);
108 }
109
110 /*
9385eb3d
A
111 * If the key/data pair won't fit on a page, store it on overflow
112 * pages. Only put the key on the overflow page if the pair are
113 * still too big after moving the data to an overflow page.
e9ce8d39
A
114 *
115 * XXX
9385eb3d 116 * If the insert fails later on, the overflow pages aren't recovered.
e9ce8d39
A
117 */
118 dflags = 0;
119 if (key->size + data->size > t->bt_ovflsize) {
120 if (key->size > t->bt_ovflsize) {
121storekey: if (__ovfl_put(t, key, &pg) == RET_ERROR)
122 return (RET_ERROR);
123 tkey.data = kb;
124 tkey.size = NOVFLSIZE;
125 memmove(kb, &pg, sizeof(pgno_t));
59e0d9fe 126 tmp = key->size;
e9ce8d39 127 memmove(kb + sizeof(pgno_t),
59e0d9fe 128 &tmp, sizeof(u_int32_t));
e9ce8d39
A
129 dflags |= P_BIGKEY;
130 key = &tkey;
131 }
132 if (key->size + data->size > t->bt_ovflsize) {
133 if (__ovfl_put(t, data, &pg) == RET_ERROR)
134 return (RET_ERROR);
135 tdata.data = db;
136 tdata.size = NOVFLSIZE;
137 memmove(db, &pg, sizeof(pgno_t));
59e0d9fe 138 tmp = data->size;
e9ce8d39 139 memmove(db + sizeof(pgno_t),
59e0d9fe 140 &tmp, sizeof(u_int32_t));
e9ce8d39
A
141 dflags |= P_BIGDATA;
142 data = &tdata;
143 }
144 if (key->size + data->size > t->bt_ovflsize)
145 goto storekey;
146 }
147
148 /* Replace the cursor. */
149 if (flags == R_CURSOR) {
9385eb3d 150 if ((h = mpool_get(t->bt_mp, t->bt_cursor.pg.pgno, 0)) == NULL)
e9ce8d39 151 return (RET_ERROR);
1f2f436a 152 idx = t->bt_cursor.pg.index;
e9ce8d39
A
153 goto delete;
154 }
155
156 /*
9385eb3d
A
157 * Find the key to delete, or, the location at which to insert.
158 * Bt_fast and __bt_search both pin the returned page.
e9ce8d39
A
159 */
160 if (t->bt_order == NOT || (e = bt_fast(t, key, data, &exact)) == NULL)
161 if ((e = __bt_search(t, key, &exact)) == NULL)
162 return (RET_ERROR);
163 h = e->page;
1f2f436a 164 idx = e->index;
e9ce8d39
A
165
166 /*
9385eb3d
A
167 * Add the key/data pair to the tree. If an identical key is already
168 * in the tree, and R_NOOVERWRITE is set, an error is returned. If
169 * R_NOOVERWRITE is not set, the key is either added (if duplicates are
170 * permitted) or an error is returned.
e9ce8d39
A
171 */
172 switch (flags) {
173 case R_NOOVERWRITE:
174 if (!exact)
175 break;
e9ce8d39
A
176 mpool_put(t->bt_mp, h, 0);
177 return (RET_SPECIAL);
178 default:
9385eb3d 179 if (!exact || !F_ISSET(t, B_NODUPS))
e9ce8d39 180 break;
9385eb3d
A
181 /*
182 * !!!
183 * Note, the delete may empty the page, so we need to put a
184 * new entry into the page immediately.
185 */
1f2f436a 186delete: if (__bt_dleaf(t, key, h, idx) == RET_ERROR) {
e9ce8d39
A
187 mpool_put(t->bt_mp, h, 0);
188 return (RET_ERROR);
189 }
190 break;
191 }
192
193 /*
194 * If not enough room, or the user has put a ceiling on the number of
195 * keys permitted in the page, split the page. The split code will
196 * insert the key and data and unpin the current page. If inserting
197 * into the offset array, shift the pointers up.
198 */
199 nbytes = NBLEAFDBT(key->size, data->size);
1f2f436a 200 if ((u_int32_t)(h->upper - h->lower) < nbytes + sizeof(indx_t)) {
e9ce8d39 201 if ((status = __bt_split(t, h, key,
1f2f436a 202 data, dflags, nbytes, idx)) != RET_SUCCESS)
e9ce8d39
A
203 return (status);
204 goto success;
205 }
206
1f2f436a
A
207 if (idx < (nxtindex = NEXTINDEX(h)))
208 memmove(h->linp + idx + 1, h->linp + idx,
209 (nxtindex - idx) * sizeof(indx_t));
e9ce8d39
A
210 h->lower += sizeof(indx_t);
211
1f2f436a 212 h->linp[idx] = h->upper -= nbytes;
e9ce8d39
A
213 dest = (char *)h + h->upper;
214 WR_BLEAF(dest, key, data, dflags);
215
9385eb3d
A
216 /* If the cursor is on this page, adjust it as necessary. */
217 if (F_ISSET(&t->bt_cursor, CURS_INIT) &&
218 !F_ISSET(&t->bt_cursor, CURS_ACQUIRE) &&
1f2f436a 219 t->bt_cursor.pg.pgno == h->pgno && t->bt_cursor.pg.index >= idx)
9385eb3d
A
220 ++t->bt_cursor.pg.index;
221
222 if (t->bt_order == NOT) {
e9ce8d39 223 if (h->nextpg == P_INVALID) {
1f2f436a 224 if (idx == NEXTINDEX(h) - 1) {
e9ce8d39 225 t->bt_order = FORWARD;
1f2f436a 226 t->bt_last.index = idx;
e9ce8d39
A
227 t->bt_last.pgno = h->pgno;
228 }
229 } else if (h->prevpg == P_INVALID) {
1f2f436a 230 if (idx == 0) {
e9ce8d39
A
231 t->bt_order = BACK;
232 t->bt_last.index = 0;
233 t->bt_last.pgno = h->pgno;
234 }
235 }
9385eb3d 236 }
e9ce8d39
A
237
238 mpool_put(t->bt_mp, h, MPOOL_DIRTY);
239
240success:
9385eb3d
A
241 if (flags == R_SETCURSOR)
242 __bt_setcur(t, e->page->pgno, e->index);
243
244 F_SET(t, B_MODIFIED);
e9ce8d39
A
245 return (RET_SUCCESS);
246}
247
248#ifdef STATISTICS
249u_long bt_cache_hit, bt_cache_miss;
250#endif
251
252/*
253 * BT_FAST -- Do a quick check for sorted data.
254 *
255 * Parameters:
256 * t: tree
257 * key: key to insert
258 *
259 * Returns:
1f2f436a 260 * EPG for new record or NULL if not found.
e9ce8d39
A
261 */
262static EPG *
1f2f436a 263bt_fast(BTREE *t, const DBT *key, const DBT *data, int *exactp)
e9ce8d39
A
264{
265 PAGE *h;
9385eb3d 266 u_int32_t nbytes;
e9ce8d39
A
267 int cmp;
268
269 if ((h = mpool_get(t->bt_mp, t->bt_last.pgno, 0)) == NULL) {
270 t->bt_order = NOT;
271 return (NULL);
272 }
273 t->bt_cur.page = h;
274 t->bt_cur.index = t->bt_last.index;
275
276 /*
9385eb3d
A
277 * If won't fit in this page or have too many keys in this page,
278 * have to search to get split stack.
e9ce8d39
A
279 */
280 nbytes = NBLEAFDBT(key->size, data->size);
1f2f436a 281 if ((u_int32_t)(h->upper - h->lower) < nbytes + sizeof(indx_t))
e9ce8d39
A
282 goto miss;
283
284 if (t->bt_order == FORWARD) {
285 if (t->bt_cur.page->nextpg != P_INVALID)
286 goto miss;
287 if (t->bt_cur.index != NEXTINDEX(h) - 1)
288 goto miss;
289 if ((cmp = __bt_cmp(t, key, &t->bt_cur)) < 0)
290 goto miss;
291 t->bt_last.index = cmp ? ++t->bt_cur.index : t->bt_cur.index;
292 } else {
293 if (t->bt_cur.page->prevpg != P_INVALID)
294 goto miss;
295 if (t->bt_cur.index != 0)
296 goto miss;
297 if ((cmp = __bt_cmp(t, key, &t->bt_cur)) > 0)
298 goto miss;
299 t->bt_last.index = 0;
300 }
301 *exactp = cmp == 0;
302#ifdef STATISTICS
303 ++bt_cache_hit;
304#endif
305 return (&t->bt_cur);
306
307miss:
308#ifdef STATISTICS
309 ++bt_cache_miss;
310#endif
311 t->bt_order = NOT;
312 mpool_put(t->bt_mp, h, 0);
313 return (NULL);
314}