| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
#if defined(LIBC_SCCS) && !defined(lint) |
| 38 |
static char sccsid[] = "@(#)bt_put.c 8.8 (Berkeley) 7/26/94"; |
| 39 |
#endif |
| 40 |
|
| 41 |
#include <sys/types.h> |
| 42 |
|
| 43 |
#include <errno.h> |
| 44 |
#include <stdio.h> |
| 45 |
#include <stdlib.h> |
| 46 |
#include <string.h> |
| 47 |
|
| 48 |
#include "../include/db.h" |
| 49 |
#include "btree.h" |
| 50 |
|
| 51 |
static EPG *bt_fast __P((BTREE *, const DBT *, const DBT *, int *)); |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
int |
| 67 |
__bt_put(dbp, key, data, flags) |
| 68 |
const DB *dbp; |
| 69 |
DBT *key; |
| 70 |
const DBT *data; |
| 71 |
u_int flags; |
| 72 |
{ |
| 73 |
BTREE *t; |
| 74 |
DBT tkey, tdata; |
| 75 |
EPG *e = 0; |
| 76 |
PAGE *h; |
| 77 |
indx_t idx, nxtindex; |
| 78 |
pgno_t pg; |
| 79 |
u_int32_t nbytes; |
| 80 |
int dflags, exact, status; |
| 81 |
char *dest, db[NOVFLSIZE], kb[NOVFLSIZE]; |
| 82 |
|
| 83 |
t = dbp->internal; |
| 84 |
|
| 85 |
|
| 86 |
if (t->bt_pinned != NULL) { |
| 87 |
mpool_put(t->bt_mp, t->bt_pinned, 0); |
| 88 |
t->bt_pinned = NULL; |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
if (F_ISSET(t, B_RDONLY)) { |
| 93 |
errno = EPERM; |
| 94 |
return (RET_ERROR); |
| 95 |
} |
| 96 |
|
| 97 |
switch (flags) { |
| 98 |
case 0: |
| 99 |
case R_NOOVERWRITE: |
| 100 |
break; |
| 101 |
case R_CURSOR: |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
if (F_ISSET(&t->bt_cursor, CURS_INIT) && |
| 107 |
!F_ISSET(&t->bt_cursor, |
| 108 |
CURS_ACQUIRE | CURS_AFTER | CURS_BEFORE)) |
| 109 |
break; |
| 110 |
|
| 111 |
default: |
| 112 |
errno = EINVAL; |
| 113 |
return (RET_ERROR); |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
dflags = 0; |
| 125 |
if (key->size + data->size > t->bt_ovflsize) { |
| 126 |
if (key->size > t->bt_ovflsize) { |
| 127 |
storekey: if (__ovfl_put(t, key, &pg) == RET_ERROR) |
| 128 |
return (RET_ERROR); |
| 129 |
tkey.data = kb; |
| 130 |
tkey.size = NOVFLSIZE; |
| 131 |
memmove(kb, &pg, sizeof(pgno_t)); |
| 132 |
memmove(kb + sizeof(pgno_t), |
| 133 |
&key->size, sizeof(u_int32_t)); |
| 134 |
dflags |= P_BIGKEY; |
| 135 |
key = &tkey; |
| 136 |
} |
| 137 |
if (key->size + data->size > t->bt_ovflsize) { |
| 138 |
if (__ovfl_put(t, data, &pg) == RET_ERROR) |
| 139 |
return (RET_ERROR); |
| 140 |
tdata.data = db; |
| 141 |
tdata.size = NOVFLSIZE; |
| 142 |
memmove(db, &pg, sizeof(pgno_t)); |
| 143 |
memmove(db + sizeof(pgno_t), |
| 144 |
&data->size, sizeof(u_int32_t)); |
| 145 |
dflags |= P_BIGDATA; |
| 146 |
data = &tdata; |
| 147 |
} |
| 148 |
if (key->size + data->size > t->bt_ovflsize) |
| 149 |
goto storekey; |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
if (flags == R_CURSOR) { |
| 154 |
if ((h = mpool_get(t->bt_mp, t->bt_cursor.pg.pgno, 0)) == NULL) |
| 155 |
return (RET_ERROR); |
| 156 |
idx = t->bt_cursor.pg.index; |
| 157 |
goto delete; |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
if (t->bt_order == NOT || (e = bt_fast(t, key, data, &exact)) == NULL) |
| 165 |
if ((e = __bt_search(t, key, &exact)) == NULL) |
| 166 |
return (RET_ERROR); |
| 167 |
h = e->page; |
| 168 |
idx = e->index; |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
switch (flags) { |
| 177 |
case R_NOOVERWRITE: |
| 178 |
if (!exact) |
| 179 |
break; |
| 180 |
mpool_put(t->bt_mp, h, 0); |
| 181 |
return (RET_SPECIAL); |
| 182 |
default: |
| 183 |
if (!exact || !F_ISSET(t, B_NODUPS)) |
| 184 |
break; |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
delete: if (__bt_dleaf(t, key, h, idx) == RET_ERROR) { |
| 191 |
mpool_put(t->bt_mp, h, 0); |
| 192 |
return (RET_ERROR); |
| 193 |
} |
| 194 |
break; |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
nbytes = NBLEAFDBT(key->size, data->size); |
| 204 |
if ((u_int32_t) (h->upper - h->lower) < nbytes + sizeof(indx_t)) { |
| 205 |
if ((status = __bt_split(t, h, key, |
| 206 |
data, dflags, nbytes, idx)) != RET_SUCCESS) |
| 207 |
return (status); |
| 208 |
goto success; |
| 209 |
} |
| 210 |
|
| 211 |
if (idx < (nxtindex = NEXTINDEX(h))) |
| 212 |
memmove(h->linp + idx + 1, h->linp + idx, |
| 213 |
(nxtindex - idx) * sizeof(indx_t)); |
| 214 |
h->lower += sizeof(indx_t); |
| 215 |
|
| 216 |
h->linp[idx] = h->upper -= nbytes; |
| 217 |
dest = (char *)h + h->upper; |
| 218 |
WR_BLEAF(dest, key, data, dflags); |
| 219 |
|
| 220 |
|
| 221 |
if (F_ISSET(&t->bt_cursor, CURS_INIT) && |
| 222 |
!F_ISSET(&t->bt_cursor, CURS_ACQUIRE) && |
| 223 |
t->bt_cursor.pg.pgno == h->pgno && t->bt_cursor.pg.index >= idx) |
| 224 |
++t->bt_cursor.pg.index; |
| 225 |
|
| 226 |
if (t->bt_order == NOT) { |
| 227 |
if (h->nextpg == P_INVALID) { |
| 228 |
if (idx == NEXTINDEX(h) - 1) { |
| 229 |
t->bt_order = FORWARD; |
| 230 |
t->bt_last.index = idx; |
| 231 |
t->bt_last.pgno = h->pgno; |
| 232 |
} |
| 233 |
} else if (h->prevpg == P_INVALID) { |
| 234 |
if (idx == 0) { |
| 235 |
t->bt_order = BACK; |
| 236 |
t->bt_last.index = 0; |
| 237 |
t->bt_last.pgno = h->pgno; |
| 238 |
} |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
mpool_put(t->bt_mp, h, MPOOL_DIRTY); |
| 243 |
|
| 244 |
success: |
| 245 |
if (flags == R_SETCURSOR) |
| 246 |
__bt_setcur(t, e->page->pgno, e->index); |
| 247 |
|
| 248 |
F_SET(t, B_MODIFIED); |
| 249 |
return (RET_SUCCESS); |
| 250 |
} |
| 251 |
|
| 252 |
#ifdef STATISTICS |
| 253 |
u_long bt_cache_hit, bt_cache_miss; |
| 254 |
#endif |
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
static EPG * |
| 267 |
bt_fast(t, key, data, exactp) |
| 268 |
BTREE *t; |
| 269 |
const DBT *key, *data; |
| 270 |
int *exactp; |
| 271 |
{ |
| 272 |
PAGE *h; |
| 273 |
u_int32_t nbytes; |
| 274 |
int cmp; |
| 275 |
|
| 276 |
if ((h = mpool_get(t->bt_mp, t->bt_last.pgno, 0)) == NULL) { |
| 277 |
t->bt_order = NOT; |
| 278 |
return (NULL); |
| 279 |
} |
| 280 |
t->bt_cur.page = h; |
| 281 |
t->bt_cur.index = t->bt_last.index; |
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
nbytes = NBLEAFDBT(key->size, data->size); |
| 288 |
if ((u_int32_t) (h->upper - h->lower) < nbytes + sizeof(indx_t)) |
| 289 |
goto miss; |
| 290 |
|
| 291 |
if (t->bt_order == FORWARD) { |
| 292 |
if (t->bt_cur.page->nextpg != P_INVALID) |
| 293 |
goto miss; |
| 294 |
if (t->bt_cur.index != NEXTINDEX(h) - 1) |
| 295 |
goto miss; |
| 296 |
if ((cmp = __bt_cmp(t, key, &t->bt_cur)) < 0) |
| 297 |
goto miss; |
| 298 |
t->bt_last.index = cmp ? ++t->bt_cur.index : t->bt_cur.index; |
| 299 |
} else { |
| 300 |
if (t->bt_cur.page->prevpg != P_INVALID) |
| 301 |
goto miss; |
| 302 |
if (t->bt_cur.index != 0) |
| 303 |
goto miss; |
| 304 |
if ((cmp = __bt_cmp(t, key, &t->bt_cur)) > 0) |
| 305 |
goto miss; |
| 306 |
t->bt_last.index = 0; |
| 307 |
} |
| 308 |
*exactp = cmp == 0; |
| 309 |
#ifdef STATISTICS |
| 310 |
++bt_cache_hit; |
| 311 |
#endif |
| 312 |
return (&t->bt_cur); |
| 313 |
|
| 314 |
miss: |
| 315 |
#ifdef STATISTICS |
| 316 |
++bt_cache_miss; |
| 317 |
#endif |
| 318 |
t->bt_order = NOT; |
| 319 |
mpool_put(t->bt_mp, h, 0); |
| 320 |
return (NULL); |
| 321 |
} |