77
88PG_MODULE_MAGIC ;
99
10- PG_FUNCTION_INFO_V1 (pg_hash_string );
11- PG_FUNCTION_INFO_V1 (pg_hash64_string );
12- PG_FUNCTION_INFO_V1 (pg_hash128_string );
13- PG_FUNCTION_INFO_V1 (pg_hash_int32 );
14- PG_FUNCTION_INFO_V1 (pg_hash_int32from64 );
15- PG_FUNCTION_INFO_V1 (pg_hash_int64 );
16-
17- /*
18- * Algorithm data
19- */
20-
21- #define HASHNAMELEN 12
22-
23- struct StrHashDesc {
24- int namelen ;
25- const char name [HASHNAMELEN ];
26- hlib_str_hash_fn hash ;
27- uint64_t initval ;
28- };
29-
30- struct Int64HashDesc {
31- int namelen ;
32- const char name [HASHNAMELEN ];
33- hlib_int64_hash_fn hash ;
34- };
35-
36- static const struct StrHashDesc string_hash_list [] = {
37- { 7 , "murmur3" , hlib_murmur3 , 0 },
38- { 0 },
39- };
40-
41- static const struct Int64HashDesc int64_hash_list [] = {
42- { 7 , "murmur3" , hlib_murmur3_int64 },
43- { 0 },
44- };
45-
46- /*
47- * Lookup functions.
48- */
49-
50- static const struct StrHashDesc *
51- find_string_hash (const char * name , unsigned nlen )
52- {
53- const struct StrHashDesc * desc ;
54- char buf [HASHNAMELEN ];
55-
56- if (nlen >= HASHNAMELEN )
57- return NULL ;
58- memset (buf , 0 , sizeof (buf ));
59- memcpy (buf , name , nlen );
60-
61- for (desc = string_hash_list ; desc -> namelen ; desc ++ ) {
62- if (desc -> namelen != nlen )
63- continue ;
64- if (name [0 ] != desc -> name [0 ])
65- continue ;
66- if (memcmp (desc -> name , name , nlen ) == 0 )
67- return desc ;
68- }
69- return NULL ;
70- }
71-
72- static const struct Int64HashDesc *
73- find_int64_hash (const char * name , unsigned nlen )
74- {
75- const struct Int64HashDesc * desc ;
76- char buf [HASHNAMELEN ];
77-
78- if (nlen >= HASHNAMELEN )
79- return NULL ;
80- memset (buf , 0 , sizeof (buf ));
81- memcpy (buf , name , nlen );
82-
83- for (desc = int64_hash_list ; desc -> namelen ; desc ++ ) {
84- if (desc -> namelen == nlen && !memcmp (desc -> name , name , nlen ))
85- return desc ;
86- }
87- return NULL ;
88- }
89-
90- /*
91- * Utility functions.
92- */
93-
94- static void
95- err_nohash (text * hashname )
96- {
97- const char * name ;
98- name = DatumGetCString (DirectFunctionCall1 (textout , PointerGetDatum (hashname )));
99- elog (ERROR , "hash '%s' not found" , name );
100- }
10+ PG_FUNCTION_INFO_V1 (spqr_hash_murmur3_str );
11+ PG_FUNCTION_INFO_V1 (spqr_hash_murmur3_int64 );
10112
10213/*
10314 * Public functions
10415 */
10516
106- /* hash64_string(bytea, text [, int8 [, int8]]) returns int8 */
10717Datum
108- pg_hash64_string (PG_FUNCTION_ARGS )
18+ spqr_hash_murmur3_str (PG_FUNCTION_ARGS )
10919{
11020 struct varlena * data ;
111- text * hashname = PG_GETARG_TEXT_PP (1 );
11221 uint64_t io [MAX_IO_VALUES ];
113- const struct StrHashDesc * desc ;
11422
11523 memset (io , 0 , sizeof (io ));
11624
@@ -121,45 +29,19 @@ pg_hash64_string(PG_FUNCTION_ARGS)
12129 data = PG_GETARG_VARLENA_P (0 );
12230#endif
12331
124- /* load hash */
125- desc = find_string_hash (VARDATA_ANY (hashname ), VARSIZE_ANY_EXHDR (hashname ));
126- if (desc == NULL )
127- err_nohash (hashname );
128-
129- /* decide initvals */
130- if (PG_NARGS () >= 4 )
131- io [1 ] = PG_GETARG_INT64 (3 );
132- if (PG_NARGS () >= 3 )
133- io [0 ] = PG_GETARG_INT64 (2 );
134- else
135- io [0 ] = desc -> initval ;
136-
13732 /* do hash */
138- desc -> hash (VARDATA_ANY (data ), VARSIZE_ANY_EXHDR (data ), io );
33+ hlib_murmur3 (VARDATA_ANY (data ), VARSIZE_ANY_EXHDR (data ), io );
13934
14035 PG_FREE_IF_COPY (data , 0 );
141- PG_FREE_IF_COPY (hashname , 1 );
142-
14336 PG_RETURN_INT64 (io [0 ]);
14437}
14538
146- /*
147- * Integer hashing
148- */
149-
150- /* hash_int8(int8, text) returns int8 */
15139Datum
152- pg_hash_int64 (PG_FUNCTION_ARGS )
40+ spqr_hash_murmur3_int64 (PG_FUNCTION_ARGS )
15341{
15442 int64 data = PG_GETARG_INT64 (0 );
155- text * hashname = PG_GETARG_TEXT_PP (1 );
156- const struct Int64HashDesc * desc ;
15743
158- desc = find_int64_hash (VARDATA_ANY (hashname ), VARSIZE_ANY_EXHDR (hashname ));
159- if (desc == NULL )
160- err_nohash (hashname );
161- PG_FREE_IF_COPY (hashname , 1 );
162-
163- PG_RETURN_INT64 (desc -> hash ((uint64_t )(data )));
44+ PG_RETURN_INT64 (hlib_murmur3_int64 ((uint64_t )(data )));
16445}
16546
47+
0 commit comments