xref: /original-bsd/lib/libc/db/hash/hash.h (revision 95a66346)
1 /*-
2  * Copyright (c) 1990 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Margo Seltzer.
7  *
8  * %sccs.include.redist.c%
9  *
10  *	@(#)hash.h	5.4 (Berkeley) 03/12/91
11  */
12 
13 /* Operations */
14 typedef enum { HASH_GET, HASH_PUT, HASH_PUTNEW, HASH_DELETE,
15 		HASH_FIRST, HASH_NEXT } ACTION;
16 
17 /* Buffer Management structures */
18 typedef struct _bufhead BUFHEAD;
19 
20 struct _bufhead {
21     BUFHEAD	*prev;		/* LRU links */
22     BUFHEAD	*next;		/* LRU links */
23     BUFHEAD	*ovfl;		/* Overflow page buffer header */
24     u_int	 addr;		/* Address of this page */
25     char	*page;		/* Actual page data */
26     char	flags;
27 #define	BUF_MOD		0x0001
28 #define BUF_DISK	0x0002
29 #define	BUF_BUCKET	0x0004
30 #define	BUF_PIN		0x0008
31 };
32 
33 
34 #define IS_BUCKET(X)	(X & BUF_BUCKET)
35 
36 typedef BUFHEAD	**SEGMENT;
37 
38 /* Hash Table Information */
39 typedef struct hashhdr {	/* Disk resident portion */
40 	int magic;	/* Magic NO for hash tables */
41 	int version;	/* Version ID */
42 	long lorder;	/* Byte Order */
43 	int bsize;	/* Bucket/Page Size */
44 	int bshift;	/* Bucket shift */
45 	int dsize;	/* Directory Size */
46 	int ssize;	/* Segment Size */
47 	int sshift;	/* Segment shift */
48 	int max_bucket;	/* ID of Maximum bucket in use */
49 	int high_mask;	/* Mask to modulo into entire table */
50 	int low_mask;	/* Mask to modulo into lower half of table */
51 	int ffactor;	/* Fill factor */
52 	int nkeys;	/* Number of keys in hash table */
53 	int hdrpages;	/* Size of table header */
54 	int h_charkey;	/* value of hash(CHARKEY) */
55 # define NCACHED		32	/* number of bit maps and spare points*/
56 	int spares[NCACHED];	/* spare pages for overflow */
57 	u_short bitmaps[NCACHED];	/* address of overflow page bitmaps */
58 } HASHHDR;
59 
60 typedef struct htab {	/* Memory resident data structure */
61 	HASHHDR hdr;	/* Header */
62 	int nsegs;	/* Number of allocated segments */
63 	int exsegs;	/* Number of extra allocated segments */
64 	int (*hash)();	/* Hash Function */
65 	int flags;	/* Flag values */
66 	int fp;		/* File pointer */
67 	char *tmp_buf;	/* Temporary Buffer for BIG data */
68 	char *tmp_key;	/* Temporary Buffer for BIG keys */
69 	BUFHEAD *cpage;	/* Current page */
70 	int cbucket;	/* Current bucket */
71 	int cndx;  	/* Index of next item on cpage */
72 	int errno;	/* Error Number -- for DBM compatability */
73 	int new_file;	/* Indicates whether fd is backing store or no */
74 	int save_file;	/* Indicates whether we need to flush file at exit */
75 	u_long *mapp[NCACHED];	/* Pointers to page maps */
76 	int nmaps;	/* Initial number of bitmaps */
77 	int nbufs;	/* Number of buffers left to allocate */
78 	BUFHEAD	bufhead; /* Header of buffer lru list */
79 	SEGMENT	 *dir;	/* Hash Bucket directory */
80 } HTAB;
81 
82 
83 /*
84  * Constants
85  */
86 #define	MAX_BSIZE		65536	/* 2^16 */
87 #define MIN_BUFFERS		6
88 #define MINHDRSIZE		512
89 #define DEF_BUFSIZE		65536	/* 64 K */
90 #define DEF_BUCKET_SIZE	256
91 #define DEF_BUCKET_SHIFT	8	/* log2(BUCKET) */
92 #define DEF_SEGSIZE		256
93 #define DEF_SEGSIZE_SHIFT		8      /* log2(SEGSIZE)	 */
94 #define DEF_DIRSIZE		256
95 #define DEF_FFACTOR		5
96 #define SPLTMAX		8
97 #define CHARKEY		"%$sniglet^&"
98 #define NUMKEY			1038583
99 #define VERSION_NO		3
100 #define BYTE_SHIFT		3
101 #define INT_TO_BYTE		2
102 #define INT_BYTE_SHIFT		5
103 #define ALL_SET		((unsigned)0xFFFFFFFF)
104 #define ALL_CLEAR		0
105 
106 
107 #define PTROF(X)	((BUFHEAD *)((unsigned)(X)&~0x3))
108 #define ISMOD(X)	((unsigned)(X)&0x1)
109 #define DOMOD(X)	(X = (char *)( (unsigned)X | 0x1))
110 #define ISDISK(X)	((unsigned)(X)&0x2)
111 #define DODISK(X)	(X = (char *)( (unsigned)X | 0x2))
112 
113 #define BITS_PER_MAP    32
114 
115 /* Given the address of the beginning of a big map, clear/set the nth bit */
116 
117 #define CLRBIT(A,N) ((A)[N/BITS_PER_MAP] &= ~(1<<(N%BITS_PER_MAP)))
118 #define SETBIT(A,N) ((A)[N/BITS_PER_MAP] |= (1<<(N%BITS_PER_MAP)))
119 #define ISSET(A,N) ((A)[N/BITS_PER_MAP] & (1<<(N%BITS_PER_MAP)))
120 
121 /* Overflow management */
122 /*
123 	Overflow page numbers are allocated per split point.
124 	At each doubling of the table, we can allocate extra
125 	pages.  So, an overflow page number has the top 5 bits
126 	indicate which split point and the lower 11 bits indicate
127 	which page at that split point is indicated (pages within
128 	split points are numberered starting with 1).
129 
130 
131 */
132 
133 #define SPLITSHIFT	11
134 #define SPLITMASK	0x7FF
135 #define SPLITNUM(N)	(((unsigned)N) >> SPLITSHIFT)
136 #define OPAGENUM(N)	(N & SPLITMASK)
137 #define	OADDR_OF(S,O)	((unsigned)((unsigned)S << SPLITSHIFT) + O)
138 
139 #define BUCKET_TO_PAGE(B) \
140 	B + hashp->HDRPAGES + (B ? hashp->SPARES[__log2(B+1)-1] : 0)
141 #define OADDR_TO_PAGE(B) 	\
142 	BUCKET_TO_PAGE ( (1 << SPLITNUM(B)) -1 ) + OPAGENUM(B);
143 
144 /*
145     page.h contains a detailed description of the page format.
146 
147     Normally, keys and data are accessed from offset tables in the
148     top of each page which point to the beginning of the key and
149     data.  There are four flag values which may be stored in these
150     offset tables which indicate the following:
151 
152 	OVFLPAGE	Rather than a key data pair, this pair contains
153 			the address of an overflow page.  The format of
154 			the pair is:
155 			    OVERFLOW_PAGE_NUMBER OVFLPAGE
156 
157 	PARTIAL_KEY	This must be the first key/data pair on a page
158 			and implies that page contains only a partial key.
159 			That is, the key is too big to fit on a single page
160 			so it starts on this page and continues on the next.
161 			The format of the page is:
162 			    KEY_OFF PARTIAL_KEY OVFL_PAGENO OVFLPAGE
163 
164 			    KEY_OFF -- offset of the beginning of the key
165 			    PARTIAL_KEY -- 1
166 			    OVFL_PAGENO - page number of the next overflow page
167 			    OVFLPAGE -- 0
168 	FULL_KEY	This must be the first key/data pair on the page.  It
169 			is used in two cases.
170 
171 			Case 1:
172 			    There is a complete key on the page but no data
173 			    (because it wouldn't fit).  The next page contains
174 			    the data.
175 
176 			    Page format it:
177 			    KEY_OFF FULL_KEY OVFL_PAGENO OVFL_PAGE
178 
179 			    KEY_OFF -- offset of the beginning of the key
180 			    FULL_KEY -- 2
181 			    OVFL_PAGENO - page number of the next overflow page
182 			    OVFLPAGE -- 0
183 
184 			Case 2:
185 			    This page contains no key, but part of a large
186 			    data field, which is continued on the next page.
187 
188 			    Page format it:
189 			    DATA_OFF FULL_KEY OVFL_PAGENO OVFL_PAGE
190 
191 			    KEY_OFF -- offset of the beginning of the data on
192 					this page
193 			    FULL_KEY -- 2
194 			    OVFL_PAGENO - page number of the next overflow page
195 			    OVFLPAGE -- 0
196 
197 	FULL_KEY_DATA	This must be the first key/data pair on the page.
198 			There are two cases:
199 
200 			Case 1:
201 			    This page contains a key and the beginning of the
202 			    data field, but the data field is continued on the
203 			    next page.
204 
205 			    Page format is:
206 			    KEY_OFF FULL_KEY_DATA OVFL_PAGENO DATA_OFF
207 
208 			    KEY_OFF -- offset of the beginning of the key
209 			    FULL_KEY_DATA -- 3
210 			    OVFL_PAGENO - page number of the next overflow page
211 			    DATA_OFF -- offset of the beginning of the data
212 
213 			Case 2:
214 			    This page contains the last page of a big data pair.
215 			    There is no key, only the  tail end of the data
216 			    on this page.
217 
218 			    Page format is:
219 			    DATA_OFF FULL_KEY_DATA <OVFL_PAGENO> <OVFLPAGE>
220 
221 			    DATA_OFF -- offset of the beginning of the data on
222 					this page
223 			    FULL_KEY_DATA -- 3
224 			    OVFL_PAGENO - page number of the next overflow page
225 			    OVFLPAGE -- 0
226 
227 			    OVFL_PAGENO and OVFLPAGE are optional (they are
228 			    not present if there is no next page).
229 */
230 
231 #define OVFLPAGE	0
232 #define PARTIAL_KEY	1
233 #define FULL_KEY	2
234 #define FULL_KEY_DATA	3
235 #define	REAL_KEY	4
236 /* Short hands for accessing structure */
237 #define BSIZE	hdr.bsize
238 #define BSHIFT	hdr.bshift
239 #define DSIZE	hdr.dsize
240 #define SGSIZE	hdr.ssize
241 #define SSHIFT	hdr.sshift
242 #define LORDER	hdr.lorder
243 #define MAX_BUCKET	hdr.max_bucket
244 #define FFACTOR		hdr.ffactor
245 #define HIGH_MASK	hdr.high_mask
246 #define LOW_MASK	hdr.low_mask
247 #define NKEYS		hdr.nkeys
248 #define HDRPAGES	hdr.hdrpages
249 #define SPARES		hdr.spares
250 #define BITMAPS		hdr.bitmaps
251 #define VERSION		hdr.version
252 #define MAGIC		hdr.magic
253 #define NEXT_FREE	hdr.next_free
254 #define H_CHARKEY	hdr.h_charkey
255