-
Notifications
You must be signed in to change notification settings - Fork 5
/
tsexample.c
243 lines (206 loc) · 4.75 KB
/
tsexample.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
/*-------------------------------------------------------------------------
*
* tsexample.c
* Example of custom full text search parser and dictionaries
*
* Copyright (c) 2016, Postgres Professional
* Author: Alexander Korotkov <[email protected]>
*
* IDENTIFICATION
* contrib/tsexample/tsexample.c
*
*-------------------------------------------------------------------------
*/
#include "postgres.h"
#include "commands/defrem.h"
#include "tsearch/ts_public.h"
#include "tsearch/ts_locale.h"
typedef struct
{
char *begin;
char *end;
char *p;
} SParserStatus;
/* Output token categories */
#define WORD_TOKEN 1
#define NUMBER_TOKEN 2
#define LAST_TOKEN_NUM 2
static const char *const tok_alias[] = {
"",
"word",
"number"
};
static const char *const lex_descr[] = {
"",
"Word, all alphanumeric characters",
"Number, all digits"
};
PG_MODULE_MAGIC;
PG_FUNCTION_INFO_V1(sparser_start);
PG_FUNCTION_INFO_V1(sparser_nexttoken);
PG_FUNCTION_INFO_V1(sparser_end);
PG_FUNCTION_INFO_V1(sparser_lextype);
PG_FUNCTION_INFO_V1(cutdict_init);
PG_FUNCTION_INFO_V1(cutdict_lexize);
Datum
sparser_start(PG_FUNCTION_ARGS)
{
SParserStatus *status = (SParserStatus *) palloc0(sizeof(SParserStatus));
status->begin = (char *) PG_GETARG_POINTER(0);
status->end = status->begin + PG_GETARG_INT32(1);
status->p = status->begin;
PG_RETURN_POINTER(status);
}
Datum
sparser_nexttoken(PG_FUNCTION_ARGS)
{
SParserStatus *status = (SParserStatus *) PG_GETARG_POINTER(0);
char **t = (char **) PG_GETARG_POINTER(1);
int *tlen = (int *) PG_GETARG_POINTER(2);
bool found = false,
has_nondigit = false;
while (status->p < status->end)
{
int p_len = pg_mblen(status->p);
if (t_isalpha(status->p) || t_isdigit(status->p) ||
(p_len == 1 && *status->p == '_'))
{
if (!t_isdigit(status->p))
has_nondigit = true;
if (!found)
{
*t = status->p;
found = true;
}
}
else
{
if (found)
break;
}
status->p += p_len;
}
if (found)
{
*tlen = status->p - *t;
if (has_nondigit)
PG_RETURN_INT32(WORD_TOKEN);
else
PG_RETURN_INT32(NUMBER_TOKEN);
}
else
{
PG_RETURN_INT32(0);
}
}
Datum
sparser_end(PG_FUNCTION_ARGS)
{
SParserStatus *status = (SParserStatus *) PG_GETARG_POINTER(0);
pfree(status);
PG_RETURN_VOID();
}
Datum
sparser_lextype(PG_FUNCTION_ARGS)
{
LexDescr *descr = (LexDescr *) palloc(sizeof(LexDescr) * (LAST_TOKEN_NUM + 1));
int i;
for (i = 1; i <= LAST_TOKEN_NUM; i++)
{
descr[i - 1].lexid = i;
descr[i - 1].alias = pstrdup(tok_alias[i]);
descr[i - 1].descr = pstrdup(lex_descr[i]);
}
descr[LAST_TOKEN_NUM].lexid = 0;
PG_RETURN_POINTER(descr);
}
typedef struct
{
int nbegin;
int nend;
} CutDict;
Datum
cutdict_init(PG_FUNCTION_ARGS)
{
List *dictoptions = (List *) PG_GETARG_POINTER(0);
CutDict *d = (CutDict *) palloc0(sizeof(CutDict));
bool nbegin_loaded = false,
nend_loaded = false;
ListCell *l;
foreach(l, dictoptions)
{
DefElem *defel = (DefElem *) lfirst(l);
if (pg_strcasecmp("nbegin", defel->defname) == 0)
{
if (nbegin_loaded)
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("multiple nbegin parameters")));
d->nbegin = atoi(defGetString(defel));
nbegin_loaded = true;
}
else if (pg_strcasecmp("nend", defel->defname) == 0)
{
if (nend_loaded)
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("multiple nend parameters")));
d->nend = atoi(defGetString(defel));
nend_loaded = true;
}
else
{
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("unrecognized cut dictionary parameter: \"%s\"",
defel->defname)));
}
}
if (!nbegin_loaded || !nend_loaded)
{
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("both nbegin and nend parameters of cut dictionary must be specified")));
}
PG_RETURN_POINTER(d);
}
Datum
cutdict_lexize(PG_FUNCTION_ARGS)
{
CutDict *d = (CutDict *) PG_GETARG_POINTER(0);
char *in = (char *) PG_GETARG_POINTER(1);
int32 len = PG_GETARG_INT32(2);
char *txt;
int strlen;
TSLexeme *res;
int residx = 0;
uint16 nvariant = 1;
res = palloc0(sizeof(TSLexeme) * 4);
txt = lowerstr_with_len(in, len);
strlen = pg_mbstrlen(txt);
if (strlen <= d->nbegin + d->nend)
{
res[residx].nvariant = nvariant;
res[residx++].lexeme = txt;
nvariant++;
}
if (strlen > d->nbegin)
{
int i;
char *p = txt;
for (i = 0; i < d->nbegin; i++)
p += pg_mblen(p);
res[residx].nvariant = nvariant;
res[residx++].lexeme = pnstrdup(txt, p - txt);
}
if (strlen > d->nend)
{
int i;
char *p = txt;
for (i = 0; i < strlen - d->nend; i++)
p += pg_mblen(p);
res[residx].nvariant = nvariant;
res[residx++].lexeme = pstrdup(p);
}
PG_RETURN_POINTER(res);
}