Debian Codegraph @

lib/grn_db.h

raw symbols SCIP
1 /*
2 Copyright (C) 2009-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 #pragma once
21
22 #include "grn.h"
23
24 #include "grn_tokenizer.h"
25
26 #include "grn_ctx.h"
27 #include "grn_options.h"
28 #include "grn_rset.h"
29 #include "grn_store.h"
30
31 #include <groonga/command.h>
32 #include <groonga/extractor.h>
33 #include <groonga/token_filter.h>
34 #include <groonga/scorer.h>
35
36 #include <float.h>
37
38 #ifdef __cplusplus
39 extern "C" {
40 #endif
41
42 #define GRN_DB_DELIMITER '.'
43 #define GRN_DB_PSEUDO_COLUMN_PREFIX '_'
44
45 #define GRN_N_RESERVED_TYPES 256
46
47 /* #define GRN_REFERENCE_COUNT_DEBUG */
48 #ifdef GRN_REFERENCE_COUNT_DEBUG
49 # define grn_log_reference_count(...) printf(__VA_ARGS__)
50 #else
51 # define grn_log_reference_count(...)
52 #endif
53
54 #define GRN_TABLE_GROUPED (0x01 << 0)
55 #define GRN_TABLE_IS_GROUPED(table) \
56 ((table)->header.impl_flags & GRN_TABLE_GROUPED)
57 #define GRN_TABLE_GROUPED_ON(table) \
58 ((table)->header.impl_flags |= GRN_TABLE_GROUPED)
59 #define GRN_TABLE_IS_MULTI_KEYS_GROUPED(table) \
60 (GRN_TABLE_IS_GROUPED(table) && table->header.domain == GRN_ID_NIL)
61
62 #define DB_OBJ(obj) ((grn_db_obj *)obj)
63
64 #define GRN_DEFINE_NAME_CUSTOM(obj, prefix) \
65 const char *prefix; \
66 char prefix##_buffer[GRN_TABLE_MAX_KEY_SIZE]; \
67 int prefix##_size; \
68 do { \
69 if (!obj) { \
70 prefix = "(NULL)"; \
71 prefix##_size = (int)strlen(prefix); \
72 } else if (DB_OBJ(obj)->id == GRN_ID_NIL) { \
73 prefix = "(temporary)"; \
74 prefix##_size = (int)strlen(prefix); \
75 } else { \
76 prefix##_size = grn_obj_name(ctx, \
77 (grn_obj *)obj, \
78 prefix##_buffer, \
79 GRN_TABLE_MAX_KEY_SIZE); \
80 prefix = prefix##_buffer; \
81 if (prefix##_size == 0) { \
82 prefix = "(anonymous)"; \
83 prefix##_size = (int)strlen(prefix); \
84 } else if (prefix##_size < GRN_TABLE_MAX_KEY_SIZE) { \
85 prefix##_buffer[prefix##_size] = '\0'; \
86 } \
87 } \
88 } while (false)
89
90 #define GRN_DEFINE_NAME(obj) GRN_DEFINE_NAME_CUSTOM(obj, name)
91
92 typedef struct _grn_db grn_db;
93 typedef struct _grn_proc grn_proc;
94
95 typedef struct {
96 uint32_t count;
97 grn_obj ids;
98 } grn_deferred_unref;
99
100 struct _grn_db {
101 grn_db_obj obj;
102 grn_obj *keys;
103 grn_ja *specs;
104 grn_hash *config;
105 grn_tiny_array values;
106 grn_critical_section lock;
107 grn_cache *cache;
108 grn_options *options;
109 bool is_closing;
110 grn_array *deferred_unrefs;
111 bool is_deferred_unrefing;
112 };
113
114 #define GRN_SERIALIZED_SPEC_INDEX_SPEC 0
115 #define GRN_SERIALIZED_SPEC_INDEX_PATH 1
116 #define GRN_SERIALIZED_SPEC_INDEX_SOURCE 2
117 #define GRN_SERIALIZED_SPEC_INDEX_HOOK 3
118 #define GRN_SERIALIZED_SPEC_INDEX_TOKEN_FILTERS 4
119 #define GRN_SERIALIZED_SPEC_INDEX_NORMALIZERS 5
120 #define GRN_SERIALIZED_SPEC_INDEX_EXTRACTORS 6
121 #define GRN_SERIALIZED_SPEC_INDEX_EXPR 4
122 #define GRN_SERIALIZED_SPEC_INDEX_JA_GENERATOR 4
123 #define GRN_SERIALIZED_SPEC_INDEX_RA_GENERATOR 4
124
125 typedef struct {
126 grn_obj_header header;
127 grn_id range;
128 } grn_obj_spec;
129
130 bool
131 grn_obj_spec_unpack(grn_ctx *ctx,
132 grn_id id,
133 void *encoded_spec,
134 uint32_t encoded_spec_size,
135 grn_obj_spec **spec,
136 grn_obj *decoded_spec,
137 const char *error_message_tag);
138
139 void
140 grn_obj_spec_get_path(grn_ctx *ctx,
141 grn_obj_spec *spec,
142 grn_id id,
143 char *buffer,
144 grn_db *db,
145 grn_obj *decoded_spec);
146
147 #define GRN_DB_EACH_SPEC_BEGIN(ctx, cursor, id, spec) \
148 do { \
149 grn_obj *db = grn_ctx_db((ctx)); \
150 grn_db *db_raw = (grn_db *)db; \
151 grn_obj decoded_spec; \
152 grn_io_win iw; \
153 bool iw_need_unref = false; \
154 GRN_OBJ_INIT(&decoded_spec, GRN_VECTOR, 0, GRN_DB_TEXT); \
155 GRN_TABLE_EACH_BEGIN((ctx), db, cursor, id) \
156 { \
157 void *encoded_spec; \
158 uint32_t encoded_spec_size; \
159 bool success; \
160 grn_obj_spec *spec; \
161 \
162 if (iw_need_unref) { \
163 grn_ja_unref(ctx, &iw); \
164 iw_need_unref = false; \
165 } \
166 encoded_spec = \
167 grn_ja_ref((ctx), db_raw->specs, id, &iw, &encoded_spec_size); \
168 if (!encoded_spec) { \
169 continue; \
170 } \
171 iw_need_unref = true; \
172 \
173 GRN_BULK_REWIND(&decoded_spec); \
174 success = grn_obj_spec_unpack(ctx, \
175 id, \
176 encoded_spec, \
177 encoded_spec_size, \
178 &spec, \
179 &decoded_spec, \
180 __FUNCTION__); \
181 if (!success) { \
182 continue; \
183 }
184
185 #define GRN_DB_EACH_SPEC_END(ctx, cursor) \
186 } \
187 GRN_TABLE_EACH_END(ctx, cursor); \
188 if (iw_need_unref) { \
189 grn_ja_unref(ctx, &iw); \
190 } \
191 GRN_OBJ_FIN((ctx), &decoded_spec); \
192 } \
193 while (false)
194
195 void
196 grn_db_init_from_env(void);
197
198 void
199 grn_db_wal_recover(grn_ctx *ctx, grn_db *db);
200
201 GRN_API grn_rc
202 grn_db_close(grn_ctx *ctx, grn_obj *db);
203
204 grn_obj *
205 grn_db_keys(grn_obj *s);
206
207 void
208 grn_db_generate_pathname(grn_ctx *ctx, grn_obj *db, grn_id id, char *buffer);
209 grn_rc
210 grn_db_clear_dirty(grn_ctx *ctx, grn_obj *db);
211
212 grn_rc
213 grn_db_set_option_values(grn_ctx *ctx,
214 grn_obj *db,
215 grn_id id,
216 const char *name,
217 int name_length,
218 grn_obj *values);
219 grn_option_revision
220 grn_db_get_option_values(grn_ctx *ctx,
221 grn_obj *db,
222 grn_id id,
223 const char *name,
224 int name_length,
225 grn_option_revision revision,
226 grn_obj *values);
227 grn_rc
228 grn_db_clear_option_values(grn_ctx *ctx, grn_obj *db, grn_id id);
229 grn_rc
230 grn_obj_refer_auto_release(grn_ctx *ctx, grn_obj *obj, uint32_t count);
231 grn_rc
232 grn_obj_refer_recursive_auto_release(grn_ctx *ctx,
233 grn_obj *obj,
234 uint32_t count);
235 grn_rc
236 grn_obj_refer_recursive_dependent_auto_release(grn_ctx *ctx,
237 grn_obj *obj,
238 uint32_t count);
239 grn_rc
240 grn_db_add_deferred_unref(grn_ctx *ctx,
241 grn_obj *db,
242 grn_deferred_unref *deferred_unref);
243 grn_rc
244 grn_db_remove_deferred_unref(grn_ctx *ctx, grn_obj *db, grn_id id);
245 grn_rc
246 grn_db_command_processed(grn_ctx *ctx, grn_obj *db);
247
248 typedef struct {
249 grn_obj *table;
250 grn_id id;
251 const void *key;
252 uint32_t key_size;
253 grn_table_delete_optarg *optarg;
254 grn_obj columns;
255 grn_obj index_columns;
256 } grn_table_delete_data;
257
258 grn_rc
259 grn_table_delete_data_init(grn_ctx *ctx,
260 grn_table_delete_data *data,
261 grn_obj *table);
262 grn_rc
263 grn_table_delete_data_fin(grn_ctx *ctx, grn_table_delete_data *data);
264
265 grn_rc
266 grn_table_delete_by_id_without_lock(grn_ctx *ctx, grn_table_delete_data *data);
267
268 grn_id
269 grn_table_get_v(
270 grn_ctx *ctx, grn_obj *table, const void *key, int key_size, void **value);
271 grn_id
272 grn_table_get_by_key(grn_ctx *ctx, grn_obj *table, grn_obj *key);
273 grn_id
274 grn_table_add_v(grn_ctx *ctx,
275 grn_obj *table,
276 const void *key,
277 int key_size,
278 void **value,
279 int *added);
280 typedef struct {
281 bool added;
282 bool ignore_empty_normalized_key;
283 bool ignored;
284 } grn_table_add_options;
285 grn_id
286 grn_table_add_by_key(grn_ctx *ctx,
287 grn_obj *table,
288 grn_obj *key,
289 grn_table_add_options *options);
290 GRN_API grn_rc
291 grn_table_get_info(grn_ctx *ctx,
292 grn_obj *table,
293 grn_table_flags *flags,
294 grn_encoding *encoding,
295 grn_obj **tokenizer,
296 grn_obj **normalizer,
297 grn_obj **token_filters);
298 const char *
299 _grn_table_key(grn_ctx *ctx, grn_obj *table, grn_id id, uint32_t *key_size);
300
301 grn_rc
302 grn_table_search(grn_ctx *ctx,
303 grn_obj *table,
304 const void *key,
305 uint32_t key_size,
306 grn_operator mode,
307 grn_obj *res,
308 grn_operator op);
309
310 grn_rc
311 grn_table_fuzzy_search(grn_ctx *ctx,
312 grn_obj *table,
313 const void *key,
314 uint32_t key_size,
315 grn_fuzzy_search_optarg *args,
316 grn_obj *res,
317 grn_operator op);
318
319 grn_id
320 grn_table_next(grn_ctx *ctx, grn_obj *table, grn_id id);
321
322 int
323 grn_table_get_key2(grn_ctx *ctx, grn_obj *table, grn_id id, grn_obj *bulk);
324
325 grn_table_cursor *
326 grn_table_cursor_open_by_id(
327 grn_ctx *ctx, grn_obj *table, grn_id min, grn_id max, int flags);
328
329 static inline void
330 grn_table_add_subrec(grn_ctx *ctx,
331 grn_obj *table,
332 grn_rset_recinfo *ri,
333 double score,
334 grn_rset_posinfo *pi,
335 int dir)
336 {
337 if (DB_OBJ(table)->header.flags & GRN_OBJ_WITH_SUBREC) {
338 ri->score += score;
339 ri->n_subrecs += 1;
340 /* This is a duplicated check but it reduces the number of
341 * function calls. It improves performance when many records are
342 * matched. */
343 uint32_t limit = DB_OBJ(table)->max_n_subrecs;
344 if (limit > 0) {
345 grn_rset_add_subrec(ctx, ri, table, score, pi, dir);
346 }
347 }
348 }
349
350 grn_obj *
351 grn_obj_graft(grn_ctx *ctx, grn_obj *obj);
352
353 grn_rc
354 grn_column_name_(grn_ctx *ctx, grn_obj *obj, grn_obj *buf);
355
356 typedef enum {
357 PROC_INIT = 0,
358 PROC_NEXT,
359 PROC_FIN
360 } grn_proc_phase;
361
362 struct _grn_type {
363 grn_db_obj obj;
364 };
365
366 #define GRN_TYPE_SIZE(type) ((type)->range)
367
368 #define GRN_OBJ_TMP_OBJECT 0x80000000
369 #define GRN_OBJ_TMP_COLUMN 0x40000000
370
371 #define GRN_DB_OBJP(obj) \
372 (obj && ((GRN_SNIP == ((grn_db_obj *)obj)->header.type) || \
373 ((GRN_CURSOR_TABLE_HASH_KEY <= ((grn_db_obj *)obj)->header.type) && \
374 (((grn_db_obj *)obj)->header.type <= GRN_COLUMN_INDEX))))
375
376 #define GRN_OBJ_TABLEP(obj) \
377 (obj && (GRN_TABLE_HASH_KEY <= ((grn_db_obj *)obj)->header.type) && \
378 (((grn_db_obj *)obj)->header.type <= GRN_DB))
379
380 #define GRN_OBJ_INDEX_COLUMNP(obj) \
381 (obj && DB_OBJ(obj)->header.type == GRN_COLUMN_INDEX)
382
383 #define GRN_OBJ_VECTOR_COLUMNP(obj) \
384 (obj && DB_OBJ(obj)->header.type == GRN_COLUMN_VAR_SIZE && \
385 (DB_OBJ(obj)->header.flags & GRN_OBJ_COLUMN_TYPE_MASK) == \
386 GRN_OBJ_COLUMN_VECTOR)
387
388 #define GRN_OBJ_WEIGHT_VECTOR_COLUMNP(obj) \
389 (GRN_OBJ_VECTOR_COLUMNP(obj) && \
390 (DB_OBJ(obj)->header.flags & GRN_OBJ_WITH_WEIGHT))
391
392 struct _grn_hook {
393 grn_hook *next;
394 grn_proc *proc;
395 uint32_t data_size;
396 };
397
398 typedef struct _grn_proc_ctx grn_proc_ctx;
399
400 struct _grn_proc_ctx {
401 grn_user_data user_data;
402 grn_proc *proc;
403 grn_obj *caller;
404 // grn_obj *obj;
405 grn_hook *hooks;
406 grn_hook *currh;
407 grn_proc_phase phase;
408 unsigned short nargs;
409 unsigned short offset;
410 grn_user_data data[16];
411 };
412
413 static inline void
414 grn_proc_ctx_init(grn_proc_ctx *pctx,
415 grn_hook *hooks,
416 unsigned short n_args,
417 unsigned short offset)
418 {
419 memset(pctx, 0, sizeof(*pctx));
420 pctx->proc = hooks->proc;
421 pctx->hooks = hooks;
422 pctx->currh = hooks;
423 pctx->phase = PROC_INIT;
424 pctx->nargs = n_args;
425 pctx->offset = offset;
426 }
427
428 struct _grn_proc {
429 grn_db_obj obj;
430 grn_obj name_buf;
431 grn_expr_var *vars;
432 uint32_t nvars;
433 /* -- compatible with grn_expr -- */
434 grn_proc_type type;
435 grn_proc_func *funcs[3];
436
437 union {
438 struct {
439 grn_selector_func *selector;
440 grn_operator selector_op;
441 bool is_stable;
442 grn_applier_func *applier;
443 grn_sorter_func *sorter;
444 } function;
445 struct {
446 grn_command_run_func *run;
447 } command;
448 struct {
449 grn_tokenizer_build_func *build;
450 grn_tokenizer_init_func *init;
451 grn_tokenizer_next_func *next;
452 grn_tokenizer_fin_func *fin;
453 } tokenizer;
454 struct {
455 grn_token_filter_init_func *init;
456 grn_token_filter_init_query_func *init_query;
457 grn_token_filter_filter_func *filter;
458 grn_token_filter_fin_func *fin;
459 } token_filter;
460 struct {
461 grn_scorer_score_func *score;
462 } scorer;
463 grn_window_function_func *window_function;
464 struct {
465 grn_aggregator_init_func *init;
466 grn_aggregator_update_func *update;
467 grn_aggregator_fin_func *fin;
468 } aggregator;
469 struct {
470 grn_extractor_extract_func *extract;
471 } extractor;
472 } callbacks;
473
474 void *user_data;
475
476 grn_id module;
477 // uint32_t nargs;
478 // uint32_t nresults;
479 // grn_obj results[16];
480 };
481
482 #define GRN_PROC_GET_VARS() (grn_proc_get_vars(ctx, user_data))
483 #define GRN_PROC_GET_VAR(name) \
484 (grn_proc_get_var(ctx, user_data, name, strlen(name)))
485 #define GRN_PROC_GET_VAR_BY_OFFSET(offset) \
486 (grn_proc_get_var_by_offset(ctx, user_data, offset))
487 #define GRN_PROC_GET_OR_ADD_VAR(name) \
488 (grn_proc_get_or_add_var(ctx, user_data, name, strlen(name)))
489 #define GRN_PROC_ALLOC(domain, flags) \
490 (grn_proc_alloc(ctx, user_data, domain, flags))
491
492 grn_obj *
493 grn_proc_get_vars(grn_ctx *ctx, grn_user_data *user_data);
494
495 grn_obj *
496 grn_proc_get_var(grn_ctx *ctx,
497 grn_user_data *user_data,
498 const char *name,
499 unsigned int name_size);
500
501 GRN_API grn_obj *
502 grn_proc_get_var_by_offset(grn_ctx *ctx,
503 grn_user_data *user_data,
504 unsigned int offset);
505 GRN_API grn_obj *
506 grn_proc_get_or_add_var(grn_ctx *ctx,
507 grn_user_data *user_data,
508 const char *name,
509 unsigned int name_size);
510
511 GRN_API grn_obj *
512 grn_proc_alloc(grn_ctx *ctx,
513 grn_user_data *user_data,
514 grn_id domain,
515 unsigned char flags);
516
517 GRN_API grn_rc
518 grn_proc_call(grn_ctx *ctx, grn_obj *proc, int nargs, grn_obj *caller);
519
520 grn_obj *
521 grn_expr_get_or_add_var(grn_ctx *ctx,
522 grn_obj *expr,
523 const char *name,
524 unsigned int name_size);
525
526 typedef struct _grn_accessor grn_accessor;
527
528 struct _grn_accessor {
529 grn_obj_header header;
530 grn_id range;
531 /* -- compatible with grn_db_obj -- */
532 uint8_t action;
533 int offset;
534 grn_obj *obj;
535 grn_accessor *next;
536 uint32_t reference_count;
537 };
538
539 GRN_API const char *
540 grn_obj_get_value_(grn_ctx *ctx, grn_obj *obj, grn_id id, uint32_t *size);
541 void
542 grn_obj_get_range_info(grn_ctx *ctx,
543 grn_obj *obj,
544 grn_id *range_id,
545 grn_obj_flags *range_flags);
546
547 grn_rc
548 grn_db_init_builtin_types(grn_ctx *ctx);
549
550 /* flag value used for grn_obj.header.flags */
551
552 #define GRN_OBJ_CUSTOM_NAME (0x01 << 12) /* db_obj which has custom name */
553
554 #define GRN_OBJ_RESOLVE(ctx, obj) \
555 (((obj)->header.type != GRN_PTR) ? (obj) \
556 : GRN_PTR_VALUE(obj) ? GRN_PTR_VALUE(obj) \
557 : grn_ctx_at((ctx), (obj)->header.domain))
558
559 /* expr */
560
561 typedef struct _grn_expr grn_expr;
562
563 /**
564 * grn_table_open:
565 * @name: The table name to be opened. `NULL` means anonymous table.
566 * @path: The path of the table to be opened.
567 *
568 * Opens an existing table. The table is associated with @name in DB
569 * that is used by @ctx. grn_ctx_get() is better rather than this
570 * function when you want to open a permanent named table that is
571 * registered in DB.
572 **/
573 GRN_API grn_obj *
574 grn_table_open(grn_ctx *ctx,
575 const char *name,
576 unsigned int name_size,
577 const char *path);
578
579 /**
580 * grn_column_open:
581 * @table: The table for the opened column.
582 * @name: The column name to be opened.
583 * @path: The path of the column to be opened.
584 * @type: The type of the column value.
585 *
586 * Opens an existing permanent column. The column is associated with
587 * @name in @table. grn_ctx_get() is better rather than this function
588 * when you want to open a column of an permanent table in DB.
589 **/
590 grn_obj *
591 grn_column_open(grn_ctx *ctx,
592 grn_obj *table,
593 const char *name,
594 unsigned int name_size,
595 const char *path,
596 grn_obj *type);
597
598 /**
599 * grn_obj_path_rename:
600 * @old_path: The current file path.
601 * @new_path: The new file path.
602 *
603 * It renames object's path that is stored in @old_path to @new_path.
604 **/
605 grn_rc
606 grn_obj_path_rename(grn_ctx *ctx, const char *old_path, const char *new_path);
607
608 grn_rc
609 grn_db_check_name(grn_ctx *ctx, const char *name, unsigned int name_size);
610 #define GRN_DB_CHECK_NAME_ERR(error_context, name, name_size) \
611 ERR(GRN_INVALID_ARGUMENT, \
612 "%s name can't start with '%c' and contains only 0-9, A-Z, a-z, #, @, " \
613 "- or _: <%.*s>", \
614 error_context, \
615 GRN_DB_PSEUDO_COLUMN_PREFIX, \
616 name_size, \
617 name)
618
619 #define GRN_DB_P(s) ((s) && ((grn_db *)s)->obj.header.type == GRN_DB)
620 #define GRN_DB_PERSISTENT_P(s) (((grn_db *)s)->specs)
621
622 #define GRN_OBJ_GET_VALUE_IMD (0xffffffffU)
623
624 grn_rc
625 grn_db_obj_init(grn_ctx *ctx, grn_obj *db, grn_id id, grn_db_obj *obj);
626
627 #define GRN_ACCESSORP(obj) \
628 ((obj) && (((grn_obj *)(obj))->header.type == GRN_ACCESSOR))
629
630 uint32_t
631 grn_obj_reference_count(grn_ctx *ctx, grn_obj *obj);
632
633 grn_id
634 grn_obj_register(grn_ctx *ctx,
635 grn_obj *db,
636 const char *name,
637 unsigned int name_size);
638 int
639 grn_obj_is_persistent(grn_ctx *ctx, grn_obj *obj);
640 void
641 grn_obj_spec_save(grn_ctx *ctx, grn_db_obj *obj);
642
643 grn_rc
644 grn_obj_reinit_for(grn_ctx *ctx, grn_obj *obj, grn_obj *domain_obj);
645
646 void
647 grn_obj_ensure_bulk(grn_ctx *ctx, grn_obj *obj);
648 void
649 grn_obj_ensure_vector(grn_ctx *ctx, grn_obj *obj);
650
651 void
652 grn_expr_pack(grn_ctx *ctx, grn_obj *buf, grn_obj *expr);
653 GRN_API grn_rc
654 grn_expr_inspect(grn_ctx *ctx, grn_obj *buf, grn_obj *expr);
655 grn_hash *
656 grn_expr_get_vars(grn_ctx *ctx, grn_obj *expr, unsigned int *nvars);
657 grn_obj *
658 grn_expr_open(grn_ctx *ctx,
659 grn_obj_spec *spec,
660 const uint8_t *p,
661 const uint8_t *pe);
662
663 GRN_API grn_rc
664 grn_table_group_with_range_gap(grn_ctx *ctx,
665 grn_obj *table,
666 grn_table_sort_key *group_key,
667 grn_obj *result_set,
668 uint32_t range_gap);
669
670 grn_obj *
671 grn_table_column(grn_ctx *ctx,
672 grn_obj *table,
673 const char *name,
674 ssize_t name_size);
675 grn_rc
676 grn_table_parse_load_columns(grn_ctx *ctx,
677 grn_obj *table,
678 const char *input,
679 size_t input_size,
680 grn_obj *columns);
681
682 typedef struct {
683 grn_id target;
684 unsigned int section;
685 } grn_obj_default_set_value_hook_data;
686
687 grn_obj *
688 grn_obj_default_set_value_hook(grn_ctx *ctx,
689 int nargs,
690 grn_obj **args,
691 grn_user_data *user_data);
692
693 grn_rc
694 grn_column_get_all_token_columns(grn_ctx *ctx,
695 grn_obj *obj,
696 grn_obj *token_columns);
697 grn_rc
698 grn_column_get_all_hooked_columns(grn_ctx *ctx,
699 grn_obj *obj,
700 grn_obj *hooked_columns);
701
702 grn_rc
703 grn_pvector_fin(grn_ctx *ctx, grn_obj *obj);
704
705 typedef struct grn_id_map {
706 grn_hash *hash;
707 grn_critical_section critical_section;
708 } grn_id_map;
709
710 static inline grn_id_map *
711 grn_id_map_open(grn_ctx *ctx)
712 {
713 grn_id_map *id_map = (grn_id_map *)GRN_MALLOC(sizeof(grn_id_map));
714 if (!id_map) {
715 return NULL;
716 }
717 id_map->hash = grn_hash_create(ctx,
718 NULL,
719 sizeof(grn_id),
720 sizeof(grn_id),
721 GRN_OBJ_TABLE_HASH_KEY | GRN_HASH_TINY);
722 CRITICAL_SECTION_INIT(id_map->critical_section);
723 return id_map;
724 }
725
726 static inline grn_rc
727 grn_id_map_close(grn_ctx *ctx, grn_id_map *id_map)
728 {
729 if (!id_map) {
730 return GRN_SUCCESS;
731 }
732 grn_rc rc = grn_hash_close(ctx, id_map->hash);
733 CRITICAL_SECTION_FIN(id_map->critical_section);
734 GRN_FREE(id_map);
735 return rc;
736 }
737
738 static inline void
739 grn_id_map_add(grn_ctx *ctx, grn_id_map *id_map, grn_id old_id, grn_id new_id)
740 {
741 if (!id_map) {
742 return;
743 }
744 CRITICAL_SECTION_ENTER(id_map->critical_section);
745 void *value;
746 grn_hash_add(ctx, id_map->hash, &old_id, sizeof(grn_id), &value, NULL);
747 *(grn_id *)value = new_id;
748 CRITICAL_SECTION_LEAVE(id_map->critical_section);
749 }
750
751 static inline grn_id
752 grn_id_map_resolve(grn_ctx *ctx, grn_id_map *id_map, grn_id id)
753 {
754 if (id == GRN_ID_NIL) {
755 return id;
756 }
757 if (!id_map) {
758 return id;
759 }
760 grn_id resolved_id = id;
761 CRITICAL_SECTION_ENTER(id_map->critical_section);
762 void *value;
763 if (grn_hash_get(ctx, id_map->hash, &id, sizeof(grn_id), &value) !=
764 GRN_ID_NIL) {
765 resolved_id = *(grn_id *)value;
766 }
767 CRITICAL_SECTION_LEAVE(id_map->critical_section);
768 return resolved_id;
769 }
770
771 #define GRN_DB_WAL_DISABLE_WAL_BEGIN(ctx) \
772 do { \
773 grn_wal_role wal_role_current = grn_ctx_get_wal_role(ctx); \
774 grn_ctx_set_wal_role(ctx, GRN_WAL_ROLE_NONE);
775
776 #define GRN_DB_WAL_DISABLE_WAL_END(ctx) \
777 grn_ctx_set_wal_role(ctx, wal_role_current); \
778 } \
779 while (false)
780
781 static inline grn_obj *
782 grn_table_create_similar_id_map(grn_ctx *ctx,
783 const char *name,
784 uint32_t name_size,
785 const char *path,
786 grn_obj *base_table,
787 grn_id_map *id_map)
788 {
789 const char *tag = "[table][create][similar]";
790 if (!grn_obj_is_table(ctx, base_table)) {
791 grn_obj inspected;
792 GRN_TEXT_INIT(&inspected, 0);
793 grn_inspect_limited(ctx, &inspected, base_table);
794 ERR(GRN_INVALID_ARGUMENT,
795 "%s must be table: %.*s",
796 tag,
797 (int)GRN_TEXT_LEN(&inspected),
798 GRN_TEXT_VALUE(&inspected));
799 GRN_OBJ_FIN(ctx, &inspected);
800 return NULL;
801 }
802
803 grn_table_flags flags = 0;
804 grn_id domain = GRN_ID_NIL;
805 grn_id range = GRN_ID_NIL;
806 grn_obj *key_type = NULL;
807 grn_obj *value_type = NULL;
808
809 if (grn_obj_is_persistent(ctx, base_table)) {
810 flags |= GRN_OBJ_PERSISTENT;
811 }
812
813 switch (base_table->header.type) {
814 case GRN_TABLE_HASH_KEY:
815 flags |= GRN_OBJ_TABLE_HASH_KEY;
816 if (((grn_hash *)base_table)->header.common->flags & GRN_OBJ_KEY_LARGE) {
817 flags |= GRN_OBJ_KEY_LARGE;
818 }
819 domain = base_table->header.domain;
820 range = DB_OBJ(base_table)->range;
821 break;
822 case GRN_TABLE_PAT_KEY:
823 flags |= GRN_OBJ_TABLE_PAT_KEY;
824 if (base_table->header.flags & GRN_OBJ_KEY_WITH_SIS) {
825 flags |= GRN_OBJ_KEY_WITH_SIS;
826 }
827 domain = base_table->header.domain;
828 range = DB_OBJ(base_table)->range;
829 break;
830 case GRN_TABLE_DAT_KEY:
831 flags |= GRN_OBJ_TABLE_DAT_KEY;
832 domain = base_table->header.domain;
833 break;
834 case GRN_TABLE_NO_KEY:
835 flags |= GRN_OBJ_TABLE_NO_KEY;
836 range = DB_OBJ(base_table)->range;
837 break;
838 default:
839 break;
840 }
841 if (domain != GRN_ID_NIL) {
842 domain = grn_id_map_resolve(ctx, id_map, domain);
843 key_type = grn_ctx_at(ctx, domain);
844 }
845 if (range != GRN_ID_NIL) {
846 range = grn_id_map_resolve(ctx, id_map, range);
847 value_type = grn_ctx_at(ctx, range);
848 }
849 grn_obj *table =
850 grn_table_create(ctx, name, name_size, path, flags, key_type, value_type);
851 GRN_DB_WAL_DISABLE_WAL_BEGIN(ctx)
852 {
853 if (key_type) {
854 grn_obj_unref(ctx, key_type);
855 }
856 if (value_type) {
857 grn_obj_unref(ctx, value_type);
858 }
859 }
860 GRN_DB_WAL_DISABLE_WAL_END(ctx);
861 if (!table) {
862 return NULL;
863 }
864
865 grn_obj buffer;
866 GRN_TEXT_INIT(&buffer, 0);
867 if (grn_obj_is_table_with_key(ctx, base_table)) {
868 GRN_BULK_REWIND(&buffer);
869 grn_table_get_default_tokenizer_string(ctx, base_table, &buffer);
870 if (GRN_TEXT_LEN(&buffer) > 0) {
871 grn_obj_set_info(ctx, table, GRN_INFO_DEFAULT_TOKENIZER, &buffer);
872 if (ctx->rc != GRN_SUCCESS) {
873 goto exit;
874 }
875 }
876
877 GRN_BULK_REWIND(&buffer);
878 grn_table_get_normalizers_string(ctx, base_table, &buffer);
879 if (GRN_TEXT_LEN(&buffer) > 0) {
880 grn_obj_set_info(ctx, table, GRN_INFO_NORMALIZERS, &buffer);
881 if (ctx->rc != GRN_SUCCESS) {
882 goto exit;
883 }
884 }
885
886 GRN_BULK_REWIND(&buffer);
887 grn_table_get_token_filters_string(ctx, base_table, &buffer);
888 if (GRN_TEXT_LEN(&buffer) > 0) {
889 grn_obj_set_info(ctx, table, GRN_INFO_TOKEN_FILTERS, &buffer);
890 if (ctx->rc != GRN_SUCCESS) {
891 goto exit;
892 }
893 }
894
895 GRN_BULK_REWIND(&buffer);
896 grn_table_get_extractors_string(ctx, base_table, &buffer);
897 if (GRN_TEXT_LEN(&buffer) > 0) {
898 grn_obj_set_info(ctx, table, GRN_INFO_EXTRACTORS, &buffer);
899 if (ctx->rc != GRN_SUCCESS) {
900 goto exit;
901 }
902 }
903 }
904
905 exit:
906 GRN_OBJ_FIN(ctx, &buffer);
907
908 if (ctx->rc != GRN_SUCCESS) {
909 grn_obj_remove(ctx, table);
910 return NULL;
911 }
912
913 return table;
914 }
915
916 static inline grn_obj *
917 grn_column_create_similar_id_map(grn_ctx *ctx,
918 grn_obj *table,
919 const char *name,
920 uint32_t name_size,
921 const char *path,
922 grn_obj *base_column,
923 grn_id_map *id_map)
924 {
925 const char *tag = "[column][create][similar]";
926
927 if (!grn_obj_is_table(ctx, table)) {
928 grn_obj inspected;
929 GRN_TEXT_INIT(&inspected, 0);
930 grn_inspect_limited(ctx, &inspected, table);
931 ERR(GRN_INVALID_ARGUMENT,
932 "%s must be table: %.*s",
933 tag,
934 (int)GRN_TEXT_LEN(&inspected),
935 GRN_TEXT_VALUE(&inspected));
936 GRN_OBJ_FIN(ctx, &inspected);
937 return NULL;
938 }
939 if (!name || name_size == 0) {
940 ERR(GRN_INVALID_ARGUMENT, "%s name is missing", tag);
941 return NULL;
942 }
943 if (!grn_obj_is_column(ctx, base_column)) {
944 grn_obj inspected;
945 GRN_TEXT_INIT(&inspected, 0);
946 grn_inspect_limited(ctx, &inspected, base_column);
947 ERR(GRN_INVALID_ARGUMENT,
948 "%s must be column: %.*s",
949 tag,
950 (int)GRN_TEXT_LEN(&inspected),
951 GRN_TEXT_VALUE(&inspected));
952 GRN_OBJ_FIN(ctx, &inspected);
953 return NULL;
954 }
955
956 grn_column_flags flags = 0;
957 if (grn_obj_is_persistent(ctx, table)) {
958 flags |= GRN_OBJ_PERSISTENT;
959 }
960 grn_id type_id = DB_OBJ(base_column)->range;
961 type_id = grn_id_map_resolve(ctx, id_map, type_id);
962 grn_obj *type = grn_ctx_at(ctx, type_id);
963 flags |= grn_column_get_flags(ctx, base_column);
964 grn_obj *column =
965 grn_column_create(ctx, table, name, name_size, path, flags, type);
966 if (!column) {
967 return NULL;
968 }
969
970 switch (column->header.type) {
971 case GRN_COLUMN_VAR_SIZE:
972 {
973 grn_obj base_source_ids;
974 GRN_RECORD_INIT(&base_source_ids, GRN_OBJ_VECTOR, GRN_ID_NIL);
975 grn_obj_get_info(ctx, base_column, GRN_INFO_SOURCE, &base_source_ids);
976 size_t n = GRN_RECORD_VECTOR_SIZE(&base_source_ids);
977 if (n > 0) {
978 grn_obj source_ids;
979 GRN_RECORD_INIT(&source_ids, GRN_OBJ_VECTOR, GRN_ID_NIL);
980 size_t i;
981 for (i = 0; i < n; i++) {
982 grn_id base_source_column_id =
983 GRN_RECORD_VALUE_AT(&base_source_ids, i);
984 grn_obj *base_source_column = grn_ctx_at(ctx, base_source_column_id);
985 if (!base_source_column) {
986 ERR(GRN_INVALID_ARGUMENT,
987 "%s base column's source doesn't exist: %u",
988 tag,
989 base_source_column_id);
990 break;
991 }
992 char source_column_name[GRN_TABLE_MAX_KEY_SIZE];
993 int source_column_name_length =
994 grn_column_name(ctx,
995 base_source_column,
996 source_column_name,
997 sizeof(source_column_name));
998 GRN_DB_WAL_DISABLE_WAL_BEGIN(ctx)
999 {
1000 grn_obj_unref(ctx, base_source_column);
1001 }
1002 GRN_DB_WAL_DISABLE_WAL_END(ctx);
1003 grn_obj *source_column = grn_table_column(ctx,
1004 table,
1005 source_column_name,
1006 source_column_name_length);
1007 if (!source_column) {
1008 ERR(GRN_INVALID_ARGUMENT,
1009 "%s source column doesn't exist: <%.*s.%.*s>",
1010 tag,
1011 (int)name_size,
1012 name,
1013 source_column_name_length,
1014 source_column_name);
1015 break;
1016 }
1017 GRN_RECORD_PUT(ctx, &source_ids, DB_OBJ(source_column)->id);
1018 GRN_DB_WAL_DISABLE_WAL_BEGIN(ctx)
1019 {
1020 grn_obj_unref(ctx, source_column);
1021 }
1022 GRN_DB_WAL_DISABLE_WAL_END(ctx);
1023 }
1024 if (ctx->rc == GRN_SUCCESS) {
1025 grn_obj_set_info(ctx, column, GRN_INFO_SOURCE, &source_ids);
1026 }
1027 GRN_OBJ_FIN(ctx, &source_ids);
1028 }
1029 GRN_OBJ_FIN(ctx, &base_source_ids);
1030 if (ctx->rc != GRN_SUCCESS) {
1031 grn_rc original_rc = ctx->rc;
1032 ctx->rc = GRN_SUCCESS;
1033 grn_obj_remove(ctx, column);
1034 ctx->rc = original_rc;
1035 return NULL;
1036 }
1037 }
1038 break;
1039 case GRN_COLUMN_INDEX:
1040 {
1041 grn_obj source_ids;
1042 GRN_RECORD_INIT(&source_ids, GRN_OBJ_VECTOR, GRN_ID_NIL);
1043 grn_obj_get_info(ctx, base_column, GRN_INFO_SOURCE, &source_ids);
1044 size_t n_source_ids = GRN_RECORD_VECTOR_SIZE(&source_ids);
1045 if (n_source_ids > 0) {
1046 grn_obj resolved_source_ids;
1047 GRN_RECORD_INIT(&resolved_source_ids, GRN_OBJ_VECTOR, GRN_ID_NIL);
1048 size_t i;
1049 for (i = 0; i < n_source_ids; i++) {
1050 grn_id source_id = GRN_RECORD_VALUE_AT(&source_ids, i);
1051 grn_id resolved_source_id =
1052 grn_id_map_resolve(ctx, id_map, source_id);
1053 GRN_RECORD_PUT(ctx, &resolved_source_ids, resolved_source_id);
1054 }
1055 grn_obj_set_info(ctx, column, GRN_INFO_SOURCE, &resolved_source_ids);
1056 GRN_OBJ_FIN(ctx, &resolved_source_ids);
1057 }
1058 GRN_OBJ_FIN(ctx, &source_ids);
1059 if (ctx->rc != GRN_SUCCESS) {
1060 grn_rc original_rc = ctx->rc;
1061 ctx->rc = GRN_SUCCESS;
1062 grn_obj_remove(ctx, column);
1063 ctx->rc = original_rc;
1064 return NULL;
1065 }
1066 }
1067 break;
1068 default:
1069 break;
1070 }
1071
1072 return column;
1073 }
1074
1075 bool
1076 grn_ctx_is_removing(grn_ctx *ctx, grn_id id);
1077
1078 #ifdef __cplusplus
1079 }
1080 #endif