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.1 (Berkeley) 02/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 int addr; /* Address of this page */ 25 char *page; /* Actual page data */ 26 char flags; /* Combination of BUF_MOD, BUF_DISK, BUF_BUCKET */ 27 }; 28 29 /* Flag Values */ 30 #define BUF_MOD 0x0001 31 #define BUF_DISK 0x0002 32 #define BUF_BUCKET 0x0004 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 nbufs; /* Number of buffers left to allocate */ 77 BUFHEAD bufhead; /* Header of buffer lru list */ 78 SEGMENT *dir; /* Hash Bucket directory */ 79 } HTAB; 80 81 82 /* 83 * Constants 84 */ 85 #define MAX_BSIZE 65536 /* 2^16 */ 86 #define MIN_BUFFERS 6 87 #define MINHDRSIZE 512 88 #define DEF_BUFSIZE 65536 /* 64 K */ 89 #define DEF_BUCKET_SIZE 256 90 #define DEF_BUCKET_SHIFT 8 /* log2(BUCKET) */ 91 #define DEF_SEGSIZE 256 92 #define DEF_SEGSIZE_SHIFT 8 /* log2(SEGSIZE) */ 93 #define DEF_DIRSIZE 256 94 #define DEF_FFACTOR 5 95 #define SPLTMAX 8 96 #define CHARKEY "%$sniglet^&" 97 #define NUMKEY 1038583 98 #define VERSION_NO 3 99 #define BYTE_SHIFT 3 100 #define INT_TO_BYTE 2 101 #define INT_BYTE_SHIFT 5 102 #define ALL_SET ((unsigned)0xFFFFFFFF) 103 #define ALL_CLEAR 0 104 105 106 #define PTROF(X) ((BUFHEAD *)((unsigned)(X)&~0x3)) 107 #define ISMOD(X) ((unsigned)(X)&0x1) 108 #define DOMOD(X) (X = (char *)( (unsigned)X | 0x1)) 109 #define ISDISK(X) ((unsigned)(X)&0x2) 110 #define DODISK(X) (X = (char *)( (unsigned)X | 0x2)) 111 112 #define BITS_PER_MAP 32 113 114 /* Given the address of the beginning of a big map, clear/set the nth bit */ 115 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. 123 At each doubling of the table, we can allocate extra 124 pages. So, an overflow page number has the top 5 bits 125 indicate which split point and the lower 11 bits indicate 126 which page at that split point is indicated (pages within 127 split points are numberered starting with 1). 128 129 130 */ 131 132 #define SPLITSHIFT 11 133 #define SPLITMASK 0x7FF 134 #define SPLITNUM(N) (((unsigned)N) >> SPLITSHIFT) 135 #define OPAGENUM(N) (N & SPLITMASK) 136 #define OADDR_OF(S,O) ((S << SPLITSHIFT) + O) 137 138 #define BUCKET_TO_PAGE(B) \ 139 B + hashp->HDRPAGES + (B ? hashp->SPARES[__log2(B+1)-1] : 0) 140 #define OADDR_TO_PAGE(B) \ 141 BUCKET_TO_PAGE ( (1 << SPLITNUM(B)) -1 ) + OPAGENUM(B); 142 143 /* 144 page.h contains a detailed description of the page format. 145 146 Normally, keys and data are accessed from offset tables in the 147 top of each page which point to the beginning of the key and 148 data. There are four flag values which may be stored in these 149 offset tables which indicate the following: 150 151 OVFLPAGE Rather than a key data pair, this pair contains 152 the address of an overflow page. The format of 153 the pair is: 154 OVERFLOW_PAGE_NUMBER OVFLPAGE 155 156 PARTIAL_KEY This must be the first key/data pair on a page 157 and implies that page contains only a partial key. 158 That is, the key is too big to fit on a single page 159 so it starts on this page and continues on the next. 160 The format of the page is: 161 KEY_OFF PARTIAL_KEY OVFL_PAGENO OVFLPAGE 162 163 KEY_OFF -- offset of the beginning of the key 164 PARTIAL_KEY -- 1 165 OVFL_PAGENO - page number of the next overflow page 166 OVFLPAGE -- 0 167 FULL_KEY This must be the first key/data pair on the page. It 168 is used in two cases. 169 170 Case 1: 171 There is a complete key on the page but no data 172 (because it wouldn't fit). The next page contains 173 the data. 174 175 Page format it: 176 KEY_OFF FULL_KEY OVFL_PAGENO OVFL_PAGE 177 178 KEY_OFF -- offset of the beginning of the key 179 FULL_KEY -- 2 180 OVFL_PAGENO - page number of the next overflow page 181 OVFLPAGE -- 0 182 183 Case 2: 184 This page contains no key, but part of a large 185 data field, which is continued on the next page. 186 187 Page format it: 188 DATA_OFF FULL_KEY OVFL_PAGENO OVFL_PAGE 189 190 KEY_OFF -- offset of the beginning of the data on 191 this page 192 FULL_KEY -- 2 193 OVFL_PAGENO - page number of the next overflow page 194 OVFLPAGE -- 0 195 196 FULL_KEY_DATA 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 #define OVFLPAGE 0 230 #define PARTIAL_KEY 1 231 #define FULL_KEY 2 232 #define FULL_KEY_DATA 3 233 #define REAL_KEY 4 234 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 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