Skip to content

Commit 0d85fc0

Browse files
authored
Rename hash funcitons (#2)
1 parent 3b0b56b commit 0d85fc0

File tree

12 files changed

+10033
-10200
lines changed

12 files changed

+10033
-10200
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,5 +73,5 @@ tgz:
7373
pg_version?=16
7474
codename?=jammy
7575
regress:
76-
docker build . --tag pg_hash:1.0 --build-arg POSTGRES_VERSION=$(pg_version) --build-arg codename=$(codename) && docker run pg_hash:1.0 | tee logs.out
76+
docker build . --tag spqrhash_regress:1.0 --build-arg POSTGRES_VERSION=$(pg_version) --build-arg codename=$(codename) && docker run spqrhash_regress:1.0 | tee logs.out
7777

README.rst

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,26 +17,15 @@ You need PostgreSQL developent environment. Then simply::
1717
Functions
1818
---------
1919

20-
hash64_string
20+
spqrhash_murmur3
2121
~~~~~~~~~~~~~
2222

2323
::
24+
spqrhash_murmur3(text) RETURNS int8
25+
spqrhash_murmur3(bytea) RETURNS int8
26+
spqrhash_murmur3(int8) RETURNS int8
2427

25-
hash64_string(data text, algo text, [, iv1 int8 [, iv2 int8]]) returns int8
26-
hash64_string(data byte, algo text, [, iv1 int8 [, iv2 int8]]) returns int8
27-
28-
Uses same algorithms as `hash_string()` but returns 64-bit result.
29-
30-
hash_int8
31-
~~~~~~~~~
32-
33-
::
34-
35-
hash_int8(val int8) returns int8
36-
37-
Hash 64-bit integer.
38-
39-
28+
Murmur3 hash algorithm
4029

4130
Hashing algorithms
4231
-------------------------

sql/spqrhash--1.0.sql

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,9 @@
11

2-
CREATE OR REPLACE FUNCTION hash64_string(text, text) RETURNS int8
3-
AS '$libdir/spqrhash', 'pg_hash64_string' LANGUAGE C IMMUTABLE STRICT;
2+
CREATE OR REPLACE FUNCTION spqrhash_murmur3(int8) RETURNS int8
3+
AS '$libdir/spqrhash', 'spqr_hash_murmur3_int64' LANGUAGE C IMMUTABLE STRICT;
44

5-
CREATE OR REPLACE FUNCTION hash64_string(bytea, text) RETURNS int8
6-
AS '$libdir/spqrhash', 'pg_hash64_string' LANGUAGE C IMMUTABLE STRICT;
7-
8-
CREATE OR REPLACE FUNCTION hash64_string(text, text, int8) RETURNS int8
9-
AS '$libdir/spqrhash', 'pg_hash64_string' LANGUAGE C IMMUTABLE STRICT;
10-
11-
CREATE OR REPLACE FUNCTION hash64_string(bytea, text, int8) RETURNS int8
12-
AS '$libdir/spqrhash', 'pg_hash64_string' LANGUAGE C IMMUTABLE STRICT;
13-
14-
CREATE OR REPLACE FUNCTION hash64_string(text, text, int8, int8) RETURNS int8
15-
AS '$libdir/spqrhash', 'pg_hash64_string' LANGUAGE C IMMUTABLE STRICT;
16-
17-
CREATE OR REPLACE FUNCTION hash64_string(bytea, text, int8, int8) RETURNS int8
18-
AS '$libdir/spqrhash', 'pg_hash64_string' LANGUAGE C IMMUTABLE STRICT;
19-
20-
CREATE OR REPLACE FUNCTION hash_int8(int8, text) RETURNS int8
21-
AS '$libdir/spqrhash', 'pg_hash_int64' LANGUAGE C IMMUTABLE STRICT;
5+
CREATE OR REPLACE FUNCTION spqrhash_murmur3(text) RETURNS int8
6+
AS '$libdir/spqrhash', 'spqr_hash_murmur3_str' LANGUAGE C IMMUTABLE STRICT;
227

8+
CREATE OR REPLACE FUNCTION spqrhash_murmur3(bytea) RETURNS int8
9+
AS '$libdir/spqrhash', 'spqr_hash_murmur3_str' LANGUAGE C IMMUTABLE STRICT;

sql/spqrhash--unpackaged--1.0.sql

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
11

2-
ALTER EXTENSION spqrhash ADD FUNCTION hash64_string(text, text);
3-
ALTER EXTENSION spqrhash ADD FUNCTION hash64_string(bytea, text);
4-
ALTER EXTENSION spqrhash ADD FUNCTION hash64_string(text, text, int8);
5-
ALTER EXTENSION spqrhash ADD FUNCTION hash64_string(bytea, text, int8);
6-
ALTER EXTENSION spqrhash ADD FUNCTION hash64_string(text, text, int8, int8);
7-
ALTER EXTENSION spqrhash ADD FUNCTION hash64_string(bytea, text, int8, int8);
8-
ALTER EXTENSION spqrhash ADD FUNCTION hash_int8(int8, text);
9-
2+
ALTER EXTENSION spqrhash ADD FUNCTION spqrhash_murmur3(int8);
3+
ALTER EXTENSION spqrhash ADD FUNCTION spqrhash_murmur3(text);
4+
ALTER EXTENSION spqrhash ADD FUNCTION spqrhash_murmur3(bytea);

sql/spqrhash.sql

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,9 @@
1-
CREATE OR REPLACE FUNCTION hash64_string(text, text) RETURNS int8
2-
AS '$libdir/spqrhash', 'pg_hash64_string' LANGUAGE C IMMUTABLE STRICT;
31

4-
CREATE OR REPLACE FUNCTION hash64_string(bytea, text) RETURNS int8
5-
AS '$libdir/spqrhash', 'pg_hash64_string' LANGUAGE C IMMUTABLE STRICT;
2+
CREATE OR REPLACE FUNCTION spqrhash_murmur3(int8) RETURNS int8
3+
AS '$libdir/spqrhash', 'spqr_hash_murmur3_int64' LANGUAGE C IMMUTABLE STRICT;
64

7-
CREATE OR REPLACE FUNCTION hash64_string(text, text, int8) RETURNS int8
8-
AS '$libdir/spqrhash', 'pg_hash64_string' LANGUAGE C IMMUTABLE STRICT;
9-
10-
CREATE OR REPLACE FUNCTION hash64_string(bytea, text, int8) RETURNS int8
11-
AS '$libdir/spqrhash', 'pg_hash64_string' LANGUAGE C IMMUTABLE STRICT;
12-
13-
CREATE OR REPLACE FUNCTION hash64_string(text, text, int8, int8) RETURNS int8
14-
AS '$libdir/spqrhash', 'pg_hash64_string' LANGUAGE C IMMUTABLE STRICT;
15-
16-
CREATE OR REPLACE FUNCTION hash64_string(bytea, text, int8, int8) RETURNS int8
17-
AS '$libdir/spqrhash', 'pg_hash64_string' LANGUAGE C IMMUTABLE STRICT;
18-
19-
CREATE OR REPLACE FUNCTION hash_int8(int8, text) RETURNS int8
20-
AS '$libdir/spqrhash', 'pg_hash_int64' LANGUAGE C IMMUTABLE STRICT;
5+
CREATE OR REPLACE FUNCTION spqrhash_murmur3(text) RETURNS int8
6+
AS '$libdir/spqrhash', 'spqr_hash_murmur3_str' LANGUAGE C IMMUTABLE STRICT;
217

8+
CREATE OR REPLACE FUNCTION spqrhash_murmur3(bytea) RETURNS int8
9+
AS '$libdir/spqrhash', 'spqr_hash_murmur3_str' LANGUAGE C IMMUTABLE STRICT;

sql/uninstall_spqrhash.sql

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11

2-
DROP FUNCTION hash64_string(text, text);
3-
DROP FUNCTION hash64_string(bytea, text);
4-
DROP FUNCTION hash64_string(text, text, int8);
5-
DROP FUNCTION hash64_string(bytea, text, int8);
6-
DROP FUNCTION hash64_string(text, text, int8, int8);
7-
DROP FUNCTION hash64_string(bytea, text, int8, int8);
8-
DROP FUNCTION hash_int8(int8, text);
2+
DROP FUNCTION spqrhash_murmur3(int8);
3+
DROP FUNCTION spqrhash_murmur3(text);
4+
DROP FUNCTION spqrhash_murmur3(bytea);
95

src/spqrhash.c

Lines changed: 7 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -7,110 +7,18 @@
77

88
PG_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 */
10717
Datum
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 */
15139
Datum
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+

src/spqrhash.h

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,14 @@
2828
/* how many values in io array will be used, max */
2929
#define MAX_IO_VALUES 2
3030

31-
/* hash function signatures */
32-
typedef void (*hlib_str_hash_fn)(const void *data, size_t len, uint64_t *io);
33-
typedef uint64_t (*hlib_int64_hash_fn)(uint64_t data);
34-
3531
/* string hashes */
3632
void hlib_murmur3(const void *data, size_t len, uint64_t *io);
3733

3834
/* integer hashes */
3935
uint64_t hlib_murmur3_int64(uint64_t data);
4036

41-
Datum pg_hash64_string(PG_FUNCTION_ARGS);
42-
Datum pg_hash_int64(PG_FUNCTION_ARGS);
37+
Datum spqr_hash_murmur3_str(PG_FUNCTION_ARGS);
38+
Datum spqr_hash_murmur3_int64(PG_FUNCTION_ARGS);
4339

4440
#endif
4541

0 commit comments

Comments
 (0)