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 */
195195dshash_table *
196196dshash_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 */
270261dshash_table *
271262dshash_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,
874875static inline dshash_hash
875876hash_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)
887886static inline bool
888887equal_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}
0 commit comments