codekingpro/portable-devtools
114k
1/*-------------------------------------------------------------------------2 *3 * dshash.h4 * Concurrent hash tables backed by dynamic shared memory areas.5 *6 * Portions Copyright (c) 1996-2023, PostgreSQL Global Development Group7 * Portions Copyright (c) 1994, Regents of the University of California8 *9 * IDENTIFICATION10 * src/include/lib/dshash.h11 *12 *-------------------------------------------------------------------------13 */14#ifndef DSHASH_H15#define DSHASH_H16 17#include "utils/dsa.h"18 19/* The opaque type representing a hash table. */20struct dshash_table;21typedef struct dshash_table dshash_table;22 23/* A handle for a dshash_table which can be shared with other processes. */24typedef dsa_pointer dshash_table_handle;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. */30typedef uint32 dshash_hash;31 32/* A function type for comparing keys. */33typedef 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. */37typedef 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. The42 * members tranche_id and tranche_name do not need to be initialized when43 * attaching to an existing hash table.44 *45 * Compare and hash functions must be supplied even when attaching, because we46 * can't safely share function pointers between backends in general. Either47 * the arg variants or the non-arg variants should be supplied; the other48 * function pointers should be NULL. If the arg variants are supplied then the49 * user data pointer supplied to the create and attach functions will be50 * passed to the hash and compare functions.51 */52typedef struct dshash_parameters53{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 */59} dshash_parameters;60 61/* Forward declaration of private types for use only by dshash.c. */62struct dshash_table_item;63typedef struct dshash_table_item dshash_table_item;64 65/*66 * Sequential scan state. The detail is exposed to let users know the storage67 * size but it should be considered as an opaque type by callers.68 */69typedef struct dshash_seq_status70{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 */78} dshash_seq_status;79 80/* Creating, sharing and destroying from hash tables. */81extern dshash_table *dshash_create(dsa_area *area,82 const dshash_parameters *params,83 void *arg);84extern dshash_table *dshash_attach(dsa_area *area,85 const dshash_parameters *params,86 dshash_table_handle handle,87 void *arg);88extern void dshash_detach(dshash_table *hash_table);89extern dshash_table_handle dshash_get_hash_table_handle(dshash_table *hash_table);90extern void dshash_destroy(dshash_table *hash_table);91 92/* Finding, creating, deleting entries. */93extern void *dshash_find(dshash_table *hash_table,94 const void *key, bool exclusive);95extern void *dshash_find_or_insert(dshash_table *hash_table,96 const void *key, bool *found);97extern bool dshash_delete_key(dshash_table *hash_table, const void *key);98extern void dshash_delete_entry(dshash_table *hash_table, void *entry);99extern void dshash_release_lock(dshash_table *hash_table, void *entry);100 101/* seq scan support */102extern void dshash_seq_init(dshash_seq_status *status, dshash_table *hash_table,103 bool exclusive);104extern void *dshash_seq_next(dshash_seq_status *status);105extern void dshash_seq_term(dshash_seq_status *status);106extern void dshash_delete_current(dshash_seq_status *status);107 108/* Convenience hash and compare functions wrapping memcmp and tag_hash. */109extern int dshash_memcmp(const void *a, const void *b, size_t size, void *arg);110extern dshash_hash dshash_memhash(const void *v, size_t size, void *arg);111 112/* Debugging support. */113extern void dshash_dump(dshash_table *hash_table);114 115#endif /* DSHASH_H */116 