comparison nss/lib/dbm/src/h_page.c @ 3:150b72113545

Add DBM and legacydb support
author Andre Heinecke <andre.heinecke@intevation.de>
date Tue, 05 Aug 2014 18:32:02 +0200
parents
children
comparison
equal deleted inserted replaced
2:a945361df361 3:150b72113545
1 /*-
2 * Copyright (c) 1990, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Margo Seltzer.
7 *
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.
21 *
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.
33 */
34
35 #if defined(unix)
36 #define MY_LSEEK lseek
37 #else
38 #define MY_LSEEK new_lseek
39 extern long new_lseek(int fd, long pos, int start);
40 #endif
41
42 #if defined(LIBC_SCCS) && !defined(lint)
43 static char sccsid[] = "@(#)hash_page.c 8.7 (Berkeley) 8/16/94";
44 #endif /* LIBC_SCCS and not lint */
45
46 /*
47 * PACKAGE: hashing
48 *
49 * DESCRIPTION:
50 * Page manipulation for hashing package.
51 *
52 * ROUTINES:
53 *
54 * External
55 * __get_page
56 * __add_ovflpage
57 * Internal
58 * overflow_page
59 * open_temp
60 */
61 #ifndef macintosh
62 #include <sys/types.h>
63 #endif
64
65 #if defined(macintosh)
66 #include <unistd.h>
67 #endif
68
69 #include <errno.h>
70 #include <fcntl.h>
71 #if defined(_WIN32) || defined(_WINDOWS)
72 #include <io.h>
73 #endif
74 #include <signal.h>
75 #include <stdio.h>
76 #include <stdlib.h>
77 #include <string.h>
78
79 #if !defined(_WIN32) && !defined(_WINDOWS) && !defined(macintosh)
80 #include <unistd.h>
81 #endif
82
83 #include <assert.h>
84
85 #include "mcom_db.h"
86 #include "hash.h"
87 #include "page.h"
88 /* #include "extern.h" */
89
90 extern int mkstempflags(char *path, int extraFlags);
91
92 static uint32 *fetch_bitmap __P((HTAB *, uint32));
93 static uint32 first_free __P((uint32));
94 static int open_temp __P((HTAB *));
95 static uint16 overflow_page __P((HTAB *));
96 static void squeeze_key __P((uint16 *, const DBT *, const DBT *));
97 static int ugly_split
98 __P((HTAB *, uint32, BUFHEAD *, BUFHEAD *, int, int));
99
100 #define PAGE_INIT(P) { \
101 ((uint16 *)(P))[0] = 0; \
102 ((uint16 *)(P))[1] = hashp->BSIZE - 3 * sizeof(uint16); \
103 ((uint16 *)(P))[2] = hashp->BSIZE; \
104 }
105
106 /* implement a new lseek using lseek that
107 * writes zero's when extending a file
108 * beyond the end.
109 */
110 long new_lseek(int fd, long offset, int origin)
111 {
112 long cur_pos=0;
113 long end_pos=0;
114 long seek_pos=0;
115
116 if(origin == SEEK_CUR)
117 {
118 if(offset < 1)
119 return(lseek(fd, offset, SEEK_CUR));
120
121 cur_pos = lseek(fd, 0, SEEK_CUR);
122
123 if(cur_pos < 0)
124 return(cur_pos);
125 }
126
127 end_pos = lseek(fd, 0, SEEK_END);
128 if(end_pos < 0)
129 return(end_pos);
130
131 if(origin == SEEK_SET)
132 seek_pos = offset;
133 else if(origin == SEEK_CUR)
134 seek_pos = cur_pos + offset;
135 else if(origin == SEEK_END)
136 seek_pos = end_pos + offset;
137 else
138 {
139 assert(0);
140 return(-1);
141 }
142
143 /* the seek position desired is before the
144 * end of the file. We don't need
145 * to do anything special except the seek.
146 */
147 if(seek_pos <= end_pos)
148 return(lseek(fd, seek_pos, SEEK_SET));
149
150 /* the seek position is beyond the end of the
151 * file. Write zero's to the end.
152 *
153 * we are already at the end of the file so
154 * we just need to "write()" zeros for the
155 * difference between seek_pos-end_pos and
156 * then seek to the position to finish
157 * the call
158 */
159 {
160 char buffer[1024];
161 long len = seek_pos-end_pos;
162 memset(&buffer, 0, 1024);
163 while(len > 0)
164 {
165 write(fd, (char*)&buffer, (size_t)(1024 > len ? len : 1024));
166 len -= 1024;
167 }
168 return(lseek(fd, seek_pos, SEEK_SET));
169 }
170
171 }
172
173 /*
174 * This is called AFTER we have verified that there is room on the page for
175 * the pair (PAIRFITS has returned true) so we go right ahead and start moving
176 * stuff on.
177 */
178 static void
179 putpair(char *p, const DBT *key, DBT * val)
180 {
181 register uint16 *bp, n, off;
182
183 bp = (uint16 *)p;
184
185 /* Enter the key first. */
186 n = bp[0];
187
188 off = OFFSET(bp) - key->size;
189 memmove(p + off, key->data, key->size);
190 bp[++n] = off;
191
192 /* Now the data. */
193 off -= val->size;
194 memmove(p + off, val->data, val->size);
195 bp[++n] = off;
196
197 /* Adjust page info. */
198 bp[0] = n;
199 bp[n + 1] = off - ((n + 3) * sizeof(uint16));
200 bp[n + 2] = off;
201 }
202
203 /*
204 * Returns:
205 * 0 OK
206 * -1 error
207 */
208 extern int
209 __delpair(HTAB *hashp, BUFHEAD *bufp, int ndx)
210 {
211 register uint16 *bp, newoff;
212 register int n;
213 uint16 pairlen;
214
215 bp = (uint16 *)bufp->page;
216 n = bp[0];
217
218 if (bp[ndx + 1] < REAL_KEY)
219 return (__big_delete(hashp, bufp));
220 if (ndx != 1)
221 newoff = bp[ndx - 1];
222 else
223 newoff = hashp->BSIZE;
224 pairlen = newoff - bp[ndx + 1];
225
226 if (ndx != (n - 1)) {
227 /* Hard Case -- need to shuffle keys */
228 register int i;
229 register char *src = bufp->page + (int)OFFSET(bp);
230 uint32 dst_offset = (uint32)OFFSET(bp) + (uint32)pairlen;
231 register char *dst = bufp->page + dst_offset;
232 uint32 length = bp[ndx + 1] - OFFSET(bp);
233
234 /*
235 * +-----------+XXX+---------+XXX+---------+---------> +infinity
236 * | | | |
237 * 0 src_offset dst_offset BSIZE
238 *
239 * Dst_offset is > src_offset, so if src_offset were bad, dst_offset
240 * would be too, therefore we check only dst_offset.
241 *
242 * If dst_offset is >= BSIZE, either OFFSET(bp), or pairlen, or both
243 * is corrupted.
244 *
245 * Once we know dst_offset is < BSIZE, we can subtract it from BSIZE
246 * to get an upper bound on length.
247 */
248 if(dst_offset > (uint32)hashp->BSIZE)
249 return(DATABASE_CORRUPTED_ERROR);
250
251 if(length > (uint32)(hashp->BSIZE - dst_offset))
252 return(DATABASE_CORRUPTED_ERROR);
253
254 memmove(dst, src, length);
255
256 /* Now adjust the pointers */
257 for (i = ndx + 2; i <= n; i += 2) {
258 if (bp[i + 1] == OVFLPAGE) {
259 bp[i - 2] = bp[i];
260 bp[i - 1] = bp[i + 1];
261 } else {
262 bp[i - 2] = bp[i] + pairlen;
263 bp[i - 1] = bp[i + 1] + pairlen;
264 }
265 }
266 }
267 /* Finally adjust the page data */
268 bp[n] = OFFSET(bp) + pairlen;
269 bp[n - 1] = bp[n + 1] + pairlen + 2 * sizeof(uint16);
270 bp[0] = n - 2;
271 hashp->NKEYS--;
272
273 bufp->flags |= BUF_MOD;
274 return (0);
275 }
276 /*
277 * Returns:
278 * 0 ==> OK
279 * -1 ==> Error
280 */
281 extern int
282 __split_page(HTAB *hashp, uint32 obucket, uint32 nbucket)
283 {
284 register BUFHEAD *new_bufp, *old_bufp;
285 register uint16 *ino;
286 register uint16 *tmp_uint16_array;
287 register char *np;
288 DBT key, val;
289 uint16 n, ndx;
290 int retval;
291 uint16 copyto, diff, moved;
292 size_t off;
293 char *op;
294
295 copyto = (uint16)hashp->BSIZE;
296 off = (uint16)hashp->BSIZE;
297 old_bufp = __get_buf(hashp, obucket, NULL, 0);
298 if (old_bufp == NULL)
299 return (-1);
300 new_bufp = __get_buf(hashp, nbucket, NULL, 0);
301 if (new_bufp == NULL)
302 return (-1);
303
304 old_bufp->flags |= (BUF_MOD | BUF_PIN);
305 new_bufp->flags |= (BUF_MOD | BUF_PIN);
306
307 ino = (uint16 *)(op = old_bufp->page);
308 np = new_bufp->page;
309
310 moved = 0;
311
312 for (n = 1, ndx = 1; n < ino[0]; n += 2) {
313 if (ino[n + 1] < REAL_KEY) {
314 retval = ugly_split(hashp, obucket, old_bufp, new_bufp,
315 (int)copyto, (int)moved);
316 old_bufp->flags &= ~BUF_PIN;
317 new_bufp->flags &= ~BUF_PIN;
318 return (retval);
319
320 }
321 key.data = (uint8 *)op + ino[n];
322
323 /* check here for ino[n] being greater than
324 * off. If it is then the database has
325 * been corrupted.
326 */
327 if(ino[n] > off)
328 return(DATABASE_CORRUPTED_ERROR);
329
330 key.size = off - ino[n];
331
332 #ifdef DEBUG
333 /* make sure the size is positive */
334 assert(((int)key.size) > -1);
335 #endif
336
337 if (__call_hash(hashp, (char *)key.data, key.size) == obucket) {
338 /* Don't switch page */
339 diff = copyto - off;
340 if (diff) {
341 copyto = ino[n + 1] + diff;
342 memmove(op + copyto, op + ino[n + 1],
343 off - ino[n + 1]);
344 ino[ndx] = copyto + ino[n] - ino[n + 1];
345 ino[ndx + 1] = copyto;
346 } else
347 copyto = ino[n + 1];
348 ndx += 2;
349 } else {
350 /* Switch page */
351 val.data = (uint8 *)op + ino[n + 1];
352 val.size = ino[n] - ino[n + 1];
353
354 /* if the pair doesn't fit something is horribly
355 * wrong. LJM
356 */
357 tmp_uint16_array = (uint16*)np;
358 if(!PAIRFITS(tmp_uint16_array, &key, &val))
359 return(DATABASE_CORRUPTED_ERROR);
360
361 putpair(np, &key, &val);
362 moved += 2;
363 }
364
365 off = ino[n + 1];
366 }
367
368 /* Now clean up the page */
369 ino[0] -= moved;
370 FREESPACE(ino) = copyto - sizeof(uint16) * (ino[0] + 3);
371 OFFSET(ino) = copyto;
372
373 #ifdef DEBUG3
374 (void)fprintf(stderr, "split %d/%d\n",
375 ((uint16 *)np)[0] / 2,
376 ((uint16 *)op)[0] / 2);
377 #endif
378 /* unpin both pages */
379 old_bufp->flags &= ~BUF_PIN;
380 new_bufp->flags &= ~BUF_PIN;
381 return (0);
382 }
383
384 /*
385 * Called when we encounter an overflow or big key/data page during split
386 * handling. This is special cased since we have to begin checking whether
387 * the key/data pairs fit on their respective pages and because we may need
388 * overflow pages for both the old and new pages.
389 *
390 * The first page might be a page with regular key/data pairs in which case
391 * we have a regular overflow condition and just need to go on to the next
392 * page or it might be a big key/data pair in which case we need to fix the
393 * big key/data pair.
394 *
395 * Returns:
396 * 0 ==> success
397 * -1 ==> failure
398 */
399
400 /* the maximum number of loops we will allow UGLY split to chew
401 * on before we assume the database is corrupted and throw it
402 * away.
403 */
404 #define MAX_UGLY_SPLIT_LOOPS 10000
405
406 static int
407 ugly_split(HTAB *hashp, uint32 obucket, BUFHEAD *old_bufp,
408 BUFHEAD *new_bufp,/* Same as __split_page. */ int copyto, int moved)
409 /* int copyto; First byte on page which contains key/data values. */
410 /* int moved; Number of pairs moved to new page. */
411 {
412 register BUFHEAD *bufp; /* Buffer header for ino */
413 register uint16 *ino; /* Page keys come off of */
414 register uint16 *np; /* New page */
415 register uint16 *op; /* Page keys go on to if they aren't moving */
416 uint32 loop_detection=0;
417
418 BUFHEAD *last_bfp; /* Last buf header OVFL needing to be freed */
419 DBT key, val;
420 SPLIT_RETURN ret;
421 uint16 n, off, ov_addr, scopyto;
422 char *cino; /* Character value of ino */
423 int status;
424
425 bufp = old_bufp;
426 ino = (uint16 *)old_bufp->page;
427 np = (uint16 *)new_bufp->page;
428 op = (uint16 *)old_bufp->page;
429 last_bfp = NULL;
430 scopyto = (uint16)copyto; /* ANSI */
431
432 n = ino[0] - 1;
433 while (n < ino[0]) {
434
435
436 /* this function goes nuts sometimes and never returns.
437 * I havent found the problem yet but I need a solution
438 * so if we loop too often we assume a database curruption error
439 * :LJM
440 */
441 loop_detection++;
442
443 if(loop_detection > MAX_UGLY_SPLIT_LOOPS)
444 return DATABASE_CORRUPTED_ERROR;
445
446 if (ino[2] < REAL_KEY && ino[2] != OVFLPAGE) {
447 if ((status = __big_split(hashp, old_bufp,
448 new_bufp, bufp, bufp->addr, obucket, &ret)))
449 return (status);
450 old_bufp = ret.oldp;
451 if (!old_bufp)
452 return (-1);
453 op = (uint16 *)old_bufp->page;
454 new_bufp = ret.newp;
455 if (!new_bufp)
456 return (-1);
457 np = (uint16 *)new_bufp->page;
458 bufp = ret.nextp;
459 if (!bufp)
460 return (0);
461 cino = (char *)bufp->page;
462 ino = (uint16 *)cino;
463 last_bfp = ret.nextp;
464 } else if (ino[n + 1] == OVFLPAGE) {
465 ov_addr = ino[n];
466 /*
467 * Fix up the old page -- the extra 2 are the fields
468 * which contained the overflow information.
469 */
470 ino[0] -= (moved + 2);
471 FREESPACE(ino) =
472 scopyto - sizeof(uint16) * (ino[0] + 3);
473 OFFSET(ino) = scopyto;
474
475 bufp = __get_buf(hashp, ov_addr, bufp, 0);
476 if (!bufp)
477 return (-1);
478
479 ino = (uint16 *)bufp->page;
480 n = 1;
481 scopyto = hashp->BSIZE;
482 moved = 0;
483
484 if (last_bfp)
485 __free_ovflpage(hashp, last_bfp);
486 last_bfp = bufp;
487 }
488 /* Move regular sized pairs of there are any */
489 off = hashp->BSIZE;
490 for (n = 1; (n < ino[0]) && (ino[n + 1] >= REAL_KEY); n += 2) {
491 cino = (char *)ino;
492 key.data = (uint8 *)cino + ino[n];
493 key.size = off - ino[n];
494 val.data = (uint8 *)cino + ino[n + 1];
495 val.size = ino[n] - ino[n + 1];
496 off = ino[n + 1];
497
498 if (__call_hash(hashp, (char*)key.data, key.size) == obucket) {
499 /* Keep on old page */
500 if (PAIRFITS(op, (&key), (&val)))
501 putpair((char *)op, &key, &val);
502 else {
503 old_bufp =
504 __add_ovflpage(hashp, old_bufp);
505 if (!old_bufp)
506 return (-1);
507 op = (uint16 *)old_bufp->page;
508 putpair((char *)op, &key, &val);
509 }
510 old_bufp->flags |= BUF_MOD;
511 } else {
512 /* Move to new page */
513 if (PAIRFITS(np, (&key), (&val)))
514 putpair((char *)np, &key, &val);
515 else {
516 new_bufp =
517 __add_ovflpage(hashp, new_bufp);
518 if (!new_bufp)
519 return (-1);
520 np = (uint16 *)new_bufp->page;
521 putpair((char *)np, &key, &val);
522 }
523 new_bufp->flags |= BUF_MOD;
524 }
525 }
526 }
527 if (last_bfp)
528 __free_ovflpage(hashp, last_bfp);
529 return (0);
530 }
531
532 /*
533 * Add the given pair to the page
534 *
535 * Returns:
536 * 0 ==> OK
537 * 1 ==> failure
538 */
539 extern int
540 __addel(HTAB *hashp, BUFHEAD *bufp, const DBT *key, const DBT * val)
541 {
542 register uint16 *bp, *sop;
543 int do_expand;
544
545 bp = (uint16 *)bufp->page;
546 do_expand = 0;
547 while (bp[0] && (bp[2] < REAL_KEY || bp[bp[0]] < REAL_KEY))
548 /* Exception case */
549 if (bp[2] == FULL_KEY_DATA && bp[0] == 2)
550 /* This is the last page of a big key/data pair
551 and we need to add another page */
552 break;
553 else if (bp[2] < REAL_KEY && bp[bp[0]] != OVFLPAGE) {
554 bufp = __get_buf(hashp, bp[bp[0] - 1], bufp, 0);
555 if (!bufp)
556 {
557 #ifdef DEBUG
558 assert(0);
559 #endif
560 return (-1);
561 }
562 bp = (uint16 *)bufp->page;
563 } else
564 /* Try to squeeze key on this page */
565 if (FREESPACE(bp) > PAIRSIZE(key, val)) {
566 {
567 squeeze_key(bp, key, val);
568
569 /* LJM: I added this because I think it was
570 * left out on accident.
571 * if this isn't incremented nkeys will not
572 * be the actual number of keys in the db.
573 */
574 hashp->NKEYS++;
575 return (0);
576 }
577 } else {
578 bufp = __get_buf(hashp, bp[bp[0] - 1], bufp, 0);
579 if (!bufp)
580 {
581 #ifdef DEBUG
582 assert(0);
583 #endif
584 return (-1);
585 }
586 bp = (uint16 *)bufp->page;
587 }
588
589 if (PAIRFITS(bp, key, val))
590 putpair(bufp->page, key, (DBT *)val);
591 else {
592 do_expand = 1;
593 bufp = __add_ovflpage(hashp, bufp);
594 if (!bufp)
595 {
596 #ifdef DEBUG
597 assert(0);
598 #endif
599 return (-1);
600 }
601 sop = (uint16 *)bufp->page;
602
603 if (PAIRFITS(sop, key, val))
604 putpair((char *)sop, key, (DBT *)val);
605 else
606 if (__big_insert(hashp, bufp, key, val))
607 {
608 #ifdef DEBUG
609 assert(0);
610 #endif
611 return (-1);
612 }
613 }
614 bufp->flags |= BUF_MOD;
615 /*
616 * If the average number of keys per bucket exceeds the fill factor,
617 * expand the table.
618 */
619 hashp->NKEYS++;
620 if (do_expand ||
621 (hashp->NKEYS / (hashp->MAX_BUCKET + 1) > hashp->FFACTOR))
622 return (__expand_table(hashp));
623 return (0);
624 }
625
626 /*
627 *
628 * Returns:
629 * pointer on success
630 * NULL on error
631 */
632 extern BUFHEAD *
633 __add_ovflpage(HTAB *hashp, BUFHEAD *bufp)
634 {
635 register uint16 *sp;
636 uint16 ndx, ovfl_num;
637 #ifdef DEBUG1
638 int tmp1, tmp2;
639 #endif
640 sp = (uint16 *)bufp->page;
641
642 /* Check if we are dynamically determining the fill factor */
643 if (hashp->FFACTOR == DEF_FFACTOR) {
644 hashp->FFACTOR = sp[0] >> 1;
645 if (hashp->FFACTOR < MIN_FFACTOR)
646 hashp->FFACTOR = MIN_FFACTOR;
647 }
648 bufp->flags |= BUF_MOD;
649 ovfl_num = overflow_page(hashp);
650 #ifdef DEBUG1
651 tmp1 = bufp->addr;
652 tmp2 = bufp->ovfl ? bufp->ovfl->addr : 0;
653 #endif
654 if (!ovfl_num || !(bufp->ovfl = __get_buf(hashp, ovfl_num, bufp, 1)))
655 return (NULL);
656 bufp->ovfl->flags |= BUF_MOD;
657 #ifdef DEBUG1
658 (void)fprintf(stderr, "ADDOVFLPAGE: %d->ovfl was %d is now %d\n",
659 tmp1, tmp2, bufp->ovfl->addr);
660 #endif
661 ndx = sp[0];
662 /*
663 * Since a pair is allocated on a page only if there's room to add
664 * an overflow page, we know that the OVFL information will fit on
665 * the page.
666 */
667 sp[ndx + 4] = OFFSET(sp);
668 sp[ndx + 3] = FREESPACE(sp) - OVFLSIZE;
669 sp[ndx + 1] = ovfl_num;
670 sp[ndx + 2] = OVFLPAGE;
671 sp[0] = ndx + 2;
672 #ifdef HASH_STATISTICS
673 hash_overflows++;
674 #endif
675 return (bufp->ovfl);
676 }
677
678 /*
679 * Returns:
680 * 0 indicates SUCCESS
681 * -1 indicates FAILURE
682 */
683 extern int
684 __get_page(HTAB *hashp,
685 char * p,
686 uint32 bucket,
687 int is_bucket,
688 int is_disk,
689 int is_bitmap)
690 {
691 register int fd, page;
692 size_t size;
693 int rsize;
694 uint16 *bp;
695
696 fd = hashp->fp;
697 size = hashp->BSIZE;
698
699 if ((fd == -1) || !is_disk) {
700 PAGE_INIT(p);
701 return (0);
702 }
703 if (is_bucket)
704 page = BUCKET_TO_PAGE(bucket);
705 else
706 page = OADDR_TO_PAGE(bucket);
707 if ((MY_LSEEK(fd, (off_t)page << hashp->BSHIFT, SEEK_SET) == -1) ||
708 ((rsize = read(fd, p, size)) == -1))
709 return (-1);
710
711 bp = (uint16 *)p;
712 if (!rsize)
713 bp[0] = 0; /* We hit the EOF, so initialize a new page */
714 else
715 if ((unsigned)rsize != size) {
716 errno = EFTYPE;
717 return (-1);
718 }
719
720 if (!is_bitmap && !bp[0]) {
721 PAGE_INIT(p);
722 } else {
723
724 #ifdef DEBUG
725 if(BYTE_ORDER == LITTLE_ENDIAN)
726 {
727 int is_little_endian;
728 is_little_endian = BYTE_ORDER;
729 }
730 else if(BYTE_ORDER == BIG_ENDIAN)
731 {
732 int is_big_endian;
733 is_big_endian = BYTE_ORDER;
734 }
735 else
736 {
737 assert(0);
738 }
739 #endif
740
741 if (hashp->LORDER != BYTE_ORDER) {
742 register int i, max;
743
744 if (is_bitmap) {
745 max = hashp->BSIZE >> 2; /* divide by 4 */
746 for (i = 0; i < max; i++)
747 M_32_SWAP(((int *)p)[i]);
748 } else {
749 M_16_SWAP(bp[0]);
750 max = bp[0] + 2;
751
752 /* bound the size of max by
753 * the maximum number of entries
754 * in the array
755 */
756 if((unsigned)max > (size / sizeof(uint16)))
757 return(DATABASE_CORRUPTED_ERROR);
758
759 /* do the byte order swap
760 */
761 for (i = 1; i <= max; i++)
762 M_16_SWAP(bp[i]);
763 }
764 }
765
766 /* check the validity of the page here
767 * (after doing byte order swaping if necessary)
768 */
769 if(!is_bitmap && bp[0] != 0)
770 {
771 uint16 num_keys = bp[0];
772 uint16 offset;
773 uint16 i;
774
775 /* bp[0] is supposed to be the number of
776 * entries currently in the page. If
777 * bp[0] is too large (larger than the whole
778 * page) then the page is corrupted
779 */
780 if(bp[0] > (size / sizeof(uint16)))
781 return(DATABASE_CORRUPTED_ERROR);
782
783 /* bound free space */
784 if(FREESPACE(bp) > size)
785 return(DATABASE_CORRUPTED_ERROR);
786
787 /* check each key and data offset to make
788 * sure they are all within bounds they
789 * should all be less than the previous
790 * offset as well.
791 */
792 offset = size;
793 for(i=1 ; i <= num_keys; i+=2)
794 {
795 /* ignore overflow pages etc. */
796 if(bp[i+1] >= REAL_KEY)
797 {
798
799 if(bp[i] > offset || bp[i+1] > bp[i])
800 return(DATABASE_CORRUPTED_ERROR);
801
802 offset = bp[i+1];
803 }
804 else
805 {
806 /* there are no other valid keys after
807 * seeing a non REAL_KEY
808 */
809 break;
810 }
811 }
812 }
813 }
814 return (0);
815 }
816
817 /*
818 * Write page p to disk
819 *
820 * Returns:
821 * 0 ==> OK
822 * -1 ==>failure
823 */
824 extern int
825 __put_page(HTAB *hashp, char *p, uint32 bucket, int is_bucket, int is_bitmap)
826 {
827 register int fd, page;
828 size_t size;
829 int wsize;
830 off_t offset;
831
832 size = hashp->BSIZE;
833 if ((hashp->fp == -1) && open_temp(hashp))
834 return (-1);
835 fd = hashp->fp;
836
837 if (hashp->LORDER != BYTE_ORDER) {
838 register int i;
839 register int max;
840
841 if (is_bitmap) {
842 max = hashp->BSIZE >> 2; /* divide by 4 */
843 for (i = 0; i < max; i++)
844 M_32_SWAP(((int *)p)[i]);
845 } else {
846 max = ((uint16 *)p)[0] + 2;
847
848 /* bound the size of max by
849 * the maximum number of entries
850 * in the array
851 */
852 if((unsigned)max > (size / sizeof(uint16)))
853 return(DATABASE_CORRUPTED_ERROR);
854
855 for (i = 0; i <= max; i++)
856 M_16_SWAP(((uint16 *)p)[i]);
857
858 }
859 }
860
861 if (is_bucket)
862 page = BUCKET_TO_PAGE(bucket);
863 else
864 page = OADDR_TO_PAGE(bucket);
865 offset = (off_t)page << hashp->BSHIFT;
866 if ((MY_LSEEK(fd, offset, SEEK_SET) == -1) ||
867 ((wsize = write(fd, p, size)) == -1))
868 /* Errno is set */
869 return (-1);
870 if ((unsigned)wsize != size) {
871 errno = EFTYPE;
872 return (-1);
873 }
874 #if defined(_WIN32) || defined(_WINDOWS)
875 if (offset + size > hashp->file_size) {
876 hashp->updateEOF = 1;
877 }
878 #endif
879 /* put the page back the way it was so that it isn't byteswapped
880 * if it remains in memory - LJM
881 */
882 if (hashp->LORDER != BYTE_ORDER) {
883 register int i;
884 register int max;
885
886 if (is_bitmap) {
887 max = hashp->BSIZE >> 2; /* divide by 4 */
888 for (i = 0; i < max; i++)
889 M_32_SWAP(((int *)p)[i]);
890 } else {
891 uint16 *bp = (uint16 *)p;
892
893 M_16_SWAP(bp[0]);
894 max = bp[0] + 2;
895
896 /* no need to bound the size if max again
897 * since it was done already above
898 */
899
900 /* do the byte order re-swap
901 */
902 for (i = 1; i <= max; i++)
903 M_16_SWAP(bp[i]);
904 }
905 }
906
907 return (0);
908 }
909
910 #define BYTE_MASK ((1 << INT_BYTE_SHIFT) -1)
911 /*
912 * Initialize a new bitmap page. Bitmap pages are left in memory
913 * once they are read in.
914 */
915 extern int
916 __ibitmap(HTAB *hashp, int pnum, int nbits, int ndx)
917 {
918 uint32 *ip;
919 size_t clearbytes, clearints;
920
921 if ((ip = (uint32 *)malloc((size_t)hashp->BSIZE)) == NULL)
922 return (1);
923 hashp->nmaps++;
924 clearints = ((nbits - 1) >> INT_BYTE_SHIFT) + 1;
925 clearbytes = clearints << INT_TO_BYTE;
926 (void)memset((char *)ip, 0, clearbytes);
927 (void)memset(((char *)ip) + clearbytes, 0xFF,
928 hashp->BSIZE - clearbytes);
929 ip[clearints - 1] = ALL_SET << (nbits & BYTE_MASK);
930 SETBIT(ip, 0);
931 hashp->BITMAPS[ndx] = (uint16)pnum;
932 hashp->mapp[ndx] = ip;
933 return (0);
934 }
935
936 static uint32
937 first_free(uint32 map)
938 {
939 register uint32 i, mask;
940
941 mask = 0x1;
942 for (i = 0; i < BITS_PER_MAP; i++) {
943 if (!(mask & map))
944 return (i);
945 mask = mask << 1;
946 }
947 return (i);
948 }
949
950 static uint16
951 overflow_page(HTAB *hashp)
952 {
953 register uint32 *freep=NULL;
954 register int max_free, offset, splitnum;
955 uint16 addr;
956 uint32 i;
957 int bit, first_page, free_bit, free_page, in_use_bits, j;
958 #ifdef DEBUG2
959 int tmp1, tmp2;
960 #endif
961 splitnum = hashp->OVFL_POINT;
962 max_free = hashp->SPARES[splitnum];
963
964 free_page = (max_free - 1) >> (hashp->BSHIFT + BYTE_SHIFT);
965 free_bit = (max_free - 1) & ((hashp->BSIZE << BYTE_SHIFT) - 1);
966
967 /* Look through all the free maps to find the first free block */
968 first_page = hashp->LAST_FREED >>(hashp->BSHIFT + BYTE_SHIFT);
969 for ( i = first_page; i <= (unsigned)free_page; i++ ) {
970 if (!(freep = (uint32 *)hashp->mapp[i]) &&
971 !(freep = fetch_bitmap(hashp, i)))
972 return (0);
973 if (i == (unsigned)free_page)
974 in_use_bits = free_bit;
975 else
976 in_use_bits = (hashp->BSIZE << BYTE_SHIFT) - 1;
977
978 if (i == (unsigned)first_page) {
979 bit = hashp->LAST_FREED &
980 ((hashp->BSIZE << BYTE_SHIFT) - 1);
981 j = bit / BITS_PER_MAP;
982 bit = bit & ~(BITS_PER_MAP - 1);
983 } else {
984 bit = 0;
985 j = 0;
986 }
987 for (; bit <= in_use_bits; j++, bit += BITS_PER_MAP)
988 if (freep[j] != ALL_SET)
989 goto found;
990 }
991
992 /* No Free Page Found */
993 hashp->LAST_FREED = hashp->SPARES[splitnum];
994 hashp->SPARES[splitnum]++;
995 offset = hashp->SPARES[splitnum] -
996 (splitnum ? hashp->SPARES[splitnum - 1] : 0);
997
998 #define OVMSG "HASH: Out of overflow pages. Increase page size\n"
999 if (offset > SPLITMASK) {
1000 if (++splitnum >= NCACHED) {
1001 #ifndef macintosh
1002 (void)write(STDERR_FILENO, OVMSG, sizeof(OVMSG) - 1);
1003 #endif
1004 return (0);
1005 }
1006 hashp->OVFL_POINT = splitnum;
1007 hashp->SPARES[splitnum] = hashp->SPARES[splitnum-1];
1008 hashp->SPARES[splitnum-1]--;
1009 offset = 1;
1010 }
1011
1012 /* Check if we need to allocate a new bitmap page */
1013 if (free_bit == (hashp->BSIZE << BYTE_SHIFT) - 1) {
1014 free_page++;
1015 if (free_page >= NCACHED) {
1016 #ifndef macintosh
1017 (void)write(STDERR_FILENO, OVMSG, sizeof(OVMSG) - 1);
1018 #endif
1019 return (0);
1020 }
1021 /*
1022 * This is tricky. The 1 indicates that you want the new page
1023 * allocated with 1 clear bit. Actually, you are going to
1024 * allocate 2 pages from this map. The first is going to be
1025 * the map page, the second is the overflow page we were
1026 * looking for. The init_bitmap routine automatically, sets
1027 * the first bit of itself to indicate that the bitmap itself
1028 * is in use. We would explicitly set the second bit, but
1029 * don't have to if we tell init_bitmap not to leave it clear
1030 * in the first place.
1031 */
1032 if (__ibitmap(hashp,
1033 (int)OADDR_OF(splitnum, offset), 1, free_page))
1034 return (0);
1035 hashp->SPARES[splitnum]++;
1036 #ifdef DEBUG2
1037 free_bit = 2;
1038 #endif
1039 offset++;
1040 if (offset > SPLITMASK) {
1041 if (++splitnum >= NCACHED) {
1042 #ifndef macintosh
1043 (void)write(STDERR_FILENO, OVMSG,
1044 sizeof(OVMSG) - 1);
1045 #endif
1046 return (0);
1047 }
1048 hashp->OVFL_POINT = splitnum;
1049 hashp->SPARES[splitnum] = hashp->SPARES[splitnum-1];
1050 hashp->SPARES[splitnum-1]--;
1051 offset = 0;
1052 }
1053 } else {
1054 /*
1055 * Free_bit addresses the last used bit. Bump it to address
1056 * the first available bit.
1057 */
1058 free_bit++;
1059 SETBIT(freep, free_bit);
1060 }
1061
1062 /* Calculate address of the new overflow page */
1063 addr = OADDR_OF(splitnum, offset);
1064 #ifdef DEBUG2
1065 (void)fprintf(stderr, "OVERFLOW_PAGE: ADDR: %d BIT: %d PAGE %d\n",
1066 addr, free_bit, free_page);
1067 #endif
1068 return (addr);
1069
1070 found:
1071 bit = bit + first_free(freep[j]);
1072 SETBIT(freep, bit);
1073 #ifdef DEBUG2
1074 tmp1 = bit;
1075 tmp2 = i;
1076 #endif
1077 /*
1078 * Bits are addressed starting with 0, but overflow pages are addressed
1079 * beginning at 1. Bit is a bit addressnumber, so we need to increment
1080 * it to convert it to a page number.
1081 */
1082 bit = 1 + bit + (i * (hashp->BSIZE << BYTE_SHIFT));
1083 if (bit >= hashp->LAST_FREED)
1084 hashp->LAST_FREED = bit - 1;
1085
1086 /* Calculate the split number for this page */
1087 for (i = 0; (i < (unsigned)splitnum) && (bit > hashp->SPARES[i]); i++) {}
1088 offset = (i ? bit - hashp->SPARES[i - 1] : bit);
1089 if (offset >= SPLITMASK)
1090 return (0); /* Out of overflow pages */
1091 addr = OADDR_OF(i, offset);
1092 #ifdef DEBUG2
1093 (void)fprintf(stderr, "OVERFLOW_PAGE: ADDR: %d BIT: %d PAGE %d\n",
1094 addr, tmp1, tmp2);
1095 #endif
1096
1097 /* Allocate and return the overflow page */
1098 return (addr);
1099 }
1100
1101 /*
1102 * Mark this overflow page as free.
1103 */
1104 extern void
1105 __free_ovflpage(HTAB *hashp, BUFHEAD *obufp)
1106 {
1107 uint16 addr;
1108 uint32 *freep;
1109 uint32 bit_address, free_page, free_bit;
1110 uint16 ndx;
1111
1112 if(!obufp || !obufp->addr)
1113 return;
1114
1115 addr = obufp->addr;
1116 #ifdef DEBUG1
1117 (void)fprintf(stderr, "Freeing %d\n", addr);
1118 #endif
1119 ndx = (((uint16)addr) >> SPLITSHIFT);
1120 bit_address =
1121 (ndx ? hashp->SPARES[ndx - 1] : 0) + (addr & SPLITMASK) - 1;
1122 if (bit_address < (uint32)hashp->LAST_FREED)
1123 hashp->LAST_FREED = bit_address;
1124 free_page = (bit_address >> (hashp->BSHIFT + BYTE_SHIFT));
1125 free_bit = bit_address & ((hashp->BSIZE << BYTE_SHIFT) - 1);
1126
1127 if (!(freep = hashp->mapp[free_page]))
1128 freep = fetch_bitmap(hashp, free_page);
1129
1130 #ifdef DEBUG
1131 /*
1132 * This had better never happen. It means we tried to read a bitmap
1133 * that has already had overflow pages allocated off it, and we
1134 * failed to read it from the file.
1135 */
1136 if (!freep)
1137 {
1138 assert(0);
1139 return;
1140 }
1141 #endif
1142 CLRBIT(freep, free_bit);
1143 #ifdef DEBUG2
1144 (void)fprintf(stderr, "FREE_OVFLPAGE: ADDR: %d BIT: %d PAGE %d\n",
1145 obufp->addr, free_bit, free_page);
1146 #endif
1147 __reclaim_buf(hashp, obufp);
1148 }
1149
1150 /*
1151 * Returns:
1152 * 0 success
1153 * -1 failure
1154 */
1155 static int
1156 open_temp(HTAB *hashp)
1157 {
1158 #ifdef XP_OS2
1159 hashp->fp = mkstemp(NULL);
1160 #else
1161 #if !defined(_WIN32) && !defined(_WINDOWS) && !defined(macintosh)
1162 sigset_t set, oset;
1163 #endif
1164 #if !defined(macintosh)
1165 char * tmpdir;
1166 size_t len;
1167 char last;
1168 #endif
1169 static const char namestr[] = "/_hashXXXXXX";
1170 char filename[1024];
1171
1172 #if !defined(_WIN32) && !defined(_WINDOWS) && !defined(macintosh)
1173 /* Block signals; make sure file goes away at process exit. */
1174 (void)sigfillset(&set);
1175 (void)sigprocmask(SIG_BLOCK, &set, &oset);
1176 #endif
1177
1178 filename[0] = 0;
1179 #if defined(macintosh)
1180 strcat(filename, namestr + 1);
1181 #else
1182 tmpdir = getenv("TMP");
1183 if (!tmpdir)
1184 tmpdir = getenv("TMPDIR");
1185 if (!tmpdir)
1186 tmpdir = getenv("TEMP");
1187 if (!tmpdir)
1188 tmpdir = ".";
1189 len = strlen(tmpdir);
1190 if (len && len < (sizeof filename - sizeof namestr)) {
1191 strcpy(filename, tmpdir);
1192 }
1193 len = strlen(filename);
1194 last = tmpdir[len - 1];
1195 strcat(filename, (last == '/' || last == '\\') ? namestr + 1 : namestr);
1196 #endif
1197
1198 #if defined(_WIN32) || defined(_WINDOWS)
1199 if ((hashp->fp = mkstempflags(filename, _O_BINARY|_O_TEMPORARY)) != -1) {
1200 if (hashp->filename) {
1201 free(hashp->filename);
1202 }
1203 hashp->filename = strdup(filename);
1204 hashp->is_temp = 1;
1205 }
1206 #else
1207 if ((hashp->fp = mkstemp(filename)) != -1) {
1208 (void)unlink(filename);
1209 #if !defined(macintosh)
1210 (void)fcntl(hashp->fp, F_SETFD, 1);
1211 #endif
1212 }
1213 #endif
1214
1215 #if !defined(_WIN32) && !defined(_WINDOWS) && !defined(macintosh)
1216 (void)sigprocmask(SIG_SETMASK, &oset, (sigset_t *)NULL);
1217 #endif
1218 #endif /* !OS2 */
1219 return (hashp->fp != -1 ? 0 : -1);
1220 }
1221
1222 /*
1223 * We have to know that the key will fit, but the last entry on the page is
1224 * an overflow pair, so we need to shift things.
1225 */
1226 static void
1227 squeeze_key(uint16 *sp, const DBT * key, const DBT * val)
1228 {
1229 register char *p;
1230 uint16 free_space, n, off, pageno;
1231
1232 p = (char *)sp;
1233 n = sp[0];
1234 free_space = FREESPACE(sp);
1235 off = OFFSET(sp);
1236
1237 pageno = sp[n - 1];
1238 off -= key->size;
1239 sp[n - 1] = off;
1240 memmove(p + off, key->data, key->size);
1241 off -= val->size;
1242 sp[n] = off;
1243 memmove(p + off, val->data, val->size);
1244 sp[0] = n + 2;
1245 sp[n + 1] = pageno;
1246 sp[n + 2] = OVFLPAGE;
1247 FREESPACE(sp) = free_space - PAIRSIZE(key, val);
1248 OFFSET(sp) = off;
1249 }
1250
1251 static uint32 *
1252 fetch_bitmap(HTAB *hashp, uint32 ndx)
1253 {
1254 if (ndx >= (unsigned)hashp->nmaps)
1255 return (NULL);
1256 if ((hashp->mapp[ndx] = (uint32 *)malloc((size_t)hashp->BSIZE)) == NULL)
1257 return (NULL);
1258 if (__get_page(hashp,
1259 (char *)hashp->mapp[ndx], hashp->BITMAPS[ndx], 0, 1, 1)) {
1260 free(hashp->mapp[ndx]);
1261 hashp->mapp[ndx] = NULL; /* NEW: 9-11-95 */
1262 return (NULL);
1263 }
1264 return (hashp->mapp[ndx]);
1265 }
1266
1267 #ifdef DEBUG4
1268 int
1269 print_chain(int addr)
1270 {
1271 BUFHEAD *bufp;
1272 short *bp, oaddr;
1273
1274 (void)fprintf(stderr, "%d ", addr);
1275 bufp = __get_buf(hashp, addr, NULL, 0);
1276 bp = (short *)bufp->page;
1277 while (bp[0] && ((bp[bp[0]] == OVFLPAGE) ||
1278 ((bp[0] > 2) && bp[2] < REAL_KEY))) {
1279 oaddr = bp[bp[0] - 1];
1280 (void)fprintf(stderr, "%d ", (int)oaddr);
1281 bufp = __get_buf(hashp, (int)oaddr, bufp, 0);
1282 bp = (short *)bufp->page;
1283 }
1284 (void)fprintf(stderr, "\n");
1285 }
1286 #endif
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)