-
Notifications
You must be signed in to change notification settings - Fork 1
/
vgram_gin.c
executable file
·267 lines (216 loc) · 5.61 KB
/
vgram_gin.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
/*-------------------------------------------------------------------------
*
* vgram_gin.c
* Routines for GIN indexing of V-grams extracted from strings.
*
* Copyright (c) 2011-2017, Alexander Korotkov
*
* IDENTIFICATION
* contrib/vgram/vgram_gin.c
*
*-------------------------------------------------------------------------
*/
#include "postgres.h"
#include "access/gin.h"
#include "access/skey.h"
#include "fmgr.h"
#include "utils/builtins.h"
#include "vgram.h"
Datum vgram_cmp(PG_FUNCTION_ARGS);
PG_FUNCTION_INFO_V1(vgram_cmp);
Datum vgram_gin_extract_value(PG_FUNCTION_ARGS);
PG_FUNCTION_INFO_V1(vgram_gin_extract_value);
Datum vgram_gin_consistent(PG_FUNCTION_ARGS);
PG_FUNCTION_INFO_V1(vgram_gin_consistent);
Datum vgram_gin_triconsistent(PG_FUNCTION_ARGS);
PG_FUNCTION_INFO_V1(vgram_gin_triconsistent);
Datum vgram_gin_extract_query(PG_FUNCTION_ARGS);
PG_FUNCTION_INFO_V1(vgram_gin_extract_query);
static int
vgram_cmp_internal(Datum d1, Datum d2)
{
text *vgram1 = DatumGetTextPP(d1);
text *vgram2 = DatumGetTextPP(d2);
int len1,
len2,
cmp;
len1 = VARSIZE_ANY_EXHDR(vgram1);
len2 = VARSIZE_ANY_EXHDR(vgram2);
cmp = strncmp(VARDATA_ANY(vgram1), VARDATA_ANY(vgram2), Min(len1, len2));
if (cmp != 0)
return cmp;
if (len1 < len2)
return -1;
else if (len1 == len2)
return 0;
else
return 1;
}
static int
vgram_sort_cmp(const void *v1, const void *v2)
{
return vgram_cmp_internal(*((Datum *) v1),
*((Datum *) v2));
}
static void
entries_unique(Datum *entries, int32 *nentries)
{
int32 n = *nentries,
i,
j = 0;
if (n == 0)
return;
qsort(entries, n, sizeof(Datum *), vgram_sort_cmp);
for (i = 1; i < n; i++)
{
if (vgram_cmp_internal(entries[i], entries[j]))
{
j++;
entries[j] = entries[i];
}
}
*nentries = j + 1;
}
Datum
vgram_cmp(PG_FUNCTION_ARGS)
{
Datum d1 = PG_GETARG_DATUM(0);
Datum d2 = PG_GETARG_DATUM(1);
PG_RETURN_INT32(vgram_cmp_internal(d1, d2));
}
typedef struct
{
Datum *entries;
int32 nentries;
int32 allocatedEntries;
} ExtractValueInfo;
static void
extractVGram(char *vgram, void *userData)
{
ExtractValueInfo *info = (ExtractValueInfo *) userData;
info->nentries++;
if (info->nentries > info->allocatedEntries)
{
info->allocatedEntries *= 2;
info->entries = (Datum *) repalloc(info->entries, sizeof(Datum) * info->allocatedEntries);
}
info->entries[info->nentries - 1] = PointerGetDatum(cstring_to_text(vgram));
pfree(vgram);
}
Datum
vgram_gin_extract_value(PG_FUNCTION_ARGS)
{
text *s = (text *) PG_GETARG_TEXT_PP(0);
int32 *nentries = (int32 *) PG_GETARG_POINTER(1);
ExtractValueInfo info;
ExtractVGramsInfo userData;
loadStats();
info.nentries = 0;
info.allocatedEntries = 4;
info.entries = (Datum *) palloc(sizeof(Datum) * info.allocatedEntries);
userData.callback = extractVGram;
userData.userData = &info;
extractWords(VARDATA_ANY(s), VARSIZE_ANY_EXHDR(s), extractMinimalVGramsWord, &userData);
PG_FREE_IF_COPY(s, 0);
entries_unique(info.entries, &info.nentries);
*nentries = info.nentries;
PG_RETURN_POINTER(info.entries);
}
Datum
vgram_gin_consistent(PG_FUNCTION_ARGS)
{
bool *check = (bool *) PG_GETARG_POINTER(0);
StrategyNumber strategy = PG_GETARG_UINT16(1);
/* text *query = PG_GETARG_TEXT_P(2); */
int32 nkeys = PG_GETARG_INT32(3);
/* Pointer *extra_data = (Pointer *) PG_GETARG_POINTER(4); */
bool *recheck = (bool *) PG_GETARG_POINTER(5);
bool res;
int32 i;
/* All cases served by this function are inexact */
*recheck = true;
switch (strategy)
{
case ILikeStrategyNumber:
case LikeStrategyNumber:
/* Check if all extracted trigrams are presented. */
res = true;
for (i = 0; i < nkeys; i++)
{
if (!check[i])
{
res = false;
break;
}
}
break;
default:
elog(ERROR, "unrecognized strategy number: %d", strategy);
res = false; /* keep compiler quiet */
break;
}
PG_RETURN_BOOL(res);
}
Datum
vgram_gin_triconsistent(PG_FUNCTION_ARGS)
{
GinTernaryValue *check = (GinTernaryValue *) PG_GETARG_POINTER(0);
StrategyNumber strategy = PG_GETARG_UINT16(1);
/* text *query = PG_GETARG_TEXT_P(2); */
int32 nkeys = PG_GETARG_INT32(3);
/* Pointer *extra_data = (Pointer *) PG_GETARG_POINTER(4); */
GinTernaryValue res = GIN_MAYBE;
int32 i;
switch (strategy)
{
case ILikeStrategyNumber:
case LikeStrategyNumber:
/* Check if all extracted trigrams are presented. */
for (i = 0; i < nkeys; i++)
{
if (check[i] == GIN_FALSE)
{
res = GIN_FALSE;
break;
}
}
break;
default:
elog(ERROR, "unrecognized strategy number: %d", strategy);
res = false; /* keep compiler quiet */
break;
}
/* All cases served by this function are inexact */
Assert(res != GIN_TRUE);
PG_RETURN_GIN_TERNARY_VALUE(res);
}
Datum
vgram_gin_extract_query(PG_FUNCTION_ARGS)
{
text *val = (text *) PG_GETARG_TEXT_P(0);
int32 *nentries = (int32 *) PG_GETARG_POINTER(1);
StrategyNumber strategy = PG_GETARG_UINT16(2);
/* bool **pmatch = (bool **) PG_GETARG_POINTER(3); */
/* Pointer *extra_data = (Pointer *) PG_GETARG_POINTER(4); */
/* bool **nullFlags = (bool **) PG_GETARG_POINTER(5); */
int32 *searchMode = (int32 *) PG_GETARG_POINTER(6);
Datum *entries = NULL;
loadStats();
switch (strategy)
{
case ILikeStrategyNumber:
case LikeStrategyNumber:
entries = extractQueryLike(nentries, val);
break;
default:
elog(ERROR, "unrecognized strategy number: %d", strategy);
break;
}
entries_unique(entries, nentries);
/*
* If no trigram was extracted then we have to scan all the index.
*/
if (*nentries == 0)
*searchMode = GIN_SEARCH_MODE_ALL;
PG_RETURN_POINTER(entries);
}