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