forked from eulerto/pg_similarity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
similarity_gin.c
227 lines (177 loc) · 4.44 KB
/
similarity_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
/*----------------------------------------------------------------------------
*
* similarity_gin.c
*
* GIN support routines
*
* Copyright (c) 2008-2020, Euler Taveira de Oliveira
*
*----------------------------------------------------------------------------
*/
#include "postgres.h"
#include "access/gin.h"
#include "access/skey.h"
/*
#include "access/reloptions.h"
#include "utils/guc.h"
#include "utils/syscache.h"
*/
#include "similarity.h"
#include "tokenizer.h"
/* choose one of them */
/*
#define PGS_BY_WORD 1
*/
#define PGS_BY_ALNUM 1
/*
#define PGS_BY_GRAM 1
#define PGS_BY_CAMELCASE 1
*/
PG_FUNCTION_INFO_V1(gin_extract_value_token);
Datum gin_extract_value_token(PG_FUNCTION_ARGS);
PG_FUNCTION_INFO_V1(gin_extract_query_token);
Datum gin_extract_query_token(PG_FUNCTION_ARGS);
PG_FUNCTION_INFO_V1(gin_token_consistent);
Datum gin_token_consistent(PG_FUNCTION_ARGS);
Datum
gin_extract_value_token(PG_FUNCTION_ARGS)
{
text *value = (text *) PG_GETARG_TEXT_P(0);
int32 *ntokens = (int32 *) PG_GETARG_POINTER(1);
Datum *tokens = NULL;
char *buf;
elog(DEBUG3, "gin_extract_value_token() called");
buf = text_to_cstring(value);
*ntokens = 0;
if (buf != NULL)
{
TokenList *tlist;
Token *t;
tlist = initTokenList(1);
/*
* TODO we want to index according to out GUCs
* TODO we need to store the tokenized-by information
* TODO so we can decide to use or not the index for a
* TODO given query
*/
#ifdef PGS_BY_WORD
tokenizeBySpace(tlist, buf);
#elif PGS_BY_ALNUM
tokenizeByNonAlnum(tlist, buf);
#elif PGS_BY_GRAM
tokenizeByGram(tlist, buf);
#elif PGS_BY_CAMELCASE
tokenizeByCamelCase(tlist, buf);
#else
elog(ERROR, "choose a supported tokenizer");
#endif
*ntokens = tlist->size;
if (tlist->size > 0)
{
int i;
tokens = (Datum *) palloc(sizeof(Datum) * tlist->size);
t = tlist->head;
for (i = 0; i < tlist->size; i++)
{
text *td;
td = cstring_to_text_with_len(t->data, strlen(t->data));
tokens[i] = PointerGetDatum(td);
t = t->next;
}
}
destroyTokenList(tlist);
}
PG_FREE_IF_COPY(value, 0);
PG_RETURN_POINTER(tokens);
}
Datum
gin_extract_query_token(PG_FUNCTION_ARGS)
{
text *value = (text *) PG_GETARG_TEXT_P(0);
int32 *ntokens = (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);
*/
#if PG_VERSION_NUM >= 90100
/*
bool **null_flags = (bool **) PG_GETARG_POINTER(5);
*/
int32 *search_mode = (int32 *) PG_GETARG_POINTER(6);
#endif
Datum *tokens = NULL;
char *buf;
elog(DEBUG3, "gin_extract_query_token() called");
buf = text_to_cstring(value);
*ntokens = 0;
if (buf != NULL)
{
TokenList *tlist;
Token *t;
tlist = initTokenList(1);
/*
* TODO we want to index according to out GUCs
* TODO we need to store the tokenized-by information
* TODO so we can decide to use or not the index for a
* TODO given query
*/
#ifdef PGS_BY_WORD
tokenizeBySpace(tlist, buf);
#elif PGS_BY_ALNUM
tokenizeByNonAlnum(tlist, buf);
#elif PGS_BY_GRAM
tokenizeByGram(tlist, buf);
#elif PGS_BY_CAMELCASE
tokenizeByCamelCase(tlist, buf);
#else
elog(ERROR, "choose a supported tokenizer");
#endif
*ntokens = tlist->size;
if (tlist->size > 0)
{
int i;
tokens = (Datum *) palloc(sizeof(Datum) * tlist->size);
t = tlist->head;
for (i = 0; i < tlist->size; i++)
{
text *td;
td = cstring_to_text_with_len(t->data, strlen(t->data));
tokens[i] = PointerGetDatum(td);
t = t->next;
}
}
destroyTokenList(tlist);
}
#if PG_VERSION_NUM >= 90100
if (*ntokens == 0)
*search_mode = GIN_SEARCH_MODE_ALL;
#endif
PG_FREE_IF_COPY(value, 0);
PG_RETURN_POINTER(tokens);
}
Datum
gin_token_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 ntokens = PG_GETARG_INT32(3);
Pointer *extra_data = (Pointer *) PG_GETARG_POINTER(4);
*/
bool *recheck = (bool *) PG_GETARG_POINTER(5);
/*
#if PG_VERSION_NUM >= 90100
Datum **query_tokens = PG_GETARG_POINTER(6);
bool **null_flags = (bool **) PG_GETARG_POINTER(7);
#endif
*/
elog(DEBUG3, "gin_token_consistent() called");
/*
* Heap tuple might match the query. Evaluating the query operator directly
* against the originally indexed item.
*/
*recheck = true;
PG_RETURN_BOOL(true);
}