Import from 1.9a8 tarball
[mozilla-nss.git] / dbm / src / h_bigkey.c
blob5cf7f15db153f07ff9dd062ff4c0efb3d2a9f951
1 /*-
2 * Copyright (c) 1990, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Margo Seltzer.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. ***REMOVED*** - see
17 * ftp://ftp.cs.berkeley.edu/pub/4bsd/README.Impt.License.Change
18 * 4. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
35 #if defined(LIBC_SCCS) && !defined(lint)
36 static char sccsid[] = "@(#)hash_bigkey.c 8.3 (Berkeley) 5/31/94";
37 #endif /* LIBC_SCCS and not lint */
39 #include "watcomfx.h"
42 * PACKAGE: hash
43 * DESCRIPTION:
44 * Big key/data handling for the hashing package.
46 * ROUTINES:
47 * External
48 * __big_keydata
49 * __big_split
50 * __big_insert
51 * __big_return
52 * __big_delete
53 * __find_last_page
54 * Internal
55 * collect_key
56 * collect_data
59 #if !defined(_WIN32) && !defined(_WINDOWS) && !defined(macintosh) && !defined(XP_OS2_VACPP)
60 #include <sys/param.h>
61 #endif
63 #include <errno.h>
64 #include <stdio.h>
65 #include <stdlib.h>
66 #include <string.h>
68 #ifdef DEBUG
69 #include <assert.h>
70 #endif
72 #include "mcom_db.h"
73 #include "hash.h"
74 #include "page.h"
75 /* #include "extern.h" */
77 static int collect_key __P((HTAB *, BUFHEAD *, int, DBT *, int));
78 static int collect_data __P((HTAB *, BUFHEAD *, int, int));
81 * Big_insert
83 * You need to do an insert and the key/data pair is too big
85 * Returns:
86 * 0 ==> OK
87 *-1 ==> ERROR
89 extern int
90 __big_insert(HTAB *hashp, BUFHEAD *bufp, const DBT *key, const DBT *val)
92 register uint16 *p;
93 uint key_size, n, val_size;
94 uint16 space, move_bytes, off;
95 char *cp, *key_data, *val_data;
97 cp = bufp->page; /* Character pointer of p. */
98 p = (uint16 *)cp;
100 key_data = (char *)key->data;
101 key_size = key->size;
102 val_data = (char *)val->data;
103 val_size = val->size;
105 /* First move the Key */
106 for (space = FREESPACE(p) - BIGOVERHEAD; key_size;
107 space = FREESPACE(p) - BIGOVERHEAD) {
108 move_bytes = PR_MIN(space, key_size);
109 off = OFFSET(p) - move_bytes;
110 memmove(cp + off, key_data, move_bytes);
111 key_size -= move_bytes;
112 key_data += move_bytes;
113 n = p[0];
114 p[++n] = off;
115 p[0] = ++n;
116 FREESPACE(p) = off - PAGE_META(n);
117 OFFSET(p) = off;
118 p[n] = PARTIAL_KEY;
119 bufp = __add_ovflpage(hashp, bufp);
120 if (!bufp)
121 return (-1);
122 n = p[0];
123 if (!key_size) {
124 if (FREESPACE(p)) {
125 move_bytes = PR_MIN(FREESPACE(p), val_size);
126 off = OFFSET(p) - move_bytes;
127 p[n] = off;
128 memmove(cp + off, val_data, move_bytes);
129 val_data += move_bytes;
130 val_size -= move_bytes;
131 p[n - 2] = FULL_KEY_DATA;
132 FREESPACE(p) = FREESPACE(p) - move_bytes;
133 OFFSET(p) = off;
134 } else
135 p[n - 2] = FULL_KEY;
137 p = (uint16 *)bufp->page;
138 cp = bufp->page;
139 bufp->flags |= BUF_MOD;
142 /* Now move the data */
143 for (space = FREESPACE(p) - BIGOVERHEAD; val_size;
144 space = FREESPACE(p) - BIGOVERHEAD) {
145 move_bytes = PR_MIN(space, val_size);
147 * Here's the hack to make sure that if the data ends on the
148 * same page as the key ends, FREESPACE is at least one.
150 if (space == val_size && val_size == val->size)
151 move_bytes--;
152 off = OFFSET(p) - move_bytes;
153 memmove(cp + off, val_data, move_bytes);
154 val_size -= move_bytes;
155 val_data += move_bytes;
156 n = p[0];
157 p[++n] = off;
158 p[0] = ++n;
159 FREESPACE(p) = off - PAGE_META(n);
160 OFFSET(p) = off;
161 if (val_size) {
162 p[n] = FULL_KEY;
163 bufp = __add_ovflpage(hashp, bufp);
164 if (!bufp)
165 return (-1);
166 cp = bufp->page;
167 p = (uint16 *)cp;
168 } else
169 p[n] = FULL_KEY_DATA;
170 bufp->flags |= BUF_MOD;
172 return (0);
176 * Called when bufp's page contains a partial key (index should be 1)
178 * All pages in the big key/data pair except bufp are freed. We cannot
179 * free bufp because the page pointing to it is lost and we can't get rid
180 * of its pointer.
182 * Returns:
183 * 0 => OK
184 *-1 => ERROR
186 extern int
187 __big_delete(HTAB *hashp, BUFHEAD *bufp)
189 register BUFHEAD *last_bfp, *rbufp;
190 uint16 *bp, pageno;
191 int key_done, n;
193 rbufp = bufp;
194 last_bfp = NULL;
195 bp = (uint16 *)bufp->page;
196 pageno = 0;
197 key_done = 0;
199 while (!key_done || (bp[2] != FULL_KEY_DATA)) {
200 if (bp[2] == FULL_KEY || bp[2] == FULL_KEY_DATA)
201 key_done = 1;
204 * If there is freespace left on a FULL_KEY_DATA page, then
205 * the data is short and fits entirely on this page, and this
206 * is the last page.
208 if (bp[2] == FULL_KEY_DATA && FREESPACE(bp))
209 break;
210 pageno = bp[bp[0] - 1];
211 rbufp->flags |= BUF_MOD;
212 rbufp = __get_buf(hashp, pageno, rbufp, 0);
213 if (last_bfp)
214 __free_ovflpage(hashp, last_bfp);
215 last_bfp = rbufp;
216 if (!rbufp)
217 return (-1); /* Error. */
218 bp = (uint16 *)rbufp->page;
222 * If we get here then rbufp points to the last page of the big
223 * key/data pair. Bufp points to the first one -- it should now be
224 * empty pointing to the next page after this pair. Can't free it
225 * because we don't have the page pointing to it.
228 /* This is information from the last page of the pair. */
229 n = bp[0];
230 pageno = bp[n - 1];
232 /* Now, bp is the first page of the pair. */
233 bp = (uint16 *)bufp->page;
234 if (n > 2) {
235 /* There is an overflow page. */
236 bp[1] = pageno;
237 bp[2] = OVFLPAGE;
238 bufp->ovfl = rbufp->ovfl;
239 } else
240 /* This is the last page. */
241 bufp->ovfl = NULL;
242 n -= 2;
243 bp[0] = n;
244 FREESPACE(bp) = hashp->BSIZE - PAGE_META(n);
245 OFFSET(bp) = hashp->BSIZE - 1;
247 bufp->flags |= BUF_MOD;
248 if (rbufp)
249 __free_ovflpage(hashp, rbufp);
250 if (last_bfp != rbufp)
251 __free_ovflpage(hashp, last_bfp);
253 hashp->NKEYS--;
254 return (0);
257 * Returns:
258 * 0 = key not found
259 * -1 = get next overflow page
260 * -2 means key not found and this is big key/data
261 * -3 error
263 extern int
264 __find_bigpair(HTAB *hashp, BUFHEAD *bufp, int ndx, char *key, int size)
266 register uint16 *bp;
267 register char *p;
268 int ksize;
269 uint16 bytes;
270 char *kkey;
272 bp = (uint16 *)bufp->page;
273 p = bufp->page;
274 ksize = size;
275 kkey = key;
277 for (bytes = hashp->BSIZE - bp[ndx];
278 bytes <= size && bp[ndx + 1] == PARTIAL_KEY;
279 bytes = hashp->BSIZE - bp[ndx]) {
280 if (memcmp(p + bp[ndx], kkey, bytes))
281 return (-2);
282 kkey += bytes;
283 ksize -= bytes;
284 bufp = __get_buf(hashp, bp[ndx + 2], bufp, 0);
285 if (!bufp)
286 return (-3);
287 p = bufp->page;
288 bp = (uint16 *)p;
289 ndx = 1;
292 if (bytes != ksize || memcmp(p + bp[ndx], kkey, bytes)) {
293 #ifdef HASH_STATISTICS
294 ++hash_collisions;
295 #endif
296 return (-2);
297 } else
298 return (ndx);
302 * Given the buffer pointer of the first overflow page of a big pair,
303 * find the end of the big pair
305 * This will set bpp to the buffer header of the last page of the big pair.
306 * It will return the pageno of the overflow page following the last page
307 * of the pair; 0 if there isn't any (i.e. big pair is the last key in the
308 * bucket)
310 extern uint16
311 __find_last_page(HTAB *hashp, BUFHEAD **bpp)
313 BUFHEAD *bufp;
314 uint16 *bp, pageno;
315 uint n;
317 bufp = *bpp;
318 bp = (uint16 *)bufp->page;
319 for (;;) {
320 n = bp[0];
323 * This is the last page if: the tag is FULL_KEY_DATA and
324 * either only 2 entries OVFLPAGE marker is explicit there
325 * is freespace on the page.
327 if (bp[2] == FULL_KEY_DATA &&
328 ((n == 2) || (bp[n] == OVFLPAGE) || (FREESPACE(bp))))
329 break;
331 /* LJM bound the size of n to reasonable limits
333 if(n > hashp->BSIZE/sizeof(uint16))
334 return(0);
336 pageno = bp[n - 1];
337 bufp = __get_buf(hashp, pageno, bufp, 0);
338 if (!bufp)
339 return (0); /* Need to indicate an error! */
340 bp = (uint16 *)bufp->page;
343 *bpp = bufp;
344 if (bp[0] > 2)
345 return (bp[3]);
346 else
347 return (0);
351 * Return the data for the key/data pair that begins on this page at this
352 * index (index should always be 1).
354 extern int
355 __big_return(
356 HTAB *hashp,
357 BUFHEAD *bufp,
358 int ndx,
359 DBT *val,
360 int set_current)
362 BUFHEAD *save_p;
363 uint16 *bp, len, off, save_addr;
364 char *tp;
365 int save_flags;
367 bp = (uint16 *)bufp->page;
368 while (bp[ndx + 1] == PARTIAL_KEY) {
369 bufp = __get_buf(hashp, bp[bp[0] - 1], bufp, 0);
370 if (!bufp)
371 return (-1);
372 bp = (uint16 *)bufp->page;
373 ndx = 1;
376 if (bp[ndx + 1] == FULL_KEY) {
377 bufp = __get_buf(hashp, bp[bp[0] - 1], bufp, 0);
378 if (!bufp)
379 return (-1);
380 bp = (uint16 *)bufp->page;
381 save_p = bufp;
382 save_addr = save_p->addr;
383 off = bp[1];
384 len = 0;
385 } else
386 if (!FREESPACE(bp)) {
388 * This is a hack. We can't distinguish between
389 * FULL_KEY_DATA that contains complete data or
390 * incomplete data, so we require that if the data
391 * is complete, there is at least 1 byte of free
392 * space left.
394 off = bp[bp[0]];
395 len = bp[1] - off;
396 save_p = bufp;
397 save_addr = bufp->addr;
398 bufp = __get_buf(hashp, bp[bp[0] - 1], bufp, 0);
399 if (!bufp)
400 return (-1);
401 bp = (uint16 *)bufp->page;
402 } else {
403 /* The data is all on one page. */
404 tp = (char *)bp;
405 off = bp[bp[0]];
406 val->data = (uint8 *)tp + off;
407 val->size = bp[1] - off;
408 if (set_current) {
409 if (bp[0] == 2) { /* No more buckets in
410 * chain */
411 hashp->cpage = NULL;
412 hashp->cbucket++;
413 hashp->cndx = 1;
414 } else {
415 hashp->cpage = __get_buf(hashp,
416 bp[bp[0] - 1], bufp, 0);
417 if (!hashp->cpage)
418 return (-1);
419 hashp->cndx = 1;
420 if (!((uint16 *)
421 hashp->cpage->page)[0]) {
422 hashp->cbucket++;
423 hashp->cpage = NULL;
427 return (0);
430 /* pin our saved buf so that we don't lose if
431 * we run out of buffers */
432 save_flags = save_p->flags;
433 save_p->flags |= BUF_PIN;
434 val->size = collect_data(hashp, bufp, (int)len, set_current);
435 save_p->flags = save_flags;
436 if (val->size == (size_t)-1)
437 return (-1);
438 if (save_p->addr != save_addr) {
439 /* We are pretty short on buffers. */
440 errno = EINVAL; /* OUT OF BUFFERS */
441 return (-1);
443 memmove(hashp->tmp_buf, (save_p->page) + off, len);
444 val->data = (uint8 *)hashp->tmp_buf;
445 return (0);
450 * Count how big the total datasize is by looping through the pages. Then
451 * allocate a buffer and copy the data in the second loop. NOTE: Our caller
452 * may already have a bp which it is holding onto. The caller is
453 * responsible for copying that bp into our temp buffer. 'len' is how much
454 * space to reserve for that buffer.
456 static int
457 collect_data(
458 HTAB *hashp,
459 BUFHEAD *bufp,
460 int len, int set)
462 register uint16 *bp;
463 BUFHEAD *save_bufp;
464 int save_flags;
465 int mylen, totlen;
468 * save the input buf head because we need to walk the list twice.
469 * pin it to make sure it doesn't leave the buffer pool.
470 * This has the effect of growing the buffer pool if necessary.
472 save_bufp = bufp;
473 save_flags = save_bufp->flags;
474 save_bufp->flags |= BUF_PIN;
476 /* read the length of the buffer */
477 for (totlen = len; bufp ; bufp = __get_buf(hashp, bp[bp[0]-1], bufp, 0)) {
478 bp = (uint16 *)bufp->page;
479 mylen = hashp->BSIZE - bp[1];
481 /* if mylen ever goes negative it means that the
482 * page is screwed up.
484 if (mylen < 0) {
485 save_bufp->flags = save_flags;
486 return (-1);
488 totlen += mylen;
489 if (bp[2] == FULL_KEY_DATA) { /* End of Data */
490 break;
494 if (!bufp) {
495 save_bufp->flags = save_flags;
496 return (-1);
499 /* allocate a temp buf */
500 if (hashp->tmp_buf)
501 free(hashp->tmp_buf);
502 if ((hashp->tmp_buf = (char *)malloc((size_t)totlen)) == NULL) {
503 save_bufp->flags = save_flags;
504 return (-1);
507 /* copy the buffers back into temp buf */
508 for (bufp = save_bufp; bufp ;
509 bufp = __get_buf(hashp, bp[bp[0]-1], bufp, 0)) {
510 bp = (uint16 *)bufp->page;
511 mylen = hashp->BSIZE - bp[1];
512 memmove(&hashp->tmp_buf[len], (bufp->page) + bp[1], (size_t)mylen);
513 len += mylen;
514 if (bp[2] == FULL_KEY_DATA) {
515 break;
519 /* 'clear' the pin flags */
520 save_bufp->flags = save_flags;
522 /* update the database cursor */
523 if (set) {
524 hashp->cndx = 1;
525 if (bp[0] == 2) { /* No more buckets in chain */
526 hashp->cpage = NULL;
527 hashp->cbucket++;
528 } else {
529 hashp->cpage = __get_buf(hashp, bp[bp[0] - 1], bufp, 0);
530 if (!hashp->cpage)
531 return (-1);
532 else if (!((uint16 *)hashp->cpage->page)[0]) {
533 hashp->cbucket++;
534 hashp->cpage = NULL;
538 return (totlen);
542 * Fill in the key and data for this big pair.
544 extern int
545 __big_keydata(
546 HTAB *hashp,
547 BUFHEAD *bufp,
548 DBT *key, DBT *val,
549 int set)
551 key->size = collect_key(hashp, bufp, 0, val, set);
552 if (key->size == (size_t)-1)
553 return (-1);
554 key->data = (uint8 *)hashp->tmp_key;
555 return (0);
559 * Count how big the total key size is by recursing through the pages. Then
560 * collect the data, allocate a buffer and copy the key as you recurse up.
562 static int
563 collect_key(
564 HTAB *hashp,
565 BUFHEAD *bufp,
566 int len,
567 DBT *val,
568 int set)
570 BUFHEAD *xbp;
571 char *p;
572 int mylen, totlen;
573 uint16 *bp, save_addr;
575 p = bufp->page;
576 bp = (uint16 *)p;
577 mylen = hashp->BSIZE - bp[1];
579 save_addr = bufp->addr;
580 totlen = len + mylen;
581 if (bp[2] == FULL_KEY || bp[2] == FULL_KEY_DATA) { /* End of Key. */
582 if (hashp->tmp_key != NULL)
583 free(hashp->tmp_key);
584 if ((hashp->tmp_key = (char *)malloc((size_t)totlen)) == NULL)
585 return (-1);
586 if (__big_return(hashp, bufp, 1, val, set))
587 return (-1);
588 } else {
589 xbp = __get_buf(hashp, bp[bp[0] - 1], bufp, 0);
590 if (!xbp || ((totlen =
591 collect_key(hashp, xbp, totlen, val, set)) < 1))
592 return (-1);
594 if (bufp->addr != save_addr) {
595 errno = EINVAL; /* MIS -- OUT OF BUFFERS */
596 return (-1);
598 memmove(&hashp->tmp_key[len], (bufp->page) + bp[1], (size_t)mylen);
599 return (totlen);
603 * Returns:
604 * 0 => OK
605 * -1 => error
607 extern int
608 __big_split(
609 HTAB *hashp,
610 BUFHEAD *op, /* Pointer to where to put keys that go in old bucket */
611 BUFHEAD *np, /* Pointer to new bucket page */
612 /* Pointer to first page containing the big key/data */
613 BUFHEAD *big_keyp,
614 uint32 addr, /* Address of big_keyp */
615 uint32 obucket,/* Old Bucket */
616 SPLIT_RETURN *ret)
618 register BUFHEAD *tmpp;
619 register uint16 *tp;
620 BUFHEAD *bp;
621 DBT key, val;
622 uint32 change;
623 uint16 free_space, n, off;
625 bp = big_keyp;
627 /* Now figure out where the big key/data goes */
628 if (__big_keydata(hashp, big_keyp, &key, &val, 0))
629 return (-1);
630 change = (__call_hash(hashp,(char*) key.data, key.size) != obucket);
632 if ((ret->next_addr = __find_last_page(hashp, &big_keyp))) {
633 if (!(ret->nextp =
634 __get_buf(hashp, ret->next_addr, big_keyp, 0)))
635 return (-1);;
636 } else
637 ret->nextp = NULL;
639 /* Now make one of np/op point to the big key/data pair */
640 #ifdef DEBUG
641 assert(np->ovfl == NULL);
642 #endif
643 if (change)
644 tmpp = np;
645 else
646 tmpp = op;
648 tmpp->flags |= BUF_MOD;
649 #ifdef DEBUG1
650 (void)fprintf(stderr,
651 "BIG_SPLIT: %d->ovfl was %d is now %d\n", tmpp->addr,
652 (tmpp->ovfl ? tmpp->ovfl->addr : 0), (bp ? bp->addr : 0));
653 #endif
654 tmpp->ovfl = bp; /* one of op/np point to big_keyp */
655 tp = (uint16 *)tmpp->page;
658 #if 0 /* this get's tripped on database corrupted error */
659 assert(FREESPACE(tp) >= OVFLSIZE);
660 #endif
661 if(FREESPACE(tp) < OVFLSIZE)
662 return(DATABASE_CORRUPTED_ERROR);
664 n = tp[0];
665 off = OFFSET(tp);
666 free_space = FREESPACE(tp);
667 tp[++n] = (uint16)addr;
668 tp[++n] = OVFLPAGE;
669 tp[0] = n;
670 OFFSET(tp) = off;
671 FREESPACE(tp) = free_space - OVFLSIZE;
674 * Finally, set the new and old return values. BIG_KEYP contains a
675 * pointer to the last page of the big key_data pair. Make sure that
676 * big_keyp has no following page (2 elements) or create an empty
677 * following page.
680 ret->newp = np;
681 ret->oldp = op;
683 tp = (uint16 *)big_keyp->page;
684 big_keyp->flags |= BUF_MOD;
685 if (tp[0] > 2) {
687 * There may be either one or two offsets on this page. If
688 * there is one, then the overflow page is linked on normally
689 * and tp[4] is OVFLPAGE. If there are two, tp[4] contains
690 * the second offset and needs to get stuffed in after the
691 * next overflow page is added.
693 n = tp[4];
694 free_space = FREESPACE(tp);
695 off = OFFSET(tp);
696 tp[0] -= 2;
697 FREESPACE(tp) = free_space + OVFLSIZE;
698 OFFSET(tp) = off;
699 tmpp = __add_ovflpage(hashp, big_keyp);
700 if (!tmpp)
701 return (-1);
702 tp[4] = n;
703 } else
704 tmpp = big_keyp;
706 if (change)
707 ret->newp = tmpp;
708 else
709 ret->oldp = tmpp;
710 return (0);