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-2020, 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 /* The type for hash values. */
28 
29 /* A function type for comparing keys. */
30 typedef int (*dshash_compare_function) (const void *a, const void *b,
31  size_t size, void *arg);
32 
33 /* A function type for computing hash values for keys. */
34 typedef dshash_hash (*dshash_hash_function) (const void *v, size_t size,
35  void *arg);
36 
37 /*
38  * The set of parameters needed to create or attach to a hash table. The
39  * members tranche_id and tranche_name do not need to be initialized when
40  * attaching to an existing hash table.
41  *
42  * Compare and hash functions must be supplied even when attaching, because we
43  * can't safely share function pointers between backends in general. Either
44  * the arg variants or the non-arg variants should be supplied; the other
45  * function pointers should be NULL. If the arg variants are supplied then the
46  * user data pointer supplied to the create and attach functions will be
47  * passed to the hash and compare functions.
48  */
49 typedef struct dshash_parameters
50 {
51  size_t key_size; /* Size of the key (initial bytes of entry) */
52  size_t entry_size; /* Total size of entry */
53  dshash_compare_function compare_function; /* Compare function */
54  dshash_hash_function hash_function; /* Hash function */
55  int tranche_id; /* The tranche ID to use for locks */
57 
58 /* Forward declaration of private types for use only by dshash.c. */
59 struct dshash_table_item;
61 
62 /* Creating, sharing and destroying from hash tables. */
63 extern dshash_table *dshash_create(dsa_area *area,
64  const dshash_parameters *params,
65  void *arg);
66 extern dshash_table *dshash_attach(dsa_area *area,
67  const dshash_parameters *params,
68  dshash_table_handle handle,
69  void *arg);
70 extern void dshash_detach(dshash_table *hash_table);
72 extern void dshash_destroy(dshash_table *hash_table);
73 
74 /* Finding, creating, deleting entries. */
75 extern void *dshash_find(dshash_table *hash_table,
76  const void *key, bool exclusive);
77 extern void *dshash_find_or_insert(dshash_table *hash_table,
78  const void *key, bool *found);
79 extern bool dshash_delete_key(dshash_table *hash_table, const void *key);
80 extern void dshash_delete_entry(dshash_table *hash_table, void *entry);
81 extern void dshash_release_lock(dshash_table *hash_table, void *entry);
82 
83 /* Convenience hash and compare functions wrapping memcmp and tag_hash. */
84 extern int dshash_memcmp(const void *a, const void *b, size_t size, void *arg);
85 extern dshash_hash dshash_memhash(const void *v, size_t size, void *arg);
86 
87 /* Debugging support. */
88 extern void dshash_dump(dshash_table *hash_table);
89 
90 #endif /* DSHASH_H */
dshash_table_handle dshash_get_hash_table_handle(dshash_table *hash_table)
Definition: dshash.c:362
int dshash_memcmp(const void *a, const void *b, size_t size, void *arg)
Definition: dshash.c:581
dsa_pointer dshash_table_handle
Definition: dshash.h:24
dshash_compare_function compare_function
Definition: dshash.h:53
uint32 dshash_hash
Definition: dshash.h:27
void * dshash_find_or_insert(dshash_table *hash_table, const void *key, bool *found)
Definition: dshash.c:430
uint64 dsa_pointer
Definition: dsa.h:62
int(* dshash_compare_function)(const void *a, const void *b, size_t size, void *arg)
Definition: dshash.h:30
size_t entry_size
Definition: dshash.h:52
void dshash_destroy(dshash_table *hash_table)
Definition: dshash.c:318
dshash_hash(* dshash_hash_function)(const void *v, size_t size, void *arg)
Definition: dshash.h:34
dshash_hash dshash_memhash(const void *v, size_t size, void *arg)
Definition: dshash.c:590
unsigned int uint32
Definition: c.h:367
void dshash_delete_entry(dshash_table *hash_table, void *entry)
Definition: dshash.c:540
void * dshash_find(dshash_table *hash_table, const void *key, bool exclusive)
Definition: dshash.c:385
dshash_hash_function hash_function
Definition: dshash.h:54
void dshash_detach(dshash_table *hash_table)
Definition: dshash.c:302
size_t key_size
Definition: dshash.h:51
Definition: dsa.c:354
dshash_table * dshash_attach(dsa_area *area, const dshash_parameters *params, dshash_table_handle handle, void *arg)
Definition: dshash.c:263
dshash_table * dshash_create(dsa_area *area, const dshash_parameters *params, void *arg)
Definition: dshash.c:196
void * arg
void dshash_release_lock(dshash_table *hash_table, void *entry)
Definition: dshash.c:561
struct dshash_parameters dshash_parameters
bool dshash_delete_key(dshash_table *hash_table, const void *key)
Definition: dshash.c:502
void dshash_dump(dshash_table *hash_table)
Definition: dshash.c:600