Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit d7694fc

Browse files
committed
Consolidate the function pointer types used by dshash.c.
Commit 8c0d7ba 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
1 parent 4569715 commit d7694fc

File tree

2 files changed

+39
-63
lines changed

2 files changed

+39
-63
lines changed

src/backend/lib/dshash.c

+29-36
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include "storage/ipc.h"
3636
#include "storage/lwlock.h"
3737
#include "utils/dsa.h"
38+
#include "utils/hsearch.h"
3839
#include "utils/memutils.h"
3940

4041
/*
@@ -188,24 +189,15 @@ static inline bool equal_keys(dshash_table *hash_table,
188189
/*
189190
* Create a new hash table backed by the given dynamic shared area, with the
190191
* given parameters. The returned object is allocated in backend-local memory
191-
* using the current MemoryContext. If 'arg' is non-null, the arg variants of
192-
* hash and compare functions must be provided in 'params' and 'arg' will be
193-
* passed down to them.
192+
* using the current MemoryContext. 'arg' will be passed through to the
193+
* compare and hash functions.
194194
*/
195195
dshash_table *
196196
dshash_create(dsa_area *area, const dshash_parameters *params, void *arg)
197197
{
198198
dshash_table *hash_table;
199199
dsa_pointer control;
200200

201-
/* Sanity checks on the set of supplied functions. */
202-
Assert((params->compare_function != NULL) ^
203-
(params->compare_arg_function != NULL));
204-
Assert((params->hash_function != NULL) ^
205-
(params->hash_arg_function != NULL));
206-
Assert(arg == NULL || (params->compare_arg_function != NULL));
207-
Assert(arg == NULL || (params->hash_arg_function != NULL));
208-
209201
/* Allocate the backend-local object representing the hash table. */
210202
hash_table = palloc(sizeof(dshash_table));
211203

@@ -263,9 +255,8 @@ dshash_create(dsa_area *area, const dshash_parameters *params, void *arg)
263255

264256
/*
265257
* Attach to an existing hash table using a handle. The returned object is
266-
* allocated in backend-local memory using the current MemoryContext. If
267-
* 'arg' is non-null, the arg variants of hash and compare functions must be
268-
* provided in 'params' and 'arg' will be passed down to them.
258+
* allocated in backend-local memory using the current MemoryContext. 'arg'
259+
* will be passed through to the compare and hash functions.
269260
*/
270261
dshash_table *
271262
dshash_attach(dsa_area *area, const dshash_parameters *params,
@@ -274,14 +265,6 @@ dshash_attach(dsa_area *area, const dshash_parameters *params,
274265
dshash_table *hash_table;
275266
dsa_pointer control;
276267

277-
/* Sanity checks on the set of supplied functions. */
278-
Assert((params->compare_function != NULL) ^
279-
(params->compare_arg_function != NULL));
280-
Assert((params->hash_function != NULL) ^
281-
(params->hash_arg_function != NULL));
282-
Assert(arg == NULL || (params->compare_arg_function != NULL));
283-
Assert(arg == NULL || (params->hash_arg_function != NULL));
284-
285268
/* Allocate the backend-local object representing the hash table. */
286269
hash_table = palloc(sizeof(dshash_table));
287270

@@ -582,6 +565,24 @@ dshash_release_lock(dshash_table *hash_table, void *entry)
582565
LWLockRelease(PARTITION_LOCK(hash_table, partition_index));
583566
}
584567

568+
/*
569+
* A compare function that forwards to memcmp.
570+
*/
571+
int
572+
dshash_memcmp(const void *a, const void *b, size_t size, void *arg)
573+
{
574+
return memcmp(a, b, size);
575+
}
576+
577+
/*
578+
* A hash function that forwards to tag_hash.
579+
*/
580+
dshash_hash
581+
dshash_memhash(const void *v, size_t size, void *arg)
582+
{
583+
return tag_hash(v, size);
584+
}
585+
585586
/*
586587
* Print debugging information about the internal state of the hash table to
587588
* stderr. The caller must hold no partition locks.
@@ -874,11 +875,9 @@ delete_item_from_bucket(dshash_table *hash_table,
874875
static inline dshash_hash
875876
hash_key(dshash_table *hash_table, const void *key)
876877
{
877-
if (hash_table->params.hash_arg_function != NULL)
878-
return hash_table->params.hash_arg_function(key, hash_table->arg);
879-
else
880-
return hash_table->params.hash_function(key,
881-
hash_table->params.key_size);
878+
return hash_table->params.hash_function(key,
879+
hash_table->params.key_size,
880+
hash_table->arg);
882881
}
883882

884883
/*
@@ -887,13 +886,7 @@ hash_key(dshash_table *hash_table, const void *key)
887886
static inline bool
888887
equal_keys(dshash_table *hash_table, const void *a, const void *b)
889888
{
890-
int r;
891-
892-
if (hash_table->params.compare_arg_function != NULL)
893-
r = hash_table->params.compare_arg_function(a, b, hash_table->arg);
894-
else
895-
r = hash_table->params.compare_function(a, b,
896-
hash_table->params.key_size);
897-
898-
return r == 0;
889+
return hash_table->params.compare_function(a, b,
890+
hash_table->params.key_size,
891+
hash_table->arg) == 0;
899892
}

src/include/lib/dshash.h

+10-27
Original file line numberDiff line numberDiff line change
@@ -26,32 +26,13 @@ typedef dsa_pointer dshash_table_handle;
2626
/* The type for hash values. */
2727
typedef uint32 dshash_hash;
2828

29-
/*
30-
* A function used for comparing keys. This version is compatible with
31-
* HashCompareFunction in hsearch.h and memcmp.
32-
*/
33-
typedef int (*dshash_compare_function) (const void *a, const void *b, size_t size);
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);
3432

35-
/*
36-
* A function type used for comparing keys. This version allows compare
37-
* functions to receive a pointer to arbitrary user data that was given to the
38-
* create or attach function. Similar to qsort_arg_comparator.
39-
*/
40-
typedef int (*dshash_compare_arg_function) (const void *a, const void *b, void *arg);
41-
42-
/*
43-
* A function type for computing hash values for keys. This version is
44-
* compatible with HashValueFunc in hsearch.h and hash functions like
45-
* tag_hash.
46-
*/
47-
typedef dshash_hash (*dshash_hash_function) (const void *v, size_t size);
48-
49-
/*
50-
* A function type for computing hash values for keys. This version allows
51-
* hash functions to receive a pointer to arbitrary user data that was given
52-
* to the create or attach function.
53-
*/
54-
typedef dshash_hash (*dshash_hash_arg_function) (const void *v, void *arg);
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);
5536

5637
/*
5738
* The set of parameters needed to create or attach to a hash table. The
@@ -70,9 +51,7 @@ typedef struct dshash_parameters
7051
size_t key_size; /* Size of the key (initial bytes of entry) */
7152
size_t entry_size; /* Total size of entry */
7253
dshash_compare_function compare_function; /* Compare function */
73-
dshash_compare_arg_function compare_arg_function; /* Arg version */
7454
dshash_hash_function hash_function; /* Hash function */
75-
dshash_hash_arg_function hash_arg_function; /* Arg version */
7655
int tranche_id; /* The tranche ID to use for locks */
7756
} dshash_parameters;
7857

@@ -101,6 +80,10 @@ extern bool dshash_delete_key(dshash_table *hash_table, const void *key);
10180
extern void dshash_delete_entry(dshash_table *hash_table, void *entry);
10281
extern void dshash_release_lock(dshash_table *hash_table, void *entry);
10382

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+
10487
/* Debugging support. */
10588
extern void dshash_dump(dshash_table *hash_table);
10689

0 commit comments

Comments
 (0)