hash.h 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /*-
  2. * Copyright (c) 1990, 1993, 1994
  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. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. * 3. All advertising materials mentioning features or use of this software
  17. * must display the following acknowledgement:
  18. * This product includes software developed by the University of
  19. * California, Berkeley and its contributors.
  20. * 4. Neither the name of the University nor the names of its contributors
  21. * may be used to endorse or promote products derived from this software
  22. * without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  25. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  28. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  29. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  30. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  31. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  32. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  33. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  34. * SUCH DAMAGE.
  35. *
  36. * @(#)hash.h 8.3 (Berkeley) 5/31/94
  37. */
  38. /* Operations */
  39. typedef enum {
  40. HASH_GET, HASH_PUT, HASH_PUTNEW, HASH_DELETE, HASH_FIRST, HASH_NEXT
  41. } ACTION;
  42. /* Buffer Management structures */
  43. typedef struct _bufhead BUFHEAD;
  44. struct _bufhead {
  45. BUFHEAD *prev; /* LRU links */
  46. BUFHEAD *next; /* LRU links */
  47. BUFHEAD *ovfl; /* Overflow page buffer header */
  48. u_int32_t addr; /* Address of this page */
  49. char *page; /* Actual page data */
  50. char flags;
  51. #define BUF_MOD 0x0001
  52. #define BUF_DISK 0x0002
  53. #define BUF_BUCKET 0x0004
  54. #define BUF_PIN 0x0008
  55. };
  56. #define IS_BUCKET(X) ((X) & BUF_BUCKET)
  57. typedef BUFHEAD **SEGMENT;
  58. /* Hash Table Information */
  59. typedef struct hashhdr { /* Disk resident portion */
  60. int magic; /* Magic NO for hash tables */
  61. int version; /* Version ID */
  62. u_int32_t lorder; /* Byte Order */
  63. int bsize; /* Bucket/Page Size */
  64. int bshift; /* Bucket shift */
  65. int dsize; /* Directory Size */
  66. int ssize; /* Segment Size */
  67. int sshift; /* Segment shift */
  68. int ovfl_point; /* Where overflow pages are being
  69. * allocated */
  70. int last_freed; /* Last overflow page freed */
  71. int max_bucket; /* ID of Maximum bucket in use */
  72. int high_mask; /* Mask to modulo into entire table */
  73. int low_mask; /* Mask to modulo into lower half of
  74. * table */
  75. int ffactor; /* Fill factor */
  76. int nkeys; /* Number of keys in hash table */
  77. int hdrpages; /* Size of table header */
  78. int h_charkey; /* value of hash(CHARKEY) */
  79. #define NCACHED 32 /* number of bit maps and spare
  80. * points */
  81. int spares[NCACHED];/* spare pages for overflow */
  82. u_int16_t bitmaps[NCACHED]; /* address of overflow page
  83. * bitmaps */
  84. } HASHHDR;
  85. typedef struct htab { /* Memory resident data structure */
  86. HASHHDR hdr; /* Header */
  87. int nsegs; /* Number of allocated segments */
  88. int exsegs; /* Number of extra allocated
  89. * segments */
  90. u_int32_t /* Hash function */
  91. (*hash)__P((const void *, size_t));
  92. int flags; /* Flag values */
  93. int fp; /* File pointer */
  94. char *tmp_buf; /* Temporary Buffer for BIG data */
  95. char *tmp_key; /* Temporary Buffer for BIG keys */
  96. BUFHEAD *cpage; /* Current page */
  97. int cbucket; /* Current bucket */
  98. int cndx; /* Index of next item on cpage */
  99. int errnum; /* Error Number -- for DBM
  100. * compatibility */
  101. int new_file; /* Indicates if fd is backing store
  102. * or no */
  103. int save_file; /* Indicates whether we need to flush
  104. * file at
  105. * exit */
  106. u_int32_t *mapp[NCACHED]; /* Pointers to page maps */
  107. int nmaps; /* Initial number of bitmaps */
  108. int nbufs; /* Number of buffers left to
  109. * allocate */
  110. BUFHEAD bufhead; /* Header of buffer lru list */
  111. SEGMENT *dir; /* Hash Bucket directory */
  112. } HTAB;
  113. /*
  114. * Constants
  115. */
  116. #define MAX_BSIZE 65536 /* 2^16 */
  117. #define MIN_BUFFERS 6
  118. #define MINHDRSIZE 512
  119. #define DEF_BUFSIZE 65536 /* 64 K */
  120. #define DEF_BUCKET_SIZE 4096
  121. #define DEF_BUCKET_SHIFT 12 /* log2(BUCKET) */
  122. #define DEF_SEGSIZE 256
  123. #define DEF_SEGSIZE_SHIFT 8 /* log2(SEGSIZE) */
  124. #define DEF_DIRSIZE 256
  125. #define DEF_FFACTOR 65536
  126. #define MIN_FFACTOR 4
  127. #define SPLTMAX 8
  128. #define CHARKEY "%$sniglet^&"
  129. #define NUMKEY 1038583
  130. #define BYTE_SHIFT 3
  131. #define INT_TO_BYTE 2
  132. #define INT_BYTE_SHIFT 5
  133. #define ALL_SET ((u_int32_t)0xFFFFFFFF)
  134. #define ALL_CLEAR 0
  135. #define PTROF(X) ((BUFHEAD *)((ptrdiff_t)(X)&~0x3))
  136. #define ISMOD(X) ((u_int32_t)(ptrdiff_t)(X)&0x1)
  137. #define DOMOD(X) ((X) = (char *)((ptrdiff_t)(X)|0x1))
  138. #define ISDISK(X) ((u_int32_t)(ptrdiff_t)(X)&0x2)
  139. #define DODISK(X) ((X) = (char *)((ptrdiff_t)(X)|0x2))
  140. #define BITS_PER_MAP 32
  141. /* Given the address of the beginning of a big map, clear/set the nth bit */
  142. #define CLRBIT(A, N) ((A)[(N)/BITS_PER_MAP] &= ~(1<<((N)%BITS_PER_MAP)))
  143. #define SETBIT(A, N) ((A)[(N)/BITS_PER_MAP] |= (1<<((N)%BITS_PER_MAP)))
  144. #define ISSET(A, N) ((A)[(N)/BITS_PER_MAP] & (1<<((N)%BITS_PER_MAP)))
  145. /* Overflow management */
  146. /*
  147. * Overflow page numbers are allocated per split point. At each doubling of
  148. * the table, we can allocate extra pages. So, an overflow page number has
  149. * the top 5 bits indicate which split point and the lower 11 bits indicate
  150. * which page at that split point is indicated (pages within split points are
  151. * numberered starting with 1).
  152. */
  153. #define SPLITSHIFT 11
  154. #define SPLITMASK 0x7FF
  155. #define SPLITNUM(N) (((u_int32_t)(N)) >> SPLITSHIFT)
  156. #define OPAGENUM(N) ((N) & SPLITMASK)
  157. #define OADDR_OF(S,O) ((u_int32_t)((u_int32_t)(S) << SPLITSHIFT) + (O))
  158. #define BUCKET_TO_PAGE(B) \
  159. (B) + hashp->HDRPAGES + ((B) ? hashp->SPARES[__hash_log2((B)+1)-1] : 0)
  160. #define OADDR_TO_PAGE(B) \
  161. BUCKET_TO_PAGE ( (1 << SPLITNUM((B))) -1 ) + OPAGENUM((B));
  162. /*
  163. * page.h contains a detailed description of the page format.
  164. *
  165. * Normally, keys and data are accessed from offset tables in the top of
  166. * each page which point to the beginning of the key and data. There are
  167. * four flag values which may be stored in these offset tables which indicate
  168. * the following:
  169. *
  170. *
  171. * OVFLPAGE Rather than a key data pair, this pair contains
  172. * the address of an overflow page. The format of
  173. * the pair is:
  174. * OVERFLOW_PAGE_NUMBER OVFLPAGE
  175. *
  176. * PARTIAL_KEY This must be the first key/data pair on a page
  177. * and implies that page contains only a partial key.
  178. * That is, the key is too big to fit on a single page
  179. * so it starts on this page and continues on the next.
  180. * The format of the page is:
  181. * KEY_OFF PARTIAL_KEY OVFL_PAGENO OVFLPAGE
  182. *
  183. * KEY_OFF -- offset of the beginning of the key
  184. * PARTIAL_KEY -- 1
  185. * OVFL_PAGENO - page number of the next overflow page
  186. * OVFLPAGE -- 0
  187. *
  188. * FULL_KEY This must be the first key/data pair on the page. It
  189. * is used in two cases.
  190. *
  191. * Case 1:
  192. * There is a complete key on the page but no data
  193. * (because it wouldn't fit). The next page contains
  194. * the data.
  195. *
  196. * Page format it:
  197. * KEY_OFF FULL_KEY OVFL_PAGENO OVFL_PAGE
  198. *
  199. * KEY_OFF -- offset of the beginning of the key
  200. * FULL_KEY -- 2
  201. * OVFL_PAGENO - page number of the next overflow page
  202. * OVFLPAGE -- 0
  203. *
  204. * Case 2:
  205. * This page contains no key, but part of a large
  206. * data field, which is continued on the next page.
  207. *
  208. * Page format it:
  209. * DATA_OFF FULL_KEY OVFL_PAGENO OVFL_PAGE
  210. *
  211. * KEY_OFF -- offset of the beginning of the data on
  212. * this page
  213. * FULL_KEY -- 2
  214. * OVFL_PAGENO - page number of the next overflow page
  215. * OVFLPAGE -- 0
  216. *
  217. * FULL_KEY_DATA
  218. * This must be the first key/data pair on the page.
  219. * There are two cases:
  220. *
  221. * Case 1:
  222. * This page contains a key and the beginning of the
  223. * data field, but the data field is continued on the
  224. * next page.
  225. *
  226. * Page format is:
  227. * KEY_OFF FULL_KEY_DATA OVFL_PAGENO DATA_OFF
  228. *
  229. * KEY_OFF -- offset of the beginning of the key
  230. * FULL_KEY_DATA -- 3
  231. * OVFL_PAGENO - page number of the next overflow page
  232. * DATA_OFF -- offset of the beginning of the data
  233. *
  234. * Case 2:
  235. * This page contains the last page of a big data pair.
  236. * There is no key, only the tail end of the data
  237. * on this page.
  238. *
  239. * Page format is:
  240. * DATA_OFF FULL_KEY_DATA <OVFL_PAGENO> <OVFLPAGE>
  241. *
  242. * DATA_OFF -- offset of the beginning of the data on
  243. * this page
  244. * FULL_KEY_DATA -- 3
  245. * OVFL_PAGENO - page number of the next overflow page
  246. * OVFLPAGE -- 0
  247. *
  248. * OVFL_PAGENO and OVFLPAGE are optional (they are
  249. * not present if there is no next page).
  250. */
  251. #define OVFLPAGE 0
  252. #define PARTIAL_KEY 1
  253. #define FULL_KEY 2
  254. #define FULL_KEY_DATA 3
  255. #define REAL_KEY 4
  256. /* Short hands for accessing structure */
  257. #define BSIZE hdr.bsize
  258. #define BSHIFT hdr.bshift
  259. #define DSIZE hdr.dsize
  260. #define SGSIZE hdr.ssize
  261. #define SSHIFT hdr.sshift
  262. #define LORDER hdr.lorder
  263. #define OVFL_POINT hdr.ovfl_point
  264. #define LAST_FREED hdr.last_freed
  265. #define MAX_BUCKET hdr.max_bucket
  266. #define FFACTOR hdr.ffactor
  267. #define HIGH_MASK hdr.high_mask
  268. #define LOW_MASK hdr.low_mask
  269. #define NKEYS hdr.nkeys
  270. #define HDRPAGES hdr.hdrpages
  271. #define SPARES hdr.spares
  272. #define BITMAPS hdr.bitmaps
  273. #define VERSION hdr.version
  274. #define MAGIC hdr.magic
  275. #define NEXT_FREE hdr.next_free
  276. #define H_CHARKEY hdr.h_charkey