Update.
[glibc.git] / db2 / db / db_dup.c
blobfaeefa0744a1196f9400f5238e34f597910a7097
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.10 (Sleepycat) 10/25/97";
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 *, int, u_int32_t));
375 __db_ditem(dbp, pagep, indx, nbytes)
376 DB *dbp;
377 PAGE *pagep;
378 int indx;
379 u_int32_t nbytes;
381 DBT ldbt;
382 db_indx_t cnt, offset;
383 int ret;
384 u_int8_t *from;
386 if (DB_LOGGING(dbp)) {
387 ldbt.data = P_ENTRY(pagep, indx);
388 ldbt.size = nbytes;
389 if ((ret = __db_addrem_log(dbp->dbenv->lg_info, dbp->txn,
390 &LSN(pagep), 0, DB_REM_DUP, dbp->log_fileid, PGNO(pagep),
391 (u_int32_t)indx, nbytes, &ldbt, NULL, &LSN(pagep))) != 0)
392 return (ret);
396 * If there's only a single item on the page, we don't have to
397 * work hard.
399 if (NUM_ENT(pagep) == 1) {
400 NUM_ENT(pagep) = 0;
401 HOFFSET(pagep) = dbp->pgsize;
402 return (0);
406 * Pack the remaining key/data items at the end of the page. Use
407 * memmove(3), the regions may overlap.
409 from = (u_int8_t *)pagep + HOFFSET(pagep);
410 memmove(from + nbytes, from, pagep->inp[indx] - HOFFSET(pagep));
411 HOFFSET(pagep) += nbytes;
413 /* Adjust the indices' offsets. */
414 offset = pagep->inp[indx];
415 for (cnt = 0; cnt < NUM_ENT(pagep); ++cnt)
416 if (pagep->inp[cnt] < offset)
417 pagep->inp[cnt] += nbytes;
419 /* Shift the indices down. */
420 --NUM_ENT(pagep);
421 if (indx != NUM_ENT(pagep))
422 memmove(&pagep->inp[indx], &pagep->inp[indx + 1],
423 sizeof(db_indx_t) * (NUM_ENT(pagep) - indx));
425 /* If it's a btree, adjust the cursors. */
426 if (dbp->type == DB_BTREE || dbp->type == DB_RECNO)
427 __bam_ca_di(dbp, PGNO(pagep), indx, -1);
429 return (0);
433 * __db_pitem --
434 * Put an item on a page.
436 * PUBLIC: int __db_pitem
437 * PUBLIC: __P((DB *, PAGE *, u_int32_t, u_int32_t, DBT *, DBT *));
440 __db_pitem(dbp, pagep, indx, nbytes, hdr, data)
441 DB *dbp;
442 PAGE *pagep;
443 u_int32_t indx;
444 u_int32_t nbytes;
445 DBT *hdr, *data;
447 BKEYDATA bk;
448 DBT thdr;
449 int ret;
450 u_int8_t *p;
453 * Put a single item onto a page. The logic figuring out where to
454 * insert and whether it fits is handled in the caller. All we do
455 * here is manage the page shuffling. We cheat a little bit in that
456 * we don't want to copy the dbt on a normal put twice. If hdr is
457 * NULL, we create a BKEYDATA structure on the page, otherwise, just
458 * copy the caller's information onto the page.
460 * This routine is also used to put entries onto the page where the
461 * entry is pre-built, e.g., during recovery. In this case, the hdr
462 * will point to the entry, and the data argument will be NULL.
464 * !!!
465 * There's a tremendous potential for off-by-one errors here, since
466 * the passed in header sizes must be adjusted for the structure's
467 * placeholder for the trailing variable-length data field.
469 if (DB_LOGGING(dbp))
470 if ((ret = __db_addrem_log(dbp->dbenv->lg_info, dbp->txn,
471 &LSN(pagep), 0, DB_ADD_DUP, dbp->log_fileid, PGNO(pagep),
472 (u_int32_t)indx, nbytes, hdr, data, &LSN(pagep))) != 0)
473 return (ret);
475 if (hdr == NULL) {
476 B_TSET(bk.type, B_KEYDATA, 0);
477 bk.len = data == NULL ? 0 : data->size;
479 thdr.data = &bk;
480 thdr.size = SSZA(BKEYDATA, data);
481 hdr = &thdr;
484 /* Adjust the index table, then put the item on the page. */
485 if (indx != NUM_ENT(pagep))
486 memmove(&pagep->inp[indx + 1], &pagep->inp[indx],
487 sizeof(db_indx_t) * (NUM_ENT(pagep) - indx));
488 HOFFSET(pagep) -= nbytes;
489 pagep->inp[indx] = HOFFSET(pagep);
490 ++NUM_ENT(pagep);
492 p = P_ENTRY(pagep, indx);
493 memcpy(p, hdr->data, hdr->size);
494 if (data != NULL)
495 memcpy(p + hdr->size, data->data, data->size);
497 /* If it's a btree, adjust the cursors. */
498 if (dbp->type == DB_BTREE || dbp->type == DB_RECNO)
499 __bam_ca_di(dbp, PGNO(pagep), indx, 1);
501 return (0);
505 * __db_relink --
506 * Relink around a deleted page.
508 * PUBLIC: int __db_relink __P((DB *, PAGE *, PAGE **, int));
511 __db_relink(dbp, pagep, new_next, needlock)
512 DB *dbp;
513 PAGE *pagep, **new_next;
514 int needlock;
516 PAGE *np, *pp;
517 DB_LOCK npl, ppl;
518 DB_LSN *nlsnp, *plsnp;
519 int ret;
521 ret = 0;
522 np = pp = NULL;
523 npl = ppl = LOCK_INVALID;
524 nlsnp = plsnp = NULL;
526 /* Retrieve and lock the two pages. */
527 if (pagep->next_pgno != PGNO_INVALID) {
528 if (needlock && (ret = __bam_lget(dbp,
529 0, pagep->next_pgno, DB_LOCK_WRITE, &npl)) != 0)
530 goto err;
531 if ((ret = memp_fget(dbp->mpf,
532 &pagep->next_pgno, 0, &np)) != 0) {
533 (void)__db_pgerr(dbp, pagep->next_pgno);
534 goto err;
536 nlsnp = &np->lsn;
538 if (pagep->prev_pgno != PGNO_INVALID) {
539 if (needlock && (ret = __bam_lget(dbp,
540 0, pagep->prev_pgno, DB_LOCK_WRITE, &ppl)) != 0)
541 goto err;
542 if ((ret = memp_fget(dbp->mpf,
543 &pagep->prev_pgno, 0, &pp)) != 0) {
544 (void)__db_pgerr(dbp, pagep->next_pgno);
545 goto err;
547 plsnp = &pp->lsn;
550 /* Log the change. */
551 if (DB_LOGGING(dbp)) {
552 if ((ret = __db_relink_log(dbp->dbenv->lg_info, dbp->txn,
553 &pagep->lsn, 0, dbp->log_fileid, pagep->pgno, &pagep->lsn,
554 pagep->prev_pgno, plsnp, pagep->next_pgno, nlsnp)) != 0)
555 goto err;
556 if (np != NULL)
557 np->lsn = pagep->lsn;
558 if (pp != NULL)
559 pp->lsn = pagep->lsn;
563 * Modify and release the two pages.
565 * !!!
566 * The parameter new_next gets set to the page following the page we
567 * are removing. If there is no following page, then new_next gets
568 * set to NULL.
570 if (np != NULL) {
571 np->prev_pgno = pagep->prev_pgno;
572 if (new_next == NULL)
573 ret = memp_fput(dbp->mpf, np, DB_MPOOL_DIRTY);
574 else {
575 *new_next = np;
576 ret = memp_fset(dbp->mpf, np, DB_MPOOL_DIRTY);
578 if (ret != 0)
579 goto err;
580 if (needlock)
581 (void)__bam_lput(dbp, npl);
582 } else if (new_next != NULL)
583 *new_next = NULL;
585 if (pp != NULL) {
586 pp->next_pgno = pagep->next_pgno;
587 if ((ret = memp_fput(dbp->mpf, pp, DB_MPOOL_DIRTY)) != 0)
588 goto err;
589 if (needlock)
590 (void)__bam_lput(dbp, ppl);
592 return (0);
594 err: if (np != NULL)
595 (void)memp_fput(dbp->mpf, np, 0);
596 if (needlock && npl != LOCK_INVALID)
597 (void)__bam_lput(dbp, npl);
598 if (pp != NULL)
599 (void)memp_fput(dbp->mpf, pp, 0);
600 if (needlock && ppl != LOCK_INVALID)
601 (void)__bam_lput(dbp, ppl);
602 return (ret);
606 * __db_ddup --
607 * Delete an offpage chain of duplicates.
609 * PUBLIC: int __db_ddup __P((DB *, db_pgno_t, int (*)(DB *, PAGE *)));
612 __db_ddup(dbp, pgno, freefunc)
613 DB *dbp;
614 db_pgno_t pgno;
615 int (*freefunc) __P((DB *, PAGE *));
617 PAGE *pagep;
618 DBT tmp_dbt;
619 int ret;
621 do {
622 if ((ret = memp_fget(dbp->mpf, &pgno, 0, &pagep)) != 0) {
623 (void)__db_pgerr(dbp, pgno);
624 return (ret);
627 if (DB_LOGGING(dbp)) {
628 tmp_dbt.data = pagep;
629 tmp_dbt.size = dbp->pgsize;
630 if ((ret = __db_split_log(dbp->dbenv->lg_info, dbp->txn,
631 &LSN(pagep), 0, DB_SPLITOLD, dbp->log_fileid,
632 PGNO(pagep), &tmp_dbt, &LSN(pagep))) != 0)
633 return (ret);
635 pgno = pagep->next_pgno;
636 if ((ret = freefunc(dbp, pagep)) != 0)
637 return (ret);
638 } while (pgno != PGNO_INVALID);
640 return (0);
644 * __db_addpage --
645 * Create a new page and link it onto the next_pgno field of the
646 * current page.
648 static int
649 __db_addpage(dbp, hp, indxp, newfunc)
650 DB *dbp;
651 PAGE **hp;
652 db_indx_t *indxp;
653 int (*newfunc) __P((DB *, u_int32_t, PAGE **));
655 PAGE *newpage;
656 int ret;
658 if ((ret = newfunc(dbp, P_DUPLICATE, &newpage)) != 0)
659 return (ret);
661 if (DB_LOGGING(dbp)) {
662 if ((ret = __db_addpage_log(dbp->dbenv->lg_info,
663 dbp->txn, &LSN(*hp), 0, dbp->log_fileid,
664 PGNO(*hp), &LSN(*hp), PGNO(newpage), &LSN(newpage))) != 0) {
665 return (ret);
667 LSN(newpage) = LSN(*hp);
670 PREV_PGNO(newpage) = PGNO(*hp);
671 NEXT_PGNO(*hp) = PGNO(newpage);
673 if ((ret = memp_fput(dbp->mpf, *hp, DB_MPOOL_DIRTY)) != 0)
674 return (ret);
675 *hp = newpage;
676 *indxp = 0;
677 return (0);