| 1 | /* |
| 2 | Copyright (C) 2017-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_ctx.h" |
| 22 | #include "grn_dat.h" |
| 23 | #include "grn_hash.h" |
| 24 | #include "grn_pat.h" |
| 25 | |
| 26 | #include <stdio.h> |
| 27 | |
| 28 | static const char *OPTION_NAME_DEFAULT_TOKENIZER = "default_tokenizer"; |
| 29 | static const char *OPTION_NAME_NORMALIZER = "normalizer"; |
| 30 | static const char *OPTION_NAME_TOKEN_FILTER = "token_filter"; |
| 31 | static const char *OPTION_NAME_EXTRACTOR = "extractor"; |
| 32 | |
| 33 | void |
| 34 | grn_table_module_init(grn_ctx *ctx, grn_table_module *module, grn_id module_id) |
| 35 | { |
| 36 | if (module_id == GRN_ID_NIL) { |
| 37 | module->proc = NULL; |
| 38 | } else { |
| 39 | module->proc = grn_ctx_at(ctx, module_id); |
| 40 | } |
| 41 | module->options = NULL; |
| 42 | module->options_revision = GRN_OPTION_REVISION_NONE; |
| 43 | module->options_close_func = NULL; |
| 44 | CRITICAL_SECTION_INIT(module->lock); |
| 45 | } |
| 46 | |
| 47 | static void |
| 48 | grn_table_module_fin_options(grn_ctx *ctx, grn_table_module *module) |
| 49 | { |
| 50 | if (module->options && module->options_close_func) { |
| 51 | module->options_close_func(ctx, module->options); |
| 52 | module->options = NULL; |
| 53 | module->options_revision = GRN_OPTION_REVISION_NONE; |
| 54 | module->options_close_func = NULL; |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | void |
| 59 | grn_table_module_fin(grn_ctx *ctx, grn_table_module *module) |
| 60 | { |
| 61 | grn_table_module_fin_options(ctx, module); |
| 62 | CRITICAL_SECTION_FIN(module->lock); |
| 63 | } |
| 64 | |
| 65 | void |
| 66 | grn_table_module_set_proc(grn_ctx *ctx, grn_table_module *module, grn_obj *proc) |
| 67 | { |
| 68 | CRITICAL_SECTION_ENTER(module->lock); |
| 69 | grn_table_module_fin_options(ctx, module); |
| 70 | |
| 71 | module->proc = proc; |
| 72 | CRITICAL_SECTION_LEAVE(module->lock); |
| 73 | } |
| 74 | |
| 75 | static void |
| 76 | grn_table_module_set_options_without_lock(grn_ctx *ctx, |
| 77 | grn_table_module *module, |
| 78 | void *options, |
| 79 | grn_option_revision revision, |
| 80 | grn_close_func close_func) |
| 81 | { |
| 82 | grn_table_module_fin_options(ctx, module); |
| 83 | |
| 84 | module->options = options; |
| 85 | module->options_revision = revision; |
| 86 | module->options_close_func = close_func; |
| 87 | } |
| 88 | |
| 89 | void |
| 90 | grn_table_module_set_options(grn_ctx *ctx, |
| 91 | grn_table_module *module, |
| 92 | void *options, |
| 93 | grn_option_revision revision, |
| 94 | grn_close_func close_func) |
| 95 | { |
| 96 | CRITICAL_SECTION_ENTER(module->lock); |
| 97 | grn_table_module_set_options_without_lock(ctx, |
| 98 | module, |
| 99 | options, |
| 100 | revision, |
| 101 | close_func); |
| 102 | CRITICAL_SECTION_LEAVE(module->lock); |
| 103 | } |
| 104 | |
| 105 | void |
| 106 | grn_table_modules_init(grn_ctx *ctx, grn_obj *modules_buffer) |
| 107 | { |
| 108 | GRN_TEXT_INIT(modules_buffer, 0); |
| 109 | } |
| 110 | |
| 111 | void |
| 112 | grn_table_modules_fin(grn_ctx *ctx, grn_obj *modules_buffer) |
| 113 | { |
| 114 | grn_table_modules_rewind(ctx, modules_buffer); |
| 115 | GRN_OBJ_FIN(ctx, modules_buffer); |
| 116 | } |
| 117 | |
| 118 | void |
| 119 | grn_table_modules_add(grn_ctx *ctx, grn_obj *modules_buffer, grn_obj *proc) |
| 120 | { |
| 121 | grn_bulk_space(ctx, modules_buffer, sizeof(grn_table_module)); |
| 122 | size_t n = GRN_BULK_VSIZE(modules_buffer) / sizeof(grn_table_module); |
| 123 | grn_table_module *module = |
| 124 | ((grn_table_module *)GRN_BULK_HEAD(modules_buffer)) + n - 1; |
| 125 | grn_table_module_init(ctx, module, grn_obj_id(ctx, proc)); |
| 126 | grn_table_module_set_proc(ctx, module, proc); |
| 127 | } |
| 128 | |
| 129 | void |
| 130 | grn_table_modules_rewind(grn_ctx *ctx, grn_obj *modules_buffer) |
| 131 | { |
| 132 | grn_table_module *modules = (grn_table_module *)GRN_BULK_HEAD(modules_buffer); |
| 133 | size_t n = GRN_BULK_VSIZE(modules_buffer) / sizeof(grn_table_module); |
| 134 | size_t i; |
| 135 | for (i = 0; i < n; i++) { |
| 136 | grn_table_module_fin(ctx, modules + i); |
| 137 | } |
| 138 | GRN_BULK_REWIND(modules_buffer); |
| 139 | } |
| 140 | |
| 141 | grn_table_module * |
| 142 | grn_table_modules_get_module(grn_ctx *ctx, grn_obj *modules_buffer, size_t i) |
| 143 | { |
| 144 | grn_table_module *modules = (grn_table_module *)GRN_BULK_HEAD(modules_buffer); |
| 145 | size_t n = GRN_BULK_VSIZE(modules_buffer) / sizeof(grn_table_module); |
| 146 | if (i >= n) { |
| 147 | return NULL; |
| 148 | } |
| 149 | return modules + i; |
| 150 | } |
| 151 | |
| 152 | grn_obj * |
| 153 | grn_table_modules_get_proc(grn_ctx *ctx, grn_obj *modules_buffer, size_t i) |
| 154 | { |
| 155 | grn_table_module *module = |
| 156 | grn_table_modules_get_module(ctx, modules_buffer, i); |
| 157 | if (!module) { |
| 158 | return NULL; |
| 159 | } |
| 160 | return module->proc; |
| 161 | } |
| 162 | |
| 163 | static grn_rc |
| 164 | grn_table_set_module_options(grn_ctx *ctx, |
| 165 | grn_obj *table, |
| 166 | const char *module_name, |
| 167 | grn_obj *options, |
| 168 | const char *context_tag) |
| 169 | { |
| 170 | GRN_API_ENTER; |
| 171 | |
| 172 | if (!grn_obj_is_table_with_key(ctx, table)) { |
| 173 | ERR(GRN_INVALID_ARGUMENT, |
| 174 | "[table][%s][options][set] table must be key table: %s", |
| 175 | context_tag, |
| 176 | table ? grn_obj_type_to_string(table->header.type) : "(null)"); |
| 177 | GRN_API_RETURN(ctx->rc); |
| 178 | } |
| 179 | |
| 180 | if (options && options->header.type == GRN_VECTOR && |
| 181 | grn_vector_size(ctx, options) > 0) { |
| 182 | grn_obj_set_option_values(ctx, table, module_name, -1, options); |
| 183 | } else { |
| 184 | grn_obj current_options; |
| 185 | GRN_VOID_INIT(¤t_options); |
| 186 | grn_obj_get_option_values(ctx, |
| 187 | table, |
| 188 | module_name, |
| 189 | -1, |
| 190 | GRN_OPTION_REVISION_NONE, |
| 191 | ¤t_options); |
| 192 | if (current_options.header.type == GRN_VECTOR && |
| 193 | grn_vector_size(ctx, ¤t_options) > 1) { |
| 194 | grn_obj empty_options; |
| 195 | GRN_TEXT_INIT(&empty_options, GRN_OBJ_VECTOR); |
| 196 | grn_obj_set_option_values(ctx, table, module_name, -1, &empty_options); |
| 197 | GRN_OBJ_FIN(ctx, &empty_options); |
| 198 | } |
| 199 | GRN_OBJ_FIN(ctx, ¤t_options); |
| 200 | } |
| 201 | |
| 202 | GRN_API_RETURN(ctx->rc); |
| 203 | } |
| 204 | |
| 205 | static grn_rc |
| 206 | grn_table_get_module_options(grn_ctx *ctx, |
| 207 | grn_obj *table, |
| 208 | const char *module_name, |
| 209 | grn_obj *options, |
| 210 | const char *context_tag) |
| 211 | { |
| 212 | GRN_API_ENTER; |
| 213 | |
| 214 | if (!grn_obj_is_table_with_key(ctx, table)) { |
| 215 | ERR(GRN_INVALID_ARGUMENT, |
| 216 | "[table][%s][options][get] table must be key table: %s", |
| 217 | context_tag, |
| 218 | table ? grn_obj_type_to_string(table->header.type) : "(null)"); |
| 219 | GRN_API_RETURN(ctx->rc); |
| 220 | } |
| 221 | |
| 222 | grn_obj_get_option_values(ctx, |
| 223 | table, |
| 224 | module_name, |
| 225 | -1, |
| 226 | GRN_OPTION_REVISION_NONE, |
| 227 | options); |
| 228 | GRN_API_RETURN(ctx->rc); |
| 229 | } |
| 230 | |
| 231 | typedef struct { |
| 232 | const char *context_tag; |
| 233 | const char *module_name; |
| 234 | grn_info_type type; |
| 235 | size_t index; |
| 236 | grn_table_module_open_options_func open_options_func; |
| 237 | grn_close_func close_options_func; |
| 238 | void *user_data; |
| 239 | } grn_table_cache_data; |
| 240 | |
| 241 | static void * |
| 242 | grn_table_cache_module_options(grn_ctx *ctx, |
| 243 | grn_obj *table, |
| 244 | grn_table_cache_data *data) |
| 245 | { |
| 246 | grn_table_module *module = NULL; |
| 247 | grn_option_revision revision; |
| 248 | grn_obj raw_options; |
| 249 | void *options = NULL; |
| 250 | |
| 251 | GRN_API_ENTER; |
| 252 | |
| 253 | if (!grn_obj_is_table_with_key(ctx, table)) { |
| 254 | ERR(GRN_INVALID_ARGUMENT, |
| 255 | "[table][%s][options][cache] table must be key table: %s", |
| 256 | data->context_tag, |
| 257 | table ? grn_obj_type_to_string(table->header.type) : "(null)"); |
| 258 | GRN_API_RETURN(NULL); |
| 259 | } |
| 260 | |
| 261 | switch (data->type) { |
| 262 | case GRN_INFO_DEFAULT_TOKENIZER: |
| 263 | switch (table->header.type) { |
| 264 | case GRN_TABLE_HASH_KEY: |
| 265 | module = &(((grn_hash *)table)->tokenizer); |
| 266 | break; |
| 267 | case GRN_TABLE_PAT_KEY: |
| 268 | module = &(((grn_pat *)table)->tokenizer); |
| 269 | break; |
| 270 | case GRN_TABLE_DAT_KEY: |
| 271 | module = &(((grn_dat *)table)->tokenizer); |
| 272 | break; |
| 273 | default: |
| 274 | break; |
| 275 | } |
| 276 | break; |
| 277 | case GRN_INFO_NORMALIZER: |
| 278 | switch (table->header.type) { |
| 279 | case GRN_TABLE_HASH_KEY: |
| 280 | module = grn_table_modules_get_module(ctx, |
| 281 | &(((grn_hash *)table)->normalizers), |
| 282 | 0); |
| 283 | break; |
| 284 | case GRN_TABLE_PAT_KEY: |
| 285 | module = grn_table_modules_get_module(ctx, |
| 286 | &(((grn_pat *)table)->normalizers), |
| 287 | 0); |
| 288 | break; |
| 289 | case GRN_TABLE_DAT_KEY: |
| 290 | module = grn_table_modules_get_module(ctx, |
| 291 | &(((grn_dat *)table)->normalizers), |
| 292 | 0); |
| 293 | break; |
| 294 | default: |
| 295 | break; |
| 296 | } |
| 297 | break; |
| 298 | case GRN_INFO_TOKEN_FILTERS: |
| 299 | { |
| 300 | grn_obj *token_filters = NULL; |
| 301 | switch (table->header.type) { |
| 302 | case GRN_TABLE_HASH_KEY: |
| 303 | token_filters = &(((grn_hash *)table)->token_filters); |
| 304 | break; |
| 305 | case GRN_TABLE_PAT_KEY: |
| 306 | token_filters = &(((grn_pat *)table)->token_filters); |
| 307 | break; |
| 308 | case GRN_TABLE_DAT_KEY: |
| 309 | token_filters = &(((grn_dat *)table)->token_filters); |
| 310 | break; |
| 311 | default: |
| 312 | break; |
| 313 | } |
| 314 | module = ((grn_table_module *)GRN_BULK_HEAD(token_filters)) + data->index; |
| 315 | } |
| 316 | break; |
| 317 | case GRN_INFO_NORMALIZERS: |
| 318 | { |
| 319 | grn_obj *normalizers = NULL; |
| 320 | switch (table->header.type) { |
| 321 | case GRN_TABLE_HASH_KEY: |
| 322 | normalizers = &(((grn_hash *)table)->normalizers); |
| 323 | break; |
| 324 | case GRN_TABLE_PAT_KEY: |
| 325 | normalizers = &(((grn_pat *)table)->normalizers); |
| 326 | break; |
| 327 | case GRN_TABLE_DAT_KEY: |
| 328 | normalizers = &(((grn_dat *)table)->normalizers); |
| 329 | break; |
| 330 | default: |
| 331 | break; |
| 332 | } |
| 333 | module = ((grn_table_module *)GRN_BULK_HEAD(normalizers)) + data->index; |
| 334 | } |
| 335 | break; |
| 336 | case GRN_INFO_EXTRACTORS: |
| 337 | { |
| 338 | grn_obj *extractors = NULL; |
| 339 | switch (table->header.type) { |
| 340 | case GRN_TABLE_HASH_KEY: |
| 341 | extractors = &(((grn_hash *)table)->extractors); |
| 342 | break; |
| 343 | case GRN_TABLE_PAT_KEY: |
| 344 | extractors = &(((grn_pat *)table)->extractors); |
| 345 | break; |
| 346 | case GRN_TABLE_DAT_KEY: |
| 347 | extractors = &(((grn_dat *)table)->extractors); |
| 348 | break; |
| 349 | default: |
| 350 | break; |
| 351 | } |
| 352 | module = ((grn_table_module *)GRN_BULK_HEAD(extractors)) + data->index; |
| 353 | } |
| 354 | break; |
| 355 | default: |
| 356 | break; |
| 357 | } |
| 358 | |
| 359 | GRN_TEXT_INIT(&raw_options, GRN_OBJ_VECTOR); |
| 360 | if (module) { |
| 361 | CRITICAL_SECTION_ENTER(module->lock); |
| 362 | revision = grn_obj_get_option_values(ctx, |
| 363 | table, |
| 364 | data->module_name, |
| 365 | -1, |
| 366 | module->options_revision, |
| 367 | &raw_options); |
| 368 | bool need_update = true; |
| 369 | if (revision == GRN_OPTION_REVISION_UNCHANGED) { |
| 370 | need_update = false; |
| 371 | } |
| 372 | if (revision == GRN_OPTION_REVISION_NONE && module->options) { |
| 373 | need_update = false; |
| 374 | } |
| 375 | if (need_update) { |
| 376 | options = data->open_options_func(ctx, |
| 377 | module->proc, |
| 378 | &raw_options, |
| 379 | data->user_data); |
| 380 | if (ctx->rc == GRN_SUCCESS) { |
| 381 | grn_table_module_set_options_without_lock(ctx, |
| 382 | module, |
| 383 | options, |
| 384 | revision, |
| 385 | data->close_options_func); |
| 386 | } else if (options) { |
| 387 | data->close_options_func(ctx, options); |
| 388 | } |
| 389 | } |
| 390 | } |
| 391 | CRITICAL_SECTION_LEAVE(module->lock); |
| 392 | GRN_OBJ_FIN(ctx, &raw_options); |
| 393 | |
| 394 | GRN_API_RETURN(module->options); |
| 395 | } |
| 396 | |
| 397 | static void |
| 398 | grn_table_get_module_string_raw(grn_ctx *ctx, |
| 399 | grn_obj *table, |
| 400 | grn_obj *output, |
| 401 | grn_obj *proc, |
| 402 | const char *module_name) |
| 403 | { |
| 404 | char name[GRN_TABLE_MAX_KEY_SIZE]; |
| 405 | int name_size; |
| 406 | grn_obj options; |
| 407 | unsigned int n = 0; |
| 408 | |
| 409 | name_size = grn_obj_name(ctx, proc, name, GRN_TABLE_MAX_KEY_SIZE); |
| 410 | GRN_TEXT_PUT(ctx, output, name, name_size); |
| 411 | |
| 412 | GRN_VOID_INIT(&options); |
| 413 | grn_obj_get_option_values(ctx, |
| 414 | table, |
| 415 | module_name, |
| 416 | -1, |
| 417 | GRN_OPTION_REVISION_NONE, |
| 418 | &options); |
| 419 | if (options.header.type != GRN_DB_VOID) { |
| 420 | n = grn_vector_size(ctx, &options); |
| 421 | } |
| 422 | if (n > 0) { |
| 423 | unsigned int i; |
| 424 | grn_obj option; |
| 425 | |
| 426 | GRN_VOID_INIT(&option); |
| 427 | GRN_TEXT_PUTS(ctx, output, "("); |
| 428 | for (i = 0; i < n; i++) { |
| 429 | const char *value; |
| 430 | unsigned int value_size; |
| 431 | grn_id domain; |
| 432 | |
| 433 | if (i > 0) { |
| 434 | GRN_TEXT_PUTS(ctx, output, ", "); |
| 435 | } |
| 436 | |
| 437 | value_size = |
| 438 | grn_vector_get_element(ctx, &options, i, &value, NULL, &domain); |
| 439 | grn_obj_reinit(ctx, &option, domain, 0); |
| 440 | grn_bulk_write(ctx, &option, value, value_size); |
| 441 | grn_text_otoj(ctx, output, &option, NULL); |
| 442 | } |
| 443 | GRN_TEXT_PUTS(ctx, output, ")"); |
| 444 | GRN_OBJ_FIN(ctx, &option); |
| 445 | } |
| 446 | GRN_OBJ_FIN(ctx, &options); |
| 447 | } |
| 448 | |
| 449 | static bool |
| 450 | grn_table_using_normalizer(grn_ctx *ctx, grn_obj *table) |
| 451 | { |
| 452 | grn_id proc_id = GRN_ID_NIL; |
| 453 | switch (table->header.type) { |
| 454 | case GRN_TABLE_HASH_KEY: |
| 455 | proc_id = ((grn_hash *)table)->header.common->normalizer; |
| 456 | break; |
| 457 | case GRN_TABLE_PAT_KEY: |
| 458 | proc_id = ((grn_pat *)table)->header->normalizer; |
| 459 | break; |
| 460 | case GRN_TABLE_DAT_KEY: |
| 461 | proc_id = ((grn_dat *)table)->header->normalizer; |
| 462 | break; |
| 463 | default: |
| 464 | break; |
| 465 | } |
| 466 | return proc_id != GRN_ID_NIL; |
| 467 | } |
| 468 | |
| 469 | static void |
| 470 | grn_table_module_format_name(grn_ctx *ctx, |
| 471 | grn_obj *table, |
| 472 | const char *module_name, |
| 473 | uint32_t i, |
| 474 | grn_info_type type, |
| 475 | bool for_get, |
| 476 | char real_module_name[GRN_TABLE_MAX_KEY_SIZE]) |
| 477 | { |
| 478 | bool need_index = false; |
| 479 | switch (type) { |
| 480 | case GRN_INFO_NORMALIZER: |
| 481 | if (for_get) { |
| 482 | need_index = !grn_table_using_normalizer(ctx, table); |
| 483 | } |
| 484 | break; |
| 485 | case GRN_INFO_NORMALIZERS: |
| 486 | if (for_get) { |
| 487 | if (i == 0) { |
| 488 | need_index = !grn_table_using_normalizer(ctx, table); |
| 489 | } else { |
| 490 | need_index = true; |
| 491 | } |
| 492 | } else { |
| 493 | need_index = true; |
| 494 | } |
| 495 | break; |
| 496 | case GRN_INFO_TOKEN_FILTERS: |
| 497 | need_index = true; |
| 498 | break; |
| 499 | case GRN_INFO_EXTRACTORS: |
| 500 | need_index = true; |
| 501 | break; |
| 502 | default: |
| 503 | break; |
| 504 | } |
| 505 | |
| 506 | if (need_index) { |
| 507 | grn_snprintf(real_module_name, |
| 508 | GRN_TABLE_MAX_KEY_SIZE, |
| 509 | GRN_TABLE_MAX_KEY_SIZE, |
| 510 | "%s%u", |
| 511 | module_name, |
| 512 | i); |
| 513 | } else { |
| 514 | grn_snprintf(real_module_name, |
| 515 | GRN_TABLE_MAX_KEY_SIZE, |
| 516 | GRN_TABLE_MAX_KEY_SIZE, |
| 517 | "%s", |
| 518 | module_name); |
| 519 | } |
| 520 | } |
| 521 | |
| 522 | static grn_rc |
| 523 | grn_table_get_module_string(grn_ctx *ctx, |
| 524 | grn_obj *table, |
| 525 | grn_obj *output, |
| 526 | grn_info_type type, |
| 527 | const char *module_name, |
| 528 | const char *context_tag) |
| 529 | { |
| 530 | grn_obj *proc; |
| 531 | |
| 532 | GRN_API_ENTER; |
| 533 | |
| 534 | if (!grn_obj_is_table_with_key(ctx, table)) { |
| 535 | ERR(GRN_INVALID_ARGUMENT, |
| 536 | "[table][%s][options][string] table must be key table: %s", |
| 537 | context_tag, |
| 538 | table ? grn_obj_type_to_string(table->header.type) : "(null)"); |
| 539 | GRN_API_RETURN(ctx->rc); |
| 540 | } |
| 541 | |
| 542 | proc = grn_obj_get_info(ctx, table, type, NULL); |
| 543 | if (!proc) { |
| 544 | GRN_API_RETURN(ctx->rc); |
| 545 | } |
| 546 | |
| 547 | grn_table_get_module_string_raw(ctx, table, output, proc, module_name); |
| 548 | |
| 549 | GRN_API_RETURN(ctx->rc); |
| 550 | } |
| 551 | |
| 552 | static grn_rc |
| 553 | grn_table_get_modules_string(grn_ctx *ctx, |
| 554 | grn_obj *table, |
| 555 | grn_obj *output, |
| 556 | grn_info_type type, |
| 557 | const char *module_name, |
| 558 | const char *context_tag) |
| 559 | { |
| 560 | grn_obj procs; |
| 561 | unsigned int i, n; |
| 562 | |
| 563 | GRN_API_ENTER; |
| 564 | |
| 565 | if (!grn_obj_is_table_with_key(ctx, table)) { |
| 566 | ERR(GRN_INVALID_ARGUMENT, |
| 567 | "[table][%s][options][string] table must be key table: %s", |
| 568 | context_tag, |
| 569 | table ? grn_obj_type_to_string(table->header.type) : "(null)"); |
| 570 | GRN_API_RETURN(ctx->rc); |
| 571 | } |
| 572 | |
| 573 | GRN_PTR_INIT(&procs, GRN_OBJ_VECTOR, GRN_ID_NIL); |
| 574 | grn_obj_get_info(ctx, table, type, &procs); |
| 575 | if (GRN_BULK_VSIZE(&procs) == 0) { |
| 576 | GRN_OBJ_FIN(ctx, &procs); |
| 577 | GRN_API_RETURN(ctx->rc); |
| 578 | } |
| 579 | |
| 580 | n = grn_vector_size(ctx, &procs); |
| 581 | if (n == 0) { |
| 582 | GRN_API_RETURN(ctx->rc); |
| 583 | } |
| 584 | |
| 585 | for (i = 0; i < n; i++) { |
| 586 | char real_module_name[GRN_TABLE_MAX_KEY_SIZE]; |
| 587 | grn_obj *proc = GRN_PTR_VALUE_AT(&procs, i); |
| 588 | |
| 589 | if (i > 0) { |
| 590 | GRN_TEXT_PUTS(ctx, output, ", "); |
| 591 | } |
| 592 | grn_table_module_format_name(ctx, |
| 593 | table, |
| 594 | module_name, |
| 595 | i, |
| 596 | type, |
| 597 | true, |
| 598 | real_module_name); |
| 599 | grn_table_get_module_string_raw(ctx, table, output, proc, real_module_name); |
| 600 | } |
| 601 | |
| 602 | GRN_OBJ_FIN(ctx, &procs); |
| 603 | |
| 604 | GRN_API_RETURN(ctx->rc); |
| 605 | } |
| 606 | |
| 607 | grn_rc |
| 608 | grn_table_set_default_tokenizer_options(grn_ctx *ctx, |
| 609 | grn_obj *table, |
| 610 | grn_obj *options) |
| 611 | { |
| 612 | return grn_table_set_module_options(ctx, |
| 613 | table, |
| 614 | OPTION_NAME_DEFAULT_TOKENIZER, |
| 615 | options, |
| 616 | "default-tokenizer"); |
| 617 | } |
| 618 | |
| 619 | grn_rc |
| 620 | grn_table_get_default_tokenizer_options(grn_ctx *ctx, |
| 621 | grn_obj *table, |
| 622 | grn_obj *options) |
| 623 | { |
| 624 | return grn_table_get_module_options(ctx, |
| 625 | table, |
| 626 | OPTION_NAME_DEFAULT_TOKENIZER, |
| 627 | options, |
| 628 | "default-tokenizer"); |
| 629 | } |
| 630 | |
| 631 | void * |
| 632 | grn_table_cache_default_tokenizer_options( |
| 633 | grn_ctx *ctx, |
| 634 | grn_obj *table, |
| 635 | grn_table_module_open_options_func open_options_func, |
| 636 | grn_close_func close_options_func, |
| 637 | void *user_data) |
| 638 | { |
| 639 | grn_table_cache_data data; |
| 640 | |
| 641 | memset(&data, 0, sizeof(data)); |
| 642 | data.context_tag = "default-tokenizer"; |
| 643 | data.module_name = OPTION_NAME_DEFAULT_TOKENIZER; |
| 644 | data.type = GRN_INFO_DEFAULT_TOKENIZER; |
| 645 | data.open_options_func = open_options_func; |
| 646 | data.close_options_func = close_options_func; |
| 647 | data.user_data = user_data; |
| 648 | return grn_table_cache_module_options(ctx, table, &data); |
| 649 | } |
| 650 | |
| 651 | grn_rc |
| 652 | grn_table_get_default_tokenizer_string(grn_ctx *ctx, |
| 653 | grn_obj *table, |
| 654 | grn_obj *output) |
| 655 | { |
| 656 | return grn_table_get_module_string(ctx, |
| 657 | table, |
| 658 | output, |
| 659 | GRN_INFO_DEFAULT_TOKENIZER, |
| 660 | OPTION_NAME_DEFAULT_TOKENIZER, |
| 661 | "default-tokenizer"); |
| 662 | } |
| 663 | |
| 664 | grn_rc |
| 665 | grn_table_set_normalizer_options(grn_ctx *ctx, grn_obj *table, grn_obj *options) |
| 666 | { |
| 667 | return grn_table_set_module_options(ctx, |
| 668 | table, |
| 669 | OPTION_NAME_NORMALIZER, |
| 670 | options, |
| 671 | "normalizer"); |
| 672 | } |
| 673 | |
| 674 | grn_rc |
| 675 | grn_table_get_normalizer_options(grn_ctx *ctx, grn_obj *table, grn_obj *options) |
| 676 | { |
| 677 | char module_name[GRN_TABLE_MAX_KEY_SIZE]; |
| 678 | grn_table_module_format_name(ctx, |
| 679 | table, |
| 680 | OPTION_NAME_NORMALIZER, |
| 681 | 0, |
| 682 | GRN_INFO_NORMALIZER, |
| 683 | true, |
| 684 | module_name); |
| 685 | return grn_table_get_module_options(ctx, |
| 686 | table, |
| 687 | module_name, |
| 688 | options, |
| 689 | "normalizer"); |
| 690 | } |
| 691 | |
| 692 | void * |
| 693 | grn_table_cache_normalizer_options( |
| 694 | grn_ctx *ctx, |
| 695 | grn_obj *table, |
| 696 | grn_obj *string, |
| 697 | grn_table_module_open_options_func open_options_func, |
| 698 | grn_close_func close_options_func, |
| 699 | void *user_data) |
| 700 | { |
| 701 | grn_table_cache_data data; |
| 702 | |
| 703 | memset(&data, 0, sizeof(data)); |
| 704 | data.context_tag = "normalizer"; |
| 705 | uint32_t i = grn_string_get_normalizer_index(ctx, string); |
| 706 | char module_name[GRN_TABLE_MAX_KEY_SIZE]; |
| 707 | grn_table_module_format_name(ctx, |
| 708 | table, |
| 709 | OPTION_NAME_NORMALIZER, |
| 710 | i, |
| 711 | GRN_INFO_NORMALIZERS, |
| 712 | true, |
| 713 | module_name); |
| 714 | data.module_name = module_name; |
| 715 | data.type = GRN_INFO_NORMALIZER; |
| 716 | data.open_options_func = open_options_func; |
| 717 | data.close_options_func = close_options_func; |
| 718 | data.user_data = user_data; |
| 719 | return grn_table_cache_module_options(ctx, table, &data); |
| 720 | } |
| 721 | |
| 722 | grn_rc |
| 723 | grn_table_get_normalizer_string(grn_ctx *ctx, grn_obj *table, grn_obj *output) |
| 724 | { |
| 725 | char module_name[GRN_TABLE_MAX_KEY_SIZE]; |
| 726 | grn_table_module_format_name(ctx, |
| 727 | table, |
| 728 | OPTION_NAME_NORMALIZER, |
| 729 | 0, |
| 730 | GRN_INFO_NORMALIZER, |
| 731 | true, |
| 732 | module_name); |
| 733 | return grn_table_get_module_string(ctx, |
| 734 | table, |
| 735 | output, |
| 736 | GRN_INFO_NORMALIZER, |
| 737 | module_name, |
| 738 | "normalizer"); |
| 739 | } |
| 740 | |
| 741 | grn_rc |
| 742 | grn_table_set_normalizers_options(grn_ctx *ctx, |
| 743 | grn_obj *table, |
| 744 | uint32_t i, |
| 745 | grn_obj *options) |
| 746 | { |
| 747 | char module_name[GRN_TABLE_MAX_KEY_SIZE]; |
| 748 | grn_table_module_format_name(ctx, |
| 749 | table, |
| 750 | OPTION_NAME_NORMALIZER, |
| 751 | i, |
| 752 | GRN_INFO_NORMALIZERS, |
| 753 | false, |
| 754 | module_name); |
| 755 | return grn_table_set_module_options(ctx, |
| 756 | table, |
| 757 | module_name, |
| 758 | options, |
| 759 | "normalizers"); |
| 760 | } |
| 761 | |
| 762 | grn_rc |
| 763 | grn_table_get_normalizers_options(grn_ctx *ctx, |
| 764 | grn_obj *table, |
| 765 | uint32_t i, |
| 766 | grn_obj *options) |
| 767 | { |
| 768 | char module_name[GRN_TABLE_MAX_KEY_SIZE]; |
| 769 | grn_table_module_format_name(ctx, |
| 770 | table, |
| 771 | OPTION_NAME_NORMALIZER, |
| 772 | i, |
| 773 | GRN_INFO_NORMALIZER, |
| 774 | true, |
| 775 | module_name); |
| 776 | return grn_table_get_module_options(ctx, |
| 777 | table, |
| 778 | module_name, |
| 779 | options, |
| 780 | "normalizers"); |
| 781 | } |
| 782 | |
| 783 | void * |
| 784 | grn_table_cache_normalizers_options( |
| 785 | grn_ctx *ctx, |
| 786 | grn_obj *table, |
| 787 | uint32_t i, |
| 788 | grn_table_module_open_options_func open_options_func, |
| 789 | grn_close_func close_options_func, |
| 790 | void *user_data) |
| 791 | { |
| 792 | grn_table_cache_data data; |
| 793 | char module_name[GRN_TABLE_MAX_KEY_SIZE]; |
| 794 | grn_table_module_format_name(ctx, |
| 795 | table, |
| 796 | OPTION_NAME_NORMALIZER, |
| 797 | i, |
| 798 | GRN_INFO_NORMALIZERS, |
| 799 | true, |
| 800 | module_name); |
| 801 | |
| 802 | memset(&data, 0, sizeof(data)); |
| 803 | data.context_tag = "normalizers"; |
| 804 | data.module_name = module_name; |
| 805 | data.type = GRN_INFO_NORMALIZERS; |
| 806 | data.index = i; |
| 807 | data.open_options_func = open_options_func; |
| 808 | data.close_options_func = close_options_func; |
| 809 | data.user_data = user_data; |
| 810 | return grn_table_cache_module_options(ctx, table, &data); |
| 811 | } |
| 812 | |
| 813 | grn_rc |
| 814 | grn_table_get_normalizers_string(grn_ctx *ctx, grn_obj *table, grn_obj *output) |
| 815 | { |
| 816 | return grn_table_get_modules_string(ctx, |
| 817 | table, |
| 818 | output, |
| 819 | GRN_INFO_NORMALIZERS, |
| 820 | OPTION_NAME_NORMALIZER, |
| 821 | "normalizers"); |
| 822 | } |
| 823 | |
| 824 | grn_rc |
| 825 | grn_table_set_token_filter_options(grn_ctx *ctx, |
| 826 | grn_obj *table, |
| 827 | unsigned int i, |
| 828 | grn_obj *options) |
| 829 | { |
| 830 | return grn_table_set_token_filters_options(ctx, table, i, options); |
| 831 | } |
| 832 | |
| 833 | grn_rc |
| 834 | grn_table_set_token_filters_options(grn_ctx *ctx, |
| 835 | grn_obj *table, |
| 836 | uint32_t i, |
| 837 | grn_obj *options) |
| 838 | { |
| 839 | char module_name[GRN_TABLE_MAX_KEY_SIZE]; |
| 840 | grn_table_module_format_name(ctx, |
| 841 | table, |
| 842 | OPTION_NAME_TOKEN_FILTER, |
| 843 | i, |
| 844 | GRN_INFO_TOKEN_FILTERS, |
| 845 | false, |
| 846 | module_name); |
| 847 | return grn_table_set_module_options(ctx, |
| 848 | table, |
| 849 | module_name, |
| 850 | options, |
| 851 | "token-filters"); |
| 852 | } |
| 853 | |
| 854 | grn_rc |
| 855 | grn_table_get_token_filter_options(grn_ctx *ctx, |
| 856 | grn_obj *table, |
| 857 | unsigned int i, |
| 858 | grn_obj *options) |
| 859 | { |
| 860 | return grn_table_get_token_filters_options(ctx, table, i, options); |
| 861 | } |
| 862 | |
| 863 | grn_rc |
| 864 | grn_table_get_token_filters_options(grn_ctx *ctx, |
| 865 | grn_obj *table, |
| 866 | uint32_t i, |
| 867 | grn_obj *options) |
| 868 | { |
| 869 | char module_name[GRN_TABLE_MAX_KEY_SIZE]; |
| 870 | grn_table_module_format_name(ctx, |
| 871 | table, |
| 872 | OPTION_NAME_TOKEN_FILTER, |
| 873 | i, |
| 874 | GRN_INFO_TOKEN_FILTERS, |
| 875 | true, |
| 876 | module_name); |
| 877 | return grn_table_get_module_options(ctx, |
| 878 | table, |
| 879 | module_name, |
| 880 | options, |
| 881 | "token-filters"); |
| 882 | } |
| 883 | |
| 884 | void * |
| 885 | grn_table_cache_token_filter_options( |
| 886 | grn_ctx *ctx, |
| 887 | grn_obj *table, |
| 888 | unsigned int i, |
| 889 | grn_table_module_open_options_func open_options_func, |
| 890 | grn_close_func close_options_func, |
| 891 | void *user_data) |
| 892 | { |
| 893 | return grn_table_cache_token_filters_options(ctx, |
| 894 | table, |
| 895 | i, |
| 896 | open_options_func, |
| 897 | close_options_func, |
| 898 | user_data); |
| 899 | } |
| 900 | |
| 901 | void * |
| 902 | grn_table_cache_token_filters_options( |
| 903 | grn_ctx *ctx, |
| 904 | grn_obj *table, |
| 905 | uint32_t i, |
| 906 | grn_table_module_open_options_func open_options_func, |
| 907 | grn_close_func close_options_func, |
| 908 | void *user_data) |
| 909 | { |
| 910 | grn_table_cache_data data; |
| 911 | char module_name[GRN_TABLE_MAX_KEY_SIZE]; |
| 912 | grn_table_module_format_name(ctx, |
| 913 | table, |
| 914 | OPTION_NAME_TOKEN_FILTER, |
| 915 | i, |
| 916 | GRN_INFO_TOKEN_FILTERS, |
| 917 | true, |
| 918 | module_name); |
| 919 | |
| 920 | memset(&data, 0, sizeof(data)); |
| 921 | data.context_tag = "token-filter"; |
| 922 | data.module_name = module_name; |
| 923 | data.type = GRN_INFO_TOKEN_FILTERS; |
| 924 | data.index = i; |
| 925 | data.open_options_func = open_options_func; |
| 926 | data.close_options_func = close_options_func; |
| 927 | data.user_data = user_data; |
| 928 | return grn_table_cache_module_options(ctx, table, &data); |
| 929 | } |
| 930 | |
| 931 | grn_rc |
| 932 | grn_table_get_token_filters_string(grn_ctx *ctx, |
| 933 | grn_obj *table, |
| 934 | grn_obj *output) |
| 935 | { |
| 936 | return grn_table_get_modules_string(ctx, |
| 937 | table, |
| 938 | output, |
| 939 | GRN_INFO_TOKEN_FILTERS, |
| 940 | OPTION_NAME_TOKEN_FILTER, |
| 941 | "token-filters"); |
| 942 | } |
| 943 | |
| 944 | grn_rc |
| 945 | grn_table_set_extractors_options(grn_ctx *ctx, |
| 946 | grn_obj *table, |
| 947 | uint32_t i, |
| 948 | grn_obj *options) |
| 949 | { |
| 950 | char module_name[GRN_TABLE_MAX_KEY_SIZE]; |
| 951 | grn_table_module_format_name(ctx, |
| 952 | table, |
| 953 | OPTION_NAME_EXTRACTOR, |
| 954 | i, |
| 955 | GRN_INFO_EXTRACTORS, |
| 956 | false, |
| 957 | module_name); |
| 958 | return grn_table_set_module_options(ctx, |
| 959 | table, |
| 960 | module_name, |
| 961 | options, |
| 962 | "extractors"); |
| 963 | } |
| 964 | |
| 965 | grn_rc |
| 966 | grn_table_get_extractors_options(grn_ctx *ctx, |
| 967 | grn_obj *table, |
| 968 | uint32_t i, |
| 969 | grn_obj *options) |
| 970 | { |
| 971 | char module_name[GRN_TABLE_MAX_KEY_SIZE]; |
| 972 | grn_table_module_format_name(ctx, |
| 973 | table, |
| 974 | OPTION_NAME_EXTRACTOR, |
| 975 | i, |
| 976 | GRN_INFO_EXTRACTORS, |
| 977 | true, |
| 978 | module_name); |
| 979 | return grn_table_get_module_options(ctx, |
| 980 | table, |
| 981 | module_name, |
| 982 | options, |
| 983 | "extractors"); |
| 984 | } |
| 985 | |
| 986 | void * |
| 987 | grn_table_cache_extractors_options( |
| 988 | grn_ctx *ctx, |
| 989 | grn_obj *table, |
| 990 | uint32_t i, |
| 991 | grn_table_module_open_options_func open_options_func, |
| 992 | grn_close_func close_options_func, |
| 993 | void *user_data) |
| 994 | { |
| 995 | grn_table_cache_data data; |
| 996 | char module_name[GRN_TABLE_MAX_KEY_SIZE]; |
| 997 | grn_table_module_format_name(ctx, |
| 998 | table, |
| 999 | OPTION_NAME_EXTRACTOR, |
| 1000 | i, |
| 1001 | GRN_INFO_EXTRACTORS, |
| 1002 | true, |
| 1003 | module_name); |
| 1004 | |
| 1005 | memset(&data, 0, sizeof(data)); |
| 1006 | data.context_tag = "extractor"; |
| 1007 | data.module_name = module_name; |
| 1008 | data.type = GRN_INFO_EXTRACTORS; |
| 1009 | data.index = i; |
| 1010 | data.open_options_func = open_options_func; |
| 1011 | data.close_options_func = close_options_func; |
| 1012 | data.user_data = user_data; |
| 1013 | return grn_table_cache_module_options(ctx, table, &data); |
| 1014 | } |
| 1015 | |
| 1016 | grn_rc |
| 1017 | grn_table_get_extractors_string(grn_ctx *ctx, grn_obj *table, grn_obj *output) |
| 1018 | { |
| 1019 | return grn_table_get_modules_string(ctx, |
| 1020 | table, |
| 1021 | output, |
| 1022 | GRN_INFO_EXTRACTORS, |
| 1023 | OPTION_NAME_EXTRACTOR, |
| 1024 | "extractors"); |
| 1025 | } |