Debian Codegraph @

lib/token_cursor.c

raw symbols SCIP
1 /*
2 Copyright (C) 2009-2018 Brazil
3 Copyright (C) 2018-2024 Sutou Kouhei <[email protected]>
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20 #include "grn_token.h"
21 #include "grn_token_cursor.h"
22
23 #include "grn_dat.h"
24 #include "grn_obj.h"
25 #include "grn_pat.h"
26 #include "grn_string.h"
27 #include "grn_table.h"
28
29 static void
30 grn_token_cursor_open_initialize_token_filters(grn_ctx *ctx,
31 grn_token_cursor *token_cursor)
32 {
33 grn_obj *token_filters = token_cursor->token_filter.objects;
34 grn_tokenizer_query *query = &(token_cursor->tokenizer.query);
35
36 token_cursor->token_filter.data = NULL;
37
38 size_t n_token_filters;
39 if (token_filters) {
40 n_token_filters = GRN_BULK_VSIZE(token_filters) / sizeof(grn_obj *);
41 } else {
42 n_token_filters = 0;
43 }
44
45 if (n_token_filters == 0) {
46 return;
47 }
48
49 token_cursor->token_filter.data =
50 GRN_CALLOC(sizeof(void *) * n_token_filters);
51 if (!token_cursor->token_filter.data) {
52 return;
53 }
54
55 size_t i;
56 for (i = 0; i < n_token_filters; i++) {
57 grn_obj *token_filter_object = GRN_PTR_VALUE_AT(token_filters, i);
58 grn_proc *token_filter = (grn_proc *)token_filter_object;
59
60 if (token_filter->callbacks.token_filter.init_query) {
61 grn_tokenizer_query_set_token_filter_index(ctx, query, (unsigned int)i);
62 token_cursor->token_filter.data[i] =
63 token_filter->callbacks.token_filter.init_query(ctx, query);
64 } else {
65 token_cursor->token_filter.data[i] =
66 token_filter->callbacks.token_filter.init(ctx,
67 token_cursor->table,
68 token_cursor->mode);
69 }
70 if (ctx->rc != GRN_SUCCESS) {
71 return;
72 }
73 }
74 }
75
76 grn_token_cursor *
77 grn_token_cursor_open(grn_ctx *ctx,
78 grn_obj *table,
79 const char *str,
80 size_t str_len,
81 grn_tokenize_mode mode,
82 uint32_t flags)
83 {
84 GRN_API_ENTER;
85
86 grn_token_cursor *token_cursor;
87 grn_encoding encoding;
88 grn_obj *tokenizer;
89 grn_obj *token_filters;
90 grn_table_flags table_flags;
91 if (grn_table_get_info(ctx,
92 table,
93 &table_flags,
94 &encoding,
95 &tokenizer,
96 NULL,
97 &token_filters)) {
98 ERR(GRN_INVALID_ARGUMENT,
99 "[token-cursor][open] failed to get table information");
100 GRN_API_RETURN(NULL);
101 }
102 if (!(token_cursor = GRN_CALLOC(sizeof(grn_token_cursor)))) {
103 GRN_API_RETURN(NULL);
104 }
105 token_cursor->initialized = false;
106 token_cursor->table = table;
107 token_cursor->mode = mode;
108 token_cursor->encoding = encoding;
109 token_cursor->flags = flags;
110 token_cursor->tokenizer.object = tokenizer;
111 grn_tokenizer_query_init(ctx, &(token_cursor->tokenizer.query));
112 grn_tokenizer_query_set_lexicon(ctx, &(token_cursor->tokenizer.query), table);
113 grn_tokenizer_query_set_flags(ctx, &(token_cursor->tokenizer.query), flags);
114 grn_tokenizer_query_set_mode(ctx, &(token_cursor->tokenizer.query), mode);
115 token_cursor->tokenizer.query_domain = GRN_ID_NIL;
116 GRN_OBJ_INIT(&(token_cursor->tokenizer.casted_query),
117 GRN_BULK,
118 0,
119 table->header.domain);
120 grn_token_init(ctx, &(token_cursor->tokenizer.current_token));
121 grn_token_init(ctx, &(token_cursor->tokenizer.next_token));
122 grn_token_init(ctx, &(token_cursor->tokenizer.original_token));
123 token_cursor->token_filter.objects = token_filters;
124 token_cursor->token_filter.data = NULL;
125 GRN_VOID_INIT(&(token_cursor->original_buffer));
126 token_cursor->orig = (const unsigned char *)str;
127 token_cursor->orig_blen = (uint32_t)str_len;
128 token_cursor->curr = NULL;
129 token_cursor->curr_size = 0;
130 token_cursor->pos = -1;
131 token_cursor->status = GRN_TOKEN_CURSOR_DOING;
132 token_cursor->pending.status = GRN_TOKEN_CURSOR_DOING;
133 token_cursor->pending.token = NULL;
134 GRN_API_RETURN(token_cursor);
135 }
136
137 grn_rc
138 grn_token_cursor_set_query_domain(grn_ctx *ctx,
139 grn_token_cursor *token_cursor,
140 grn_id domain)
141 {
142 GRN_API_ENTER;
143 token_cursor->tokenizer.query_domain = domain;
144 GRN_API_RETURN(ctx->rc);
145 }
146
147 static bool
148 grn_token_cursor_ensure_initialize(grn_ctx *ctx, grn_token_cursor *token_cursor)
149 {
150 if (token_cursor->initialized) {
151 goto exit;
152 }
153
154 token_cursor->initialized = true;
155
156 grn_tokenizer_query *query = &(token_cursor->tokenizer.query);
157 if (token_cursor->orig_blen == 0) {
158 grn_obj *source_column = grn_tokenizer_query_get_source_column(ctx, query);
159 grn_id source_id = grn_tokenizer_query_get_source_id(ctx, query);
160 if (source_column && source_id != GRN_ID_NIL) {
161 GRN_BULK_REWIND(&(token_cursor->original_buffer));
162 grn_obj_get_value(ctx,
163 source_column,
164 source_id,
165 &(token_cursor->original_buffer));
166 token_cursor->orig = GRN_BULK_HEAD(&(token_cursor->original_buffer));
167 token_cursor->orig_blen =
168 (uint32_t)GRN_BULK_VSIZE(&(token_cursor->original_buffer));
169 }
170 }
171
172 if (token_cursor->tokenizer.object) {
173 grn_proc *tokenizer_proc = (grn_proc *)(token_cursor->tokenizer.object);
174 if (tokenizer_proc->callbacks.tokenizer.init) {
175 grn_id query_domain = token_cursor->tokenizer.query_domain;
176 if (query_domain == GRN_ID_NIL) {
177 query_domain = GRN_DB_TEXT;
178 }
179 token_cursor->tokenizer.user_data = NULL;
180 grn_tokenizer_query_set_data(ctx,
181 query,
182 token_cursor->orig,
183 token_cursor->orig_blen,
184 query_domain);
185 if (ctx->rc != GRN_SUCCESS) {
186 goto exit;
187 }
188 token_cursor->tokenizer.user_data =
189 tokenizer_proc->callbacks.tokenizer.init(ctx, query);
190 } else if (tokenizer_proc->funcs[PROC_INIT]) {
191 grn_obj str_, flags_, mode_;
192 GRN_TEXT_INIT(&str_, GRN_OBJ_DO_SHALLOW_COPY);
193 GRN_TEXT_SET_REF(&str_, token_cursor->orig, token_cursor->orig_blen);
194 GRN_UINT32_INIT(&flags_, 0);
195 GRN_UINT32_SET(ctx, &flags_, token_cursor->flags);
196 GRN_UINT32_INIT(&mode_, 0);
197 GRN_UINT32_SET(ctx, &mode_, token_cursor->mode);
198 token_cursor->tokenizer.pctx.caller = NULL;
199 token_cursor->tokenizer.pctx.user_data.ptr = NULL;
200 token_cursor->tokenizer.pctx.proc = tokenizer_proc;
201 token_cursor->tokenizer.pctx.hooks = NULL;
202 token_cursor->tokenizer.pctx.currh = NULL;
203 token_cursor->tokenizer.pctx.phase = PROC_INIT;
204 grn_ctx_push(ctx, &mode_);
205 grn_ctx_push(ctx, &str_);
206 grn_ctx_push(ctx, &flags_);
207 tokenizer_proc->funcs[PROC_INIT](
208 ctx,
209 1,
210 &(token_cursor->table),
211 &(token_cursor->tokenizer.pctx.user_data));
212 grn_obj_close(ctx, &flags_);
213 grn_obj_close(ctx, &str_);
214 grn_obj_close(ctx, &mode_);
215 }
216 } else {
217 grn_id query_domain = token_cursor->tokenizer.query_domain;
218
219 grn_tokenizer_query_set_data(ctx,
220 query,
221 token_cursor->orig,
222 token_cursor->orig_blen,
223 query_domain == GRN_ID_NIL ? GRN_DB_TEXT
224 : query_domain);
225 if (ctx->rc != GRN_SUCCESS) {
226 goto exit;
227 }
228 grn_obj *casted_query = &(token_cursor->tokenizer.casted_query);
229 grn_obj *table = token_cursor->table;
230 grn_id table_id = casted_query->header.domain;
231 if (query_domain == GRN_ID_NIL ||
232 (grn_type_id_is_text_family(ctx, query_domain) &&
233 grn_type_id_is_text_family(ctx, casted_query->header.domain))) {
234 grn_obj *string;
235 const char *normalized;
236 string = grn_tokenizer_query_get_normalized_string(ctx, query);
237 grn_string_get_normalized(ctx,
238 string,
239 &normalized,
240 &(token_cursor->curr_size),
241 NULL);
242 token_cursor->curr = (const unsigned char *)normalized;
243 grn_token_set_data(ctx,
244 &(token_cursor->tokenizer.current_token),
245 token_cursor->curr,
246 (int)(token_cursor->curr_size));
247 } else {
248 if (query_domain == table_id || query_domain == table->header.domain) {
249 token_cursor->curr = token_cursor->orig;
250 token_cursor->curr_size = token_cursor->orig_blen;
251 grn_token_set_data(ctx,
252 &(token_cursor->tokenizer.current_token),
253 token_cursor->curr,
254 (int)(token_cursor->curr_size));
255 grn_token_set_domain(ctx,
256 &(token_cursor->tokenizer.current_token),
257 query_domain);
258 } else {
259 grn_obj query;
260 GRN_OBJ_INIT(&query, GRN_BULK, GRN_OBJ_DO_SHALLOW_COPY, query_domain);
261 GRN_TEXT_SET(ctx, &query, token_cursor->orig, token_cursor->orig_blen);
262 GRN_BULK_REWIND(casted_query);
263 grn_rc rc = grn_obj_cast(ctx, &query, casted_query, false);
264 GRN_OBJ_FIN(ctx, &query);
265 if (rc == GRN_SUCCESS) {
266 token_cursor->curr = GRN_BULK_HEAD(casted_query);
267 token_cursor->curr_size = GRN_BULK_VSIZE(casted_query);
268 grn_token_set_data(ctx,
269 &(token_cursor->tokenizer.current_token),
270 token_cursor->curr,
271 (int)(token_cursor->curr_size));
272 grn_token_set_domain(
273 ctx,
274 &(token_cursor->tokenizer.current_token),
275 token_cursor->tokenizer.casted_query.header.domain);
276 }
277 }
278 }
279 grn_token_set_status(ctx,
280 &(token_cursor->tokenizer.current_token),
281 GRN_TOKEN_LAST);
282 }
283
284 if (ctx->rc == GRN_SUCCESS) {
285 grn_token_cursor_open_initialize_token_filters(ctx, token_cursor);
286 }
287
288 exit:
289 return ctx->rc == GRN_SUCCESS;
290 }
291
292 grn_rc
293 grn_token_cursor_set_source_column(grn_ctx *ctx,
294 grn_token_cursor *token_cursor,
295 grn_obj *column)
296 {
297 GRN_API_ENTER;
298 grn_tokenizer_query *query = &(token_cursor->tokenizer.query);
299 grn_rc rc = grn_tokenizer_query_set_source_column(ctx, query, column);
300 GRN_API_RETURN(rc);
301 }
302
303 grn_rc
304 grn_token_cursor_set_source_id(grn_ctx *ctx,
305 grn_token_cursor *token_cursor,
306 grn_id id)
307 {
308 GRN_API_ENTER;
309 grn_tokenizer_query *query = &(token_cursor->tokenizer.query);
310 grn_rc rc = grn_tokenizer_query_set_source_id(ctx, query, id);
311 GRN_API_RETURN(rc);
312 }
313
314 grn_rc
315 grn_token_cursor_set_index_column(grn_ctx *ctx,
316 grn_token_cursor *token_cursor,
317 grn_obj *column)
318 {
319 GRN_API_ENTER;
320 grn_tokenizer_query *query = &(token_cursor->tokenizer.query);
321 grn_rc rc = grn_tokenizer_query_set_index_column(ctx, query, column);
322 GRN_API_RETURN(rc);
323 }
324
325 grn_rc
326 grn_token_cursor_set_query_options(grn_ctx *ctx,
327 grn_token_cursor *token_cursor,
328 grn_obj *query_options)
329 {
330 GRN_API_ENTER;
331 grn_tokenizer_query *query = &(token_cursor->tokenizer.query);
332 grn_rc rc = grn_tokenizer_query_set_options(ctx, query, query_options);
333 GRN_API_RETURN(rc);
334 }
335
336 static void
337 grn_token_cursor_next_apply_token_filters(grn_ctx *ctx,
338 grn_token_cursor *token_cursor)
339 {
340 grn_obj *token_filters = token_cursor->token_filter.objects;
341 grn_token *current_token = &(token_cursor->tokenizer.current_token);
342 grn_token *next_token = &(token_cursor->tokenizer.next_token);
343 grn_token *original_token = &(token_cursor->tokenizer.original_token);
344 grn_tokenizer_query *query = &(token_cursor->tokenizer.query);
345
346 size_t n_token_filters;
347 if (token_filters) {
348 n_token_filters = GRN_BULK_VSIZE(token_filters) / sizeof(grn_obj *);
349 } else {
350 n_token_filters = 0;
351 }
352
353 if (n_token_filters > 0) {
354 grn_token_copy(ctx, original_token, current_token);
355 grn_token_copy(ctx, next_token, current_token);
356 size_t i;
357 for (i = 0; i < n_token_filters; i++) {
358 grn_obj *token_filter_object = GRN_PTR_VALUE_AT(token_filters, i);
359 grn_proc *token_filter = (grn_proc *)token_filter_object;
360 void *data = token_cursor->token_filter.data[i];
361
362 grn_tokenizer_query_set_token_filter_index(ctx, query, (unsigned int)i);
363
364 #define SKIP_FLAGS (GRN_TOKEN_SKIP | GRN_TOKEN_SKIP_WITH_POSITION)
365 if (grn_token_get_status(ctx, current_token) & SKIP_FLAGS) {
366 break;
367 }
368 #undef SKIP_FLAGS
369
370 token_filter->callbacks.token_filter.filter(ctx,
371 current_token,
372 next_token,
373 data);
374 grn_token_copy(ctx, current_token, next_token);
375 }
376 }
377
378 {
379 size_t size;
380 token_cursor->curr = grn_token_get_data_raw(ctx, current_token, &size);
381 token_cursor->curr_size = (uint32_t)size;
382 }
383 }
384
385 static bool
386 grn_token_cursor_next_need_keep_original(grn_ctx *ctx,
387 grn_token_cursor *token_cursor)
388 {
389 grn_token *current_token = &(token_cursor->tokenizer.current_token);
390 grn_token *original_token = &(token_cursor->tokenizer.original_token);
391
392 if (!(grn_token_get_status(ctx, current_token) & GRN_TOKEN_KEEP_ORIGINAL)) {
393 return false;
394 }
395
396 grn_raw_string current_data;
397 current_data.value =
398 grn_token_get_data_raw(ctx, current_token, &(current_data.length));
399 grn_raw_string original_data;
400 original_data.value =
401 grn_token_get_data_raw(ctx, original_token, &(original_data.length));
402 return !GRN_RAW_STRING_EQUAL(current_data, original_data);
403 }
404
405 grn_id
406 grn_token_cursor_next(grn_ctx *ctx, grn_token_cursor *token_cursor)
407 {
408 GRN_API_ENTER;
409
410 if (!grn_token_cursor_ensure_initialize(ctx, token_cursor)) {
411 token_cursor->status = GRN_TOKEN_CURSOR_DONE;
412 GRN_API_RETURN(GRN_ID_NIL);
413 }
414
415 grn_id tid = GRN_ID_NIL;
416 grn_obj *table = token_cursor->table;
417 grn_obj *tokenizer = token_cursor->tokenizer.object;
418 grn_tokenizer_query *query = &(token_cursor->tokenizer.query);
419 grn_token *current_token = &(token_cursor->tokenizer.current_token);
420 void *user_data = token_cursor->tokenizer.user_data;
421 while (token_cursor->status != GRN_TOKEN_CURSOR_DONE) {
422 if (token_cursor->pending.token) {
423 token_cursor->status = token_cursor->pending.status;
424 grn_token_copy(ctx, current_token, token_cursor->pending.token);
425 grn_token_remove_status(ctx, current_token, GRN_TOKEN_KEEP_ORIGINAL);
426 size_t size;
427 token_cursor->curr = grn_token_get_data_raw(ctx, current_token, &size);
428 token_cursor->curr_size = (uint32_t)size;
429 token_cursor->pending.token = NULL;
430 token_cursor->pos--;
431 } else if (tokenizer) {
432 grn_proc *tokenizer_proc = (grn_proc *)tokenizer;
433 grn_token_reset(ctx, current_token);
434 if (tokenizer_proc->callbacks.tokenizer.next) {
435 tokenizer_proc->callbacks.tokenizer.next(ctx,
436 query,
437 current_token,
438 user_data);
439 } else if (tokenizer_proc->funcs[PROC_NEXT]) {
440 grn_obj *data, *status_obj;
441 tokenizer_proc->funcs[PROC_NEXT](
442 ctx,
443 1,
444 &table,
445 &token_cursor->tokenizer.pctx.user_data);
446 status_obj = grn_ctx_pop(ctx);
447 data = grn_ctx_pop(ctx);
448 grn_token_set_data(ctx,
449 current_token,
450 GRN_TEXT_VALUE(data),
451 (int)GRN_TEXT_LEN(data));
452 grn_token_set_status(ctx, current_token, GRN_UINT32_VALUE(status_obj));
453 }
454 grn_token_cursor_next_apply_token_filters(ctx, token_cursor);
455 } else {
456 grn_token_set_status(ctx, current_token, GRN_TOKEN_LAST);
457 grn_token_cursor_next_apply_token_filters(ctx, token_cursor);
458 }
459 {
460 grn_token_status status = grn_token_get_status(ctx, current_token);
461 token_cursor->status =
462 ((status & GRN_TOKEN_LAST) || (token_cursor->mode == GRN_TOKENIZE_GET &&
463 (status & GRN_TOKEN_REACH_END)))
464 ? GRN_TOKEN_CURSOR_DONE
465 : GRN_TOKEN_CURSOR_DOING;
466 #define SKIP_FLAGS (GRN_TOKEN_SKIP | GRN_TOKEN_SKIP_WITH_POSITION)
467 if (status & SKIP_FLAGS) {
468 if (status & GRN_TOKEN_SKIP) {
469 token_cursor->pos++;
470 }
471 if (token_cursor->status == GRN_TOKEN_CURSOR_DONE &&
472 tid == GRN_ID_NIL) {
473 token_cursor->status = GRN_TOKEN_CURSOR_DONE_SKIP;
474 break;
475 } else {
476 continue;
477 }
478 }
479 #undef SKIP_FLAGS
480 if (status & GRN_TOKEN_FORCE_PREFIX) {
481 grn_token_set_force_prefix_search(ctx, current_token, true);
482 }
483 if (token_cursor->curr_size == 0) {
484 continue;
485 }
486 if (token_cursor->curr_size > GRN_TABLE_MAX_KEY_SIZE) {
487 GRN_LOG(ctx,
488 GRN_WARN,
489 "[token_next] ignore too long token. "
490 "Token must be less than or equal to %d: <%d>(<%.*s>)",
491 GRN_TABLE_MAX_KEY_SIZE,
492 token_cursor->curr_size,
493 token_cursor->curr_size,
494 token_cursor->curr);
495 continue;
496 }
497 if (status & GRN_TOKEN_UNMATURED) {
498 if (status & GRN_TOKEN_OVERLAP) {
499 if (token_cursor->mode == GRN_TOKENIZE_GET) {
500 token_cursor->pos++;
501 continue;
502 }
503 } else {
504 if (status & GRN_TOKEN_REACH_END) {
505 grn_token_set_force_prefix_search(ctx, current_token, true);
506 }
507 }
508 }
509 }
510 if (grn_id_maybe_table(ctx, grn_token_get_domain(ctx, current_token)) &&
511 GRN_BULK_VSIZE(&(current_token->data)) == sizeof(grn_id)) {
512 tid = GRN_RECORD_VALUE(&(current_token->data));
513 } else if (token_cursor->mode == GRN_TOKENIZE_ADD) {
514 switch (table->header.type) {
515 case GRN_TABLE_PAT_KEY:
516 if (token_cursor->flags & GRN_TOKEN_CURSOR_PARALLEL) {
517 tid = grn_pat_get(ctx,
518 (grn_pat *)table,
519 token_cursor->curr,
520 token_cursor->curr_size,
521 NULL);
522 }
523 if (tid == GRN_ID_NIL) {
524 GRN_TABLE_LOCK_BEGIN(ctx, table)
525 {
526 tid = grn_pat_add(ctx,
527 (grn_pat *)table,
528 token_cursor->curr,
529 token_cursor->curr_size,
530 NULL,
531 NULL);
532 }
533 GRN_TABLE_LOCK_END(ctx);
534 }
535 break;
536 case GRN_TABLE_DAT_KEY:
537 if (token_cursor->flags & GRN_TOKEN_CURSOR_PARALLEL) {
538 tid = grn_dat_get(ctx,
539 (grn_dat *)table,
540 token_cursor->curr,
541 token_cursor->curr_size,
542 NULL);
543 }
544 if (tid == GRN_ID_NIL) {
545 GRN_TABLE_LOCK_BEGIN(ctx, table)
546 {
547 tid = grn_dat_add(ctx,
548 (grn_dat *)table,
549 token_cursor->curr,
550 token_cursor->curr_size,
551 NULL,
552 NULL);
553 }
554 GRN_TABLE_LOCK_END(ctx);
555 }
556 break;
557 case GRN_TABLE_HASH_KEY:
558 if (token_cursor->flags & GRN_TOKEN_CURSOR_PARALLEL) {
559 tid = grn_hash_get(ctx,
560 (grn_hash *)table,
561 token_cursor->curr,
562 token_cursor->curr_size,
563 NULL);
564 }
565 GRN_TABLE_LOCK_BEGIN(ctx, table)
566 {
567 tid = grn_hash_add(ctx,
568 (grn_hash *)table,
569 token_cursor->curr,
570 token_cursor->curr_size,
571 NULL,
572 NULL);
573 }
574 GRN_TABLE_LOCK_END(ctx);
575 break;
576 case GRN_TABLE_NO_KEY:
577 if (token_cursor->curr_size == sizeof(grn_id)) {
578 tid = *((grn_id *)token_cursor->curr);
579 } else {
580 tid = GRN_ID_NIL;
581 }
582 break;
583 }
584 } else if (token_cursor->mode != GRN_TOKENIZE_ONLY) {
585 switch (table->header.type) {
586 case GRN_TABLE_PAT_KEY:
587 tid = grn_pat_get(ctx,
588 (grn_pat *)table,
589 token_cursor->curr,
590 token_cursor->curr_size,
591 NULL);
592 break;
593 case GRN_TABLE_DAT_KEY:
594 tid = grn_dat_get(ctx,
595 (grn_dat *)table,
596 token_cursor->curr,
597 token_cursor->curr_size,
598 NULL);
599 break;
600 case GRN_TABLE_HASH_KEY:
601 tid = grn_hash_get(ctx,
602 (grn_hash *)table,
603 token_cursor->curr,
604 token_cursor->curr_size,
605 NULL);
606 break;
607 case GRN_TABLE_NO_KEY:
608 if (token_cursor->curr_size == sizeof(grn_id)) {
609 tid = *((grn_id *)token_cursor->curr);
610 } else {
611 tid = GRN_ID_NIL;
612 }
613 break;
614 }
615 }
616 if (token_cursor->mode != GRN_TOKENIZE_ONLY && tid == GRN_ID_NIL &&
617 token_cursor->status != GRN_TOKEN_CURSOR_DONE) {
618 token_cursor->status = GRN_TOKEN_CURSOR_NOT_FOUND;
619 }
620 if (grn_token_cursor_next_need_keep_original(ctx, token_cursor)) {
621 token_cursor->pending.status = token_cursor->status;
622 token_cursor->status = GRN_TOKEN_CURSOR_DOING;
623 token_cursor->pending.token = &(token_cursor->tokenizer.original_token);
624 }
625 token_cursor->pos++;
626 grn_token_set_position(ctx, current_token, (uint32_t)(token_cursor->pos));
627 break;
628 }
629 GRN_API_RETURN(tid);
630 }
631
632 grn_token_cursor_status
633 grn_token_cursor_get_status(grn_ctx *ctx, grn_token_cursor *token_cursor)
634 {
635 GRN_API_ENTER;
636 if (!token_cursor) {
637 ERR(GRN_INVALID_ARGUMENT,
638 "[token-cursor][status] token cursor must not NULL");
639 GRN_API_RETURN(GRN_TOKEN_CURSOR_DONE);
640 }
641 GRN_API_RETURN(token_cursor->status);
642 }
643
644 static void
645 grn_token_cursor_close_token_filters(grn_ctx *ctx,
646 grn_token_cursor *token_cursor)
647 {
648 grn_obj *token_filters = token_cursor->token_filter.objects;
649 grn_tokenizer_query *query = &(token_cursor->tokenizer.query);
650
651 if (!token_cursor->token_filter.data) {
652 return;
653 }
654
655 size_t n_token_filters;
656 if (token_filters) {
657 n_token_filters = GRN_BULK_VSIZE(token_filters) / sizeof(grn_obj *);
658 } else {
659 n_token_filters = 0;
660 }
661
662 if (n_token_filters == 0) {
663 return;
664 }
665
666 size_t i;
667 for (i = 0; i < n_token_filters; i++) {
668 grn_obj *token_filter_object = GRN_PTR_VALUE_AT(token_filters, i);
669 grn_proc *token_filter = (grn_proc *)token_filter_object;
670 void *data = token_cursor->token_filter.data[i];
671
672 grn_tokenizer_query_set_token_filter_index(ctx, query, (unsigned int)i);
673 token_filter->callbacks.token_filter.fin(ctx, data);
674 }
675 GRN_FREE(token_cursor->token_filter.data);
676 }
677
678 grn_rc
679 grn_token_cursor_close(grn_ctx *ctx, grn_token_cursor *token_cursor)
680 {
681 GRN_API_ENTER;
682 if (!token_cursor) {
683 GRN_API_RETURN(GRN_INVALID_ARGUMENT);
684 }
685
686 if (token_cursor->tokenizer.object && token_cursor->initialized) {
687 grn_proc *tokenizer_proc = (grn_proc *)(token_cursor->tokenizer.object);
688 if (tokenizer_proc->callbacks.tokenizer.fin) {
689 void *user_data = token_cursor->tokenizer.user_data;
690 if (user_data) {
691 tokenizer_proc->callbacks.tokenizer.fin(ctx, user_data);
692 }
693 } else if (tokenizer_proc->funcs[PROC_FIN]) {
694 tokenizer_proc->funcs[PROC_FIN](ctx,
695 1,
696 &token_cursor->table,
697 &token_cursor->tokenizer.pctx.user_data);
698 }
699 }
700 grn_token_fin(ctx, &(token_cursor->tokenizer.current_token));
701 grn_token_fin(ctx, &(token_cursor->tokenizer.next_token));
702 grn_token_fin(ctx, &(token_cursor->tokenizer.original_token));
703 GRN_OBJ_FIN(ctx, &(token_cursor->tokenizer.casted_query));
704 grn_tokenizer_query_fin(ctx, &(token_cursor->tokenizer.query));
705 grn_token_cursor_close_token_filters(ctx, token_cursor);
706 GRN_OBJ_FIN(ctx, &(token_cursor->original_buffer));
707 GRN_FREE(token_cursor);
708 GRN_API_RETURN(GRN_SUCCESS);
709 }
710
711 grn_token *
712 grn_token_cursor_get_token(grn_ctx *ctx, grn_token_cursor *token_cursor)
713 {
714 GRN_API_ENTER;
715 GRN_API_RETURN(&(token_cursor->tokenizer.current_token));
716 }