-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathatom_ngram_parser.c
537 lines (447 loc) · 13.3 KB
/
atom_ngram_parser.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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
/* Copyright (c) 2016, [email protected]. All rights reserved.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
// First include (the generated) my_config.h, to get correct platform defines.
#include "my_config.h"
#include "mysqld_error.h"
#include <stdlib.h>
#include <ctype.h>
#include <mysql/plugin_ftparser.h>
#include <m_ctype.h>
/* atom-ngram token size, by default bigram. */
static int atom_ngram_token_size;
/*
atom_ngram_parser interface functions:
Plugin declaration functions:
- atom_ngram_parser_plugin_init()
- atom_ngram_parser_plugin_deinit()
Parser descriptor functions:
- atom_ngram_parser_parse()
- atom_ngram_parser_init()
- atom_ngram_parser_deinit()
*/
/*
Initialize the parser plugin at server start or plugin installation.
SYNOPSIS
atom_ngram_parser_plugin_init()
DESCRIPTION
Does nothing.
RETURN VALUE
0 success
1 failure (cannot happen)
*/
static int atom_ngram_parser_plugin_init(void *arg __attribute__((unused)))
{
return(0);
}
/*
Terminate the parser plugin at server shutdown or plugin deinstallation.
SYNOPSIS
atom_ngram_parser_plugin_deinit()
Does nothing.
RETURN VALUE
0 success
1 failure (cannot happen)
*/
static int atom_ngram_parser_plugin_deinit(void *arg __attribute__((unused)))
{
return(0);
}
/*
Initialize the parser on the first use in the query
SYNOPSIS
atom_ngram_parser_init()
DESCRIPTION
Does nothing.
RETURN VALUE
0 success
1 failure (cannot happen)
*/
static int atom_ngram_parser_init(MYSQL_FTPARSER_PARAM *param
__attribute__((unused)))
{
return(0);
}
/*
Terminate the parser at the end of the query
SYNOPSIS
atom_ngram_parser_deinit()
DESCRIPTION
Does nothing.
RETURN VALUE
0 success
1 failure (cannot happen)
*/
static int atom_ngram_parser_deinit(MYSQL_FTPARSER_PARAM *param
__attribute__((unused)))
{
return(0);
}
//atom-ngram 回调声明
typedef int(*ATOM_NGRAM_CALLBACK)(
char* doc, int doc_len, char* word, int len,
MYSQL_FTPARSER_PARAM *param, MYSQL_FTPARSER_BOOLEAN_INFO *bool_info
);
//atom-ngram 解析器
static int atom_ngram_parser(
char* doc, int doc_len, int token_size,
ATOM_NGRAM_CALLBACK callback, MYSQL_FTPARSER_PARAM *param,
MYSQL_FTPARSER_BOOLEAN_INFO *bool_info)
{
//abcd
//n=1, a b c d
//n=2, ab bc cd
//n=3, abc bcd
//n=4, abcd
int ret = 0;
char* start = doc;
char* end = doc + doc_len;
char* next = start;
int nlen, slen,
n = 0;
while (next < end)
{
//计算字符长度,零或超出则跳出循环
nlen = my_mbcharlen_ptr(param->cs, next, end);
if(0 == nlen || next + nlen > end) {
break;
}
//下一个,并记录词数
if(0 == n) slen = nlen;
next += nlen;
n++;
//词大小到达设置值或到字符串尾
if (token_size == n || end == next)
{
//把词传递给回调函数
ret = callback(doc, doc_len,
start, (int)(next - start), param, bool_info);
//返回错误或到达结尾则跳出
if (0 != ret || end == next) {
break;
}
//初始化
start += slen;
next = start;
n = 0;
}
}
return(ret);
}
//atom-ngram 简单模式处理
static int atom_ngram_simple_callback(
char* doc, int doc_len, char* word, int len,
MYSQL_FTPARSER_PARAM *param, MYSQL_FTPARSER_BOOLEAN_INFO *bool_info)
{
bool_info->type = FT_TOKEN_WORD;
bool_info->position = (int)(word - doc);
return param->mysql_add_word(param, word, len, bool_info);
}
//字符串 \ 转义符处理
static char * atom_ngram_escape(
char* str, int slen, int *rlen,
MYSQL_FTPARSER_PARAM *param)
{
char * nstr = (char *)malloc(slen);
if (NULL != nstr)
{
char* start = str;
char* end = str + slen;
int nlen, ctype, escape = 0;
*rlen = 0;
while (start < end)
{
nlen = param->cs->cset->ctype(
param->cs, &ctype, (uchar *)start, (uchar *)end);
if (0 == nlen ||
start + nlen > end) {
break;
}
if (0 == escape)
{
if (0 != my_isascii(ctype))
{
if ('\\' == *start) {
start += nlen;
escape = 1;
continue;
}
}
}
else
{
escape = 0;
}
memcpy(nstr + *rlen, start, nlen);
(*rlen) += nlen;
start += nlen;
}
}
return(nstr);
}
//atom-ngram boolean 回调声明
typedef int(*ATOM_NGRAM_BOOLEAN_CALLBACK)(
char* doc, int doc_len, char* word, int len,
MYSQL_FTPARSER_PARAM *param, MYSQL_FTPARSER_BOOLEAN_INFO *bool_info
);
//布尔模式解析
static int atom_ngram_boolean_parser(
char* doc, int doc_len, ATOM_NGRAM_BOOLEAN_CALLBACK callback,
MYSQL_FTPARSER_PARAM *param, MYSQL_FTPARSER_BOOLEAN_INFO *bool_info)
{
int ret = 0;
char* start = doc;
char* end = doc + doc_len;
char* next = start;
char chr;
int nlen, wlen, ctype,
escape = 0;
//重置布尔状态
bool_info->yesno = bool_info->weight_adjust =
bool_info->wasign = bool_info->trunc = 0;
//开始解析语法
while (next < end)
{
//获取字符长度和类型
nlen = param->cs->cset->ctype(
param->cs, &ctype, (uchar *)next, (uchar *)end);
//长度为零或超出则跳出循环
if (0 == nlen || next + nlen > end) {
break;
}
//是否转义
if (0 == escape)
{
//ascii则继续解析
if (0 != my_isascii(ctype))
{
chr = *next;
if ('\\' == chr)
{
//检测到转义
//转义状态开启
escape = 1;
}
else if ('+' == chr || ' ' == chr || '-' == chr ||
'>' == chr || '<' == chr || '~' == chr || '*' == chr ||
'"' == chr || '(' == chr || ')' == chr)
{
//词截断处理
//非引号状态 或 出现引号字符在处理
if (('"' != chr && 0 == bool_info->quot) || '"' == chr)
{
//字符串长度非0
if (0 != (wlen = (int)(next - start)))
{
//通配符和引号的处理
if ('*' == chr) bool_info->trunc = 1;
if (0 != bool_info->quot) bool_info->yesno = +1;
//把词传递给回调函数
bool_info->type = FT_TOKEN_WORD;
ret = callback(doc, doc_len, start, wlen, param, bool_info);
if (0 != ret) break;
//重置布尔状态
bool_info->yesno = bool_info->weight_adjust =
bool_info->wasign = bool_info->trunc = 0;
}
}
//优先处理引号
if ('"' == chr || 0 != bool_info->quot)
{
//非引号则跳过
if ('"' == chr)
{
//设置引号模式
//开启子表达式和必须存在字符
bool_info->yesno = +1;
bool_info->position = 0;
bool_info->quot = (0 == bool_info->quot ? (char *)1 : 0);
bool_info->type = (0 != bool_info->quot ? FT_TOKEN_LEFT_PAREN : FT_TOKEN_RIGHT_PAREN);
ret = param->mysql_add_word(param, NULL, 0, bool_info);
if (0 != ret) break;
//重置布尔状态
bool_info->yesno = bool_info->weight_adjust =
bool_info->wasign = bool_info->trunc = 0;
//字符串开始位置
start = next + nlen;
}
}
else
{
//布尔模式语法解析
if ('+' == chr) bool_info->yesno = +1;
else if ('-' == chr) bool_info->yesno = -1;
else if ('>' == chr) bool_info->weight_adjust++;
else if ('<' == chr) bool_info->weight_adjust--;
else if ('~' == chr) bool_info->wasign = !bool_info->wasign;
else if ('(' == chr || ')' == chr)
{
//子表达式
if ('(' == chr)
{
//子表达式开始
bool_info->type = FT_TOKEN_LEFT_PAREN;
}
else if (')' == chr)
{
//子表达式结束
bool_info->type = FT_TOKEN_RIGHT_PAREN;
bool_info->yesno = bool_info->weight_adjust =
bool_info->wasign = bool_info->trunc = 0;
}
//设置子表达式
bool_info->position = 0;
ret = param->mysql_add_word(param, NULL, 0, bool_info);
if (0 != ret) break;
//重置布尔状态
bool_info->yesno = bool_info->weight_adjust =
bool_info->wasign = bool_info->trunc = 0;
}
else if (' ' == chr)
{
//重置布尔状态
bool_info->yesno = bool_info->weight_adjust =
bool_info->wasign = bool_info->trunc = 0;
}
//字符串开始位置
start = next + nlen;
}
}
}
}
else
{
//转义符
//重置转义状态
escape = 0;
}
//是否末尾
next += nlen;
if (end == next &&
0 != (wlen = (int)(next - start)))
{
//把词传递给回调函数
bool_info->type = FT_TOKEN_WORD;
ret = callback(doc, doc_len, start, wlen, param, bool_info);
if (0 != ret) break;
}
}
return(ret);
}
//布尔模式处理
static int atom_ngram_boolean_callback(
char* doc, int doc_len, char* word, int len,
MYSQL_FTPARSER_PARAM *param, MYSQL_FTPARSER_BOOLEAN_INFO *bool_info)
{
int ret = 0, rlen;
char *rword = atom_ngram_escape(word, len, &rlen, param);
if (NULL != rword)
{
//使用简单解析
ret = atom_ngram_parser(rword, rlen, atom_ngram_token_size,
atom_ngram_simple_callback, param, bool_info);
free(rword);
}
else
{
//分配内存失败
my_error(ER_OUTOFMEMORY, MYF(0), len);
ret = 1;
}
return(ret);
}
/*
Parse a document or a search query.
SYNOPSIS
atom_ngram_parser_parse()
param parsing context
DESCRIPTION
This is the main plugin function which is called to parse
a document or a search query. The call mode is set in
param->mode. This function simply splits the text into words
and passes every word to the MySQL full-text indexing engine.
RETURN VALUE
0 success
1 failure (cannot happen)
*/
static int atom_ngram_parser_parse(MYSQL_FTPARSER_PARAM *param)
{
int ret = 0;
MYSQL_FTPARSER_BOOLEAN_INFO bool_info =
{ FT_TOKEN_WORD, 0, 0, 0, 0, 0, ' ', 0 };
//简单模式和停词模式
//解析器忽略这两种模式的区别
if(MYSQL_FTPARSER_SIMPLE_MODE == param->mode ||
MYSQL_FTPARSER_WITH_STOPWORDS == param->mode)
{
//直接解析
ret = atom_ngram_parser(param->doc, param->length,
atom_ngram_token_size, atom_ngram_simple_callback, param, &bool_info);
}
//布尔模式
//支持默认语法外,增加 \ 转义符
else if (MYSQL_FTPARSER_FULL_BOOLEAN_INFO == param->mode)
{
//先解析布尔模式的语法
ret = atom_ngram_boolean_parser(param->doc, param->length,
atom_ngram_boolean_callback, param, &bool_info);
}
return(ret);
}
/*
Plugin type-specific descriptor
*/
static struct st_mysql_ftparser atom_ngram_parser_descriptor=
{
MYSQL_FTPARSER_INTERFACE_VERSION, /* interface version */
atom_ngram_parser_parse, /* parsing function */
atom_ngram_parser_init, /* parser init function */
atom_ngram_parser_deinit /* parser deinit function */
};
/*
Plugin status variables for SHOW STATUS
*/
static struct st_mysql_show_var atom_ngram_status[]=
{
{"atom_ngram_version", (char *)"v1.0", SHOW_CHAR, SHOW_SCOPE_GLOBAL},
{0,0,0, SHOW_SCOPE_GLOBAL}
};
/*
Plugin system variables.
*/
static MYSQL_SYSVAR_INT(token_size, atom_ngram_token_size,
PLUGIN_VAR_READONLY, "atom ngram full text plugin parser token size in characters",
NULL, NULL, 2, 1, 10, 0
);
static struct st_mysql_sys_var* atom_ngram_system_variables[]= {
MYSQL_SYSVAR(token_size),
NULL
};
/*
Plugin library descriptor
*/
mysql_declare_plugin(ftexample)
{
MYSQL_FTPARSER_PLUGIN, /* type */
&atom_ngram_parser_descriptor, /* descriptor */
"atom_ngram", /* name */
"[email protected]", /* author */
"Atom-Ngram Full-Text Parser", /* description */
PLUGIN_LICENSE_GPL,
atom_ngram_parser_plugin_init, /* init function (when loaded) */
atom_ngram_parser_plugin_deinit,/* deinit function (when unloaded) */
0x0001, /* version */
atom_ngram_status, /* status variables */
atom_ngram_system_variables, /* system variables */
NULL,
0,
}
mysql_declare_plugin_end;