Update.
[glibc.git] / db2 / db / db_dup.c
blob59dfb85b92ce66efeb0df8cd90cd0768dc9c4f46
1 /*-
2 * See the file LICENSE for redistribution information.
4 * Copyright (c) 1996, 1997
5 * Sleepycat Software. All rights reserved.
6 */
8 #include "config.h"
10 #ifndef lint
11 static const char sccsid[] = "@(#)db_dup.c 10.11 (Sleepycat) 1/8/98";
12 #endif /* not lint */
14 #ifndef NO_SYSTEM_INCLUDES
15 #include <sys/types.h>
16 #include <sys/stat.h>
18 #include <errno.h>
19 #include <fcntl.h>
20 #include <stddef.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <unistd.h>
25 #endif
27 #include "db_int.h"
28 #include "db_page.h"
29 #include "db_swap.h"
30 #include "btree.h"
31 #include "db_am.h"
32 #include "common_ext.h"
34 static int __db_addpage __P((DB *,
35 PAGE **, db_indx_t *, int (*)(DB *, u_int32_t, PAGE **)));
36 static int __db_dsplit __P((DB *,
37 PAGE **, db_indx_t *, u_int32_t, int (*)(DB *, u_int32_t, PAGE **)));
40 * __db_dput --
41 * Put a duplicate item onto a duplicate page at the given index.
43 * PUBLIC: int __db_dput __P((DB *,
44 * PUBLIC: DBT *, PAGE **, db_indx_t *, int (*)(DB *, u_int32_t, PAGE **)));
46 int
47 __db_dput(dbp, dbt, pp, indxp, newfunc)
48 DB *dbp;
49 DBT *dbt;
50 PAGE **pp;
51 db_indx_t *indxp;
52 int (*newfunc) __P((DB *, u_int32_t, PAGE **));
54 BOVERFLOW bo;
55 DBT *data_dbtp, hdr_dbt, *hdr_dbtp;
56 PAGE *pagep;
57 db_indx_t size, isize;
58 db_pgno_t pgno;
59 int ret;
62 * We need some access method independent threshold for when we put
63 * a duplicate item onto an overflow page.
65 if (dbt->size > 0.25 * dbp->pgsize) {
66 if ((ret = __db_poff(dbp, dbt, &pgno, newfunc)) != 0)
67 return (ret);
68 B_TSET(bo.type, B_OVERFLOW, 0);
69 bo.tlen = dbt->size;
70 bo.pgno = pgno;
71 hdr_dbt.data = &bo;
72 hdr_dbt.size = isize = BOVERFLOW_SIZE;
73 hdr_dbtp = &hdr_dbt;
74 size = BOVERFLOW_PSIZE;
75 data_dbtp = NULL;
76 } else {
77 size = BKEYDATA_PSIZE(dbt->size);
78 isize = BKEYDATA_SIZE(dbt->size);
79 hdr_dbtp = NULL;
80 data_dbtp = dbt;
83 pagep = *pp;
84 if (size > P_FREESPACE(pagep)) {
85 if (*indxp == NUM_ENT(*pp) && NEXT_PGNO(*pp) == PGNO_INVALID)
86 ret = __db_addpage(dbp, pp, indxp, newfunc);
87 else
88 ret = __db_dsplit(dbp, pp, indxp, isize, newfunc);
89 if (ret != 0)
90 /* XXX: Pages not returned to free list. */
91 return (ret);
92 pagep = *pp;
96 * Now, pagep references the page on which to insert and indx is the
97 * the location to insert.
99 if ((ret = __db_pitem(dbp,
100 pagep, (u_int32_t)*indxp, isize, hdr_dbtp, data_dbtp)) != 0)
101 return (ret);
103 (void)memp_fset(dbp->mpf, pagep, DB_MPOOL_DIRTY);
104 return (0);
108 * __db_drem --
109 * Remove a duplicate at the given index on the given page.
111 * PUBLIC: int __db_drem __P((DB *,
112 * PUBLIC: PAGE **, u_int32_t, int (*)(DB *, PAGE *)));
115 __db_drem(dbp, pp, indx, freefunc)
116 DB *dbp;
117 PAGE **pp;
118 u_int32_t indx;
119 int (*freefunc) __P((DB *, PAGE *));
121 PAGE *pagep;
122 int ret;
124 pagep = *pp;
126 /* Check if we are freeing a big item. */
127 if (B_TYPE(GET_BKEYDATA(pagep, indx)->type) == B_OVERFLOW) {
128 if ((ret = __db_doff(dbp,
129 GET_BOVERFLOW(pagep, indx)->pgno, freefunc)) != 0)
130 return (ret);
131 ret = __db_ditem(dbp, pagep, indx, BOVERFLOW_SIZE);
132 } else
133 ret = __db_ditem(dbp, pagep, indx,
134 BKEYDATA_SIZE(GET_BKEYDATA(pagep, indx)->len));
135 if (ret != 0)
136 return (ret);
138 if (NUM_ENT(pagep) == 0) {
140 * If the page is emptied, then the page is freed and the pp
141 * parameter is set to reference the next, locked page in the
142 * duplicate chain, if one exists. If there was no such page,
143 * then it is set to NULL.
145 * !!!
146 * __db_relink will set the dirty bit for us.
148 if ((ret = __db_relink(dbp, pagep, pp, 0)) != 0)
149 return (ret);
150 if ((ret = freefunc(dbp, pagep)) != 0)
151 return (ret);
152 } else
153 (void)memp_fset(dbp->mpf, pagep, DB_MPOOL_DIRTY);
155 return (0);
159 * __db_dend --
160 * Find the last page in a set of offpage duplicates.
162 * PUBLIC: int __db_dend __P((DB *, db_pgno_t, PAGE **));
165 __db_dend(dbp, pgno, pagep)
166 DB *dbp;
167 db_pgno_t pgno;
168 PAGE **pagep;
170 PAGE *h;
171 int ret;
174 * This implements DB_KEYLAST. The last page is returned in pp; pgno
175 * should be the page number of the first page of the duplicate chain.
177 for (;;) {
178 if ((ret = memp_fget(dbp->mpf, &pgno, 0, &h)) != 0) {
179 (void)__db_pgerr(dbp, pgno);
180 return (ret);
182 if ((pgno = NEXT_PGNO(h)) == PGNO_INVALID)
183 break;
184 (void)memp_fput(dbp->mpf, h, 0);
187 *pagep = h;
188 return (0);
192 * __db_dsplit --
193 * Split a page of duplicates, calculating the split point based
194 * on an element of size "size" being added at "*indxp".
195 * On entry hp contains a pointer to the page-pointer of the original
196 * page. On exit, it returns a pointer to the page containing "*indxp"
197 * and "indxp" has been modified to reflect the index on the new page
198 * where the element should be added. The function returns with
199 * the page on which the insert should happen, not yet put.
201 static int
202 __db_dsplit(dbp, hp, indxp, size, newfunc)
203 DB *dbp;
204 PAGE **hp;
205 db_indx_t *indxp;
206 u_int32_t size;
207 int (*newfunc) __P((DB *, u_int32_t, PAGE **));
209 PAGE *h, *np, *tp;
210 BKEYDATA *bk;
211 DBT page_dbt;
212 db_indx_t indx, nindex, oindex, sum;
213 db_indx_t halfbytes, i, lastsum;
214 int did_indx, ret, s;
216 h = *hp;
217 indx = *indxp;
219 /* Create a temporary page to do compaction onto. */
220 if ((tp = (PAGE *)__db_malloc(dbp->pgsize)) == NULL)
221 return (ENOMEM);
222 #ifdef DEBUG
223 memset(tp, 0xff, dbp->pgsize);
224 #endif
225 /* Create new page for the split. */
226 if ((ret = newfunc(dbp, P_DUPLICATE, &np)) != 0) {
227 FREE(tp, dbp->pgsize);
228 return (ret);
231 P_INIT(np, dbp->pgsize, PGNO(np), PGNO(h), NEXT_PGNO(h), 0,
232 P_DUPLICATE);
233 P_INIT(tp, dbp->pgsize, PGNO(h), PREV_PGNO(h), PGNO(np), 0,
234 P_DUPLICATE);
236 /* Figure out the split point */
237 halfbytes = (dbp->pgsize - HOFFSET(h)) / 2;
238 did_indx = 0;
239 for (sum = 0, lastsum = 0, i = 0; i < NUM_ENT(h); i++) {
240 if (i == indx) {
241 sum += size;
242 if (lastsum < halfbytes && sum >= halfbytes) {
243 /* We've crossed the halfway point. */
244 if ((db_indx_t)(halfbytes - lastsum) <
245 (db_indx_t)(sum - halfbytes)) {
246 *hp = np;
247 *indxp = 0;
248 i--;
249 } else
250 *indxp = i;
251 break;
253 *indxp = i;
254 lastsum = sum;
255 did_indx = 1;
257 if (B_TYPE(GET_BKEYDATA(h, i)->type) == B_KEYDATA)
258 sum += BKEYDATA_SIZE(GET_BKEYDATA(h, i)->len);
259 else
260 sum += BOVERFLOW_SIZE;
262 if (lastsum < halfbytes && sum >= halfbytes) {
263 /* We've crossed the halfway point. */
264 if ((db_indx_t)(halfbytes - lastsum) <
265 (db_indx_t)(sum - halfbytes))
266 i--;
267 break;
272 * Check if we have set the return values of the index pointer and
273 * page pointer.
275 if (!did_indx) {
276 *hp = np;
277 *indxp = indx - i - 1;
280 if (DB_LOGGING(dbp)) {
281 page_dbt.size = dbp->pgsize;
282 page_dbt.data = h;
283 if ((ret = __db_split_log(dbp->dbenv->lg_info,
284 dbp->txn, &LSN(h), 0, DB_SPLITOLD, dbp->log_fileid,
285 PGNO(h), &page_dbt, &LSN(h))) != 0) {
286 FREE(tp, dbp->pgsize);
287 return (ret);
289 LSN(tp) = LSN(h);
293 * If it's a btree, adjust the cursors.
295 * i is the index of the last element to stay on the page.
297 if (dbp->type == DB_BTREE || dbp->type == DB_RECNO)
298 __bam_ca_split(dbp, PGNO(h), PGNO(h), PGNO(np), i + 1, 0);
300 for (nindex = 0, oindex = i + 1; oindex < NUM_ENT(h); oindex++) {
301 bk = GET_BKEYDATA(h, oindex);
302 if (B_TYPE(bk->type) == B_KEYDATA)
303 s = BKEYDATA_SIZE(bk->len);
304 else
305 s = BOVERFLOW_SIZE;
307 np->inp[nindex++] = HOFFSET(np) -= s;
308 memcpy((u_int8_t *)np + HOFFSET(np), bk, s);
309 NUM_ENT(np)++;
313 * Now do data compaction by copying the remaining stuff onto the
314 * temporary page and then copying it back to the real page.
316 for (nindex = 0, oindex = 0; oindex <= i; oindex++) {
317 bk = GET_BKEYDATA(h, oindex);
318 if (B_TYPE(bk->type) == B_KEYDATA)
319 s = BKEYDATA_SIZE(bk->len);
320 else
321 s = BOVERFLOW_SIZE;
323 tp->inp[nindex++] = HOFFSET(tp) -= s;
324 memcpy((u_int8_t *)tp + HOFFSET(tp), bk, s);
325 NUM_ENT(tp)++;
329 * This page (the temporary) should be only half full, so we do two
330 * memcpy's, one for the top of the page and one for the bottom of
331 * the page. This way we avoid copying the middle which should be
332 * about half a page.
334 memcpy(h, tp, LOFFSET(tp));
335 memcpy((u_int8_t *)h + HOFFSET(tp),
336 (u_int8_t *)tp + HOFFSET(tp), dbp->pgsize - HOFFSET(tp));
337 FREE(tp, dbp->pgsize);
339 if (DB_LOGGING(dbp)) {
340 page_dbt.size = dbp->pgsize;
341 page_dbt.data = h;
342 if ((ret = __db_split_log(dbp->dbenv->lg_info,
343 dbp->txn, &LSN(h), 0, DB_SPLITNEW, dbp->log_fileid,
344 PGNO(h), &page_dbt, &LSN(h))) != 0)
345 return (ret);
347 page_dbt.size = dbp->pgsize;
348 page_dbt.data = np;
349 if ((ret = __db_split_log(dbp->dbenv->lg_info,
350 dbp->txn, &LSN(np), 0, DB_SPLITNEW, dbp->log_fileid,
351 PGNO(np), &page_dbt, &LSN(np))) != 0)
352 return (ret);
356 * Figure out if the location we're interested in is on the new
357 * page, and if so, reset the callers' pointer. Push the other
358 * page back to the store.
360 if (*hp == h)
361 ret = memp_fput(dbp->mpf, np, DB_MPOOL_DIRTY);
362 else
363 ret = memp_fput(dbp->mpf, h, DB_MPOOL_DIRTY);
365 return (ret);
369 * __db_ditem --
370 * Remove an item from a page.
372 * PUBLIC: int __db_ditem __P((DB *, PAGE *, u_int32_t, u_int32_t));
375 __db_ditem(dbp, pagep, indx, nbytes)
376 DB *dbp;
377 PAGE *pagep;
378 u_int32_t indx, nbytes;
380 DBT ldbt;
381 db_indx_t cnt, offset;
382 int ret;
383 u_int8_t *from;
385 if (DB_LOGGING(dbp)) {
386 ldbt.data = P_ENTRY(pagep, indx);
387 ldbt.size = nbytes;
388 if ((ret = __db_addrem_log(dbp->dbenv->lg_info, dbp->txn,
389 &LSN(pagep), 0, DB_REM_DUP, dbp->log_fileid, PGNO(pagep),
390 (u_int32_t)indx, nbytes, &ldbt, NULL, &LSN(pagep))) != 0)
391 return (ret);
395 * If there's only a single item on the page, we don't have to
396 * work hard.
398 if (NUM_ENT(pagep) == 1) {
399 NUM_ENT(pagep) = 0;
400 HOFFSET(pagep) = dbp->pgsize;
401 return (0);
405 * Pack the remaining key/data items at the end of the page. Use
406 * memmove(3), the regions may overlap.
408 from = (u_int8_t *)pagep + HOFFSET(pagep);
409 memmove(from + nbytes, from, pagep->inp[indx] - HOFFSET(pagep));
410 HOFFSET(pagep) += nbytes;
412 /* Adjust the indices' offsets. */
413 offset = pagep->inp[indx];
414 for (cnt = 0; cnt < NUM_ENT(pagep); ++cnt)
415 if (pagep->inp[cnt] < offset)
416 pagep->inp[cnt] += nbytes;
418 /* Shift the indices down. */
419 --NUM_ENT(pagep);
420 if (indx != NUM_ENT(pagep))
421 memmove(&pagep->inp[indx], &pagep->inp[indx + 1],
422 sizeof(db_indx_t) * (NUM_ENT(pagep) - indx));
424 /* If it's a btree, adjust the cursors. */
425 if (dbp->type == DB_BTREE || dbp->type == DB_RECNO)
426 __bam_ca_di(dbp, PGNO(pagep), indx, -1);
428 return (0);
432 * __db_pitem --
433 * Put an item on a page.
435 * PUBLIC: int __db_pitem
436 * PUBLIC: __P((DB *, PAGE *, u_int32_t, u_int32_t, DBT *, DBT *));
439 __db_pitem(dbp, pagep, indx, nbytes, hdr, data)
440 DB *dbp;
441 PAGE *pagep;
442 u_int32_t indx;
443 u_int32_t nbytes;
444 DBT *hdr, *data;
446 BKEYDATA bk;
447 DBT thdr;
448 int ret;
449 u_int8_t *p;
452 * Put a single item onto a page. The logic figuring out where to
453 * insert and whether it fits is handled in the caller. All we do
454 * here is manage the page shuffling. We cheat a little bit in that
455 * we don't want to copy the dbt on a normal put twice. If hdr is
456 * NULL, we create a BKEYDATA structure on the page, otherwise, just
457 * copy the caller's information onto the page.
459 * This routine is also used to put entries onto the page where the
460 * entry is pre-built, e.g., during recovery. In this case, the hdr
461 * will point to the entry, and the data argument will be NULL.
463 * !!!
464 * There's a tremendous potential for off-by-one errors here, since
465 * the passed in header sizes must be adjusted for the structure's
466 * placeholder for the trailing variable-length data field.
468 if (DB_LOGGING(dbp))
469 if ((ret = __db_addrem_log(dbp->dbenv->lg_info, dbp->txn,
470 &LSN(pagep), 0, DB_ADD_DUP, dbp->log_fileid, PGNO(pagep),
471 (u_int32_t)indx, nbytes, hdr, data, &LSN(pagep))) != 0)
472 return (ret);
474 if (hdr == NULL) {
475 B_TSET(bk.type, B_KEYDATA, 0);
476 bk.len = data == NULL ? 0 : data->size;
478 thdr.data = &bk;
479 thdr.size = SSZA(BKEYDATA, data);
480 hdr = &thdr;
483 /* Adjust the index table, then put the item on the page. */
484 if (indx != NUM_ENT(pagep))
485 memmove(&pagep->inp[indx + 1], &pagep->inp[indx],
486 sizeof(db_indx_t) * (NUM_ENT(pagep) - indx));
487 HOFFSET(pagep) -= nbytes;
488 pagep->inp[indx] = HOFFSET(pagep);
489 ++NUM_ENT(pagep);
491 p = P_ENTRY(pagep, indx);
492 memcpy(p, hdr->data, hdr->size);
493 if (data != NULL)
494 memcpy(p + hdr->size, data->data, data->size);
496 /* If it's a btree, adjust the cursors. */
497 if (dbp->type == DB_BTREE || dbp->type == DB_RECNO)
498 __bam_ca_di(dbp, PGNO(pagep), indx, 1);
500 return (0);
504 * __db_relink --
505 * Relink around a deleted page.
507 * PUBLIC: int __db_relink __P((DB *, PAGE *, PAGE **, int));
510 __db_relink(dbp, pagep, new_next, needlock)
511 DB *dbp;
512 PAGE *pagep, **new_next;
513 int needlock;
515 PAGE *np, *pp;
516 DB_LOCK npl, ppl;
517 DB_LSN *nlsnp, *plsnp;
518 int ret;
520 ret = 0;
521 np = pp = NULL;
522 npl = ppl = LOCK_INVALID;
523 nlsnp = plsnp = NULL;
525 /* Retrieve and lock the two pages. */
526 if (pagep->next_pgno != PGNO_INVALID) {
527 if (needlock && (ret = __bam_lget(dbp,
528 0, pagep->next_pgno, DB_LOCK_WRITE, &npl)) != 0)
529 goto err;
530 if ((ret = memp_fget(dbp->mpf,
531 &pagep->next_pgno, 0, &np)) != 0) {
532 (void)__db_pgerr(dbp, pagep->next_pgno);
533 goto err;
535 nlsnp = &np->lsn;
537 if (pagep->prev_pgno != PGNO_INVALID) {
538 if (needlock && (ret = __bam_lget(dbp,
539 0, pagep->prev_pgno, DB_LOCK_WRITE, &ppl)) != 0)
540 goto err;
541 if ((ret = memp_fget(dbp->mpf,
542 &pagep->prev_pgno, 0, &pp)) != 0) {
543 (void)__db_pgerr(dbp, pagep->next_pgno);
544 goto err;
546 plsnp = &pp->lsn;
549 /* Log the change. */
550 if (DB_LOGGING(dbp)) {
551 if ((ret = __db_relink_log(dbp->dbenv->lg_info, dbp->txn,
552 &pagep->lsn, 0, dbp->log_fileid, pagep->pgno, &pagep->lsn,
553 pagep->prev_pgno, plsnp, pagep->next_pgno, nlsnp)) != 0)
554 goto err;
555 if (np != NULL)
556 np->lsn = pagep->lsn;
557 if (pp != NULL)
558 pp->lsn = pagep->lsn;
562 * Modify and release the two pages.
564 * !!!
565 * The parameter new_next gets set to the page following the page we
566 * are removing. If there is no following page, then new_next gets
567 * set to NULL.
569 if (np != NULL) {
570 np->prev_pgno = pagep->prev_pgno;
571 if (new_next == NULL)
572 ret = memp_fput(dbp->mpf, np, DB_MPOOL_DIRTY);
573 else {
574 *new_next = np;
575 ret = memp_fset(dbp->mpf, np, DB_MPOOL_DIRTY);
577 if (ret != 0)
578 goto err;
579 if (needlock)
580 (void)__bam_lput(dbp, npl);
581 } else if (new_next != NULL)
582 *new_next = NULL;
584 if (pp != NULL) {
585 pp->next_pgno = pagep->next_pgno;
586 if ((ret = memp_fput(dbp->mpf, pp, DB_MPOOL_DIRTY)) != 0)
587 goto err;
588 if (needlock)
589 (void)__bam_lput(dbp, ppl);
591 return (0);
593 err: if (np != NULL)
594 (void)memp_fput(dbp->mpf, np, 0);
595 if (needlock && npl != LOCK_INVALID)
596 (void)__bam_lput(dbp, npl);
597 if (pp != NULL)
598 (void)memp_fput(dbp->mpf, pp, 0);
599 if (needlock && ppl != LOCK_INVALID)
600 (void)__bam_lput(dbp, ppl);
601 return (ret);
605 * __db_ddup --
606 * Delete an offpage chain of duplicates.
608 * PUBLIC: int __db_ddup __P((DB *, db_pgno_t, int (*)(DB *, PAGE *)));
611 __db_ddup(dbp, pgno, freefunc)
612 DB *dbp;
613 db_pgno_t pgno;
614 int (*freefunc) __P((DB *, PAGE *));
616 PAGE *pagep;
617 DBT tmp_dbt;
618 int ret;
620 do {
621 if ((ret = memp_fget(dbp->mpf, &pgno, 0, &pagep)) != 0) {
622 (void)__db_pgerr(dbp, pgno);
623 return (ret);
626 if (DB_LOGGING(dbp)) {
627 tmp_dbt.data = pagep;
628 tmp_dbt.size = dbp->pgsize;
629 if ((ret = __db_split_log(dbp->dbenv->lg_info, dbp->txn,
630 &LSN(pagep), 0, DB_SPLITOLD, dbp->log_fileid,
631 PGNO(pagep), &tmp_dbt, &LSN(pagep))) != 0)
632 return (ret);
634 pgno = pagep->next_pgno;
635 if ((ret = freefunc(dbp, pagep)) != 0)
636 return (ret);
637 } while (pgno != PGNO_INVALID);
639 return (0);
643 * __db_addpage --
644 * Create a new page and link it onto the next_pgno field of the
645 * current page.
647 static int
648 __db_addpage(dbp, hp, indxp, newfunc)
649 DB *dbp;
650 PAGE **hp;
651 db_indx_t *indxp;
652 int (*newfunc) __P((DB *, u_int32_t, PAGE **));
654 PAGE *newpage;
655 int ret;
657 if ((ret = newfunc(dbp, P_DUPLICATE, &newpage)) != 0)
658 return (ret);
660 if (DB_LOGGING(dbp)) {
661 if ((ret = __db_addpage_log(dbp->dbenv->lg_info,
662 dbp->txn, &LSN(*hp), 0, dbp->log_fileid,
663 PGNO(*hp), &LSN(*hp), PGNO(newpage), &LSN(newpage))) != 0) {
664 return (ret);
666 LSN(newpage) = LSN(*hp);
669 PREV_PGNO(newpage) = PGNO(*hp);
670 NEXT_PGNO(*hp) = PGNO(newpage);
672 if ((ret = memp_fput(dbp->mpf, *hp, DB_MPOOL_DIRTY)) != 0)
673 return (ret);
674 *hp = newpage;
675 *indxp = 0;
676 return (0);