2
0

hsearch.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*-------------------------------------------------------------------------
  2. *
  3. * hsearch.h
  4. * exported definitions for utils/hash/dynahash.c; see notes therein
  5. *
  6. *
  7. * Portions Copyright (c) 1996-2022, PostgreSQL Global Development Group
  8. * Portions Copyright (c) 1994, Regents of the University of California
  9. *
  10. * src/include/utils/hsearch.h
  11. *
  12. *-------------------------------------------------------------------------
  13. */
  14. #ifndef HSEARCH_H
  15. #define HSEARCH_H
  16. /*
  17. * Hash functions must have this signature.
  18. */
  19. typedef uint32 (*HashValueFunc) (const void *key, Size keysize);
  20. /*
  21. * Key comparison functions must have this signature. Comparison functions
  22. * return zero for match, nonzero for no match. (The comparison function
  23. * definition is designed to allow memcmp() and strncmp() to be used directly
  24. * as key comparison functions.)
  25. */
  26. typedef int (*HashCompareFunc) (const void *key1, const void *key2,
  27. Size keysize);
  28. /*
  29. * Key copying functions must have this signature. The return value is not
  30. * used. (The definition is set up to allow memcpy() and strlcpy() to be
  31. * used directly.)
  32. */
  33. typedef void *(*HashCopyFunc) (void *dest, const void *src, Size keysize);
  34. /*
  35. * Space allocation function for a hashtable --- designed to match malloc().
  36. * Note: there is no free function API; can't destroy a hashtable unless you
  37. * use the default allocator.
  38. */
  39. typedef void *(*HashAllocFunc) (Size request);
  40. /*
  41. * HASHELEMENT is the private part of a hashtable entry. The caller's data
  42. * follows the HASHELEMENT structure (on a MAXALIGN'd boundary). The hash key
  43. * is expected to be at the start of the caller's hash entry data structure.
  44. */
  45. typedef struct HASHELEMENT
  46. {
  47. struct HASHELEMENT *link; /* link to next entry in same bucket */
  48. uint32 hashvalue; /* hash function result for this entry */
  49. } HASHELEMENT;
  50. /* Hash table header struct is an opaque type known only within dynahash.c */
  51. typedef struct HASHHDR HASHHDR;
  52. /* Hash table control struct is an opaque type known only within dynahash.c */
  53. typedef struct HTAB HTAB;
  54. /* Parameter data structure for hash_create */
  55. /* Only those fields indicated by hash_flags need be set */
  56. typedef struct HASHCTL
  57. {
  58. /* Used if HASH_PARTITION flag is set: */
  59. long num_partitions; /* # partitions (must be power of 2) */
  60. /* Used if HASH_SEGMENT flag is set: */
  61. long ssize; /* segment size */
  62. /* Used if HASH_DIRSIZE flag is set: */
  63. long dsize; /* (initial) directory size */
  64. long max_dsize; /* limit to dsize if dir size is limited */
  65. /* Used if HASH_ELEM flag is set (which is now required): */
  66. Size keysize; /* hash key length in bytes */
  67. Size entrysize; /* total user element size in bytes */
  68. /* Used if HASH_FUNCTION flag is set: */
  69. HashValueFunc hash; /* hash function */
  70. /* Used if HASH_COMPARE flag is set: */
  71. HashCompareFunc match; /* key comparison function */
  72. /* Used if HASH_KEYCOPY flag is set: */
  73. HashCopyFunc keycopy; /* key copying function */
  74. /* Used if HASH_ALLOC flag is set: */
  75. HashAllocFunc alloc; /* memory allocator */
  76. /* Used if HASH_CONTEXT flag is set: */
  77. MemoryContext hcxt; /* memory context to use for allocations */
  78. /* Used if HASH_SHARED_MEM flag is set: */
  79. HASHHDR *hctl; /* location of header in shared mem */
  80. } HASHCTL;
  81. /* Flag bits for hash_create; most indicate which parameters are supplied */
  82. #define HASH_PARTITION 0x0001 /* Hashtable is used w/partitioned locking */
  83. #define HASH_SEGMENT 0x0002 /* Set segment size */
  84. #define HASH_DIRSIZE 0x0004 /* Set directory size (initial and max) */
  85. #define HASH_ELEM 0x0008 /* Set keysize and entrysize (now required!) */
  86. #define HASH_STRINGS 0x0010 /* Select support functions for string keys */
  87. #define HASH_BLOBS 0x0020 /* Select support functions for binary keys */
  88. #define HASH_FUNCTION 0x0040 /* Set user defined hash function */
  89. #define HASH_COMPARE 0x0080 /* Set user defined comparison function */
  90. #define HASH_KEYCOPY 0x0100 /* Set user defined key-copying function */
  91. #define HASH_ALLOC 0x0200 /* Set memory allocator */
  92. #define HASH_CONTEXT 0x0400 /* Set memory allocation context */
  93. #define HASH_SHARED_MEM 0x0800 /* Hashtable is in shared memory */
  94. #define HASH_ATTACH 0x1000 /* Do not initialize hctl */
  95. #define HASH_FIXED_SIZE 0x2000 /* Initial size is a hard limit */
  96. /* max_dsize value to indicate expansible directory */
  97. #define NO_MAX_DSIZE (-1)
  98. /* hash_search operations */
  99. typedef enum
  100. {
  101. HASH_FIND,
  102. HASH_ENTER,
  103. HASH_REMOVE,
  104. HASH_ENTER_NULL
  105. } HASHACTION;
  106. /* hash_seq status (should be considered an opaque type by callers) */
  107. typedef struct
  108. {
  109. HTAB *hashp;
  110. uint32 curBucket; /* index of current bucket */
  111. HASHELEMENT *curEntry; /* current entry in bucket */
  112. } HASH_SEQ_STATUS;
  113. /*
  114. * prototypes for functions in dynahash.c
  115. */
  116. extern HTAB *hash_create(const char *tabname, long nelem,
  117. const HASHCTL *info, int flags);
  118. extern void hash_destroy(HTAB *hashp);
  119. extern void hash_stats(const char *where, HTAB *hashp);
  120. extern void *hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action,
  121. bool *foundPtr);
  122. extern uint32 get_hash_value(HTAB *hashp, const void *keyPtr);
  123. extern void *hash_search_with_hash_value(HTAB *hashp, const void *keyPtr,
  124. uint32 hashvalue, HASHACTION action,
  125. bool *foundPtr);
  126. extern bool hash_update_hash_key(HTAB *hashp, void *existingEntry,
  127. const void *newKeyPtr);
  128. extern long hash_get_num_entries(HTAB *hashp);
  129. extern void hash_seq_init(HASH_SEQ_STATUS *status, HTAB *hashp);
  130. extern void *hash_seq_search(HASH_SEQ_STATUS *status);
  131. extern void hash_seq_term(HASH_SEQ_STATUS *status);
  132. extern void hash_freeze(HTAB *hashp);
  133. extern Size hash_estimate_size(long num_entries, Size entrysize);
  134. extern long hash_select_dirsize(long num_entries);
  135. extern Size hash_get_shared_size(HASHCTL *info, int flags);
  136. extern void AtEOXact_HashTables(bool isCommit);
  137. extern void AtEOSubXact_HashTables(bool isCommit, int nestDepth);
  138. #endif /* HSEARCH_H */