hash.h revision 92905
1169689Skan/*- 2169689Skan * Copyright (c) 1990, 1993, 1994 3169689Skan * The Regents of the University of California. All rights reserved. 4169689Skan * 5169689Skan * This code is derived from software contributed to Berkeley by 6169689Skan * Margo Seltzer. 7169689Skan * 8169689Skan * Redistribution and use in source and binary forms, with or without 9169689Skan * modification, are permitted provided that the following conditions 10169689Skan * are met: 11169689Skan * 1. Redistributions of source code must retain the above copyright 12169689Skan * notice, this list of conditions and the following disclaimer. 13169689Skan * 2. Redistributions in binary form must reproduce the above copyright 14169689Skan * notice, this list of conditions and the following disclaimer in the 15169689Skan * documentation and/or other materials provided with the distribution. 16169689Skan * 3. All advertising materials mentioning features or use of this software 17169689Skan * must display the following acknowledgement: 18169689Skan * This product includes software developed by the University of 19169689Skan * California, Berkeley and its contributors. 20169689Skan * 4. Neither the name of the University nor the names of its contributors 21169689Skan * may be used to endorse or promote products derived from this software 22169689Skan * without specific prior written permission. 23169689Skan * 24169689Skan * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25169689Skan * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26169689Skan * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27169689Skan * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28169689Skan * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29169689Skan * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30169689Skan * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31169689Skan * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32169689Skan * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33169689Skan * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34169689Skan * SUCH DAMAGE. 35169689Skan * 36169689Skan * @(#)hash.h 8.3 (Berkeley) 5/31/94 37169689Skan * $FreeBSD: head/lib/libc/db/hash/hash.h 92905 2002-03-21 22:49:10Z obrien $ 38169689Skan */ 39169689Skan 40169689Skan/* Operations */ 41169689Skantypedef enum { 42169689Skan HASH_GET, HASH_PUT, HASH_PUTNEW, HASH_DELETE, HASH_FIRST, HASH_NEXT 43169689Skan} ACTION; 44169689Skan 45169689Skan/* Buffer Management structures */ 46169689Skantypedef struct _bufhead BUFHEAD; 47169689Skan 48169689Skanstruct _bufhead { 49169689Skan BUFHEAD *prev; /* LRU links */ 50169689Skan BUFHEAD *next; /* LRU links */ 51169689Skan BUFHEAD *ovfl; /* Overflow page buffer header */ 52169689Skan u_int32_t addr; /* Address of this page */ 53169689Skan char *page; /* Actual page data */ 54169689Skan char flags; 55169689Skan#define BUF_MOD 0x0001 56169689Skan#define BUF_DISK 0x0002 57169689Skan#define BUF_BUCKET 0x0004 58169689Skan#define BUF_PIN 0x0008 59169689Skan}; 60169689Skan 61169689Skan#define IS_BUCKET(X) ((X) & BUF_BUCKET) 62169689Skan 63169689Skantypedef BUFHEAD **SEGMENT; 64169689Skan 65169689Skan/* Hash Table Information */ 66169689Skantypedef struct hashhdr { /* Disk resident portion */ 67169689Skan int magic; /* Magic NO for hash tables */ 68169689Skan int version; /* Version ID */ 69169689Skan u_int32_t lorder; /* Byte Order */ 70169689Skan int bsize; /* Bucket/Page Size */ 71169689Skan int bshift; /* Bucket shift */ 72169689Skan int dsize; /* Directory Size */ 73169689Skan int ssize; /* Segment Size */ 74169689Skan int sshift; /* Segment shift */ 75169689Skan int ovfl_point; /* Where overflow pages are being 76169689Skan * allocated */ 77169689Skan int last_freed; /* Last overflow page freed */ 78169689Skan int max_bucket; /* ID of Maximum bucket in use */ 79169689Skan int high_mask; /* Mask to modulo into entire table */ 80169689Skan int low_mask; /* Mask to modulo into lower half of 81169689Skan * table */ 82169689Skan int ffactor; /* Fill factor */ 83169689Skan int nkeys; /* Number of keys in hash table */ 84169689Skan int hdrpages; /* Size of table header */ 85169689Skan int h_charkey; /* value of hash(CHARKEY) */ 86169689Skan#define NCACHED 32 /* number of bit maps and spare 87169689Skan * points */ 88169689Skan int spares[NCACHED];/* spare pages for overflow */ 89169689Skan u_int16_t bitmaps[NCACHED]; /* address of overflow page 90169689Skan * bitmaps */ 91169689Skan} HASHHDR; 92169689Skan 93169689Skantypedef struct htab { /* Memory resident data structure */ 94169689Skan HASHHDR hdr; /* Header */ 95169689Skan int nsegs; /* Number of allocated segments */ 96169689Skan int exsegs; /* Number of extra allocated 97169689Skan * segments */ 98169689Skan u_int32_t /* Hash function */ 99169689Skan (*hash)(const void *, size_t); 100169689Skan int flags; /* Flag values */ 101169689Skan int fp; /* File pointer */ 102169689Skan char *tmp_buf; /* Temporary Buffer for BIG data */ 103169689Skan char *tmp_key; /* Temporary Buffer for BIG keys */ 104169689Skan BUFHEAD *cpage; /* Current page */ 105169689Skan int cbucket; /* Current bucket */ 106169689Skan int cndx; /* Index of next item on cpage */ 107169689Skan int error; /* Error Number -- for DBM 108169689Skan * compatibility */ 109 int new_file; /* Indicates if fd is backing store 110 * or no */ 111 int save_file; /* Indicates whether we need to flush 112 * file at 113 * exit */ 114 u_int32_t *mapp[NCACHED]; /* Pointers to page maps */ 115 int nmaps; /* Initial number of bitmaps */ 116 int nbufs; /* Number of buffers left to 117 * allocate */ 118 BUFHEAD bufhead; /* Header of buffer lru list */ 119 SEGMENT *dir; /* Hash Bucket directory */ 120} HTAB; 121 122/* 123 * Constants 124 */ 125#define MAX_BSIZE 65536 /* 2^16 */ 126#define MIN_BUFFERS 6 127#define MINHDRSIZE 512 128#define DEF_BUFSIZE 65536 /* 64 K */ 129#define DEF_BUCKET_SIZE 4096 130#define DEF_BUCKET_SHIFT 12 /* log2(BUCKET) */ 131#define DEF_SEGSIZE 256 132#define DEF_SEGSIZE_SHIFT 8 /* log2(SEGSIZE) */ 133#define DEF_DIRSIZE 256 134#define DEF_FFACTOR 65536 135#define MIN_FFACTOR 4 136#define SPLTMAX 8 137#define CHARKEY "%$sniglet^&" 138#define NUMKEY 1038583 139#define BYTE_SHIFT 3 140#define INT_TO_BYTE 2 141#define INT_BYTE_SHIFT 5 142#define ALL_SET ((u_int32_t)0xFFFFFFFF) 143#define ALL_CLEAR 0 144 145#define PTROF(X) ((BUFHEAD *)((ptrdiff_t)(X)&~0x3)) 146#define ISMOD(X) ((u_int32_t)(ptrdiff_t)(X)&0x1) 147#define DOMOD(X) ((X) = (char *)((ptrdiff_t)(X)|0x1)) 148#define ISDISK(X) ((u_int32_t)(ptrdiff_t)(X)&0x2) 149#define DODISK(X) ((X) = (char *)((ptrdiff_t)(X)|0x2)) 150 151#define BITS_PER_MAP 32 152 153/* Given the address of the beginning of a big map, clear/set the nth bit */ 154#define CLRBIT(A, N) ((A)[(N)/BITS_PER_MAP] &= ~(1<<((N)%BITS_PER_MAP))) 155#define SETBIT(A, N) ((A)[(N)/BITS_PER_MAP] |= (1<<((N)%BITS_PER_MAP))) 156#define ISSET(A, N) ((A)[(N)/BITS_PER_MAP] & (1<<((N)%BITS_PER_MAP))) 157 158/* Overflow management */ 159/* 160 * Overflow page numbers are allocated per split point. At each doubling of 161 * the table, we can allocate extra pages. So, an overflow page number has 162 * the top 5 bits indicate which split point and the lower 11 bits indicate 163 * which page at that split point is indicated (pages within split points are 164 * numberered starting with 1). 165 */ 166 167#define SPLITSHIFT 11 168#define SPLITMASK 0x7FF 169#define SPLITNUM(N) (((u_int32_t)(N)) >> SPLITSHIFT) 170#define OPAGENUM(N) ((N) & SPLITMASK) 171#define OADDR_OF(S,O) ((u_int32_t)((u_int32_t)(S) << SPLITSHIFT) + (O)) 172 173#define BUCKET_TO_PAGE(B) \ 174 (B) + hashp->HDRPAGES + ((B) ? hashp->SPARES[__log2((B)+1)-1] : 0) 175#define OADDR_TO_PAGE(B) \ 176 BUCKET_TO_PAGE ( (1 << SPLITNUM((B))) -1 ) + OPAGENUM((B)); 177 178/* 179 * page.h contains a detailed description of the page format. 180 * 181 * Normally, keys and data are accessed from offset tables in the top of 182 * each page which point to the beginning of the key and data. There are 183 * four flag values which may be stored in these offset tables which indicate 184 * the following: 185 * 186 * 187 * OVFLPAGE Rather than a key data pair, this pair contains 188 * the address of an overflow page. The format of 189 * the pair is: 190 * OVERFLOW_PAGE_NUMBER OVFLPAGE 191 * 192 * PARTIAL_KEY This must be the first key/data pair on a page 193 * and implies that page contains only a partial key. 194 * That is, the key is too big to fit on a single page 195 * so it starts on this page and continues on the next. 196 * The format of the page is: 197 * KEY_OFF PARTIAL_KEY OVFL_PAGENO OVFLPAGE 198 * 199 * KEY_OFF -- offset of the beginning of the key 200 * PARTIAL_KEY -- 1 201 * OVFL_PAGENO - page number of the next overflow page 202 * OVFLPAGE -- 0 203 * 204 * FULL_KEY This must be the first key/data pair on the page. It 205 * is used in two cases. 206 * 207 * Case 1: 208 * There is a complete key on the page but no data 209 * (because it wouldn't fit). The next page contains 210 * the data. 211 * 212 * Page format it: 213 * KEY_OFF FULL_KEY OVFL_PAGENO OVFL_PAGE 214 * 215 * KEY_OFF -- offset of the beginning of the key 216 * FULL_KEY -- 2 217 * OVFL_PAGENO - page number of the next overflow page 218 * OVFLPAGE -- 0 219 * 220 * Case 2: 221 * This page contains no key, but part of a large 222 * data field, which is continued on the next page. 223 * 224 * Page format it: 225 * DATA_OFF FULL_KEY OVFL_PAGENO OVFL_PAGE 226 * 227 * KEY_OFF -- offset of the beginning of the data on 228 * this page 229 * FULL_KEY -- 2 230 * OVFL_PAGENO - page number of the next overflow page 231 * OVFLPAGE -- 0 232 * 233 * FULL_KEY_DATA 234 * This must be the first key/data pair on the page. 235 * There are two cases: 236 * 237 * Case 1: 238 * This page contains a key and the beginning of the 239 * data field, but the data field is continued on the 240 * next page. 241 * 242 * Page format is: 243 * KEY_OFF FULL_KEY_DATA OVFL_PAGENO DATA_OFF 244 * 245 * KEY_OFF -- offset of the beginning of the key 246 * FULL_KEY_DATA -- 3 247 * OVFL_PAGENO - page number of the next overflow page 248 * DATA_OFF -- offset of the beginning of the data 249 * 250 * Case 2: 251 * This page contains the last page of a big data pair. 252 * There is no key, only the tail end of the data 253 * on this page. 254 * 255 * Page format is: 256 * DATA_OFF FULL_KEY_DATA <OVFL_PAGENO> <OVFLPAGE> 257 * 258 * DATA_OFF -- offset of the beginning of the data on 259 * this page 260 * FULL_KEY_DATA -- 3 261 * OVFL_PAGENO - page number of the next overflow page 262 * OVFLPAGE -- 0 263 * 264 * OVFL_PAGENO and OVFLPAGE are optional (they are 265 * not present if there is no next page). 266 */ 267 268#define OVFLPAGE 0 269#define PARTIAL_KEY 1 270#define FULL_KEY 2 271#define FULL_KEY_DATA 3 272#define REAL_KEY 4 273 274/* Short hands for accessing structure */ 275#define BSIZE hdr.bsize 276#define BSHIFT hdr.bshift 277#define DSIZE hdr.dsize 278#define SGSIZE hdr.ssize 279#define SSHIFT hdr.sshift 280#define LORDER hdr.lorder 281#define OVFL_POINT hdr.ovfl_point 282#define LAST_FREED hdr.last_freed 283#define MAX_BUCKET hdr.max_bucket 284#define FFACTOR hdr.ffactor 285#define HIGH_MASK hdr.high_mask 286#define LOW_MASK hdr.low_mask 287#define NKEYS hdr.nkeys 288#define HDRPAGES hdr.hdrpages 289#define SPARES hdr.spares 290#define BITMAPS hdr.bitmaps 291#define VERSION hdr.version 292#define MAGIC hdr.magic 293#define NEXT_FREE hdr.next_free 294#define H_CHARKEY hdr.h_charkey 295