Header And Logo

PostgreSQL
| The world's most advanced open source database.

hsearch.h

Go to the documentation of this file.
00001 /*-------------------------------------------------------------------------
00002  *
00003  * hsearch.h
00004  *    exported definitions for utils/hash/dynahash.c; see notes therein
00005  *
00006  *
00007  * Portions Copyright (c) 1996-2013, PostgreSQL Global Development Group
00008  * Portions Copyright (c) 1994, Regents of the University of California
00009  *
00010  * src/include/utils/hsearch.h
00011  *
00012  *-------------------------------------------------------------------------
00013  */
00014 #ifndef HSEARCH_H
00015 #define HSEARCH_H
00016 
00017 
00018 /*
00019  * Hash functions must have this signature.
00020  */
00021 typedef uint32 (*HashValueFunc) (const void *key, Size keysize);
00022 
00023 /*
00024  * Key comparison functions must have this signature.  Comparison functions
00025  * return zero for match, nonzero for no match.  (The comparison function
00026  * definition is designed to allow memcmp() and strncmp() to be used directly
00027  * as key comparison functions.)
00028  */
00029 typedef int (*HashCompareFunc) (const void *key1, const void *key2,
00030                                             Size keysize);
00031 
00032 /*
00033  * Key copying functions must have this signature.  The return value is not
00034  * used.  (The definition is set up to allow memcpy() and strncpy() to be
00035  * used directly.)
00036  */
00037 typedef void *(*HashCopyFunc) (void *dest, const void *src, Size keysize);
00038 
00039 /*
00040  * Space allocation function for a hashtable --- designed to match malloc().
00041  * Note: there is no free function API; can't destroy a hashtable unless you
00042  * use the default allocator.
00043  */
00044 typedef void *(*HashAllocFunc) (Size request);
00045 
00046 /*
00047  * HASHELEMENT is the private part of a hashtable entry.  The caller's data
00048  * follows the HASHELEMENT structure (on a MAXALIGN'd boundary).  The hash key
00049  * is expected to be at the start of the caller's hash entry data structure.
00050  */
00051 typedef struct HASHELEMENT
00052 {
00053     struct HASHELEMENT *link;   /* link to next entry in same bucket */
00054     uint32      hashvalue;      /* hash function result for this entry */
00055 } HASHELEMENT;
00056 
00057 /* Hash table header struct is an opaque type known only within dynahash.c */
00058 typedef struct HASHHDR HASHHDR;
00059 
00060 /* Hash table control struct is an opaque type known only within dynahash.c */
00061 typedef struct HTAB HTAB;
00062 
00063 /* Parameter data structure for hash_create */
00064 /* Only those fields indicated by hash_flags need be set */
00065 typedef struct HASHCTL
00066 {
00067     long        num_partitions; /* # partitions (must be power of 2) */
00068     long        ssize;          /* segment size */
00069     long        dsize;          /* (initial) directory size */
00070     long        max_dsize;      /* limit to dsize if dir size is limited */
00071     long        ffactor;        /* fill factor */
00072     Size        keysize;        /* hash key length in bytes */
00073     Size        entrysize;      /* total user element size in bytes */
00074     HashValueFunc hash;         /* hash function */
00075     HashCompareFunc match;      /* key comparison function */
00076     HashCopyFunc keycopy;       /* key copying function */
00077     HashAllocFunc alloc;        /* memory allocator */
00078     MemoryContext hcxt;         /* memory context to use for allocations */
00079     HASHHDR    *hctl;           /* location of header in shared mem */
00080 } HASHCTL;
00081 
00082 /* Flags to indicate which parameters are supplied */
00083 #define HASH_PARTITION  0x001   /* Hashtable is used w/partitioned locking */
00084 #define HASH_SEGMENT    0x002   /* Set segment size */
00085 #define HASH_DIRSIZE    0x004   /* Set directory size (initial and max) */
00086 #define HASH_FFACTOR    0x008   /* Set fill factor */
00087 #define HASH_FUNCTION   0x010   /* Set user defined hash function */
00088 #define HASH_ELEM       0x020   /* Set keysize and entrysize */
00089 #define HASH_SHARED_MEM 0x040   /* Hashtable is in shared memory */
00090 #define HASH_ATTACH     0x080   /* Do not initialize hctl */
00091 #define HASH_ALLOC      0x100   /* Set memory allocator */
00092 #define HASH_CONTEXT    0x200   /* Set memory allocation context */
00093 #define HASH_COMPARE    0x400   /* Set user defined comparison function */
00094 #define HASH_KEYCOPY    0x800   /* Set user defined key-copying function */
00095 #define HASH_FIXED_SIZE 0x1000  /* Initial size is a hard limit */
00096 
00097 
00098 /* max_dsize value to indicate expansible directory */
00099 #define NO_MAX_DSIZE            (-1)
00100 
00101 /* hash_search operations */
00102 typedef enum
00103 {
00104     HASH_FIND,
00105     HASH_ENTER,
00106     HASH_REMOVE,
00107     HASH_ENTER_NULL
00108 } HASHACTION;
00109 
00110 /* hash_seq status (should be considered an opaque type by callers) */
00111 typedef struct
00112 {
00113     HTAB       *hashp;
00114     uint32      curBucket;      /* index of current bucket */
00115     HASHELEMENT *curEntry;      /* current entry in bucket */
00116 } HASH_SEQ_STATUS;
00117 
00118 /*
00119  * prototypes for functions in dynahash.c
00120  */
00121 extern HTAB *hash_create(const char *tabname, long nelem,
00122             HASHCTL *info, int flags);
00123 extern void hash_destroy(HTAB *hashp);
00124 extern void hash_stats(const char *where, HTAB *hashp);
00125 extern void *hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action,
00126             bool *foundPtr);
00127 extern uint32 get_hash_value(HTAB *hashp, const void *keyPtr);
00128 extern void *hash_search_with_hash_value(HTAB *hashp, const void *keyPtr,
00129                             uint32 hashvalue, HASHACTION action,
00130                             bool *foundPtr);
00131 extern bool hash_update_hash_key(HTAB *hashp, void *existingEntry,
00132                      const void *newKeyPtr);
00133 extern long hash_get_num_entries(HTAB *hashp);
00134 extern void hash_seq_init(HASH_SEQ_STATUS *status, HTAB *hashp);
00135 extern void *hash_seq_search(HASH_SEQ_STATUS *status);
00136 extern void hash_seq_term(HASH_SEQ_STATUS *status);
00137 extern void hash_freeze(HTAB *hashp);
00138 extern Size hash_estimate_size(long num_entries, Size entrysize);
00139 extern long hash_select_dirsize(long num_entries);
00140 extern Size hash_get_shared_size(HASHCTL *info, int flags);
00141 extern void AtEOXact_HashTables(bool isCommit);
00142 extern void AtEOSubXact_HashTables(bool isCommit, int nestDepth);
00143 
00144 /*
00145  * prototypes for functions in hashfn.c
00146  */
00147 extern uint32 string_hash(const void *key, Size keysize);
00148 extern uint32 tag_hash(const void *key, Size keysize);
00149 extern uint32 oid_hash(const void *key, Size keysize);
00150 extern uint32 bitmap_hash(const void *key, Size keysize);
00151 extern int  bitmap_match(const void *key1, const void *key2, Size keysize);
00152 
00153 #endif   /* HSEARCH_H */