Debian Codegraph @

lib/index_column.c

raw symbols SCIP
1 /*
2 Copyright (C) 2009-2015 Brazil
3 Copyright (C) 2018-2026 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_index_column.h"
21
22 #include "grn_hash.h"
23 #include "grn_ii.h"
24 #include "grn_progress_logger.h"
25
26 #include <math.h>
27 #include <string.h>
28
29 static uint64_t grn_index_sparsity = 10;
30
31 void
32 grn_index_column_init_from_env(void)
33 {
34 {
35 char grn_index_sparsity_env[GRN_ENV_BUFFER_SIZE];
36 grn_getenv("GRN_INDEX_SPARSITY",
37 grn_index_sparsity_env,
38 GRN_ENV_BUFFER_SIZE);
39 if (grn_index_sparsity_env[0]) {
40 uint64_t sparsity;
41 errno = 0;
42 sparsity = strtoull(grn_index_sparsity_env, NULL, 0);
43 if (errno == 0) {
44 grn_index_sparsity = sparsity;
45 }
46 }
47 }
48 }
49
50 static inline void
51 grn_index_column_build_call_hook(grn_ctx *ctx,
52 grn_obj *obj,
53 grn_id id,
54 grn_obj *old_value,
55 grn_obj *value,
56 int flags)
57 {
58 grn_hook *hooks = DB_OBJ(obj)->hooks[GRN_HOOK_SET];
59
60 if (hooks) {
61 /* todo : grn_proc_ctx_open() */
62 grn_obj id_, flags_;
63 grn_proc_ctx pctx;
64 grn_proc_ctx_init(&pctx, hooks, 4, 4);
65 GRN_UINT32_INIT(&id_, 0);
66 GRN_UINT32_INIT(&flags_, 0);
67 GRN_UINT32_SET(ctx, &id_, id);
68 GRN_UINT32_SET(ctx, &flags_, flags);
69 while (hooks) {
70 grn_ctx_push(ctx, &id_);
71 grn_ctx_push(ctx, old_value);
72 grn_ctx_push(ctx, value);
73 grn_ctx_push(ctx, &flags_);
74 pctx.caller = NULL;
75 pctx.currh = hooks;
76 if (hooks->proc) {
77 hooks->proc->funcs[PROC_INIT](ctx, 1, &obj, &pctx.user_data);
78 } else {
79 grn_obj_default_set_value_hook(ctx, 1, &obj, &pctx.user_data);
80 }
81 if (ctx->rc) {
82 return;
83 }
84 hooks = hooks->next;
85 pctx.offset++;
86 }
87 }
88 }
89
90 static void
91 grn_index_column_build_column(grn_ctx *ctx,
92 grn_obj *index_column,
93 grn_obj *table,
94 grn_obj *column)
95 {
96 grn_obj old_value;
97 grn_obj value;
98 int cursor_flags = GRN_CURSOR_BY_ID;
99
100 GRN_VOID_INIT(&old_value);
101 grn_obj_reinit_for(ctx, &old_value, column);
102 GRN_VOID_INIT(&value);
103 grn_obj_reinit_for(ctx, &value, column);
104 if (GRN_OBJ_TABLEP(column)) {
105 GRN_TABLE_EACH_BEGIN_FLAGS(ctx, table, cursor, id, cursor_flags)
106 {
107 GRN_BULK_REWIND(&value);
108 grn_table_get_key2(ctx, column, id, &value);
109 grn_index_column_build_call_hook(ctx, column, id, &old_value, &value, 0);
110 }
111 GRN_TABLE_EACH_END(ctx, cursor);
112 } else {
113 grn_column_cache *cache;
114 cache = grn_column_cache_open(ctx, column);
115 if (cache) {
116 GRN_TABLE_EACH_BEGIN_FLAGS(ctx, table, cursor, id, cursor_flags)
117 {
118 void *raw_value;
119 size_t value_size;
120 GRN_BULK_REWIND(&value);
121 raw_value = grn_column_cache_ref(ctx, cache, id, &value_size);
122 grn_bulk_write(ctx, &value, raw_value, value_size);
123 grn_index_column_build_call_hook(ctx,
124 column,
125 id,
126 &old_value,
127 &value,
128 0);
129 }
130 GRN_TABLE_EACH_END(ctx, cursor);
131 grn_column_cache_close(ctx, cache);
132 } else {
133 GRN_TABLE_EACH_BEGIN_FLAGS(ctx, table, cursor, id, cursor_flags)
134 {
135 GRN_BULK_REWIND(&value);
136 grn_obj_get_value(ctx, column, id, &value);
137 grn_index_column_build_call_hook(ctx,
138 column,
139 id,
140 &old_value,
141 &value,
142 0);
143 }
144 GRN_TABLE_EACH_END(ctx, cursor);
145 }
146 }
147 GRN_OBJ_FIN(ctx, &old_value);
148 GRN_OBJ_FIN(ctx, &value);
149 }
150
151 grn_rc
152 grn_index_column_build(grn_ctx *ctx, grn_obj *index_column)
153 {
154 grn_obj *src, *target;
155 grn_id *s = DB_OBJ(index_column)->source;
156
157 if (!(DB_OBJ(index_column)->source_size) || !s) {
158 return ctx->rc;
159 }
160
161 src = grn_ctx_at(ctx, *s);
162 if (!src) {
163 ERR(GRN_INVALID_ARGUMENT, "invalid source");
164 return ctx->rc;
165 }
166
167 target = GRN_OBJ_TABLEP(src) ? src : grn_ctx_at(ctx, src->header.domain);
168 if (!target) {
169 ERR(GRN_INVALID_ARGUMENT, "invalid target");
170 return ctx->rc;
171 }
172
173 grn_table_flags lexicon_flags;
174 grn_ii *ii = (grn_ii *)index_column;
175 bool use_grn_ii_build;
176 grn_obj *tokenizer = NULL;
177 grn_table_get_info(ctx,
178 ii->lexicon,
179 &lexicon_flags,
180 NULL,
181 &tokenizer,
182 NULL,
183 NULL);
184 switch (lexicon_flags & GRN_OBJ_TABLE_TYPE_MASK) {
185 case GRN_OBJ_TABLE_HASH_KEY:
186 case GRN_OBJ_TABLE_PAT_KEY:
187 case GRN_OBJ_TABLE_DAT_KEY:
188 use_grn_ii_build = true;
189 break;
190 default:
191 use_grn_ii_build = false;
192 break;
193 }
194 const grn_column_flags flags = grn_ii_get_flags(ctx, ii);
195 if ((flags & GRN_OBJ_WITH_POSITION) &&
196 (!tokenizer && !GRN_TYPE_IS_TEXT_FAMILY(ii->lexicon->header.domain))) {
197 /* TODO: Support offline index construction for WITH_POSITION
198 * index against UInt32 vector column. */
199 use_grn_ii_build = false;
200 }
201
202 size_t n_columns = DB_OBJ(index_column)->source_size / sizeof(grn_id);
203 grn_obj **columns = GRN_MALLOC(n_columns * sizeof(grn_obj *));
204 if (!columns) {
205 return ctx->rc;
206 }
207
208 grn_obj **cp;
209 size_t i;
210 for (cp = columns, i = n_columns; i > 0; s++, cp++, i--) {
211 if (!(*cp = grn_ctx_at(ctx, *s))) {
212 ERR(GRN_INVALID_ARGUMENT, "source invalid, i=%" GRN_FMT_SIZE, i);
213 GRN_FREE(columns);
214 return ctx->rc;
215 }
216 }
217 grn_obj_set_visibility(ctx, index_column, false);
218 if (use_grn_ii_build) {
219 grn_ii_build(ctx, ii, NULL);
220 } else {
221 for (i = 0; i < n_columns; i++) {
222 grn_obj *column = columns[i];
223 grn_index_column_build_column(ctx, index_column, target, column);
224 }
225 }
226 grn_obj_set_visibility(ctx, index_column, true);
227 for (i = 0; i < n_columns; i++) {
228 grn_obj_unref(ctx, columns[i]);
229 }
230 GRN_FREE(columns);
231 grn_obj_touch(ctx, index_column, NULL);
232
233 if (target != src) {
234 grn_obj_unref(ctx, target);
235 }
236 grn_obj_unref(ctx, src);
237
238 return ctx->rc;
239 }
240
241 grn_rc
242 grn_index_column_rebuild(grn_ctx *ctx, grn_obj *index_column)
243 {
244 grn_ii *ii = (grn_ii *)index_column;
245
246 GRN_API_ENTER;
247
248 grn_ii_truncate(ctx, ii);
249 grn_index_column_build(ctx, index_column);
250
251 GRN_API_RETURN(ctx->rc);
252 }
253
254 static const grn_log_level grn_index_column_diff_progress_log_level_default =
255 GRN_LOG_DEBUG;
256
257 grn_rc
258 grn_index_column_diff_options_init(grn_ctx *ctx,
259 grn_index_column_diff_options *options)
260 {
261 options->progress_log_level =
262 grn_index_column_diff_progress_log_level_default;
263 return GRN_SUCCESS;
264 }
265
266 grn_rc
267 grn_index_column_diff_options_fin(grn_ctx *ctx,
268 grn_index_column_diff_options *options)
269 {
270 return GRN_SUCCESS;
271 }
272
273 grn_index_column_diff_options *
274 grn_index_column_diff_options_open(grn_ctx *ctx)
275 {
276 GRN_API_ENTER;
277 grn_index_column_diff_options *options =
278 GRN_MALLOC(sizeof(grn_index_column_diff_options));
279 if (!options) {
280 GRN_API_RETURN(NULL);
281 }
282 grn_rc rc = grn_index_column_diff_options_init(ctx, options);
283 if (rc != GRN_SUCCESS) {
284 GRN_FREE(options);
285 GRN_API_RETURN(NULL);
286 }
287 GRN_API_RETURN(options);
288 }
289
290 grn_rc
291 grn_index_column_diff_options_close(grn_ctx *ctx,
292 grn_index_column_diff_options *options)
293 {
294 GRN_API_ENTER;
295 grn_rc rc = GRN_SUCCESS;
296 if (options) {
297 rc = grn_index_column_diff_options_fin(ctx, options);
298 GRN_FREE(options);
299 }
300 GRN_API_RETURN(rc);
301 }
302
303 grn_log_level
304 grn_index_column_diff_options_get_progress_log_level(
305 grn_ctx *ctx, grn_index_column_diff_options *options)
306 {
307 if (options) {
308 return options->progress_log_level;
309 } else {
310 return grn_index_column_diff_progress_log_level_default;
311 }
312 }
313
314 grn_rc
315 grn_index_column_diff_options_set_progress_log_level(
316 grn_ctx *ctx, grn_index_column_diff_options *options, grn_log_level level)
317 {
318 options->progress_log_level = level;
319 return GRN_SUCCESS;
320 }
321
322 typedef enum {
323 GRN_INDEX_COLUMN_DIFF_TYPE_REMAINS,
324 GRN_INDEX_COLUMN_DIFF_TYPE_MISSINGS,
325 GRN_INDEX_COLUMN_DIFF_TYPE_LOOK_AHEAD,
326 } grn_index_column_diff_type;
327 /* Except LOOK_AHEAD */
328 #define N_GRN_INDEX_COLUMN_DIFF_PERSISTENT_TYPES 2
329
330 static const char *
331 grn_index_column_diff_type_to_string(grn_index_column_diff_type type)
332 {
333 switch (type) {
334 case GRN_INDEX_COLUMN_DIFF_TYPE_REMAINS:
335 return "remains";
336 case GRN_INDEX_COLUMN_DIFF_TYPE_MISSINGS:
337 return "missings";
338 case GRN_INDEX_COLUMN_DIFF_TYPE_LOOK_AHEAD:
339 return "look-ahead";
340 default:
341 return "unknown";
342 }
343 }
344
345 static const char *remains_column_name = "remains";
346 static const char *missings_column_name = "missings";
347
348 typedef struct {
349 grn_id token_id;
350 grn_obj remains;
351 grn_obj missings;
352 grn_obj look_ahead;
353 size_t look_ahead_offset;
354 grn_posting last_posting;
355 grn_ii_cursor *cursor;
356 bool need_cursor_next;
357 size_t n_postings[N_GRN_INDEX_COLUMN_DIFF_PERSISTENT_TYPES];
358 size_t thresholds[N_GRN_INDEX_COLUMN_DIFF_PERSISTENT_TYPES];
359 } grn_index_column_diff_posting_list;
360
361 typedef struct {
362 grn_index_column_diff_options *options;
363 grn_obj *lexicon;
364 grn_ii *ii;
365 struct {
366 char name[GRN_TABLE_MAX_KEY_SIZE];
367 int name_size;
368 bool with_section;
369 bool with_position;
370 uint32_t n_elements;
371 } index;
372 bool have_tokenizer;
373 size_t n_posting_elements;
374 grn_obj *source_table;
375 grn_obj source_columns;
376 grn_obj *diff;
377 grn_hash *posting_lists;
378 grn_obj *remains;
379 grn_obj *missings;
380 struct {
381 grn_id token_id;
382 grn_posting posting;
383 grn_hash *token_counters;
384 } current;
385 struct {
386 grn_obj value;
387 grn_obj postings;
388 } buffers;
389 struct {
390 grn_obj tag;
391 grn_progress_logger logger;
392 } progress;
393 } grn_index_column_diff_data;
394
395 static void
396 grn_index_column_diff_data_init(grn_ctx *ctx,
397 grn_index_column_diff_data *data,
398 grn_index_column_diff_options *options)
399 {
400 data->options = options;
401 GRN_PTR_INIT(&(data->source_columns), GRN_OBJ_VECTOR, GRN_ID_NIL);
402 GRN_VOID_INIT(&(data->buffers.value));
403 GRN_UINT32_INIT(&(data->buffers.postings), GRN_OBJ_VECTOR);
404 data->current.token_counters = grn_hash_create(ctx,
405 NULL,
406 sizeof(grn_id),
407 sizeof(uint32_t),
408 GRN_OBJ_TABLE_HASH_KEY);
409 if (!data->current.token_counters) {
410 ERR(GRN_NO_MEMORY_AVAILABLE,
411 "[index-column][diff] failed to create token counters");
412
413 GRN_OBJ_FIN(ctx, &(data->source_columns));
414 GRN_OBJ_FIN(ctx, &(data->buffers.value));
415 GRN_OBJ_FIN(ctx, &(data->buffers.postings));
416 }
417 }
418
419 static void
420 grn_index_column_diff_data_fin(grn_ctx *ctx, grn_index_column_diff_data *data)
421 {
422 {
423 size_t n_columns = GRN_PTR_VECTOR_SIZE(&(data->source_columns));
424 for (size_t i = 0; i < n_columns; i++) {
425 grn_obj *column = GRN_PTR_VALUE_AT(&(data->source_columns), i);
426 if (grn_obj_is_accessor(ctx, column)) {
427 grn_obj_close(ctx, column);
428 }
429 }
430 GRN_OBJ_FIN(ctx, &(data->source_columns));
431 }
432
433 GRN_OBJ_FIN(ctx, &(data->buffers.value));
434 GRN_OBJ_FIN(ctx, &(data->buffers.postings));
435 grn_hash_close(ctx, data->current.token_counters);
436 GRN_OBJ_FIN(ctx, &(data->progress.tag));
437 }
438
439 static void
440 grn_index_column_diff_init_progress(grn_ctx *ctx,
441 grn_index_column_diff_data *data)
442 {
443 GRN_TEXT_INIT(&(data->progress.tag), 0);
444 grn_text_printf(ctx,
445 &(data->progress.tag),
446 "[index-column][diff][progress][%.*s]",
447 data->index.name_size,
448 data->index.name);
449 GRN_TEXT_PUTC(ctx, &(data->progress.tag), '\0');
450 grn_progress_logger_init(ctx,
451 &(data->progress.logger),
452 GRN_TEXT_VALUE(&(data->progress.tag)),
453 "records",
454 grn_table_size(ctx, data->source_table));
455 data->progress.logger.log_level =
456 grn_index_column_diff_options_get_progress_log_level(ctx, data->options);
457 }
458
459 static void
460 grn_index_column_diff_progress(grn_ctx *ctx, grn_index_column_diff_data *data)
461 {
462 data->progress.logger.n_processed_targets++;
463 grn_progress_logger_log(ctx, &(data->progress.logger));
464 }
465
466 typedef enum {
467 GRN_INDEX_COLUMN_DIFF_COMPARED_EQUAL,
468 GRN_INDEX_COLUMN_DIFF_COMPARED_LESS,
469 GRN_INDEX_COLUMN_DIFF_COMPARED_GREATER,
470 } grn_index_column_diff_compared;
471
472 static char
473 grn_index_column_diff_compared_to_char(grn_index_column_diff_compared compared)
474 {
475 switch (compared) {
476 case GRN_INDEX_COLUMN_DIFF_COMPARED_EQUAL:
477 return '=';
478 case GRN_INDEX_COLUMN_DIFF_COMPARED_GREATER:
479 return '>';
480 case GRN_INDEX_COLUMN_DIFF_COMPARED_LESS:
481 return '<';
482 default:
483 return '?';
484 }
485 }
486
487 static grn_index_column_diff_compared
488 grn_index_column_diff_compare_posting(grn_ctx *ctx,
489 grn_index_column_diff_data *data,
490 const grn_posting *posting)
491 {
492 const bool with_section = data->index.with_section;
493 const bool with_position = data->index.with_position;
494 const grn_posting *current_posting = &(data->current.posting);
495
496 if (posting->rid < current_posting->rid) {
497 return GRN_INDEX_COLUMN_DIFF_COMPARED_LESS;
498 } else if (posting->rid > current_posting->rid) {
499 return GRN_INDEX_COLUMN_DIFF_COMPARED_GREATER;
500 }
501
502 if (with_section) {
503 if (posting->sid < current_posting->sid) {
504 return GRN_INDEX_COLUMN_DIFF_COMPARED_LESS;
505 } else if (posting->sid > current_posting->sid) {
506 return GRN_INDEX_COLUMN_DIFF_COMPARED_GREATER;
507 }
508 }
509
510 if (with_position) {
511 if (posting->pos < current_posting->pos) {
512 return GRN_INDEX_COLUMN_DIFF_COMPARED_LESS;
513 } else if (posting->pos > current_posting->pos) {
514 return GRN_INDEX_COLUMN_DIFF_COMPARED_GREATER;
515 }
516 }
517
518 return GRN_INDEX_COLUMN_DIFF_COMPARED_EQUAL;
519 }
520
521 static int
522 grn_index_column_diff_token_id_width(grn_ctx *ctx,
523 grn_index_column_diff_data *data)
524 {
525 unsigned int n_tokens = grn_table_size(ctx, data->lexicon);
526 if (n_tokens == 0) {
527 return 0;
528 } else {
529 double width = floor(log10(n_tokens)) + 1;
530 return (int)width;
531 }
532 }
533
534 static void
535 grn_index_column_diff_posting_list_init(
536 grn_ctx *ctx,
537 grn_index_column_diff_data *data,
538 grn_index_column_diff_posting_list *posting_list)
539 {
540 posting_list->token_id = data->current.token_id;
541 GRN_UINT32_INIT(&(posting_list->remains), GRN_OBJ_VECTOR);
542 GRN_UINT32_INIT(&(posting_list->missings), GRN_OBJ_VECTOR);
543 GRN_UINT32_INIT(&(posting_list->look_ahead), GRN_OBJ_VECTOR);
544 posting_list->look_ahead_offset = 0;
545 posting_list->last_posting.tf = 0;
546
547 const int ii_cursor_flags = 0;
548 posting_list->cursor = grn_ii_cursor_open(ctx,
549 data->ii,
550 posting_list->token_id,
551 GRN_ID_NIL,
552 GRN_ID_MAX,
553 (int)(data->index.n_elements),
554 ii_cursor_flags);
555 posting_list->need_cursor_next = true;
556 for (size_t i = 0; i < N_GRN_INDEX_COLUMN_DIFF_PERSISTENT_TYPES; i++) {
557 posting_list->n_postings[i] = 0;
558 posting_list->thresholds[i] = 4096;
559 }
560 }
561
562 static void
563 grn_index_column_diff_posting_list_flush(
564 grn_ctx *ctx,
565 grn_index_column_diff_data *data,
566 grn_index_column_diff_posting_list *posting_list)
567 {
568 grn_obj *remains = &(posting_list->remains);
569 grn_obj *missings = &(posting_list->missings);
570 {
571 grn_obj *look_ahead = &(posting_list->look_ahead);
572 const size_t offset = posting_list->look_ahead_offset * sizeof(uint32_t);
573 grn_bulk_write(ctx,
574 remains,
575 GRN_BULK_HEAD(look_ahead) + offset,
576 GRN_BULK_VSIZE(look_ahead) - offset);
577 }
578
579 grn_ii_cursor *cursor = posting_list->cursor;
580 if (cursor) {
581 const bool with_section = data->index.with_section;
582 const bool with_position = data->index.with_position;
583 if (with_position) {
584 while (!posting_list->need_cursor_next ||
585 grn_ii_cursor_next(ctx, cursor)) {
586 if (ctx->rc != GRN_SUCCESS) {
587 return;
588 }
589 grn_posting *posting;
590 while ((posting = grn_ii_cursor_next_pos(ctx, cursor))) {
591 if (ctx->rc != GRN_SUCCESS) {
592 return;
593 }
594 posting_list->need_cursor_next = false;
595 GRN_UINT32_PUT(ctx, remains, posting->rid);
596 if (with_section) {
597 GRN_UINT32_PUT(ctx, remains, posting->sid);
598 }
599 GRN_UINT32_PUT(ctx, remains, posting->pos);
600 }
601 posting_list->need_cursor_next = true;
602 }
603 } else {
604 grn_posting *posting;
605 while ((posting = grn_ii_cursor_next(ctx, cursor))) {
606 if (ctx->rc != GRN_SUCCESS) {
607 return;
608 }
609 GRN_UINT32_PUT(ctx, remains, posting->rid);
610 if (with_section) {
611 GRN_UINT32_PUT(ctx, remains, posting->sid);
612 }
613 }
614 }
615 }
616
617 if (GRN_BULK_VSIZE(remains) > 0 || GRN_BULK_VSIZE(missings) > 0) {
618 const grn_id diff_id = grn_table_add(ctx,
619 data->diff,
620 &(posting_list->token_id),
621 sizeof(grn_id),
622 NULL);
623 if (diff_id != GRN_ID_NIL) {
624 if (GRN_BULK_VSIZE(remains) > 0) {
625 grn_obj_set_value(ctx, data->remains, diff_id, remains, GRN_OBJ_APPEND);
626 }
627 if (GRN_BULK_VSIZE(missings) > 0) {
628 grn_obj_set_value(ctx,
629 data->missings,
630 diff_id,
631 missings,
632 GRN_OBJ_APPEND);
633 }
634 }
635 }
636 }
637
638 static void
639 grn_index_column_diff_posting_list_fin(
640 grn_ctx *ctx,
641 grn_index_column_diff_data *data,
642 grn_index_column_diff_posting_list *posting_list)
643 {
644 if (ctx->rc == GRN_SUCCESS) {
645 grn_index_column_diff_posting_list_flush(ctx, data, posting_list);
646 }
647 GRN_OBJ_FIN(ctx, &(posting_list->remains));
648 GRN_OBJ_FIN(ctx, &(posting_list->missings));
649 GRN_OBJ_FIN(ctx, &(posting_list->look_ahead));
650 if (posting_list->cursor) {
651 grn_ii_cursor_close(ctx, posting_list->cursor);
652 posting_list->cursor = NULL;
653 }
654 }
655
656 static void
657 grn_index_column_diff_posting_list_put(
658 grn_ctx *ctx,
659 grn_index_column_diff_data *data,
660 grn_index_column_diff_posting_list *posting_list,
661 grn_index_column_diff_type type,
662 const grn_posting *posting)
663 {
664 grn_obj *column;
665 grn_obj *postings;
666 switch (type) {
667 case GRN_INDEX_COLUMN_DIFF_TYPE_REMAINS:
668 column = data->remains;
669 postings = &(posting_list->remains);
670 break;
671 case GRN_INDEX_COLUMN_DIFF_TYPE_MISSINGS:
672 column = data->missings;
673 postings = &(posting_list->missings);
674 break;
675 default:
676 column = NULL;
677 postings = &(posting_list->look_ahead);
678 break;
679 }
680
681 GRN_UINT32_PUT(ctx, postings, posting->rid);
682 if (data->index.with_section) {
683 GRN_UINT32_PUT(ctx, postings, posting->sid);
684 }
685 if (data->index.with_position) {
686 GRN_UINT32_PUT(ctx, postings, posting->pos);
687 }
688
689 if (!column) {
690 return;
691 }
692
693 posting_list->n_postings[type]++;
694 const size_t current_n_postings =
695 GRN_UINT32_VECTOR_SIZE(postings) / data->n_posting_elements;
696 if (current_n_postings < posting_list->thresholds[type]) {
697 return;
698 }
699
700 const grn_id diff_id = grn_table_add(ctx,
701 data->diff,
702 &(posting_list->token_id),
703 sizeof(grn_id),
704 NULL);
705 if (diff_id == GRN_ID_NIL) {
706 return;
707 }
708 grn_obj_set_value(ctx, column, diff_id, postings, GRN_OBJ_APPEND);
709 if (ctx->rc == GRN_SUCCESS) {
710 GRN_BULK_REWIND(postings);
711 }
712 posting_list->thresholds[type] *= 2;
713 GRN_LOG(ctx,
714 GRN_LOG_DUMP,
715 "[index-column][diff][append][%.*s][%u][%*s] "
716 "%" GRN_FMT_SIZE "(+%" GRN_FMT_SIZE ")",
717 data->index.name_size,
718 data->index.name,
719 grn_index_column_diff_token_id_width(ctx, data),
720 data->current.token_id,
721 grn_index_column_diff_type_to_string(type),
722 posting_list->n_postings[type],
723 current_n_postings);
724 }
725
726 static void
727 grn_index_column_diff_posting_list_consume_look_ahead(
728 grn_ctx *ctx,
729 grn_index_column_diff_data *data,
730 grn_index_column_diff_posting_list *posting_list)
731 {
732 grn_obj *look_ahead = &(posting_list->look_ahead);
733 posting_list->look_ahead_offset++;
734 if (posting_list->look_ahead_offset ==
735 (GRN_UINT32_VECTOR_SIZE(look_ahead) / data->n_posting_elements)) {
736 GRN_BULK_REWIND(look_ahead);
737 posting_list->look_ahead_offset = 0;
738 }
739 }
740
741 static void
742 grn_index_column_diff_process_token_id(grn_ctx *ctx,
743 grn_index_column_diff_data *data)
744 {
745 {
746 void *value = NULL;
747 int added = 0;
748 const grn_id token_counter_id = grn_hash_add(ctx,
749 data->current.token_counters,
750 &(data->current.token_id),
751 sizeof(grn_id),
752 &value,
753 &added);
754 if (token_counter_id == GRN_ID_NIL) {
755 grn_rc rc = ctx->rc;
756 if (rc == GRN_SUCCESS) {
757 rc = GRN_UNKNOWN_ERROR;
758 }
759 char message[GRN_CTX_MSGSIZE];
760 grn_strcpy(message, GRN_CTX_MSGSIZE, ctx->errbuf);
761 ERR(rc,
762 "[index-column][diff] failed to add a token counter: "
763 "<%.*s>: %u: %s",
764 data->index.name_size,
765 data->index.name,
766 data->current.token_id,
767 message);
768 return;
769 }
770
771 uint32_t *token_counter = value;
772 if (added) {
773 *token_counter = 0;
774 } else {
775 (*token_counter)++;
776 }
777
778 if (*token_counter >= GRN_II_MAX_TF) {
779 return;
780 }
781 }
782
783 void *value = NULL;
784 int added = 0;
785 const grn_id posting_list_id = grn_hash_add(ctx,
786 data->posting_lists,
787 &(data->current.token_id),
788 sizeof(grn_id),
789 &value,
790 &added);
791 if (posting_list_id == GRN_ID_NIL) {
792 grn_rc rc = ctx->rc;
793 if (rc == GRN_SUCCESS) {
794 rc = GRN_UNKNOWN_ERROR;
795 }
796 char message[GRN_CTX_MSGSIZE];
797 grn_strcpy(message, GRN_CTX_MSGSIZE, ctx->errbuf);
798 ERR(rc,
799 "[index-column][diff] failed to add a record to keep a posting list: "
800 "%u: %s",
801 data->current.token_id,
802 message);
803 return;
804 }
805
806 grn_index_column_diff_posting_list *posting_list = value;
807 if (added) {
808 grn_index_column_diff_posting_list_init(ctx, data, posting_list);
809 }
810
811 const bool with_section = data->index.with_section;
812 const bool with_position = data->index.with_position;
813
814 if (posting_list->last_posting.tf > 0) {
815 const grn_index_column_diff_compared compared =
816 grn_index_column_diff_compare_posting(ctx,
817 data,
818 &(posting_list->last_posting));
819 GRN_LOG(ctx,
820 GRN_LOG_DUMP,
821 "[index-column][diff][process][%.*s][%*u] "
822 " last[%u:%u:%u:%u] %c current[%u:%u:%u]",
823 data->index.name_size,
824 data->index.name,
825 grn_index_column_diff_token_id_width(ctx, data),
826 data->current.token_id,
827 posting_list->last_posting.rid,
828 posting_list->last_posting.sid,
829 posting_list->last_posting.pos,
830 posting_list->last_posting.tf,
831 grn_index_column_diff_compared_to_char(compared),
832 data->current.posting.rid,
833 data->current.posting.sid,
834 data->current.posting.pos);
835 if (compared == GRN_INDEX_COLUMN_DIFF_COMPARED_EQUAL) {
836 posting_list->last_posting.tf--;
837 return;
838 } else {
839 posting_list->last_posting.tf = 0;
840 }
841 }
842
843 grn_obj *look_ahead = &(posting_list->look_ahead);
844 const size_t look_ahead_offset = posting_list->look_ahead_offset;
845
846 const size_t n_posting_elements = data->n_posting_elements;
847 const size_t n_postings =
848 GRN_UINT32_VECTOR_SIZE(look_ahead) / n_posting_elements;
849 const uint32_t *raw_postings = (const uint32_t *)GRN_BULK_HEAD(look_ahead);
850 for (size_t i = look_ahead_offset; i < n_postings; i++) {
851 const size_t offset = i * data->n_posting_elements;
852 const uint32_t *raw_posting = raw_postings + offset;
853 grn_posting posting = {0};
854 size_t i = 0;
855 posting.rid = raw_posting[i++];
856 if (with_section) {
857 posting.sid = raw_posting[i++];
858 }
859 if (with_position) {
860 posting.pos = raw_posting[i++];
861 }
862 const grn_index_column_diff_compared compared =
863 grn_index_column_diff_compare_posting(ctx, data, &posting);
864 GRN_LOG(ctx,
865 GRN_LOG_DUMP,
866 "[index-column][diff][process][%.*s][%*u] "
867 "look-ahead[%u:%u:%u] %c current[%u:%u:%u]",
868 data->index.name_size,
869 data->index.name,
870 grn_index_column_diff_token_id_width(ctx, data),
871 data->current.token_id,
872 posting.rid,
873 posting.sid,
874 posting.pos,
875 grn_index_column_diff_compared_to_char(compared),
876 data->current.posting.rid,
877 data->current.posting.sid,
878 data->current.posting.pos);
879 switch (compared) {
880 case GRN_INDEX_COLUMN_DIFF_COMPARED_EQUAL:
881 grn_index_column_diff_posting_list_consume_look_ahead(ctx,
882 data,
883 posting_list);
884 return;
885 case GRN_INDEX_COLUMN_DIFF_COMPARED_GREATER:
886 grn_index_column_diff_posting_list_put(
887 ctx,
888 data,
889 posting_list,
890 GRN_INDEX_COLUMN_DIFF_TYPE_MISSINGS,
891 &(data->current.posting));
892 return;
893 default:
894 grn_index_column_diff_posting_list_put(ctx,
895 data,
896 posting_list,
897 GRN_INDEX_COLUMN_DIFF_TYPE_REMAINS,
898 &posting);
899 grn_index_column_diff_posting_list_consume_look_ahead(ctx,
900 data,
901 posting_list);
902 break;
903 }
904 }
905
906 grn_ii_cursor *cursor = posting_list->cursor;
907 if (cursor) {
908 if (with_position) {
909 while (!posting_list->need_cursor_next ||
910 grn_ii_cursor_next(ctx, cursor)) {
911 if (ctx->rc != GRN_SUCCESS) {
912 return;
913 }
914 grn_posting *posting;
915 while ((posting = grn_ii_cursor_next_pos(ctx, cursor))) {
916 if (ctx->rc != GRN_SUCCESS) {
917 return;
918 }
919 posting_list->need_cursor_next = false;
920 const grn_index_column_diff_compared compared =
921 grn_index_column_diff_compare_posting(ctx, data, posting);
922 GRN_LOG(ctx,
923 GRN_LOG_DUMP,
924 "[index-column][diff][process][%.*s][%*u] "
925 " read[%u:%u:%u] %c current[%u:%u:%u]",
926 data->index.name_size,
927 data->index.name,
928 grn_index_column_diff_token_id_width(ctx, data),
929 data->current.token_id,
930 posting->rid,
931 posting->sid,
932 posting->pos,
933 grn_index_column_diff_compared_to_char(compared),
934 data->current.posting.rid,
935 data->current.posting.sid,
936 data->current.posting.pos);
937 switch (compared) {
938 case GRN_INDEX_COLUMN_DIFF_COMPARED_EQUAL:
939 return;
940 case GRN_INDEX_COLUMN_DIFF_COMPARED_GREATER:
941 grn_index_column_diff_posting_list_put(
942 ctx,
943 data,
944 posting_list,
945 GRN_INDEX_COLUMN_DIFF_TYPE_LOOK_AHEAD,
946 posting);
947 grn_index_column_diff_posting_list_put(
948 ctx,
949 data,
950 posting_list,
951 GRN_INDEX_COLUMN_DIFF_TYPE_MISSINGS,
952 &(data->current.posting));
953 return;
954 default:
955 grn_index_column_diff_posting_list_put(
956 ctx,
957 data,
958 posting_list,
959 GRN_INDEX_COLUMN_DIFF_TYPE_REMAINS,
960 posting);
961 break;
962 }
963 }
964 posting_list->need_cursor_next = true;
965 }
966 } else {
967 grn_posting *posting;
968 while ((posting = grn_ii_cursor_next(ctx, cursor))) {
969 if (ctx->rc != GRN_SUCCESS) {
970 return;
971 }
972 const grn_index_column_diff_compared compared =
973 grn_index_column_diff_compare_posting(ctx, data, posting);
974 GRN_LOG(ctx,
975 GRN_LOG_DUMP,
976 "[index-column][diff][process][%.*s][%*u] "
977 " read[%u:%u:%u:%u] %c current[%u:%u:%u]",
978 data->index.name_size,
979 data->index.name,
980 grn_index_column_diff_token_id_width(ctx, data),
981 data->current.token_id,
982 posting->rid,
983 posting->sid,
984 posting->pos,
985 posting->tf,
986 grn_index_column_diff_compared_to_char(compared),
987 data->current.posting.rid,
988 data->current.posting.sid,
989 data->current.posting.pos);
990 switch (compared) {
991 case GRN_INDEX_COLUMN_DIFF_COMPARED_EQUAL:
992 if (!with_position && posting->tf > 0) {
993 posting_list->last_posting = *posting;
994 posting_list->last_posting.tf--;
995 }
996 return;
997 case GRN_INDEX_COLUMN_DIFF_COMPARED_GREATER:
998 grn_index_column_diff_posting_list_put(
999 ctx,
1000 data,
1001 posting_list,
1002 GRN_INDEX_COLUMN_DIFF_TYPE_LOOK_AHEAD,
1003 posting);
1004 grn_index_column_diff_posting_list_put(
1005 ctx,
1006 data,
1007 posting_list,
1008 GRN_INDEX_COLUMN_DIFF_TYPE_MISSINGS,
1009 &(data->current.posting));
1010 return;
1011 default:
1012 grn_index_column_diff_posting_list_put(
1013 ctx,
1014 data,
1015 posting_list,
1016 GRN_INDEX_COLUMN_DIFF_TYPE_REMAINS,
1017 posting);
1018 break;
1019 }
1020 }
1021 }
1022 }
1023
1024 GRN_LOG(ctx,
1025 GRN_LOG_DUMP,
1026 "[index-column][diff][process][%.*s][%*u] "
1027 " no posting list: current[%u:%u:%u]",
1028 data->index.name_size,
1029 data->index.name,
1030 grn_index_column_diff_token_id_width(ctx, data),
1031 data->current.token_id,
1032 data->current.posting.rid,
1033 data->current.posting.sid,
1034 data->current.posting.pos);
1035 grn_index_column_diff_posting_list_put(ctx,
1036 data,
1037 posting_list,
1038 GRN_INDEX_COLUMN_DIFF_TYPE_MISSINGS,
1039 &(data->current.posting));
1040 }
1041
1042 static void
1043 grn_index_column_diff_process_token(grn_ctx *ctx,
1044 grn_index_column_diff_data *data,
1045 const void *value_data,
1046 size_t value_size,
1047 grn_id value_domain)
1048 {
1049 if (value_size == 0) {
1050 return;
1051 }
1052
1053 const unsigned int token_cursor_flags = 0;
1054 grn_token_cursor *token_cursor = grn_token_cursor_open(ctx,
1055 data->lexicon,
1056 value_data,
1057 value_size,
1058 GRN_TOKEN_ADD,
1059 token_cursor_flags);
1060 if (!token_cursor) {
1061 return;
1062 }
1063
1064 grn_token_cursor_set_query_domain(ctx, token_cursor, value_domain);
1065
1066 const bool with_position = data->index.with_position;
1067 while (grn_token_cursor_get_status(ctx, token_cursor) ==
1068 GRN_TOKEN_CURSOR_DOING) {
1069 data->current.token_id = grn_token_cursor_next(ctx, token_cursor);
1070 if (data->current.token_id == GRN_ID_NIL) {
1071 continue;
1072 }
1073
1074 if (with_position && data->have_tokenizer) {
1075 grn_token *token = grn_token_cursor_get_token(ctx, token_cursor);
1076 data->current.posting.pos = grn_token_get_position(ctx, token);
1077 }
1078
1079 grn_index_column_diff_process_token_id(ctx, data);
1080 if (ctx->rc != GRN_SUCCESS) {
1081 break;
1082 }
1083 }
1084 grn_token_cursor_close(ctx, token_cursor);
1085 }
1086
1087 static void
1088 grn_index_column_diff_compute(grn_ctx *ctx, grn_index_column_diff_data *data)
1089 {
1090 grn_obj *source_columns = &(data->source_columns);
1091 const size_t n_source_columns = GRN_PTR_VECTOR_SIZE(source_columns);
1092
1093 grn_index_column_diff_init_progress(ctx, data);
1094
1095 GRN_TABLE_EACH_BEGIN_FLAGS(ctx,
1096 data->source_table,
1097 cursor,
1098 id,
1099 GRN_CURSOR_BY_ID)
1100 {
1101 grn_index_column_diff_progress(ctx, data);
1102 for (size_t i = 0; i < n_source_columns; i++) {
1103 grn_obj *source = GRN_PTR_VALUE_AT(source_columns, i);
1104 const bool is_reference = grn_obj_is_reference_column(ctx, source);
1105
1106 data->current.posting.rid = id;
1107 data->current.posting.sid = (uint32_t)(i + 1);
1108 /* This is faster than grn_hash_truncate() or grn_hash_create(). */
1109 GRN_HASH_EACH_BEGIN(ctx, data->current.token_counters, cursor, id)
1110 {
1111 grn_hash_cursor_delete(ctx, cursor, NULL);
1112 }
1113 GRN_HASH_EACH_END(ctx, cursor);
1114
1115 grn_obj *value = &(data->buffers.value);
1116 GRN_BULK_REWIND(value);
1117 grn_obj_get_value(ctx, source, id, value);
1118 if (ctx->rc != GRN_SUCCESS) {
1119 break;
1120 }
1121 grn_obj *extracted_value = grn_table_extract(ctx, data->lexicon, value);
1122 if (ctx->rc != GRN_SUCCESS) {
1123 break;
1124 }
1125 bool extracted = false;
1126 if (extracted_value != value) {
1127 extracted = true;
1128 value = extracted_value;
1129 }
1130
1131 switch (value->header.type) {
1132 case GRN_VECTOR:
1133 {
1134 const size_t n_elements = grn_vector_size(ctx, value);
1135 for (size_t j = 0; j < n_elements; j++) {
1136 const char *element = NULL;
1137 grn_id domain = GRN_ID_NIL;
1138 const unsigned int element_size =
1139 grn_vector_get_element(ctx,
1140 value,
1141 (uint32_t)j,
1142 &element,
1143 NULL,
1144 &domain);
1145 data->current.posting.sid = (uint32_t)(j + 1);
1146 if (!data->have_tokenizer) {
1147 data->current.posting.pos = j;
1148 }
1149 grn_index_column_diff_process_token(ctx,
1150 data,
1151 element,
1152 element_size,
1153 domain);
1154 if (ctx->rc != GRN_SUCCESS) {
1155 if (extracted) {
1156 grn_obj_close(ctx, extracted_value);
1157 }
1158 break;
1159 }
1160 }
1161 }
1162 break;
1163 case GRN_UVECTOR:
1164 if (is_reference) {
1165 const size_t n_elements = grn_uvector_size(ctx, value);
1166 for (size_t j = 0; j < n_elements; j++) {
1167 const grn_id element =
1168 grn_uvector_get_element(ctx, value, (uint32_t)j, NULL);
1169 if (element == GRN_ID_NIL) {
1170 continue;
1171 }
1172 data->current.token_id = element;
1173 if (data->have_tokenizer) {
1174 data->current.posting.pos = 0;
1175 } else {
1176 data->current.posting.pos = j;
1177 }
1178 grn_index_column_diff_process_token_id(ctx, data);
1179 if (ctx->rc != GRN_SUCCESS) {
1180 break;
1181 }
1182 }
1183 } else {
1184 const size_t n_elements = grn_uvector_size(ctx, value);
1185 const size_t element_size = grn_uvector_element_size(ctx, value);
1186 for (size_t j = 0; j < n_elements; j++) {
1187 const char *element = GRN_BULK_HEAD(value) + (element_size * j);
1188 data->current.token_id =
1189 grn_table_get(ctx,
1190 data->lexicon,
1191 element,
1192 (const unsigned int)element_size);
1193 if (data->current.token_id == GRN_ID_NIL) {
1194 continue;
1195 }
1196 if (data->have_tokenizer) {
1197 data->current.posting.pos = 0;
1198 } else {
1199 data->current.posting.pos = j;
1200 }
1201 grn_index_column_diff_process_token_id(ctx, data);
1202 if (ctx->rc != GRN_SUCCESS) {
1203 break;
1204 }
1205 }
1206 }
1207 break;
1208 case GRN_BULK:
1209 if (GRN_BULK_VSIZE(value) > 0) {
1210 if (is_reference) {
1211 data->current.token_id = GRN_RECORD_VALUE(value);
1212 if (data->current.token_id != GRN_ID_NIL) {
1213 data->current.posting.pos = 0;
1214 grn_index_column_diff_process_token_id(ctx, data);
1215 }
1216 } else {
1217 grn_index_column_diff_process_token(ctx,
1218 data,
1219 GRN_BULK_HEAD(value),
1220 GRN_BULK_VSIZE(value),
1221 value->header.domain);
1222 }
1223 }
1224 break;
1225 default:
1226 break;
1227 }
1228
1229 if (extracted) {
1230 grn_obj_close(ctx, extracted_value);
1231 }
1232
1233 if (ctx->rc != GRN_SUCCESS) {
1234 break;
1235 }
1236 }
1237 if (ctx->rc != GRN_SUCCESS) {
1238 break;
1239 }
1240 }
1241 GRN_TABLE_EACH_END(ctx, cursor);
1242
1243 GRN_HASH_EACH_BEGIN(ctx, data->posting_lists, cursor, id)
1244 {
1245 void *value;
1246 grn_hash_cursor_get_value(ctx, cursor, &value);
1247 grn_index_column_diff_posting_list *posting_list = value;
1248 grn_index_column_diff_posting_list_fin(ctx, data, posting_list);
1249 }
1250 GRN_HASH_EACH_END(ctx, cursor);
1251 grn_hash_close(ctx, data->posting_lists);
1252 }
1253
1254 grn_rc
1255 grn_index_column_diff_full(grn_ctx *ctx,
1256 grn_obj *index_column,
1257 grn_index_column_diff_options *options,
1258 grn_obj **diff)
1259 {
1260 grn_index_column_diff_data data = {0};
1261
1262 GRN_API_ENTER;
1263
1264 grn_index_column_diff_data_init(ctx, &data, options);
1265 if (ctx->rc != GRN_SUCCESS) {
1266 GRN_API_RETURN(ctx->rc);
1267 }
1268
1269 if (!index_column) {
1270 ERR(GRN_INVALID_ARGUMENT,
1271 "[index-column][diff] index column must not NULL");
1272 goto exit;
1273 }
1274 if (!grn_obj_is_index_column(ctx, index_column)) {
1275 char name[GRN_TABLE_MAX_KEY_SIZE];
1276 int name_size;
1277 name_size = grn_obj_name(ctx, index_column, name, sizeof(name));
1278 ERR(GRN_INVALID_ARGUMENT,
1279 "[index-column][diff] invalid index column: <%.*s>: <%s>",
1280 name_size,
1281 name,
1282 grn_obj_type_to_string(index_column->header.type));
1283 goto exit;
1284 }
1285 data.ii = (grn_ii *)index_column;
1286 {
1287 data.index.name_size =
1288 grn_obj_name(ctx, index_column, data.index.name, sizeof(data.index.name));
1289 grn_column_flags flags = grn_column_get_flags(ctx, index_column);
1290 data.index.with_section =
1291 ((flags & GRN_OBJ_WITH_SECTION) == GRN_OBJ_WITH_SECTION);
1292 data.index.with_position =
1293 ((flags & GRN_OBJ_WITH_POSITION) == GRN_OBJ_WITH_POSITION);
1294 data.index.n_elements = grn_ii_get_n_elements(ctx, data.ii);
1295 }
1296
1297 data.n_posting_elements = 1;
1298 if (data.index.with_section) {
1299 data.n_posting_elements++;
1300 }
1301 if (data.index.with_position) {
1302 data.n_posting_elements++;
1303 }
1304
1305 data.source_table = grn_ctx_at(ctx, grn_obj_get_range(ctx, index_column));
1306 {
1307 grn_obj source_columns;
1308 GRN_RECORD_INIT(&source_columns, GRN_OBJ_VECTOR, GRN_ID_NIL);
1309 grn_obj_get_info(ctx, index_column, GRN_INFO_SOURCE, &source_columns);
1310 size_t n_columns = GRN_RECORD_VECTOR_SIZE(&source_columns);
1311 for (size_t i = 0; i < n_columns; i++) {
1312 grn_id source_id = GRN_RECORD_VALUE_AT(&source_columns, i);
1313 grn_obj *source = grn_ctx_at(ctx, source_id);
1314 if (grn_obj_is_table(ctx, source)) {
1315 source = grn_obj_column(ctx,
1316 source,
1317 GRN_COLUMN_NAME_KEY,
1318 GRN_COLUMN_NAME_KEY_LEN);
1319 }
1320 GRN_PTR_PUT(ctx, &(data.source_columns), source);
1321 }
1322 GRN_OBJ_FIN(ctx, &source_columns);
1323 }
1324
1325 data.lexicon = grn_ctx_at(ctx, index_column->header.domain);
1326 if (ctx->rc != GRN_SUCCESS) {
1327 goto exit;
1328 }
1329 data.have_tokenizer = grn_table_have_tokenizer(ctx, data.lexicon);
1330
1331 data.diff = grn_table_create(ctx,
1332 NULL,
1333 0,
1334 NULL,
1335 GRN_TABLE_HASH_KEY,
1336 data.lexicon,
1337 NULL);
1338 if (!data.diff) {
1339 char message[GRN_CTX_MSGSIZE];
1340 grn_strcpy(message, GRN_CTX_MSGSIZE, ctx->errbuf);
1341 ERR(GRN_INVALID_ARGUMENT,
1342 "[index-column][diff] failed to create diff table: <%.*s>: %s",
1343 data.index.name_size,
1344 data.index.name,
1345 message);
1346 goto exit;
1347 }
1348 data.posting_lists =
1349 grn_hash_create(ctx,
1350 NULL,
1351 sizeof(grn_id),
1352 sizeof(grn_index_column_diff_posting_list),
1353 GRN_OBJ_TABLE_HASH_KEY);
1354 if (!data.posting_lists) {
1355 char message[GRN_CTX_MSGSIZE];
1356 grn_strcpy(message, GRN_CTX_MSGSIZE, ctx->errbuf);
1357 ERR(GRN_INVALID_ARGUMENT,
1358 "[index-column][diff] failed to create hash table for posting lists: "
1359 "<%.*s>: %s",
1360 data.index.name_size,
1361 data.index.name,
1362 message);
1363 goto exit;
1364 }
1365 DB_OBJ(data.posting_lists)->header.domain = grn_obj_id(ctx, data.diff);
1366 data.remains = grn_column_create(ctx,
1367 data.diff,
1368 remains_column_name,
1369 (unsigned int)strlen(remains_column_name),
1370 NULL,
1371 GRN_OBJ_COLUMN_VECTOR,
1372 grn_ctx_at(ctx, GRN_DB_UINT32));
1373 if (!data.remains) {
1374 char message[GRN_CTX_MSGSIZE];
1375 grn_strcpy(message, GRN_CTX_MSGSIZE, ctx->errbuf);
1376 ERR(GRN_INVALID_ARGUMENT,
1377 "[index-column][diff] failed to create remains column: <%.*s>: %s",
1378 data.index.name_size,
1379 data.index.name,
1380 message);
1381 goto exit;
1382 }
1383 data.missings = grn_column_create(ctx,
1384 data.diff,
1385 missings_column_name,
1386 (unsigned int)strlen(missings_column_name),
1387 NULL,
1388 GRN_OBJ_COLUMN_VECTOR,
1389 grn_ctx_at(ctx, GRN_DB_UINT32));
1390 if (!data.missings) {
1391 char message[GRN_CTX_MSGSIZE];
1392 grn_strcpy(message, GRN_CTX_MSGSIZE, ctx->errbuf);
1393 ERR(GRN_INVALID_ARGUMENT,
1394 "[index-column][diff] failed to create missings column: <%.*s>: %s",
1395 data.index.name_size,
1396 data.index.name,
1397 message);
1398 goto exit;
1399 }
1400
1401 grn_index_column_diff_compute(ctx, &data);
1402 *diff = data.diff;
1403 data.diff = NULL;
1404
1405 exit:
1406 if (data.diff) {
1407 grn_obj_close(ctx, data.diff);
1408 }
1409
1410 grn_index_column_diff_data_fin(ctx, &data);
1411
1412 GRN_API_RETURN(ctx->rc);
1413 }
1414
1415 grn_rc
1416 grn_index_column_diff(grn_ctx *ctx, grn_obj *index_column, grn_obj **diff)
1417 {
1418 return grn_index_column_diff_full(ctx, index_column, NULL, diff);
1419 }
1420
1421 grn_obj *
1422 grn_index_column_get_tokenizer(grn_ctx *ctx, grn_obj *index_column)
1423 {
1424 grn_obj *tokenizer;
1425 grn_obj *lexicon;
1426
1427 lexicon = grn_ctx_at(ctx, index_column->header.domain);
1428 if (!lexicon) {
1429 return NULL;
1430 }
1431
1432 grn_table_get_info(ctx, lexicon, NULL, NULL, &tokenizer, NULL, NULL);
1433 grn_obj_unref(ctx, lexicon);
1434
1435 return tokenizer;
1436 }
1437
1438 static bool
1439 grn_index_column_is_usable_equal(grn_ctx *ctx,
1440 grn_obj *index_column,
1441 grn_operator op)
1442 {
1443 grn_obj *tokenizer;
1444 tokenizer = grn_index_column_get_tokenizer(ctx, index_column);
1445 if (!tokenizer) {
1446 return true;
1447 }
1448 /* TODO: Improve this. Which tokenizer can be used with equal operation?
1449 Do we need metadata in tokenizer? */
1450 if (DB_OBJ(tokenizer)->id == GRN_DB_DELIMIT) {
1451 return true;
1452 }
1453 return false;
1454 }
1455
1456 static bool
1457 grn_index_column_is_usable_match(grn_ctx *ctx,
1458 grn_obj *index_column,
1459 grn_operator op)
1460 {
1461 return true;
1462 }
1463
1464 static bool
1465 grn_index_column_is_usable_range(grn_ctx *ctx,
1466 grn_obj *index_column,
1467 grn_operator op)
1468 {
1469 grn_obj *tokenizer;
1470 grn_obj *lexicon;
1471
1472 lexicon = grn_ctx_at(ctx, index_column->header.domain);
1473 if (!lexicon) {
1474 return false;
1475 }
1476
1477 switch (lexicon->header.type) {
1478 case GRN_TABLE_PAT_KEY:
1479 case GRN_TABLE_DAT_KEY:
1480 break;
1481 default:
1482 grn_obj_unref(ctx, lexicon);
1483 return false;
1484 }
1485
1486 grn_table_get_info(ctx, lexicon, NULL, NULL, &tokenizer, NULL, NULL);
1487 grn_obj_unref(ctx, lexicon);
1488 return tokenizer == NULL;
1489 }
1490
1491 static bool
1492 grn_index_column_is_usable_regexp(grn_ctx *ctx,
1493 grn_obj *index_column,
1494 grn_operator op)
1495 {
1496 grn_obj *tokenizer;
1497 tokenizer = grn_index_column_get_tokenizer(ctx, index_column);
1498 if (!tokenizer) {
1499 return false;
1500 }
1501 return tokenizer == grn_ctx_get(ctx, "TokenRegexp", -1);
1502 }
1503
1504 bool
1505 grn_index_column_is_usable(grn_ctx *ctx, grn_obj *index_column, grn_operator op)
1506 {
1507 GRN_API_ENTER;
1508
1509 if (!grn_obj_is_index_column(ctx, index_column)) {
1510 GRN_API_RETURN(false);
1511 }
1512
1513 if (!grn_obj_is_visible(ctx, index_column)) {
1514 GRN_API_RETURN(false);
1515 }
1516
1517 bool usable = false;
1518 switch (op) {
1519 case GRN_OP_EQUAL:
1520 case GRN_OP_NOT_EQUAL:
1521 usable = grn_index_column_is_usable_equal(ctx, index_column, op);
1522 break;
1523 case GRN_OP_MATCH:
1524 case GRN_OP_NEAR:
1525 case GRN_OP_NEAR_NO_OFFSET:
1526 case GRN_OP_NEAR_PHRASE:
1527 case GRN_OP_ORDERED_NEAR_PHRASE:
1528 case GRN_OP_NEAR_PHRASE_PRODUCT:
1529 case GRN_OP_ORDERED_NEAR_PHRASE_PRODUCT:
1530 case GRN_OP_SIMILAR:
1531 case GRN_OP_PREFIX:
1532 case GRN_OP_SUFFIX:
1533 case GRN_OP_FUZZY:
1534 case GRN_OP_QUORUM:
1535 usable = grn_index_column_is_usable_match(ctx, index_column, op);
1536 break;
1537 case GRN_OP_LESS:
1538 case GRN_OP_GREATER:
1539 case GRN_OP_LESS_EQUAL:
1540 case GRN_OP_GREATER_EQUAL:
1541 case GRN_OP_CALL:
1542 usable = grn_index_column_is_usable_range(ctx, index_column, op);
1543 break;
1544 case GRN_OP_REGEXP:
1545 usable = grn_index_column_is_usable_regexp(ctx, index_column, op);
1546 break;
1547 default:
1548 break;
1549 }
1550
1551 GRN_API_RETURN(usable);
1552 }