| 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_seq.c 8.7 (Berkeley) 7/20/94"; |
| 39 |
#endif |
| 40 |
|
| 41 |
#include <sys/types.h> |
| 42 |
|
| 43 |
#include <errno.h> |
| 44 |
#include <stddef.h> |
| 45 |
#include <stdio.h> |
| 46 |
#include <stdlib.h> |
| 47 |
|
| 48 |
#include "../include/db.h" |
| 49 |
#include "btree.h" |
| 50 |
|
| 51 |
static int __bt_first __P((BTREE *, const DBT *, EPG *, int *)); |
| 52 |
static int __bt_seqadv __P((BTREE *, EPG *, int)); |
| 53 |
static int __bt_seqset __P((BTREE *, EPG *, DBT *, int)); |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
int |
| 77 |
__bt_seq(dbp, key, data, flags) |
| 78 |
const DB *dbp; |
| 79 |
DBT *key, *data; |
| 80 |
u_int flags; |
| 81 |
{ |
| 82 |
BTREE *t; |
| 83 |
EPG e; |
| 84 |
int status; |
| 85 |
|
| 86 |
t = dbp->internal; |
| 87 |
|
| 88 |
|
| 89 |
if (t->bt_pinned != NULL) { |
| 90 |
mpool_put(t->bt_mp, t->bt_pinned, 0); |
| 91 |
t->bt_pinned = NULL; |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
switch (flags) { |
| 100 |
case R_NEXT: |
| 101 |
case R_PREV: |
| 102 |
if (F_ISSET(&t->bt_cursor, CURS_INIT)) { |
| 103 |
status = __bt_seqadv(t, &e, flags); |
| 104 |
break; |
| 105 |
} |
| 106 |
|
| 107 |
case R_FIRST: |
| 108 |
case R_LAST: |
| 109 |
case R_CURSOR: |
| 110 |
status = __bt_seqset(t, &e, key, flags); |
| 111 |
break; |
| 112 |
default: |
| 113 |
errno = EINVAL; |
| 114 |
return (RET_ERROR); |
| 115 |
} |
| 116 |
|
| 117 |
if (status == RET_SUCCESS) { |
| 118 |
__bt_setcur(t, e.page->pgno, e.index); |
| 119 |
|
| 120 |
status = |
| 121 |
__bt_ret(t, &e, key, &t->bt_rkey, data, &t->bt_rdata, 0); |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
if (F_ISSET(t, B_DB_LOCK)) |
| 128 |
mpool_put(t->bt_mp, e.page, 0); |
| 129 |
else |
| 130 |
t->bt_pinned = e.page; |
| 131 |
} |
| 132 |
return (status); |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
static int |
| 152 |
__bt_seqset(t, ep, key, flags) |
| 153 |
BTREE *t; |
| 154 |
EPG *ep; |
| 155 |
DBT *key; |
| 156 |
int flags; |
| 157 |
{ |
| 158 |
PAGE *h; |
| 159 |
pgno_t pg; |
| 160 |
int exact; |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
switch (flags) { |
| 168 |
case R_CURSOR: |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
if (key->data == NULL || key->size == 0) { |
| 174 |
errno = EINVAL; |
| 175 |
return (RET_ERROR); |
| 176 |
} |
| 177 |
return (__bt_first(t, key, ep, &exact)); |
| 178 |
case R_FIRST: |
| 179 |
case R_NEXT: |
| 180 |
|
| 181 |
for (pg = P_ROOT;;) { |
| 182 |
if ((h = mpool_get(t->bt_mp, pg, 0)) == NULL) |
| 183 |
return (RET_ERROR); |
| 184 |
|
| 185 |
|
| 186 |
if (NEXTINDEX(h) == 0) { |
| 187 |
mpool_put(t->bt_mp, h, 0); |
| 188 |
return (RET_SPECIAL); |
| 189 |
} |
| 190 |
|
| 191 |
if (h->flags & (P_BLEAF | P_RLEAF)) |
| 192 |
break; |
| 193 |
pg = GETBINTERNAL(h, 0)->pgno; |
| 194 |
mpool_put(t->bt_mp, h, 0); |
| 195 |
} |
| 196 |
ep->page = h; |
| 197 |
ep->index = 0; |
| 198 |
break; |
| 199 |
case R_LAST: |
| 200 |
case R_PREV: |
| 201 |
|
| 202 |
for (pg = P_ROOT;;) { |
| 203 |
if ((h = mpool_get(t->bt_mp, pg, 0)) == NULL) |
| 204 |
return (RET_ERROR); |
| 205 |
|
| 206 |
|
| 207 |
if (NEXTINDEX(h) == 0) { |
| 208 |
mpool_put(t->bt_mp, h, 0); |
| 209 |
return (RET_SPECIAL); |
| 210 |
} |
| 211 |
|
| 212 |
if (h->flags & (P_BLEAF | P_RLEAF)) |
| 213 |
break; |
| 214 |
pg = GETBINTERNAL(h, NEXTINDEX(h) - 1)->pgno; |
| 215 |
mpool_put(t->bt_mp, h, 0); |
| 216 |
} |
| 217 |
|
| 218 |
ep->page = h; |
| 219 |
ep->index = NEXTINDEX(h) - 1; |
| 220 |
break; |
| 221 |
} |
| 222 |
return (RET_SUCCESS); |
| 223 |
} |
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
static int |
| 240 |
__bt_seqadv(t, ep, flags) |
| 241 |
BTREE *t; |
| 242 |
EPG *ep; |
| 243 |
int flags; |
| 244 |
{ |
| 245 |
CURSOR *c; |
| 246 |
PAGE *h; |
| 247 |
indx_t index = 0; |
| 248 |
pgno_t pg; |
| 249 |
int exact; |
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
c = &t->bt_cursor; |
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
if (F_ISSET(c, CURS_ACQUIRE)) |
| 266 |
return (__bt_first(t, &c->key, ep, &exact)); |
| 267 |
|
| 268 |
|
| 269 |
if ((h = mpool_get(t->bt_mp, c->pg.pgno, 0)) == NULL) |
| 270 |
return (RET_ERROR); |
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
switch (flags) { |
| 277 |
case R_NEXT: |
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
if (F_ISSET(c, CURS_AFTER)) |
| 284 |
goto usecurrent; |
| 285 |
index = c->pg.index; |
| 286 |
if (++index == NEXTINDEX(h)) { |
| 287 |
pg = h->nextpg; |
| 288 |
mpool_put(t->bt_mp, h, 0); |
| 289 |
if (pg == P_INVALID) |
| 290 |
return (RET_SPECIAL); |
| 291 |
if ((h = mpool_get(t->bt_mp, pg, 0)) == NULL) |
| 292 |
return (RET_ERROR); |
| 293 |
index = 0; |
| 294 |
} |
| 295 |
break; |
| 296 |
case R_PREV: |
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
if (F_ISSET(c, CURS_BEFORE)) { |
| 303 |
usecurrent: F_CLR(c, CURS_AFTER | CURS_BEFORE); |
| 304 |
ep->page = h; |
| 305 |
ep->index = c->pg.index; |
| 306 |
return (RET_SUCCESS); |
| 307 |
} |
| 308 |
index = c->pg.index; |
| 309 |
if (index == 0) { |
| 310 |
pg = h->prevpg; |
| 311 |
mpool_put(t->bt_mp, h, 0); |
| 312 |
if (pg == P_INVALID) |
| 313 |
return (RET_SPECIAL); |
| 314 |
if ((h = mpool_get(t->bt_mp, pg, 0)) == NULL) |
| 315 |
return (RET_ERROR); |
| 316 |
index = NEXTINDEX(h) - 1; |
| 317 |
} else |
| 318 |
--index; |
| 319 |
break; |
| 320 |
} |
| 321 |
|
| 322 |
ep->page = h; |
| 323 |
ep->index = index; |
| 324 |
return (RET_SUCCESS); |
| 325 |
} |
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
static int |
| 342 |
__bt_first(t, key, erval, exactp) |
| 343 |
BTREE *t; |
| 344 |
const DBT *key; |
| 345 |
EPG *erval; |
| 346 |
int *exactp; |
| 347 |
{ |
| 348 |
PAGE *h; |
| 349 |
EPG *ep, save; |
| 350 |
pgno_t pg; |
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
if ((ep = __bt_search(t, key, exactp)) == NULL) |
| 361 |
return (RET_SPECIAL); |
| 362 |
if (*exactp) { |
| 363 |
if (F_ISSET(t, B_NODUPS)) { |
| 364 |
*erval = *ep; |
| 365 |
return (RET_SUCCESS); |
| 366 |
} |
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
save = *ep; |
| 374 |
h = ep->page; |
| 375 |
do { |
| 376 |
if (save.page->pgno != ep->page->pgno) { |
| 377 |
mpool_put(t->bt_mp, save.page, 0); |
| 378 |
save = *ep; |
| 379 |
} else |
| 380 |
save.index = ep->index; |
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
if (ep->index == 0) { |
| 388 |
if (h->prevpg == P_INVALID) |
| 389 |
break; |
| 390 |
if (h->pgno != save.page->pgno) |
| 391 |
mpool_put(t->bt_mp, h, 0); |
| 392 |
if ((h = mpool_get(t->bt_mp, |
| 393 |
h->prevpg, 0)) == NULL) { |
| 394 |
if (h->pgno == save.page->pgno) |
| 395 |
mpool_put(t->bt_mp, |
| 396 |
save.page, 0); |
| 397 |
return (RET_ERROR); |
| 398 |
} |
| 399 |
ep->page = h; |
| 400 |
ep->index = NEXTINDEX(h); |
| 401 |
} |
| 402 |
--ep->index; |
| 403 |
} while (__bt_cmp(t, key, ep) == 0); |
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
if (h->pgno != save.page->pgno) |
| 411 |
mpool_put(t->bt_mp, h, 0); |
| 412 |
|
| 413 |
*erval = save; |
| 414 |
return (RET_SUCCESS); |
| 415 |
} |
| 416 |
|
| 417 |
|
| 418 |
if (ep->index == NEXTINDEX(ep->page)) { |
| 419 |
h = ep->page; |
| 420 |
pg = h->nextpg; |
| 421 |
mpool_put(t->bt_mp, h, 0); |
| 422 |
if (pg == P_INVALID) |
| 423 |
return (RET_SPECIAL); |
| 424 |
if ((h = mpool_get(t->bt_mp, pg, 0)) == NULL) |
| 425 |
return (RET_ERROR); |
| 426 |
ep->index = 0; |
| 427 |
ep->page = h; |
| 428 |
} |
| 429 |
*erval = *ep; |
| 430 |
return (RET_SUCCESS); |
| 431 |
} |
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
void |
| 443 |
__bt_setcur(t, pgno, index) |
| 444 |
BTREE *t; |
| 445 |
pgno_t pgno; |
| 446 |
u_int index; |
| 447 |
{ |
| 448 |
|
| 449 |
if (t->bt_cursor.key.data != NULL) { |
| 450 |
free(t->bt_cursor.key.data); |
| 451 |
t->bt_cursor.key.size = 0; |
| 452 |
t->bt_cursor.key.data = NULL; |
| 453 |
} |
| 454 |
F_CLR(&t->bt_cursor, CURS_ACQUIRE | CURS_AFTER | CURS_BEFORE); |
| 455 |
|
| 456 |
|
| 457 |
t->bt_cursor.pg.pgno = pgno; |
| 458 |
t->bt_cursor.pg.index = index; |
| 459 |
F_SET(&t->bt_cursor, CURS_INIT); |
| 460 |
} |