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

Commit42a1de3

Browse files
Add helper functions for dshash tables with string keys.
Presently, string keys are not well-supported for dshash tables.The dshash code always copies key_size bytes into new entries'keys, and dshash.h only provides compare and hash functions thatforward to memcmp() and tag_hash(), both of which do not stop atthe first NUL. This means that callers must pad string keys sothat the data beyond the first NUL does not adversely affect theresults of copying, comparing, and hashing the keys.To better support string keys in dshash tables, this commit doesa couple things:* A new copy_function field is added to the dshash_parameters struct. This function pointer specifies how the key should be copied into new table entries. For example, we only want to copy up to the first NUL byte for string keys. A dshash_memcpy() helper function is provided and used for all existing in-tree dshash tables without string keys.* A set of helper functions for string keys are provided. These helper functions forward to strcmp(), strcpy(), and string_hash(), all of which ignore data beyond the first NUL.This commit also adjusts the DSM registry's dshash table to use thenew helper functions for string keys.Reviewed-by: Andy FanDiscussion:https://postgr.es/m/20240119215941.GA1322079%40nathanxps13
1 parent5fe08c0 commit42a1de3

File tree

6 files changed

+83
-7
lines changed

6 files changed

+83
-7
lines changed

‎src/backend/lib/dshash.c

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,8 @@ static bool delete_item_from_bucket(dshash_table *hash_table,
188188
staticinlinedshash_hashhash_key(dshash_table*hash_table,constvoid*key);
189189
staticinlineboolequal_keys(dshash_table*hash_table,
190190
constvoid*a,constvoid*b);
191+
staticinlinevoidcopy_key(dshash_table*hash_table,void*dest,
192+
constvoid*src);
191193

192194
#definePARTITION_LOCK(hash_table,i)\
193195
(&(hash_table)->control->partitions[(i)].lock)
@@ -583,6 +585,49 @@ dshash_memhash(const void *v, size_t size, void *arg)
583585
returntag_hash(v,size);
584586
}
585587

588+
/*
589+
* A copy function that forwards to memcpy.
590+
*/
591+
void
592+
dshash_memcpy(void*dest,constvoid*src,size_tsize,void*arg)
593+
{
594+
(void)memcpy(dest,src,size);
595+
}
596+
597+
/*
598+
* A compare function that forwards to strcmp.
599+
*/
600+
int
601+
dshash_strcmp(constvoid*a,constvoid*b,size_tsize,void*arg)
602+
{
603+
Assert(strlen((constchar*)a)<size);
604+
Assert(strlen((constchar*)b)<size);
605+
606+
returnstrcmp((constchar*)a, (constchar*)b);
607+
}
608+
609+
/*
610+
* A hash function that forwards to string_hash.
611+
*/
612+
dshash_hash
613+
dshash_strhash(constvoid*v,size_tsize,void*arg)
614+
{
615+
Assert(strlen((constchar*)v)<size);
616+
617+
returnstring_hash((constchar*)v,size);
618+
}
619+
620+
/*
621+
* A copy function that forwards to strcpy.
622+
*/
623+
void
624+
dshash_strcpy(void*dest,constvoid*src,size_tsize,void*arg)
625+
{
626+
Assert(strlen((constchar*)src)<size);
627+
628+
(void)strcpy((char*)dest, (constchar*)src);
629+
}
630+
586631
/*
587632
* Sequentially scan through dshash table and return all the elements one by
588633
* one, return NULL when all elements have been returned.
@@ -949,7 +994,7 @@ insert_into_bucket(dshash_table *hash_table,
949994
hash_table->params.entry_size+
950995
MAXALIGN(sizeof(dshash_table_item)));
951996
item=dsa_get_address(hash_table->area,item_pointer);
952-
memcpy(ENTRY_FROM_ITEM(item),key,hash_table->params.key_size);
997+
copy_key(hash_table,ENTRY_FROM_ITEM(item),key);
953998
insert_item_into_bucket(hash_table,item_pointer,item,bucket);
954999
returnitem;
9551000
}
@@ -1032,3 +1077,14 @@ equal_keys(dshash_table *hash_table, const void *a, const void *b)
10321077
hash_table->params.key_size,
10331078
hash_table->arg)==0;
10341079
}
1080+
1081+
/*
1082+
* Copy a key.
1083+
*/
1084+
staticinlinevoid
1085+
copy_key(dshash_table*hash_table,void*dest,constvoid*src)
1086+
{
1087+
hash_table->params.copy_function(dest,src,
1088+
hash_table->params.key_size,
1089+
hash_table->arg);
1090+
}

‎src/backend/replication/logical/launcher.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ static const dshash_parameters dsh_params = {
8888
sizeof(LauncherLastStartTimesEntry),
8989
dshash_memcmp,
9090
dshash_memhash,
91+
dshash_memcpy,
9192
LWTRANCHE_LAUNCHER_HASH
9293
};
9394

