35
35
#include "storage/ipc.h"
36
36
#include "storage/lwlock.h"
37
37
#include "utils/dsa.h"
38
+ #include "utils/hsearch.h"
38
39
#include "utils/memutils.h"
39
40
40
41
/*
@@ -188,24 +189,15 @@ static inline bool equal_keys(dshash_table *hash_table,
188
189
/*
189
190
* Create a new hash table backed by the given dynamic shared area, with the
190
191
* 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.
194
194
*/
195
195
dshash_table *
196
196
dshash_create (dsa_area * area , const dshash_parameters * params , void * arg )
197
197
{
198
198
dshash_table * hash_table ;
199
199
dsa_pointer control ;
200
200
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
-
209
201
/* Allocate the backend-local object representing the hash table. */
210
202
hash_table = palloc (sizeof (dshash_table ));
211
203
@@ -263,9 +255,8 @@ dshash_create(dsa_area *area, const dshash_parameters *params, void *arg)
263
255
264
256
/*
265
257
* 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.
269
260
*/
270
261
dshash_table *
271
262
dshash_attach (dsa_area * area , const dshash_parameters * params ,
@@ -274,14 +265,6 @@ dshash_attach(dsa_area *area, const dshash_parameters *params,
274
265
dshash_table * hash_table ;
275
266
dsa_pointer control ;
276
267
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
-
285
268
/* Allocate the backend-local object representing the hash table. */
286
269
hash_table = palloc (sizeof (dshash_table ));
287
270
@@ -582,6 +565,24 @@ dshash_release_lock(dshash_table *hash_table, void *entry)
582
565
LWLockRelease (PARTITION_LOCK (hash_table , partition_index ));
583
566
}
584
567
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
+
585
586
/*
586
587
* Print debugging information about the internal state of the hash table to
587
588
* stderr. The caller must hold no partition locks.
@@ -874,11 +875,9 @@ delete_item_from_bucket(dshash_table *hash_table,
874
875
static inline dshash_hash
875
876
hash_key (dshash_table * hash_table , const void * key )
876
877
{
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 );
882
881
}
883
882
884
883
/*
@@ -887,13 +886,7 @@ hash_key(dshash_table *hash_table, const void *key)
887
886
static inline bool
888
887
equal_keys (dshash_table * hash_table , const void * a , const void * b )
889
888
{
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 ;
899
892
}
0 commit comments