hsearch.h 4.2 KB
Newer Older
1 2 3
/*-------------------------------------------------------------------------
 *
 * hsearch.h--
4
 *	  for hashing in the new buffer manager
5 6 7 8
 *
 *
 * Copyright (c) 1994, Regents of the University of California
 *
9
 * $Id: hsearch.h,v 1.9 1998/09/01 04:39:12 momjian Exp $
10 11 12 13 14 15 16 17 18 19
 *
 *-------------------------------------------------------------------------
 */
#ifndef HSEARCH_H
#define HSEARCH_H


/*
 * Constants
 */
20 21 22 23 24 25 26 27 28
#define DEF_BUCKET_SIZE		   256
#define DEF_BUCKET_SHIFT	   8/* log2(BUCKET) */
#define DEF_SEGSIZE			   256
#define DEF_SEGSIZE_SHIFT			   8		/* log2(SEGSIZE)  */
#define DEF_DIRSIZE			   256
#define PRIME1				   37
#define PRIME2				   1048583
#define DEF_FFACTOR			   1
#define SPLTMAX				   8
29 30 31 32 33 34


/*
 * Hash bucket is actually bigger than this.  Key field can have
 * variable length and a variable length data field follows it.
 */
35 36
typedef struct element
{
37 38
	unsigned long next;			/* secret from user		 */
	long		key;
39
} ELEMENT;
40 41

typedef unsigned long BUCKET_INDEX;
42

43 44 45 46
/* segment is an array of bucket pointers  */
typedef BUCKET_INDEX *SEGMENT;
typedef unsigned long SEG_OFFSET;

47 48
typedef struct hashhdr
{
49 50 51 52 53 54 55 56 57 58 59 60 61 62
	long		bsize;			/* Bucket/Page Size */
	long		bshift;			/* Bucket shift */
	long		dsize;			/* Directory Size */
	long		ssize;			/* Segment Size */
	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 */
	long		max_dsize;		/* 'dsize' limit if directory is fixed
63
								 * size */
64
	BUCKET_INDEX freeBucketIndex;
65
	/* index of first free bucket */
66
#ifdef HASH_STATISTICS
67 68
	long		accesses;
	long		collisions;
69
#endif
70
} HHDR;
71 72 73

typedef struct htab
{
74 75 76
	HHDR	   *hctl;			/* shared control information */
	long		(*hash) ();		/* Hash Function */
	char	   *segbase;		/* segment base address for calculating
77
								 * pointer values */
78 79
	SEG_OFFSET *dir;			/* 'directory' of segm starts */
	long	   *(*alloc) ();	/* memory allocator (long * for alignment
80 81
								 * reasons) */

82
} HTAB;
83 84 85

typedef struct hashctl
{
86 87 88 89 90 91 92 93
	long		bsize;			/* Bucket Size */
	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 */
	long		max_size;		/* limit to dsize if directory size is
94
								 * limited */
95 96 97 98
	long	   *segbase;		/* base for calculating bucket + seg ptrs */
	long	   *(*alloc) ();	/* memory allocation function */
	long	   *dir;			/* directory if allocated already */
	long	   *hctl;			/* location of header information in shd
99
								 * mem */
100
} HASHCTL;
101 102

/* Flags to indicate action for hctl */
103
#define HASH_BUCKET		0x001	/* Setting bucket size */
104 105 106 107
#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 */
108 109 110 111
#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 */
112 113 114


/* seg_alloc assumes that INVALID_INDEX is 0*/
115 116
#define INVALID_INDEX			(0)
#define NO_MAX_DSIZE			(-1)
117
/* number of hash buckets allocated at once */
118
#define BUCKET_ALLOC_INCR		(30)
119 120

/* hash_search operations */
121 122 123 124 125 126 127
typedef enum
{
	HASH_FIND,
	HASH_ENTER,
	HASH_REMOVE,
	HASH_FIND_SAVE,
	HASH_REMOVE_SAVED
128
} HASHACTION;
129 130

/*
131 132
 * prototypes from functions in dynahash.c
 */
133 134 135
extern HTAB *hash_create(int nelem, HASHCTL *info, int flags);
extern void hash_destroy(HTAB *hashp);
extern void hash_stats(char *where, HTAB *hashp);
136
extern long *hash_search(HTAB *hashp, char *keyPtr, HASHACTION action,
137 138
			bool *foundPtr);
extern long *hash_seq(HTAB *hashp);
139 140

/*
141 142
 * prototypes from functions in hashfn.c
 */
143 144
extern long string_hash(char *key, int keysize);
extern long tag_hash(int *key, int keysize);
145

146
#endif	 /* HSEARCH_H */