hsearch.h 5.1 KB
Newer Older
1 2
/*-------------------------------------------------------------------------
 *
3
 * hsearch.h
4
 *	  for hash tables, particularly hash tables in shared memory
5 6
 *
 *
B
Add:  
Bruce Momjian 已提交
7 8
 * Portions Copyright (c) 1996-2000, PostgreSQL, Inc
 * Portions Copyright (c) 1994, Regents of the University of California
9
 *
10
 * $Id: hsearch.h,v 1.17 2001/01/02 04:33:24 tgl Exp $
11 12 13 14 15 16 17 18 19
 *
 *-------------------------------------------------------------------------
 */
#ifndef HSEARCH_H
#define HSEARCH_H


/*
 * Constants
20 21
 *
 * A hash table has a top-level "directory", each of whose entries points
B
Bruce Momjian 已提交
22
 * to a "segment" of ssize bucket headers.	The maximum number of hash
23 24 25 26 27
 * buckets is thus dsize * ssize (but dsize may be expansible).  Of course,
 * the number of records in the table can be larger, but we don't want a
 * whole lot of records per bucket or performance goes down.
 *
 * In a hash table allocated in shared memory, the directory cannot be
28 29
 * expanded because it must stay at a fixed address.  The directory size
 * should be selected using hash_select_dirsize (and you'd better have
30
 * a good idea of the maximum number of entries!).	For non-shared hash
31
 * tables, the initial directory size can be left at the default.
32
 */
33
#define DEF_SEGSIZE			   256
34
#define DEF_SEGSIZE_SHIFT	   8/* must be log2(DEF_SEGSIZE) */
35
#define DEF_DIRSIZE			   256
36
#define DEF_FFACTOR			   1/* default fill factor */
37

B
Bruce Momjian 已提交
38
#define PRIME1				   37		/* for the hash function */
39
#define PRIME2				   1048583
40 41 42 43 44 45


/*
 * Hash bucket is actually bigger than this.  Key field can have
 * variable length and a variable length data field follows it.
 */
46 47
typedef struct element
{
48
	unsigned long next;			/* secret from user */
49
	long		key;
50
} ELEMENT;
51 52

typedef unsigned long BUCKET_INDEX;
53

54
/* segment is an array of bucket pointers */
55 56 57
typedef BUCKET_INDEX *SEGMENT;
typedef unsigned long SEG_OFFSET;

58 59
typedef struct hashhdr
{
60
	long		dsize;			/* Directory Size */
61
	long		ssize;			/* Segment Size --- must be power of 2 */
62 63 64 65 66 67 68 69 70
	long		sshift;			/* Segment shift */
	long		max_bucket;		/* ID of Maximum bucket in use */
	long		high_mask;		/* Mask to modulo into entire table */
	long		low_mask;		/* Mask to modulo into lower half of table */
	long		ffactor;		/* Fill factor */
	long		nkeys;			/* Number of keys in hash table */
	long		nsegs;			/* Number of allocated segments */
	long		keysize;		/* hash key length in bytes */
	long		datasize;		/* elem data length in bytes */
71 72 73
	long		max_dsize;		/* 'dsize' limit if directory is fixed
								 * size */
	BUCKET_INDEX freeBucketIndex;		/* index of first free bucket */
74
#ifdef HASH_STATISTICS
75 76
	long		accesses;
	long		collisions;
77
#endif
78
} HHDR;
79 80 81

typedef struct htab
{
82 83 84
	HHDR	   *hctl;			/* shared control information */
	long		(*hash) ();		/* Hash Function */
	char	   *segbase;		/* segment base address for calculating
85
								 * pointer values */
86
	SEG_OFFSET *dir;			/* 'directory' of segm starts */
87
	void	   *(*alloc) (Size);	/* memory allocator */
88
} HTAB;
89 90 91

typedef struct hashctl
{
92 93 94 95 96 97
	long		ssize;			/* Segment Size */
	long		dsize;			/* Dirsize Size */
	long		ffactor;		/* Fill factor */
	long		(*hash) ();		/* Hash Function */
	long		keysize;		/* hash key length in bytes */
	long		datasize;		/* elem data length in bytes */
98
	long		max_dsize;		/* limit to dsize if directory size is
99
								 * limited */
100
	long	   *segbase;		/* base for calculating bucket + seg ptrs */
101
	void	   *(*alloc) (Size);	/* memory allocation function */
102 103
	long	   *dir;			/* directory if allocated already */
	long	   *hctl;			/* location of header information in shd
104
								 * mem */
105
} HASHCTL;
106 107 108 109 110 111

/* Flags to indicate action for hctl */
#define HASH_SEGMENT	0x002	/* Setting segment size */
#define HASH_DIRSIZE	0x004	/* Setting directory size */
#define HASH_FFACTOR	0x008	/* Setting fill factor */
#define HASH_FUNCTION	0x010	/* Set user defined hash function */
112 113 114 115
#define HASH_ELEM		0x020	/* Setting key/data size */
#define HASH_SHARED_MEM 0x040	/* Setting shared mem const */
#define HASH_ATTACH		0x080	/* Do not initialize hctl */
#define HASH_ALLOC		0x100	/* Setting memory allocator */
116 117


118
/* seg_alloc assumes that INVALID_INDEX is 0 */
119 120
#define INVALID_INDEX			(0)
#define NO_MAX_DSIZE			(-1)
121
/* number of hash buckets allocated at once */
122
#define BUCKET_ALLOC_INCR		(30)
123 124

/* hash_search operations */
125 126 127 128 129 130 131
typedef enum
{
	HASH_FIND,
	HASH_ENTER,
	HASH_REMOVE,
	HASH_FIND_SAVE,
	HASH_REMOVE_SAVED
132
} HASHACTION;
133

134 135 136 137 138 139 140 141
/* hash_seq status (should be considered an opaque type by callers) */
typedef struct
{
	HTAB   *hashp;
	long	curBucket;
	BUCKET_INDEX curIndex;
} HASH_SEQ_STATUS;

142
/*
143 144
 * prototypes from functions in dynahash.c
 */
145 146 147
extern HTAB *hash_create(int nelem, HASHCTL *info, int flags);
extern void hash_destroy(HTAB *hashp);
extern void hash_stats(char *where, HTAB *hashp);
148
extern long *hash_search(HTAB *hashp, char *keyPtr, HASHACTION action,
149
			bool *foundPtr);
150 151
extern void hash_seq_init(HASH_SEQ_STATUS *status, HTAB *hashp);
extern long *hash_seq_search(HASH_SEQ_STATUS *status);
152
extern long hash_estimate_size(long num_entries, long keysize, long datasize);
153
extern long hash_select_dirsize(long num_entries);
154 155

/*
156 157
 * prototypes from functions in hashfn.c
 */
158 159
extern long string_hash(char *key, int keysize);
extern long tag_hash(int *key, int keysize);
160

161
#endif	 /* HSEARCH_H */