-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpg_xxhash.c
161 lines (107 loc) · 3.71 KB
/
pg_xxhash.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include "postgres.h"
#include <stdio.h>
#include <string.h>
#include "fmgr.h"
#include "libpq/pqformat.h"
#include "utils/builtins.h"
#define XXH_INLINE_ALL
#include "xxhash.h"
#include "xsum_config.h"
#include "pg_xxhash.h"
PG_MODULE_MAGIC;
/* xxhash, 32-bit */
PG_FUNCTION_INFO_V1(pg_xxh32);
Datum pg_xxh32(PG_FUNCTION_ARGS) {
char *result = malloc(8);
char *input;
input = text_to_cstring(PG_GETARG_TEXT_P(0));
XXH32_hash_t const h = XXH32(input, strlen(input), 0);
sprintf(result, "%08x", (unsigned)h);
PG_RETURN_VARCHAR_P(cstring_to_text(result));
}
PG_FUNCTION_INFO_V1(pg_xxh32b);
Datum pg_xxh32b(PG_FUNCTION_ARGS) {
char *input;
input = text_to_cstring(PG_GETARG_TEXT_P(0));
XXH32_hash_t const h = XXH32(input, strlen(input), 0);
StringInfoData buf;
pq_begintypsend(&buf);
pq_sendint32(&buf, (unsigned)h);
PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
}
/* xxhash, 64-bit */
PG_FUNCTION_INFO_V1(pg_xxh64);
Datum pg_xxh64(PG_FUNCTION_ARGS) {
char *result = malloc(16);
char *input;
input = text_to_cstring(PG_GETARG_TEXT_P(0));
XXH64_hash_t const h = XXH64(input, strlen(input), 0);
sprintf(result, "%08x%08x", (unsigned)(h >> 32), (unsigned)h);
PG_RETURN_VARCHAR_P(cstring_to_text(result));
}
PG_FUNCTION_INFO_V1(pg_xxh64b);
Datum pg_xxh64b(PG_FUNCTION_ARGS) {
char *input;
input = text_to_cstring(PG_GETARG_TEXT_P(0));
XXH64_hash_t const h = XXH64(input, strlen(input), 0);
StringInfoData buf;
pq_begintypsend(&buf);
pq_sendint32(&buf, (unsigned)(h >> 32));
pq_sendint32(&buf, (unsigned)h);
PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
}
/* xxhash3, 64-bit */
PG_FUNCTION_INFO_V1(pg_xxh3_64);
Datum pg_xxh3_64(PG_FUNCTION_ARGS) {
char *result = malloc(16);
char *input;
input = text_to_cstring(PG_GETARG_TEXT_P(0));
XXH64_hash_t const h = XXH3_64bits(input, strlen(input));
sprintf(result, "%08x%08x", (unsigned)(h >> 32), (unsigned)h);
PG_RETURN_VARCHAR_P(cstring_to_text(result));
}
PG_FUNCTION_INFO_V1(pg_xxh3_64b);
Datum pg_xxh3_64b(PG_FUNCTION_ARGS) {
char *input;
input = text_to_cstring(PG_GETARG_TEXT_P(0));
XXH64_hash_t const h = XXH3_64bits(input, strlen(input));
StringInfoData buf;
pq_begintypsend(&buf);
pq_sendint32(&buf, (unsigned)(h >> 32));
pq_sendint32(&buf, (unsigned)h);
PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
}
/* xxhash3, 128-bit */
PG_FUNCTION_INFO_V1(pg_xxh3_128);
Datum pg_xxh3_128(PG_FUNCTION_ARGS) {
char *result = malloc(32);
char *input;
input = text_to_cstring(PG_GETARG_TEXT_P(0));
XXH128_hash_t const h = XXH3_128bits(input, strlen(input));
XXH128_canonical_t hcbe128;
(void)XXH128_canonicalFromHash(&hcbe128, h);
const XSUM_U8* const p = (const XSUM_U8*) &hcbe128;
sprintf(
result,
"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7], p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]
);
PG_RETURN_VARCHAR_P(cstring_to_text(result));
}
PG_FUNCTION_INFO_V1(pg_xxh3_128b);
Datum pg_xxh3_128b(PG_FUNCTION_ARGS) {
char *input;
input = text_to_cstring(PG_GETARG_TEXT_P(0));
XXH128_hash_t const h = XXH3_128bits(input, strlen(input));
XXH128_canonical_t hcbe128;
(void)XXH128_canonicalFromHash(&hcbe128, h);
// const XSUM_U32* const p = (const XSUM_U32*) &hcbe128;
const XSUM_U32* const p = (const XSUM_U32*) &h;
StringInfoData buf;
pq_begintypsend(&buf);
pq_sendint32(&buf, (unsigned)p[3]);
pq_sendint32(&buf, (unsigned)p[2]);
pq_sendint32(&buf, (unsigned)p[1]);
pq_sendint32(&buf, (unsigned)p[0]);
PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
}