PostgreSQL Source Code  git master
dshash.h
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * dshash.h
4  * Concurrent hash tables backed by dynamic shared memory areas.
5  *
6  * Portions Copyright (c) 1996-2023, 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 
17 #include "utils/dsa.h"
18 
19 /* The opaque type representing a hash table. */
20 struct dshash_table;
21 typedef struct dshash_table dshash_table;
22 
23 /* A handle for a dshash_table which can be shared with other processes. */
25 
26 /* Sentinel value to use for invalid dshash_table handles. */
27 #define DSHASH_HANDLE_INVALID ((dshash_table_handle) InvalidDsaPointer)
28 
29 /* The type for hash values. */
31 
32 /* A function type for comparing keys. */
33 typedef int (*dshash_compare_function) (const void *a, const void *b,
34  size_t size, void *arg);
35 
36 /* A function type for computing hash values for keys. */
37 typedef dshash_hash (*dshash_hash_function) (const void *v, size_t size,
38  void *arg);
39 
40 /*
41  * The set of parameters needed to create or attach to a hash table. The
42  * members tranche_id and tranche_name do not need to be initialized when
43  * attaching to an existing hash table.
44  *
45  * Compare and hash functions must be supplied even when attaching, because we
46  * can't safely share function pointers between backends in general. Either
47  * the arg variants or the non-arg variants should be supplied; the other
48  * function pointers should be NULL. If the arg variants are supplied then the
49  * user data pointer supplied to the create and attach functions will be
50  * passed to the hash and compare functions.
51  */
52 typedef struct dshash_parameters
53 {
54  size_t key_size; /* Size of the key (initial bytes of entry) */
55  size_t entry_size; /* Total size of entry */
56  dshash_compare_function compare_function; /* Compare function */
57  dshash_hash_function hash_function; /* Hash function */
58  int tranche_id; /* The tranche ID to use for locks */
60 
61 /* Forward declaration of private types for use only by dshash.c. */
62 struct dshash_table_item;
64 
65 /*
66  * Sequential scan state. The detail is exposed to let users know the storage
67  * size but it should be considered as an opaque type by callers.
68  */
69 typedef struct dshash_seq_status
70 {
71  dshash_table *hash_table; /* dshash table working on */
72  int curbucket; /* bucket number we are at */
73  int nbuckets; /* total number of buckets in the dshash */
74  dshash_table_item *curitem; /* item we are currently at */
75  dsa_pointer pnextitem; /* dsa-pointer to the next item */
76  int curpartition; /* partition number we are at */
77  bool exclusive; /* locking mode */
79 
80 /* Creating, sharing and destroying from hash tables. */
81 extern dshash_table *dshash_create(dsa_area *area,
82  const dshash_parameters *params,
83  void *arg);
84 extern dshash_table *dshash_attach(dsa_area *area,
85  const dshash_parameters *params,
86  dshash_table_handle handle,
87  void *arg);
88 extern void dshash_detach(dshash_table *hash_table);
90 extern void dshash_destroy(dshash_table *hash_table);
91 
92 /* Finding, creating, deleting entries. */
93 extern void *dshash_find(dshash_table *hash_table,
94  const void *key, bool exclusive);
95 extern void *dshash_find_or_insert(dshash_table *hash_table,
96  const void *key, bool *found);
97 extern bool dshash_delete_key(dshash_table *hash_table, const void *key);
98 extern void dshash_delete_entry(dshash_table *hash_table, void *entry);
99 extern void dshash_release_lock(dshash_table *hash_table, void *entry);
100 
101 /* seq scan support */
102 extern void dshash_seq_init(dshash_seq_status *status, dshash_table *hash_table,
103  bool exclusive);
104 extern void *dshash_seq_next(dshash_seq_status *status);
105 extern void dshash_seq_term(dshash_seq_status *status);
106 extern void dshash_delete_current(dshash_seq_status *status);
107 
108 /* Convenience hash and compare functions wrapping memcmp and tag_hash. */
109 extern int dshash_memcmp(const void *a, const void *b, size_t size, void *arg);
110 extern dshash_hash dshash_memhash(const void *v, size_t size, void *arg);
111 
112 /* Debugging support. */
113 extern void dshash_dump(dshash_table *hash_table);
114 
115 #endif /* DSHASH_H */
unsigned int uint32
Definition: c.h:495
uint64 dsa_pointer
Definition: dsa.h:62
bool dshash_delete_key(dshash_table *hash_table, const void *key)
Definition: dshash.c:503
void dshash_delete_entry(dshash_table *hash_table, void *entry)
Definition: dshash.c:541
void dshash_destroy(dshash_table *hash_table)
Definition: dshash.c:323
void dshash_release_lock(dshash_table *hash_table, void *entry)
Definition: dshash.c:558
void dshash_detach(dshash_table *hash_table)
Definition: dshash.c:307
void dshash_seq_init(dshash_seq_status *status, dshash_table *hash_table, bool exclusive)
Definition: dshash.c:595
void * dshash_find(dshash_table *hash_table, const void *key, bool exclusive)
Definition: dshash.c:390
dsa_pointer dshash_table_handle
Definition: dshash.h:24
dshash_table_handle dshash_get_hash_table_handle(dshash_table *hash_table)
Definition: dshash.c:367
void dshash_dump(dshash_table *hash_table)
Definition: dshash.c:735
int(* dshash_compare_function)(const void *a, const void *b, size_t size, void *arg)
Definition: dshash.h:33
struct dshash_parameters dshash_parameters
void dshash_seq_term(dshash_seq_status *status)
Definition: dshash.c:704
uint32 dshash_hash
Definition: dshash.h:30
struct dshash_seq_status dshash_seq_status
dshash_hash dshash_memhash(const void *v, size_t size, void *arg)
Definition: dshash.c:581
dshash_hash(* dshash_hash_function)(const void *v, size_t size, void *arg)
Definition: dshash.h:37
void * dshash_find_or_insert(dshash_table *hash_table, const void *key, bool *found)
Definition: dshash.c:433
dshash_table * dshash_attach(dsa_area *area, const dshash_parameters *params, dshash_table_handle handle, void *arg)
Definition: dshash.c:270
int dshash_memcmp(const void *a, const void *b, size_t size, void *arg)
Definition: dshash.c:572
dshash_table * dshash_create(dsa_area *area, const dshash_parameters *params, void *arg)
Definition: dshash.c:206
void * dshash_seq_next(dshash_seq_status *status)
Definition: dshash.c:614
void dshash_delete_current(dshash_seq_status *status)
Definition: dshash.c:714
int b
Definition: isn.c:70
int a
Definition: isn.c:69
void * arg
Definition: dsa.c:368
dshash_hash_function hash_function
Definition: dshash.h:57
size_t key_size
Definition: dshash.h:54
dshash_compare_function compare_function
Definition: dshash.h:56
size_t entry_size
Definition: dshash.h:55
bool exclusive
Definition: dshash.h:77
dshash_table_item * curitem
Definition: dshash.h:74
dsa_pointer pnextitem
Definition: dshash.h:75
int curpartition
Definition: dshash.h:76
dshash_table * hash_table
Definition: dshash.h:71