This repository has been archived by the owner on Dec 1, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
pg_sphinx.c
360 lines (289 loc) · 9.41 KB
/
pg_sphinx.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
#include <postgres.h>
#include <fmgr.h>
#include <funcapi.h>
#include <utils/builtins.h>
#include <utils/array.h>
#include <utils/elog.h>
#include <catalog/pg_type.h>
#include <executor/spi.h>
#if PG_VERSION_NUM >= 90300
#include <access/htup_details.h>
#endif
#include "dict.h"
#include "sphinx.h"
#ifdef PG_MODULE_MAGIC
PG_MODULE_MAGIC;
#endif
PG_FUNCTION_INFO_V1(pg_sphinx_select);
Datum pg_sphinx_select(PG_FUNCTION_ARGS);
PG_FUNCTION_INFO_V1(pg_sphinx_replace);
Datum pg_sphinx_replace(PG_FUNCTION_ARGS);
PG_FUNCTION_INFO_V1(pg_sphinx_delete);
Datum pg_sphinx_delete(PG_FUNCTION_ARGS);
PG_FUNCTION_INFO_V1(pg_sphinx_snippet);
Datum pg_sphinx_snippet(PG_FUNCTION_ARGS);
PG_FUNCTION_INFO_V1(pg_sphinx_snippet_options);
Datum pg_sphinx_snippet_options(PG_FUNCTION_ARGS);
#define STRLCPY(dst, src, size) \
do { \
strncpy(dst, src, size); \
dst[size - 1] = '\0'; \
} while(0)
#define TO_PSTRING(pstr, d, isnull) \
do { \
if (isnull) \
{ \
pstr.str = NULL; \
pstr.len = 0; \
} \
else \
{ \
pstr.str = VARDATA(d); \
pstr.len = VARSIZE(d) - VARHDRSZ; \
} \
} while(0)
#define VARCHAR_TO_PSTRING(pstr, d, isnull) \
do { \
if (isnull) \
{ \
pstr.str = NULL; \
pstr.len = 0; \
} \
else \
{ \
pstr.str = VARDATA(d); \
pstr.len = VARSIZE(d) - VARHDRSZ; \
} \
} while(0)
static int fetch_config(sphinx_config *config)
{
int result = 0;
int i;
default_config(config);
if (SPI_connect() != SPI_OK_CONNECT)
goto end;
if (SPI_execute("SELECT \"key\", \"value\" FROM sphinx_config", true, 0) != SPI_OK_SELECT)
goto end;
for (i = 0; i < SPI_processed; ++i)
{
char *key, *val;
key = SPI_getvalue(SPI_tuptable->vals[i], SPI_tuptable->tupdesc, 1);
val = SPI_getvalue(SPI_tuptable->vals[i], SPI_tuptable->tupdesc, 2);
if (!strcmp(key, "host"))
STRLCPY(config->host, val, sizeof(config->host));
else if (!strcmp(key, "port"))
config->port = atoi(val);
else if (!strcmp(key, "username"))
STRLCPY(config->username, val, sizeof(config->username));
else if (!strcmp(key, "password"))
STRLCPY(config->password, val, sizeof(config->password));
else if (!strcmp(key, "prefix"))
STRLCPY(config->prefix, val, sizeof(config->prefix));
pfree(key);
pfree(val);
}
result = 1;
end:
SPI_finish();
return result;
}
Datum pg_sphinx_select(PG_FUNCTION_ARGS)
{
FuncCallContext *funcctx;
sphinx_context ctx;
int id, weight;
char *error = NULL;
if (SRF_IS_FIRSTCALL())
{
MemoryContext oldcontext;
funcctx = SRF_FIRSTCALL_INIT();
oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
funcctx->tuple_desc = BlessTupleDesc(RelationNameGetTupleDesc("sphinx_search_result"));
if (PG_ARGISNULL(0))
{
funcctx->user_fctx = NULL;
elog(ERROR, "%s", "'index' parameter has to be a valid name of search index but NULL was given.");
}
else if (PG_ARGISNULL(1))
{
funcctx->user_fctx = NULL;
elog(ERROR, "%s", "'query' parameter has to be a search query but NULL was given.");
}
else
{
PString index = {0, 0}, query = {0, 0}, condition = {0, 0}, order = {0, 0}, options = {0, 0};
int offset, limit;
sphinx_config config;
TO_PSTRING(index, PG_GETARG_DATUM(0), 0);
TO_PSTRING(query, PG_GETARG_DATUM(1), 0);
TO_PSTRING(condition, PG_GETARG_DATUM(2), PG_ARGISNULL(2));
TO_PSTRING(order, PG_GETARG_DATUM(3), PG_ARGISNULL(3));
offset = PG_ARGISNULL(4) ? 0 : PG_GETARG_UINT32(4);
limit = PG_ARGISNULL(5) ? 20 : PG_GETARG_UINT32(5);
TO_PSTRING(options, PG_GETARG_DATUM(6), PG_ARGISNULL(6));
fetch_config(&config);
funcctx->user_fctx = sphinx_select(&config, &index, &query, &condition, &order, offset, limit, &options, &error);
}
if (error) {
elog(ERROR, "%s", error);
free(error);
error = NULL;
}
MemoryContextSwitchTo(oldcontext);
}
funcctx = SRF_PERCALL_SETUP();
ctx = funcctx->user_fctx;
if (sphinx_context_next(ctx, &id, &weight))
{
Datum result;
Datum values[2];
char nulls[2] = {0, 0};
values[0] = Int32GetDatum(id);
values[1] = Int32GetDatum(weight);
result = HeapTupleGetDatum(heap_form_tuple(funcctx->tuple_desc, values, nulls));
SRF_RETURN_NEXT(funcctx, result);
}
else
{
sphinx_context_free(ctx);
SRF_RETURN_DONE(funcctx);
}
}
static int array_to_dict(ArrayType *input, Dict *dict)
{
ArrayIterator iter;
size_t i;
Datum value;
bool isnull;
// TODO: check element type. ARR_ELEMTYPE(input)
// one dimensional even length array
if (ARR_NDIM(input) != 1 || ARR_DIMS(input)[0] % 2 == 1)
return -1;
dict->len = ARR_DIMS(input)[0] / 2;
dict->names = palloc(sizeof(PString) * dict->len);
dict->values = palloc(sizeof(PString) * dict->len);
#if PG_VERSION_NUM >= 90500
iter = array_create_iterator(input, 0, NULL);
#else
iter = array_create_iterator(input, 0);
#endif
i = 0;
for (; array_iterate(iter, &value, &isnull); ++i)
{
if (i % 2 == 0)
TO_PSTRING(dict->names[i / 2], value, isnull);
else
TO_PSTRING(dict->values[i / 2], value, isnull);
}
array_free_iterator(iter);
return 0;
}
Datum pg_sphinx_replace(PG_FUNCTION_ARGS)
{
PString index = {0, 0};
int id;
ArrayType *input;
char *error = NULL;
sphinx_config config;
Dict data;
if (PG_ARGISNULL(0) || PG_ARGISNULL(1) || PG_ARGISNULL(2))
PG_RETURN_VOID();
TO_PSTRING(index, PG_GETARG_DATUM(0), 0);
id = PG_GETARG_UINT32(1);
input = PG_GETARG_ARRAYTYPE_P(2);
if (array_to_dict(input, &data))
PG_RETURN_VOID();
fetch_config(&config);
sphinx_replace(&config, &index, id, &data, &error);
if (error) {
elog(ERROR, "%s", error);
free(error);
}
pfree(data.names);
pfree(data.values);
PG_RETURN_VOID();
}
Datum pg_sphinx_delete(PG_FUNCTION_ARGS)
{
PString index = {0, 0};
int id;
char *error = NULL;
sphinx_config config;
if (PG_ARGISNULL(0) || PG_ARGISNULL(1))
PG_RETURN_VOID();
TO_PSTRING(index, PG_GETARG_DATUM(0), 0);
id = PG_GETARG_UINT32(1);
fetch_config(&config);
sphinx_delete(&config, &index, id, &error);
if (error) {
elog(ERROR, "%s", error);
free(error);
}
PG_RETURN_VOID();
}
static void return_text(void *data, size_t size, void *user_data)
{
if (user_data)
{
text *r = palloc(VARHDRSZ + size);
SET_VARSIZE(r, VARHDRSZ + size);
memcpy((void *)VARDATA(r), data, size);
*(text **)user_data = r;
}
}
Datum pg_sphinx_snippet(PG_FUNCTION_ARGS)
{
PString index = {0, 0}, match = {0, 0}, data = {0, 0};
PString option_names[2] = {PSTRING_CONST("before_match"), PSTRING_CONST("after_match")};
PString option_values[2] = {{0, 0}, {0, 0}};
Dict options = {2, option_names, option_values};
text *result_text = NULL;
char *error = NULL;
sphinx_config config;
if (PG_ARGISNULL(0) || PG_ARGISNULL(1) || PG_ARGISNULL(2))
PG_RETURN_NULL();
VARCHAR_TO_PSTRING(index, PG_GETARG_VARCHAR_P(0), 0);
VARCHAR_TO_PSTRING(match, PG_GETARG_VARCHAR_P(1), 0);
VARCHAR_TO_PSTRING(data, PG_GETARG_VARCHAR_P(2), 0);
VARCHAR_TO_PSTRING(options.values[0], PG_GETARG_VARCHAR_P(3), PG_ARGISNULL(3));
VARCHAR_TO_PSTRING(options.values[1], PG_GETARG_VARCHAR_P(4), PG_ARGISNULL(4));
fetch_config(&config);
sphinx_snippet(&config, &index, &match, &data, &options,
return_text, &result_text, &error);
if (error) {
elog(ERROR, "%s", error);
free(error);
}
if (result_text)
PG_RETURN_TEXT_P(result_text);
else
PG_RETURN_NULL();
}
Datum pg_sphinx_snippet_options(PG_FUNCTION_ARGS)
{
PString index = {0, 0}, match = {0, 0}, data = {0, 0};
ArrayType *input;
Dict options;
text *result_text = NULL;
char *error = NULL;
sphinx_config config;
if (PG_ARGISNULL(0) || PG_ARGISNULL(1) || PG_ARGISNULL(2) || PG_ARGISNULL(3))
PG_RETURN_NULL();
VARCHAR_TO_PSTRING(index, PG_GETARG_VARCHAR_P(0), 0);
VARCHAR_TO_PSTRING(match, PG_GETARG_VARCHAR_P(1), 0);
VARCHAR_TO_PSTRING(data, PG_GETARG_VARCHAR_P(2), 0);
input = PG_GETARG_ARRAYTYPE_P(3);
if (array_to_dict(input, &options))
PG_RETURN_NULL();
fetch_config(&config);
sphinx_snippet(&config, &index, &match, &data, &options,
return_text, &result_text, &error);
if (error) {
elog(ERROR, "%s", error);
free(error);
}
if (result_text)
PG_RETURN_TEXT_P(result_text);
else
PG_RETURN_NULL();
}