diff options
author | Andres Freund <andres@anarazel.de> | 2017-08-24 17:01:36 -0700 |
---|---|---|
committer | Andres Freund <andres@anarazel.de> | 2017-08-24 17:01:36 -0700 |
commit | d7694fc148707cd8335d9ccfde9f4c17290189db (patch) | |
tree | 1329774530a926ab580e878be5ea41da0d0775a7 /src/backend/lib/dshash.c | |
parent | 4569715bd6faa4c43e489a7069ab7abca68ff663 (diff) | |
download | postgresql-d7694fc148707cd8335d9ccfde9f4c17290189db.tar.gz postgresql-d7694fc148707cd8335d9ccfde9f4c17290189db.zip |
Consolidate the function pointer types used by dshash.c.
Commit 8c0d7bafad36434cb08ac2c78e69ae72c194ca20 introduced dshash with hash
and compare functions like DynaHash's, and also variants that take a user
data pointer instead of size. Simplify the interface by merging them into
a single pair of function pointer types that take both size and a user data
pointer.
Since it is anticipated that memcmp and tag_hash behavior will be a common
requirement, provide wrapper functions dshash_memcmp and dshash_memhash that
conform to the new function types.
Author: Thomas Munro
Reviewed-By: Andres Freund
Discussion: https://postgr.es/m/20170823054644.efuzftxjpfi6wwqs%40alap3.anarazel.de
Diffstat (limited to 'src/backend/lib/dshash.c')
-rw-r--r-- | src/backend/lib/dshash.c | 65 |
1 files changed, 29 insertions, 36 deletions
diff --git a/src/backend/lib/dshash.c b/src/backend/lib/dshash.c index 06ff32313cd..5dbd0c42275 100644 --- a/src/backend/lib/dshash.c +++ b/src/backend/lib/dshash.c @@ -35,6 +35,7 @@ #include "storage/ipc.h" #include "storage/lwlock.h" #include "utils/dsa.h" +#include "utils/hsearch.h" #include "utils/memutils.h" /* @@ -188,9 +189,8 @@ static inline bool equal_keys(dshash_table *hash_table, /* * Create a new hash table backed by the given dynamic shared area, with the * given parameters. The returned object is allocated in backend-local memory - * using the current MemoryContext. If 'arg' is non-null, the arg variants of - * hash and compare functions must be provided in 'params' and 'arg' will be - * passed down to them. + * using the current MemoryContext. 'arg' will be passed through to the + * compare and hash functions. */ dshash_table * dshash_create(dsa_area *area, const dshash_parameters *params, void *arg) @@ -198,14 +198,6 @@ dshash_create(dsa_area *area, const dshash_parameters *params, void *arg) dshash_table *hash_table; dsa_pointer control; - /* Sanity checks on the set of supplied functions. */ - Assert((params->compare_function != NULL) ^ - (params->compare_arg_function != NULL)); - Assert((params->hash_function != NULL) ^ - (params->hash_arg_function != NULL)); - Assert(arg == NULL || (params->compare_arg_function != NULL)); - Assert(arg == NULL || (params->hash_arg_function != NULL)); - /* Allocate the backend-local object representing the hash table. */ hash_table = palloc(sizeof(dshash_table)); @@ -263,9 +255,8 @@ dshash_create(dsa_area *area, const dshash_parameters *params, void *arg) /* * Attach to an existing hash table using a handle. The returned object is - * allocated in backend-local memory using the current MemoryContext. If - * 'arg' is non-null, the arg variants of hash and compare functions must be - * provided in 'params' and 'arg' will be passed down to them. + * allocated in backend-local memory using the current MemoryContext. 'arg' + * will be passed through to the compare and hash functions. */ dshash_table * dshash_attach(dsa_area *area, const dshash_parameters *params, @@ -274,14 +265,6 @@ dshash_attach(dsa_area *area, const dshash_parameters *params, dshash_table *hash_table; dsa_pointer control; - /* Sanity checks on the set of supplied functions. */ - Assert((params->compare_function != NULL) ^ - (params->compare_arg_function != NULL)); - Assert((params->hash_function != NULL) ^ - (params->hash_arg_function != NULL)); - Assert(arg == NULL || (params->compare_arg_function != NULL)); - Assert(arg == NULL || (params->hash_arg_function != NULL)); - /* Allocate the backend-local object representing the hash table. */ hash_table = palloc(sizeof(dshash_table)); @@ -583,6 +566,24 @@ dshash_release_lock(dshash_table *hash_table, void *entry) } /* + * A compare function that forwards to memcmp. + */ +int +dshash_memcmp(const void *a, const void *b, size_t size, void *arg) +{ + return memcmp(a, b, size); +} + +/* + * A hash function that forwards to tag_hash. + */ +dshash_hash +dshash_memhash(const void *v, size_t size, void *arg) +{ + return tag_hash(v, size); +} + +/* * Print debugging information about the internal state of the hash table to * stderr. The caller must hold no partition locks. */ @@ -874,11 +875,9 @@ delete_item_from_bucket(dshash_table *hash_table, static inline dshash_hash hash_key(dshash_table *hash_table, const void *key) { - if (hash_table->params.hash_arg_function != NULL) - return hash_table->params.hash_arg_function(key, hash_table->arg); - else - return hash_table->params.hash_function(key, - hash_table->params.key_size); + return hash_table->params.hash_function(key, + hash_table->params.key_size, + hash_table->arg); } /* @@ -887,13 +886,7 @@ hash_key(dshash_table *hash_table, const void *key) static inline bool equal_keys(dshash_table *hash_table, const void *a, const void *b) { - int r; - - if (hash_table->params.compare_arg_function != NULL) - r = hash_table->params.compare_arg_function(a, b, hash_table->arg); - else - r = hash_table->params.compare_function(a, b, - hash_table->params.key_size); - - return r == 0; + return hash_table->params.compare_function(a, b, + hash_table->params.key_size, + hash_table->arg) == 0; } |