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-2024, 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 /* A function type for copying keys. */
41 typedef void (*dshash_copy_function) (void *dest, const void *src, size_t size,
42  void *arg);
43 
44 /*
45  * The set of parameters needed to create or attach to a hash table. The
46  * tranche_id member does not need to be initialized when attaching to an
47  * existing hash table.
48  *
49  * Compare, hash, and copy functions must be supplied even when attaching,
50  * because we can't safely share function pointers between backends in general.
51  * The user data pointer supplied to the create and attach functions will be
52  * passed to these functions.
53  */
54 typedef struct dshash_parameters
55 {
56  size_t key_size; /* Size of the key (initial bytes of entry) */
57  size_t entry_size; /* Total size of entry */
58  dshash_compare_function compare_function; /* Compare function */
59  dshash_hash_function hash_function; /* Hash function */
60  dshash_copy_function copy_function; /* Copy function */
61  int tranche_id; /* The tranche ID to use for locks */
63 
64 /* Forward declaration of private types for use only by dshash.c. */
65 struct dshash_table_item;
67 
68 /*
69  * Sequential scan state. The detail is exposed to let users know the storage
70  * size but it should be considered as an opaque type by callers.
71  */
72 typedef struct dshash_seq_status
73 {
74  dshash_table *hash_table; /* dshash table working on */
75  int curbucket; /* bucket number we are at */
76  int nbuckets; /* total number of buckets in the dshash */
77  dshash_table_item *curitem; /* item we are currently at */
78  dsa_pointer pnextitem; /* dsa-pointer to the next item */
79  int curpartition; /* partition number we are at */
80  bool exclusive; /* locking mode */
82 
83 /* Creating, sharing and destroying from hash tables. */
84 extern dshash_table *dshash_create(dsa_area *area,
85  const dshash_parameters *params,
86  void *arg);
87 extern dshash_table *dshash_attach(dsa_area *area,
88  const dshash_parameters *params,
89  dshash_table_handle handle,
90  void *arg);
91 extern void dshash_detach(dshash_table *hash_table);
93 extern void dshash_destroy(dshash_table *hash_table);
94 
95 /* Finding, creating, deleting entries. */
96 extern void *dshash_find(dshash_table *hash_table,
97  const void *key, bool exclusive);
98 extern void *dshash_find_or_insert(dshash_table *hash_table,
99  const void *key, bool *found);
100 extern bool dshash_delete_key(dshash_table *hash_table, const void *key);
101 extern void dshash_delete_entry(dshash_table *hash_table, void *entry);
102 extern void dshash_release_lock(dshash_table *hash_table, void *entry);
103 
104 /* seq scan support */
105 extern void dshash_seq_init(dshash_seq_status *status, dshash_table *hash_table,
106  bool exclusive);
107 extern void *dshash_seq_next(dshash_seq_status *status);
108 extern void dshash_seq_term(dshash_seq_status *status);
109 extern void dshash_delete_current(dshash_seq_status *status);
110 
111 /*
112  * Convenience hash, compare, and copy functions wrapping memcmp, tag_hash, and
113  * memcpy.
114  */
115 extern int dshash_memcmp(const void *a, const void *b, size_t size, void *arg);
116 extern dshash_hash dshash_memhash(const void *v, size_t size, void *arg);
117 extern void dshash_memcpy(void *dest, const void *src, size_t size, void *arg);
118 
119 /*
120  * Convenience hash, compare, and copy functions wrapping strcmp, string_hash,
121  * and strcpy.
122  */
123 extern int dshash_strcmp(const void *a, const void *b, size_t size, void *arg);
124 extern dshash_hash dshash_strhash(const void *v, size_t size, void *arg);
125 extern void dshash_strcpy(void *dest, const void *src, size_t size, void *arg);
126 
127 /* Debugging support. */
128 extern void dshash_dump(dshash_table *hash_table);
129 
130 #endif /* DSHASH_H */
unsigned int uint32
Definition: c.h:506
uint64 dsa_pointer
Definition: dsa.h:62
bool dshash_delete_key(dshash_table *hash_table, const void *key)
Definition: dshash.c:503
void dshash_memcpy(void *dest, const void *src, size_t size, void *arg)
Definition: dshash.c:590
void dshash_delete_entry(dshash_table *hash_table, void *entry)
Definition: dshash.c:541
void dshash_strcpy(void *dest, const void *src, size_t size, void *arg)
Definition: dshash.c:622
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:638
void * dshash_find(dshash_table *hash_table, const void *key, bool exclusive)
Definition: dshash.c:390
dshash_hash dshash_strhash(const void *v, size_t size, void *arg)
Definition: dshash.c:611
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:778
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:747
uint32 dshash_hash
Definition: dshash.h:30
void(* dshash_copy_function)(void *dest, const void *src, size_t size, void *arg)
Definition: dshash.h:41
int dshash_strcmp(const void *a, const void *b, size_t size, void *arg)
Definition: dshash.c:599
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:657
void dshash_delete_current(dshash_seq_status *status)
Definition: dshash.c:757
int b
Definition: isn.c:70
int a
Definition: isn.c:69
void * arg
static pg_noinline void Size size
Definition: slab.c:607
Definition: dsa.c:348
dshash_hash_function hash_function
Definition: dshash.h:59
size_t key_size
Definition: dshash.h:56
dshash_compare_function compare_function
Definition: dshash.h:58
dshash_copy_function copy_function
Definition: dshash.h:60
size_t entry_size
Definition: dshash.h:57
bool exclusive
Definition: dshash.h:80
dshash_table_item * curitem
Definition: dshash.h:77
dsa_pointer pnextitem
Definition: dshash.h:78
int curpartition
Definition: dshash.h:79
dshash_table * hash_table
Definition: dshash.h:74