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