‎src/backend/storage/ipc/dsm_registry.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,9 @@ typedef struct DSMRegistryEntry
5050
staticconstdshash_parametersdsh_params= {
5151
offsetof(DSMRegistryEntry,handle),
5252
sizeof(DSMRegistryEntry),
53-
dshash_memcmp,
54-
dshash_memhash,
53+
dshash_strcmp,
54+
dshash_strhash,
55+
dshash_strcpy,
5556
LWTRANCHE_DSM_REGISTRY_HASH
5657
};
5758

@@ -132,7 +133,6 @@ GetNamedDSMSegment(const char *name, size_t size,
132133
{
133134
DSMRegistryEntry*entry;
134135
MemoryContextoldcontext;
135-
charname_padded[offsetof(DSMRegistryEntry,handle)]= {0};
136136
void*ret;
137137

138138
Assert(found);
@@ -155,8 +155,7 @@ GetNamedDSMSegment(const char *name, size_t size,
155155
/* Connect to the registry. */
156156
init_dsm_registry();
157157

158-
strcpy(name_padded,name);
159-
entry=dshash_find_or_insert(dsm_registry_table,name_padded,found);
158+
entry=dshash_find_or_insert(dsm_registry_table,name,found);
160159
if (!(*found))
161160
{
162161
/* Initialize the segment. */

‎src/backend/utils/activity/pgstat_shmem.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ static const dshash_parameters dsh_params = {
6464
sizeof(PgStatShared_HashEntry),
6565
pgstat_cmp_hash_key,
6666
pgstat_hash_hash_key,
67+
dshash_memcpy,
6768
LWTRANCHE_PGSTATS_HASH
6869
};
6970

‎src/backend/utils/cache/typcache.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,7 @@ static const dshash_parameters srtr_record_table_params = {
259259
sizeof(SharedRecordTableEntry),
260260
shared_record_table_compare,
261261
shared_record_table_hash,
262+
dshash_memcpy,
262263
LWTRANCHE_PER_SESSION_RECORD_TYPE
263264
};
264265

@@ -268,6 +269,7 @@ static const dshash_parameters srtr_typmod_table_params = {
268269
sizeof(SharedTypmodTableEntry),
269270
dshash_memcmp,
270271
dshash_memhash,
272+
dshash_memcpy,
271273
LWTRANCHE_PER_SESSION_RECORD_TYPMOD
272274
};
273275

‎src/include/lib/dshash.h

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ typedef int (*dshash_compare_function) (const void *a, const void *b,
3737
typedefdshash_hash (*dshash_hash_function) (constvoid*v,size_tsize,
3838
void*arg);
3939

40+
/* A function type for copying keys. */
41+
typedefvoid (*dshash_copy_function) (void*dest,constvoid*src,size_tsize,
42+
void*arg);
43+
4044
/*
4145
* The set of parameters needed to create or attach to a hash table. The
4246
* members tranche_id and tranche_name do not need to be initialized when
@@ -55,6 +59,7 @@ typedef struct dshash_parameters
5559
size_tentry_size;/* Total size of entry */
5660
dshash_compare_functioncompare_function;/* Compare function */
5761
dshash_hash_functionhash_function;/* Hash function */
62+
dshash_copy_functioncopy_function;/* Copy function */
5863
inttranche_id;/* The tranche ID to use for locks */
5964
}dshash_parameters;
6065

@@ -105,9 +110,21 @@ extern void *dshash_seq_next(dshash_seq_status *status);
105110
externvoiddshash_seq_term(dshash_seq_status*status);
106111
externvoiddshash_delete_current(dshash_seq_status*status);
107112

108-
/* Convenience hash and compare functions wrapping memcmp and tag_hash. */
113+
/*
114+
* Convenience hash, compare, and copy functions wrapping memcmp, tag_hash, and
115+
* memcpy.
116+
*/
109117
externintdshash_memcmp(constvoid*a,constvoid*b,size_tsize,void*arg);
110118
externdshash_hashdshash_memhash(constvoid*v,size_tsize,void*arg);
119+
externvoiddshash_memcpy(void*dest,constvoid*src,size_tsize,void*arg);
120+
121+
/*
122+
* Convenience hash, compare, and copy functions wrapping strcmp, string_hash,
123+
* and strcpy.
124+
*/
125+
externintdshash_strcmp(constvoid*a,constvoid*b,size_tsize,void*arg);
126+
externdshash_hashdshash_strhash(constvoid*v,size_tsize,void*arg);
127+
externvoiddshash_strcpy(void*dest,constvoid*src,size_tsize,void*arg);
111128

112129
/* Debugging support. */
113130
externvoiddshash_dump(dshash_table*hash_table);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp