dshash.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*-------------------------------------------------------------------------
  2. *
  3. * dshash.h
  4. * Concurrent hash tables backed by dynamic shared memory areas.
  5. *
  6. * Portions Copyright (c) 1996-2022, PostgreSQL Global Development Group
  7. * Portions Copyright (c) 1994, Regents of the University of California
  8. *
  9. * IDENTIFICATION
  10. * src/include/lib/dshash.h
  11. *
  12. *-------------------------------------------------------------------------
  13. */
  14. #ifndef DSHASH_H
  15. #define DSHASH_H
  16. #include "utils/dsa.h"
  17. /* The opaque type representing a hash table. */
  18. struct dshash_table;
  19. typedef struct dshash_table dshash_table;
  20. /* A handle for a dshash_table which can be shared with other processes. */
  21. typedef dsa_pointer dshash_table_handle;
  22. /* The type for hash values. */
  23. typedef uint32 dshash_hash;
  24. /* A function type for comparing keys. */
  25. typedef int (*dshash_compare_function) (const void *a, const void *b,
  26. size_t size, void *arg);
  27. /* A function type for computing hash values for keys. */
  28. typedef dshash_hash (*dshash_hash_function) (const void *v, size_t size,
  29. void *arg);
  30. /*
  31. * The set of parameters needed to create or attach to a hash table. The
  32. * members tranche_id and tranche_name do not need to be initialized when
  33. * attaching to an existing hash table.
  34. *
  35. * Compare and hash functions must be supplied even when attaching, because we
  36. * can't safely share function pointers between backends in general. Either
  37. * the arg variants or the non-arg variants should be supplied; the other
  38. * function pointers should be NULL. If the arg variants are supplied then the
  39. * user data pointer supplied to the create and attach functions will be
  40. * passed to the hash and compare functions.
  41. */
  42. typedef struct dshash_parameters
  43. {
  44. size_t key_size; /* Size of the key (initial bytes of entry) */
  45. size_t entry_size; /* Total size of entry */
  46. dshash_compare_function compare_function; /* Compare function */
  47. dshash_hash_function hash_function; /* Hash function */
  48. int tranche_id; /* The tranche ID to use for locks */
  49. } dshash_parameters;
  50. /* Forward declaration of private types for use only by dshash.c. */
  51. struct dshash_table_item;
  52. typedef struct dshash_table_item dshash_table_item;
  53. /*
  54. * Sequential scan state. The detail is exposed to let users know the storage
  55. * size but it should be considered as an opaque type by callers.
  56. */
  57. typedef struct dshash_seq_status
  58. {
  59. dshash_table *hash_table; /* dshash table working on */
  60. int curbucket; /* bucket number we are at */
  61. int nbuckets; /* total number of buckets in the dshash */
  62. dshash_table_item *curitem; /* item we are currently at */
  63. dsa_pointer pnextitem; /* dsa-pointer to the next item */
  64. int curpartition; /* partition number we are at */
  65. bool exclusive; /* locking mode */
  66. } dshash_seq_status;
  67. /* Creating, sharing and destroying from hash tables. */
  68. extern dshash_table *dshash_create(dsa_area *area,
  69. const dshash_parameters *params,
  70. void *arg);
  71. extern dshash_table *dshash_attach(dsa_area *area,
  72. const dshash_parameters *params,
  73. dshash_table_handle handle,
  74. void *arg);
  75. extern void dshash_detach(dshash_table *hash_table);
  76. extern dshash_table_handle dshash_get_hash_table_handle(dshash_table *hash_table);
  77. extern void dshash_destroy(dshash_table *hash_table);
  78. /* Finding, creating, deleting entries. */
  79. extern void *dshash_find(dshash_table *hash_table,
  80. const void *key, bool exclusive);
  81. extern void *dshash_find_or_insert(dshash_table *hash_table,
  82. const void *key, bool *found);
  83. extern bool dshash_delete_key(dshash_table *hash_table, const void *key);
  84. extern void dshash_delete_entry(dshash_table *hash_table, void *entry);
  85. extern void dshash_release_lock(dshash_table *hash_table, void *entry);
  86. /* seq scan support */
  87. extern void dshash_seq_init(dshash_seq_status *status, dshash_table *hash_table,
  88. bool exclusive);
  89. extern void *dshash_seq_next(dshash_seq_status *status);
  90. extern void dshash_seq_term(dshash_seq_status *status);
  91. extern void dshash_delete_current(dshash_seq_status *status);
  92. /* Convenience hash and compare functions wrapping memcmp and tag_hash. */
  93. extern int dshash_memcmp(const void *a, const void *b, size_t size, void *arg);
  94. extern dshash_hash dshash_memhash(const void *v, size_t size, void *arg);
  95. /* Debugging support. */
  96. extern void dshash_dump(dshash_table *hash_table);
  97. #endif /* DSHASH_H */