]>
Commit | Line | Data |
---|---|---|
e9ce8d39 A |
1 | /* |
2 | * Copyright (c) 1999 Apple Computer, Inc. All rights reserved. | |
3 | * | |
4 | * @APPLE_LICENSE_HEADER_START@ | |
5 | * | |
734aad71 | 6 | * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved. |
e9ce8d39 | 7 | * |
734aad71 A |
8 | * This file contains Original Code and/or Modifications of Original Code |
9 | * as defined in and that are subject to the Apple Public Source License | |
10 | * Version 2.0 (the 'License'). You may not use this file except in | |
11 | * compliance with the License. Please obtain a copy of the License at | |
12 | * http://www.opensource.apple.com/apsl/ and read it before using this | |
13 | * file. | |
14 | * | |
15 | * The Original Code and all software distributed under the License are | |
16 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
e9ce8d39 A |
17 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
18 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
734aad71 A |
19 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. |
20 | * Please see the License for the specific language governing rights and | |
21 | * limitations under the License. | |
e9ce8d39 A |
22 | * |
23 | * @APPLE_LICENSE_HEADER_END@ | |
24 | */ | |
25 | /* | |
26 | * Copyright (c) 1990, 1993 | |
27 | * The Regents of the University of California. All rights reserved. | |
28 | * | |
29 | * Redistribution and use in source and binary forms, with or without | |
30 | * modification, are permitted provided that the following conditions | |
31 | * are met: | |
32 | * 1. Redistributions of source code must retain the above copyright | |
33 | * notice, this list of conditions and the following disclaimer. | |
34 | * 2. Redistributions in binary form must reproduce the above copyright | |
35 | * notice, this list of conditions and the following disclaimer in the | |
36 | * documentation and/or other materials provided with the distribution. | |
37 | * 3. All advertising materials mentioning features or use of this software | |
38 | * must display the following acknowledgement: | |
39 | * This product includes software developed by the University of | |
40 | * California, Berkeley and its contributors. | |
41 | * 4. Neither the name of the University nor the names of its contributors | |
42 | * may be used to endorse or promote products derived from this software | |
43 | * without specific prior written permission. | |
44 | * | |
45 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
46 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
47 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
48 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
49 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
50 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
51 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
52 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
53 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
54 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
55 | * SUCH DAMAGE. | |
56 | */ | |
57 | ||
58 | ||
59 | #include <sys/types.h> | |
60 | ||
61 | #include <errno.h> | |
62 | #include <stdio.h> | |
63 | #include <stdlib.h> | |
64 | #include <string.h> | |
65 | ||
66 | #include <db.h> | |
67 | #include "recno.h" | |
68 | ||
69 | /* | |
70 | * __REC_PUT -- Add a recno item to the tree. | |
71 | * | |
72 | * Parameters: | |
73 | * dbp: pointer to access method | |
74 | * key: key | |
75 | * data: data | |
76 | * flag: R_CURSOR, R_IAFTER, R_IBEFORE, R_NOOVERWRITE | |
77 | * | |
78 | * Returns: | |
79 | * RET_ERROR, RET_SUCCESS and RET_SPECIAL if the key is | |
80 | * already in the tree and R_NOOVERWRITE specified. | |
81 | */ | |
82 | int | |
83 | __rec_put(dbp, key, data, flags) | |
84 | const DB *dbp; | |
85 | DBT *key; | |
86 | const DBT *data; | |
87 | u_int flags; | |
88 | { | |
89 | BTREE *t; | |
90 | DBT tdata; | |
91 | recno_t nrec; | |
92 | int status; | |
93 | ||
94 | t = dbp->internal; | |
95 | ||
96 | /* Toss any page pinned across calls. */ | |
97 | if (t->bt_pinned != NULL) { | |
98 | mpool_put(t->bt_mp, t->bt_pinned, 0); | |
99 | t->bt_pinned = NULL; | |
100 | } | |
101 | ||
102 | switch (flags) { | |
103 | case R_CURSOR: | |
104 | if (!ISSET(t, B_SEQINIT)) | |
105 | goto einval; | |
106 | nrec = t->bt_rcursor; | |
107 | break; | |
108 | case R_SETCURSOR: | |
109 | if ((nrec = *(recno_t *)key->data) == 0) | |
110 | goto einval; | |
111 | break; | |
112 | case R_IAFTER: | |
113 | if ((nrec = *(recno_t *)key->data) == 0) { | |
114 | nrec = 1; | |
115 | flags = R_IBEFORE; | |
116 | } | |
117 | break; | |
118 | case 0: | |
119 | case R_IBEFORE: | |
120 | if ((nrec = *(recno_t *)key->data) == 0) | |
121 | goto einval; | |
122 | break; | |
123 | case R_NOOVERWRITE: | |
124 | if ((nrec = *(recno_t *)key->data) == 0) | |
125 | goto einval; | |
126 | if (nrec <= t->bt_nrecs) | |
127 | return (RET_SPECIAL); | |
128 | break; | |
129 | default: | |
130 | einval: errno = EINVAL; | |
131 | return (RET_ERROR); | |
132 | } | |
133 | ||
134 | /* | |
135 | * Make sure that records up to and including the put record are | |
136 | * already in the database. If skipping records, create empty ones. | |
137 | */ | |
138 | if (nrec > t->bt_nrecs) { | |
139 | if (!ISSET(t, R_EOF | R_INMEM) && | |
140 | t->bt_irec(t, nrec) == RET_ERROR) | |
141 | return (RET_ERROR); | |
142 | if (nrec > t->bt_nrecs + 1) { | |
143 | if (ISSET(t, R_FIXLEN)) { | |
144 | if ((tdata.data = | |
145 | (void *)malloc(t->bt_reclen)) == NULL) | |
146 | return (RET_ERROR); | |
147 | tdata.size = t->bt_reclen; | |
148 | memset(tdata.data, t->bt_bval, tdata.size); | |
149 | } else { | |
150 | tdata.data = NULL; | |
151 | tdata.size = 0; | |
152 | } | |
153 | while (nrec > t->bt_nrecs + 1) | |
154 | if (__rec_iput(t, | |
155 | t->bt_nrecs, &tdata, 0) != RET_SUCCESS) | |
156 | return (RET_ERROR); | |
157 | if (ISSET(t, R_FIXLEN)) | |
158 | free(tdata.data); | |
159 | } | |
160 | } | |
161 | ||
162 | if ((status = __rec_iput(t, nrec - 1, data, flags)) != RET_SUCCESS) | |
163 | return (status); | |
164 | ||
165 | if (flags == R_SETCURSOR) | |
166 | t->bt_rcursor = nrec; | |
167 | ||
168 | SET(t, R_MODIFIED); | |
169 | return (__rec_ret(t, NULL, nrec, key, NULL)); | |
170 | } | |
171 | ||
172 | /* | |
173 | * __REC_IPUT -- Add a recno item to the tree. | |
174 | * | |
175 | * Parameters: | |
176 | * t: tree | |
177 | * nrec: record number | |
178 | * data: data | |
179 | * | |
180 | * Returns: | |
181 | * RET_ERROR, RET_SUCCESS | |
182 | */ | |
183 | int | |
184 | __rec_iput(t, nrec, data, flags) | |
185 | BTREE *t; | |
186 | recno_t nrec; | |
187 | const DBT *data; | |
188 | u_int flags; | |
189 | { | |
190 | DBT tdata; | |
191 | EPG *e; | |
192 | PAGE *h; | |
193 | indx_t index, nxtindex; | |
194 | pgno_t pg; | |
195 | size_t nbytes; | |
196 | int dflags, status; | |
197 | char *dest, db[NOVFLSIZE]; | |
198 | ||
199 | /* | |
200 | * If the data won't fit on a page, store it on indirect pages. | |
201 | * | |
202 | * XXX | |
203 | * If the insert fails later on, these pages aren't recovered. | |
204 | */ | |
205 | if (data->size > t->bt_ovflsize) { | |
206 | if (__ovfl_put(t, data, &pg) == RET_ERROR) | |
207 | return (RET_ERROR); | |
208 | tdata.data = db; | |
209 | tdata.size = NOVFLSIZE; | |
210 | *(pgno_t *)db = pg; | |
211 | *(size_t *)(db + sizeof(pgno_t)) = data->size; | |
212 | dflags = P_BIGDATA; | |
213 | data = &tdata; | |
214 | } else | |
215 | dflags = 0; | |
216 | ||
217 | /* __rec_search pins the returned page. */ | |
218 | if ((e = __rec_search(t, nrec, | |
219 | nrec > t->bt_nrecs || flags == R_IAFTER || flags == R_IBEFORE ? | |
220 | SINSERT : SEARCH)) == NULL) | |
221 | return (RET_ERROR); | |
222 | ||
223 | h = e->page; | |
224 | index = e->index; | |
225 | ||
226 | /* | |
227 | * Add the specified key/data pair to the tree. The R_IAFTER and | |
228 | * R_IBEFORE flags insert the key after/before the specified key. | |
229 | * | |
230 | * Pages are split as required. | |
231 | */ | |
232 | switch (flags) { | |
233 | case R_IAFTER: | |
234 | ++index; | |
235 | break; | |
236 | case R_IBEFORE: | |
237 | break; | |
238 | default: | |
239 | if (nrec < t->bt_nrecs && | |
240 | __rec_dleaf(t, h, index) == RET_ERROR) { | |
241 | mpool_put(t->bt_mp, h, 0); | |
242 | return (RET_ERROR); | |
243 | } | |
244 | break; | |
245 | } | |
246 | ||
247 | /* | |
248 | * If not enough room, split the page. The split code will insert | |
249 | * the key and data and unpin the current page. If inserting into | |
250 | * the offset array, shift the pointers up. | |
251 | */ | |
252 | nbytes = NRLEAFDBT(data->size); | |
253 | if (h->upper - h->lower < nbytes + sizeof(indx_t)) { | |
254 | status = __bt_split(t, h, NULL, data, dflags, nbytes, index); | |
255 | if (status == RET_SUCCESS) | |
256 | ++t->bt_nrecs; | |
257 | return (status); | |
258 | } | |
259 | ||
260 | if (index < (nxtindex = NEXTINDEX(h))) | |
261 | memmove(h->linp + index + 1, h->linp + index, | |
262 | (nxtindex - index) * sizeof(indx_t)); | |
263 | h->lower += sizeof(indx_t); | |
264 | ||
265 | h->linp[index] = h->upper -= nbytes; | |
266 | dest = (char *)h + h->upper; | |
267 | WR_RLEAF(dest, data, dflags); | |
268 | ||
269 | ++t->bt_nrecs; | |
270 | SET(t, B_MODIFIED); | |
271 | mpool_put(t->bt_mp, h, MPOOL_DIRTY); | |
272 | ||
273 | return (RET_SUCCESS); | |
274 | } |