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