Debian Codegraph @

lib/obj.c

raw symbols SCIP
1 /*
2 Copyright (C) 2015-2018 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.h"
21 #include "grn_accessor.h"
22 #include "grn_ctx_impl.h"
23 #include "grn_expr.h"
24 #include "grn_dat.h"
25 #include "grn_float.h"
26 #include "grn_hash.h"
27 #include "grn_ii.h"
28 #include "grn_index_column.h"
29 #include "grn_pat.h"
30 #include "grn_store.h"
31 #include "grn_table.h"
32 #include "grn_token_column.h"
33
34 const char *
35 grn_obj_set_flag_to_string(int flags)
36 {
37 const char *message = "invalid set flag";
38
39 switch (flags & GRN_OBJ_SET_MASK) {
40 case GRN_OBJ_SET:
41 message = "set";
42 break;
43 case GRN_OBJ_INCR:
44 message = "increment";
45 break;
46 case GRN_OBJ_DECR:
47 message = "decrement";
48 break;
49 case GRN_OBJ_APPEND:
50 message = "append";
51 break;
52 case GRN_OBJ_PREPEND:
53 message = "prepend";
54 break;
55 default:
56 break;
57 }
58
59 return message;
60 }
61
62 bool
63 grn_obj_is_true(grn_ctx *ctx, grn_obj *obj)
64 {
65 if (!obj) {
66 return false;
67 }
68
69 switch (obj->header.type) {
70 case GRN_BULK:
71 switch (obj->header.domain) {
72 case GRN_DB_BOOL:
73 return GRN_BOOL_VALUE(obj);
74 case GRN_DB_INT32:
75 return GRN_INT32_VALUE(obj) != 0;
76 case GRN_DB_UINT32:
77 return GRN_UINT32_VALUE(obj) != 0;
78 #ifdef GRN_HAVE_BFLOAT16
79 case GRN_DB_BFLOAT16:
80 {
81 grn_bfloat16 bfloat16_value;
82 bfloat16_value = GRN_BFLOAT16_VALUE(obj);
83 return grn_bfloat16_is_zero(bfloat16_value);
84 }
85 #endif
86 case GRN_DB_FLOAT32:
87 {
88 float float_value;
89 float_value = GRN_FLOAT32_VALUE(obj);
90 return grn_float32_is_zero(float_value);
91 }
92 case GRN_DB_FLOAT:
93 {
94 double float_value;
95 float_value = GRN_FLOAT_VALUE(obj);
96 return grn_float_is_zero(float_value);
97 }
98 case GRN_DB_SHORT_TEXT:
99 case GRN_DB_TEXT:
100 case GRN_DB_LONG_TEXT:
101 return GRN_TEXT_LEN(obj) != 0;
102 default:
103 if (grn_id_maybe_table(ctx, obj->header.domain)) {
104 return GRN_BULK_VSIZE(obj) > 0 && GRN_PTR_VALUE(obj) != GRN_ID_NIL;
105 } else {
106 return false;
107 }
108 }
109 break;
110 case GRN_VECTOR:
111 if (grn_ctx_get_command_version(ctx) < GRN_COMMAND_VERSION_3) {
112 return true;
113 } else {
114 return grn_vector_size(ctx, obj) > 0;
115 }
116 case GRN_UVECTOR:
117 if (grn_ctx_get_command_version(ctx) < GRN_COMMAND_VERSION_3) {
118 return true;
119 } else {
120 return grn_uvector_size(ctx, obj) > 0;
121 }
122 default:
123 return false;
124 }
125 }
126
127 bool
128 grn_obj_is_temporary(grn_ctx *ctx, grn_obj *obj)
129 {
130 if (!obj) {
131 return false;
132 }
133
134 if (!GRN_DB_OBJP(obj)) {
135 return false;
136 }
137
138 if (DB_OBJ(obj)->id & GRN_OBJ_TMP_OBJECT) {
139 return true;
140 }
141
142 if (DB_OBJ(obj)->id == GRN_ID_NIL) {
143 return true;
144 }
145
146 return false;
147 }
148
149 bool
150 grn_obj_is_builtin(grn_ctx *ctx, grn_obj *obj)
151 {
152 grn_id id;
153
154 if (!obj) {
155 return false;
156 }
157
158 id = grn_obj_id(ctx, obj);
159 return grn_id_is_builtin(ctx, id);
160 }
161
162 bool
163 grn_obj_is_bulk(grn_ctx *ctx, grn_obj *obj)
164 {
165 if (!obj) {
166 return false;
167 }
168
169 return obj->header.type == GRN_BULK;
170 }
171
172 bool
173 grn_obj_is_text_family_bulk(grn_ctx *ctx, grn_obj *obj)
174 {
175 if (!grn_obj_is_bulk(ctx, obj)) {
176 return false;
177 }
178
179 return grn_type_id_is_text_family(ctx, obj->header.domain);
180 }
181
182 bool
183 grn_obj_is_binary_family_bulk(grn_ctx *ctx, grn_obj *obj)
184 {
185 if (!grn_obj_is_bulk(ctx, obj)) {
186 return false;
187 }
188
189 return grn_type_id_is_binary_family(ctx, obj->header.domain);
190 }
191
192 bool
193 grn_obj_is_number_family_bulk(grn_ctx *ctx, grn_obj *obj)
194 {
195 if (!grn_obj_is_bulk(ctx, obj)) {
196 return false;
197 }
198
199 return grn_type_id_is_number_family(ctx, obj->header.domain);
200 }
201
202 bool
203 grn_obj_maybe_record_bulk(grn_ctx *ctx, grn_obj *obj)
204 {
205 if (!grn_obj_is_bulk(ctx, obj)) {
206 return false;
207 }
208
209 return grn_id_maybe_table(ctx, obj->header.domain);
210 }
211
212 bool
213 grn_obj_is_vector(grn_ctx *ctx, grn_obj *obj)
214 {
215 if (!obj) {
216 return false;
217 }
218
219 return obj->header.type == GRN_VECTOR;
220 }
221
222 bool
223 grn_obj_is_text_family_vector(grn_ctx *ctx, grn_obj *obj)
224 {
225 if (!grn_obj_is_vector(ctx, obj)) {
226 return false;
227 }
228
229 return grn_type_id_is_text_family(ctx, obj->header.domain);
230 }
231
232 bool
233 grn_obj_is_binary_family_vector(grn_ctx *ctx, grn_obj *obj)
234 {
235 if (!grn_obj_is_vector(ctx, obj)) {
236 return false;
237 }
238
239 return grn_type_id_is_binary_family(ctx, obj->header.domain);
240 }
241
242 bool
243 grn_obj_is_number_family_vector(grn_ctx *ctx, grn_obj *obj)
244 {
245 if (!grn_obj_is_uvector(ctx, obj)) {
246 return false;
247 }
248
249 return grn_type_id_is_number_family(ctx, obj->header.domain);
250 }
251
252 bool
253 grn_obj_is_weight_vector(grn_ctx *ctx, grn_obj *obj)
254 {
255 if (!grn_obj_is_vector(ctx, obj)) {
256 return false;
257 }
258
259 return obj->header.flags & GRN_OBJ_WITH_WEIGHT;
260 }
261
262 bool
263 grn_obj_is_uvector(grn_ctx *ctx, grn_obj *obj)
264 {
265 if (!obj) {
266 return false;
267 }
268
269 return obj->header.type == GRN_UVECTOR;
270 }
271
272 bool
273 grn_obj_is_weight_uvector(grn_ctx *ctx, grn_obj *obj)
274 {
275 if (!grn_obj_is_uvector(ctx, obj)) {
276 return false;
277 }
278
279 return obj->header.flags & GRN_OBJ_WITH_WEIGHT;
280 }
281
282 bool
283 grn_obj_is_db(grn_ctx *ctx, grn_obj *obj)
284 {
285 if (!obj) {
286 return false;
287 }
288
289 return obj->header.type == GRN_DB;
290 }
291
292 bool
293 grn_obj_is_table(grn_ctx *ctx, grn_obj *obj)
294 {
295 bool is_table = false;
296
297 if (!obj) {
298 return false;
299 }
300
301 switch (obj->header.type) {
302 case GRN_TABLE_NO_KEY:
303 case GRN_TABLE_HASH_KEY:
304 case GRN_TABLE_PAT_KEY:
305 case GRN_TABLE_DAT_KEY:
306 is_table = true;
307 break;
308 default:
309 break;
310 }
311
312 return is_table;
313 }
314
315 bool
316 grn_obj_is_table_with_key(grn_ctx *ctx, grn_obj *obj)
317 {
318 bool is_table_with_key = false;
319
320 if (!obj) {
321 return false;
322 }
323
324 switch (obj->header.type) {
325 case GRN_TABLE_HASH_KEY:
326 case GRN_TABLE_PAT_KEY:
327 case GRN_TABLE_DAT_KEY:
328 is_table_with_key = true;
329 default:
330 break;
331 }
332
333 return is_table_with_key;
334 }
335
336 bool
337 grn_obj_is_table_with_value(grn_ctx *ctx, grn_obj *obj)
338 {
339 bool is_table_with_value = false;
340
341 if (!obj) {
342 return false;
343 }
344
345 switch (obj->header.type) {
346 case GRN_TABLE_HASH_KEY:
347 case GRN_TABLE_PAT_KEY:
348 case GRN_TABLE_DAT_KEY:
349 case GRN_TABLE_NO_KEY:
350 is_table_with_value = DB_OBJ(obj)->range != GRN_ID_NIL;
351 default:
352 break;
353 }
354
355 return is_table_with_value;
356 }
357
358 bool
359 grn_obj_is_lexicon(grn_ctx *ctx, grn_obj *obj)
360 {
361 if (!grn_obj_is_table_with_key(ctx, obj)) {
362 return false;
363 }
364
365 grn_hash *columns = grn_hash_create(ctx,
366 NULL,
367 sizeof(grn_id),
368 0,
369 GRN_OBJ_TABLE_HASH_KEY | GRN_HASH_TINY);
370 if (!columns) {
371 return false;
372 }
373 grn_table_columns(ctx, obj, "", 0, (grn_obj *)columns);
374 bool is_lexicon = false;
375 if (grn_hash_size(ctx, columns) > 0) {
376 GRN_HASH_EACH_BEGIN(ctx, columns, cursor, id)
377 {
378 void *key;
379 grn_hash_cursor_get_key(ctx, cursor, &key);
380 grn_id column_id = *((grn_id *)key);
381 grn_obj *column = grn_ctx_at(ctx, column_id);
382 if (grn_obj_is_index_column(ctx, column)) {
383 is_lexicon = true;
384 }
385 grn_obj_unref(ctx, column);
386 if (is_lexicon) {
387 break;
388 }
389 }
390 GRN_HASH_EACH_END(ctx, cursor);
391 }
392 grn_hash_close(ctx, columns);
393 return is_lexicon;
394 }
395
396 bool
397 grn_obj_is_lexicon_without_data_column(grn_ctx *ctx, grn_obj *obj)
398 {
399 if (!grn_obj_is_table_with_key(ctx, obj)) {
400 return false;
401 }
402
403 grn_hash *columns = grn_hash_create(ctx,
404 NULL,
405 sizeof(grn_id),
406 0,
407 GRN_OBJ_TABLE_HASH_KEY | GRN_HASH_TINY);
408 if (!columns) {
409 return false;
410 }
411 grn_table_columns(ctx, obj, "", 0, (grn_obj *)columns);
412 bool is_lexicon_without_data_column;
413 if (grn_hash_size(ctx, columns) == 0) {
414 is_lexicon_without_data_column = false;
415 } else {
416 is_lexicon_without_data_column = true;
417 GRN_HASH_EACH_BEGIN(ctx, columns, cursor, id)
418 {
419 void *key;
420 grn_hash_cursor_get_key(ctx, cursor, &key);
421 grn_id column_id = *((grn_id *)key);
422 grn_obj *column = grn_ctx_at(ctx, column_id);
423 if (!grn_obj_is_index_column(ctx, column)) {
424 is_lexicon_without_data_column = false;
425 }
426 grn_obj_unref(ctx, column);
427 if (!is_lexicon_without_data_column) {
428 break;
429 }
430 }
431 GRN_HASH_EACH_END(ctx, cursor);
432 }
433 grn_hash_close(ctx, columns);
434 return is_lexicon_without_data_column;
435 }
436
437 bool
438 grn_obj_is_tiny_hash_table(grn_ctx *ctx, grn_obj *obj)
439 {
440 if (!obj) {
441 return false;
442 }
443
444 if (obj->header.type != GRN_TABLE_HASH_KEY) {
445 return false;
446 }
447
448 return obj->header.flags & GRN_HASH_TINY;
449 }
450
451 bool
452 grn_obj_is_patricia_trie(grn_ctx *ctx, grn_obj *obj)
453 {
454 if (!obj) {
455 return false;
456 }
457
458 return obj->header.type == GRN_TABLE_PAT_KEY;
459 }
460
461 bool
462 grn_obj_is_result_set(grn_ctx *ctx, grn_obj *obj)
463 {
464 if (!grn_obj_is_temporary(ctx, obj)) {
465 return false;
466 }
467
468 if (obj->header.type != GRN_TABLE_HASH_KEY) {
469 return false;
470 }
471
472 grn_table_flags flags = 0;
473 grn_table_get_info(ctx, obj, &flags, NULL, NULL, NULL, NULL);
474 return flags & GRN_OBJ_WITH_SUBREC;
475 }
476
477 bool
478 grn_obj_is_column(grn_ctx *ctx, grn_obj *obj)
479 {
480 bool is_column = false;
481
482 if (!obj) {
483 return false;
484 }
485
486 switch (obj->header.type) {
487 case GRN_COLUMN_FIX_SIZE:
488 case GRN_COLUMN_VAR_SIZE:
489 case GRN_COLUMN_INDEX:
490 is_column = true;
491 default:
492 break;
493 }
494
495 return is_column;
496 }
497
498 bool
499 grn_obj_is_number_family_column(grn_ctx *ctx, grn_obj *obj)
500 {
501 if (!grn_obj_is_column(ctx, obj)) {
502 return false;
503 }
504
505 return grn_type_id_is_number_family(ctx, grn_obj_get_range(ctx, obj));
506 }
507
508 bool
509 grn_obj_is_scalar_column(grn_ctx *ctx, grn_obj *obj)
510 {
511 if (!grn_obj_is_column(ctx, obj)) {
512 return false;
513 }
514
515 return (obj->header.flags & GRN_OBJ_COLUMN_TYPE_MASK) ==
516 GRN_OBJ_COLUMN_SCALAR;
517 }
518
519 bool
520 grn_obj_is_text_family_scalar_column(grn_ctx *ctx, grn_obj *obj)
521 {
522 if (!grn_obj_is_scalar_column(ctx, obj)) {
523 return false;
524 }
525
526 return grn_type_id_is_text_family(ctx, grn_obj_get_range(ctx, obj));
527 }
528
529 bool
530 grn_obj_is_binary_family_scalar_column(grn_ctx *ctx, grn_obj *obj)
531 {
532 if (!grn_obj_is_scalar_column(ctx, obj)) {
533 return false;
534 }
535
536 return grn_type_id_is_binary_family(ctx, grn_obj_get_range(ctx, obj));
537 }
538
539 bool
540 grn_obj_is_number_family_scalar_column(grn_ctx *ctx, grn_obj *obj)
541 {
542 if (!grn_obj_is_scalar_column(ctx, obj)) {
543 return false;
544 }
545
546 return grn_type_id_is_number_family(ctx, grn_obj_get_range(ctx, obj));
547 }
548
549 bool
550 grn_obj_is_vector_column(grn_ctx *ctx, grn_obj *obj)
551 {
552 if (!grn_obj_is_column(ctx, obj)) {
553 return false;
554 }
555
556 return (
557 (obj->header.type == GRN_COLUMN_VAR_SIZE) &&
558 ((obj->header.flags & GRN_OBJ_COLUMN_TYPE_MASK) == GRN_OBJ_COLUMN_VECTOR));
559 }
560
561 bool
562 grn_obj_is_text_family_vector_column(grn_ctx *ctx, grn_obj *obj)
563 {
564 if (!grn_obj_is_vector_column(ctx, obj)) {
565 return false;
566 }
567
568 return grn_type_id_is_text_family(ctx, grn_obj_get_range(ctx, obj));
569 }
570
571 bool
572 grn_obj_is_binary_family_vector_column(grn_ctx *ctx, grn_obj *obj)
573 {
574 if (!grn_obj_is_vector_column(ctx, obj)) {
575 return false;
576 }
577
578 return grn_type_id_is_binary_family(ctx, grn_obj_get_range(ctx, obj));
579 }
580
581 bool
582 grn_obj_is_weight_vector_column(grn_ctx *ctx, grn_obj *obj)
583 {
584 if (!grn_obj_is_vector_column(ctx, obj)) {
585 return false;
586 }
587
588 return (obj->header.flags & GRN_OBJ_WITH_WEIGHT) == GRN_OBJ_WITH_WEIGHT;
589 }
590
591 bool
592 grn_obj_is_reference_column(grn_ctx *ctx, grn_obj *obj)
593 {
594 if (!grn_obj_is_column(ctx, obj)) {
595 return false;
596 }
597
598 const grn_id range_id = grn_obj_get_range(ctx, obj);
599 if (grn_id_is_builtin(ctx, range_id)) {
600 return false;
601 }
602
603 grn_obj *range = range = grn_ctx_at(ctx, range_id);
604 if (!range) {
605 return false;
606 }
607
608 uint8_t range_type = range->header.type;
609 grn_obj_unref(ctx, range);
610
611 switch (range_type) {
612 case GRN_TABLE_HASH_KEY:
613 case GRN_TABLE_PAT_KEY:
614 case GRN_TABLE_DAT_KEY:
615 case GRN_TABLE_NO_KEY:
616 return true;
617 default:
618 return false;
619 }
620 }
621
622 bool
623 grn_obj_is_data_column(grn_ctx *ctx, grn_obj *obj)
624 {
625 if (!grn_obj_is_column(ctx, obj)) {
626 return false;
627 }
628
629 return obj->header.type == GRN_COLUMN_FIX_SIZE ||
630 obj->header.type == GRN_COLUMN_VAR_SIZE;
631 }
632
633 /**
634 * \brief Check whether the specified object is an index column or not.
635 *
636 * \param ctx The context object
637 * \param obj The target object to check
638 *
639 * \return `true` if the object is exactly an index column, `false` otherwise.
640 */
641 bool
642 grn_obj_is_index_column(grn_ctx *ctx, grn_obj *obj)
643 {
644 if (!grn_obj_is_column(ctx, obj)) {
645 return false;
646 }
647
648 return obj->header.type == GRN_COLUMN_INDEX;
649 }
650
651 bool
652 grn_obj_is_accessor(grn_ctx *ctx, grn_obj *obj)
653 {
654 if (!obj) {
655 return false;
656 }
657
658 return obj->header.type == GRN_ACCESSOR;
659 }
660
661 bool
662 grn_obj_is_id_accessor(grn_ctx *ctx, grn_obj *obj)
663 {
664 grn_accessor *accessor;
665
666 if (!grn_obj_is_accessor(ctx, obj)) {
667 return false;
668 }
669
670 accessor = (grn_accessor *)obj;
671 if (accessor->next) {
672 return false;
673 }
674
675 return accessor->action == GRN_ACCESSOR_GET_ID;
676 }
677
678 bool
679 grn_obj_is_key_accessor(grn_ctx *ctx, grn_obj *obj)
680 {
681 grn_accessor *accessor;
682
683 if (!grn_obj_is_accessor(ctx, obj)) {
684 return false;
685 }
686
687 accessor = (grn_accessor *)obj;
688 if (accessor->next) {
689 return false;
690 }
691
692 return accessor->action == GRN_ACCESSOR_GET_KEY;
693 }
694
695 bool
696 grn_obj_is_value_accessor(grn_ctx *ctx, grn_obj *obj)
697 {
698 grn_accessor *accessor;
699
700 if (!grn_obj_is_accessor(ctx, obj)) {
701 return false;
702 }
703
704 accessor = (grn_accessor *)obj;
705 if (accessor->next) {
706 return false;
707 }
708
709 return accessor->action == GRN_ACCESSOR_GET_VALUE;
710 }
711
712 bool
713 grn_obj_is_score_accessor(grn_ctx *ctx, grn_obj *obj)
714 {
715 grn_accessor *accessor;
716
717 if (!grn_obj_is_accessor(ctx, obj)) {
718 return false;
719 }
720
721 accessor = (grn_accessor *)obj;
722 if (accessor->next) {
723 return false;
724 }
725
726 return accessor->action == GRN_ACCESSOR_GET_SCORE;
727 }
728
729 bool
730 grn_obj_is_referable_score_accessor(grn_ctx *ctx, grn_obj *obj)
731 {
732 if (!grn_obj_is_score_accessor(ctx, obj)) {
733 return false;
734 }
735
736 grn_accessor *accessor = (grn_accessor *)obj;
737 grn_id parent_domain = accessor->obj->header.domain;
738 if (!(parent_domain & GRN_OBJ_TMP_OBJECT)) {
739 return true;
740 }
741
742 bool need_recursive_collect = false;
743 grn_obj *parent = grn_ctx_at(ctx, parent_domain);
744 if (parent) {
745 if (parent->header.flags & GRN_OBJ_WITH_SUBREC) {
746 need_recursive_collect = true;
747 }
748 grn_obj_unref(ctx, parent);
749 }
750
751 return !need_recursive_collect;
752 }
753
754 bool
755 grn_obj_is_nsubrecs_accessor(grn_ctx *ctx, grn_obj *obj)
756 {
757 grn_accessor *accessor;
758
759 if (!grn_obj_is_accessor(ctx, obj)) {
760 return false;
761 }
762
763 accessor = (grn_accessor *)obj;
764 if (accessor->next) {
765 return false;
766 }
767
768 return accessor->action == GRN_ACCESSOR_GET_NSUBRECS;
769 }
770
771 bool
772 grn_obj_is_max_accessor(grn_ctx *ctx, grn_obj *obj)
773 {
774 grn_accessor *accessor;
775
776 if (!grn_obj_is_accessor(ctx, obj)) {
777 return false;
778 }
779
780 accessor = (grn_accessor *)obj;
781 if (accessor->next) {
782 return false;
783 }
784
785 return accessor->action == GRN_ACCESSOR_GET_MAX;
786 }
787
788 bool
789 grn_obj_is_min_accessor(grn_ctx *ctx, grn_obj *obj)
790 {
791 grn_accessor *accessor;
792
793 if (!grn_obj_is_accessor(ctx, obj)) {
794 return false;
795 }
796
797 accessor = (grn_accessor *)obj;
798 if (accessor->next) {
799 return false;
800 }
801
802 return accessor->action == GRN_ACCESSOR_GET_MIN;
803 }
804
805 bool
806 grn_obj_is_sum_accessor(grn_ctx *ctx, grn_obj *obj)
807 {
808 grn_accessor *accessor;
809
810 if (!grn_obj_is_accessor(ctx, obj)) {
811 return false;
812 }
813
814 accessor = (grn_accessor *)obj;
815 if (accessor->next) {
816 return false;
817 }
818
819 return accessor->action == GRN_ACCESSOR_GET_SUM;
820 }
821
822 bool
823 grn_obj_is_avg_accessor(grn_ctx *ctx, grn_obj *obj)
824 {
825 grn_accessor *accessor;
826
827 if (!grn_obj_is_accessor(ctx, obj)) {
828 return false;
829 }
830
831 accessor = (grn_accessor *)obj;
832 if (accessor->next) {
833 return false;
834 }
835
836 return accessor->action == GRN_ACCESSOR_GET_AVG;
837 }
838
839 bool
840 grn_obj_is_mean_accessor(grn_ctx *ctx, grn_obj *obj)
841 {
842 grn_accessor *accessor;
843
844 if (!grn_obj_is_accessor(ctx, obj)) {
845 return false;
846 }
847
848 accessor = (grn_accessor *)obj;
849 if (accessor->next) {
850 return false;
851 }
852
853 return accessor->action == GRN_ACCESSOR_GET_MEAN;
854 }
855
856 bool
857 grn_obj_is_column_value_accessor(grn_ctx *ctx, grn_obj *obj)
858 {
859 grn_accessor *accessor;
860
861 if (!grn_obj_is_accessor(ctx, obj)) {
862 return false;
863 }
864
865 accessor = (grn_accessor *)obj;
866 if (accessor->next) {
867 return false;
868 }
869
870 return accessor->action == GRN_ACCESSOR_GET_COLUMN_VALUE;
871 }
872
873 bool
874 grn_obj_is_scalar_accessor(grn_ctx *ctx, grn_obj *obj)
875 {
876 grn_accessor *accessor;
877
878 if (!grn_obj_is_accessor(ctx, obj)) {
879 return false;
880 }
881
882 accessor = (grn_accessor *)obj;
883 while (accessor->next) {
884 accessor = accessor->next;
885 }
886
887 switch (accessor->action) {
888 case GRN_ACCESSOR_GET_ID:
889 case GRN_ACCESSOR_GET_SCORE:
890 case GRN_ACCESSOR_GET_NSUBRECS:
891 case GRN_ACCESSOR_GET_MAX:
892 case GRN_ACCESSOR_GET_MIN:
893 case GRN_ACCESSOR_GET_SUM:
894 case GRN_ACCESSOR_GET_AVG:
895 case GRN_ACCESSOR_GET_MEAN:
896 case GRN_ACCESSOR_GET_VALUE:
897 return true;
898 case GRN_ACCESSOR_GET_COLUMN_VALUE:
899 return grn_obj_is_scalar_column(ctx, accessor->obj);
900 default:
901 return false;
902 }
903 }
904
905 bool
906 grn_obj_is_text_family_scalar_accessor(grn_ctx *ctx, grn_obj *obj)
907 {
908 grn_accessor *accessor;
909
910 if (!grn_obj_is_accessor(ctx, obj)) {
911 return false;
912 }
913
914 accessor = (grn_accessor *)obj;
915 while (accessor->next) {
916 accessor = accessor->next;
917 }
918
919 switch (accessor->action) {
920 case GRN_ACCESSOR_GET_ID:
921 case GRN_ACCESSOR_GET_SCORE:
922 case GRN_ACCESSOR_GET_NSUBRECS:
923 case GRN_ACCESSOR_GET_MAX:
924 case GRN_ACCESSOR_GET_MIN:
925 case GRN_ACCESSOR_GET_SUM:
926 case GRN_ACCESSOR_GET_AVG:
927 case GRN_ACCESSOR_GET_MEAN:
928 return false;
929 case GRN_ACCESSOR_GET_VALUE:
930 return grn_type_id_is_text_family(ctx,
931 grn_obj_get_range(ctx, accessor->obj));
932 case GRN_ACCESSOR_GET_COLUMN_VALUE:
933 return grn_obj_is_text_family_scalar_column(ctx, accessor->obj);
934 default:
935 return false;
936 }
937 }
938
939 bool
940 grn_obj_is_binary_family_scalar_accessor(grn_ctx *ctx, grn_obj *obj)
941 {
942 grn_accessor *accessor;
943
944 if (!grn_obj_is_accessor(ctx, obj)) {
945 return false;
946 }
947
948 accessor = (grn_accessor *)obj;
949 while (accessor->next) {
950 accessor = accessor->next;
951 }
952
953 switch (accessor->action) {
954 case GRN_ACCESSOR_GET_ID:
955 case GRN_ACCESSOR_GET_SCORE:
956 case GRN_ACCESSOR_GET_NSUBRECS:
957 case GRN_ACCESSOR_GET_MAX:
958 case GRN_ACCESSOR_GET_MIN:
959 case GRN_ACCESSOR_GET_SUM:
960 case GRN_ACCESSOR_GET_AVG:
961 case GRN_ACCESSOR_GET_MEAN:
962 return false;
963 case GRN_ACCESSOR_GET_VALUE:
964 return grn_type_id_is_binary_family(ctx,
965 grn_obj_get_range(ctx, accessor->obj));
966 case GRN_ACCESSOR_GET_COLUMN_VALUE:
967 return grn_obj_is_binary_family_scalar_column(ctx, accessor->obj);
968 default:
969 return false;
970 }
971 }
972
973 bool
974 grn_obj_is_number_family_scalar_accessor(grn_ctx *ctx, grn_obj *obj)
975 {
976 grn_accessor *accessor;
977
978 if (!grn_obj_is_accessor(ctx, obj)) {
979 return false;
980 }
981
982 accessor = (grn_accessor *)obj;
983 while (accessor->next) {
984 accessor = accessor->next;
985 }
986
987 switch (accessor->action) {
988 case GRN_ACCESSOR_GET_ID:
989 case GRN_ACCESSOR_GET_SCORE:
990 case GRN_ACCESSOR_GET_NSUBRECS:
991 case GRN_ACCESSOR_GET_MAX:
992 case GRN_ACCESSOR_GET_MIN:
993 case GRN_ACCESSOR_GET_SUM:
994 case GRN_ACCESSOR_GET_AVG:
995 case GRN_ACCESSOR_GET_MEAN:
996 return true;
997 case GRN_ACCESSOR_GET_VALUE:
998 return grn_type_id_is_number_family(ctx,
999 grn_obj_get_range(ctx, accessor->obj));
1000 case GRN_ACCESSOR_GET_COLUMN_VALUE:
1001 return grn_obj_is_number_family_scalar_column(ctx, accessor->obj);
1002 default:
1003 return false;
1004 }
1005 }
1006
1007 bool
1008 grn_obj_is_vector_accessor(grn_ctx *ctx, grn_obj *obj)
1009 {
1010 grn_accessor *accessor;
1011
1012 if (!grn_obj_is_accessor(ctx, obj)) {
1013 return false;
1014 }
1015
1016 accessor = (grn_accessor *)obj;
1017 while (accessor->next) {
1018 accessor = accessor->next;
1019 }
1020
1021 switch (accessor->action) {
1022 case GRN_ACCESSOR_GET_COLUMN_VALUE:
1023 return grn_obj_is_vector_column(ctx, accessor->obj);
1024 default:
1025 return false;
1026 }
1027 }
1028
1029 bool
1030 grn_obj_is_type(grn_ctx *ctx, grn_obj *obj)
1031 {
1032 if (!obj) {
1033 return false;
1034 }
1035
1036 return obj->header.type == GRN_TYPE;
1037 }
1038
1039 bool
1040 grn_obj_is_text_family_type(grn_ctx *ctx, grn_obj *obj)
1041 {
1042 if (!grn_obj_is_type(ctx, obj)) {
1043 return false;
1044 }
1045
1046 return grn_type_id_is_text_family(ctx, grn_obj_id(ctx, obj));
1047 }
1048
1049 bool
1050 grn_obj_is_binary_family_type(grn_ctx *ctx, grn_obj *obj)
1051 {
1052 if (!grn_obj_is_type(ctx, obj)) {
1053 return false;
1054 }
1055
1056 return grn_type_id_is_binary_family(ctx, grn_obj_id(ctx, obj));
1057 }
1058
1059 bool
1060 grn_obj_is_proc(grn_ctx *ctx, grn_obj *obj)
1061 {
1062 if (!obj) {
1063 return false;
1064 }
1065
1066 return obj->header.type == GRN_PROC;
1067 }
1068
1069 bool
1070 grn_obj_is_tokenizer_proc(grn_ctx *ctx, grn_obj *obj)
1071 {
1072 grn_proc *proc;
1073
1074 if (!grn_obj_is_proc(ctx, obj)) {
1075 return false;
1076 }
1077
1078 proc = (grn_proc *)obj;
1079 return proc->type == GRN_PROC_TOKENIZER;
1080 }
1081
1082 bool
1083 grn_obj_is_command_proc(grn_ctx *ctx, grn_obj *obj)
1084 {
1085 if (!grn_obj_is_proc(ctx, obj)) {
1086 return false;
1087 }
1088
1089 grn_proc *proc = (grn_proc *)obj;
1090 return proc->type == GRN_PROC_COMMAND;
1091 }
1092
1093 bool
1094 grn_obj_is_function_proc(grn_ctx *ctx, grn_obj *obj)
1095 {
1096 grn_proc *proc;
1097
1098 if (!grn_obj_is_proc(ctx, obj)) {
1099 return false;
1100 }
1101
1102 proc = (grn_proc *)obj;
1103 return proc->type == GRN_PROC_FUNCTION;
1104 }
1105
1106 bool
1107 grn_obj_is_selector_proc(grn_ctx *ctx, grn_obj *obj)
1108 {
1109 grn_proc *proc;
1110
1111 if (!grn_obj_is_function_proc(ctx, obj)) {
1112 return false;
1113 }
1114
1115 proc = (grn_proc *)obj;
1116 return proc->callbacks.function.selector != NULL;
1117 }
1118
1119 bool
1120 grn_obj_is_selector_only_proc(grn_ctx *ctx, grn_obj *obj)
1121 {
1122 grn_proc *proc;
1123
1124 if (!grn_obj_is_selector_proc(ctx, obj)) {
1125 return false;
1126 }
1127
1128 proc = (grn_proc *)obj;
1129 return proc->funcs[PROC_INIT] == NULL;
1130 }
1131
1132 bool
1133 grn_obj_is_applier_proc(grn_ctx *ctx, grn_obj *obj)
1134 {
1135 if (!grn_obj_is_function_proc(ctx, obj)) {
1136 return false;
1137 }
1138
1139 grn_proc *proc = (grn_proc *)obj;
1140 return proc->callbacks.function.applier != NULL;
1141 }
1142
1143 bool
1144 grn_obj_is_sorter_proc(grn_ctx *ctx, grn_obj *obj)
1145 {
1146 if (!grn_obj_is_function_proc(ctx, obj)) {
1147 return false;
1148 }
1149
1150 grn_proc *proc = (grn_proc *)obj;
1151 return proc->callbacks.function.sorter != NULL;
1152 }
1153
1154 bool
1155 grn_obj_is_normalizer_proc(grn_ctx *ctx, grn_obj *obj)
1156 {
1157 grn_proc *proc;
1158
1159 if (!grn_obj_is_proc(ctx, obj)) {
1160 return false;
1161 }
1162
1163 proc = (grn_proc *)obj;
1164 return proc->type == GRN_PROC_NORMALIZER;
1165 }
1166
1167 bool
1168 grn_obj_is_token_filter_proc(grn_ctx *ctx, grn_obj *obj)
1169 {
1170 grn_proc *proc;
1171
1172 if (!grn_obj_is_proc(ctx, obj)) {
1173 return false;
1174 }
1175
1176 proc = (grn_proc *)obj;
1177 return proc->type == GRN_PROC_TOKEN_FILTER;
1178 }
1179
1180 bool
1181 grn_obj_is_extractor_proc(grn_ctx *ctx, grn_obj *obj)
1182 {
1183 grn_proc *proc;
1184
1185 if (!grn_obj_is_proc(ctx, obj)) {
1186 return false;
1187 }
1188
1189 proc = (grn_proc *)obj;
1190 return proc->type == GRN_PROC_EXTRACTOR;
1191 }
1192
1193 bool
1194 grn_obj_is_scorer_proc(grn_ctx *ctx, grn_obj *obj)
1195 {
1196 grn_proc *proc;
1197
1198 if (!grn_obj_is_proc(ctx, obj)) {
1199 return false;
1200 }
1201
1202 proc = (grn_proc *)obj;
1203 return proc->type == GRN_PROC_SCORER;
1204 }
1205
1206 bool
1207 grn_obj_is_window_function_proc(grn_ctx *ctx, grn_obj *obj)
1208 {
1209 grn_proc *proc;
1210
1211 if (!grn_obj_is_proc(ctx, obj)) {
1212 return false;
1213 }
1214
1215 proc = (grn_proc *)obj;
1216 return proc->type == GRN_PROC_WINDOW_FUNCTION;
1217 }
1218
1219 bool
1220 grn_obj_is_aggregator_proc(grn_ctx *ctx, grn_obj *obj)
1221 {
1222 grn_proc *proc;
1223
1224 if (!grn_obj_is_proc(ctx, obj)) {
1225 return false;
1226 }
1227
1228 proc = (grn_proc *)obj;
1229 return proc->type == GRN_PROC_AGGREGATOR;
1230 }
1231
1232 bool
1233 grn_obj_is_expr(grn_ctx *ctx, grn_obj *obj)
1234 {
1235 if (!obj) {
1236 return false;
1237 }
1238
1239 return obj->header.type == GRN_EXPR;
1240 }
1241
1242 bool
1243 grn_obj_is_visible(grn_ctx *ctx, grn_obj *obj)
1244 {
1245 if (!obj) {
1246 return false;
1247 }
1248
1249 uint32_t flags = 0;
1250 switch (obj->header.type) {
1251 case GRN_TABLE_HASH_KEY:
1252 flags = ((grn_hash *)obj)->header.common->flags;
1253 break;
1254 case GRN_TABLE_PAT_KEY:
1255 flags = ((grn_pat *)obj)->header->flags;
1256 break;
1257 case GRN_TABLE_DAT_KEY:
1258 flags = ((grn_dat *)obj)->header->flags;
1259 break;
1260 case GRN_TABLE_NO_KEY:
1261 flags = grn_array_get_flags(ctx, ((grn_array *)obj));
1262 break;
1263 case GRN_COLUMN_FIX_SIZE:
1264 flags = ((grn_ra *)obj)->header->flags;
1265 break;
1266 case GRN_COLUMN_VAR_SIZE:
1267 flags = grn_ja_get_flags(ctx, (grn_ja *)obj);
1268 break;
1269 case GRN_COLUMN_INDEX:
1270 flags = ((grn_ii *)obj)->header.common->flags;
1271 break;
1272 default:
1273 return false;
1274 break;
1275 }
1276
1277 return !(flags & GRN_OBJ_INVISIBLE);
1278 }
1279
1280 grn_rc
1281 grn_obj_set_visibility(grn_ctx *ctx, grn_obj *obj, bool is_visible)
1282 {
1283 GRN_API_ENTER;
1284
1285 if (!obj) {
1286 ERR(GRN_INVALID_ARGUMENT, "[obj][set-visibility] must not be NULL");
1287 GRN_API_RETURN(ctx->rc);
1288 }
1289
1290 if (!(grn_obj_is_index_column(ctx, obj) ||
1291 grn_obj_is_token_column(ctx, obj) ||
1292 grn_obj_is_generated_column(ctx, obj))) {
1293 grn_obj inspected;
1294 GRN_TEXT_INIT(&inspected, 0);
1295 grn_inspect_limited(ctx, &inspected, obj);
1296 ERR(GRN_INVALID_ARGUMENT,
1297 "[obj][set-visibility] "
1298 "must be an index column, a token column or a generated column: <%.*s>",
1299 (int)(GRN_TEXT_LEN(&inspected)),
1300 GRN_TEXT_VALUE(&inspected));
1301 GRN_OBJ_FIN(ctx, &inspected);
1302 GRN_API_RETURN(ctx->rc);
1303 }
1304
1305 if (grn_obj_is_index_column(ctx, obj)) {
1306 grn_ii_set_visibility(ctx, (grn_ii *)obj, is_visible);
1307 } else if (grn_obj_is_token_column(ctx, obj)) {
1308 grn_ja_set_visibility(ctx, (grn_ja *)obj, is_visible);
1309 }
1310
1311 GRN_API_RETURN(GRN_SUCCESS);
1312 }
1313
1314 bool
1315 grn_obj_have_source(grn_ctx *ctx, grn_obj *obj)
1316 {
1317 if (!obj) {
1318 return false;
1319 }
1320
1321 if (!GRN_DB_OBJP(obj)) {
1322 return false;
1323 }
1324
1325 return DB_OBJ(obj)->source_size > 0;
1326 }
1327
1328 bool
1329 grn_obj_is_token_column(grn_ctx *ctx, grn_obj *obj)
1330 {
1331 return grn_obj_is_vector_column(ctx, obj) && grn_obj_have_source(ctx, obj) &&
1332 ((grn_ja *)obj)->generator.length == 0;
1333 }
1334
1335 bool
1336 grn_obj_is_generated_column(grn_ctx *ctx, grn_obj *obj)
1337 {
1338 if (!obj) {
1339 return false;
1340 }
1341
1342 grn_raw_string *generator = NULL;
1343 switch (obj->header.type) {
1344 case GRN_COLUMN_FIX_SIZE:
1345 generator = &(((grn_ra *)obj)->generator);
1346 break;
1347 case GRN_COLUMN_VAR_SIZE:
1348 generator = &(((grn_ja *)obj)->generator);
1349 break;
1350 default:
1351 return false;
1352 }
1353 if (!grn_obj_have_source(ctx, obj)) {
1354 return false;
1355 }
1356
1357 return generator->length > 0;
1358 }
1359
1360 static void
1361 grn_db_reindex(grn_ctx *ctx, grn_obj *db)
1362 {
1363 grn_table_cursor *cursor;
1364 grn_id id;
1365
1366 cursor =
1367 grn_table_cursor_open(ctx, db, NULL, 0, NULL, 0, 0, -1, GRN_CURSOR_BY_ID);
1368 if (!cursor) {
1369 return;
1370 }
1371
1372 while ((id = grn_table_cursor_next(ctx, cursor)) != GRN_ID_NIL) {
1373 grn_obj *object;
1374
1375 object = grn_ctx_at(ctx, id);
1376 if (!object) {
1377 ERRCLR(ctx);
1378 continue;
1379 }
1380
1381 switch (object->header.type) {
1382 case GRN_TABLE_HASH_KEY:
1383 case GRN_TABLE_PAT_KEY:
1384 case GRN_TABLE_DAT_KEY:
1385 grn_obj_reindex(ctx, object);
1386 break;
1387 default:
1388 break;
1389 }
1390
1391 grn_obj_unlink(ctx, object);
1392
1393 if (ctx->rc != GRN_SUCCESS) {
1394 break;
1395 }
1396 }
1397 grn_table_cursor_close(ctx, cursor);
1398 }
1399
1400 static void
1401 grn_table_reindex(grn_ctx *ctx, grn_obj *table)
1402 {
1403 grn_hash *columns;
1404
1405 columns = grn_hash_create(ctx,
1406 NULL,
1407 sizeof(grn_id),
1408 0,
1409 GRN_OBJ_TABLE_HASH_KEY | GRN_HASH_TINY);
1410 if (!columns) {
1411 ERR(GRN_NO_MEMORY_AVAILABLE,
1412 "[table][reindex] failed to create a table to store columns");
1413 return;
1414 }
1415
1416 if (grn_table_columns(ctx, table, "", 0, (grn_obj *)columns) > 0) {
1417 grn_id *key;
1418 GRN_HASH_EACH(ctx, columns, id, &key, NULL, NULL, {
1419 grn_obj *column = grn_ctx_at(ctx, *key);
1420 if (column && column->header.type == GRN_COLUMN_INDEX) {
1421 grn_obj_reindex(ctx, column);
1422 }
1423 });
1424 }
1425 grn_hash_close(ctx, columns);
1426 }
1427
1428 static void
1429 grn_data_column_reindex(grn_ctx *ctx, grn_obj *data_column)
1430 {
1431 grn_hook *hooks;
1432
1433 if (grn_obj_is_token_column(ctx, data_column)) {
1434 grn_token_column_build(ctx, data_column);
1435 }
1436
1437 for (hooks = DB_OBJ(data_column)->hooks[GRN_HOOK_SET]; hooks;
1438 hooks = hooks->next) {
1439 grn_obj_default_set_value_hook_data *data = (void *)GRN_NEXT_ADDR(hooks);
1440 grn_obj *target = grn_ctx_at(ctx, data->target);
1441 if (target->header.type != GRN_COLUMN_INDEX) {
1442 continue;
1443 }
1444 grn_obj_reindex(ctx, target);
1445 if (ctx->rc != GRN_SUCCESS) {
1446 break;
1447 }
1448 }
1449 }
1450
1451 grn_rc
1452 grn_obj_reindex(grn_ctx *ctx, grn_obj *obj)
1453 {
1454 GRN_API_ENTER;
1455
1456 if (!obj) {
1457 ERR(GRN_INVALID_ARGUMENT, "[object][reindex] object must not be NULL");
1458 GRN_API_RETURN(ctx->rc);
1459 }
1460
1461 switch (obj->header.type) {
1462 case GRN_DB:
1463 grn_db_reindex(ctx, obj);
1464 break;
1465 case GRN_TABLE_HASH_KEY:
1466 case GRN_TABLE_PAT_KEY:
1467 case GRN_TABLE_DAT_KEY:
1468 grn_table_reindex(ctx, obj);
1469 break;
1470 case GRN_COLUMN_FIX_SIZE:
1471 case GRN_COLUMN_VAR_SIZE:
1472 grn_data_column_reindex(ctx, obj);
1473 break;
1474 case GRN_COLUMN_INDEX:
1475 grn_index_column_rebuild(ctx, obj);
1476 break;
1477 default:
1478 {
1479 grn_obj type_name;
1480 GRN_TEXT_INIT(&type_name, 0);
1481 grn_inspect_type(ctx, &type_name, obj->header.type);
1482 ERR(GRN_INVALID_ARGUMENT,
1483 "[object][reindex] object must be TABLE_HASH_KEY, "
1484 "TABLE_PAT_KEY, TABLE_DAT_KEY or COLUMN_INDEX: <%.*s>",
1485 (int)GRN_TEXT_LEN(&type_name),
1486 GRN_TEXT_VALUE(&type_name));
1487 GRN_OBJ_FIN(ctx, &type_name);
1488 GRN_API_RETURN(ctx->rc);
1489 }
1490 break;
1491 }
1492
1493 GRN_API_RETURN(ctx->rc);
1494 }
1495
1496 const char *
1497 grn_obj_type_to_string(uint8_t type)
1498 {
1499 switch (type) {
1500 case GRN_VOID:
1501 return "void";
1502 case GRN_BULK:
1503 return "bulk";
1504 case GRN_PTR:
1505 return "ptr";
1506 case GRN_UVECTOR:
1507 return "uvector";
1508 case GRN_PVECTOR:
1509 return "pvector";
1510 case GRN_VECTOR:
1511 return "vector";
1512 case GRN_MSG:
1513 return "msg";
1514 case GRN_QUERY:
1515 return "query";
1516 case GRN_ACCESSOR:
1517 return "accessor";
1518 case GRN_SNIP:
1519 return "snip";
1520 case GRN_PATSNIP:
1521 return "patsnip";
1522 case GRN_STRING:
1523 return "string";
1524 case GRN_CURSOR_TABLE_HASH_KEY:
1525 return "cursor:table:hash_key";
1526 case GRN_CURSOR_TABLE_PAT_KEY:
1527 return "cursor:table:pat_key";
1528 case GRN_CURSOR_TABLE_DAT_KEY:
1529 return "cursor:table:dat_key";
1530 case GRN_CURSOR_TABLE_NO_KEY:
1531 return "cursor:table:no_key";
1532 case GRN_CURSOR_COLUMN_INDEX:
1533 return "cursor:column:index";
1534 case GRN_CURSOR_COLUMN_GEO_INDEX:
1535 return "cursor:column:geo_index";
1536 case GRN_CURSOR_CONFIG:
1537 return "cursor:config";
1538 case GRN_TYPE:
1539 return "type";
1540 case GRN_PROC:
1541 return "proc";
1542 case GRN_EXPR:
1543 return "expr";
1544 case GRN_TABLE_HASH_KEY:
1545 return "table:hash_key";
1546 case GRN_TABLE_PAT_KEY:
1547 return "table:pat_key";
1548 case GRN_TABLE_DAT_KEY:
1549 return "table:dat_key";
1550 case GRN_TABLE_NO_KEY:
1551 return "table:no_key";
1552 case GRN_DB:
1553 return "db";
1554 case GRN_COLUMN_FIX_SIZE:
1555 return "column:fix_size";
1556 case GRN_COLUMN_VAR_SIZE:
1557 return "column:var_size";
1558 case GRN_COLUMN_INDEX:
1559 return "column:index";
1560 default:
1561 return "unknown";
1562 }
1563 }
1564
1565 bool
1566 grn_obj_type_is_table(uint8_t type)
1567 {
1568 switch (type) {
1569 case GRN_TABLE_HASH_KEY:
1570 case GRN_TABLE_PAT_KEY:
1571 case GRN_TABLE_DAT_KEY:
1572 case GRN_TABLE_NO_KEY:
1573 return true;
1574 default:
1575 return false;
1576 }
1577 }
1578
1579 bool
1580 grn_obj_type_is_column(uint8_t type)
1581 {
1582 switch (type) {
1583 case GRN_COLUMN_FIX_SIZE:
1584 case GRN_COLUMN_VAR_SIZE:
1585 case GRN_COLUMN_INDEX:
1586 return true;
1587 default:
1588 return false;
1589 }
1590 }
1591
1592 bool
1593 grn_obj_name_is_column(grn_ctx *ctx, const char *name, int name_len)
1594 {
1595 if (!name) {
1596 return false;
1597 }
1598
1599 if (name_len < 0) {
1600 name_len = (int)strlen(name);
1601 }
1602
1603 return memchr(name, GRN_DB_DELIMITER, (size_t)name_len) != NULL;
1604 }
1605
1606 bool
1607 grn_obj_is_empty(grn_ctx *ctx, grn_obj *obj, grn_id id)
1608 {
1609 if (obj->header.type == GRN_COLUMN_VAR_SIZE) {
1610 return grn_ja_is_empty(ctx, (grn_ja *)obj, id);
1611 }
1612 ERR(GRN_FUNCTION_NOT_IMPLEMENTED,
1613 "[obj][is-empty] unsupported type: %s",
1614 grn_obj_type_to_string(obj->header.type));
1615 return false;
1616 }
1617
1618 grn_io *
1619 grn_obj_get_io(grn_ctx *ctx, grn_obj *obj)
1620 {
1621 grn_io *io = NULL;
1622
1623 if (!obj) {
1624 return NULL;
1625 }
1626
1627 if (obj->header.type == GRN_DB) {
1628 obj = ((grn_db *)obj)->keys;
1629 }
1630
1631 switch (obj->header.type) {
1632 case GRN_TABLE_PAT_KEY:
1633 io = ((grn_pat *)obj)->io;
1634 break;
1635 case GRN_TABLE_DAT_KEY:
1636 io = ((grn_dat *)obj)->io;
1637 break;
1638 case GRN_TABLE_HASH_KEY:
1639 io = ((grn_hash *)obj)->io;
1640 break;
1641 case GRN_TABLE_NO_KEY:
1642 io = ((grn_array *)obj)->io;
1643 break;
1644 case GRN_COLUMN_VAR_SIZE:
1645 io = ((grn_ja *)obj)->io;
1646 break;
1647 case GRN_COLUMN_FIX_SIZE:
1648 io = ((grn_ra *)obj)->io;
1649 break;
1650 case GRN_COLUMN_INDEX:
1651 io = ((grn_ii *)obj)->seg;
1652 break;
1653 }
1654
1655 return io;
1656 }
1657
1658 size_t
1659 grn_obj_get_disk_usage(grn_ctx *ctx, grn_obj *obj)
1660 {
1661 size_t usage = 0;
1662
1663 GRN_API_ENTER;
1664
1665 if (!obj) {
1666 ERR(GRN_INVALID_ARGUMENT, "[object][disk-usage] object must not be NULL");
1667 GRN_API_RETURN(0);
1668 }
1669
1670 switch (obj->header.type) {
1671 case GRN_DB:
1672 {
1673 grn_db *db = (grn_db *)obj;
1674 usage = grn_obj_get_disk_usage(ctx, db->keys);
1675 if (db->specs) {
1676 usage += grn_obj_get_disk_usage(ctx, (grn_obj *)(db->specs));
1677 }
1678 usage += grn_obj_get_disk_usage(ctx, (grn_obj *)(db->config));
1679 }
1680 break;
1681 case GRN_TABLE_DAT_KEY:
1682 usage = grn_dat_get_disk_usage(ctx, (grn_dat *)obj);
1683 break;
1684 case GRN_COLUMN_INDEX:
1685 usage = grn_ii_get_disk_usage(ctx, (grn_ii *)obj);
1686 break;
1687 default:
1688 {
1689 grn_io *io;
1690 io = grn_obj_get_io(ctx, obj);
1691 if (io) {
1692 usage = grn_io_get_disk_usage(ctx, io);
1693 }
1694 }
1695 break;
1696 }
1697
1698 GRN_API_RETURN(usage);
1699 }
1700
1701 grn_rc
1702 grn_obj_set_option_values(grn_ctx *ctx,
1703 grn_obj *obj,
1704 const char *name,
1705 int name_length,
1706 grn_obj *values)
1707 {
1708 grn_id id;
1709
1710 GRN_API_ENTER;
1711
1712 id = grn_obj_id(ctx, obj);
1713 if (id & GRN_OBJ_TMP_OBJECT) {
1714 grn_options_set(ctx,
1715 ctx->impl->temporary_options,
1716 id & ~GRN_OBJ_TMP_OBJECT,
1717 name,
1718 name_length,
1719 values);
1720 } else {
1721 grn_db_set_option_values(ctx,
1722 grn_ctx_db(ctx),
1723 id,
1724 name,
1725 name_length,
1726 values);
1727 }
1728
1729 GRN_API_RETURN(ctx->rc);
1730 }
1731
1732 grn_option_revision
1733 grn_obj_get_option_values(grn_ctx *ctx,
1734 grn_obj *obj,
1735 const char *name,
1736 int name_length,
1737 grn_option_revision revision,
1738 grn_obj *values)
1739 {
1740 grn_id id;
1741 grn_option_revision returned_revision;
1742
1743 GRN_API_ENTER;
1744
1745 id = grn_obj_id(ctx, obj);
1746 if (id & GRN_OBJ_TMP_OBJECT) {
1747 returned_revision = grn_options_get(ctx,
1748 ctx->impl->temporary_options,
1749 id & ~GRN_OBJ_TMP_OBJECT,
1750 name,
1751 name_length,
1752 revision,
1753 values);
1754 } else {
1755 returned_revision = grn_db_get_option_values(ctx,
1756 grn_ctx_db(ctx),
1757 id,
1758 name,
1759 name_length,
1760 revision,
1761 values);
1762 }
1763
1764 GRN_API_RETURN(returned_revision);
1765 }
1766
1767 grn_rc
1768 grn_obj_clear_option_values(grn_ctx *ctx, grn_obj *obj)
1769 {
1770 grn_id id;
1771 grn_rc rc;
1772
1773 GRN_API_ENTER;
1774
1775 id = grn_obj_id(ctx, obj);
1776 if (id & GRN_OBJ_TMP_OBJECT) {
1777 rc = grn_options_clear(ctx,
1778 ctx->impl->temporary_options,
1779 id & ~GRN_OBJ_TMP_OBJECT);
1780 } else {
1781 rc = grn_db_clear_option_values(ctx, grn_ctx_db(ctx), id);
1782 }
1783
1784 GRN_API_RETURN(rc);
1785 }
1786
1787 grn_rc
1788 grn_obj_to_script_syntax(grn_ctx *ctx, grn_obj *obj, grn_obj *buffer)
1789 {
1790 GRN_API_ENTER;
1791 switch (obj->header.type) {
1792 case GRN_VOID:
1793 GRN_TEXT_PUTS(ctx, buffer, "null");
1794 break;
1795 case GRN_BULK:
1796 grn_inspect(ctx, buffer, obj);
1797 break;
1798 case GRN_PTR:
1799 if (GRN_BULK_VSIZE(obj) == 0) {
1800 GRN_TEXT_PUTS(ctx, buffer, "null");
1801 } else {
1802 grn_obj_to_script_syntax(ctx, GRN_PTR_VALUE(obj), buffer);
1803 }
1804 break;
1805 case GRN_UVECTOR:
1806 case GRN_PVECTOR:
1807 case GRN_VECTOR:
1808 grn_inspect(ctx, buffer, obj);
1809 break;
1810 case GRN_ACCESSOR:
1811 grn_accessor_to_script_syntax(ctx, obj, buffer);
1812 break;
1813 case GRN_TYPE:
1814 case GRN_PROC:
1815 {
1816 char name[GRN_TABLE_MAX_KEY_SIZE];
1817 int name_length = grn_obj_name(ctx, obj, name, sizeof(name));
1818 GRN_TEXT_PUT(ctx, buffer, name, name_length);
1819 }
1820 break;
1821 case GRN_EXPR:
1822 grn_expr_to_script_syntax(ctx, obj, buffer);
1823 break;
1824 case GRN_TABLE_HASH_KEY:
1825 case GRN_TABLE_PAT_KEY:
1826 case GRN_TABLE_DAT_KEY:
1827 case GRN_TABLE_NO_KEY:
1828 if (grn_obj_is_tiny_hash_table(ctx, obj)) {
1829 GRN_TEXT_PUTC(ctx, buffer, '{');
1830 uint32_t i = 0;
1831 grn_obj key;
1832 GRN_TEXT_INIT(&key, GRN_OBJ_DO_SHALLOW_COPY);
1833 GRN_TABLE_EACH_BEGIN_FLAGS(ctx, obj, cursor, id, GRN_CURSOR_BY_ID)
1834 {
1835 if (i > 0) {
1836 GRN_TEXT_PUTS(ctx, buffer, ", ");
1837 }
1838 void *raw_key;
1839 int raw_key_size = grn_table_cursor_get_key(ctx, cursor, &raw_key);
1840 GRN_TEXT_SET(ctx, &key, raw_key, raw_key_size);
1841 grn_obj_to_script_syntax(ctx, &key, buffer);
1842 GRN_TEXT_PUTS(ctx, buffer, ": ");
1843 void *raw_value;
1844 grn_table_cursor_get_value(ctx, cursor, &raw_value);
1845 grn_obj *value = raw_value;
1846 grn_obj_to_script_syntax(ctx, value, buffer);
1847 i++;
1848 }
1849 GRN_TABLE_EACH_END(ctx, cursor);
1850 GRN_OBJ_FIN(ctx, &key);
1851 GRN_TEXT_PUTC(ctx, buffer, '}');
1852 } else {
1853 char name[GRN_TABLE_MAX_KEY_SIZE];
1854 int name_length = grn_obj_name(ctx, obj, name, sizeof(name));
1855 GRN_TEXT_PUT(ctx, buffer, name, name_length);
1856 }
1857 break;
1858 case GRN_COLUMN_FIX_SIZE:
1859 case GRN_COLUMN_VAR_SIZE:
1860 case GRN_COLUMN_INDEX:
1861 grn_column_name_(ctx, obj, buffer);
1862 break;
1863 default:
1864 ERR(GRN_FUNCTION_NOT_IMPLEMENTED,
1865 "[obj][to-script-syntax] unsupported type: %s",
1866 grn_obj_type_to_string(obj->header.type));
1867 break;
1868 }
1869 GRN_API_RETURN(ctx->rc);
1870 }
1871
1872 static void
1873 grn_obj_warm_dispatch(grn_ctx *ctx, grn_obj *obj);
1874
1875 static void
1876 grn_db_warm(grn_ctx *ctx, grn_obj *obj)
1877 {
1878 grn_db *db = (grn_db *)obj;
1879 grn_obj_warm_dispatch(ctx, db->keys);
1880 if (grn_ja_warm(ctx, db->specs) != GRN_SUCCESS) {
1881 return;
1882 }
1883 if (grn_hash_warm(ctx, db->config) != GRN_SUCCESS) {
1884 return;
1885 }
1886 if (grn_options_warm(ctx, db->options) != GRN_SUCCESS) {
1887 return;
1888 }
1889
1890 GRN_TABLE_EACH_BEGIN_FLAGS(ctx, obj, cursor, id, GRN_CURSOR_BY_ID)
1891 {
1892 if (id < GRN_N_RESERVED_TYPES) {
1893 continue;
1894 }
1895
1896 grn_obj *object = grn_ctx_at(ctx, id);
1897 if (!object) {
1898 continue;
1899 }
1900 if (grn_obj_is_table(ctx, object)) {
1901 grn_obj_warm_dispatch(ctx, object);
1902 }
1903 grn_obj_unref(ctx, object);
1904 if (ctx->rc != GRN_SUCCESS) {
1905 break;
1906 }
1907 }
1908 GRN_TABLE_EACH_END(ctx, cursor);
1909 }
1910
1911 static void
1912 grn_table_warm(grn_ctx *ctx, grn_obj *obj)
1913 {
1914 grn_hash *columns = grn_table_all_columns(ctx, obj);
1915 if (!columns) {
1916 return;
1917 }
1918
1919 GRN_HASH_EACH_BEGIN(ctx, columns, cursor, id)
1920 {
1921 void *key;
1922 grn_hash_cursor_get_key(ctx, cursor, &key);
1923 grn_id column_id = *((grn_id *)key);
1924 grn_obj *column = grn_ctx_at(ctx, column_id);
1925 grn_obj_warm_dispatch(ctx, column);
1926 grn_obj_unref(ctx, column);
1927 if (ctx->rc != GRN_SUCCESS) {
1928 return;
1929 }
1930 }
1931 GRN_HASH_EACH_END(ctx, cursor);
1932 }
1933
1934 static void
1935 grn_obj_warm_dispatch(grn_ctx *ctx, grn_obj *obj)
1936 {
1937 switch (obj->header.type) {
1938 case GRN_DB:
1939 grn_db_warm(ctx, obj);
1940 break;
1941 case GRN_TABLE_HASH_KEY:
1942 if (grn_hash_warm(ctx, (grn_hash *)obj) != GRN_SUCCESS) {
1943 return;
1944 }
1945 grn_table_warm(ctx, obj);
1946 break;
1947 case GRN_TABLE_PAT_KEY:
1948 if (grn_pat_warm(ctx, (grn_pat *)obj) != GRN_SUCCESS) {
1949 return;
1950 }
1951 grn_table_warm(ctx, obj);
1952 break;
1953 case GRN_TABLE_DAT_KEY:
1954 if (grn_dat_warm(ctx, (grn_dat *)obj) != GRN_SUCCESS) {
1955 return;
1956 }
1957 grn_table_warm(ctx, obj);
1958 break;
1959 case GRN_TABLE_NO_KEY:
1960 if (grn_array_warm(ctx, (grn_array *)obj) != GRN_SUCCESS) {
1961 return;
1962 }
1963 grn_table_warm(ctx, obj);
1964 break;
1965 case GRN_COLUMN_FIX_SIZE:
1966 grn_ra_warm(ctx, (grn_ra *)obj);
1967 break;
1968 case GRN_COLUMN_VAR_SIZE:
1969 grn_ja_warm(ctx, (grn_ja *)obj);
1970 break;
1971 case GRN_COLUMN_INDEX:
1972 grn_ii_warm(ctx, (grn_ii *)obj);
1973 break;
1974 default:
1975 break;
1976 }
1977 }
1978
1979 grn_rc
1980 grn_obj_warm(grn_ctx *ctx, grn_obj *obj)
1981 {
1982 GRN_API_ENTER;
1983 grn_obj_warm_dispatch(ctx, obj);
1984 GRN_API_RETURN(ctx->rc);
1985 }
1986
1987 void
1988 grn_obj_set_error(grn_ctx *ctx,
1989 grn_obj *obj,
1990 grn_rc rc,
1991 grn_id id,
1992 const char *tag,
1993 const char *format,
1994 ...)
1995 {
1996 grn_obj message;
1997 GRN_TEXT_INIT(&message, 0);
1998 va_list args;
1999 va_start(args, format);
2000 grn_text_printfv(ctx, &message, format, args);
2001 va_end(args);
2002
2003 grn_io *io = grn_obj_get_io(ctx, obj);
2004 bool have_path = (io->path[0] != '\0');
2005 GRN_DEFINE_NAME(obj);
2006 if (id == GRN_ID_NIL) {
2007 ERR(rc,
2008 "%s[%.*s] %.*s%s%s%s",
2009 tag,
2010 name_size,
2011 name,
2012 (int)GRN_TEXT_LEN(&message),
2013 GRN_TEXT_VALUE(&message),
2014 have_path ? ": path:<" : "",
2015 have_path ? io->path : "",
2016 have_path ? ">" : "");
2017 } else {
2018 ERR(rc,
2019 "%s[%.*s][%u] %.*s%s%s%s",
2020 tag,
2021 name_size,
2022 name,
2023 id,
2024 (int)GRN_TEXT_LEN(&message),
2025 GRN_TEXT_VALUE(&message),
2026 have_path ? ": path:<" : "",
2027 have_path ? io->path : "",
2028 have_path ? ">" : "");
2029 }
2030
2031 GRN_OBJ_FIN(ctx, &message);
2032 }
2033
2034 void
2035 grn_obj_log(grn_ctx *ctx,
2036 grn_obj *obj,
2037 grn_log_level level,
2038 grn_id id,
2039 const char *tag,
2040 const char *format,
2041 ...)
2042 {
2043 if (!grn_logger_pass(ctx, level)) {
2044 return;
2045 }
2046
2047 grn_obj message;
2048 GRN_TEXT_INIT(&message, 0);
2049 va_list args;
2050 va_start(args, format);
2051 grn_text_printfv(ctx, &message, format, args);
2052 va_end(args);
2053
2054 grn_io *io = grn_obj_get_io(ctx, obj);
2055 bool have_path = (io->path[0] != '\0');
2056 GRN_DEFINE_NAME(obj);
2057 if (id == GRN_ID_NIL) {
2058 GRN_LOG(ctx,
2059 level,
2060 "%s[%.*s] %.*s%s%s%s",
2061 tag,
2062 name_size,
2063 name,
2064 (int)GRN_TEXT_LEN(&message),
2065 GRN_TEXT_VALUE(&message),
2066 have_path ? ": path:<" : "",
2067 have_path ? io->path : "",
2068 have_path ? ">" : "");
2069 } else {
2070 GRN_LOG(ctx,
2071 level,
2072 "%s[%.*s][%u] %.*s%s%s%s",
2073 tag,
2074 name_size,
2075 name,
2076 id,
2077 (int)GRN_TEXT_LEN(&message),
2078 GRN_TEXT_VALUE(&message),
2079 have_path ? ": path:<" : "",
2080 have_path ? io->path : "",
2081 have_path ? ">" : "");
2082 }
2083
2084 GRN_OBJ_FIN(ctx, &message);
2085 }