-
-
Notifications
You must be signed in to change notification settings - Fork 26
/
pg_hashids.c
227 lines (188 loc) · 5.71 KB
/
pg_hashids.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#include "postgres.h"
#include "fmgr.h"
#include "funcapi.h"
#include "catalog/pg_type.h"
#include "utils/array.h"
#include "utils/builtins.h"
#include "hashids.h"
#ifdef PG_MODULE_MAGIC
PG_MODULE_MAGIC;
#endif
PG_FUNCTION_INFO_V1(id_encode);
PG_FUNCTION_INFO_V1(id_encode_array);
PG_FUNCTION_INFO_V1(id_decode);
PG_FUNCTION_INFO_V1(id_decode_once);
static void hashids_error()
{
switch (hashids_errno) {
case HASHIDS_ERROR_ALLOC:
ereport(ERROR, (errcode(ERRCODE_OUT_OF_MEMORY), errmsg("allocation failed")));
break;
case HASHIDS_ERROR_ALPHABET_LENGTH:
ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("alphabet is too short")));
break;
case HASHIDS_ERROR_ALPHABET_SPACE:
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("alphabet contains whitespace characters")));
break;
case HASHIDS_ERROR_INVALID_HASH:
ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("invalid hash")));
break;
default:
ereport(ERROR, (errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION), errmsg("unknown error")));
break;
}
}
// based on new_intArrayType contrib/intarray/_int_tool.c
static ArrayType *new_intArrayType(int num)
{
ArrayType *r;
int nbytes = ARR_OVERHEAD_NONULLS(1) + sizeof(int64) * num;
r = (ArrayType *) palloc0(nbytes);
SET_VARSIZE(r, nbytes);
ARR_NDIM(r) = 1;
r->dataoffset = 0; /* marker for no null bitmap */
ARR_ELEMTYPE(r) = INT8OID;
ARR_DIMS(r)[0] = num;
ARR_LBOUND(r)[0] = 1;
return r;
}
Datum
id_encode(PG_FUNCTION_ARGS)
{
// Declaration
int64 number;
text *hash_string;
hashids_t *hashids;
unsigned int bytes_encoded;
char *hash;
// Arguments
number = PG_GETARG_INT64(0);
if (PG_NARGS() == 2) {
hashids = hashids_init2(text_to_cstring(PG_GETARG_TEXT_P(1)), 0);
} else if (PG_NARGS() == 3) {
hashids = hashids_init2(text_to_cstring(PG_GETARG_TEXT_P(1)), PG_GETARG_INT32(2));
} else if (PG_NARGS() == 4) {
hashids = hashids_init3(text_to_cstring(PG_GETARG_TEXT_P(1)), PG_GETARG_INT32(2),
text_to_cstring(PG_GETARG_TEXT_P(3)));
} else {
hashids = hashids_init(NULL);
}
if (!hashids) {
hashids_error();
}
hash = palloc0(hashids_estimate_encoded_size(hashids, 1, (unsigned long long *) &number));
bytes_encoded = hashids_encode_one(hashids, hash, (unsigned long long) number);
hash_string = cstring_to_text_with_len(hash, bytes_encoded);
hashids_free(hashids);
pfree(hash);
PG_RETURN_TEXT_P(hash_string);
}
Datum
id_encode_array(PG_FUNCTION_ARGS)
{
ArrayType *numbers;
size_t numbers_count;
// Declaration
text *hash_string;
hashids_t *hashids;
unsigned int bytes_encoded;
char *hash;
// Arguments
numbers = PG_GETARG_ARRAYTYPE_P(0);
numbers_count = ARR_DIMS(numbers)[0];
if (array_contains_nulls(numbers)) {
ereport(ERROR,
(errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
errmsg("null value not allowed for array element")));
}
if (PG_NARGS() == 2) {
hashids = hashids_init2(text_to_cstring(PG_GETARG_TEXT_P(1)), 0);
} else if (PG_NARGS() == 3) {
hashids = hashids_init2(text_to_cstring(PG_GETARG_TEXT_P(1)), PG_GETARG_INT32(2));
} else if (PG_NARGS() == 4) {
hashids = hashids_init3(text_to_cstring(PG_GETARG_TEXT_P(1)), PG_GETARG_INT32(2),
text_to_cstring(PG_GETARG_TEXT_P(3)));
} else {
hashids = hashids_init(NULL);
}
if (!hashids) {
hashids_error();
}
hash = palloc0(
hashids_estimate_encoded_size(hashids, numbers_count, (unsigned long long *) ARR_DATA_PTR(numbers)));
bytes_encoded = hashids_encode(hashids, hash, numbers_count, (unsigned long long *) ARR_DATA_PTR(numbers));
hash_string = cstring_to_text_with_len(hash, bytes_encoded);
hashids_free(hashids);
pfree(hash);
PG_RETURN_TEXT_P(hash_string);
}
Datum
id_decode(PG_FUNCTION_ARGS)
{
// Declaration
hashids_t *hashids;
int64 *numbers, *resultValues;
char *hash;
size_t numbers_count;
ArrayType *resultArray;
if (PG_NARGS() == 2) {
hashids = hashids_init2(text_to_cstring(PG_GETARG_TEXT_P(1)), 0);
} else if (PG_NARGS() == 3) {
hashids = hashids_init2(text_to_cstring(PG_GETARG_TEXT_P(1)), PG_GETARG_INT32(2));
} else if (PG_NARGS() == 4) {
hashids = hashids_init3(text_to_cstring(PG_GETARG_TEXT_P(1)), PG_GETARG_INT32(2),
text_to_cstring(PG_GETARG_TEXT_P(3)));
} else {
hashids = hashids_init(NULL);
}
if (!hashids) {
hashids_error();
}
hash = text_to_cstring(PG_GETARG_TEXT_P(0));
numbers_count = hashids_numbers_count(hashids, hash);
if (!numbers_count) {
hashids_error();
}
numbers = palloc0(numbers_count * sizeof(int64));
hashids_decode(hashids, hash, (unsigned long long *) numbers, numbers_count);
hashids_free(hashids);
pfree(hash);
resultArray = new_intArrayType(numbers_count);
resultValues = (int64 *) ARR_DATA_PTR(resultArray);
memcpy(resultValues, numbers, numbers_count * sizeof(int64));
pfree(numbers);
PG_RETURN_ARRAYTYPE_P(resultArray);
}
Datum
id_decode_once(PG_FUNCTION_ARGS)
{
// Declaration
hashids_t *hashids;
int64 number;
char *hash;
size_t numbers_count;
if (PG_NARGS() == 2) {
hashids = hashids_init2(text_to_cstring(PG_GETARG_TEXT_P(1)), 0);
} else if (PG_NARGS() == 3) {
hashids = hashids_init2(text_to_cstring(PG_GETARG_TEXT_P(1)), PG_GETARG_INT32(2));
} else if (PG_NARGS() == 4) {
hashids = hashids_init3(text_to_cstring(PG_GETARG_TEXT_P(1)), PG_GETARG_INT32(2),
text_to_cstring(PG_GETARG_TEXT_P(3)));
} else {
hashids = hashids_init(NULL);
}
if (!hashids) {
hashids_error();
}
hash = text_to_cstring(PG_GETARG_TEXT_P(0));
numbers_count = hashids_numbers_count(hashids, hash);
if (!numbers_count) {
hashids_error();
}
hashids_decode(hashids, hash, (unsigned long long *) &number, 1);
hashids_free(hashids);
pfree(hash);
PG_RETURN_INT64(number);
}