hash.h revision 1.3 1 /*-
2 * Copyright (c) 1990, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Margo Seltzer.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 * from: @(#)hash.h 8.1 (Berkeley) 6/4/93
37 * $Id: hash.h,v 1.3 1993/08/01 18:43:35 mycroft Exp $
38 */
39
40 /* Operations */
41 typedef enum {
42 HASH_GET, HASH_PUT, HASH_PUTNEW, HASH_DELETE, HASH_FIRST, HASH_NEXT
43 } ACTION;
44
45 /* Buffer Management structures */
46 typedef struct _bufhead BUFHEAD;
47
48 struct _bufhead {
49 BUFHEAD *prev; /* LRU links */
50 BUFHEAD *next; /* LRU links */
51 BUFHEAD *ovfl; /* Overflow page buffer header */
52 u_int addr; /* Address of this page */
53 char *page; /* Actual page data */
54 char flags;
55 #define BUF_MOD 0x0001
56 #define BUF_DISK 0x0002
57 #define BUF_BUCKET 0x0004
58 #define BUF_PIN 0x0008
59 };
60
61 #define IS_BUCKET(X) ((X) & BUF_BUCKET)
62
63 typedef BUFHEAD **SEGMENT;
64
65 /* Hash Table Information */
66 typedef struct hashhdr { /* Disk resident portion */
67 int magic; /* Magic NO for hash tables */
68 int version; /* Version ID */
69 long lorder; /* Byte Order */
70 int bsize; /* Bucket/Page Size */
71 int bshift; /* Bucket shift */
72 int dsize; /* Directory Size */
73 int ssize; /* Segment Size */
74 int sshift; /* Segment shift */
75 int ovfl_point; /* Where overflow pages are being allocated */
76 int last_freed; /* Last overflow page freed */
77 int max_bucket; /* ID of Maximum bucket in use */
78 int high_mask; /* Mask to modulo into entire table */
79 int low_mask; /* Mask to modulo into lower half of table */
80 int ffactor; /* Fill factor */
81 int nkeys; /* Number of keys in hash table */
82 int hdrpages; /* Size of table header */
83 int h_charkey; /* value of hash(CHARKEY) */
84 #define NCACHED 32 /* number of bit maps and spare points */
85 int spares[NCACHED];/* spare pages for overflow */
86 u_short bitmaps[NCACHED]; /* address of overflow page bitmaps */
87 } HASHHDR;
88
89 typedef struct htab { /* Memory resident data structure */
90 HASHHDR hdr; /* Header */
91 int nsegs; /* Number of allocated segments */
92 int exsegs; /* Number of extra allocated segments */
93 int (*hash) (); /* Hash Function */
94 int flags; /* Flag values */
95 int fp; /* File pointer */
96 char *tmp_buf; /* Temporary Buffer for BIG data */
97 char *tmp_key; /* Temporary Buffer for BIG keys */
98 BUFHEAD *cpage; /* Current page */
99 int cbucket; /* Current bucket */
100 int cndx; /* Index of next item on cpage */
101 int errno; /* Error Number -- for DBM compatability */
102 int new_file; /* Indicates if fd is backing store or no */
103 int save_file; /* Indicates whether we need to flush file at
104 * exit */
105 u_long *mapp[NCACHED]; /* Pointers to page maps */
106 int nmaps; /* Initial number of bitmaps */
107 int nbufs; /* Number of buffers left to allocate */
108 BUFHEAD bufhead; /* Header of buffer lru list */
109 SEGMENT *dir; /* Hash Bucket directory */
110 } HTAB;
111
112 /*
113 * Constants
114 */
115 #define MAX_BSIZE 65536 /* 2^16 */
116 #define MIN_BUFFERS 6
117 #define MINHDRSIZE 512
118 #define DEF_BUFSIZE 65536 /* 64 K */
119 #define DEF_BUCKET_SIZE 4096
120 #define DEF_BUCKET_SHIFT 12 /* log2(BUCKET) */
121 #define DEF_SEGSIZE 256
122 #define DEF_SEGSIZE_SHIFT 8 /* log2(SEGSIZE) */
123 #define DEF_DIRSIZE 256
124 #define DEF_FFACTOR 65536
125 #define MIN_FFACTOR 4
126 #define SPLTMAX 8
127 #define CHARKEY "%$sniglet^&"
128 #define NUMKEY 1038583
129 #define BYTE_SHIFT 3
130 #define INT_TO_BYTE 2
131 #define INT_BYTE_SHIFT 5
132 #define ALL_SET ((u_int)0xFFFFFFFF)
133 #define ALL_CLEAR 0
134
135 #define PTROF(X) ((BUFHEAD *)((u_int)(X)&~0x3))
136 #define ISMOD(X) ((u_int)(X)&0x1)
137 #define DOMOD(X) ((X) = (char *)((u_int)(X)|0x1))
138 #define ISDISK(X) ((u_int)(X)&0x2)
139 #define DODISK(X) ((X) = (char *)((u_int)(X)|0x2))
140
141 #define BITS_PER_MAP 32
142
143 /* Given the address of the beginning of a big map, clear/set the nth bit */
144 #define CLRBIT(A, N) ((A)[(N)/BITS_PER_MAP] &= ~(1<<((N)%BITS_PER_MAP)))
145 #define SETBIT(A, N) ((A)[(N)/BITS_PER_MAP] |= (1<<((N)%BITS_PER_MAP)))
146 #define ISSET(A, N) ((A)[(N)/BITS_PER_MAP] & (1<<((N)%BITS_PER_MAP)))
147
148 /* Overflow management */
149 /*
150 * Overflow page numbers are allocated per split point. At each doubling of
151 * the table, we can allocate extra pages. So, an overflow page number has
152 * the top 5 bits indicate which split point and the lower 11 bits indicate
153 * which page at that split point is indicated (pages within split points are
154 * numberered starting with 1).
155 */
156
157 #define SPLITSHIFT 11
158 #define SPLITMASK 0x7FF
159 #define SPLITNUM(N) (((u_int)(N)) >> SPLITSHIFT)
160 #define OPAGENUM(N) ((N) & SPLITMASK)
161 #define OADDR_OF(S,O) ((u_int)((u_int)(S) << SPLITSHIFT) + (O))
162
163 #define BUCKET_TO_PAGE(B) \
164 (B) + hashp->HDRPAGES + ((B) ? hashp->SPARES[__log2((B)+1)-1] : 0)
165 #define OADDR_TO_PAGE(B) \
166 BUCKET_TO_PAGE ( (1 << SPLITNUM((B))) -1 ) + OPAGENUM((B));
167
168 /*
169 * page.h contains a detailed description of the page format.
170 *
171 * Normally, keys and data are accessed from offset tables in the top of
172 * each page which point to the beginning of the key and data. There are
173 * four flag values which may be stored in these offset tables which indicate
174 * the following:
175 *
176 *
177 * OVFLPAGE Rather than a key data pair, this pair contains
178 * the address of an overflow page. The format of
179 * the pair is:
180 * OVERFLOW_PAGE_NUMBER OVFLPAGE
181 *
182 * PARTIAL_KEY This must be the first key/data pair on a page
183 * and implies that page contains only a partial key.
184 * That is, the key is too big to fit on a single page
185 * so it starts on this page and continues on the next.
186 * The format of the page is:
187 * KEY_OFF PARTIAL_KEY OVFL_PAGENO OVFLPAGE
188 *
189 * KEY_OFF -- offset of the beginning of the key
190 * PARTIAL_KEY -- 1
191 * OVFL_PAGENO - page number of the next overflow page
192 * OVFLPAGE -- 0
193 *
194 * FULL_KEY This must be the first key/data pair on the page. It
195 * is used in two cases.
196 *
197 * Case 1:
198 * There is a complete key on the page but no data
199 * (because it wouldn't fit). The next page contains
200 * the data.
201 *
202 * Page format it:
203 * KEY_OFF FULL_KEY OVFL_PAGENO OVFL_PAGE
204 *
205 * KEY_OFF -- offset of the beginning of the key
206 * FULL_KEY -- 2
207 * OVFL_PAGENO - page number of the next overflow page
208 * OVFLPAGE -- 0
209 *
210 * Case 2:
211 * This page contains no key, but part of a large
212 * data field, which is continued on the next page.
213 *
214 * Page format it:
215 * DATA_OFF FULL_KEY OVFL_PAGENO OVFL_PAGE
216 *
217 * KEY_OFF -- offset of the beginning of the data on
218 * this page
219 * FULL_KEY -- 2
220 * OVFL_PAGENO - page number of the next overflow page
221 * OVFLPAGE -- 0
222 *
223 * FULL_KEY_DATA
224 * This must be the first key/data pair on the page.
225 * There are two cases:
226 *
227 * Case 1:
228 * This page contains a key and the beginning of the
229 * data field, but the data field is continued on the
230 * next page.
231 *
232 * Page format is:
233 * KEY_OFF FULL_KEY_DATA OVFL_PAGENO DATA_OFF
234 *
235 * KEY_OFF -- offset of the beginning of the key
236 * FULL_KEY_DATA -- 3
237 * OVFL_PAGENO - page number of the next overflow page
238 * DATA_OFF -- offset of the beginning of the data
239 *
240 * Case 2:
241 * This page contains the last page of a big data pair.
242 * There is no key, only the tail end of the data
243 * on this page.
244 *
245 * Page format is:
246 * DATA_OFF FULL_KEY_DATA <OVFL_PAGENO> <OVFLPAGE>
247 *
248 * DATA_OFF -- offset of the beginning of the data on
249 * this page
250 * FULL_KEY_DATA -- 3
251 * OVFL_PAGENO - page number of the next overflow page
252 * OVFLPAGE -- 0
253 *
254 * OVFL_PAGENO and OVFLPAGE are optional (they are
255 * not present if there is no next page).
256 */
257
258 #define OVFLPAGE 0
259 #define PARTIAL_KEY 1
260 #define FULL_KEY 2
261 #define FULL_KEY_DATA 3
262 #define REAL_KEY 4
263
264 /* Short hands for accessing structure */
265 #define BSIZE hdr.bsize
266 #define BSHIFT hdr.bshift
267 #define DSIZE hdr.dsize
268 #define SGSIZE hdr.ssize
269 #define SSHIFT hdr.sshift
270 #define LORDER hdr.lorder
271 #define OVFL_POINT hdr.ovfl_point
272 #define LAST_FREED hdr.last_freed
273 #define MAX_BUCKET hdr.max_bucket
274 #define FFACTOR hdr.ffactor
275 #define HIGH_MASK hdr.high_mask
276 #define LOW_MASK hdr.low_mask
277 #define NKEYS hdr.nkeys
278 #define HDRPAGES hdr.hdrpages
279 #define SPARES hdr.spares
280 #define BITMAPS hdr.bitmaps
281 #define VERSION hdr.version
282 #define MAGIC hdr.magic
283 #define NEXT_FREE hdr.next_free
284 #define H_CHARKEY hdr.h_charkey
285