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 * %sccs.include.redist.c% 9 * 10 * @(#)hash.h 8.1 (Berkeley) 06/04/93 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 4096 93 #define DEF_BUCKET_SHIFT 12 /* log2(BUCKET) */ 94 #define DEF_SEGSIZE 256 95 #define DEF_SEGSIZE_SHIFT 8 /* log2(SEGSIZE) */ 96 #define DEF_DIRSIZE 256 97 #define DEF_FFACTOR 65536 98 #define MIN_FFACTOR 4 99 #define SPLTMAX 8 100 #define CHARKEY "%$sniglet^&" 101 #define NUMKEY 1038583 102 #define BYTE_SHIFT 3 103 #define INT_TO_BYTE 2 104 #define INT_BYTE_SHIFT 5 105 #define ALL_SET ((u_int)0xFFFFFFFF) 106 #define ALL_CLEAR 0 107 108 #define PTROF(X) ((BUFHEAD *)((u_int)(X)&~0x3)) 109 #define ISMOD(X) ((u_int)(X)&0x1) 110 #define DOMOD(X) ((X) = (char *)((u_int)(X)|0x1)) 111 #define ISDISK(X) ((u_int)(X)&0x2) 112 #define DODISK(X) ((X) = (char *)((u_int)(X)|0x2)) 113 114 #define BITS_PER_MAP 32 115 116 /* Given the address of the beginning of a big map, clear/set the nth bit */ 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. At each doubling of 124 * the table, we can allocate extra pages. So, an overflow page number has 125 * the top 5 bits indicate which split point and the lower 11 bits indicate 126 * which page at that split point is indicated (pages within split points are 127 * numberered starting with 1). 128 */ 129 130 #define SPLITSHIFT 11 131 #define SPLITMASK 0x7FF 132 #define SPLITNUM(N) (((u_int)(N)) >> SPLITSHIFT) 133 #define OPAGENUM(N) ((N) & SPLITMASK) 134 #define OADDR_OF(S,O) ((u_int)((u_int)(S) << SPLITSHIFT) + (O)) 135 136 #define BUCKET_TO_PAGE(B) \ 137 (B) + hashp->HDRPAGES + ((B) ? hashp->SPARES[__log2((B)+1)-1] : 0) 138 #define OADDR_TO_PAGE(B) \ 139 BUCKET_TO_PAGE ( (1 << SPLITNUM((B))) -1 ) + OPAGENUM((B)); 140 141 /* 142 * page.h contains a detailed description of the page format. 143 * 144 * Normally, keys and data are accessed from offset tables in the top of 145 * each page which point to the beginning of the key and data. There are 146 * four flag values which may be stored in these offset tables which indicate 147 * the following: 148 * 149 * 150 * OVFLPAGE Rather than a key data pair, this pair contains 151 * the address of an overflow page. The format of 152 * the pair is: 153 * OVERFLOW_PAGE_NUMBER OVFLPAGE 154 * 155 * PARTIAL_KEY This must be the first key/data pair on a page 156 * and implies that page contains only a partial key. 157 * That is, the key is too big to fit on a single page 158 * so it starts on this page and continues on the next. 159 * The format of the page is: 160 * KEY_OFF PARTIAL_KEY OVFL_PAGENO OVFLPAGE 161 * 162 * KEY_OFF -- offset of the beginning of the key 163 * PARTIAL_KEY -- 1 164 * OVFL_PAGENO - page number of the next overflow page 165 * OVFLPAGE -- 0 166 * 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 197 * 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 237 /* Short hands for accessing structure */ 238 #define BSIZE hdr.bsize 239 #define BSHIFT hdr.bshift 240 #define DSIZE hdr.dsize 241 #define SGSIZE hdr.ssize 242 #define SSHIFT hdr.sshift 243 #define LORDER hdr.lorder 244 #define OVFL_POINT hdr.ovfl_point 245 #define LAST_FREED hdr.last_freed 246 #define MAX_BUCKET hdr.max_bucket 247 #define FFACTOR hdr.ffactor 248 #define HIGH_MASK hdr.high_mask 249 #define LOW_MASK hdr.low_mask 250 #define NKEYS hdr.nkeys 251 #define HDRPAGES hdr.hdrpages 252 #define SPARES hdr.spares 253 #define BITMAPS hdr.bitmaps 254 #define VERSION hdr.version 255 #define MAGIC hdr.magic 256 #define NEXT_FREE hdr.next_free 257 #define H_CHARKEY hdr.h_charkey 258