-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvgram_like.c
executable file
·227 lines (203 loc) · 4.75 KB
/
vgram_like.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
/*-------------------------------------------------------------------------
*
* vgram_like.c
* Routines for using index over V-grams to accelerate like/ilike
* queries.
*
* Copyright (c) 2011-2017, Alexander Korotkov
*
* IDENTIFICATION
* contrib/vgram/vgram_like.c
*
*-------------------------------------------------------------------------
*/
#include "postgres.h"
#include "fmgr.h"
#include "utils/builtins.h"
#include "vgram.h"
#define ISESCAPECHAR(x) (*(x) == '\\') /* Wildcard escape character */
#define ISWILDCARDCHAR(x) (*(x) == '_' || *(x) == '%') /* Wildcard
* meta-character */
/*
* Extract the next non-wildcard part of a search string, ie, a word bounded
* by '_' or '%' meta-characters, non-word characters or string end.
*
* str: source string, of length lenstr bytes (need not be null-terminated)
* buf: where to return the substring (must be long enough)
* *bytelen: receives byte length of the found substring
* *charlen: receives character length of the found substring
*
* Returns pointer to end+1 of the found substring in the source string.
* Returns NULL if no word found (in which case buf, bytelen, charlen not set)
*
* If the found word is bounded by non-word characters or string boundaries
* then this function will include corresponding padding spaces into buf.
*/
static const char *
get_wildcard_part(const char *str, int lenstr,
char *buf, int *bytelen, int *charlen)
{
const char *beginword = str;
const char *endword;
char *s = buf;
bool in_wildcard_meta = false;
bool in_escape = false;
int clen;
/*
* Find the first word character remembering whether last character was
* wildcard meta-character.
*/
while (beginword - str < lenstr)
{
if (in_escape)
{
in_escape = false;
in_wildcard_meta = false;
if (isExtractable(beginword))
break;
}
else
{
if (ISESCAPECHAR(beginword))
in_escape = true;
else if (ISWILDCARDCHAR(beginword))
in_wildcard_meta = true;
else if (isExtractable(beginword))
break;
else
in_wildcard_meta = false;
}
beginword += pg_mblen(beginword);
}
/*
* Handle string end.
*/
if (beginword - str >= lenstr)
return NULL;
/*
* Add left padding spaces if last character wasn't wildcard
* meta-character.
*/
*charlen = 0;
if (!in_wildcard_meta)
{
*s++ = EMPTY_CHARACTER;
(*charlen)++;
}
/*
* Copy data into buf until wildcard meta-character, non-word character or
* string boundary. Strip escapes during copy.
*/
endword = beginword;
in_wildcard_meta = false;
in_escape = false;
while (endword - str < lenstr)
{
clen = pg_mblen(endword);
if (in_escape)
{
in_escape = false;
in_wildcard_meta = false;
if (isExtractable(endword))
{
memcpy(s, endword, clen);
(*charlen)++;
s += clen;
}
else
break;
}
else
{
if (ISESCAPECHAR(endword))
in_escape = true;
else if (ISWILDCARDCHAR(endword))
{
in_wildcard_meta = true;
break;
}
else if (isExtractable(endword))
{
memcpy(s, endword, clen);
(*charlen)++;
s += clen;
}
else
{
in_wildcard_meta = false;
break;
}
}
endword += clen;
}
/*
* Add right padding spaces if last character wasn't wildcard
* meta-character.
*/
if (!in_wildcard_meta)
{
*s++ = EMPTY_CHARACTER;
(*charlen)++;
}
*bytelen = s - buf;
return endword;
}
#define OPTIMAL_VGRAM_COUNT 5
typedef struct
{
char **data;
int count;
int allocated;
} VGramInfo;
static void
addVGram(char *vgram, void *userData)
{
VGramInfo *vgrams = (VGramInfo *) userData;
if (vgrams->count >= vgrams->allocated)
{
vgrams->allocated *= 2;
vgrams->data = (char **) repalloc(vgrams->data, sizeof(char *) * vgrams->allocated);
}
vgrams->data[vgrams->count] = vgram;
vgrams->count++;
}
Datum *
extractQueryLike(int32 *nentries, text *pattern)
{
char *buf,
*buf2;
const char *eword,
*str;
int len,
bytelen,
charlen,
i;
VGramInfo vgrams;
ExtractVGramsInfo userData;
Datum *entries;
userData.callback = addVGram;
userData.userData = (void *) &vgrams;
vgrams.count = 0;
vgrams.allocated = 16;
vgrams.data = (char **) palloc(sizeof(char *) * vgrams.allocated);
str = (char *) VARDATA_ANY(pattern);
len = VARSIZE_ANY_EXHDR(pattern);
buf = (char *) palloc(len + 3);
eword = str;
while ((eword = get_wildcard_part(eword, len - (eword - str),
buf, &bytelen, &charlen)) != NULL)
{
buf2 = lowerstr_with_len(buf, bytelen);
bytelen = strlen(buf2);
extractMinimalVGramsWord(buf2, buf2 + bytelen, &userData);
pfree(buf2);
}
pfree(buf);
*nentries = vgrams.count;
entries = (Datum *) palloc(sizeof(Datum) * vgrams.count);
for (i = 0; i < vgrams.count; i++)
{
entries[i] = PointerGetDatum(cstring_to_text(vgrams.data[i]));
}
return entries;
}