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