Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitd7694fc

Browse files
committed
Consolidate the function pointer types used by dshash.c.
Commit8c0d7ba introduced dshash with hashand compare functions like DynaHash's, and also variants that take a userdata pointer instead of size. Simplify the interface by merging them intoa single pair of function pointer types that take both size and a user datapointer.Since it is anticipated that memcmp and tag_hash behavior will be a commonrequirement, provide wrapper functions dshash_memcmp and dshash_memhash thatconform to the new function types.Author: Thomas MunroReviewed-By: Andres FreundDiscussion:https://postgr.es/m/20170823054644.efuzftxjpfi6wwqs%40alap3.anarazel.de
1 parent4569715 commitd7694fc

File tree

2 files changed

+39
-63
lines changed

2 files changed

+39
-63
lines changed

‎src/backend/lib/dshash.c

Lines changed: 29 additions & 36 deletions
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,constdshash_parameters*params,void*arg)
197197
{
198198
dshash_table*hash_table;
199199
dsa_pointercontrol;
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,constdshash_parameters*params,
@@ -274,14 +265,6 @@ dshash_attach(dsa_area *area, const dshash_parameters *params,
274265
dshash_table*hash_table;
275266
dsa_pointercontrol;
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(constvoid*a,constvoid*b,size_tsize,void*arg)
573+
{
574+
returnmemcmp(a,b,size);
575+
}
576+
577+
/*
578+
* A hash function that forwards to tag_hash.
579+
*/
580+
dshash_hash
581+
dshash_memhash(constvoid*v,size_tsize,void*arg)
582+
{
583+
returntag_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
staticinlinedshash_hash
875876
hash_key(dshash_table*hash_table,constvoid*key)
876877
{
877-
if (hash_table->params.hash_arg_function!=NULL)
878-
returnhash_table->params.hash_arg_function(key,hash_table->arg);
879-
else
880-
returnhash_table->params.hash_function(key,
881-
hash_table->params.key_size);
878+
returnhash_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
staticinlinebool
888887
equal_keys(dshash_table*hash_table,constvoid*a,constvoid*b)
889888
{
890-
intr;
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-
returnr==0;
889+
returnhash_table->params.compare_function(a,b,
890+
hash_table->params.key_size,
891+
hash_table->arg)==0;
899892
}

‎src/include/lib/dshash.h

Lines changed: 10 additions & 27 deletions
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
typedefuint32dshash_hash;
2828

29-
/*
30-
* A function used for comparing keys. This version is compatible with
31-
* HashCompareFunction in hsearch.h and memcmp.
32-
*/
33-
typedefint (*dshash_compare_function) (constvoid*a,constvoid*b,size_tsize);
29+
/* A function type for comparing keys. */
30+
typedefint (*dshash_compare_function) (constvoid*a,constvoid*b,
31+
size_tsize,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-
typedefint (*dshash_compare_arg_function) (constvoid*a,constvoid*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-
typedefdshash_hash (*dshash_hash_function) (constvoid*v,size_tsize);
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-
typedefdshash_hash (*dshash_hash_arg_function) (constvoid*v,void*arg);
33+
/* A function type for computing hash values for keys. */
34+
typedefdshash_hash (*dshash_hash_function) (constvoid*v,size_tsize,
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_tkey_size;/* Size of the key (initial bytes of entry) */
7152
size_tentry_size;/* Total size of entry */
7253
dshash_compare_functioncompare_function;/* Compare function */
73-
dshash_compare_arg_functioncompare_arg_function;/* Arg version */
7454
dshash_hash_functionhash_function;/* Hash function */
75-
dshash_hash_arg_functionhash_arg_function;/* Arg version */
7655
inttranche_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
externvoiddshash_delete_entry(dshash_table*hash_table,void*entry);
10281
externvoiddshash_release_lock(dshash_table*hash_table,void*entry);
10382

83+
/* Convenience hash and compare functions wrapping memcmp and tag_hash. */
84+
externintdshash_memcmp(constvoid*a,constvoid*b,size_tsize,void*arg);
85+
externdshash_hashdshash_memhash(constvoid*v,size_tsize,void*arg);
86+
10487
/* Debugging support. */
10588
externvoiddshash_dump(dshash_table*hash_table);
10689

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp