Debian Codegraph @

lib/proc/proc_schema.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_proc.h"
21
22 #include "../grn_db.h"
23
24 #include <groonga/plugin.h>
25
26 typedef struct {
27 bool is_close_opened_object_mode;
28 } grn_schema_data;
29
30 static void
31 command_schema_output_id(grn_ctx *ctx, grn_obj *obj)
32 {
33 if (obj) {
34 grn_id id;
35 id = grn_obj_id(ctx, obj);
36 grn_ctx_output_uint64(ctx, id);
37 } else {
38 grn_ctx_output_null(ctx);
39 }
40 }
41
42 static void
43 command_schema_output_name(grn_ctx *ctx, grn_obj *obj)
44 {
45 if (obj) {
46 char name[GRN_TABLE_MAX_KEY_SIZE];
47 int name_size;
48 name_size = grn_obj_name(ctx, obj, name, GRN_TABLE_MAX_KEY_SIZE);
49 grn_ctx_output_str(ctx, name, (size_t)name_size);
50 } else {
51 grn_ctx_output_null(ctx);
52 }
53 }
54
55 static void
56 command_schema_output_column_name(grn_ctx *ctx, grn_obj *column)
57 {
58 char name[GRN_TABLE_MAX_KEY_SIZE];
59 int name_size;
60 name_size = grn_column_name(ctx, column, name, GRN_TABLE_MAX_KEY_SIZE);
61 grn_ctx_output_str(ctx, name, (size_t)name_size);
62 }
63
64 static void
65 command_schema_output_type(grn_ctx *ctx, const char *type_label, grn_obj *type)
66 {
67 if (!type) {
68 grn_ctx_output_null(ctx);
69 return;
70 }
71
72 grn_ctx_output_map_open(ctx, type_label, 3);
73
74 grn_ctx_output_cstr(ctx, "id");
75 command_schema_output_id(ctx, type);
76
77 grn_ctx_output_cstr(ctx, "name");
78 command_schema_output_name(ctx, type);
79
80 grn_ctx_output_cstr(ctx, "type");
81 if (grn_obj_is_table(ctx, type)) {
82 grn_ctx_output_cstr(ctx, "reference");
83 } else {
84 grn_ctx_output_cstr(ctx, "type");
85 }
86 grn_ctx_output_map_close(ctx);
87 }
88
89 static void
90 command_schema_output_key_type(grn_ctx *ctx, grn_obj *key_type)
91 {
92 command_schema_output_type(ctx, "key_type", key_type);
93 }
94
95 static void
96 command_schema_output_value_type(grn_ctx *ctx, grn_obj *value_type)
97 {
98 command_schema_output_type(ctx, "value_type", value_type);
99 }
100
101 bool
102 grn_proc_text_include_special_character(grn_ctx *ctx,
103 const char *text,
104 size_t size)
105 {
106 const char *end = text + size;
107
108 for (; text < end; text++) {
109 switch (text[0]) {
110 case '(':
111 case ')':
112 case ' ':
113 case '"':
114 case '\'':
115 return true;
116 break;
117 default:
118 break;
119 }
120 }
121
122 return false;
123 }
124
125 static void
126 command_schema_output_command(grn_ctx *ctx,
127 const char *command_name,
128 grn_obj *arguments)
129 {
130 grn_ctx_output_map_open(ctx, "command", 3);
131
132 grn_ctx_output_cstr(ctx, "name");
133 grn_ctx_output_cstr(ctx, command_name);
134
135 grn_ctx_output_cstr(ctx, "arguments");
136 {
137 uint32_t i, n;
138
139 n = grn_vector_size(ctx, arguments);
140 grn_ctx_output_map_open(ctx, "arguments", (int)(n / 2));
141 for (i = 0; i < n; i += 2) {
142 const char *name;
143 unsigned int name_size;
144 const char *value;
145 unsigned int value_size;
146
147 name_size = grn_vector_get_element(ctx, arguments, i, &name, NULL, NULL);
148 value_size =
149 grn_vector_get_element(ctx, arguments, i + 1, &value, NULL, NULL);
150 grn_ctx_output_str(ctx, name, name_size);
151 grn_ctx_output_str(ctx, value, value_size);
152 }
153 grn_ctx_output_map_close(ctx);
154 }
155
156 grn_ctx_output_cstr(ctx, "command_line");
157 {
158 uint32_t i, n;
159 grn_obj command_line;
160
161 GRN_TEXT_INIT(&command_line, 0);
162 GRN_TEXT_PUTS(ctx, &command_line, command_name);
163 n = grn_vector_size(ctx, arguments);
164 for (i = 0; i < n; i += 2) {
165 const char *name;
166 unsigned int name_size;
167 const char *value;
168 unsigned int value_size;
169
170 name_size = grn_vector_get_element(ctx, arguments, i, &name, NULL, NULL);
171 grn_text_printf(ctx, &command_line, " --%.*s ", name_size, name);
172 value_size =
173 grn_vector_get_element(ctx, arguments, i + 1, &value, NULL, NULL);
174 if (grn_proc_text_include_special_character(ctx, value, value_size)) {
175 grn_obj value_text;
176 GRN_TEXT_INIT(&value_text, GRN_OBJ_DO_SHALLOW_COPY);
177 GRN_TEXT_SET(ctx, &value_text, value, value_size);
178 grn_text_otoj(ctx, &command_line, &value_text, NULL);
179 GRN_OBJ_FIN(ctx, &value_text);
180 } else {
181 GRN_TEXT_PUT(ctx, &command_line, value, value_size);
182 }
183 }
184 grn_ctx_output_str(ctx,
185 GRN_TEXT_VALUE(&command_line),
186 GRN_TEXT_LEN(&command_line));
187 GRN_OBJ_FIN(ctx, &command_line);
188 }
189
190 grn_ctx_output_map_close(ctx);
191 }
192
193 static void
194 command_schema_output_plugins(grn_ctx *ctx)
195 {
196 grn_obj plugin_names;
197 uint32_t i, n;
198
199 GRN_TEXT_INIT(&plugin_names, GRN_OBJ_VECTOR);
200
201 grn_plugin_get_names(ctx, &plugin_names);
202
203 grn_ctx_output_cstr(ctx, "plugins");
204
205 n = grn_vector_size(ctx, &plugin_names);
206 grn_ctx_output_map_open(ctx, "plugins", (int)n);
207 for (i = 0; i < n; i++) {
208 const char *name;
209 unsigned int name_size;
210
211 name_size =
212 grn_vector_get_element(ctx, &plugin_names, i, &name, NULL, NULL);
213 grn_ctx_output_str(ctx, name, name_size);
214
215 grn_ctx_output_map_open(ctx, "plugin", 1);
216 grn_ctx_output_cstr(ctx, "name");
217 grn_ctx_output_str(ctx, name, name_size);
218 grn_ctx_output_map_close(ctx);
219 }
220 grn_ctx_output_map_close(ctx);
221
222 GRN_OBJ_FIN(ctx, &plugin_names);
223 }
224
225 static void
226 command_schema_output_types(grn_ctx *ctx)
227 {
228 int n_types = 0;
229 GRN_DB_EACH_BEGIN_BY_KEY(ctx, cursor, id)
230 {
231 if (grn_id_is_builtin_type(ctx, id)) {
232 n_types++;
233 }
234 }
235 GRN_DB_EACH_END(ctx, cursor);
236
237 grn_ctx_output_cstr(ctx, "types");
238
239 grn_ctx_output_map_open(ctx, "types", n_types);
240 GRN_DB_EACH_BEGIN_BY_KEY(ctx, cursor, id)
241 {
242 grn_obj *type;
243
244 if (!grn_id_is_builtin_type(ctx, id)) {
245 continue;
246 }
247
248 type = grn_ctx_at(ctx, id);
249
250 command_schema_output_name(ctx, type);
251
252 grn_ctx_output_map_open(ctx, "type", 5);
253
254 grn_ctx_output_cstr(ctx, "id");
255 command_schema_output_id(ctx, type);
256
257 grn_ctx_output_cstr(ctx, "name");
258 command_schema_output_name(ctx, type);
259
260 grn_ctx_output_cstr(ctx, "size");
261 grn_ctx_output_int64(ctx, grn_type_size(ctx, type));
262
263 grn_ctx_output_cstr(ctx, "can_be_key_type");
264 grn_ctx_output_bool(ctx,
265 grn_type_size(ctx, type) <= GRN_TABLE_MAX_KEY_SIZE);
266
267 grn_ctx_output_cstr(ctx, "can_be_value_type");
268 grn_ctx_output_bool(ctx, !(type->header.flags & GRN_OBJ_KEY_VAR_SIZE));
269
270 grn_ctx_output_map_close(ctx);
271 }
272 GRN_DB_EACH_END(ctx, cursor);
273 grn_ctx_output_map_close(ctx);
274 }
275
276 static void
277 command_schema_output_tokenizers(grn_ctx *ctx, grn_schema_data *data)
278 {
279 grn_obj tokenizer_ids;
280
281 GRN_RECORD_INIT(&tokenizer_ids, GRN_OBJ_VECTOR, GRN_ID_NIL);
282 GRN_DB_EACH_BEGIN_BY_KEY(ctx, cursor, id)
283 {
284 void *name;
285 int name_size;
286 grn_obj *object;
287
288 name_size = grn_table_cursor_get_key(ctx, cursor, &name);
289 if (grn_obj_name_is_column(ctx, name, name_size)) {
290 continue;
291 }
292
293 if (data->is_close_opened_object_mode) {
294 grn_ctx_push_temporary_open_space(ctx);
295 }
296
297 object = grn_ctx_at(ctx, id);
298 if (object) {
299 if (grn_obj_is_tokenizer_proc(ctx, object)) {
300 GRN_RECORD_PUT(ctx, &tokenizer_ids, id);
301 }
302 } else {
303 /* XXX: this clause is executed when MeCab tokenizer is enabled in
304 database but the groonga isn't supported MeCab.
305 We should return error message about it and error exit status
306 but it's too difficult for this architecture. :< */
307 GRN_PLUGIN_CLEAR_ERROR(ctx);
308 }
309
310 if (data->is_close_opened_object_mode) {
311 grn_ctx_pop_temporary_open_space(ctx);
312 }
313 }
314 GRN_DB_EACH_END(ctx, cursor);
315
316 grn_ctx_output_cstr(ctx, "tokenizers");
317
318 size_t i, n;
319 n = GRN_BULK_VSIZE(&tokenizer_ids) / sizeof(grn_id);
320 grn_ctx_output_map_open(ctx, "tokenizers", (int)n);
321 for (i = 0; i < n; i++) {
322 grn_id tokenizer_id;
323 grn_obj *tokenizer;
324
325 tokenizer_id = GRN_RECORD_VALUE_AT(&tokenizer_ids, i);
326 tokenizer = grn_ctx_at(ctx, tokenizer_id);
327
328 command_schema_output_name(ctx, tokenizer);
329
330 grn_ctx_output_map_open(ctx, "tokenizer", 2);
331
332 grn_ctx_output_cstr(ctx, "id");
333 command_schema_output_id(ctx, tokenizer);
334
335 grn_ctx_output_cstr(ctx, "name");
336 command_schema_output_name(ctx, tokenizer);
337
338 grn_ctx_output_map_close(ctx);
339 }
340 grn_ctx_output_map_close(ctx);
341
342 GRN_OBJ_FIN(ctx, &tokenizer_ids);
343 }
344
345 static void
346 command_schema_output_normalizers(grn_ctx *ctx, grn_schema_data *data)
347 {
348 grn_obj normalizer_ids;
349
350 GRN_RECORD_INIT(&normalizer_ids, GRN_OBJ_VECTOR, GRN_ID_NIL);
351 GRN_DB_EACH_BEGIN_BY_KEY(ctx, cursor, id)
352 {
353 void *name;
354 int name_size;
355 grn_obj *object;
356
357 name_size = grn_table_cursor_get_key(ctx, cursor, &name);
358 if (grn_obj_name_is_column(ctx, name, name_size)) {
359 continue;
360 }
361
362 if (data->is_close_opened_object_mode) {
363 grn_ctx_push_temporary_open_space(ctx);
364 }
365
366 object = grn_ctx_at(ctx, id);
367 if (object) {
368 if (grn_obj_is_normalizer_proc(ctx, object)) {
369 GRN_RECORD_PUT(ctx, &normalizer_ids, id);
370 }
371 } else {
372 /* XXX: this clause is executed when MeCab normalizer is enabled in
373 database but the groonga isn't supported MeCab.
374 We should return error message about it and error exit status
375 but it's too difficult for this architecture. :< */
376 GRN_PLUGIN_CLEAR_ERROR(ctx);
377 }
378
379 if (data->is_close_opened_object_mode) {
380 grn_ctx_pop_temporary_open_space(ctx);
381 }
382 }
383 GRN_DB_EACH_END(ctx, cursor);
384
385 grn_ctx_output_cstr(ctx, "normalizers");
386
387 size_t i, n;
388 n = GRN_BULK_VSIZE(&normalizer_ids) / sizeof(grn_id);
389 grn_ctx_output_map_open(ctx, "normalizers", (int)n);
390 for (i = 0; i < n; i++) {
391 grn_id normalizer_id;
392 grn_obj *normalizer;
393
394 normalizer_id = GRN_RECORD_VALUE_AT(&normalizer_ids, i);
395 normalizer = grn_ctx_at(ctx, normalizer_id);
396
397 command_schema_output_name(ctx, normalizer);
398
399 grn_ctx_output_map_open(ctx, "normalizer", 2);
400
401 grn_ctx_output_cstr(ctx, "id");
402 command_schema_output_id(ctx, normalizer);
403
404 grn_ctx_output_cstr(ctx, "name");
405 command_schema_output_name(ctx, normalizer);
406
407 grn_ctx_output_map_close(ctx);
408 }
409 grn_ctx_output_map_close(ctx);
410
411 GRN_OBJ_FIN(ctx, &normalizer_ids);
412 }
413
414 static void
415 command_schema_output_token_filters(grn_ctx *ctx, grn_schema_data *data)
416 {
417 grn_obj token_filter_ids;
418
419 GRN_RECORD_INIT(&token_filter_ids, GRN_OBJ_VECTOR, GRN_ID_NIL);
420 GRN_DB_EACH_BEGIN_BY_KEY(ctx, cursor, id)
421 {
422 void *name;
423 int name_size;
424 grn_obj *object;
425
426 name_size = grn_table_cursor_get_key(ctx, cursor, &name);
427 if (grn_obj_name_is_column(ctx, name, name_size)) {
428 continue;
429 }
430
431 if (data->is_close_opened_object_mode) {
432 grn_ctx_push_temporary_open_space(ctx);
433 }
434
435 object = grn_ctx_at(ctx, id);
436 if (object) {
437 if (grn_obj_is_token_filter_proc(ctx, object)) {
438 GRN_RECORD_PUT(ctx, &token_filter_ids, id);
439 }
440 } else {
441 /* XXX: this clause is executed when MeCab normalizer is enabled in
442 database but the groonga isn't supported MeCab.
443 We should return error message about it and error exit status
444 but it's too difficult for this architecture. :< */
445 GRN_PLUGIN_CLEAR_ERROR(ctx);
446 }
447
448 if (data->is_close_opened_object_mode) {
449 grn_ctx_pop_temporary_open_space(ctx);
450 }
451 }
452 GRN_DB_EACH_END(ctx, cursor);
453
454 grn_ctx_output_cstr(ctx, "token_filters");
455
456 size_t i, n;
457 n = GRN_BULK_VSIZE(&token_filter_ids) / sizeof(grn_id);
458 grn_ctx_output_map_open(ctx, "token_filters", (int)n);
459 for (i = 0; i < n; i++) {
460 grn_id token_filter_id;
461 grn_obj *token_filter;
462
463 token_filter_id = GRN_RECORD_VALUE_AT(&token_filter_ids, i);
464 token_filter = grn_ctx_at(ctx, token_filter_id);
465
466 command_schema_output_name(ctx, token_filter);
467
468 grn_ctx_output_map_open(ctx, "token_filter", 2);
469
470 grn_ctx_output_cstr(ctx, "id");
471 command_schema_output_id(ctx, token_filter);
472
473 grn_ctx_output_cstr(ctx, "name");
474 command_schema_output_name(ctx, token_filter);
475
476 grn_ctx_output_map_close(ctx);
477 }
478 grn_ctx_output_map_close(ctx);
479
480 GRN_OBJ_FIN(ctx, &token_filter_ids);
481 }
482
483 static const char *
484 command_schema_table_type_name(grn_ctx *ctx, grn_obj *table)
485 {
486 const char *name = "unknown";
487
488 switch (table->header.type) {
489 case GRN_TABLE_NO_KEY:
490 name = "array";
491 break;
492 case GRN_TABLE_HASH_KEY:
493 name = "hash table";
494 break;
495 case GRN_TABLE_PAT_KEY:
496 name = "patricia trie";
497 break;
498 case GRN_TABLE_DAT_KEY:
499 name = "double array trie";
500 break;
501 default:
502 break;
503 }
504
505 return name;
506 }
507
508 static void
509 command_schema_table_output_key_type(grn_ctx *ctx, grn_obj *table)
510 {
511 grn_obj *key_type = NULL;
512
513 if (table->header.type != GRN_TABLE_NO_KEY &&
514 table->header.domain != GRN_ID_NIL) {
515 key_type = grn_ctx_at(ctx, table->header.domain);
516 }
517
518 command_schema_output_key_type(ctx, key_type);
519 }
520
521 static void
522 command_schema_table_output_value_type(grn_ctx *ctx, grn_obj *table)
523 {
524 grn_obj *value_type = NULL;
525 grn_id range = GRN_ID_NIL;
526
527 if (table->header.type != GRN_TABLE_DAT_KEY) {
528 range = grn_obj_get_range(ctx, table);
529 }
530 if (range != GRN_ID_NIL) {
531 value_type = grn_ctx_at(ctx, range);
532 }
533
534 command_schema_output_value_type(ctx, value_type);
535 }
536
537 static void
538 command_schema_table_output_options(grn_ctx *ctx, grn_obj *options)
539 {
540 if (options->header.type == GRN_VOID) {
541 grn_ctx_output_null(ctx);
542 } else {
543 uint32_t i;
544 uint32_t n;
545 grn_obj option;
546
547 n = grn_vector_size(ctx, options);
548 grn_ctx_output_array_open(ctx, "options", (int)n);
549 GRN_VOID_INIT(&option);
550 for (i = 0; i < n; i++) {
551 const char *value;
552 unsigned int length;
553 grn_id domain;
554
555 length = grn_vector_get_element(ctx, options, i, &value, NULL, &domain);
556 grn_obj_reinit(ctx, &option, domain, 0);
557 grn_bulk_write(ctx, &option, value, length);
558 grn_ctx_output_obj(ctx, &option, NULL);
559 }
560 GRN_OBJ_FIN(ctx, &option);
561 grn_ctx_output_array_close(ctx);
562 }
563 }
564
565 static void
566 command_schema_table_output_tokenizer(grn_ctx *ctx, grn_obj *table)
567 {
568 grn_obj *tokenizer;
569
570 tokenizer = grn_obj_get_info(ctx, table, GRN_INFO_DEFAULT_TOKENIZER, NULL);
571 if (!tokenizer) {
572 grn_ctx_output_null(ctx);
573 return;
574 }
575
576 grn_ctx_output_map_open(ctx, "tokenizer", 3);
577
578 grn_ctx_output_cstr(ctx, "id");
579 command_schema_output_id(ctx, tokenizer);
580
581 grn_ctx_output_cstr(ctx, "name");
582 command_schema_output_name(ctx, tokenizer);
583
584 grn_ctx_output_cstr(ctx, "options");
585 {
586 grn_obj options;
587
588 GRN_VOID_INIT(&options);
589 grn_table_get_default_tokenizer_options(ctx, table, &options);
590 command_schema_table_output_options(ctx, &options);
591 GRN_OBJ_FIN(ctx, &options);
592 }
593
594 grn_ctx_output_map_close(ctx);
595 }
596
597 static void
598 command_schema_table_output_normalizer(grn_ctx *ctx, grn_obj *table)
599 {
600 grn_obj *normalizer;
601
602 normalizer = grn_obj_get_info(ctx, table, GRN_INFO_NORMALIZER, NULL);
603 if (!normalizer) {
604 grn_ctx_output_null(ctx);
605 return;
606 }
607
608 grn_ctx_output_map_open(ctx, "normalizer", 3);
609
610 grn_ctx_output_cstr(ctx, "id");
611 command_schema_output_id(ctx, normalizer);
612
613 grn_ctx_output_cstr(ctx, "name");
614 command_schema_output_name(ctx, normalizer);
615
616 grn_ctx_output_cstr(ctx, "options");
617 {
618 grn_obj options;
619
620 GRN_VOID_INIT(&options);
621 grn_table_get_normalizer_options(ctx, table, &options);
622 command_schema_table_output_options(ctx, &options);
623 GRN_OBJ_FIN(ctx, &options);
624 }
625
626 grn_ctx_output_map_close(ctx);
627 }
628
629 static void
630 command_schema_table_output_table_modules(grn_ctx *ctx,
631 grn_obj *table,
632 grn_info_type type,
633 const char *singular,
634 const char *plural)
635 {
636 grn_obj modules;
637 GRN_PTR_INIT(&modules, GRN_OBJ_VECTOR, GRN_ID_NIL);
638 if (grn_obj_is_table_with_key(ctx, table)) {
639 grn_obj_get_info(ctx, table, type, &modules);
640 }
641
642 size_t n = GRN_PTR_VECTOR_SIZE(&modules);
643 grn_ctx_output_array_open(ctx, plural, (int)n);
644 size_t i;
645 for (i = 0; i < n; i++) {
646 grn_obj *module = GRN_PTR_VALUE_AT(&modules, i);
647
648 grn_ctx_output_map_open(ctx, singular, 3);
649 {
650 grn_ctx_output_cstr(ctx, "id");
651 command_schema_output_id(ctx, module);
652
653 grn_ctx_output_cstr(ctx, "name");
654 command_schema_output_name(ctx, module);
655
656 grn_ctx_output_cstr(ctx, "options");
657 {
658 grn_obj options;
659 GRN_VOID_INIT(&options);
660 switch (type) {
661 case GRN_INFO_NORMALIZERS:
662 grn_table_get_normalizers_options(ctx, table, (uint32_t)i, &options);
663 break;
664 case GRN_INFO_TOKEN_FILTERS:
665 grn_table_get_token_filters_options(ctx,
666 table,
667 (uint32_t)i,
668 &options);
669 break;
670 case GRN_INFO_EXTRACTORS:
671 grn_table_get_token_filters_options(ctx,
672 table,
673 (uint32_t)i,
674 &options);
675 break;
676 default:
677 break;
678 }
679 command_schema_table_output_options(ctx, &options);
680 GRN_OBJ_FIN(ctx, &options);
681 }
682 }
683 grn_ctx_output_map_close(ctx);
684 }
685 grn_ctx_output_array_close(ctx);
686
687 GRN_OBJ_FIN(ctx, &modules);
688 }
689
690 static void
691 command_schema_table_command_collect_arguments(grn_ctx *ctx,
692 grn_obj *table,
693 grn_obj *arguments)
694 {
695 #define ADD(name_, value_) \
696 grn_vector_add_element(ctx, \
697 arguments, \
698 name_, \
699 (uint32_t)strlen(name_), \
700 0, \
701 GRN_DB_TEXT); \
702 grn_vector_add_element(ctx, \
703 arguments, \
704 value_, \
705 (uint32_t)strlen(value_), \
706 0, \
707 GRN_DB_TEXT)
708
709 #define ADD_OBJECT_NAME(name_, object_) \
710 do { \
711 char object_name[GRN_TABLE_MAX_KEY_SIZE]; \
712 int object_name_size; \
713 object_name_size = \
714 grn_obj_name(ctx, object_, object_name, GRN_TABLE_MAX_KEY_SIZE); \
715 object_name[object_name_size] = '\0'; \
716 ADD(name_, object_name); \
717 } while (false)
718
719 ADD_OBJECT_NAME("name", table);
720
721 {
722 grn_obj flags;
723 grn_table_flags table_flags;
724 grn_table_flags ignored_flags = GRN_OBJ_KEY_NORMALIZE | GRN_OBJ_PERSISTENT;
725 GRN_TEXT_INIT(&flags, 0);
726 grn_table_get_info(ctx, table, &table_flags, NULL, NULL, NULL, NULL);
727 grn_dump_table_create_flags(ctx, table_flags & ~ignored_flags, &flags);
728 GRN_TEXT_PUTC(ctx, &flags, '\0');
729 ADD("flags", GRN_TEXT_VALUE(&flags));
730 GRN_OBJ_FIN(ctx, &flags);
731 }
732
733 {
734 grn_obj *key_type = NULL;
735
736 if (table->header.type != GRN_TABLE_NO_KEY &&
737 table->header.domain != GRN_ID_NIL) {
738 key_type = grn_ctx_at(ctx, table->header.domain);
739 }
740 if (key_type) {
741 ADD_OBJECT_NAME("key_type", key_type);
742 }
743 }
744
745 {
746 grn_obj *value_type = NULL;
747 grn_id range = GRN_ID_NIL;
748
749 if (table->header.type != GRN_TABLE_DAT_KEY) {
750 range = grn_obj_get_range(ctx, table);
751 }
752 if (range != GRN_ID_NIL) {
753 value_type = grn_ctx_at(ctx, range);
754 }
755 if (value_type) {
756 ADD_OBJECT_NAME("value_type", value_type);
757 }
758 }
759
760 {
761 grn_obj *tokenizer;
762 tokenizer = grn_obj_get_info(ctx, table, GRN_INFO_DEFAULT_TOKENIZER, NULL);
763 if (tokenizer) {
764 grn_obj sub_output;
765 GRN_TEXT_INIT(&sub_output, 0);
766 grn_table_get_default_tokenizer_string(ctx, table, &sub_output);
767 GRN_TEXT_PUTC(ctx, &sub_output, '\0');
768 ADD("default_tokenizer", GRN_TEXT_VALUE(&sub_output));
769 GRN_OBJ_FIN(ctx, &sub_output);
770 }
771 }
772
773 {
774 grn_obj *normalizer;
775 normalizer = grn_obj_get_info(ctx, table, GRN_INFO_NORMALIZER, NULL);
776 if (!normalizer && (table->header.flags & GRN_OBJ_KEY_NORMALIZE)) {
777 normalizer = grn_ctx_get(ctx, "NormalizerAuto", -1);
778 }
779 if (normalizer) {
780 grn_obj sub_output;
781 GRN_TEXT_INIT(&sub_output, 0);
782 grn_table_get_normalizers_string(ctx, table, &sub_output);
783 GRN_TEXT_PUTC(ctx, &sub_output, '\0');
784 ADD("normalizer", GRN_TEXT_VALUE(&sub_output));
785 GRN_OBJ_FIN(ctx, &sub_output);
786 }
787 }
788
789 if (table->header.type != GRN_TABLE_NO_KEY) {
790 grn_obj token_filters;
791 GRN_PTR_INIT(&token_filters, GRN_OBJ_VECTOR, GRN_ID_NIL);
792 grn_obj_get_info(ctx, table, GRN_INFO_TOKEN_FILTERS, &token_filters);
793 if (grn_vector_size(ctx, &token_filters) > 0) {
794 grn_obj sub_output;
795 GRN_TEXT_INIT(&sub_output, 0);
796 grn_table_get_token_filters_string(ctx, table, &sub_output);
797 GRN_TEXT_PUTC(ctx, &sub_output, '\0');
798 ADD("token_filters", GRN_TEXT_VALUE(&sub_output));
799 GRN_OBJ_FIN(ctx, &sub_output);
800 }
801 GRN_OBJ_FIN(ctx, &token_filters);
802
803 grn_obj extractors;
804 GRN_PTR_INIT(&extractors, GRN_OBJ_VECTOR, GRN_ID_NIL);
805 grn_obj_get_info(ctx, table, GRN_INFO_EXTRACTORS, &extractors);
806 if (grn_vector_size(ctx, &extractors) > 0) {
807 grn_obj sub_output;
808 GRN_TEXT_INIT(&sub_output, 0);
809 grn_table_get_extractors_string(ctx, table, &sub_output);
810 GRN_TEXT_PUTC(ctx, &sub_output, '\0');
811 ADD("extractors", GRN_TEXT_VALUE(&sub_output));
812 GRN_OBJ_FIN(ctx, &sub_output);
813 }
814 GRN_OBJ_FIN(ctx, &extractors);
815 }
816
817 #undef ADD_OBJECT_NAME
818 #undef ADD
819 }
820
821 static void
822 command_schema_table_output_command(grn_ctx *ctx, grn_obj *table)
823 {
824 grn_obj arguments;
825
826 GRN_TEXT_INIT(&arguments, GRN_OBJ_VECTOR);
827 command_schema_table_command_collect_arguments(ctx, table, &arguments);
828
829 command_schema_output_command(ctx, "table_create", &arguments);
830
831 GRN_OBJ_FIN(ctx, &arguments);
832 }
833
834 static void
835 command_schema_column_output_type(grn_ctx *ctx, grn_obj *column)
836 {
837 switch (column->header.type) {
838 case GRN_COLUMN_FIX_SIZE:
839 case GRN_COLUMN_VAR_SIZE:
840 switch (column->header.flags & GRN_OBJ_COLUMN_TYPE_MASK) {
841 case GRN_OBJ_COLUMN_SCALAR:
842 grn_ctx_output_cstr(ctx, "scalar");
843 break;
844 case GRN_OBJ_COLUMN_VECTOR:
845 grn_ctx_output_cstr(ctx, "vector");
846 break;
847 }
848 break;
849 case GRN_COLUMN_INDEX:
850 grn_ctx_output_cstr(ctx, "index");
851 break;
852 }
853 }
854
855 static void
856 command_schema_column_output_value_type(grn_ctx *ctx, grn_obj *column)
857 {
858 grn_obj *value_type;
859 value_type = grn_ctx_at(ctx, grn_obj_get_range(ctx, column));
860 command_schema_output_value_type(ctx, value_type);
861 }
862
863 static void
864 command_schema_column_output_compress(grn_ctx *ctx, grn_obj *column)
865 {
866 const char *compress = NULL;
867
868 if (column->header.type != GRN_COLUMN_INDEX) {
869 switch (column->header.flags & GRN_OBJ_COMPRESS_MASK) {
870 case GRN_OBJ_COMPRESS_ZLIB:
871 compress = "zlib";
872 break;
873 case GRN_OBJ_COMPRESS_LZ4:
874 compress = "lz4";
875 break;
876 case GRN_OBJ_COMPRESS_ZSTD:
877 compress = "zstd";
878 break;
879 case GRN_OBJ_COMPRESS_OPENZL:
880 compress = "openzl";
881 break;
882 default:
883 break;
884 }
885 }
886
887 if (compress) {
888 grn_ctx_output_cstr(ctx, compress);
889 } else {
890 grn_ctx_output_null(ctx);
891 }
892 }
893
894 static void
895 command_schema_column_output_missing(grn_ctx *ctx, grn_obj *column)
896 {
897 const char *missing = NULL;
898
899 if (column->header.type != GRN_COLUMN_INDEX) {
900 switch (grn_column_get_flags(ctx, column) & GRN_OBJ_MISSING_MASK) {
901 case GRN_OBJ_MISSING_ADD:
902 missing = "add";
903 break;
904 case GRN_OBJ_MISSING_IGNORE:
905 missing = "ignore";
906 break;
907 case GRN_OBJ_MISSING_NIL:
908 missing = "nil";
909 break;
910 default:
911 break;
912 }
913 }
914
915 if (missing) {
916 grn_ctx_output_cstr(ctx, missing);
917 } else {
918 grn_ctx_output_null(ctx);
919 }
920 }
921
922 static void
923 command_schema_column_output_invalid(grn_ctx *ctx, grn_obj *column)
924 {
925 const char *invalid = NULL;
926
927 if (column->header.type != GRN_COLUMN_INDEX) {
928 switch (grn_column_get_flags(ctx, column) & GRN_OBJ_INVALID_MASK) {
929 case GRN_OBJ_INVALID_ERROR:
930 invalid = "error";
931 break;
932 case GRN_OBJ_INVALID_WARN:
933 invalid = "warn";
934 break;
935 case GRN_OBJ_INVALID_IGNORE:
936 invalid = "ignore";
937 break;
938 default:
939 break;
940 }
941 }
942
943 if (invalid) {
944 grn_ctx_output_cstr(ctx, invalid);
945 } else {
946 grn_ctx_output_null(ctx);
947 }
948 }
949
950 static void
951 command_schema_column_output_sources(grn_ctx *ctx, grn_obj *column)
952 {
953 grn_obj *source_table;
954 grn_obj source_ids;
955 size_t i, n_ids;
956
957 source_table = grn_ctx_at(ctx, grn_obj_get_range(ctx, column));
958
959 GRN_RECORD_INIT(&source_ids, GRN_OBJ_VECTOR, GRN_ID_NIL);
960 grn_obj_get_info(ctx, column, GRN_INFO_SOURCE, &source_ids);
961 n_ids = GRN_BULK_VSIZE(&source_ids) / sizeof(grn_id);
962 grn_ctx_output_array_open(ctx, "sources", (int)n_ids);
963 for (i = 0; i < n_ids; i++) {
964 grn_id source_id;
965 grn_obj *source;
966
967 source_id = GRN_RECORD_VALUE_AT(&source_ids, i);
968 source = grn_ctx_at(ctx, source_id);
969
970 grn_ctx_output_map_open(ctx, "source", 4);
971
972 grn_ctx_output_cstr(ctx, "id");
973 if (grn_obj_is_table(ctx, source)) {
974 command_schema_output_id(ctx, NULL);
975 } else {
976 command_schema_output_id(ctx, source);
977 }
978
979 grn_ctx_output_cstr(ctx, "name");
980 if (grn_obj_is_table(ctx, source)) {
981 grn_ctx_output_cstr(ctx, "_key");
982 } else {
983 command_schema_output_column_name(ctx, source);
984 }
985
986 grn_ctx_output_cstr(ctx, "table");
987 command_schema_output_name(ctx, source_table);
988
989 grn_ctx_output_cstr(ctx, "full_name");
990 if (grn_obj_is_table(ctx, source)) {
991 char name[GRN_TABLE_MAX_KEY_SIZE];
992 int name_size;
993 name_size = grn_obj_name(ctx, source, name, GRN_TABLE_MAX_KEY_SIZE);
994 name[name_size] = '\0';
995 grn_strcat(name, GRN_TABLE_MAX_KEY_SIZE, "._key");
996 grn_ctx_output_cstr(ctx, name);
997 } else {
998 command_schema_output_name(ctx, source);
999 }
1000
1001 grn_ctx_output_map_close(ctx);
1002 }
1003 grn_ctx_output_array_close(ctx);
1004
1005 GRN_OBJ_FIN(ctx, &source_ids);
1006 }
1007
1008 static void
1009 command_schema_column_output_generator(grn_ctx *ctx, grn_obj *column)
1010 {
1011 grn_obj generator;
1012 GRN_TEXT_INIT(&generator, 0);
1013 if (grn_obj_is_generated_column(ctx, column)) {
1014 grn_obj_get_info(ctx, column, GRN_INFO_GENERATOR, &generator);
1015 }
1016 grn_ctx_output_str(ctx, GRN_TEXT_VALUE(&generator), GRN_TEXT_LEN(&generator));
1017 GRN_OBJ_FIN(ctx, &generator);
1018 }
1019
1020 static void
1021 command_schema_output_indexes(grn_ctx *ctx, grn_obj *object)
1022 {
1023 uint32_t i;
1024 grn_index_datum *index_data = NULL;
1025 uint32_t n_index_data = 0;
1026
1027 n_index_data = grn_column_get_all_index_data(ctx, object, NULL, 0);
1028 if (n_index_data > 0) {
1029 index_data = GRN_PLUGIN_MALLOC(ctx, sizeof(grn_index_datum) * n_index_data);
1030 if (!index_data) {
1031 GRN_PLUGIN_ERROR(ctx,
1032 GRN_NO_MEMORY_AVAILABLE,
1033 "[schema] failed to allocate memory for indexes");
1034 return;
1035 }
1036 grn_column_get_all_index_data(ctx, object, index_data, n_index_data);
1037 }
1038
1039 grn_ctx_output_array_open(ctx, "indexes", (int)n_index_data);
1040 for (i = 0; i < n_index_data; i++) {
1041 grn_obj *lexicon;
1042
1043 grn_ctx_output_map_open(ctx, "index", 5);
1044
1045 grn_ctx_output_cstr(ctx, "id");
1046 command_schema_output_id(ctx, index_data[i].index);
1047
1048 grn_ctx_output_cstr(ctx, "full_name");
1049 command_schema_output_name(ctx, index_data[i].index);
1050
1051 grn_ctx_output_cstr(ctx, "table");
1052 lexicon = grn_ctx_at(ctx, index_data[i].index->header.domain);
1053 command_schema_output_name(ctx, lexicon);
1054
1055 grn_ctx_output_cstr(ctx, "name");
1056 command_schema_output_column_name(ctx, index_data[i].index);
1057
1058 grn_ctx_output_cstr(ctx, "section");
1059 grn_ctx_output_uint64(ctx, index_data[i].section);
1060
1061 grn_ctx_output_map_close(ctx);
1062 }
1063 grn_ctx_output_array_close(ctx);
1064
1065 if (index_data) {
1066 GRN_PLUGIN_FREE(ctx, index_data);
1067 }
1068 }
1069
1070 static void
1071 command_schema_column_command_collect_arguments(grn_ctx *ctx,
1072 grn_obj *table,
1073 grn_obj *column,
1074 grn_obj *arguments)
1075 {
1076 #define ADD(name_, value_) \
1077 grn_vector_add_element(ctx, \
1078 arguments, \
1079 name_, \
1080 (uint32_t)strlen(name_), \
1081 0, \
1082 GRN_DB_TEXT); \
1083 grn_vector_add_element(ctx, \
1084 arguments, \
1085 value_, \
1086 (uint32_t)strlen(value_), \
1087 0, \
1088 GRN_DB_TEXT)
1089
1090 #define ADD_OBJECT_NAME(name_, object_) \
1091 do { \
1092 char object_name[GRN_TABLE_MAX_KEY_SIZE]; \
1093 int object_name_size; \
1094 object_name_size = \
1095 grn_obj_name(ctx, object_, object_name, GRN_TABLE_MAX_KEY_SIZE); \
1096 object_name[object_name_size] = '\0'; \
1097 ADD(name_, object_name); \
1098 } while (false)
1099
1100 ADD_OBJECT_NAME("table", table);
1101 {
1102 char column_name[GRN_TABLE_MAX_KEY_SIZE];
1103 int column_name_size;
1104 column_name_size =
1105 grn_column_name(ctx, column, column_name, GRN_TABLE_MAX_KEY_SIZE);
1106 column_name[column_name_size] = '\0';
1107 ADD("name", column_name);
1108 }
1109
1110 {
1111 grn_obj flags;
1112 grn_column_flags column_flags;
1113
1114 GRN_TEXT_INIT(&flags, 0);
1115 column_flags = grn_column_get_flags(ctx, column);
1116 column_flags &= ~((grn_column_flags)GRN_OBJ_PERSISTENT);
1117 grn_dump_column_create_flags(ctx, column_flags, &flags);
1118 GRN_TEXT_PUTC(ctx, &flags, '\0');
1119 ADD("flags", GRN_TEXT_VALUE(&flags));
1120 GRN_OBJ_FIN(ctx, &flags);
1121 }
1122
1123 {
1124 grn_obj *value_type;
1125
1126 value_type = grn_ctx_at(ctx, grn_obj_get_range(ctx, column));
1127 ADD_OBJECT_NAME("type", value_type);
1128 }
1129
1130 {
1131 grn_obj source_ids;
1132 size_t n_ids;
1133
1134 GRN_RECORD_INIT(&source_ids, GRN_OBJ_VECTOR, GRN_ID_NIL);
1135 grn_obj_get_info(ctx, column, GRN_INFO_SOURCE, &source_ids);
1136
1137 n_ids = GRN_BULK_VSIZE(&source_ids) / sizeof(grn_id);
1138 if (n_ids > 0) {
1139 grn_obj sources;
1140 size_t i;
1141
1142 GRN_TEXT_INIT(&sources, 0);
1143 for (i = 0; i < n_ids; i++) {
1144 grn_id source_id;
1145 grn_obj *source;
1146 char name[GRN_TABLE_MAX_KEY_SIZE];
1147 size_t name_size;
1148
1149 source_id = GRN_RECORD_VALUE_AT(&source_ids, i);
1150 source = grn_ctx_at(ctx, source_id);
1151
1152 if (grn_obj_is_table(ctx, source)) {
1153 grn_strcpy(name, GRN_TABLE_MAX_KEY_SIZE, "_key");
1154 name_size = strlen(name);
1155 } else {
1156 name_size =
1157 (size_t)grn_column_name(ctx, source, name, GRN_TABLE_MAX_KEY_SIZE);
1158 }
1159 if (i > 0) {
1160 GRN_TEXT_PUTC(ctx, &sources, ',');
1161 }
1162 GRN_TEXT_PUT(ctx, &sources, name, name_size);
1163 }
1164 GRN_TEXT_PUTC(ctx, &sources, '\0');
1165 ADD("source", GRN_TEXT_VALUE(&sources));
1166 GRN_OBJ_FIN(ctx, &sources);
1167 }
1168 GRN_OBJ_FIN(ctx, &source_ids);
1169 }
1170
1171 {
1172 grn_obj generator;
1173 GRN_TEXT_INIT(&generator, 0);
1174 if (grn_obj_is_generated_column(ctx, column)) {
1175 grn_obj_get_info(ctx, column, GRN_INFO_GENERATOR, &generator);
1176 }
1177 if (GRN_TEXT_LEN(&generator) > 0) {
1178 GRN_TEXT_PUTC(ctx, &generator, '\0');
1179 ADD("generator", GRN_TEXT_VALUE(&generator));
1180 }
1181 GRN_OBJ_FIN(ctx, &generator);
1182 }
1183
1184 #undef ADD_OBJECT_NAME
1185 #undef ADD
1186 }
1187
1188 static void
1189 command_schema_column_output_command(grn_ctx *ctx,
1190 grn_obj *table,
1191 grn_obj *column)
1192 {
1193 grn_obj arguments;
1194
1195 GRN_TEXT_INIT(&arguments, GRN_OBJ_VECTOR);
1196 command_schema_column_command_collect_arguments(ctx,
1197 table,
1198 column,
1199 &arguments);
1200
1201 command_schema_output_command(ctx, "column_create", &arguments);
1202
1203 GRN_OBJ_FIN(ctx, &arguments);
1204 }
1205
1206 static void
1207 command_schema_column_output(grn_ctx *ctx, grn_obj *table, grn_obj *column)
1208 {
1209 if (!column) {
1210 return;
1211 }
1212
1213 command_schema_output_column_name(ctx, column);
1214
1215 grn_ctx_output_map_open(ctx, "column", 15);
1216
1217 grn_ctx_output_cstr(ctx, "id");
1218 command_schema_output_id(ctx, column);
1219
1220 grn_ctx_output_cstr(ctx, "name");
1221 command_schema_output_column_name(ctx, column);
1222
1223 grn_ctx_output_cstr(ctx, "table");
1224 command_schema_output_name(ctx, table);
1225
1226 grn_ctx_output_cstr(ctx, "full_name");
1227 command_schema_output_name(ctx, column);
1228
1229 grn_ctx_output_cstr(ctx, "type");
1230 command_schema_column_output_type(ctx, column);
1231
1232 grn_ctx_output_cstr(ctx, "value_type");
1233 command_schema_column_output_value_type(ctx, column);
1234
1235 grn_ctx_output_cstr(ctx, "compress");
1236 command_schema_column_output_compress(ctx, column);
1237
1238 grn_ctx_output_cstr(ctx, "missing");
1239 command_schema_column_output_missing(ctx, column);
1240
1241 grn_ctx_output_cstr(ctx, "invalid");
1242 command_schema_column_output_invalid(ctx, column);
1243
1244 grn_column_flags flags = grn_column_get_flags(ctx, column);
1245 grn_ctx_output_cstr(ctx, "section");
1246 grn_ctx_output_bool(ctx, (flags & GRN_OBJ_WITH_SECTION) != 0);
1247
1248 grn_ctx_output_cstr(ctx, "weight");
1249 grn_ctx_output_bool(ctx, (flags & GRN_OBJ_WITH_WEIGHT) != 0);
1250
1251 grn_ctx_output_cstr(ctx, "weight_float32");
1252 grn_ctx_output_bool(ctx, (flags & GRN_OBJ_WEIGHT_FLOAT32) != 0);
1253
1254 grn_ctx_output_cstr(ctx, "weight_bfloat16");
1255 grn_ctx_output_bool(ctx, (flags & GRN_OBJ_WEIGHT_BFLOAT16) != 0);
1256
1257 grn_ctx_output_cstr(ctx, "position");
1258 grn_ctx_output_bool(ctx, (flags & GRN_OBJ_WITH_POSITION) != 0);
1259
1260 grn_ctx_output_cstr(ctx, "sources");
1261 command_schema_column_output_sources(ctx, column);
1262
1263 grn_ctx_output_cstr(ctx, "generator");
1264 command_schema_column_output_generator(ctx, column);
1265
1266 grn_ctx_output_cstr(ctx, "indexes");
1267 command_schema_output_indexes(ctx, column);
1268
1269 grn_ctx_output_cstr(ctx, "command");
1270 command_schema_column_output_command(ctx, table, column);
1271
1272 grn_ctx_output_map_close(ctx);
1273 }
1274
1275 static void
1276 command_schema_table_output_columns(grn_ctx *ctx,
1277 grn_obj *table,
1278 grn_schema_data *data)
1279 {
1280 grn_hash *columns;
1281
1282 columns = grn_hash_create(ctx,
1283 NULL,
1284 sizeof(grn_id),
1285 0,
1286 GRN_OBJ_TABLE_HASH_KEY | GRN_HASH_TINY);
1287 if (!columns) {
1288 grn_ctx_output_map_open(ctx, "columns", 0);
1289 grn_ctx_output_map_close(ctx);
1290 return;
1291 }
1292
1293 grn_table_columns(ctx, table, "", 0, (grn_obj *)columns);
1294 grn_ctx_output_map_open(ctx, "columns", (int)grn_hash_size(ctx, columns));
1295 {
1296 grn_id *key;
1297 GRN_HASH_EACH(ctx, columns, id, &key, NULL, NULL, {
1298 grn_obj *column;
1299
1300 if (data->is_close_opened_object_mode) {
1301 grn_ctx_push_temporary_open_space(ctx);
1302 }
1303
1304 column = grn_ctx_at(ctx, *key);
1305 command_schema_column_output(ctx, table, column);
1306
1307 if (data->is_close_opened_object_mode) {
1308 grn_ctx_pop_temporary_open_space(ctx);
1309 }
1310 });
1311 }
1312 grn_ctx_output_map_close(ctx);
1313 grn_hash_close(ctx, columns);
1314 }
1315
1316 static void
1317 command_schema_output_table(grn_ctx *ctx, grn_schema_data *data, grn_obj *table)
1318 {
1319 command_schema_output_name(ctx, table);
1320
1321 grn_ctx_output_map_open(ctx, "table", 13);
1322
1323 grn_ctx_output_cstr(ctx, "id");
1324 command_schema_output_id(ctx, table);
1325
1326 grn_ctx_output_cstr(ctx, "name");
1327 command_schema_output_name(ctx, table);
1328
1329 grn_ctx_output_cstr(ctx, "type");
1330 grn_ctx_output_cstr(ctx, command_schema_table_type_name(ctx, table));
1331
1332 grn_ctx_output_cstr(ctx, "key_type");
1333 command_schema_table_output_key_type(ctx, table);
1334
1335 grn_ctx_output_cstr(ctx, "value_type");
1336 command_schema_table_output_value_type(ctx, table);
1337
1338 grn_ctx_output_cstr(ctx, "tokenizer");
1339 command_schema_table_output_tokenizer(ctx, table);
1340
1341 grn_ctx_output_cstr(ctx, "normalizer");
1342 command_schema_table_output_normalizer(ctx, table);
1343
1344 grn_ctx_output_cstr(ctx, "normalizers");
1345 command_schema_table_output_table_modules(ctx,
1346 table,
1347 GRN_INFO_NORMALIZERS,
1348 "normalizer",
1349 "normalizers");
1350
1351 grn_ctx_output_cstr(ctx, "token_filters");
1352 command_schema_table_output_table_modules(ctx,
1353 table,
1354 GRN_INFO_TOKEN_FILTERS,
1355 "token_filter",
1356 "token_filters");
1357
1358 grn_ctx_output_cstr(ctx, "extractors");
1359 command_schema_table_output_table_modules(ctx,
1360 table,
1361 GRN_INFO_EXTRACTORS,
1362 "extractor",
1363 "extractors");
1364
1365 grn_ctx_output_cstr(ctx, "indexes");
1366 command_schema_output_indexes(ctx, table);
1367
1368 grn_ctx_output_cstr(ctx, "command");
1369 command_schema_table_output_command(ctx, table);
1370
1371 grn_ctx_output_cstr(ctx, "columns");
1372 command_schema_table_output_columns(ctx, table, data);
1373
1374 grn_ctx_output_map_close(ctx);
1375 }
1376
1377 static void
1378 command_schema_output_tables(grn_ctx *ctx, grn_schema_data *data)
1379 {
1380 grn_obj table_ids;
1381
1382 GRN_RECORD_INIT(&table_ids, GRN_OBJ_VECTOR, GRN_ID_NIL);
1383 GRN_DB_EACH_BEGIN_BY_KEY(ctx, cursor, id)
1384 {
1385 void *name;
1386 int name_size;
1387 grn_obj *object;
1388
1389 if (grn_id_is_builtin(ctx, id)) {
1390 continue;
1391 }
1392
1393 name_size = grn_table_cursor_get_key(ctx, cursor, &name);
1394 if (grn_obj_name_is_column(ctx, name, name_size)) {
1395 continue;
1396 }
1397
1398 if (data->is_close_opened_object_mode) {
1399 grn_ctx_push_temporary_open_space(ctx);
1400 }
1401
1402 object = grn_ctx_at(ctx, id);
1403 if (!object) {
1404 /* XXX: this clause is executed when MeCab tokenizer is enabled in
1405 database but the groonga isn't supported MeCab.
1406 We should return error message about it and error exit status
1407 but it's too difficult for this architecture. :< */
1408 GRN_PLUGIN_CLEAR_ERROR(ctx);
1409 goto next_loop;
1410 }
1411
1412 if (grn_obj_is_table(ctx, object)) {
1413 GRN_RECORD_PUT(ctx, &table_ids, id);
1414 }
1415
1416 next_loop:
1417 if (data->is_close_opened_object_mode) {
1418 grn_ctx_pop_temporary_open_space(ctx);
1419 }
1420 }
1421 GRN_TABLE_EACH_END(ctx, cursor);
1422
1423 size_t i, n;
1424 n = GRN_BULK_VSIZE(&table_ids) / sizeof(grn_id);
1425
1426 grn_ctx_output_cstr(ctx, "tables");
1427 grn_ctx_output_map_open(ctx, "tables", (int)n);
1428 for (i = 0; i < n; i++) {
1429 grn_id table_id;
1430 grn_obj *table;
1431
1432 if (data->is_close_opened_object_mode) {
1433 grn_ctx_push_temporary_open_space(ctx);
1434 }
1435
1436 table_id = GRN_RECORD_VALUE_AT(&table_ids, i);
1437 table = grn_ctx_at(ctx, table_id);
1438
1439 command_schema_output_table(ctx, data, table);
1440
1441 if (data->is_close_opened_object_mode) {
1442 grn_ctx_pop_temporary_open_space(ctx);
1443 }
1444 }
1445 grn_ctx_output_map_close(ctx);
1446
1447 GRN_OBJ_FIN(ctx, &table_ids);
1448 }
1449
1450 static grn_obj *
1451 command_schema(grn_ctx *ctx,
1452 int nargs,
1453 grn_obj **args,
1454 grn_user_data *user_data)
1455 {
1456 grn_schema_data data;
1457
1458 data.is_close_opened_object_mode = (grn_thread_get_limit() == 1);
1459
1460 grn_ctx_output_map_open(ctx, "schema", 6);
1461 command_schema_output_plugins(ctx);
1462 command_schema_output_types(ctx);
1463 command_schema_output_tokenizers(ctx, &data);
1464 command_schema_output_normalizers(ctx, &data);
1465 command_schema_output_token_filters(ctx, &data);
1466 command_schema_output_tables(ctx, &data);
1467 grn_ctx_output_map_close(ctx);
1468
1469 return NULL;
1470 }
1471
1472 void
1473 grn_proc_init_schema(grn_ctx *ctx)
1474 {
1475 grn_plugin_command_create(ctx, "schema", -1, command_schema, 0, NULL);
1476 }