/[resiprocate]/main/contrib/db/btree/bt_put.c
ViewVC logotype

Contents of /main/contrib/db/btree/bt_put.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 5295 - (show annotations) (download)
Mon Aug 22 00:30:05 2005 UTC (14 years, 3 months ago) by jason
File MIME type: text/plain
File size: 23532 byte(s)
merged 5270:HEAD from b-directory-reorg
1 /*-
2 * See the file LICENSE for redistribution information.
3 *
4 * Copyright (c) 1996-2004
5 * Sleepycat Software. All rights reserved.
6 */
7 /*
8 * Copyright (c) 1990, 1993, 1994, 1995, 1996
9 * Keith Bostic. All rights reserved.
10 */
11 /*
12 * Copyright (c) 1990, 1993, 1994, 1995
13 * The Regents of the University of California. All rights reserved.
14 *
15 * This code is derived from software contributed to Berkeley by
16 * Mike Olson.
17 *
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions
20 * are met:
21 * 1. Redistributions of source code must retain the above copyright
22 * notice, this list of conditions and the following disclaimer.
23 * 2. Redistributions in binary form must reproduce the above copyright
24 * notice, this list of conditions and the following disclaimer in the
25 * documentation and/or other materials provided with the distribution.
26 * 3. Neither the name of the University nor the names of its contributors
27 * may be used to endorse or promote products derived from this software
28 * without specific prior written permission.
29 *
30 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
31 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
34 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
38 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
39 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 * SUCH DAMAGE.
41 *
42 * $Id: bt_put.c,v 11.80 2004/10/29 17:33:25 ubell Exp $
43 */
44
45 #include "db_config.h"
46
47 #ifndef NO_SYSTEM_INCLUDES
48 #include <sys/types.h>
49
50 #include <string.h>
51 #endif
52
53 #include "db_int.h"
54 #include "dbinc/db_page.h"
55 #include "dbinc/db_shash.h"
56 #include "dbinc/btree.h"
57 #include "dbinc/mp.h"
58
59 static int __bam_build
60 __P((DBC *, u_int32_t, DBT *, PAGE *, u_int32_t, u_int32_t));
61 static int __bam_dup_convert __P((DBC *, PAGE *, u_int32_t));
62 static int __bam_ovput
63 __P((DBC *, u_int32_t, db_pgno_t, PAGE *, u_int32_t, DBT *));
64 static u_int32_t
65 __bam_partsize __P((DB *, u_int32_t, DBT *, PAGE *, u_int32_t));
66
67 /*
68 * __bam_iitem --
69 * Insert an item into the tree.
70 *
71 * PUBLIC: int __bam_iitem __P((DBC *, DBT *, DBT *, u_int32_t, u_int32_t));
72 */
73 int
74 __bam_iitem(dbc, key, data, op, flags)
75 DBC *dbc;
76 DBT *key, *data;
77 u_int32_t op, flags;
78 {
79 DB_ENV *dbenv;
80 BKEYDATA *bk, bk_tmp;
81 BTREE *t;
82 BTREE_CURSOR *cp;
83 DB *dbp;
84 DBT bk_hdr, tdbt;
85 DB_MPOOLFILE *mpf;
86 PAGE *h;
87 db_indx_t indx;
88 u_int32_t data_size, have_bytes, need_bytes, needed;
89 int cmp, bigkey, bigdata, dupadjust, padrec, replace, ret, was_deleted;
90
91 COMPQUIET(bk, NULL);
92
93 dbp = dbc->dbp;
94 dbenv = dbp->dbenv;
95 mpf = dbp->mpf;
96 cp = (BTREE_CURSOR *)dbc->internal;
97 t = dbp->bt_internal;
98 h = cp->page;
99 indx = cp->indx;
100 dupadjust = replace = was_deleted = 0;
101
102 /*
103 * Fixed-length records with partial puts: it's an error to specify
104 * anything other simple overwrite.
105 */
106 if (F_ISSET(dbp, DB_AM_FIXEDLEN) &&
107 F_ISSET(data, DB_DBT_PARTIAL) && data->size != data->dlen)
108 return (__db_rec_repl(dbenv, data->size, data->dlen));
109
110 /*
111 * Figure out how much space the data will take, including if it's a
112 * partial record.
113 *
114 * Fixed-length records: it's an error to specify a record that's
115 * longer than the fixed-length, and we never require less than
116 * the fixed-length record size.
117 */
118 data_size = F_ISSET(data, DB_DBT_PARTIAL) ?
119 __bam_partsize(dbp, op, data, h, indx) : data->size;
120 padrec = 0;
121 if (F_ISSET(dbp, DB_AM_FIXEDLEN)) {
122 if (data_size > t->re_len)
123 return (__db_rec_toobig(dbenv, data_size, t->re_len));
124
125 /* Records that are deleted anyway needn't be padded out. */
126 if (!LF_ISSET(BI_DELETED) && data_size < t->re_len) {
127 padrec = 1;
128 data_size = t->re_len;
129 }
130 }
131
132 /*
133 * Handle partial puts or short fixed-length records: build the
134 * real record.
135 */
136 if (padrec || F_ISSET(data, DB_DBT_PARTIAL)) {
137 tdbt = *data;
138 if ((ret =
139 __bam_build(dbc, op, &tdbt, h, indx, data_size)) != 0)
140 return (ret);
141 data = &tdbt;
142 }
143
144 /*
145 * If the user has specified a duplicate comparison function, return
146 * an error if DB_CURRENT was specified and the replacement data
147 * doesn't compare equal to the current data. This stops apps from
148 * screwing up the duplicate sort order. We have to do this after
149 * we build the real record so that we're comparing the real items.
150 */
151 if (op == DB_CURRENT && dbp->dup_compare != NULL) {
152 if ((ret = __bam_cmp(dbp, data, h,
153 indx + (TYPE(h) == P_LBTREE ? O_INDX : 0),
154 dbp->dup_compare, &cmp)) != 0)
155 return (ret);
156 if (cmp != 0) {
157 __db_err(dbenv,
158 "Existing data sorts differently from put data");
159 return (EINVAL);
160 }
161 }
162
163 /*
164 * If the key or data item won't fit on a page, we'll have to store
165 * them on overflow pages.
166 */
167 needed = 0;
168 bigdata = data_size > cp->ovflsize;
169 switch (op) {
170 case DB_KEYFIRST:
171 /* We're adding a new key and data pair. */
172 bigkey = key->size > cp->ovflsize;
173 if (bigkey)
174 needed += BOVERFLOW_PSIZE;
175 else
176 needed += BKEYDATA_PSIZE(key->size);
177 if (bigdata)
178 needed += BOVERFLOW_PSIZE;
179 else
180 needed += BKEYDATA_PSIZE(data_size);
181 break;
182 case DB_AFTER:
183 case DB_BEFORE:
184 case DB_CURRENT:
185 /*
186 * We're either overwriting the data item of a key/data pair
187 * or we're creating a new on-page duplicate and only adding
188 * a data item.
189 *
190 * !!!
191 * We're not currently correcting for space reclaimed from
192 * already deleted items, but I don't think it's worth the
193 * complexity.
194 */
195 bigkey = 0;
196 if (op == DB_CURRENT) {
197 bk = GET_BKEYDATA(dbp, h,
198 indx + (TYPE(h) == P_LBTREE ? O_INDX : 0));
199 if (B_TYPE(bk->type) == B_KEYDATA)
200 have_bytes = BKEYDATA_PSIZE(bk->len);
201 else
202 have_bytes = BOVERFLOW_PSIZE;
203 need_bytes = 0;
204 } else {
205 have_bytes = 0;
206 need_bytes = sizeof(db_indx_t);
207 }
208 if (bigdata)
209 need_bytes += BOVERFLOW_PSIZE;
210 else
211 need_bytes += BKEYDATA_PSIZE(data_size);
212
213 if (have_bytes < need_bytes)
214 needed += need_bytes - have_bytes;
215 break;
216 default:
217 return (__db_unknown_flag(dbenv, "DB->put", op));
218 }
219
220 /*
221 * If there's not enough room, or the user has put a ceiling on the
222 * number of keys permitted in the page, split the page.
223 */
224 if (P_FREESPACE(dbp, h) < needed)
225 return (DB_NEEDSPLIT);
226
227 /*
228 * The code breaks it up into five cases:
229 *
230 * 1. Insert a new key/data pair.
231 * 2. Append a new data item (a new duplicate).
232 * 3. Insert a new data item (a new duplicate).
233 * 4. Delete and re-add the data item (overflow item).
234 * 5. Overwrite the data item.
235 */
236 switch (op) {
237 case DB_KEYFIRST: /* 1. Insert a new key/data pair. */
238 if (bigkey) {
239 if ((ret = __bam_ovput(dbc,
240 B_OVERFLOW, PGNO_INVALID, h, indx, key)) != 0)
241 return (ret);
242 } else
243 if ((ret = __db_pitem(dbc, h, indx,
244 BKEYDATA_SIZE(key->size), NULL, key)) != 0)
245 return (ret);
246
247 if ((ret = __bam_ca_di(dbc, PGNO(h), indx, 1)) != 0)
248 return (ret);
249 ++indx;
250 break;
251 case DB_AFTER: /* 2. Append a new data item. */
252 if (TYPE(h) == P_LBTREE) {
253 /* Copy the key for the duplicate and adjust cursors. */
254 if ((ret =
255 __bam_adjindx(dbc, h, indx + P_INDX, indx, 1)) != 0)
256 return (ret);
257 if ((ret =
258 __bam_ca_di(dbc, PGNO(h), indx + P_INDX, 1)) != 0)
259 return (ret);
260
261 indx += 3;
262 dupadjust = 1;
263
264 cp->indx += 2;
265 } else {
266 ++indx;
267 cp->indx += 1;
268 }
269 break;
270 case DB_BEFORE: /* 3. Insert a new data item. */
271 if (TYPE(h) == P_LBTREE) {
272 /* Copy the key for the duplicate and adjust cursors. */
273 if ((ret = __bam_adjindx(dbc, h, indx, indx, 1)) != 0)
274 return (ret);
275 if ((ret = __bam_ca_di(dbc, PGNO(h), indx, 1)) != 0)
276 return (ret);
277
278 ++indx;
279 dupadjust = 1;
280 }
281 break;
282 case DB_CURRENT:
283 /*
284 * Clear the cursor's deleted flag. The problem is that if
285 * we deadlock or fail while deleting the overflow item or
286 * replacing the non-overflow item, a subsequent cursor close
287 * will try and remove the item because the cursor's delete
288 * flag is set.
289 */
290 (void)__bam_ca_delete(dbp, PGNO(h), indx, 0);
291
292 if (TYPE(h) == P_LBTREE) {
293 ++indx;
294 dupadjust = 1;
295 }
296
297 /*
298 * In a Btree deleted records aren't counted (deleted records
299 * are counted in a Recno because all accesses are based on
300 * record number). If it's a Btree and it's a DB_CURRENT
301 * operation overwriting a previously deleted record, increment
302 * the record count.
303 */
304 if (TYPE(h) == P_LBTREE || TYPE(h) == P_LDUP)
305 was_deleted = B_DISSET(bk->type);
306
307 /*
308 * 4. Delete and re-add the data item.
309 *
310 * If we're changing the type of the on-page structure, or we
311 * are referencing offpage items, we have to delete and then
312 * re-add the item. We do not do any cursor adjustments here
313 * because we're going to immediately re-add the item into the
314 * same slot.
315 */
316 if (bigdata || B_TYPE(bk->type) != B_KEYDATA) {
317 if ((ret = __bam_ditem(dbc, h, indx)) != 0)
318 return (ret);
319 break;
320 }
321
322 /* 5. Overwrite the data item. */
323 replace = 1;
324 break;
325 default:
326 return (__db_unknown_flag(dbenv, "DB->put", op));
327 }
328
329 /* Add the data. */
330 if (bigdata) {
331 /*
332 * We do not have to handle deleted (BI_DELETED) records
333 * in this case; the actual records should never be created.
334 */
335 DB_ASSERT(!LF_ISSET(BI_DELETED));
336 if ((ret = __bam_ovput(dbc,
337 B_OVERFLOW, PGNO_INVALID, h, indx, data)) != 0)
338 return (ret);
339 } else {
340 if (LF_ISSET(BI_DELETED)) {
341 B_TSET(bk_tmp.type, B_KEYDATA, 1);
342 bk_tmp.len = data->size;
343 bk_hdr.data = &bk_tmp;
344 bk_hdr.size = SSZA(BKEYDATA, data);
345 ret = __db_pitem(dbc, h, indx,
346 BKEYDATA_SIZE(data->size), &bk_hdr, data);
347 } else if (replace)
348 ret = __bam_ritem(dbc, h, indx, data);
349 else
350 ret = __db_pitem(dbc, h, indx,
351 BKEYDATA_SIZE(data->size), NULL, data);
352 if (ret != 0)
353 return (ret);
354 }
355 if ((ret = __memp_fset(mpf, h, DB_MPOOL_DIRTY)) != 0)
356 return (ret);
357
358 /*
359 * Re-position the cursors if necessary and reset the current cursor
360 * to point to the new item.
361 */
362 if (op != DB_CURRENT) {
363 if ((ret = __bam_ca_di(dbc, PGNO(h), indx, 1)) != 0)
364 return (ret);
365 cp->indx = TYPE(h) == P_LBTREE ? indx - O_INDX : indx;
366 }
367
368 /*
369 * If we've changed the record count, update the tree. There's no
370 * need to adjust the count if the operation not performed on the
371 * current record or when the current record was previously deleted.
372 */
373 if (F_ISSET(cp, C_RECNUM) && (op != DB_CURRENT || was_deleted))
374 if ((ret = __bam_adjust(dbc, 1)) != 0)
375 return (ret);
376
377 /*
378 * If a Btree leaf page is at least 50% full and we may have added or
379 * modified a duplicate data item, see if the set of duplicates takes
380 * up at least 25% of the space on the page. If it does, move it onto
381 * its own page.
382 */
383 if (dupadjust && P_FREESPACE(dbp, h) <= dbp->pgsize / 2) {
384 if ((ret = __bam_dup_convert(dbc, h, indx - O_INDX)) != 0)
385 return (ret);
386 }
387
388 /* If we've modified a recno file, set the flag. */
389 if (dbc->dbtype == DB_RECNO)
390 t->re_modified = 1;
391
392 return (ret);
393 }
394
395 /*
396 * __bam_partsize --
397 * Figure out how much space a partial data item is in total.
398 */
399 static u_int32_t
400 __bam_partsize(dbp, op, data, h, indx)
401 DB *dbp;
402 u_int32_t op, indx;
403 DBT *data;
404 PAGE *h;
405 {
406 BKEYDATA *bk;
407 u_int32_t nbytes;
408
409 /*
410 * If the record doesn't already exist, it's simply the data we're
411 * provided.
412 */
413 if (op != DB_CURRENT)
414 return (data->doff + data->size);
415
416 /*
417 * Otherwise, it's the data provided plus any already existing data
418 * that we're not replacing.
419 */
420 bk = GET_BKEYDATA(dbp, h, indx + (TYPE(h) == P_LBTREE ? O_INDX : 0));
421 nbytes =
422 B_TYPE(bk->type) == B_OVERFLOW ? ((BOVERFLOW *)bk)->tlen : bk->len;
423
424 return (__db_partsize(nbytes, data));
425 }
426
427 /*
428 * __bam_build --
429 * Build the real record for a partial put, or short fixed-length record.
430 */
431 static int
432 __bam_build(dbc, op, dbt, h, indx, nbytes)
433 DBC *dbc;
434 u_int32_t op, indx, nbytes;
435 DBT *dbt;
436 PAGE *h;
437 {
438 BKEYDATA *bk, tbk;
439 BOVERFLOW *bo;
440 BTREE *t;
441 DB *dbp;
442 DBT copy, *rdata;
443 u_int32_t len, tlen;
444 u_int8_t *p;
445 int ret;
446
447 COMPQUIET(bo, NULL);
448
449 dbp = dbc->dbp;
450 t = dbp->bt_internal;
451
452 /* We use the record data return memory, it's only a short-term use. */
453 rdata = &dbc->my_rdata;
454 if (rdata->ulen < nbytes) {
455 if ((ret = __os_realloc(dbp->dbenv,
456 nbytes, &rdata->data)) != 0) {
457 rdata->ulen = 0;
458 rdata->data = NULL;
459 return (ret);
460 }
461 rdata->ulen = nbytes;
462 }
463
464 /*
465 * We use nul or pad bytes for any part of the record that isn't
466 * specified; get it over with.
467 */
468 memset(rdata->data,
469 F_ISSET(dbp, DB_AM_FIXEDLEN) ? t->re_pad : 0, nbytes);
470
471 /*
472 * In the next clauses, we need to do three things: a) set p to point
473 * to the place at which to copy the user's data, b) set tlen to the
474 * total length of the record, not including the bytes contributed by
475 * the user, and c) copy any valid data from an existing record. If
476 * it's not a partial put (this code is called for both partial puts
477 * and fixed-length record padding) or it's a new key, we can cut to
478 * the chase.
479 */
480 if (!F_ISSET(dbt, DB_DBT_PARTIAL) || op != DB_CURRENT) {
481 p = (u_int8_t *)rdata->data + dbt->doff;
482 tlen = dbt->doff;
483 goto user_copy;
484 }
485
486 /* Find the current record. */
487 if (indx < NUM_ENT(h)) {
488 bk = GET_BKEYDATA(dbp, h, indx + (TYPE(h) == P_LBTREE ?
489 O_INDX : 0));
490 bo = (BOVERFLOW *)bk;
491 } else {
492 bk = &tbk;
493 B_TSET(bk->type, B_KEYDATA, 0);
494 bk->len = 0;
495 }
496 if (B_TYPE(bk->type) == B_OVERFLOW) {
497 /*
498 * In the case of an overflow record, we shift things around
499 * in the current record rather than allocate a separate copy.
500 */
501 memset(&copy, 0, sizeof(copy));
502 if ((ret = __db_goff(dbp, &copy, bo->tlen,
503 bo->pgno, &rdata->data, &rdata->ulen)) != 0)
504 return (ret);
505
506 /* Skip any leading data from the original record. */
507 tlen = dbt->doff;
508 p = (u_int8_t *)rdata->data + dbt->doff;
509
510 /*
511 * Copy in any trailing data from the original record.
512 *
513 * If the original record was larger than the original offset
514 * plus the bytes being deleted, there is trailing data in the
515 * original record we need to preserve. If we aren't deleting
516 * the same number of bytes as we're inserting, copy it up or
517 * down, into place.
518 *
519 * Use memmove(), the regions may overlap.
520 */
521 if (bo->tlen > dbt->doff + dbt->dlen) {
522 len = bo->tlen - (dbt->doff + dbt->dlen);
523 if (dbt->dlen != dbt->size)
524 memmove(p + dbt->size, p + dbt->dlen, len);
525 tlen += len;
526 }
527 } else {
528 /* Copy in any leading data from the original record. */
529 memcpy(rdata->data,
530 bk->data, dbt->doff > bk->len ? bk->len : dbt->doff);
531 tlen = dbt->doff;
532 p = (u_int8_t *)rdata->data + dbt->doff;
533
534 /* Copy in any trailing data from the original record. */
535 len = dbt->doff + dbt->dlen;
536 if (bk->len > len) {
537 memcpy(p + dbt->size, bk->data + len, bk->len - len);
538 tlen += bk->len - len;
539 }
540 }
541
542 user_copy:
543 /*
544 * Copy in the application provided data -- p and tlen must have been
545 * initialized above.
546 */
547 memcpy(p, dbt->data, dbt->size);
548 tlen += dbt->size;
549
550 /* Set the DBT to reference our new record. */
551 rdata->size = F_ISSET(dbp, DB_AM_FIXEDLEN) ? t->re_len : tlen;
552 rdata->dlen = 0;
553 rdata->doff = 0;
554 rdata->flags = 0;
555 *dbt = *rdata;
556 return (0);
557 }
558
559 /*
560 * __bam_ritem --
561 * Replace an item on a page.
562 *
563 * PUBLIC: int __bam_ritem __P((DBC *, PAGE *, u_int32_t, DBT *));
564 */
565 int
566 __bam_ritem(dbc, h, indx, data)
567 DBC *dbc;
568 PAGE *h;
569 u_int32_t indx;
570 DBT *data;
571 {
572 BKEYDATA *bk;
573 DB *dbp;
574 DBT orig, repl;
575 db_indx_t cnt, lo, ln, min, off, prefix, suffix;
576 int32_t nbytes;
577 int ret;
578 db_indx_t *inp;
579 u_int8_t *p, *t;
580
581 dbp = dbc->dbp;
582
583 /*
584 * Replace a single item onto a page. The logic figuring out where
585 * to insert and whether it fits is handled in the caller. All we do
586 * here is manage the page shuffling.
587 */
588 bk = GET_BKEYDATA(dbp, h, indx);
589
590 /* Log the change. */
591 if (DBC_LOGGING(dbc)) {
592 /*
593 * We might as well check to see if the two data items share
594 * a common prefix and suffix -- it can save us a lot of log
595 * message if they're large.
596 */
597 min = data->size < bk->len ? data->size : bk->len;
598 for (prefix = 0,
599 p = bk->data, t = data->data;
600 prefix < min && *p == *t; ++prefix, ++p, ++t)
601 ;
602
603 min -= prefix;
604 for (suffix = 0,
605 p = (u_int8_t *)bk->data + bk->len - 1,
606 t = (u_int8_t *)data->data + data->size - 1;
607 suffix < min && *p == *t; ++suffix, --p, --t)
608 ;
609
610 /* We only log the parts of the keys that have changed. */
611 orig.data = (u_int8_t *)bk->data + prefix;
612 orig.size = bk->len - (prefix + suffix);
613 repl.data = (u_int8_t *)data->data + prefix;
614 repl.size = data->size - (prefix + suffix);
615 if ((ret = __bam_repl_log(dbp, dbc->txn, &LSN(h), 0, PGNO(h),
616 &LSN(h), (u_int32_t)indx, (u_int32_t)B_DISSET(bk->type),
617 &orig, &repl, (u_int32_t)prefix, (u_int32_t)suffix)) != 0)
618 return (ret);
619 } else
620 LSN_NOT_LOGGED(LSN(h));
621
622 /*
623 * Set references to the first in-use byte on the page and the
624 * first byte of the item being replaced.
625 */
626 inp = P_INP(dbp, h);
627 p = (u_int8_t *)h + HOFFSET(h);
628 t = (u_int8_t *)bk;
629
630 /*
631 * If the entry is growing in size, shift the beginning of the data
632 * part of the page down. If the entry is shrinking in size, shift
633 * the beginning of the data part of the page up. Use memmove(3),
634 * the regions overlap.
635 */
636 lo = BKEYDATA_SIZE(bk->len);
637 ln = (db_indx_t)BKEYDATA_SIZE(data->size);
638 if (lo != ln) {
639 nbytes = lo - ln; /* Signed difference. */
640 if (p == t) /* First index is fast. */
641 inp[indx] += nbytes;
642 else { /* Else, shift the page. */
643 memmove(p + nbytes, p, (size_t)(t - p));
644
645 /* Adjust the indices' offsets. */
646 off = inp[indx];
647 for (cnt = 0; cnt < NUM_ENT(h); ++cnt)
648 if (inp[cnt] <= off)
649 inp[cnt] += nbytes;
650 }
651
652 /* Clean up the page and adjust the item's reference. */
653 HOFFSET(h) += nbytes;
654 t += nbytes;
655 }
656
657 /* Copy the new item onto the page. */
658 bk = (BKEYDATA *)t;
659 B_TSET(bk->type, B_KEYDATA, 0);
660 bk->len = data->size;
661 memcpy(bk->data, data->data, data->size);
662
663 return (0);
664 }
665
666 /*
667 * __bam_dup_convert --
668 * Check to see if the duplicate set at indx should have its own page.
669 * If it should, create it.
670 */
671 static int
672 __bam_dup_convert(dbc, h, indx)
673 DBC *dbc;
674 PAGE *h;
675 u_int32_t indx;
676 {
677 BKEYDATA *bk;
678 DB *dbp;
679 DBT hdr;
680 DB_MPOOLFILE *mpf;
681 PAGE *dp;
682 db_indx_t cnt, cpindx, dindx, first, *inp, sz;
683 int ret;
684
685 dbp = dbc->dbp;
686 mpf = dbp->mpf;
687 inp = P_INP(dbp, h);
688
689 /*
690 * Count the duplicate records and calculate how much room they're
691 * using on the page.
692 */
693 while (indx > 0 && inp[indx] == inp[indx - P_INDX])
694 indx -= P_INDX;
695
696 /* Count the key once. */
697 bk = GET_BKEYDATA(dbp, h, indx);
698 sz = B_TYPE(bk->type) == B_KEYDATA ?
699 BKEYDATA_PSIZE(bk->len) : BOVERFLOW_PSIZE;
700
701 /* Sum up all the data items. */
702 for (cnt = 0, first = indx;
703 indx < NUM_ENT(h) && inp[first] == inp[indx];
704 ++cnt, indx += P_INDX) {
705 bk = GET_BKEYDATA(dbp, h, indx + O_INDX);
706 sz += B_TYPE(bk->type) == B_KEYDATA ?
707 BKEYDATA_PSIZE(bk->len) : BOVERFLOW_PSIZE;
708 }
709
710 /*
711 * We have to do these checks when the user is replacing the cursor's
712 * data item -- if the application replaces a duplicate item with a
713 * larger data item, it can increase the amount of space used by the
714 * duplicates, requiring this check. But that means we may have done
715 * this check when it wasn't a duplicate item after all.
716 */
717 if (cnt == 1)
718 return (0);
719
720 /*
721 * If this set of duplicates is using more than 25% of the page, move
722 * them off. The choice of 25% is a WAG, but the value must be small
723 * enough that we can always split a page without putting duplicates
724 * on two different pages.
725 */
726 if (sz < dbp->pgsize / 4)
727 return (0);
728
729 /* Get a new page. */
730 if ((ret = __db_new(dbc,
731 dbp->dup_compare == NULL ? P_LRECNO : P_LDUP, &dp)) != 0)
732 return (ret);
733 P_INIT(dp, dbp->pgsize, dp->pgno,
734 PGNO_INVALID, PGNO_INVALID, LEAFLEVEL, TYPE(dp));
735
736 /*
737 * Move this set of duplicates off the page. First points to the first
738 * key of the first duplicate key/data pair, cnt is the number of pairs
739 * we're dealing with.
740 */
741 memset(&hdr, 0, sizeof(hdr));
742 dindx = first;
743 indx = first;
744 cpindx = 0;
745 do {
746 /* Move cursors referencing the old entry to the new entry. */
747 if ((ret = __bam_ca_dup(dbc, first,
748 PGNO(h), indx, PGNO(dp), cpindx)) != 0)
749 goto err;
750
751 /*
752 * Copy the entry to the new page. If the off-duplicate page
753 * If the off-duplicate page is a Btree page (i.e. dup_compare
754 * will be non-NULL, we use Btree pages for sorted dups,
755 * and Recno pages for unsorted dups), move all entries
756 * normally, even deleted ones. If it's a Recno page,
757 * deleted entries are discarded (if the deleted entry is
758 * overflow, then free up those pages).
759 */
760 bk = GET_BKEYDATA(dbp, h, dindx + 1);
761 hdr.data = bk;
762 hdr.size = B_TYPE(bk->type) == B_KEYDATA ?
763 BKEYDATA_SIZE(bk->len) : BOVERFLOW_SIZE;
764 if (dbp->dup_compare == NULL && B_DISSET(bk->type)) {
765 /*
766 * Unsorted dups, i.e. recno page, and we have
767 * a deleted entry, don't move it, but if it was
768 * an overflow entry, we need to free those pages.
769 */
770 if (B_TYPE(bk->type) == B_OVERFLOW &&
771 (ret = __db_doff(dbc,
772 (GET_BOVERFLOW(dbp, h, dindx + 1))->pgno)) != 0)
773 goto err;
774 } else {
775 if ((ret = __db_pitem(
776 dbc, dp, cpindx, hdr.size, &hdr, NULL)) != 0)
777 goto err;
778 ++cpindx;
779 }
780 /* Delete all but the last reference to the key. */
781 if (cnt != 1) {
782 if ((ret = __bam_adjindx(dbc,
783 h, dindx, first + 1, 0)) != 0)
784 goto err;
785 } else
786 dindx++;
787
788 /* Delete the data item. */
789 if ((ret = __db_ditem(dbc, h, dindx, hdr.size)) != 0)
790 goto err;
791 indx += P_INDX;
792 } while (--cnt);
793
794 /* Put in a new data item that points to the duplicates page. */
795 if ((ret = __bam_ovput(dbc,
796 B_DUPLICATE, dp->pgno, h, first + 1, NULL)) != 0)
797 goto err;
798
799 /* Adjust cursors for all the above movements. */
800 if ((ret = __bam_ca_di(dbc,
801 PGNO(h), first + P_INDX, (int)(first + P_INDX - indx))) != 0)
802 goto err;
803
804 return (__memp_fput(mpf, dp, DB_MPOOL_DIRTY));
805
806 err: (void)__memp_fput(mpf, dp, 0);
807 return (ret);
808 }
809
810 /*
811 * __bam_ovput --
812 * Build an item for an off-page duplicates page or overflow page and
813 * insert it on the page.
814 */
815 static int
816 __bam_ovput(dbc, type, pgno, h, indx, item)
817 DBC *dbc;
818 u_int32_t type, indx;
819 db_pgno_t pgno;
820 PAGE *h;
821 DBT *item;
822 {
823 BOVERFLOW bo;
824 DBT hdr;
825 int ret;
826
827 UMRW_SET(bo.unused1);
828 B_TSET(bo.type, type, 0);
829 UMRW_SET(bo.unused2);
830
831 /*
832 * If we're creating an overflow item, do so and acquire the page
833 * number for it. If we're creating an off-page duplicates tree,
834 * we are giving the page number as an argument.
835 */
836 if (type == B_OVERFLOW) {
837 if ((ret = __db_poff(dbc, item, &bo.pgno)) != 0)
838 return (ret);
839 bo.tlen = item->size;
840 } else {
841 bo.pgno = pgno;
842 bo.tlen = 0;
843 }
844
845 /* Store the new record on the page. */
846 memset(&hdr, 0, sizeof(hdr));
847 hdr.data = &bo;
848 hdr.size = BOVERFLOW_SIZE;
849 return (__db_pitem(dbc, h, indx, BOVERFLOW_SIZE, &hdr, NULL));
850 }

webmaster AT resiprocate DOT org
ViewVC Help
Powered by ViewVC 1.1.27