| 1 | /* |
| 2 | Copyright (C) 2009-2018 Brazil |
| 3 | Copyright (C) 2018-2026 Sutou Kouhei <[email protected]> |
| 4 | Copyright (C) 2021 Horimoto Yasuhiro <[email protected]> |
| 5 | |
| 6 | This library is free software; you can redistribute it and/or |
| 7 | modify it under the terms of the GNU Lesser General Public |
| 8 | License as published by the Free Software Foundation; either |
| 9 | version 2.1 of the License, or (at your option) any later version. |
| 10 | |
| 11 | This library is distributed in the hope that it will be useful, |
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | Lesser General Public License for more details. |
| 15 | |
| 16 | You should have received a copy of the GNU Lesser General Public |
| 17 | License along with this library; if not, write to the Free Software |
| 18 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 19 | */ |
| 20 | |
| 21 | #include "grn_cache.h" |
| 22 | #include "grn_ctx.h" |
| 23 | #include "grn_ctx_impl.h" |
| 24 | #include "grn_db.h" |
| 25 | #include "grn_expr.h" |
| 26 | #include "grn_float.h" |
| 27 | #include "grn_geo.h" |
| 28 | #include "grn_ii.h" |
| 29 | #include "grn_load.h" |
| 30 | #include "grn_output.h" |
| 31 | #include "grn_pat.h" |
| 32 | #include "grn_posting.h" |
| 33 | #include "grn_proc.h" |
| 34 | #include "grn_report.h" |
| 35 | #include "grn_str.h" |
| 36 | #include "grn_util.h" |
| 37 | |
| 38 | #include <string.h> |
| 39 | #include <stdlib.h> |
| 40 | #include <fcntl.h> |
| 41 | #include <sys/stat.h> |
| 42 | |
| 43 | #ifdef WIN32 |
| 44 | # include <io.h> |
| 45 | # include <share.h> |
| 46 | #endif /* WIN32 */ |
| 47 | |
| 48 | #ifdef GRN_WITH_APACHE_ARROW |
| 49 | # include <arrow/util/config.h> |
| 50 | #endif |
| 51 | |
| 52 | #ifndef O_NOFOLLOW |
| 53 | # define O_NOFOLLOW 0 |
| 54 | #endif |
| 55 | |
| 56 | /**** globals for procs ****/ |
| 57 | const char *grn_document_root = NULL; |
| 58 | |
| 59 | #define VAR GRN_PROC_GET_VAR_BY_OFFSET |
| 60 | |
| 61 | static double grn_between_too_many_index_match_ratio = 0.01; |
| 62 | static double grn_in_values_too_many_index_match_ratio = 0.01; |
| 63 | static int32_t grn_sub_filter_pre_filter_threshold = 10; |
| 64 | |
| 65 | void |
| 66 | grn_proc_init_from_env(void) |
| 67 | { |
| 68 | { |
| 69 | char grn_between_too_many_index_match_ratio_env[GRN_ENV_BUFFER_SIZE]; |
| 70 | grn_getenv("GRN_BETWEEN_TOO_MANY_INDEX_MATCH_RATIO", |
| 71 | grn_between_too_many_index_match_ratio_env, |
| 72 | GRN_ENV_BUFFER_SIZE); |
| 73 | if (grn_between_too_many_index_match_ratio_env[0]) { |
| 74 | grn_between_too_many_index_match_ratio = |
| 75 | atof(grn_between_too_many_index_match_ratio_env); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | { |
| 80 | char grn_in_values_too_many_index_match_ratio_env[GRN_ENV_BUFFER_SIZE]; |
| 81 | grn_getenv("GRN_IN_VALUES_TOO_MANY_INDEX_MATCH_RATIO", |
| 82 | grn_in_values_too_many_index_match_ratio_env, |
| 83 | GRN_ENV_BUFFER_SIZE); |
| 84 | if (grn_in_values_too_many_index_match_ratio_env[0]) { |
| 85 | grn_in_values_too_many_index_match_ratio = |
| 86 | atof(grn_in_values_too_many_index_match_ratio_env); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | { |
| 91 | char env[GRN_ENV_BUFFER_SIZE]; |
| 92 | grn_getenv("GRN_SUB_FILTER_PRE_FILTER_THRESHOLD", env, GRN_ENV_BUFFER_SIZE); |
| 93 | if (env[0]) { |
| 94 | grn_sub_filter_pre_filter_threshold = |
| 95 | grn_atoi(env, env + strlen(env), NULL); |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | /* bulk must be initialized grn_bulk or grn_msg */ |
| 101 | static int |
| 102 | grn_bulk_put_from_file(grn_ctx *ctx, grn_obj *bulk, const char *path) |
| 103 | { |
| 104 | /* FIXME: implement more smartly with grn_bulk */ |
| 105 | int fd, ret = 0; |
| 106 | struct stat stat; |
| 107 | grn_open(fd, path, O_RDONLY | O_NOFOLLOW | GRN_OPEN_FLAG_BINARY); |
| 108 | if (fd == -1) { |
| 109 | switch (errno) { |
| 110 | case EACCES: |
| 111 | ERR(GRN_OPERATION_NOT_PERMITTED, "request is not allowed: <%s>", path); |
| 112 | break; |
| 113 | case ENOENT: |
| 114 | ERR(GRN_NO_SUCH_FILE_OR_DIRECTORY, "no such file: <%s>", path); |
| 115 | break; |
| 116 | #ifndef WIN32 |
| 117 | case ELOOP: |
| 118 | ERR(GRN_NO_SUCH_FILE_OR_DIRECTORY, |
| 119 | "symbolic link is not allowed: <%s>", |
| 120 | path); |
| 121 | break; |
| 122 | #endif /* WIN32 */ |
| 123 | default: |
| 124 | ERRNO_ERR("failed to open file: <%s>", path); |
| 125 | break; |
| 126 | } |
| 127 | return 0; |
| 128 | } |
| 129 | if (fstat(fd, &stat) != -1) { |
| 130 | char *buf, *bp; |
| 131 | off_t rest = stat.st_size; |
| 132 | if ((buf = GRN_MALLOC(rest))) { |
| 133 | ssize_t ss; |
| 134 | for (bp = buf; rest; rest -= ss, bp += ss) { |
| 135 | if ((ss = grn_read(fd, bp, rest)) == -1) { |
| 136 | goto exit; |
| 137 | } |
| 138 | } |
| 139 | GRN_TEXT_PUT(ctx, bulk, buf, stat.st_size); |
| 140 | ret = 1; |
| 141 | } |
| 142 | GRN_FREE(buf); |
| 143 | } else { |
| 144 | ERR(GRN_INVALID_ARGUMENT, "cannot stat file: <%s>", path); |
| 145 | } |
| 146 | exit: |
| 147 | grn_close(fd); |
| 148 | return ret; |
| 149 | } |
| 150 | |
| 151 | #ifdef stat |
| 152 | # undef stat |
| 153 | #endif /* stat */ |
| 154 | |
| 155 | grn_rc |
| 156 | grn_proc_prefixed_options_parse(grn_ctx *ctx, |
| 157 | grn_obj *options, |
| 158 | const char *prefix, |
| 159 | const char *tag, |
| 160 | const char *name, |
| 161 | ...) |
| 162 | { |
| 163 | va_list args; |
| 164 | |
| 165 | va_start(args, name); |
| 166 | grn_rc rc = |
| 167 | grn_proc_prefixed_options_parsev(ctx, options, prefix, tag, name, args); |
| 168 | va_end(args); |
| 169 | |
| 170 | return rc; |
| 171 | } |
| 172 | |
| 173 | grn_rc |
| 174 | grn_proc_prefixed_options_parsev(grn_ctx *ctx, |
| 175 | grn_obj *options, |
| 176 | const char *prefix, |
| 177 | const char *tag, |
| 178 | const char *name, |
| 179 | va_list args) |
| 180 | { |
| 181 | GRN_API_ENTER; |
| 182 | |
| 183 | grn_obj func_tag_buffer; |
| 184 | GRN_TEXT_INIT(&func_tag_buffer, 0); |
| 185 | grn_text_printf(ctx, &func_tag_buffer, "%s[options][parse]", tag); |
| 186 | GRN_TEXT_PUTC(ctx, &func_tag_buffer, '\0'); |
| 187 | const char *func_tag = GRN_TEXT_VALUE(&func_tag_buffer); |
| 188 | |
| 189 | grn_obj full_name; |
| 190 | GRN_TEXT_INIT(&full_name, 0); |
| 191 | |
| 192 | grn_obj used_ids; |
| 193 | GRN_RECORD_INIT(&used_ids, GRN_OBJ_VECTOR, GRN_ID_NIL); |
| 194 | |
| 195 | if (options->header.type != GRN_TABLE_HASH_KEY) { |
| 196 | grn_obj inspected; |
| 197 | GRN_TEXT_INIT(&inspected, 0); |
| 198 | grn_inspect(ctx, &inspected, options); |
| 199 | GRN_PLUGIN_ERROR(ctx, |
| 200 | GRN_INVALID_ARGUMENT, |
| 201 | "%s options must be a hash table: %.*s", |
| 202 | func_tag, |
| 203 | (int)GRN_TEXT_LEN(&inspected), |
| 204 | GRN_TEXT_VALUE(&inspected)); |
| 205 | GRN_OBJ_FIN(ctx, &inspected); |
| 206 | goto exit; |
| 207 | } |
| 208 | |
| 209 | const size_t n_specified_options = grn_table_size(ctx, options); |
| 210 | while (name && GRN_RECORD_VECTOR_SIZE(&used_ids) < n_specified_options) { |
| 211 | grn_proc_option_value_type type = va_arg(args, grn_proc_option_value_type); |
| 212 | void *value_raw = NULL; |
| 213 | GRN_TEXT_SETS(ctx, &full_name, prefix); |
| 214 | GRN_TEXT_PUTS(ctx, &full_name, name); |
| 215 | GRN_TEXT_PUTC(ctx, &full_name, '\0'); |
| 216 | grn_id id = grn_hash_get(ctx, |
| 217 | (grn_hash *)options, |
| 218 | GRN_TEXT_VALUE(&full_name), |
| 219 | GRN_TEXT_LEN(&full_name) - 1, |
| 220 | &value_raw); |
| 221 | grn_obj *value = value_raw; |
| 222 | switch (type) { |
| 223 | case GRN_PROC_OPTION_VALUE_RAW: |
| 224 | { |
| 225 | grn_obj **raw = va_arg(args, grn_obj **); |
| 226 | if (id != GRN_ID_NIL) { |
| 227 | GRN_RECORD_PUT(ctx, &used_ids, id); |
| 228 | *raw = value; |
| 229 | } |
| 230 | } |
| 231 | break; |
| 232 | case GRN_PROC_OPTION_VALUE_MODE: |
| 233 | { |
| 234 | grn_operator *mode = va_arg(args, grn_operator *); |
| 235 | if (id != GRN_ID_NIL) { |
| 236 | GRN_RECORD_PUT(ctx, &used_ids, id); |
| 237 | *mode = grn_proc_get_value_mode(ctx, value, *mode, func_tag); |
| 238 | if (ctx->rc != GRN_SUCCESS) { |
| 239 | goto exit; |
| 240 | } |
| 241 | } |
| 242 | } |
| 243 | break; |
| 244 | case GRN_PROC_OPTION_VALUE_OPERATOR: |
| 245 | { |
| 246 | grn_operator *op = va_arg(args, grn_operator *); |
| 247 | if (id != GRN_ID_NIL) { |
| 248 | GRN_RECORD_PUT(ctx, &used_ids, id); |
| 249 | *op = grn_proc_get_value_operator(ctx, value, *op, func_tag); |
| 250 | if (ctx->rc != GRN_SUCCESS) { |
| 251 | goto exit; |
| 252 | } |
| 253 | } |
| 254 | } |
| 255 | break; |
| 256 | case GRN_PROC_OPTION_VALUE_EXPR_FLAGS: |
| 257 | { |
| 258 | grn_expr_flags *flags = va_arg(args, grn_expr_flags *); |
| 259 | if (id != GRN_ID_NIL) { |
| 260 | GRN_RECORD_PUT(ctx, &used_ids, id); |
| 261 | *flags = grn_proc_expr_query_flags_parse(ctx, value, func_tag); |
| 262 | if (ctx->rc != GRN_SUCCESS) { |
| 263 | goto exit; |
| 264 | } |
| 265 | } |
| 266 | } |
| 267 | break; |
| 268 | case GRN_PROC_OPTION_VALUE_INT32: |
| 269 | { |
| 270 | int32_t *number = va_arg(args, int32_t *); |
| 271 | if (id != GRN_ID_NIL) { |
| 272 | GRN_RECORD_PUT(ctx, &used_ids, id); |
| 273 | *number = grn_proc_get_value_int32(ctx, value, *number, func_tag); |
| 274 | if (ctx->rc != GRN_SUCCESS) { |
| 275 | goto exit; |
| 276 | } |
| 277 | } |
| 278 | } |
| 279 | break; |
| 280 | case GRN_PROC_OPTION_VALUE_UINT32: |
| 281 | { |
| 282 | uint32_t *number = va_arg(args, uint32_t *); |
| 283 | if (id != GRN_ID_NIL) { |
| 284 | GRN_RECORD_PUT(ctx, &used_ids, id); |
| 285 | *number = grn_proc_get_value_uint32(ctx, value, *number, func_tag); |
| 286 | if (ctx->rc != GRN_SUCCESS) { |
| 287 | goto exit; |
| 288 | } |
| 289 | } |
| 290 | } |
| 291 | break; |
| 292 | case GRN_PROC_OPTION_VALUE_INT64: |
| 293 | { |
| 294 | int64_t *number = va_arg(args, int64_t *); |
| 295 | if (id != GRN_ID_NIL) { |
| 296 | GRN_RECORD_PUT(ctx, &used_ids, id); |
| 297 | *number = grn_proc_get_value_int64(ctx, value, *number, func_tag); |
| 298 | if (ctx->rc != GRN_SUCCESS) { |
| 299 | goto exit; |
| 300 | } |
| 301 | } |
| 302 | } |
| 303 | break; |
| 304 | case GRN_PROC_OPTION_VALUE_BOOL: |
| 305 | { |
| 306 | bool *b = va_arg(args, bool *); |
| 307 | if (id != GRN_ID_NIL) { |
| 308 | GRN_RECORD_PUT(ctx, &used_ids, id); |
| 309 | *b = grn_proc_get_value_bool(ctx, value, *b, func_tag); |
| 310 | if (ctx->rc != GRN_SUCCESS) { |
| 311 | goto exit; |
| 312 | } |
| 313 | } |
| 314 | } |
| 315 | break; |
| 316 | case GRN_PROC_OPTION_VALUE_FUNC: |
| 317 | { |
| 318 | grn_proc_option_value_parse_func func = |
| 319 | va_arg(args, grn_proc_option_value_parse_func); |
| 320 | void *user_data = va_arg(args, void *); |
| 321 | if (id != GRN_ID_NIL) { |
| 322 | GRN_RECORD_PUT(ctx, &used_ids, id); |
| 323 | grn_rc rc = |
| 324 | func(ctx, GRN_TEXT_VALUE(&full_name), value, func_tag, user_data); |
| 325 | if (rc != GRN_SUCCESS) { |
| 326 | if (ctx->rc == GRN_SUCCESS) { |
| 327 | grn_obj inspected; |
| 328 | GRN_TEXT_INIT(&inspected, 0); |
| 329 | grn_inspect(ctx, &inspected, value); |
| 330 | GRN_PLUGIN_ERROR(ctx, |
| 331 | rc, |
| 332 | "%s[%s] failed to parse: <%.*s>", |
| 333 | func_tag, |
| 334 | name, |
| 335 | (int)GRN_TEXT_LEN(&inspected), |
| 336 | GRN_TEXT_VALUE(&inspected)); |
| 337 | GRN_OBJ_FIN(ctx, &inspected); |
| 338 | } |
| 339 | goto exit; |
| 340 | } |
| 341 | } |
| 342 | } |
| 343 | break; |
| 344 | case GRN_PROC_OPTION_VALUE_TOKENIZE_MODE: |
| 345 | { |
| 346 | grn_tokenize_mode *mode = va_arg(args, grn_tokenize_mode *); |
| 347 | if (id != GRN_ID_NIL) { |
| 348 | GRN_RECORD_PUT(ctx, &used_ids, id); |
| 349 | *mode = grn_proc_get_value_tokenize_mode(ctx, value, *mode, func_tag); |
| 350 | if (ctx->rc != GRN_SUCCESS) { |
| 351 | goto exit; |
| 352 | } |
| 353 | } |
| 354 | } |
| 355 | break; |
| 356 | case GRN_PROC_OPTION_VALUE_TOKEN_CURSOR_FLAGS: |
| 357 | { |
| 358 | uint32_t *flags = va_arg(args, uint32_t *); |
| 359 | if (id != GRN_ID_NIL) { |
| 360 | GRN_RECORD_PUT(ctx, &used_ids, id); |
| 361 | *flags = |
| 362 | grn_proc_get_value_token_cursor_flags(ctx, value, *flags, func_tag); |
| 363 | if (ctx->rc != GRN_SUCCESS) { |
| 364 | goto exit; |
| 365 | } |
| 366 | } |
| 367 | } |
| 368 | break; |
| 369 | case GRN_PROC_OPTION_VALUE_DOUBLE: |
| 370 | { |
| 371 | double *number = va_arg(args, double *); |
| 372 | if (id != GRN_ID_NIL) { |
| 373 | GRN_RECORD_PUT(ctx, &used_ids, id); |
| 374 | *number = grn_proc_get_value_double(ctx, value, *number, func_tag); |
| 375 | if (ctx->rc != GRN_SUCCESS) { |
| 376 | goto exit; |
| 377 | } |
| 378 | } |
| 379 | } |
| 380 | break; |
| 381 | case GRN_PROC_OPTION_VALUE_RAW_STRING: |
| 382 | { |
| 383 | grn_raw_string *string = va_arg(args, grn_raw_string *); |
| 384 | if (id != GRN_ID_NIL) { |
| 385 | GRN_RECORD_PUT(ctx, &used_ids, id); |
| 386 | *string = grn_proc_get_value_raw_string(ctx, value, string, func_tag); |
| 387 | if (ctx->rc != GRN_SUCCESS) { |
| 388 | goto exit; |
| 389 | } |
| 390 | } |
| 391 | } |
| 392 | break; |
| 393 | default: |
| 394 | GRN_PLUGIN_ERROR(ctx, |
| 395 | GRN_INVALID_ARGUMENT, |
| 396 | "%s[%s] invalid option value type: %d", |
| 397 | func_tag, |
| 398 | GRN_TEXT_VALUE(&full_name), |
| 399 | type); |
| 400 | goto exit; |
| 401 | } |
| 402 | name = va_arg(args, const char *); |
| 403 | } |
| 404 | |
| 405 | const size_t n_used_options = GRN_RECORD_VECTOR_SIZE(&used_ids); |
| 406 | if (n_used_options == n_specified_options) { |
| 407 | goto exit; |
| 408 | } |
| 409 | |
| 410 | { |
| 411 | grn_obj message; |
| 412 | GRN_TEXT_INIT(&message, 0); |
| 413 | GRN_HASH_EACH_BEGIN(ctx, (grn_hash *)options, cursor, id) |
| 414 | { |
| 415 | bool is_used_option = false; |
| 416 | size_t i; |
| 417 | for (i = 0; i < n_used_options; i++) { |
| 418 | if (GRN_RECORD_VALUE_AT(&used_ids, i) == id) { |
| 419 | is_used_option = true; |
| 420 | break; |
| 421 | } |
| 422 | } |
| 423 | if (is_used_option) { |
| 424 | continue; |
| 425 | } |
| 426 | |
| 427 | void *key; |
| 428 | int key_size = grn_hash_cursor_get_key(ctx, cursor, &key); |
| 429 | grn_raw_string key_string; |
| 430 | key_string.value = key; |
| 431 | key_string.length = key_size; |
| 432 | if (!GRN_RAW_STRING_START_WITH_CSTRING(key_string, prefix)) { |
| 433 | continue; |
| 434 | } |
| 435 | if (GRN_TEXT_LEN(&message) > 0) { |
| 436 | GRN_TEXT_PUTS(ctx, &message, ", "); |
| 437 | } |
| 438 | grn_text_printf(ctx, &message, "<%.*s>", key_size, (char *)key); |
| 439 | } |
| 440 | GRN_HASH_EACH_END(ctx, cursor); |
| 441 | if (GRN_TEXT_LEN(&message) > 0) { |
| 442 | grn_obj inspected; |
| 443 | GRN_TEXT_INIT(&inspected, 0); |
| 444 | grn_inspect(ctx, &inspected, options); |
| 445 | GRN_PLUGIN_ERROR(ctx, |
| 446 | GRN_INVALID_ARGUMENT, |
| 447 | "%s unknown option names: %.*s: %.*s", |
| 448 | func_tag, |
| 449 | (int)GRN_TEXT_LEN(&message), |
| 450 | GRN_TEXT_VALUE(&message), |
| 451 | (int)GRN_TEXT_LEN(&inspected), |
| 452 | GRN_TEXT_VALUE(&inspected)); |
| 453 | GRN_OBJ_FIN(ctx, &inspected); |
| 454 | } |
| 455 | GRN_OBJ_FIN(ctx, &message); |
| 456 | } |
| 457 | |
| 458 | exit: |
| 459 | GRN_OBJ_FIN(ctx, &used_ids); |
| 460 | GRN_OBJ_FIN(ctx, &full_name); |
| 461 | GRN_OBJ_FIN(ctx, &func_tag_buffer); |
| 462 | GRN_API_RETURN(ctx->rc); |
| 463 | } |
| 464 | |
| 465 | grn_rc |
| 466 | grn_proc_options_parse( |
| 467 | grn_ctx *ctx, grn_obj *options, const char *tag, const char *name, ...) |
| 468 | { |
| 469 | va_list args; |
| 470 | |
| 471 | va_start(args, name); |
| 472 | grn_rc rc = grn_proc_options_parsev(ctx, options, tag, name, args); |
| 473 | va_end(args); |
| 474 | |
| 475 | return rc; |
| 476 | } |
| 477 | |
| 478 | grn_rc |
| 479 | grn_proc_options_parsev(grn_ctx *ctx, |
| 480 | grn_obj *options, |
| 481 | const char *tag, |
| 482 | const char *name, |
| 483 | va_list args) |
| 484 | { |
| 485 | return grn_proc_prefixed_options_parsev(ctx, options, "", tag, name, args); |
| 486 | } |
| 487 | |
| 488 | static void |
| 489 | grn_proc_func_generate_cache_key_add(grn_ctx *ctx, |
| 490 | grn_obj *cache_key, |
| 491 | grn_obj *arg) |
| 492 | { |
| 493 | switch (arg->header.type) { |
| 494 | case GRN_TABLE_HASH_KEY: |
| 495 | GRN_HASH_EACH_BEGIN(ctx, (grn_hash *)arg, cursor, id) |
| 496 | { |
| 497 | void *key; |
| 498 | unsigned int key_size; |
| 499 | void *value; |
| 500 | int value_size = |
| 501 | grn_hash_cursor_get_key_value(ctx, cursor, &key, &key_size, &value); |
| 502 | GRN_TEXT_PUT(ctx, cache_key, key, key_size); |
| 503 | GRN_TEXT_PUTC(ctx, cache_key, '\1'); |
| 504 | if (value_size == sizeof(grn_obj)) { |
| 505 | grn_proc_func_generate_cache_key_add(ctx, cache_key, (grn_obj *)value); |
| 506 | } |
| 507 | GRN_TEXT_PUTC(ctx, cache_key, '\2'); |
| 508 | } |
| 509 | GRN_HASH_EACH_END(ctx, cursor); |
| 510 | break; |
| 511 | default: |
| 512 | GRN_TEXT_PUT(ctx, cache_key, GRN_BULK_HEAD(arg), GRN_BULK_VSIZE(arg)); |
| 513 | break; |
| 514 | } |
| 515 | } |
| 516 | |
| 517 | grn_rc |
| 518 | grn_proc_func_generate_cache_key(grn_ctx *ctx, |
| 519 | const char *function_name, |
| 520 | grn_obj **args, |
| 521 | int n_args, |
| 522 | grn_obj *cache_key) |
| 523 | { |
| 524 | GRN_API_ENTER; |
| 525 | GRN_TEXT_PUTS(ctx, cache_key, function_name); |
| 526 | GRN_TEXT_PUTC(ctx, cache_key, '\0'); |
| 527 | int i; |
| 528 | for (i = 0; i < n_args; i++) { |
| 529 | grn_proc_func_generate_cache_key_add(ctx, cache_key, args[i]); |
| 530 | GRN_TEXT_PUTC(ctx, cache_key, '\0'); |
| 531 | } |
| 532 | GRN_API_RETURN(ctx->rc); |
| 533 | } |
| 534 | |
| 535 | /**** procs ****/ |
| 536 | |
| 537 | static grn_obj * |
| 538 | proc_load(grn_ctx *ctx, int nargs, grn_obj **args, grn_user_data *user_data) |
| 539 | { |
| 540 | grn_load_input input; |
| 541 | |
| 542 | input.type = grn_plugin_proc_get_var_content_type(ctx, |
| 543 | user_data, |
| 544 | "input_type", |
| 545 | -1, |
| 546 | GRN_CONTENT_JSON); |
| 547 | #define INIT_STRING_ARGUMENT(member_name, arg_name) \ |
| 548 | input.member_name.value = \ |
| 549 | grn_plugin_proc_get_var_string(ctx, \ |
| 550 | user_data, \ |
| 551 | arg_name, \ |
| 552 | -1, \ |
| 553 | &(input.member_name.length)) |
| 554 | |
| 555 | INIT_STRING_ARGUMENT(table, "table"); |
| 556 | INIT_STRING_ARGUMENT(columns, "columns"); |
| 557 | INIT_STRING_ARGUMENT(values, "values"); |
| 558 | INIT_STRING_ARGUMENT(if_exists, "ifexists"); |
| 559 | INIT_STRING_ARGUMENT(each, "each"); |
| 560 | |
| 561 | #undef INIT_STRING_ARGUMENT |
| 562 | |
| 563 | input.output_ids = |
| 564 | grn_plugin_proc_get_var_bool(ctx, user_data, "output_ids", -1, false); |
| 565 | input.output_errors = |
| 566 | grn_plugin_proc_get_var_bool(ctx, user_data, "output_errors", -1, false); |
| 567 | input.lock_table = |
| 568 | grn_plugin_proc_get_var_bool(ctx, user_data, "lock_table", -1, false); |
| 569 | input.emit_level = 1; |
| 570 | |
| 571 | grn_load_internal(ctx, &input); |
| 572 | if (ctx->rc != GRN_SUCCESS) { |
| 573 | ctx->impl->loader.stat = GRN_LOADER_END; |
| 574 | if (ctx->rc == GRN_CANCEL) { |
| 575 | ctx->impl->loader.error.rc = GRN_SUCCESS; |
| 576 | } |
| 577 | } |
| 578 | bool accept_more_data = true; |
| 579 | if (ctx->impl->loader.stat == GRN_LOADER_END) { |
| 580 | accept_more_data = false; |
| 581 | } |
| 582 | if (ctx->impl->command.flags & GRN_CTX_TAIL) { |
| 583 | accept_more_data = false; |
| 584 | } |
| 585 | if (accept_more_data) { |
| 586 | grn_obj *command = grn_proc_get_info(ctx, user_data, NULL, NULL, NULL); |
| 587 | grn_ctx_set_keep_command(ctx, command); |
| 588 | return NULL; |
| 589 | } |
| 590 | |
| 591 | if (ctx->impl->loader.error.rc != GRN_SUCCESS) { |
| 592 | ctx->rc = ctx->impl->loader.error.rc; |
| 593 | grn_strcpy(ctx->errbuf, GRN_CTX_MSGSIZE, ctx->impl->loader.error.buffer); |
| 594 | ctx->errline = ctx->impl->loader.error.line; |
| 595 | ctx->errfile = ctx->impl->loader.error.file; |
| 596 | ctx->errfunc = ctx->impl->loader.error.func; |
| 597 | } |
| 598 | { |
| 599 | unsigned int n_records; |
| 600 | if (ctx->impl->loader.table) { |
| 601 | n_records = grn_table_size(ctx, ctx->impl->loader.table); |
| 602 | } else { |
| 603 | n_records = 0; |
| 604 | } |
| 605 | GRN_QUERY_LOG(ctx, |
| 606 | GRN_QUERY_LOG_SIZE, |
| 607 | ":", |
| 608 | "load(%d): [%d][%d][%d]", |
| 609 | ctx->impl->loader.n_records, |
| 610 | ctx->impl->loader.n_record_errors, |
| 611 | ctx->impl->loader.n_column_errors, |
| 612 | n_records); |
| 613 | } |
| 614 | if (grn_ctx_get_command_version(ctx) >= GRN_COMMAND_VERSION_3) { |
| 615 | int n_elements = 1; |
| 616 | if (ctx->impl->loader.output_ids) { |
| 617 | n_elements++; |
| 618 | } |
| 619 | if (ctx->impl->loader.output_errors) { |
| 620 | n_elements++; |
| 621 | } |
| 622 | GRN_OUTPUT_MAP_OPEN("result", n_elements); |
| 623 | GRN_OUTPUT_CSTR("n_loaded_records"); |
| 624 | GRN_OUTPUT_INT64(ctx->impl->loader.n_records); |
| 625 | if (ctx->impl->loader.output_ids) { |
| 626 | grn_obj *ids = &(ctx->impl->loader.ids); |
| 627 | int i, n_ids; |
| 628 | |
| 629 | GRN_OUTPUT_CSTR("loaded_ids"); |
| 630 | n_ids = GRN_BULK_VSIZE(ids) / sizeof(uint32_t); |
| 631 | GRN_OUTPUT_ARRAY_OPEN("loaded_ids", n_ids); |
| 632 | for (i = 0; i < n_ids; i++) { |
| 633 | GRN_OUTPUT_UINT64(GRN_UINT32_VALUE_AT(ids, i)); |
| 634 | } |
| 635 | GRN_OUTPUT_ARRAY_CLOSE(); |
| 636 | } |
| 637 | if (ctx->impl->loader.output_errors) { |
| 638 | grn_obj *return_codes = &(ctx->impl->loader.return_codes); |
| 639 | grn_obj *error_messages = &(ctx->impl->loader.error_messages); |
| 640 | int i, n; |
| 641 | |
| 642 | GRN_OUTPUT_CSTR("errors"); |
| 643 | n = GRN_BULK_VSIZE(return_codes) / sizeof(int32_t); |
| 644 | GRN_OUTPUT_ARRAY_OPEN("errors", n); |
| 645 | for (i = 0; i < n; i++) { |
| 646 | const char *message; |
| 647 | unsigned int message_size; |
| 648 | |
| 649 | message_size = |
| 650 | grn_vector_get_element(ctx, error_messages, i, &message, NULL, NULL); |
| 651 | |
| 652 | GRN_OUTPUT_MAP_OPEN("error", 2); |
| 653 | GRN_OUTPUT_CSTR("return_code"); |
| 654 | GRN_OUTPUT_INT64(GRN_INT32_VALUE_AT(return_codes, i)); |
| 655 | GRN_OUTPUT_CSTR("message"); |
| 656 | if (message_size == 0) { |
| 657 | GRN_OUTPUT_NULL(); |
| 658 | } else { |
| 659 | GRN_OUTPUT_STR(message, message_size); |
| 660 | } |
| 661 | GRN_OUTPUT_MAP_CLOSE(); |
| 662 | } |
| 663 | GRN_OUTPUT_ARRAY_CLOSE(); |
| 664 | } |
| 665 | GRN_OUTPUT_MAP_CLOSE(); |
| 666 | } else { |
| 667 | GRN_OUTPUT_INT64(ctx->impl->loader.n_records); |
| 668 | } |
| 669 | if (ctx->impl->loader.table) { |
| 670 | grn_db_touch(ctx, DB_OBJ(ctx->impl->loader.table)->db); |
| 671 | } |
| 672 | grn_ctx_loader_clear(ctx); |
| 673 | |
| 674 | return NULL; |
| 675 | } |
| 676 | |
| 677 | static grn_obj * |
| 678 | proc_status(grn_ctx *ctx, int nargs, grn_obj **args, grn_user_data *user_data) |
| 679 | { |
| 680 | grn_timeval now; |
| 681 | grn_cache *cache; |
| 682 | grn_cache_statistics statistics; |
| 683 | int n_elements = 17; |
| 684 | #ifdef GRN_WITH_APACHE_ARROW |
| 685 | n_elements++; |
| 686 | #endif |
| 687 | |
| 688 | grn_timeval_now(ctx, &now); |
| 689 | cache = grn_cache_current_get(ctx); |
| 690 | grn_cache_get_statistics(ctx, cache, &statistics); |
| 691 | GRN_OUTPUT_MAP_OPEN("RESULT", n_elements); |
| 692 | GRN_OUTPUT_CSTR("alloc_count"); |
| 693 | GRN_OUTPUT_INT64(grn_alloc_count()); |
| 694 | GRN_OUTPUT_CSTR("starttime"); |
| 695 | GRN_OUTPUT_INT64(grn_starttime.tv_sec); |
| 696 | GRN_OUTPUT_CSTR("start_time"); |
| 697 | GRN_OUTPUT_INT64(grn_starttime.tv_sec); |
| 698 | GRN_OUTPUT_CSTR("uptime"); |
| 699 | GRN_OUTPUT_INT64(now.tv_sec - grn_starttime.tv_sec); |
| 700 | GRN_OUTPUT_CSTR("version"); |
| 701 | GRN_OUTPUT_CSTR(grn_get_version()); |
| 702 | GRN_OUTPUT_CSTR("n_queries"); |
| 703 | GRN_OUTPUT_INT64(statistics.nfetches); |
| 704 | GRN_OUTPUT_CSTR("cache_hit_rate"); |
| 705 | if (statistics.nfetches == 0) { |
| 706 | GRN_OUTPUT_FLOAT(0.0); |
| 707 | } else { |
| 708 | double cache_hit_rate; |
| 709 | cache_hit_rate = (double)statistics.nhits / (double)statistics.nfetches; |
| 710 | GRN_OUTPUT_FLOAT(cache_hit_rate * 100.0); |
| 711 | } |
| 712 | GRN_OUTPUT_CSTR("command_version"); |
| 713 | GRN_OUTPUT_INT32(grn_ctx_get_command_version(ctx)); |
| 714 | GRN_OUTPUT_CSTR("default_command_version"); |
| 715 | GRN_OUTPUT_INT32(grn_get_default_command_version()); |
| 716 | GRN_OUTPUT_CSTR("max_command_version"); |
| 717 | GRN_OUTPUT_INT32(GRN_COMMAND_VERSION_MAX); |
| 718 | { |
| 719 | grn_com_queue *job_queue; |
| 720 | job_queue = grn_job_queue_current_get(ctx); |
| 721 | GRN_OUTPUT_CSTR("n_jobs"); |
| 722 | if (job_queue) { |
| 723 | GRN_OUTPUT_UINT64(grn_com_queue_size(ctx, job_queue)); |
| 724 | } else { |
| 725 | GRN_OUTPUT_UINT64(0); |
| 726 | } |
| 727 | } |
| 728 | GRN_OUTPUT_CSTR("features"); |
| 729 | { |
| 730 | const int n_features = 23; |
| 731 | GRN_OUTPUT_MAP_OPEN("features", n_features); |
| 732 | |
| 733 | GRN_OUTPUT_CSTR("nfkc"); |
| 734 | #ifndef NO_NFKC |
| 735 | GRN_OUTPUT_BOOL(true); |
| 736 | #else |
| 737 | GRN_OUTPUT_BOOL(false); |
| 738 | #endif |
| 739 | |
| 740 | GRN_OUTPUT_CSTR("mecab"); |
| 741 | #ifdef GRN_WITH_MECAB |
| 742 | GRN_OUTPUT_BOOL(true); |
| 743 | #else |
| 744 | GRN_OUTPUT_BOOL(false); |
| 745 | #endif |
| 746 | |
| 747 | GRN_OUTPUT_CSTR("message_pack"); |
| 748 | #ifdef GRN_WITH_MESSAGE_PACK |
| 749 | GRN_OUTPUT_BOOL(true); |
| 750 | #else |
| 751 | GRN_OUTPUT_BOOL(false); |
| 752 | #endif |
| 753 | |
| 754 | GRN_OUTPUT_CSTR("mruby"); |
| 755 | #ifdef GRN_WITH_MRUBY |
| 756 | GRN_OUTPUT_BOOL(true); |
| 757 | #else |
| 758 | GRN_OUTPUT_BOOL(false); |
| 759 | #endif |
| 760 | |
| 761 | GRN_OUTPUT_CSTR("onigmo"); |
| 762 | #ifdef GRN_WITH_ONIGMO |
| 763 | GRN_OUTPUT_BOOL(true); |
| 764 | #else |
| 765 | GRN_OUTPUT_BOOL(false); |
| 766 | #endif |
| 767 | |
| 768 | GRN_OUTPUT_CSTR("zlib"); |
| 769 | #ifdef GRN_WITH_ZLIB |
| 770 | GRN_OUTPUT_BOOL(true); |
| 771 | #else |
| 772 | GRN_OUTPUT_BOOL(false); |
| 773 | #endif |
| 774 | |
| 775 | GRN_OUTPUT_CSTR("lz4"); |
| 776 | #ifdef GRN_WITH_LZ4 |
| 777 | GRN_OUTPUT_BOOL(true); |
| 778 | #else |
| 779 | GRN_OUTPUT_BOOL(false); |
| 780 | #endif |
| 781 | |
| 782 | GRN_OUTPUT_CSTR("zstandard"); |
| 783 | #ifdef GRN_WITH_ZSTD |
| 784 | GRN_OUTPUT_BOOL(true); |
| 785 | #else |
| 786 | GRN_OUTPUT_BOOL(false); |
| 787 | #endif |
| 788 | |
| 789 | GRN_OUTPUT_CSTR("kqueue"); |
| 790 | #ifdef USE_KQUEUE |
| 791 | GRN_OUTPUT_BOOL(true); |
| 792 | #else |
| 793 | GRN_OUTPUT_BOOL(false); |
| 794 | #endif |
| 795 | |
| 796 | GRN_OUTPUT_CSTR("epoll"); |
| 797 | #ifdef USE_EPOLL |
| 798 | GRN_OUTPUT_BOOL(true); |
| 799 | #else |
| 800 | GRN_OUTPUT_BOOL(false); |
| 801 | #endif |
| 802 | |
| 803 | GRN_OUTPUT_CSTR("poll"); |
| 804 | #ifdef USE_POLL |
| 805 | GRN_OUTPUT_BOOL(true); |
| 806 | #else |
| 807 | GRN_OUTPUT_BOOL(false); |
| 808 | #endif |
| 809 | |
| 810 | GRN_OUTPUT_CSTR("rapidjson"); |
| 811 | #ifdef GRN_WITH_RAPIDJSON |
| 812 | GRN_OUTPUT_BOOL(true); |
| 813 | #else |
| 814 | GRN_OUTPUT_BOOL(false); |
| 815 | #endif |
| 816 | |
| 817 | GRN_OUTPUT_CSTR("apache_arrow"); |
| 818 | #ifdef GRN_WITH_APACHE_ARROW |
| 819 | GRN_OUTPUT_BOOL(true); |
| 820 | #else |
| 821 | GRN_OUTPUT_BOOL(false); |
| 822 | #endif |
| 823 | |
| 824 | GRN_OUTPUT_CSTR("xxhash"); |
| 825 | #ifdef GRN_WITH_XXHASH |
| 826 | GRN_OUTPUT_BOOL(true); |
| 827 | #else |
| 828 | GRN_OUTPUT_BOOL(false); |
| 829 | #endif |
| 830 | |
| 831 | GRN_OUTPUT_CSTR("blosc"); |
| 832 | #ifdef GRN_WITH_BLOSC |
| 833 | GRN_OUTPUT_BOOL(true); |
| 834 | #else |
| 835 | GRN_OUTPUT_BOOL(false); |
| 836 | #endif |
| 837 | |
| 838 | GRN_OUTPUT_CSTR("bfloat16"); |
| 839 | #ifdef GRN_HAVE_BFLOAT16 |
| 840 | GRN_OUTPUT_BOOL(true); |
| 841 | #else |
| 842 | GRN_OUTPUT_BOOL(false); |
| 843 | #endif |
| 844 | |
| 845 | GRN_OUTPUT_CSTR("h3"); |
| 846 | #ifdef GRN_WITH_H3 |
| 847 | GRN_OUTPUT_BOOL(true); |
| 848 | #else |
| 849 | GRN_OUTPUT_BOOL(false); |
| 850 | #endif |
| 851 | |
| 852 | GRN_OUTPUT_CSTR("simdjson"); |
| 853 | #ifdef GRN_WITH_SIMDJSON |
| 854 | GRN_OUTPUT_BOOL(true); |
| 855 | #else |
| 856 | GRN_OUTPUT_BOOL(false); |
| 857 | #endif |
| 858 | |
| 859 | GRN_OUTPUT_CSTR("llama.cpp"); |
| 860 | #ifdef GRN_WITH_LLAMA_CPP |
| 861 | GRN_OUTPUT_BOOL(true); |
| 862 | #else |
| 863 | GRN_OUTPUT_BOOL(false); |
| 864 | #endif |
| 865 | |
| 866 | GRN_OUTPUT_CSTR("faiss"); |
| 867 | #ifdef GRN_WITH_FAISS |
| 868 | GRN_OUTPUT_BOOL(true); |
| 869 | #else |
| 870 | GRN_OUTPUT_BOOL(false); |
| 871 | #endif |
| 872 | |
| 873 | GRN_OUTPUT_CSTR("openzl"); |
| 874 | #ifdef GRN_WITH_OPENZL |
| 875 | GRN_OUTPUT_BOOL(true); |
| 876 | #else |
| 877 | GRN_OUTPUT_BOOL(false); |
| 878 | #endif |
| 879 | |
| 880 | GRN_OUTPUT_CSTR("jsoncons"); |
| 881 | #ifdef GRN_WITH_JSONCONS |
| 882 | GRN_OUTPUT_BOOL(true); |
| 883 | #else |
| 884 | GRN_OUTPUT_BOOL(false); |
| 885 | #endif |
| 886 | |
| 887 | GRN_OUTPUT_CSTR("back_trace"); |
| 888 | GRN_OUTPUT_BOOL(grn_is_back_trace_enable()); |
| 889 | |
| 890 | GRN_OUTPUT_CSTR("reference_count"); |
| 891 | GRN_OUTPUT_BOOL(grn_is_reference_count_enable()); |
| 892 | |
| 893 | GRN_OUTPUT_MAP_CLOSE(); |
| 894 | } |
| 895 | #ifdef GRN_WITH_APACHE_ARROW |
| 896 | GRN_OUTPUT_CSTR("apache_arrow"); |
| 897 | { |
| 898 | const int n_apache_arrow_elements = 4; |
| 899 | GRN_OUTPUT_MAP_OPEN("apache_arrow", n_apache_arrow_elements); |
| 900 | |
| 901 | GRN_OUTPUT_CSTR("version_major"); |
| 902 | GRN_OUTPUT_INT32(ARROW_VERSION_MAJOR); |
| 903 | |
| 904 | GRN_OUTPUT_CSTR("version_minor"); |
| 905 | GRN_OUTPUT_INT32(ARROW_VERSION_MINOR); |
| 906 | |
| 907 | GRN_OUTPUT_CSTR("version_patch"); |
| 908 | GRN_OUTPUT_INT32(ARROW_VERSION_PATCH); |
| 909 | |
| 910 | GRN_OUTPUT_CSTR("version"); |
| 911 | GRN_OUTPUT_CSTR(ARROW_VERSION_STRING); |
| 912 | |
| 913 | GRN_OUTPUT_MAP_CLOSE(); |
| 914 | } |
| 915 | #endif |
| 916 | GRN_OUTPUT_CSTR("memory_map_size"); |
| 917 | GRN_OUTPUT_UINT64(grn_get_memory_map_size()); |
| 918 | GRN_OUTPUT_CSTR("n_workers"); |
| 919 | GRN_OUTPUT_INT32(grn_ctx_get_n_workers(ctx)); |
| 920 | GRN_OUTPUT_CSTR("default_n_workers"); |
| 921 | GRN_OUTPUT_INT32(grn_get_default_n_workers()); |
| 922 | GRN_OUTPUT_CSTR("os"); |
| 923 | #ifdef HOST_OS |
| 924 | GRN_OUTPUT_CSTR(HOST_OS); |
| 925 | #else |
| 926 | GRN_OUTPUT_CSTR("Unknown"); |
| 927 | #endif |
| 928 | GRN_OUTPUT_CSTR("cpu"); |
| 929 | #ifdef HOST_CPU |
| 930 | GRN_OUTPUT_CSTR(HOST_CPU); |
| 931 | #else |
| 932 | GRN_OUTPUT_CSTR("unknown"); |
| 933 | #endif |
| 934 | GRN_OUTPUT_MAP_CLOSE(); |
| 935 | |
| 936 | #ifdef GRN_WITH_MEMORY_DEBUG |
| 937 | if (grn_plugin_proc_get_var_bool(ctx, |
| 938 | user_data, |
| 939 | "dump_alloc_info", |
| 940 | -1, |
| 941 | false)) { |
| 942 | grn_alloc_info_dump(&grn_gctx); |
| 943 | } |
| 944 | #endif |
| 945 | |
| 946 | return NULL; |
| 947 | } |
| 948 | |
| 949 | #define GRN_STRLEN(s) ((s) ? strlen(s) : 0) |
| 950 | |
| 951 | void |
| 952 | grn_proc_output_object_name(grn_ctx *ctx, grn_obj *obj) |
| 953 | { |
| 954 | grn_obj bulk; |
| 955 | int name_len; |
| 956 | char name[GRN_TABLE_MAX_KEY_SIZE]; |
| 957 | |
| 958 | if (obj) { |
| 959 | GRN_TEXT_INIT(&bulk, GRN_OBJ_DO_SHALLOW_COPY); |
| 960 | name_len = grn_obj_name(ctx, obj, name, GRN_TABLE_MAX_KEY_SIZE); |
| 961 | GRN_TEXT_SET(ctx, &bulk, name, name_len); |
| 962 | } else { |
| 963 | GRN_VOID_INIT(&bulk); |
| 964 | } |
| 965 | |
| 966 | GRN_OUTPUT_OBJ(&bulk, NULL); |
| 967 | GRN_OBJ_FIN(ctx, &bulk); |
| 968 | } |
| 969 | |
| 970 | void |
| 971 | grn_proc_output_object_id_name(grn_ctx *ctx, grn_id id) |
| 972 | { |
| 973 | grn_obj *obj = NULL; |
| 974 | |
| 975 | if (id != GRN_ID_NIL) { |
| 976 | obj = grn_ctx_at(ctx, id); |
| 977 | } |
| 978 | |
| 979 | grn_proc_output_object_name(ctx, obj); |
| 980 | } |
| 981 | |
| 982 | static grn_obj * |
| 983 | proc_missing(grn_ctx *ctx, int nargs, grn_obj **args, grn_user_data *user_data) |
| 984 | { |
| 985 | uint32_t plen; |
| 986 | grn_obj *outbuf = ctx->impl->output.buf; |
| 987 | static int grn_document_root_len = -1; |
| 988 | if (!grn_document_root) { |
| 989 | return NULL; |
| 990 | } |
| 991 | if (grn_document_root_len < 0) { |
| 992 | size_t l; |
| 993 | if ((l = strlen(grn_document_root)) > PATH_MAX) { |
| 994 | return NULL; |
| 995 | } |
| 996 | grn_document_root_len = (int)l; |
| 997 | if (l > 0 && grn_document_root[l - 1] == '/') { |
| 998 | grn_document_root_len--; |
| 999 | } |
| 1000 | } |
| 1001 | if ((plen = GRN_TEXT_LEN(VAR(0))) + grn_document_root_len < PATH_MAX) { |
| 1002 | char path[PATH_MAX]; |
| 1003 | grn_memcpy(path, grn_document_root, grn_document_root_len); |
| 1004 | path[grn_document_root_len] = '/'; |
| 1005 | grn_str_url_path_normalize(ctx, |
| 1006 | GRN_TEXT_VALUE(VAR(0)), |
| 1007 | GRN_TEXT_LEN(VAR(0)), |
| 1008 | path + grn_document_root_len + 1, |
| 1009 | PATH_MAX - grn_document_root_len - 1); |
| 1010 | grn_bulk_put_from_file(ctx, outbuf, path); |
| 1011 | } else { |
| 1012 | uint32_t abbrlen = 32; |
| 1013 | ERR(GRN_INVALID_ARGUMENT, |
| 1014 | "too long path name: <%s/%.*s...> %u(%u)", |
| 1015 | grn_document_root, |
| 1016 | abbrlen < plen ? abbrlen : plen, |
| 1017 | GRN_TEXT_VALUE(VAR(0)), |
| 1018 | plen + grn_document_root_len, |
| 1019 | PATH_MAX); |
| 1020 | } |
| 1021 | return NULL; |
| 1022 | } |
| 1023 | |
| 1024 | static grn_obj * |
| 1025 | proc_quit(grn_ctx *ctx, int nargs, grn_obj **args, grn_user_data *user_data) |
| 1026 | { |
| 1027 | ctx->stat = GRN_CTX_QUITTING; |
| 1028 | GRN_OUTPUT_BOOL(!ctx->rc); |
| 1029 | return NULL; |
| 1030 | } |
| 1031 | |
| 1032 | static grn_obj * |
| 1033 | proc_shutdown(grn_ctx *ctx, int nargs, grn_obj **args, grn_user_data *user_data) |
| 1034 | { |
| 1035 | const char *mode; |
| 1036 | size_t mode_size; |
| 1037 | |
| 1038 | mode = grn_plugin_proc_get_var_string(ctx, user_data, "mode", -1, &mode_size); |
| 1039 | #define MODE_EQUAL(name) \ |
| 1040 | (mode_size == strlen(name) && memcmp(mode, name, mode_size) == 0) |
| 1041 | if (mode_size == 0 || MODE_EQUAL("graceful")) { |
| 1042 | /* Do nothing. This is the default. */ |
| 1043 | } else if (MODE_EQUAL("immediate")) { |
| 1044 | grn_request_canceler_cancel_all(); |
| 1045 | if (ctx->rc == GRN_INTERRUPTED_FUNCTION_CALL) { |
| 1046 | ctx->rc = GRN_SUCCESS; |
| 1047 | } |
| 1048 | } else { |
| 1049 | ERR(GRN_INVALID_ARGUMENT, |
| 1050 | "[shutdown] mode must be <graceful> or <immediate>: <%.*s>", |
| 1051 | (int)mode_size, |
| 1052 | mode); |
| 1053 | } |
| 1054 | #undef MODE_EQUAL |
| 1055 | |
| 1056 | if (ctx->rc == GRN_SUCCESS) { |
| 1057 | grn_gctx.stat = GRN_CTX_QUIT; |
| 1058 | ctx->stat = GRN_CTX_QUITTING; |
| 1059 | } |
| 1060 | |
| 1061 | GRN_OUTPUT_BOOL(!ctx->rc); |
| 1062 | |
| 1063 | return NULL; |
| 1064 | } |
| 1065 | |
| 1066 | static grn_obj * |
| 1067 | proc_defrag(grn_ctx *ctx, int nargs, grn_obj **args, grn_user_data *user_data) |
| 1068 | { |
| 1069 | grn_obj *obj; |
| 1070 | int olen, threshold; |
| 1071 | olen = GRN_TEXT_LEN(VAR(0)); |
| 1072 | |
| 1073 | if (olen) { |
| 1074 | obj = grn_ctx_get(ctx, GRN_TEXT_VALUE(VAR(0)), olen); |
| 1075 | } else { |
| 1076 | obj = ctx->impl->db; |
| 1077 | } |
| 1078 | |
| 1079 | threshold = GRN_TEXT_LEN(VAR(1)) |
| 1080 | ? grn_atoi(GRN_TEXT_VALUE(VAR(1)), GRN_BULK_CURR(VAR(1)), NULL) |
| 1081 | : 0; |
| 1082 | |
| 1083 | if (obj) { |
| 1084 | grn_obj_defrag(ctx, obj, threshold); |
| 1085 | } else { |
| 1086 | ERR(GRN_INVALID_ARGUMENT, "defrag object not found"); |
| 1087 | } |
| 1088 | GRN_OUTPUT_BOOL(!ctx->rc); |
| 1089 | return NULL; |
| 1090 | } |
| 1091 | |
| 1092 | static grn_rc |
| 1093 | proc_delete_validate_selector(grn_ctx *ctx, |
| 1094 | grn_obj *table, |
| 1095 | grn_obj *table_name, |
| 1096 | grn_obj *key, |
| 1097 | grn_obj *id, |
| 1098 | grn_obj *filter) |
| 1099 | { |
| 1100 | grn_rc rc = GRN_SUCCESS; |
| 1101 | |
| 1102 | if (!table) { |
| 1103 | rc = GRN_INVALID_ARGUMENT; |
| 1104 | ERR(rc, |
| 1105 | "[table][record][delete] table doesn't exist: <%.*s>", |
| 1106 | (int)GRN_TEXT_LEN(table_name), |
| 1107 | GRN_TEXT_VALUE(table_name)); |
| 1108 | return rc; |
| 1109 | } |
| 1110 | |
| 1111 | if (GRN_TEXT_LEN(key) == 0 && GRN_TEXT_LEN(id) == 0 && |
| 1112 | GRN_TEXT_LEN(filter) == 0) { |
| 1113 | rc = GRN_INVALID_ARGUMENT; |
| 1114 | ERR(rc, |
| 1115 | "[table][record][delete] either key, id or filter must be specified: " |
| 1116 | "table: <%.*s>", |
| 1117 | (int)GRN_TEXT_LEN(table_name), |
| 1118 | GRN_TEXT_VALUE(table_name)); |
| 1119 | return rc; |
| 1120 | } |
| 1121 | |
| 1122 | if (GRN_TEXT_LEN(key) && GRN_TEXT_LEN(id) && GRN_TEXT_LEN(filter)) { |
| 1123 | rc = GRN_INVALID_ARGUMENT; |
| 1124 | ERR(rc, |
| 1125 | "[table][record][delete] " |
| 1126 | "record selector must be one of key, id and filter: " |
| 1127 | "table: <%.*s>, key: <%.*s>, id: <%.*s>, filter: <%.*s>", |
| 1128 | (int)GRN_TEXT_LEN(table_name), |
| 1129 | GRN_TEXT_VALUE(table_name), |
| 1130 | (int)GRN_TEXT_LEN(key), |
| 1131 | GRN_TEXT_VALUE(key), |
| 1132 | (int)GRN_TEXT_LEN(id), |
| 1133 | GRN_TEXT_VALUE(id), |
| 1134 | (int)GRN_TEXT_LEN(filter), |
| 1135 | GRN_TEXT_VALUE(filter)); |
| 1136 | return rc; |
| 1137 | } |
| 1138 | |
| 1139 | if (GRN_TEXT_LEN(key) && GRN_TEXT_LEN(id) && GRN_TEXT_LEN(filter) == 0) { |
| 1140 | rc = GRN_INVALID_ARGUMENT; |
| 1141 | ERR(rc, |
| 1142 | "[table][record][delete] " |
| 1143 | "can't use both key and id: table: <%.*s>, key: <%.*s>, id: <%.*s>", |
| 1144 | (int)GRN_TEXT_LEN(table_name), |
| 1145 | GRN_TEXT_VALUE(table_name), |
| 1146 | (int)GRN_TEXT_LEN(key), |
| 1147 | GRN_TEXT_VALUE(key), |
| 1148 | (int)GRN_TEXT_LEN(id), |
| 1149 | GRN_TEXT_VALUE(id)); |
| 1150 | return rc; |
| 1151 | } |
| 1152 | |
| 1153 | if (GRN_TEXT_LEN(key) && GRN_TEXT_LEN(id) == 0 && GRN_TEXT_LEN(filter)) { |
| 1154 | rc = GRN_INVALID_ARGUMENT; |
| 1155 | ERR(rc, |
| 1156 | "[table][record][delete] " |
| 1157 | "can't use both key and filter: " |
| 1158 | "table: <%.*s>, key: <%.*s>, filter: <%.*s>", |
| 1159 | (int)GRN_TEXT_LEN(table_name), |
| 1160 | GRN_TEXT_VALUE(table_name), |
| 1161 | (int)GRN_TEXT_LEN(key), |
| 1162 | GRN_TEXT_VALUE(key), |
| 1163 | (int)GRN_TEXT_LEN(filter), |
| 1164 | GRN_TEXT_VALUE(filter)); |
| 1165 | return rc; |
| 1166 | } |
| 1167 | |
| 1168 | if (GRN_TEXT_LEN(key) == 0 && GRN_TEXT_LEN(id) && GRN_TEXT_LEN(filter)) { |
| 1169 | rc = GRN_INVALID_ARGUMENT; |
| 1170 | ERR(rc, |
| 1171 | "[table][record][delete] " |
| 1172 | "can't use both id and filter: " |
| 1173 | "table: <%.*s>, id: <%.*s>, filter: <%.*s>", |
| 1174 | (int)GRN_TEXT_LEN(table_name), |
| 1175 | GRN_TEXT_VALUE(table_name), |
| 1176 | (int)GRN_TEXT_LEN(id), |
| 1177 | GRN_TEXT_VALUE(id), |
| 1178 | (int)GRN_TEXT_LEN(filter), |
| 1179 | GRN_TEXT_VALUE(filter)); |
| 1180 | return rc; |
| 1181 | } |
| 1182 | |
| 1183 | return rc; |
| 1184 | } |
| 1185 | |
| 1186 | static grn_obj * |
| 1187 | proc_delete(grn_ctx *ctx, int nargs, grn_obj **args, grn_user_data *user_data) |
| 1188 | { |
| 1189 | grn_rc rc = GRN_INVALID_ARGUMENT; |
| 1190 | grn_obj *table_name = VAR(0); |
| 1191 | grn_obj *key = VAR(1); |
| 1192 | grn_obj *id = VAR(2); |
| 1193 | grn_obj *filter = VAR(3); |
| 1194 | grn_obj *table = NULL; |
| 1195 | uint32_t n_deleted = 0; |
| 1196 | uint32_t n_errors = 0; |
| 1197 | int32_t limit; |
| 1198 | |
| 1199 | if (GRN_TEXT_LEN(table_name) == 0) { |
| 1200 | rc = GRN_INVALID_ARGUMENT; |
| 1201 | ERR(rc, "[table][record][delete] table name isn't specified"); |
| 1202 | goto exit; |
| 1203 | } |
| 1204 | |
| 1205 | table = |
| 1206 | grn_ctx_get(ctx, GRN_TEXT_VALUE(table_name), GRN_TEXT_LEN(table_name)); |
| 1207 | rc = proc_delete_validate_selector(ctx, table, table_name, key, id, filter); |
| 1208 | if (rc != GRN_SUCCESS) { |
| 1209 | goto exit; |
| 1210 | } |
| 1211 | |
| 1212 | limit = grn_plugin_proc_get_var_int32(ctx, user_data, "limit", -1, -1); |
| 1213 | if (limit == 0) { |
| 1214 | goto exit; |
| 1215 | } |
| 1216 | |
| 1217 | if (GRN_TEXT_LEN(key)) { |
| 1218 | grn_obj casted_key; |
| 1219 | if (key->header.domain != table->header.domain) { |
| 1220 | GRN_OBJ_INIT(&casted_key, GRN_BULK, 0, table->header.domain); |
| 1221 | grn_obj_cast(ctx, key, &casted_key, false); |
| 1222 | key = &casted_key; |
| 1223 | } |
| 1224 | if (ctx->rc) { |
| 1225 | rc = ctx->rc; |
| 1226 | } else { |
| 1227 | if (limit < 0) { |
| 1228 | const int32_t n_records = 1; |
| 1229 | limit += n_records + 1; |
| 1230 | } |
| 1231 | if (limit >= 1) { |
| 1232 | rc = |
| 1233 | grn_table_delete(ctx, table, GRN_BULK_HEAD(key), GRN_BULK_VSIZE(key)); |
| 1234 | if (rc == GRN_SUCCESS) { |
| 1235 | n_deleted++; |
| 1236 | } else { |
| 1237 | n_errors++; |
| 1238 | } |
| 1239 | } |
| 1240 | } |
| 1241 | if (key == &casted_key) { |
| 1242 | GRN_OBJ_FIN(ctx, &casted_key); |
| 1243 | } |
| 1244 | } else if (GRN_TEXT_LEN(id)) { |
| 1245 | const char *end; |
| 1246 | grn_id parsed_id = grn_atoui(GRN_TEXT_VALUE(id), GRN_BULK_CURR(id), &end); |
| 1247 | if (end == GRN_BULK_CURR(id)) { |
| 1248 | if (limit < 0) { |
| 1249 | const int32_t n_records = 1; |
| 1250 | limit += n_records + 1; |
| 1251 | } |
| 1252 | if (limit >= 1) { |
| 1253 | rc = grn_table_delete_by_id(ctx, table, parsed_id); |
| 1254 | if (rc == GRN_SUCCESS) { |
| 1255 | n_deleted++; |
| 1256 | } else { |
| 1257 | n_errors++; |
| 1258 | } |
| 1259 | } |
| 1260 | } else { |
| 1261 | rc = GRN_INVALID_ARGUMENT; |
| 1262 | ERR(rc, |
| 1263 | "[table][record][delete] id should be number: " |
| 1264 | "table: <%.*s>, id: <%.*s>, detail: <%.*s|%c|%.*s>", |
| 1265 | (int)GRN_TEXT_LEN(table_name), |
| 1266 | GRN_TEXT_VALUE(table_name), |
| 1267 | (int)GRN_TEXT_LEN(id), |
| 1268 | GRN_TEXT_VALUE(id), |
| 1269 | (int)(end - GRN_TEXT_VALUE(id)), |
| 1270 | GRN_TEXT_VALUE(id), |
| 1271 | end[0], |
| 1272 | (int)(GRN_TEXT_VALUE(id) - end - 1), |
| 1273 | end + 1); |
| 1274 | } |
| 1275 | } else if (GRN_TEXT_LEN(filter)) { |
| 1276 | grn_obj *cond, *v; |
| 1277 | |
| 1278 | GRN_EXPR_CREATE_FOR_QUERY(ctx, table, cond, v); |
| 1279 | grn_expr_parse(ctx, |
| 1280 | cond, |
| 1281 | GRN_TEXT_VALUE(filter), |
| 1282 | GRN_TEXT_LEN(filter), |
| 1283 | NULL, |
| 1284 | GRN_OP_MATCH, |
| 1285 | GRN_OP_AND, |
| 1286 | GRN_EXPR_SYNTAX_SCRIPT); |
| 1287 | if (ctx->rc) { |
| 1288 | char errbuf[GRN_CTX_MSGSIZE]; |
| 1289 | grn_strcpy(errbuf, GRN_CTX_MSGSIZE, ctx->errbuf); |
| 1290 | rc = ctx->rc; |
| 1291 | ERR(rc, |
| 1292 | "[table][record][delete] failed to parse filter: " |
| 1293 | "table: <%.*s>, filter: <%.*s>, detail: <%s>", |
| 1294 | (int)GRN_TEXT_LEN(table_name), |
| 1295 | GRN_TEXT_VALUE(table_name), |
| 1296 | (int)GRN_TEXT_LEN(filter), |
| 1297 | GRN_TEXT_VALUE(filter), |
| 1298 | errbuf); |
| 1299 | } else { |
| 1300 | grn_obj *records; |
| 1301 | |
| 1302 | records = grn_table_select(ctx, table, cond, NULL, GRN_OP_OR); |
| 1303 | if (records) { |
| 1304 | if (limit < 0) { |
| 1305 | limit += grn_table_size(ctx, records) + 1; |
| 1306 | } |
| 1307 | GRN_TABLE_EACH_BEGIN(ctx, records, cursor, result_id) |
| 1308 | { |
| 1309 | void *key; |
| 1310 | grn_id id; |
| 1311 | grn_rc sub_rc; |
| 1312 | |
| 1313 | if (grn_table_cursor_get_key(ctx, cursor, &key) == 0) { |
| 1314 | continue; |
| 1315 | } |
| 1316 | |
| 1317 | if (n_deleted >= (uint32_t)limit) { |
| 1318 | break; |
| 1319 | } |
| 1320 | |
| 1321 | id = *(grn_id *)key; |
| 1322 | sub_rc = grn_table_delete_by_id(ctx, table, id); |
| 1323 | if (sub_rc == GRN_SUCCESS) { |
| 1324 | n_deleted++; |
| 1325 | } else { |
| 1326 | n_errors++; |
| 1327 | } |
| 1328 | if (rc == GRN_SUCCESS) { |
| 1329 | rc = sub_rc; |
| 1330 | } |
| 1331 | if (ctx->rc == GRN_CANCEL) { |
| 1332 | break; |
| 1333 | } |
| 1334 | if (ctx->rc != GRN_SUCCESS) { |
| 1335 | ERRCLR(ctx); |
| 1336 | } |
| 1337 | } |
| 1338 | GRN_TABLE_EACH_END(ctx, cursor); |
| 1339 | grn_obj_unlink(ctx, records); |
| 1340 | } |
| 1341 | } |
| 1342 | grn_obj_unlink(ctx, cond); |
| 1343 | } |
| 1344 | |
| 1345 | exit: |
| 1346 | { |
| 1347 | unsigned int n_rest_records = 0; |
| 1348 | if (table) { |
| 1349 | n_rest_records = grn_table_size(ctx, table); |
| 1350 | } |
| 1351 | GRN_QUERY_LOG(ctx, |
| 1352 | GRN_QUERY_LOG_SIZE, |
| 1353 | ":", |
| 1354 | "delete(%u): [%u][%u]", |
| 1355 | n_deleted, |
| 1356 | n_errors, |
| 1357 | n_rest_records); |
| 1358 | } |
| 1359 | if (table) { |
| 1360 | grn_obj_unlink(ctx, table); |
| 1361 | } |
| 1362 | GRN_OUTPUT_BOOL(rc == GRN_SUCCESS); |
| 1363 | return NULL; |
| 1364 | } |
| 1365 | |
| 1366 | bool |
| 1367 | grn_proc_option_value_bool(grn_ctx *ctx, grn_obj *option, bool default_value) |
| 1368 | { |
| 1369 | if (!option) { |
| 1370 | return default_value; |
| 1371 | } |
| 1372 | |
| 1373 | grn_raw_string value; |
| 1374 | GRN_RAW_STRING_SET(value, option); |
| 1375 | if (value.length == 0) { |
| 1376 | return default_value; |
| 1377 | } |
| 1378 | |
| 1379 | if (GRN_RAW_STRING_EQUAL_CSTRING(value, "yes") || |
| 1380 | GRN_RAW_STRING_EQUAL_CSTRING(value, "true")) { |
| 1381 | return true; |
| 1382 | } else if (GRN_RAW_STRING_EQUAL_CSTRING(value, "no") || |
| 1383 | GRN_RAW_STRING_EQUAL_CSTRING(value, "false")) { |
| 1384 | return false; |
| 1385 | } else { |
| 1386 | return default_value; |
| 1387 | } |
| 1388 | } |
| 1389 | |
| 1390 | int32_t |
| 1391 | grn_proc_option_value_int32(grn_ctx *ctx, |
| 1392 | grn_obj *option, |
| 1393 | int32_t default_value) |
| 1394 | { |
| 1395 | const char *value; |
| 1396 | size_t value_length; |
| 1397 | int32_t int32_value; |
| 1398 | const char *rest; |
| 1399 | |
| 1400 | if (!option) { |
| 1401 | return default_value; |
| 1402 | } |
| 1403 | |
| 1404 | value = GRN_TEXT_VALUE(option); |
| 1405 | value_length = GRN_TEXT_LEN(option); |
| 1406 | |
| 1407 | if (value_length == 0) { |
| 1408 | return default_value; |
| 1409 | } |
| 1410 | |
| 1411 | int32_value = grn_atoi(value, value + value_length, &rest); |
| 1412 | if (rest == value + value_length) { |
| 1413 | return int32_value; |
| 1414 | } else { |
| 1415 | return default_value; |
| 1416 | } |
| 1417 | } |
| 1418 | |
| 1419 | uint32_t |
| 1420 | grn_proc_option_value_uint32(grn_ctx *ctx, |
| 1421 | grn_obj *option, |
| 1422 | uint32_t default_value) |
| 1423 | { |
| 1424 | const char *value; |
| 1425 | size_t value_length; |
| 1426 | uint32_t uint32_value; |
| 1427 | const char *rest; |
| 1428 | |
| 1429 | if (!option) { |
| 1430 | return default_value; |
| 1431 | } |
| 1432 | |
| 1433 | value = GRN_TEXT_VALUE(option); |
| 1434 | value_length = GRN_TEXT_LEN(option); |
| 1435 | |
| 1436 | if (value_length == 0) { |
| 1437 | return default_value; |
| 1438 | } |
| 1439 | |
| 1440 | uint32_value = grn_atoui(value, value + value_length, &rest); |
| 1441 | if (rest == value + value_length) { |
| 1442 | return uint32_value; |
| 1443 | } else { |
| 1444 | return default_value; |
| 1445 | } |
| 1446 | } |
| 1447 | |
| 1448 | double |
| 1449 | grn_proc_option_value_double(grn_ctx *ctx, |
| 1450 | grn_obj *option, |
| 1451 | double default_value) |
| 1452 | { |
| 1453 | if (!option) { |
| 1454 | return default_value; |
| 1455 | } |
| 1456 | |
| 1457 | if (GRN_TEXT_LEN(option) == 0) { |
| 1458 | return default_value; |
| 1459 | } |
| 1460 | |
| 1461 | double value = default_value; |
| 1462 | grn_obj buffer; |
| 1463 | GRN_FLOAT_INIT(&buffer, 0); |
| 1464 | grn_rc rc = grn_obj_cast(ctx, option, &buffer, false); |
| 1465 | if (rc == GRN_SUCCESS) { |
| 1466 | value = GRN_FLOAT_VALUE(&buffer); |
| 1467 | } |
| 1468 | GRN_OBJ_FIN(ctx, &buffer); |
| 1469 | |
| 1470 | return value; |
| 1471 | } |
| 1472 | |
| 1473 | float |
| 1474 | grn_proc_option_value_float(grn_ctx *ctx, grn_obj *option, float default_value) |
| 1475 | { |
| 1476 | if (!option) { |
| 1477 | return default_value; |
| 1478 | } |
| 1479 | |
| 1480 | if (GRN_TEXT_LEN(option) == 0) { |
| 1481 | return default_value; |
| 1482 | } |
| 1483 | |
| 1484 | float value = default_value; |
| 1485 | grn_obj buffer; |
| 1486 | GRN_FLOAT_INIT(&buffer, 0); |
| 1487 | grn_rc rc = grn_obj_cast(ctx, option, &buffer, false); |
| 1488 | if (rc == GRN_SUCCESS) { |
| 1489 | value = GRN_FLOAT_VALUE(&buffer); |
| 1490 | } |
| 1491 | GRN_OBJ_FIN(ctx, &buffer); |
| 1492 | |
| 1493 | return value; |
| 1494 | } |
| 1495 | |
| 1496 | const char * |
| 1497 | grn_proc_option_value_string(grn_ctx *ctx, grn_obj *option, size_t *size) |
| 1498 | { |
| 1499 | const char *value; |
| 1500 | size_t value_length; |
| 1501 | |
| 1502 | if (!option) { |
| 1503 | if (size) { |
| 1504 | *size = 0; |
| 1505 | } |
| 1506 | return NULL; |
| 1507 | } |
| 1508 | |
| 1509 | value = GRN_TEXT_VALUE(option); |
| 1510 | value_length = GRN_TEXT_LEN(option); |
| 1511 | |
| 1512 | if (size) { |
| 1513 | *size = value_length; |
| 1514 | } |
| 1515 | |
| 1516 | if (value_length == 0) { |
| 1517 | return NULL; |
| 1518 | } else { |
| 1519 | return value; |
| 1520 | } |
| 1521 | } |
| 1522 | |
| 1523 | grn_content_type |
| 1524 | grn_proc_option_value_content_type(grn_ctx *ctx, |
| 1525 | grn_obj *option, |
| 1526 | grn_content_type default_value) |
| 1527 | { |
| 1528 | if (!option) { |
| 1529 | return default_value; |
| 1530 | } |
| 1531 | |
| 1532 | return grn_content_type_parse(ctx, option, default_value); |
| 1533 | } |
| 1534 | |
| 1535 | grn_log_level |
| 1536 | grn_proc_option_value_log_level(grn_ctx *ctx, |
| 1537 | grn_obj *option, |
| 1538 | grn_log_level default_value) |
| 1539 | { |
| 1540 | if (!option) { |
| 1541 | return default_value; |
| 1542 | } |
| 1543 | |
| 1544 | grn_obj value; |
| 1545 | GRN_TEXT_INIT(&value, 0); |
| 1546 | GRN_TEXT_SET(ctx, &value, GRN_TEXT_VALUE(option), GRN_TEXT_LEN(option)); |
| 1547 | GRN_TEXT_PUTC(ctx, &value, '\0'); |
| 1548 | grn_log_level level = default_value; |
| 1549 | if (!grn_log_level_parse(GRN_TEXT_VALUE(&value), &level)) { |
| 1550 | level = default_value; |
| 1551 | } |
| 1552 | GRN_OBJ_FIN(ctx, &value); |
| 1553 | return level; |
| 1554 | } |
| 1555 | |
| 1556 | bool |
| 1557 | grn_proc_get_value_bool(grn_ctx *ctx, |
| 1558 | grn_obj *value, |
| 1559 | bool default_value, |
| 1560 | const char *tag) |
| 1561 | { |
| 1562 | if (!value) { |
| 1563 | return default_value; |
| 1564 | } |
| 1565 | |
| 1566 | if (value->header.domain == GRN_DB_BOOL) { |
| 1567 | return GRN_BOOL_VALUE(value); |
| 1568 | } |
| 1569 | |
| 1570 | if (grn_type_id_is_text_family(ctx, value->header.domain)) { |
| 1571 | return grn_proc_option_value_bool(ctx, value, default_value); |
| 1572 | } |
| 1573 | |
| 1574 | grn_obj inspected; |
| 1575 | GRN_TEXT_INIT(&inspected, 0); |
| 1576 | grn_inspect(ctx, &inspected, value); |
| 1577 | GRN_PLUGIN_ERROR(ctx, |
| 1578 | GRN_INVALID_ARGUMENT, |
| 1579 | "%s value must be a bool or string: <%.*s>", |
| 1580 | tag, |
| 1581 | (int)GRN_TEXT_LEN(&inspected), |
| 1582 | GRN_TEXT_VALUE(&inspected)); |
| 1583 | GRN_OBJ_FIN(ctx, &inspected); |
| 1584 | return default_value; |
| 1585 | } |
| 1586 | |
| 1587 | int32_t |
| 1588 | grn_proc_get_value_int32(grn_ctx *ctx, |
| 1589 | grn_obj *value, |
| 1590 | int32_t default_value_raw, |
| 1591 | const char *tag) |
| 1592 | { |
| 1593 | int32_t value_raw; |
| 1594 | |
| 1595 | if (!value) { |
| 1596 | return default_value_raw; |
| 1597 | } |
| 1598 | |
| 1599 | if (!grn_type_id_is_number_family(ctx, value->header.domain)) { |
| 1600 | grn_obj inspected; |
| 1601 | |
| 1602 | GRN_TEXT_INIT(&inspected, 0); |
| 1603 | grn_inspect(ctx, &inspected, value); |
| 1604 | GRN_PLUGIN_ERROR(ctx, |
| 1605 | GRN_INVALID_ARGUMENT, |
| 1606 | "%s value must be a number: <%.*s>", |
| 1607 | tag, |
| 1608 | (int)GRN_TEXT_LEN(&inspected), |
| 1609 | GRN_TEXT_VALUE(&inspected)); |
| 1610 | GRN_OBJ_FIN(ctx, &inspected); |
| 1611 | return default_value_raw; |
| 1612 | } |
| 1613 | |
| 1614 | if (value->header.domain == GRN_DB_INT32) { |
| 1615 | value_raw = GRN_INT32_VALUE(value); |
| 1616 | } else if (value->header.domain == GRN_DB_INT64) { |
| 1617 | value_raw = (int32_t)GRN_INT64_VALUE(value); |
| 1618 | } else { |
| 1619 | grn_obj buffer; |
| 1620 | grn_rc rc; |
| 1621 | |
| 1622 | GRN_INT32_INIT(&buffer, 0); |
| 1623 | rc = grn_obj_cast(ctx, value, &buffer, false); |
| 1624 | if (rc == GRN_SUCCESS) { |
| 1625 | value_raw = GRN_INT32_VALUE(&buffer); |
| 1626 | } |
| 1627 | GRN_OBJ_FIN(ctx, &buffer); |
| 1628 | |
| 1629 | if (rc != GRN_SUCCESS) { |
| 1630 | grn_obj inspected; |
| 1631 | |
| 1632 | GRN_TEXT_INIT(&inspected, 0); |
| 1633 | grn_inspect(ctx, &inspected, value); |
| 1634 | GRN_PLUGIN_ERROR(ctx, |
| 1635 | rc, |
| 1636 | "%s " |
| 1637 | "failed to cast value to number: <%.*s>", |
| 1638 | tag, |
| 1639 | (int)GRN_TEXT_LEN(&inspected), |
| 1640 | GRN_TEXT_VALUE(&inspected)); |
| 1641 | GRN_OBJ_FIN(ctx, &inspected); |
| 1642 | value_raw = default_value_raw; |
| 1643 | } |
| 1644 | } |
| 1645 | |
| 1646 | return value_raw; |
| 1647 | } |
| 1648 | |
| 1649 | uint32_t |
| 1650 | grn_proc_get_value_uint32(grn_ctx *ctx, |
| 1651 | grn_obj *value, |
| 1652 | uint32_t default_value_raw, |
| 1653 | const char *tag) |
| 1654 | { |
| 1655 | uint32_t value_raw; |
| 1656 | |
| 1657 | if (!value) { |
| 1658 | return default_value_raw; |
| 1659 | } |
| 1660 | |
| 1661 | if (!grn_type_id_is_number_family(ctx, value->header.domain)) { |
| 1662 | grn_obj inspected; |
| 1663 | |
| 1664 | GRN_TEXT_INIT(&inspected, 0); |
| 1665 | grn_inspect(ctx, &inspected, value); |
| 1666 | GRN_PLUGIN_ERROR(ctx, |
| 1667 | GRN_INVALID_ARGUMENT, |
| 1668 | "%s value must be a number: <%.*s>", |
| 1669 | tag, |
| 1670 | (int)GRN_TEXT_LEN(&inspected), |
| 1671 | GRN_TEXT_VALUE(&inspected)); |
| 1672 | GRN_OBJ_FIN(ctx, &inspected); |
| 1673 | return default_value_raw; |
| 1674 | } |
| 1675 | |
| 1676 | if (value->header.domain == GRN_DB_UINT32) { |
| 1677 | value_raw = GRN_UINT32_VALUE(value); |
| 1678 | } else if (value->header.domain == GRN_DB_INT32) { |
| 1679 | value_raw = (uint32_t)GRN_INT32_VALUE(value); |
| 1680 | } else if (value->header.domain == GRN_DB_UINT64) { |
| 1681 | value_raw = (uint32_t)GRN_UINT64_VALUE(value); |
| 1682 | } else if (value->header.domain == GRN_DB_INT64) { |
| 1683 | value_raw = (uint32_t)GRN_INT64_VALUE(value); |
| 1684 | } else { |
| 1685 | grn_obj buffer; |
| 1686 | grn_rc rc; |
| 1687 | |
| 1688 | GRN_UINT32_INIT(&buffer, 0); |
| 1689 | rc = grn_obj_cast(ctx, value, &buffer, false); |
| 1690 | if (rc == GRN_SUCCESS) { |
| 1691 | value_raw = GRN_UINT32_VALUE(&buffer); |
| 1692 | } |
| 1693 | GRN_OBJ_FIN(ctx, &buffer); |
| 1694 | |
| 1695 | if (rc != GRN_SUCCESS) { |
| 1696 | grn_obj inspected; |
| 1697 | |
| 1698 | GRN_TEXT_INIT(&inspected, 0); |
| 1699 | grn_inspect(ctx, &inspected, value); |
| 1700 | GRN_PLUGIN_ERROR(ctx, |
| 1701 | rc, |
| 1702 | "%s " |
| 1703 | "failed to cast value to number: <%.*s>", |
| 1704 | tag, |
| 1705 | (int)GRN_TEXT_LEN(&inspected), |
| 1706 | GRN_TEXT_VALUE(&inspected)); |
| 1707 | GRN_OBJ_FIN(ctx, &inspected); |
| 1708 | value_raw = default_value_raw; |
| 1709 | } |
| 1710 | } |
| 1711 | |
| 1712 | return value_raw; |
| 1713 | } |
| 1714 | |
| 1715 | int64_t |
| 1716 | grn_proc_get_value_int64(grn_ctx *ctx, |
| 1717 | grn_obj *value, |
| 1718 | int64_t default_value_raw, |
| 1719 | const char *tag) |
| 1720 | { |
| 1721 | int64_t value_raw; |
| 1722 | |
| 1723 | if (!value) { |
| 1724 | return default_value_raw; |
| 1725 | } |
| 1726 | |
| 1727 | if (!grn_type_id_is_number_family(ctx, value->header.domain)) { |
| 1728 | grn_obj inspected; |
| 1729 | |
| 1730 | GRN_TEXT_INIT(&inspected, 0); |
| 1731 | grn_inspect(ctx, &inspected, value); |
| 1732 | GRN_PLUGIN_ERROR(ctx, |
| 1733 | GRN_INVALID_ARGUMENT, |
| 1734 | "%s value must be a number: <%.*s>", |
| 1735 | tag, |
| 1736 | (int)GRN_TEXT_LEN(&inspected), |
| 1737 | GRN_TEXT_VALUE(&inspected)); |
| 1738 | GRN_OBJ_FIN(ctx, &inspected); |
| 1739 | return default_value_raw; |
| 1740 | } |
| 1741 | |
| 1742 | if (value->header.domain == GRN_DB_INT32) { |
| 1743 | value_raw = GRN_INT32_VALUE(value); |
| 1744 | } else if (value->header.domain == GRN_DB_INT64) { |
| 1745 | value_raw = GRN_INT64_VALUE(value); |
| 1746 | } else { |
| 1747 | grn_obj buffer; |
| 1748 | grn_rc rc; |
| 1749 | |
| 1750 | GRN_INT64_INIT(&buffer, 0); |
| 1751 | rc = grn_obj_cast(ctx, value, &buffer, false); |
| 1752 | if (rc == GRN_SUCCESS) { |
| 1753 | value_raw = GRN_INT64_VALUE(&buffer); |
| 1754 | } |
| 1755 | GRN_OBJ_FIN(ctx, &buffer); |
| 1756 | |
| 1757 | if (rc != GRN_SUCCESS) { |
| 1758 | grn_obj inspected; |
| 1759 | |
| 1760 | GRN_TEXT_INIT(&inspected, 0); |
| 1761 | grn_inspect(ctx, &inspected, value); |
| 1762 | GRN_PLUGIN_ERROR(ctx, |
| 1763 | rc, |
| 1764 | "%s " |
| 1765 | "failed to cast value to number: <%.*s>", |
| 1766 | tag, |
| 1767 | (int)GRN_TEXT_LEN(&inspected), |
| 1768 | GRN_TEXT_VALUE(&inspected)); |
| 1769 | GRN_OBJ_FIN(ctx, &inspected); |
| 1770 | value_raw = default_value_raw; |
| 1771 | } |
| 1772 | } |
| 1773 | |
| 1774 | return value_raw; |
| 1775 | } |
| 1776 | |
| 1777 | grn_operator |
| 1778 | grn_proc_get_value_mode(grn_ctx *ctx, |
| 1779 | grn_obj *value, |
| 1780 | grn_operator default_mode, |
| 1781 | const char *tag) |
| 1782 | { |
| 1783 | if (!value) { |
| 1784 | return default_mode; |
| 1785 | } |
| 1786 | |
| 1787 | if (value->header.domain != GRN_DB_TEXT) { |
| 1788 | grn_obj inspected; |
| 1789 | GRN_TEXT_INIT(&inspected, 0); |
| 1790 | grn_inspect(ctx, &inspected, value); |
| 1791 | GRN_PLUGIN_ERROR(ctx, |
| 1792 | GRN_INVALID_ARGUMENT, |
| 1793 | "%s mode must be text: <%.*s>", |
| 1794 | tag, |
| 1795 | (int)GRN_TEXT_LEN(&inspected), |
| 1796 | GRN_TEXT_VALUE(&inspected)); |
| 1797 | GRN_OBJ_FIN(ctx, &inspected); |
| 1798 | return default_mode; |
| 1799 | } |
| 1800 | |
| 1801 | if (GRN_TEXT_LEN(value) == 0) { |
| 1802 | return default_mode; |
| 1803 | } |
| 1804 | |
| 1805 | #define EQUAL_MODE(name) \ |
| 1806 | (GRN_TEXT_LEN(value) == strlen(name) && \ |
| 1807 | memcmp(GRN_TEXT_VALUE(value), name, strlen(name)) == 0) |
| 1808 | |
| 1809 | if (EQUAL_MODE("==") || EQUAL_MODE("EQUAL")) { |
| 1810 | return GRN_OP_EQUAL; |
| 1811 | } else if (EQUAL_MODE("!=") || EQUAL_MODE("NOT_EQUAL")) { |
| 1812 | return GRN_OP_NOT_EQUAL; |
| 1813 | } else if (EQUAL_MODE("<") || EQUAL_MODE("LESS")) { |
| 1814 | return GRN_OP_LESS; |
| 1815 | } else if (EQUAL_MODE(">") || EQUAL_MODE("GREATER")) { |
| 1816 | return GRN_OP_GREATER; |
| 1817 | } else if (EQUAL_MODE("<=") || EQUAL_MODE("LESS_EQUAL")) { |
| 1818 | return GRN_OP_LESS_EQUAL; |
| 1819 | } else if (EQUAL_MODE(">=") || EQUAL_MODE("GREATER_EQUAL")) { |
| 1820 | return GRN_OP_GREATER_EQUAL; |
| 1821 | } else if (EQUAL_MODE("@") || EQUAL_MODE("MATCH")) { |
| 1822 | return GRN_OP_MATCH; |
| 1823 | } else if (EQUAL_MODE("*N") || EQUAL_MODE("NEAR")) { |
| 1824 | return GRN_OP_NEAR; |
| 1825 | } else if (EQUAL_MODE("*NP") || EQUAL_MODE("NEAR_PHRASE")) { |
| 1826 | return GRN_OP_NEAR_PHRASE; |
| 1827 | } else if (EQUAL_MODE("*ONP") || EQUAL_MODE("ORDERED_NEAR_PHRASE")) { |
| 1828 | return GRN_OP_ORDERED_NEAR_PHRASE; |
| 1829 | } else if (EQUAL_MODE("*NPP") || EQUAL_MODE("NEAR_PHRASE_PRODUCT")) { |
| 1830 | return GRN_OP_NEAR_PHRASE_PRODUCT; |
| 1831 | } else if (EQUAL_MODE("*ONPP") || EQUAL_MODE("ORDERED_NEAR_PHRASE_PRODUCT")) { |
| 1832 | return GRN_OP_ORDERED_NEAR_PHRASE_PRODUCT; |
| 1833 | } else if (EQUAL_MODE("*S") || EQUAL_MODE("SIMILAR")) { |
| 1834 | return GRN_OP_SIMILAR; |
| 1835 | } else if (EQUAL_MODE("^") || EQUAL_MODE("@^") || EQUAL_MODE("PREFIX")) { |
| 1836 | return GRN_OP_PREFIX; |
| 1837 | } else if (EQUAL_MODE("$") || EQUAL_MODE("@$") || EQUAL_MODE("SUFFIX")) { |
| 1838 | return GRN_OP_SUFFIX; |
| 1839 | } else if (EQUAL_MODE("~") || EQUAL_MODE("@~") || EQUAL_MODE("REGEXP")) { |
| 1840 | return GRN_OP_REGEXP; |
| 1841 | } else { |
| 1842 | GRN_PLUGIN_ERROR(ctx, |
| 1843 | GRN_INVALID_ARGUMENT, |
| 1844 | "%s mode must be one of them: " |
| 1845 | "[" |
| 1846 | "\"==\", \"EQUAL\", " |
| 1847 | "\"!=\", \"NOT_EQUAL\", " |
| 1848 | "\"<\", \"LESS\", " |
| 1849 | "\">\", \"GREATER\", " |
| 1850 | "\"<=\", \"LESS_EQUAL\", " |
| 1851 | "\">=\", \"GREATER_EQUAL\", " |
| 1852 | "\"@\", \"MATCH\", " |
| 1853 | "\"*N\", \"NEAR\", " |
| 1854 | "\"*NP\", \"NEAR_PHRASE\", " |
| 1855 | "\"*ONP\", \"ORDERED_NEAR_PHRASE\", " |
| 1856 | "\"*NPP\", \"NEAR_PHRASE_PRODUCT\", " |
| 1857 | "\"*ONPP\", \"ORDERED_NEAR_PHRASE_PRODUCT\", " |
| 1858 | "\"*S\", \"SIMILAR\", " |
| 1859 | "\"^\", \"@^\", \"PREFIX\", " |
| 1860 | "\"$\", \"@$\", \"SUFFIX\", " |
| 1861 | "\"~\", \"@~\", \"REGEXP\"" |
| 1862 | "]: <%.*s>", |
| 1863 | tag, |
| 1864 | (int)GRN_TEXT_LEN(value), |
| 1865 | GRN_TEXT_VALUE(value)); |
| 1866 | return default_mode; |
| 1867 | } |
| 1868 | |
| 1869 | #undef EQUAL_MODE |
| 1870 | } |
| 1871 | |
| 1872 | grn_operator |
| 1873 | grn_proc_get_value_operator(grn_ctx *ctx, |
| 1874 | grn_obj *value, |
| 1875 | grn_operator default_operator, |
| 1876 | const char *tag) |
| 1877 | { |
| 1878 | if (!value) { |
| 1879 | return default_operator; |
| 1880 | } |
| 1881 | |
| 1882 | if (value->header.domain != GRN_DB_TEXT) { |
| 1883 | grn_obj inspected; |
| 1884 | GRN_TEXT_INIT(&inspected, 0); |
| 1885 | grn_inspect(ctx, &inspected, value); |
| 1886 | GRN_PLUGIN_ERROR(ctx, |
| 1887 | GRN_INVALID_ARGUMENT, |
| 1888 | "%s operator must be text: <%.*s>", |
| 1889 | tag, |
| 1890 | (int)GRN_TEXT_LEN(&inspected), |
| 1891 | GRN_TEXT_VALUE(&inspected)); |
| 1892 | GRN_OBJ_FIN(ctx, &inspected); |
| 1893 | return default_operator; |
| 1894 | } |
| 1895 | |
| 1896 | if (GRN_TEXT_LEN(value) == 0) { |
| 1897 | return default_operator; |
| 1898 | } |
| 1899 | |
| 1900 | grn_raw_string operator_string; |
| 1901 | GRN_RAW_STRING_SET(operator_string, value); |
| 1902 | |
| 1903 | if (GRN_RAW_STRING_EQUAL_CSTRING(operator_string, "&&") || |
| 1904 | GRN_RAW_STRING_EQUAL_CSTRING(operator_string, "+") || |
| 1905 | GRN_RAW_STRING_EQUAL_CSTRING(operator_string, "AND")) { |
| 1906 | return GRN_OP_AND; |
| 1907 | } else if (GRN_RAW_STRING_EQUAL_CSTRING(operator_string, "||") || |
| 1908 | GRN_RAW_STRING_EQUAL_CSTRING(operator_string, "OR")) { |
| 1909 | return GRN_OP_OR; |
| 1910 | } else if (GRN_RAW_STRING_EQUAL_CSTRING(operator_string, "!") || |
| 1911 | GRN_RAW_STRING_EQUAL_CSTRING(operator_string, "NOT")) { |
| 1912 | return GRN_OP_NOT; |
| 1913 | } else if (GRN_RAW_STRING_EQUAL_CSTRING(operator_string, "&!") || |
| 1914 | GRN_RAW_STRING_EQUAL_CSTRING(operator_string, "-") || |
| 1915 | GRN_RAW_STRING_EQUAL_CSTRING(operator_string, "AND_NOT")) { |
| 1916 | return GRN_OP_AND_NOT; |
| 1917 | } else { |
| 1918 | GRN_PLUGIN_ERROR(ctx, |
| 1919 | GRN_INVALID_ARGUMENT, |
| 1920 | "%s operator must be one of them: " |
| 1921 | "[" |
| 1922 | "\"&&\", \"+\", \"AND\", " |
| 1923 | "\"||\", \"OR\", " |
| 1924 | "\"!\" \"NOT\", " |
| 1925 | "\"&!\", \"-\", \"AND_NOT\"" |
| 1926 | "]: <%.*s>", |
| 1927 | tag, |
| 1928 | (int)operator_string.length, |
| 1929 | operator_string.value); |
| 1930 | return default_operator; |
| 1931 | } |
| 1932 | } |
| 1933 | |
| 1934 | grn_tokenize_mode |
| 1935 | grn_proc_get_value_tokenize_mode(grn_ctx *ctx, |
| 1936 | grn_obj *value, |
| 1937 | grn_tokenize_mode default_mode, |
| 1938 | const char *tag) |
| 1939 | { |
| 1940 | if (!value) { |
| 1941 | return default_mode; |
| 1942 | } |
| 1943 | |
| 1944 | if (!grn_obj_is_text_family_bulk(ctx, value)) { |
| 1945 | grn_obj inspected; |
| 1946 | GRN_TEXT_INIT(&inspected, 0); |
| 1947 | grn_inspect(ctx, &inspected, value); |
| 1948 | GRN_PLUGIN_ERROR(ctx, |
| 1949 | GRN_INVALID_ARGUMENT, |
| 1950 | "%s tokenize mode must be text bulk: <%.*s>", |
| 1951 | tag, |
| 1952 | (int)GRN_TEXT_LEN(&inspected), |
| 1953 | GRN_TEXT_VALUE(&inspected)); |
| 1954 | GRN_OBJ_FIN(ctx, &inspected); |
| 1955 | return default_mode; |
| 1956 | } |
| 1957 | |
| 1958 | if (GRN_TEXT_LEN(value) == 0) { |
| 1959 | return default_mode; |
| 1960 | } |
| 1961 | |
| 1962 | #define EQUAL_MODE(name) \ |
| 1963 | (GRN_TEXT_LEN(value) == strlen(name) && \ |
| 1964 | memcmp(GRN_TEXT_VALUE(value), name, strlen(name)) == 0) |
| 1965 | |
| 1966 | if (EQUAL_MODE("GET")) { |
| 1967 | return GRN_TOKENIZE_GET; |
| 1968 | } else if (EQUAL_MODE("ADD")) { |
| 1969 | return GRN_TOKENIZE_ADD; |
| 1970 | } else if (EQUAL_MODE("ONLY")) { |
| 1971 | return GRN_TOKENIZE_ONLY; |
| 1972 | } else { |
| 1973 | GRN_PLUGIN_ERROR(ctx, |
| 1974 | GRN_INVALID_ARGUMENT, |
| 1975 | "%s tokenize mode must be one of them: " |
| 1976 | "[" |
| 1977 | "\"GET\", " |
| 1978 | "\"ADD\", " |
| 1979 | "\"ONLY\"" |
| 1980 | "]: <%.*s>", |
| 1981 | tag, |
| 1982 | (int)GRN_TEXT_LEN(value), |
| 1983 | GRN_TEXT_VALUE(value)); |
| 1984 | return default_mode; |
| 1985 | } |
| 1986 | |
| 1987 | #undef EQUAL_MODE |
| 1988 | } |
| 1989 | |
| 1990 | uint32_t |
| 1991 | grn_proc_get_value_token_cursor_flags(grn_ctx *ctx, |
| 1992 | grn_obj *value, |
| 1993 | uint32_t default_flags, |
| 1994 | const char *tag) |
| 1995 | { |
| 1996 | if (!value) { |
| 1997 | return default_flags; |
| 1998 | } |
| 1999 | |
| 2000 | if (!grn_obj_is_text_family_bulk(ctx, value)) { |
| 2001 | grn_obj inspected; |
| 2002 | GRN_TEXT_INIT(&inspected, 0); |
| 2003 | grn_inspect(ctx, &inspected, value); |
| 2004 | GRN_PLUGIN_ERROR(ctx, |
| 2005 | GRN_INVALID_ARGUMENT, |
| 2006 | "%s token cursor flags must be text bulk: <%.*s>", |
| 2007 | tag, |
| 2008 | (int)GRN_TEXT_LEN(&inspected), |
| 2009 | GRN_TEXT_VALUE(&inspected)); |
| 2010 | GRN_OBJ_FIN(ctx, &inspected); |
| 2011 | return default_flags; |
| 2012 | } |
| 2013 | |
| 2014 | if (GRN_TEXT_LEN(value) == 0) { |
| 2015 | return default_flags; |
| 2016 | } |
| 2017 | |
| 2018 | uint32_t flags = 0; |
| 2019 | const char *names, *names_end; |
| 2020 | names = GRN_TEXT_VALUE(value); |
| 2021 | names_end = names + GRN_TEXT_LEN(value); |
| 2022 | while (names < names_end) { |
| 2023 | if (*names == '|' || *names == ' ') { |
| 2024 | names += 1; |
| 2025 | continue; |
| 2026 | } |
| 2027 | |
| 2028 | #define CHECK_FLAG(name) \ |
| 2029 | if (((size_t)(names_end - names) >= (sizeof(#name) - 1)) && \ |
| 2030 | (!memcmp(names, #name, sizeof(#name) - 1))) { \ |
| 2031 | flags |= GRN_TOKEN_CURSOR_##name; \ |
| 2032 | names += sizeof(#name) - 1; \ |
| 2033 | continue; \ |
| 2034 | } |
| 2035 | |
| 2036 | CHECK_FLAG(ENABLE_TOKENIZED_DELIMITER); |
| 2037 | |
| 2038 | #define GRN_TOKEN_CURSOR_NONE 0 |
| 2039 | CHECK_FLAG(NONE); |
| 2040 | #undef GRN_TOKEN_CURSOR_NONE |
| 2041 | |
| 2042 | GRN_PLUGIN_ERROR(ctx, |
| 2043 | GRN_INVALID_ARGUMENT, |
| 2044 | "%s token cursor flag must be one of them: " |
| 2045 | "[" |
| 2046 | "\"ENABLE_TOKENIZED_DELIMITER\", " |
| 2047 | "\"NONE\"" |
| 2048 | "]: <%.*s>", |
| 2049 | tag, |
| 2050 | (int)(names_end - names), |
| 2051 | names); |
| 2052 | return default_flags; |
| 2053 | #undef CHECK_FLAG |
| 2054 | } |
| 2055 | |
| 2056 | return flags; |
| 2057 | } |
| 2058 | |
| 2059 | double |
| 2060 | grn_proc_get_value_double(grn_ctx *ctx, |
| 2061 | grn_obj *value, |
| 2062 | double default_value_raw, |
| 2063 | const char *tag) |
| 2064 | { |
| 2065 | double value_raw; |
| 2066 | |
| 2067 | if (!value) { |
| 2068 | return default_value_raw; |
| 2069 | } |
| 2070 | |
| 2071 | if (!grn_type_id_is_number_family(ctx, value->header.domain)) { |
| 2072 | grn_obj inspected; |
| 2073 | |
| 2074 | GRN_TEXT_INIT(&inspected, 0); |
| 2075 | grn_inspect(ctx, &inspected, value); |
| 2076 | GRN_PLUGIN_ERROR(ctx, |
| 2077 | GRN_INVALID_ARGUMENT, |
| 2078 | "%s value must be a number: <%.*s>", |
| 2079 | tag, |
| 2080 | (int)GRN_TEXT_LEN(&inspected), |
| 2081 | GRN_TEXT_VALUE(&inspected)); |
| 2082 | GRN_OBJ_FIN(ctx, &inspected); |
| 2083 | return default_value_raw; |
| 2084 | } |
| 2085 | |
| 2086 | if (value->header.domain == GRN_DB_FLOAT) { |
| 2087 | value_raw = GRN_FLOAT_VALUE(value); |
| 2088 | } else if (value->header.domain == GRN_DB_FLOAT32) { |
| 2089 | value_raw = GRN_FLOAT32_VALUE(value); |
| 2090 | #ifdef GRN_HAVE_BFLOAT16 |
| 2091 | } else if (value->header.domain == GRN_DB_BFLOAT16) { |
| 2092 | value_raw = grn_bfloat16_to_float32(GRN_BFLOAT16_VALUE(value)); |
| 2093 | #endif |
| 2094 | } else { |
| 2095 | grn_obj buffer; |
| 2096 | grn_rc rc; |
| 2097 | |
| 2098 | GRN_FLOAT_INIT(&buffer, 0); |
| 2099 | rc = grn_obj_cast(ctx, value, &buffer, false); |
| 2100 | if (rc == GRN_SUCCESS) { |
| 2101 | value_raw = GRN_FLOAT_VALUE(&buffer); |
| 2102 | } |
| 2103 | GRN_OBJ_FIN(ctx, &buffer); |
| 2104 | |
| 2105 | if (rc != GRN_SUCCESS) { |
| 2106 | grn_obj inspected; |
| 2107 | |
| 2108 | GRN_TEXT_INIT(&inspected, 0); |
| 2109 | grn_inspect(ctx, &inspected, value); |
| 2110 | GRN_PLUGIN_ERROR(ctx, |
| 2111 | rc, |
| 2112 | "%s " |
| 2113 | "failed to cast value to number: <%.*s>", |
| 2114 | tag, |
| 2115 | (int)GRN_TEXT_LEN(&inspected), |
| 2116 | GRN_TEXT_VALUE(&inspected)); |
| 2117 | GRN_OBJ_FIN(ctx, &inspected); |
| 2118 | value_raw = default_value_raw; |
| 2119 | } |
| 2120 | } |
| 2121 | |
| 2122 | return value_raw; |
| 2123 | } |
| 2124 | |
| 2125 | grn_obj * |
| 2126 | grn_proc_get_value_object(grn_ctx *ctx, grn_obj *value, const char *tag) |
| 2127 | { |
| 2128 | if (!value) { |
| 2129 | return NULL; |
| 2130 | } |
| 2131 | |
| 2132 | if (!grn_obj_is_text_family_bulk(ctx, value)) { |
| 2133 | return value; |
| 2134 | } |
| 2135 | |
| 2136 | if (GRN_TEXT_LEN(value) == 0) { |
| 2137 | GRN_PLUGIN_ERROR(ctx, |
| 2138 | GRN_INVALID_ARGUMENT, |
| 2139 | "%s object name isn't specified", |
| 2140 | tag); |
| 2141 | return NULL; |
| 2142 | } |
| 2143 | |
| 2144 | grn_obj *object = |
| 2145 | grn_ctx_get(ctx, GRN_TEXT_VALUE(value), GRN_TEXT_LEN(value)); |
| 2146 | if (!object) { |
| 2147 | GRN_PLUGIN_ERROR(ctx, |
| 2148 | GRN_INVALID_ARGUMENT, |
| 2149 | "%s object doesn't exist: <%.*s>", |
| 2150 | tag, |
| 2151 | (int)GRN_TEXT_LEN(value), |
| 2152 | GRN_TEXT_VALUE(value)); |
| 2153 | return NULL; |
| 2154 | } |
| 2155 | |
| 2156 | return object; |
| 2157 | } |
| 2158 | |
| 2159 | grn_obj * |
| 2160 | grn_proc_get_value_column(grn_ctx *ctx, |
| 2161 | grn_obj *value, |
| 2162 | grn_obj *table, |
| 2163 | const char *tag) |
| 2164 | { |
| 2165 | if (!value) { |
| 2166 | return NULL; |
| 2167 | } |
| 2168 | |
| 2169 | if (!grn_obj_is_text_family_bulk(ctx, value)) { |
| 2170 | return value; |
| 2171 | } |
| 2172 | |
| 2173 | if (GRN_TEXT_LEN(value) == 0) { |
| 2174 | GRN_PLUGIN_ERROR(ctx, |
| 2175 | GRN_INVALID_ARGUMENT, |
| 2176 | "%s column name isn't specified", |
| 2177 | tag); |
| 2178 | return NULL; |
| 2179 | } |
| 2180 | |
| 2181 | grn_obj *column = |
| 2182 | grn_obj_column(ctx, table, GRN_TEXT_VALUE(value), GRN_TEXT_LEN(value)); |
| 2183 | if (!column) { |
| 2184 | grn_obj inspected; |
| 2185 | GRN_TEXT_INIT(&inspected, 0); |
| 2186 | grn_inspect_limited(ctx, &inspected, table); |
| 2187 | GRN_PLUGIN_ERROR(ctx, |
| 2188 | GRN_INVALID_ARGUMENT, |
| 2189 | "%s column doesn't exist: <%.*s>: %.*s", |
| 2190 | tag, |
| 2191 | (int)GRN_TEXT_LEN(value), |
| 2192 | GRN_TEXT_VALUE(value), |
| 2193 | (int)GRN_TEXT_LEN(&inspected), |
| 2194 | GRN_TEXT_VALUE(&inspected)); |
| 2195 | GRN_OBJ_FIN(ctx, &inspected); |
| 2196 | return NULL; |
| 2197 | } |
| 2198 | |
| 2199 | return column; |
| 2200 | } |
| 2201 | |
| 2202 | grn_raw_string |
| 2203 | grn_proc_get_value_raw_string(grn_ctx *ctx, |
| 2204 | grn_obj *value, |
| 2205 | grn_raw_string *default_value, |
| 2206 | const char *tag) |
| 2207 | { |
| 2208 | if (!value) { |
| 2209 | return (grn_raw_string){NULL, 0}; |
| 2210 | } |
| 2211 | |
| 2212 | if (!grn_obj_is_text_family_bulk(ctx, value)) { |
| 2213 | return *default_value; |
| 2214 | } |
| 2215 | |
| 2216 | return (grn_raw_string){GRN_TEXT_VALUE(value), GRN_TEXT_LEN(value)}; |
| 2217 | } |
| 2218 | |
| 2219 | static grn_obj * |
| 2220 | proc_cache_limit(grn_ctx *ctx, |
| 2221 | int nargs, |
| 2222 | grn_obj **args, |
| 2223 | grn_user_data *user_data) |
| 2224 | { |
| 2225 | grn_cache *cache; |
| 2226 | unsigned int current_max_n_entries; |
| 2227 | |
| 2228 | cache = grn_cache_current_get(ctx); |
| 2229 | current_max_n_entries = grn_cache_get_max_n_entries(ctx, cache); |
| 2230 | if (GRN_TEXT_LEN(VAR(0))) { |
| 2231 | const char *rest; |
| 2232 | uint32_t max = |
| 2233 | grn_atoui(GRN_TEXT_VALUE(VAR(0)), GRN_BULK_CURR(VAR(0)), &rest); |
| 2234 | if (GRN_BULK_CURR(VAR(0)) == rest) { |
| 2235 | grn_cache_set_max_n_entries(ctx, cache, max); |
| 2236 | } else { |
| 2237 | ERR(GRN_INVALID_ARGUMENT, |
| 2238 | "max value is invalid unsigned integer format: <%.*s>", |
| 2239 | (int)GRN_TEXT_LEN(VAR(0)), |
| 2240 | GRN_TEXT_VALUE(VAR(0))); |
| 2241 | } |
| 2242 | } |
| 2243 | if (ctx->rc == GRN_SUCCESS) { |
| 2244 | GRN_OUTPUT_INT64(current_max_n_entries); |
| 2245 | } |
| 2246 | return NULL; |
| 2247 | } |
| 2248 | |
| 2249 | static grn_obj * |
| 2250 | proc_register(grn_ctx *ctx, int nargs, grn_obj **args, grn_user_data *user_data) |
| 2251 | { |
| 2252 | if (GRN_TEXT_LEN(VAR(0))) { |
| 2253 | const char *name; |
| 2254 | GRN_TEXT_PUTC(ctx, VAR(0), '\0'); |
| 2255 | name = GRN_TEXT_VALUE(VAR(0)); |
| 2256 | grn_plugin_register(ctx, name); |
| 2257 | } else { |
| 2258 | ERR(GRN_INVALID_ARGUMENT, "path is required"); |
| 2259 | } |
| 2260 | GRN_OUTPUT_BOOL(!ctx->rc); |
| 2261 | return NULL; |
| 2262 | } |
| 2263 | |
| 2264 | void |
| 2265 | grn_ii_buffer_check(grn_ctx *ctx, grn_ii *ii, uint32_t seg); |
| 2266 | |
| 2267 | static grn_obj * |
| 2268 | proc_check(grn_ctx *ctx, int nargs, grn_obj **args, grn_user_data *user_data) |
| 2269 | { |
| 2270 | grn_obj *obj = grn_ctx_get(ctx, GRN_TEXT_VALUE(VAR(0)), GRN_TEXT_LEN(VAR(0))); |
| 2271 | if (!obj) { |
| 2272 | ERR(GRN_INVALID_ARGUMENT, |
| 2273 | "no such object: <%.*s>", |
| 2274 | (int)GRN_TEXT_LEN(VAR(0)), |
| 2275 | GRN_TEXT_VALUE(VAR(0))); |
| 2276 | GRN_OUTPUT_BOOL(!ctx->rc); |
| 2277 | } else { |
| 2278 | switch (obj->header.type) { |
| 2279 | case GRN_DB: |
| 2280 | GRN_OUTPUT_BOOL(!ctx->rc); |
| 2281 | break; |
| 2282 | case GRN_TABLE_PAT_KEY: |
| 2283 | grn_pat_check(ctx, (grn_pat *)obj); |
| 2284 | break; |
| 2285 | case GRN_TABLE_HASH_KEY: |
| 2286 | grn_hash_check(ctx, (grn_hash *)obj); |
| 2287 | break; |
| 2288 | case GRN_TABLE_DAT_KEY: |
| 2289 | case GRN_TABLE_NO_KEY: |
| 2290 | case GRN_COLUMN_FIX_SIZE: |
| 2291 | GRN_OUTPUT_BOOL(!ctx->rc); |
| 2292 | break; |
| 2293 | case GRN_COLUMN_VAR_SIZE: |
| 2294 | grn_ja_check(ctx, (grn_ja *)obj); |
| 2295 | break; |
| 2296 | case GRN_COLUMN_INDEX: |
| 2297 | { |
| 2298 | grn_ii *ii = (grn_ii *)obj; |
| 2299 | grn_ii_header_common *h = ii->header.common; |
| 2300 | char buf[8]; |
| 2301 | GRN_OUTPUT_ARRAY_OPEN("RESULT", 8); |
| 2302 | { |
| 2303 | uint32_t i, j, g = 0, a = 0, b = 0; |
| 2304 | uint32_t max = 0; |
| 2305 | for (i = h->bgqtail; i != h->bgqhead; |
| 2306 | i = ((i + 1) & (GRN_II_BGQSIZE - 1))) { |
| 2307 | j = h->bgqbody[i]; |
| 2308 | g++; |
| 2309 | if (j > max) { |
| 2310 | max = j; |
| 2311 | } |
| 2312 | } |
| 2313 | for (i = 0; i < GRN_II_MAX_LSEG; i++) { |
| 2314 | j = h->binfo[i]; |
| 2315 | if (j != GRN_II_PSEG_NOT_ASSIGNED) { |
| 2316 | if (j > max) { |
| 2317 | max = j; |
| 2318 | } |
| 2319 | b++; |
| 2320 | } |
| 2321 | } |
| 2322 | for (i = 0; i < GRN_II_MAX_LSEG; i++) { |
| 2323 | j = h->ainfo[i]; |
| 2324 | if (j != GRN_II_PSEG_NOT_ASSIGNED) { |
| 2325 | if (j > max) { |
| 2326 | max = j; |
| 2327 | } |
| 2328 | a++; |
| 2329 | } |
| 2330 | } |
| 2331 | GRN_OUTPUT_MAP_OPEN("SUMMARY", 12); |
| 2332 | GRN_OUTPUT_CSTR("flags"); |
| 2333 | grn_itoh(h->flags, buf, 8); |
| 2334 | GRN_OUTPUT_STR(buf, 8); |
| 2335 | GRN_OUTPUT_CSTR("max sid"); |
| 2336 | GRN_OUTPUT_INT64(h->smax); |
| 2337 | GRN_OUTPUT_CSTR("number of garbage segments"); |
| 2338 | GRN_OUTPUT_INT64(g); |
| 2339 | GRN_OUTPUT_CSTR("number of array segments"); |
| 2340 | GRN_OUTPUT_INT64(a); |
| 2341 | GRN_OUTPUT_CSTR("max id of array segment"); |
| 2342 | GRN_OUTPUT_INT64(h->amax); |
| 2343 | GRN_OUTPUT_CSTR("number of buffer segments"); |
| 2344 | GRN_OUTPUT_INT64(b); |
| 2345 | GRN_OUTPUT_CSTR("max id of buffer segment"); |
| 2346 | GRN_OUTPUT_INT64(h->bmax); |
| 2347 | GRN_OUTPUT_CSTR("max id of physical segment in use"); |
| 2348 | GRN_OUTPUT_INT64(max); |
| 2349 | GRN_OUTPUT_CSTR("number of unmanaged segments"); |
| 2350 | GRN_OUTPUT_INT64(h->pnext - a - b - g); |
| 2351 | GRN_OUTPUT_CSTR("total chunk size"); |
| 2352 | GRN_OUTPUT_INT64(h->total_chunk_size); |
| 2353 | for (max = 0, i = 0; i < (GRN_II_MAX_CHUNK >> 3); i++) { |
| 2354 | if ((j = h->chunks[i])) { |
| 2355 | int k; |
| 2356 | for (k = 0; k < 8; k++) { |
| 2357 | if ((j & (1 << k))) { |
| 2358 | max = (i << 3) + j; |
| 2359 | } |
| 2360 | } |
| 2361 | } |
| 2362 | } |
| 2363 | GRN_OUTPUT_CSTR("max id of chunk segments in use"); |
| 2364 | GRN_OUTPUT_INT64(max); |
| 2365 | GRN_OUTPUT_CSTR("number of garbage chunk"); |
| 2366 | GRN_OUTPUT_ARRAY_OPEN("NGARBAGES", GRN_II_N_CHUNK_VARIATION); |
| 2367 | for (i = 0; i <= GRN_II_N_CHUNK_VARIATION; i++) { |
| 2368 | GRN_OUTPUT_INT64(h->ngarbages[i]); |
| 2369 | } |
| 2370 | GRN_OUTPUT_ARRAY_CLOSE(); |
| 2371 | GRN_OUTPUT_MAP_CLOSE(); |
| 2372 | const uint32_t n_logical_segments = grn_ii_n_logical_segments(ii); |
| 2373 | for (uint32_t lseg = 0; lseg < n_logical_segments; lseg++) { |
| 2374 | const uint32_t pseg = grn_ii_get_buffer_pseg(ii, lseg); |
| 2375 | if (pseg != GRN_II_PSEG_NOT_ASSIGNED) { |
| 2376 | grn_ii_buffer_check(ctx, ii, lseg); |
| 2377 | } |
| 2378 | } |
| 2379 | } |
| 2380 | GRN_OUTPUT_ARRAY_CLOSE(); |
| 2381 | } |
| 2382 | break; |
| 2383 | } |
| 2384 | } |
| 2385 | return NULL; |
| 2386 | } |
| 2387 | |
| 2388 | static grn_obj * |
| 2389 | proc_truncate(grn_ctx *ctx, int nargs, grn_obj **args, grn_user_data *user_data) |
| 2390 | { |
| 2391 | const char *target_name; |
| 2392 | int target_name_len; |
| 2393 | |
| 2394 | target_name_len = GRN_TEXT_LEN(VAR(0)); |
| 2395 | if (target_name_len > 0) { |
| 2396 | target_name = GRN_TEXT_VALUE(VAR(0)); |
| 2397 | } else { |
| 2398 | target_name_len = GRN_TEXT_LEN(VAR(1)); |
| 2399 | if (target_name_len == 0) { |
| 2400 | ERR(GRN_INVALID_ARGUMENT, "[truncate] table name is missing"); |
| 2401 | goto exit; |
| 2402 | } |
| 2403 | target_name = GRN_TEXT_VALUE(VAR(1)); |
| 2404 | } |
| 2405 | |
| 2406 | { |
| 2407 | grn_obj *target = grn_ctx_get(ctx, target_name, target_name_len); |
| 2408 | if (!target) { |
| 2409 | ERR(GRN_INVALID_ARGUMENT, |
| 2410 | "[truncate] no such target: <%.*s>", |
| 2411 | target_name_len, |
| 2412 | target_name); |
| 2413 | goto exit; |
| 2414 | } |
| 2415 | |
| 2416 | switch (target->header.type) { |
| 2417 | case GRN_TABLE_HASH_KEY: |
| 2418 | case GRN_TABLE_PAT_KEY: |
| 2419 | case GRN_TABLE_DAT_KEY: |
| 2420 | case GRN_TABLE_NO_KEY: |
| 2421 | grn_table_truncate(ctx, target); |
| 2422 | break; |
| 2423 | case GRN_COLUMN_FIX_SIZE: |
| 2424 | case GRN_COLUMN_VAR_SIZE: |
| 2425 | case GRN_COLUMN_INDEX: |
| 2426 | grn_column_truncate(ctx, target); |
| 2427 | break; |
| 2428 | default: |
| 2429 | { |
| 2430 | grn_obj buffer; |
| 2431 | GRN_TEXT_INIT(&buffer, 0); |
| 2432 | grn_inspect(ctx, &buffer, target); |
| 2433 | ERR(GRN_INVALID_ARGUMENT, |
| 2434 | "[truncate] not a table nor column object: <%.*s>", |
| 2435 | (int)GRN_TEXT_LEN(&buffer), |
| 2436 | GRN_TEXT_VALUE(&buffer)); |
| 2437 | GRN_OBJ_FIN(ctx, &buffer); |
| 2438 | } |
| 2439 | break; |
| 2440 | } |
| 2441 | } |
| 2442 | |
| 2443 | exit: |
| 2444 | GRN_OUTPUT_BOOL(!ctx->rc); |
| 2445 | return NULL; |
| 2446 | } |
| 2447 | |
| 2448 | static void |
| 2449 | list_proc(grn_ctx *ctx, |
| 2450 | grn_proc_type target_proc_type, |
| 2451 | const char *name, |
| 2452 | const char *plural_name) |
| 2453 | { |
| 2454 | grn_obj *db; |
| 2455 | grn_table_cursor *cursor; |
| 2456 | grn_obj target_procs; |
| 2457 | |
| 2458 | db = grn_ctx_db(ctx); |
| 2459 | cursor = |
| 2460 | grn_table_cursor_open(ctx, db, NULL, 0, NULL, 0, 0, -1, GRN_CURSOR_BY_ID); |
| 2461 | if (!cursor) { |
| 2462 | return; |
| 2463 | } |
| 2464 | |
| 2465 | GRN_PTR_INIT(&target_procs, GRN_OBJ_VECTOR, GRN_ID_NIL); |
| 2466 | { |
| 2467 | grn_id id; |
| 2468 | |
| 2469 | while ((id = grn_table_cursor_next(ctx, cursor)) != GRN_ID_NIL) { |
| 2470 | grn_obj *obj; |
| 2471 | grn_proc_type proc_type; |
| 2472 | |
| 2473 | obj = grn_ctx_at(ctx, id); |
| 2474 | if (!obj) { |
| 2475 | continue; |
| 2476 | } |
| 2477 | |
| 2478 | if (obj->header.type != GRN_PROC) { |
| 2479 | grn_obj_unlink(ctx, obj); |
| 2480 | continue; |
| 2481 | } |
| 2482 | |
| 2483 | proc_type = grn_proc_get_type(ctx, obj); |
| 2484 | if (proc_type != target_proc_type) { |
| 2485 | grn_obj_unlink(ctx, obj); |
| 2486 | continue; |
| 2487 | } |
| 2488 | |
| 2489 | GRN_PTR_PUT(ctx, &target_procs, obj); |
| 2490 | } |
| 2491 | grn_table_cursor_close(ctx, cursor); |
| 2492 | |
| 2493 | { |
| 2494 | int i, n_procs; |
| 2495 | |
| 2496 | n_procs = GRN_BULK_VSIZE(&target_procs) / sizeof(grn_obj *); |
| 2497 | GRN_OUTPUT_ARRAY_OPEN(plural_name, n_procs); |
| 2498 | for (i = 0; i < n_procs; i++) { |
| 2499 | grn_obj *proc; |
| 2500 | char name[GRN_TABLE_MAX_KEY_SIZE]; |
| 2501 | int name_size; |
| 2502 | |
| 2503 | proc = GRN_PTR_VALUE_AT(&target_procs, i); |
| 2504 | name_size = grn_obj_name(ctx, proc, name, GRN_TABLE_MAX_KEY_SIZE); |
| 2505 | GRN_OUTPUT_MAP_OPEN(name, 1); |
| 2506 | GRN_OUTPUT_CSTR("name"); |
| 2507 | GRN_OUTPUT_STR(name, name_size); |
| 2508 | GRN_OUTPUT_MAP_CLOSE(); |
| 2509 | |
| 2510 | grn_obj_unlink(ctx, proc); |
| 2511 | } |
| 2512 | GRN_OUTPUT_ARRAY_CLOSE(); |
| 2513 | } |
| 2514 | |
| 2515 | grn_obj_unlink(ctx, &target_procs); |
| 2516 | } |
| 2517 | } |
| 2518 | |
| 2519 | static grn_obj * |
| 2520 | proc_tokenizer_list(grn_ctx *ctx, |
| 2521 | int nargs, |
| 2522 | grn_obj **args, |
| 2523 | grn_user_data *user_data) |
| 2524 | { |
| 2525 | list_proc(ctx, GRN_PROC_TOKENIZER, "tokenizer", "tokenizers"); |
| 2526 | return NULL; |
| 2527 | } |
| 2528 | |
| 2529 | static grn_obj * |
| 2530 | proc_normalizer_list(grn_ctx *ctx, |
| 2531 | int nargs, |
| 2532 | grn_obj **args, |
| 2533 | grn_user_data *user_data) |
| 2534 | { |
| 2535 | list_proc(ctx, GRN_PROC_NORMALIZER, "normalizer", "normalizers"); |
| 2536 | return NULL; |
| 2537 | } |
| 2538 | |
| 2539 | static grn_obj * |
| 2540 | func_rand(grn_ctx *ctx, int nargs, grn_obj **args, grn_user_data *user_data) |
| 2541 | { |
| 2542 | int val; |
| 2543 | grn_obj *obj; |
| 2544 | if (nargs > 0) { |
| 2545 | int max = GRN_INT32_VALUE(args[0]); |
| 2546 | val = (int)(1.0 * max * rand() / (RAND_MAX + 1.0)); |
| 2547 | } else { |
| 2548 | val = rand(); |
| 2549 | } |
| 2550 | if ((obj = GRN_PROC_ALLOC(GRN_DB_INT32, 0))) { |
| 2551 | GRN_INT32_SET(ctx, obj, val); |
| 2552 | } |
| 2553 | return obj; |
| 2554 | } |
| 2555 | |
| 2556 | static grn_obj * |
| 2557 | func_now(grn_ctx *ctx, int nargs, grn_obj **args, grn_user_data *user_data) |
| 2558 | { |
| 2559 | grn_obj *obj; |
| 2560 | if ((obj = GRN_PROC_ALLOC(GRN_DB_TIME, 0))) { |
| 2561 | GRN_TIME_NOW(ctx, obj); |
| 2562 | } |
| 2563 | return obj; |
| 2564 | } |
| 2565 | |
| 2566 | static inline bool |
| 2567 | is_comparable_number_type(grn_id type) |
| 2568 | { |
| 2569 | return GRN_DB_INT8 <= type && type <= GRN_DB_TIME; |
| 2570 | } |
| 2571 | |
| 2572 | static inline grn_id |
| 2573 | larger_number_type(grn_id type1, grn_id type2) |
| 2574 | { |
| 2575 | if (type1 == type2) { |
| 2576 | return type1; |
| 2577 | } |
| 2578 | |
| 2579 | switch (type1) { |
| 2580 | case GRN_DB_BFLOAT16: |
| 2581 | if (type2 == GRN_DB_FLOAT32 || type2 == GRN_DB_FLOAT || |
| 2582 | type2 == GRN_DB_TIME) { |
| 2583 | return type2; |
| 2584 | } else { |
| 2585 | return type1; |
| 2586 | } |
| 2587 | case GRN_DB_FLOAT32: |
| 2588 | if (type2 == GRN_DB_FLOAT || type2 == GRN_DB_TIME) { |
| 2589 | return type2; |
| 2590 | } else { |
| 2591 | return type1; |
| 2592 | } |
| 2593 | case GRN_DB_FLOAT: |
| 2594 | return type1; |
| 2595 | case GRN_DB_TIME: |
| 2596 | if (type2 == GRN_DB_FLOAT) { |
| 2597 | return type2; |
| 2598 | } else { |
| 2599 | return type1; |
| 2600 | } |
| 2601 | default: |
| 2602 | if (type2 > type1) { |
| 2603 | return type2; |
| 2604 | } else { |
| 2605 | return type1; |
| 2606 | } |
| 2607 | } |
| 2608 | } |
| 2609 | |
| 2610 | static inline grn_id |
| 2611 | smaller_number_type(grn_id type1, grn_id type2) |
| 2612 | { |
| 2613 | if (type1 == type2) { |
| 2614 | return type1; |
| 2615 | } |
| 2616 | |
| 2617 | switch (type1) { |
| 2618 | case GRN_DB_BFLOAT16: |
| 2619 | if (type2 == GRN_DB_FLOAT32 || type2 == GRN_DB_FLOAT || |
| 2620 | type2 == GRN_DB_TIME) { |
| 2621 | return type1; |
| 2622 | } else { |
| 2623 | return type2; |
| 2624 | } |
| 2625 | case GRN_DB_FLOAT32: |
| 2626 | if (type2 == GRN_DB_FLOAT || type2 == GRN_DB_TIME) { |
| 2627 | return type1; |
| 2628 | } else { |
| 2629 | return type2; |
| 2630 | } |
| 2631 | case GRN_DB_FLOAT: |
| 2632 | return type1; |
| 2633 | case GRN_DB_TIME: |
| 2634 | if (type2 == GRN_DB_FLOAT) { |
| 2635 | return type1; |
| 2636 | } else { |
| 2637 | return type2; |
| 2638 | } |
| 2639 | default: |
| 2640 | { |
| 2641 | grn_id smaller_number_type; |
| 2642 | if (type2 > type1) { |
| 2643 | smaller_number_type = type2; |
| 2644 | } else { |
| 2645 | smaller_number_type = type1; |
| 2646 | } |
| 2647 | switch (smaller_number_type) { |
| 2648 | case GRN_DB_UINT8: |
| 2649 | return GRN_DB_INT8; |
| 2650 | case GRN_DB_UINT16: |
| 2651 | return GRN_DB_INT16; |
| 2652 | case GRN_DB_UINT32: |
| 2653 | return GRN_DB_INT32; |
| 2654 | case GRN_DB_UINT64: |
| 2655 | return GRN_DB_INT64; |
| 2656 | default: |
| 2657 | return smaller_number_type; |
| 2658 | } |
| 2659 | } |
| 2660 | } |
| 2661 | } |
| 2662 | |
| 2663 | static inline bool |
| 2664 | is_negative_value(grn_obj *number) |
| 2665 | { |
| 2666 | switch (number->header.domain) { |
| 2667 | case GRN_DB_INT8: |
| 2668 | return GRN_INT8_VALUE(number) < 0; |
| 2669 | case GRN_DB_INT16: |
| 2670 | return GRN_INT16_VALUE(number) < 0; |
| 2671 | case GRN_DB_INT32: |
| 2672 | return GRN_INT32_VALUE(number) < 0; |
| 2673 | case GRN_DB_INT64: |
| 2674 | return GRN_INT64_VALUE(number) < 0; |
| 2675 | case GRN_DB_TIME: |
| 2676 | return GRN_TIME_VALUE(number) < 0; |
| 2677 | #ifdef GRN_HAVE_BFLOAT16 |
| 2678 | case GRN_DB_BFLOAT16: |
| 2679 | return GRN_BFLOAT16_VALUE(number) < 0; |
| 2680 | #endif |
| 2681 | case GRN_DB_FLOAT32: |
| 2682 | return GRN_FLOAT32_VALUE(number) < 0; |
| 2683 | case GRN_DB_FLOAT: |
| 2684 | return GRN_FLOAT_VALUE(number) < 0; |
| 2685 | default: |
| 2686 | return false; |
| 2687 | } |
| 2688 | } |
| 2689 | |
| 2690 | static inline bool |
| 2691 | number_safe_cast(grn_ctx *ctx, grn_obj *src, grn_obj *dest, grn_id type) |
| 2692 | { |
| 2693 | grn_obj_reinit(ctx, dest, type, 0); |
| 2694 | if (src->header.domain == type) { |
| 2695 | GRN_TEXT_SET(ctx, dest, GRN_TEXT_VALUE(src), GRN_TEXT_LEN(src)); |
| 2696 | return true; |
| 2697 | } |
| 2698 | |
| 2699 | switch (type) { |
| 2700 | case GRN_DB_UINT8: |
| 2701 | if (is_negative_value(src)) { |
| 2702 | GRN_UINT8_SET(ctx, dest, 0); |
| 2703 | return true; |
| 2704 | } |
| 2705 | break; |
| 2706 | case GRN_DB_UINT16: |
| 2707 | if (is_negative_value(src)) { |
| 2708 | GRN_UINT16_SET(ctx, dest, 0); |
| 2709 | return true; |
| 2710 | } |
| 2711 | break; |
| 2712 | case GRN_DB_UINT32: |
| 2713 | if (is_negative_value(src)) { |
| 2714 | GRN_UINT32_SET(ctx, dest, 0); |
| 2715 | return true; |
| 2716 | } |
| 2717 | break; |
| 2718 | case GRN_DB_UINT64: |
| 2719 | if (is_negative_value(src)) { |
| 2720 | GRN_UINT64_SET(ctx, dest, 0); |
| 2721 | return true; |
| 2722 | } |
| 2723 | break; |
| 2724 | } |
| 2725 | return grn_obj_cast(ctx, src, dest, false) == GRN_SUCCESS; |
| 2726 | } |
| 2727 | |
| 2728 | static inline int |
| 2729 | compare_number(grn_ctx *ctx, grn_obj *number1, grn_obj *number2, grn_id type) |
| 2730 | { |
| 2731 | #define COMPARE_AND_RETURN(type, value1, value2) \ |
| 2732 | { \ |
| 2733 | type computed_value1 = value1; \ |
| 2734 | type computed_value2 = value2; \ |
| 2735 | if (computed_value1 > computed_value2) { \ |
| 2736 | return 1; \ |
| 2737 | } else if (computed_value1 < computed_value2) { \ |
| 2738 | return -1; \ |
| 2739 | } else { \ |
| 2740 | return 0; \ |
| 2741 | } \ |
| 2742 | } |
| 2743 | |
| 2744 | switch (type) { |
| 2745 | case GRN_DB_INT8: |
| 2746 | COMPARE_AND_RETURN(int8_t, |
| 2747 | GRN_INT8_VALUE(number1), |
| 2748 | GRN_INT8_VALUE(number2)); |
| 2749 | case GRN_DB_UINT8: |
| 2750 | COMPARE_AND_RETURN(uint8_t, |
| 2751 | GRN_UINT8_VALUE(number1), |
| 2752 | GRN_UINT8_VALUE(number2)); |
| 2753 | case GRN_DB_INT16: |
| 2754 | COMPARE_AND_RETURN(int16_t, |
| 2755 | GRN_INT16_VALUE(number1), |
| 2756 | GRN_INT16_VALUE(number2)); |
| 2757 | case GRN_DB_UINT16: |
| 2758 | COMPARE_AND_RETURN(uint16_t, |
| 2759 | GRN_UINT16_VALUE(number1), |
| 2760 | GRN_UINT16_VALUE(number2)); |
| 2761 | case GRN_DB_INT32: |
| 2762 | COMPARE_AND_RETURN(int32_t, |
| 2763 | GRN_INT32_VALUE(number1), |
| 2764 | GRN_INT32_VALUE(number2)); |
| 2765 | case GRN_DB_UINT32: |
| 2766 | COMPARE_AND_RETURN(uint32_t, |
| 2767 | GRN_UINT32_VALUE(number1), |
| 2768 | GRN_UINT32_VALUE(number2)); |
| 2769 | case GRN_DB_INT64: |
| 2770 | COMPARE_AND_RETURN(int64_t, |
| 2771 | GRN_INT64_VALUE(number1), |
| 2772 | GRN_INT64_VALUE(number2)); |
| 2773 | case GRN_DB_UINT64: |
| 2774 | COMPARE_AND_RETURN(uint64_t, |
| 2775 | GRN_UINT64_VALUE(number1), |
| 2776 | GRN_UINT64_VALUE(number2)); |
| 2777 | #ifdef GRN_HAVE_BFLOAT16 |
| 2778 | case GRN_DB_BFLOAT16: |
| 2779 | COMPARE_AND_RETURN(grn_bfloat16, |
| 2780 | GRN_BFLOAT16_VALUE(number1), |
| 2781 | GRN_BFLOAT16_VALUE(number2)); |
| 2782 | #endif |
| 2783 | case GRN_DB_FLOAT32: |
| 2784 | COMPARE_AND_RETURN(float, |
| 2785 | GRN_FLOAT32_VALUE(number1), |
| 2786 | GRN_FLOAT32_VALUE(number2)); |
| 2787 | case GRN_DB_FLOAT: |
| 2788 | COMPARE_AND_RETURN(double, |
| 2789 | GRN_FLOAT_VALUE(number1), |
| 2790 | GRN_FLOAT_VALUE(number2)); |
| 2791 | case GRN_DB_TIME: |
| 2792 | COMPARE_AND_RETURN(int64_t, |
| 2793 | GRN_TIME_VALUE(number1), |
| 2794 | GRN_TIME_VALUE(number2)); |
| 2795 | default: |
| 2796 | return 0; |
| 2797 | } |
| 2798 | |
| 2799 | #undef COMPARE_AND_RETURN |
| 2800 | } |
| 2801 | |
| 2802 | static inline void |
| 2803 | get_number_in_grn_uvector(grn_ctx *ctx, |
| 2804 | grn_obj *uvector, |
| 2805 | unsigned int offset, |
| 2806 | grn_obj *buf) |
| 2807 | { |
| 2808 | #define GET_UVECTOR_ELEMENT_AS(type) \ |
| 2809 | do { \ |
| 2810 | GRN_##type##_SET(ctx, buf, GRN_##type##_VALUE_AT(uvector, offset)); \ |
| 2811 | } while (false) |
| 2812 | switch (uvector->header.domain) { |
| 2813 | case GRN_DB_BOOL: |
| 2814 | GET_UVECTOR_ELEMENT_AS(BOOL); |
| 2815 | break; |
| 2816 | case GRN_DB_INT8: |
| 2817 | GET_UVECTOR_ELEMENT_AS(INT8); |
| 2818 | break; |
| 2819 | case GRN_DB_UINT8: |
| 2820 | GET_UVECTOR_ELEMENT_AS(UINT8); |
| 2821 | break; |
| 2822 | case GRN_DB_INT16: |
| 2823 | GET_UVECTOR_ELEMENT_AS(INT16); |
| 2824 | break; |
| 2825 | case GRN_DB_UINT16: |
| 2826 | GET_UVECTOR_ELEMENT_AS(UINT16); |
| 2827 | break; |
| 2828 | case GRN_DB_INT32: |
| 2829 | GET_UVECTOR_ELEMENT_AS(INT32); |
| 2830 | break; |
| 2831 | case GRN_DB_UINT32: |
| 2832 | GET_UVECTOR_ELEMENT_AS(UINT32); |
| 2833 | break; |
| 2834 | case GRN_DB_INT64: |
| 2835 | GET_UVECTOR_ELEMENT_AS(INT64); |
| 2836 | break; |
| 2837 | case GRN_DB_UINT64: |
| 2838 | GET_UVECTOR_ELEMENT_AS(UINT64); |
| 2839 | break; |
| 2840 | #ifdef GRN_HAVE_BFLOAT16 |
| 2841 | case GRN_DB_BFLOAT16: |
| 2842 | GET_UVECTOR_ELEMENT_AS(BFLOAT16); |
| 2843 | break; |
| 2844 | #endif |
| 2845 | case GRN_DB_FLOAT32: |
| 2846 | GET_UVECTOR_ELEMENT_AS(FLOAT32); |
| 2847 | break; |
| 2848 | case GRN_DB_FLOAT: |
| 2849 | GET_UVECTOR_ELEMENT_AS(FLOAT); |
| 2850 | break; |
| 2851 | case GRN_DB_TIME: |
| 2852 | GET_UVECTOR_ELEMENT_AS(TIME); |
| 2853 | break; |
| 2854 | default: |
| 2855 | GET_UVECTOR_ELEMENT_AS(RECORD); |
| 2856 | break; |
| 2857 | } |
| 2858 | #undef GET_UVECTOR_ELEMENT_AS |
| 2859 | } |
| 2860 | |
| 2861 | static inline void |
| 2862 | apply_max(grn_ctx *ctx, |
| 2863 | grn_obj *number, |
| 2864 | grn_obj *max, |
| 2865 | grn_obj *casted_number, |
| 2866 | grn_obj *casted_max, |
| 2867 | grn_id cast_type) |
| 2868 | { |
| 2869 | grn_id domain = number->header.domain; |
| 2870 | if (!is_comparable_number_type(domain)) { |
| 2871 | return; |
| 2872 | } |
| 2873 | cast_type = larger_number_type(cast_type, domain); |
| 2874 | if (!number_safe_cast(ctx, number, casted_number, cast_type)) { |
| 2875 | return; |
| 2876 | } |
| 2877 | if (max->header.domain == GRN_DB_VOID) { |
| 2878 | grn_obj_reinit(ctx, max, cast_type, 0); |
| 2879 | GRN_TEXT_SET(ctx, |
| 2880 | max, |
| 2881 | GRN_TEXT_VALUE(casted_number), |
| 2882 | GRN_TEXT_LEN(casted_number)); |
| 2883 | return; |
| 2884 | } |
| 2885 | |
| 2886 | if (max->header.domain != cast_type) { |
| 2887 | if (!number_safe_cast(ctx, max, casted_max, cast_type)) { |
| 2888 | return; |
| 2889 | } |
| 2890 | grn_obj_reinit(ctx, max, cast_type, 0); |
| 2891 | GRN_TEXT_SET(ctx, |
| 2892 | max, |
| 2893 | GRN_TEXT_VALUE(casted_max), |
| 2894 | GRN_TEXT_LEN(casted_max)); |
| 2895 | } |
| 2896 | if (compare_number(ctx, casted_number, max, cast_type) > 0) { |
| 2897 | grn_obj_reinit(ctx, max, cast_type, 0); |
| 2898 | GRN_TEXT_SET(ctx, |
| 2899 | max, |
| 2900 | GRN_TEXT_VALUE(casted_number), |
| 2901 | GRN_TEXT_LEN(casted_number)); |
| 2902 | } |
| 2903 | } |
| 2904 | |
| 2905 | static grn_obj * |
| 2906 | func_max(grn_ctx *ctx, int nargs, grn_obj **args, grn_user_data *user_data) |
| 2907 | { |
| 2908 | grn_obj *max; |
| 2909 | grn_id cast_type = GRN_DB_INT8; |
| 2910 | grn_obj casted_max, casted_number; |
| 2911 | int i; |
| 2912 | |
| 2913 | max = GRN_PROC_ALLOC(GRN_DB_VOID, 0); |
| 2914 | if (!max) { |
| 2915 | return max; |
| 2916 | } |
| 2917 | |
| 2918 | GRN_VOID_INIT(&casted_max); |
| 2919 | GRN_VOID_INIT(&casted_number); |
| 2920 | |
| 2921 | for (i = 0; i < nargs; i++) { |
| 2922 | switch (args[i]->header.type) { |
| 2923 | case GRN_BULK: |
| 2924 | apply_max(ctx, args[i], max, &casted_number, &casted_max, cast_type); |
| 2925 | break; |
| 2926 | case GRN_UVECTOR: |
| 2927 | { |
| 2928 | unsigned int j; |
| 2929 | unsigned int n_elements; |
| 2930 | grn_obj number_in_uvector; |
| 2931 | grn_obj *domain; |
| 2932 | |
| 2933 | domain = grn_ctx_at(ctx, args[i]->header.domain); |
| 2934 | GRN_OBJ_INIT(&number_in_uvector, GRN_BULK, 0, args[i]->header.domain); |
| 2935 | n_elements = grn_uvector_size(ctx, args[i]); |
| 2936 | for (j = 0; j < n_elements; j++) { |
| 2937 | get_number_in_grn_uvector(ctx, args[i], j, &number_in_uvector); |
| 2938 | if (grn_obj_is_table(ctx, domain)) { |
| 2939 | grn_obj_reinit(ctx, &number_in_uvector, domain->header.domain, 0); |
| 2940 | grn_table_get_key2(ctx, |
| 2941 | domain, |
| 2942 | GRN_RECORD_VALUE(&number_in_uvector), |
| 2943 | &number_in_uvector); |
| 2944 | } |
| 2945 | apply_max(ctx, |
| 2946 | &number_in_uvector, |
| 2947 | max, |
| 2948 | &casted_number, |
| 2949 | &casted_max, |
| 2950 | cast_type); |
| 2951 | } |
| 2952 | GRN_OBJ_FIN(ctx, &number_in_uvector); |
| 2953 | } |
| 2954 | break; |
| 2955 | default: |
| 2956 | continue; |
| 2957 | } |
| 2958 | } |
| 2959 | GRN_OBJ_FIN(ctx, &casted_max); |
| 2960 | GRN_OBJ_FIN(ctx, &casted_number); |
| 2961 | |
| 2962 | return max; |
| 2963 | } |
| 2964 | |
| 2965 | static void |
| 2966 | apply_min(grn_ctx *ctx, |
| 2967 | grn_obj *number, |
| 2968 | grn_obj *min, |
| 2969 | grn_obj *casted_number, |
| 2970 | grn_obj *casted_min, |
| 2971 | grn_id cast_type) |
| 2972 | { |
| 2973 | grn_id domain = number->header.domain; |
| 2974 | if (!is_comparable_number_type(domain)) { |
| 2975 | return; |
| 2976 | } |
| 2977 | cast_type = smaller_number_type(cast_type, domain); |
| 2978 | if (!number_safe_cast(ctx, number, casted_number, cast_type)) { |
| 2979 | return; |
| 2980 | } |
| 2981 | if (min->header.domain == GRN_DB_VOID) { |
| 2982 | grn_obj_reinit(ctx, min, cast_type, 0); |
| 2983 | GRN_TEXT_SET(ctx, |
| 2984 | min, |
| 2985 | GRN_TEXT_VALUE(casted_number), |
| 2986 | GRN_TEXT_LEN(casted_number)); |
| 2987 | return; |
| 2988 | } |
| 2989 | |
| 2990 | if (min->header.domain != cast_type) { |
| 2991 | if (!number_safe_cast(ctx, min, casted_min, cast_type)) { |
| 2992 | return; |
| 2993 | } |
| 2994 | grn_obj_reinit(ctx, min, cast_type, 0); |
| 2995 | GRN_TEXT_SET(ctx, |
| 2996 | min, |
| 2997 | GRN_TEXT_VALUE(casted_min), |
| 2998 | GRN_TEXT_LEN(casted_min)); |
| 2999 | } |
| 3000 | if (compare_number(ctx, casted_number, min, cast_type) < 0) { |
| 3001 | grn_obj_reinit(ctx, min, cast_type, 0); |
| 3002 | GRN_TEXT_SET(ctx, |
| 3003 | min, |
| 3004 | GRN_TEXT_VALUE(casted_number), |
| 3005 | GRN_TEXT_LEN(casted_number)); |
| 3006 | } |
| 3007 | } |
| 3008 | |
| 3009 | static grn_obj * |
| 3010 | func_min(grn_ctx *ctx, int nargs, grn_obj **args, grn_user_data *user_data) |
| 3011 | { |
| 3012 | grn_obj *min; |
| 3013 | grn_id cast_type = GRN_DB_INT8; |
| 3014 | grn_obj casted_min, casted_number; |
| 3015 | int i; |
| 3016 | |
| 3017 | min = GRN_PROC_ALLOC(GRN_DB_VOID, 0); |
| 3018 | if (!min) { |
| 3019 | return min; |
| 3020 | } |
| 3021 | |
| 3022 | GRN_VOID_INIT(&casted_min); |
| 3023 | GRN_VOID_INIT(&casted_number); |
| 3024 | for (i = 0; i < nargs; i++) { |
| 3025 | switch (args[i]->header.type) { |
| 3026 | case GRN_BULK: |
| 3027 | apply_min(ctx, args[i], min, &casted_number, &casted_min, cast_type); |
| 3028 | break; |
| 3029 | case GRN_UVECTOR: |
| 3030 | { |
| 3031 | unsigned int j; |
| 3032 | unsigned int n_elements; |
| 3033 | grn_obj number_in_uvector; |
| 3034 | grn_obj *domain; |
| 3035 | |
| 3036 | domain = grn_ctx_at(ctx, args[i]->header.domain); |
| 3037 | GRN_OBJ_INIT(&number_in_uvector, GRN_BULK, 0, args[i]->header.domain); |
| 3038 | n_elements = grn_uvector_size(ctx, args[i]); |
| 3039 | for (j = 0; j < n_elements; j++) { |
| 3040 | get_number_in_grn_uvector(ctx, args[i], j, &number_in_uvector); |
| 3041 | if (grn_obj_is_table(ctx, domain)) { |
| 3042 | grn_obj_reinit(ctx, &number_in_uvector, domain->header.domain, 0); |
| 3043 | grn_table_get_key2(ctx, |
| 3044 | domain, |
| 3045 | GRN_RECORD_VALUE(&number_in_uvector), |
| 3046 | &number_in_uvector); |
| 3047 | } |
| 3048 | apply_min(ctx, |
| 3049 | &number_in_uvector, |
| 3050 | min, |
| 3051 | &casted_number, |
| 3052 | &casted_min, |
| 3053 | cast_type); |
| 3054 | } |
| 3055 | GRN_OBJ_FIN(ctx, &number_in_uvector); |
| 3056 | } |
| 3057 | break; |
| 3058 | default: |
| 3059 | continue; |
| 3060 | } |
| 3061 | } |
| 3062 | GRN_OBJ_FIN(ctx, &casted_min); |
| 3063 | GRN_OBJ_FIN(ctx, &casted_number); |
| 3064 | |
| 3065 | return min; |
| 3066 | } |
| 3067 | |
| 3068 | static grn_obj * |
| 3069 | func_geo_in_circle(grn_ctx *ctx, |
| 3070 | int nargs, |
| 3071 | grn_obj **args, |
| 3072 | grn_user_data *user_data) |
| 3073 | { |
| 3074 | grn_obj *obj; |
| 3075 | bool r = false; |
| 3076 | grn_geo_approximate_type type = GRN_GEO_APPROXIMATE_RECTANGLE; |
| 3077 | switch (nargs) { |
| 3078 | case 4: |
| 3079 | if (grn_geo_resolve_approximate_type(ctx, args[3], &type) != GRN_SUCCESS) { |
| 3080 | break; |
| 3081 | } |
| 3082 | /* fallthru */ |
| 3083 | case 3: |
| 3084 | r = grn_geo_in_circle(ctx, args[0], args[1], args[2], type); |
| 3085 | break; |
| 3086 | default: |
| 3087 | break; |
| 3088 | } |
| 3089 | if ((obj = GRN_PROC_ALLOC(GRN_DB_BOOL, 0))) { |
| 3090 | GRN_BOOL_SET(ctx, obj, r); |
| 3091 | } |
| 3092 | return obj; |
| 3093 | } |
| 3094 | |
| 3095 | static grn_obj * |
| 3096 | func_geo_in_rectangle(grn_ctx *ctx, |
| 3097 | int nargs, |
| 3098 | grn_obj **args, |
| 3099 | grn_user_data *user_data) |
| 3100 | { |
| 3101 | grn_obj *obj; |
| 3102 | bool r = false; |
| 3103 | if (nargs == 3) { |
| 3104 | r = grn_geo_in_rectangle(ctx, args[0], args[1], args[2]); |
| 3105 | } |
| 3106 | if ((obj = GRN_PROC_ALLOC(GRN_DB_BOOL, 0))) { |
| 3107 | GRN_BOOL_SET(ctx, obj, r); |
| 3108 | } |
| 3109 | return obj; |
| 3110 | } |
| 3111 | |
| 3112 | static grn_obj * |
| 3113 | func_geo_distance(grn_ctx *ctx, |
| 3114 | int nargs, |
| 3115 | grn_obj **args, |
| 3116 | grn_user_data *user_data) |
| 3117 | { |
| 3118 | grn_obj *obj; |
| 3119 | double d = 0.0; |
| 3120 | grn_geo_approximate_type type = GRN_GEO_APPROXIMATE_RECTANGLE; |
| 3121 | switch (nargs) { |
| 3122 | case 3: |
| 3123 | if (grn_geo_resolve_approximate_type(ctx, args[2], &type) != GRN_SUCCESS) { |
| 3124 | break; |
| 3125 | } |
| 3126 | /* fallthru */ |
| 3127 | case 2: |
| 3128 | d = grn_geo_distance(ctx, args[0], args[1], type); |
| 3129 | break; |
| 3130 | default: |
| 3131 | break; |
| 3132 | } |
| 3133 | if ((obj = GRN_PROC_ALLOC(GRN_DB_FLOAT, 0))) { |
| 3134 | GRN_FLOAT_SET(ctx, obj, d); |
| 3135 | } |
| 3136 | return obj; |
| 3137 | } |
| 3138 | |
| 3139 | /* deprecated. */ |
| 3140 | static grn_obj * |
| 3141 | func_geo_distance2(grn_ctx *ctx, |
| 3142 | int nargs, |
| 3143 | grn_obj **args, |
| 3144 | grn_user_data *user_data) |
| 3145 | { |
| 3146 | grn_obj *obj; |
| 3147 | double d = 0; |
| 3148 | if (nargs == 2) { |
| 3149 | d = grn_geo_distance_sphere(ctx, args[0], args[1]); |
| 3150 | } |
| 3151 | if ((obj = GRN_PROC_ALLOC(GRN_DB_FLOAT, 0))) { |
| 3152 | GRN_FLOAT_SET(ctx, obj, d); |
| 3153 | } |
| 3154 | return obj; |
| 3155 | } |
| 3156 | |
| 3157 | /* deprecated. */ |
| 3158 | static grn_obj * |
| 3159 | func_geo_distance3(grn_ctx *ctx, |
| 3160 | int nargs, |
| 3161 | grn_obj **args, |
| 3162 | grn_user_data *user_data) |
| 3163 | { |
| 3164 | grn_obj *obj; |
| 3165 | double d = 0; |
| 3166 | if (nargs == 2) { |
| 3167 | d = grn_geo_distance_ellipsoid(ctx, args[0], args[1]); |
| 3168 | } |
| 3169 | if ((obj = GRN_PROC_ALLOC(GRN_DB_FLOAT, 0))) { |
| 3170 | GRN_FLOAT_SET(ctx, obj, d); |
| 3171 | } |
| 3172 | return obj; |
| 3173 | } |
| 3174 | |
| 3175 | static grn_obj * |
| 3176 | func_all_records(grn_ctx *ctx, |
| 3177 | int nargs, |
| 3178 | grn_obj **args, |
| 3179 | grn_user_data *user_data) |
| 3180 | { |
| 3181 | grn_obj *true_value; |
| 3182 | if ((true_value = GRN_PROC_ALLOC(GRN_DB_BOOL, 0))) { |
| 3183 | GRN_BOOL_SET(ctx, true_value, true); |
| 3184 | } |
| 3185 | return true_value; |
| 3186 | } |
| 3187 | |
| 3188 | static grn_rc |
| 3189 | selector_all_records(grn_ctx *ctx, |
| 3190 | grn_obj *table, |
| 3191 | grn_obj *index, |
| 3192 | int nargs, |
| 3193 | grn_obj **args, |
| 3194 | grn_obj *res, |
| 3195 | grn_operator op) |
| 3196 | { |
| 3197 | const double score = 1.0; |
| 3198 | return grn_result_set_add_table(ctx, (grn_hash *)res, table, score, op); |
| 3199 | } |
| 3200 | |
| 3201 | bool |
| 3202 | grn_proc_selector_to_function_data_init( |
| 3203 | grn_ctx *ctx, |
| 3204 | grn_proc_selector_to_function_data *data, |
| 3205 | grn_user_data *user_data) |
| 3206 | { |
| 3207 | grn_obj *condition = NULL; |
| 3208 | grn_obj *variable; |
| 3209 | |
| 3210 | data->table = NULL; |
| 3211 | data->records = NULL; |
| 3212 | |
| 3213 | data->found = GRN_PROC_ALLOC(GRN_DB_BOOL, 0); |
| 3214 | if (!data->found) { |
| 3215 | return false; |
| 3216 | } |
| 3217 | GRN_BOOL_SET(ctx, data->found, false); |
| 3218 | |
| 3219 | grn_proc_get_info(ctx, user_data, NULL, NULL, &condition); |
| 3220 | if (!condition) { |
| 3221 | return false; |
| 3222 | } |
| 3223 | |
| 3224 | variable = grn_expr_get_var_by_offset(ctx, condition, 0); |
| 3225 | if (!variable) { |
| 3226 | return false; |
| 3227 | } |
| 3228 | |
| 3229 | data->table = grn_ctx_at(ctx, variable->header.domain); |
| 3230 | if (!data->table) { |
| 3231 | return false; |
| 3232 | } |
| 3233 | |
| 3234 | data->records = grn_table_create(ctx, |
| 3235 | NULL, |
| 3236 | 0, |
| 3237 | NULL, |
| 3238 | GRN_OBJ_TABLE_HASH_KEY | GRN_OBJ_WITH_SUBREC, |
| 3239 | data->table, |
| 3240 | NULL); |
| 3241 | if (!data->records) { |
| 3242 | return false; |
| 3243 | } |
| 3244 | |
| 3245 | { |
| 3246 | grn_rset_posinfo pi; |
| 3247 | unsigned int key_size; |
| 3248 | memset(&pi, 0, sizeof(grn_rset_posinfo)); |
| 3249 | pi.rid = GRN_RECORD_VALUE(variable); |
| 3250 | key_size = ((grn_hash *)(data->records))->key_size; |
| 3251 | if (grn_table_add(ctx, data->records, &pi, key_size, NULL) == GRN_ID_NIL) { |
| 3252 | return false; |
| 3253 | } |
| 3254 | } |
| 3255 | |
| 3256 | return true; |
| 3257 | } |
| 3258 | |
| 3259 | void |
| 3260 | grn_proc_selector_to_function_data_selected( |
| 3261 | grn_ctx *ctx, grn_proc_selector_to_function_data *data) |
| 3262 | { |
| 3263 | GRN_BOOL_SET(ctx, data->found, grn_table_size(ctx, data->records) > 0); |
| 3264 | } |
| 3265 | |
| 3266 | void |
| 3267 | grn_proc_selector_to_function_data_fin(grn_ctx *ctx, |
| 3268 | grn_proc_selector_to_function_data *data) |
| 3269 | { |
| 3270 | if (data->records) { |
| 3271 | grn_obj_unlink(ctx, data->records); |
| 3272 | } |
| 3273 | if (data->table) { |
| 3274 | grn_obj_unref(ctx, data->table); |
| 3275 | } |
| 3276 | } |
| 3277 | |
| 3278 | static bool |
| 3279 | sub_filter_pre_filter_accessor_is_available(grn_ctx *ctx, |
| 3280 | grn_accessor *accessor) |
| 3281 | { |
| 3282 | if (grn_obj_is_scalar_column(ctx, accessor->obj) || |
| 3283 | grn_obj_is_table(ctx, accessor->obj)) { |
| 3284 | if (accessor->next) { |
| 3285 | return sub_filter_pre_filter_accessor_is_available(ctx, accessor->next); |
| 3286 | } else { |
| 3287 | return true; |
| 3288 | } |
| 3289 | } else if (grn_obj_is_vector_column(ctx, accessor->obj)) { |
| 3290 | if (accessor->next) { |
| 3291 | return sub_filter_pre_filter_accessor_is_available(ctx, accessor->next); |
| 3292 | } else { |
| 3293 | return true; |
| 3294 | } |
| 3295 | } else if (grn_obj_is_index_column(ctx, accessor->obj)) { |
| 3296 | if (accessor->next) { |
| 3297 | return false; |
| 3298 | } else { |
| 3299 | return true; |
| 3300 | } |
| 3301 | } else { |
| 3302 | return false; |
| 3303 | } |
| 3304 | } |
| 3305 | |
| 3306 | static void |
| 3307 | sub_filter_pre_filter_accessor(grn_ctx *ctx, |
| 3308 | grn_accessor *accessor, |
| 3309 | grn_id id, |
| 3310 | grn_obj *base_res) |
| 3311 | { |
| 3312 | if (grn_obj_is_scalar_column(ctx, accessor->obj) || |
| 3313 | grn_obj_is_table(ctx, accessor->obj)) { |
| 3314 | grn_posting_internal posting = {0}; |
| 3315 | posting.weight_float = 1; |
| 3316 | if (grn_obj_is_scalar_column(ctx, accessor->obj)) { |
| 3317 | grn_obj value; |
| 3318 | GRN_RECORD_INIT(&value, 0, DB_OBJ(accessor->obj)->range); |
| 3319 | grn_obj_get_value(ctx, accessor->obj, id, &value); |
| 3320 | if (GRN_BULK_VSIZE(&value) > 0) { |
| 3321 | posting.rid = GRN_RECORD_VALUE(&value); |
| 3322 | } |
| 3323 | GRN_OBJ_FIN(ctx, &value); |
| 3324 | } else { |
| 3325 | grn_table_get_key(ctx, accessor->obj, id, &(posting.rid), sizeof(grn_id)); |
| 3326 | } |
| 3327 | if (posting.rid != GRN_ID_NIL) { |
| 3328 | if (accessor->next) { |
| 3329 | sub_filter_pre_filter_accessor(ctx, |
| 3330 | accessor->next, |
| 3331 | posting.rid, |
| 3332 | base_res); |
| 3333 | } else { |
| 3334 | grn_ii_posting_add_float(ctx, |
| 3335 | (grn_posting *)(&posting), |
| 3336 | (grn_hash *)base_res, |
| 3337 | GRN_OP_OR); |
| 3338 | } |
| 3339 | } |
| 3340 | } else if (grn_obj_is_vector_column(ctx, accessor->obj)) { |
| 3341 | grn_posting_internal posting = {0}; |
| 3342 | grn_obj values; |
| 3343 | unsigned int i, n; |
| 3344 | |
| 3345 | posting.weight_float = 1; |
| 3346 | GRN_RECORD_INIT(&values, GRN_OBJ_VECTOR, DB_OBJ(accessor->obj)->range); |
| 3347 | grn_obj_get_value(ctx, accessor->obj, id, &values); |
| 3348 | n = grn_vector_size(ctx, &values); |
| 3349 | for (i = 0; i < n; i++) { |
| 3350 | posting.rid = grn_uvector_get_element(ctx, &values, i, &(posting.weight)); |
| 3351 | if (accessor->next) { |
| 3352 | sub_filter_pre_filter_accessor(ctx, |
| 3353 | accessor->next, |
| 3354 | posting.rid, |
| 3355 | base_res); |
| 3356 | } else { |
| 3357 | grn_ii_posting_add_float(ctx, |
| 3358 | (grn_posting *)(&posting), |
| 3359 | (grn_hash *)base_res, |
| 3360 | GRN_OP_OR); |
| 3361 | } |
| 3362 | } |
| 3363 | GRN_OBJ_FIN(ctx, &values); |
| 3364 | } else if (grn_obj_is_index_column(ctx, accessor->obj)) { |
| 3365 | grn_ii_at(ctx, |
| 3366 | (grn_ii *)(accessor->obj), |
| 3367 | id, |
| 3368 | (grn_hash *)base_res, |
| 3369 | GRN_OP_OR); |
| 3370 | } |
| 3371 | } |
| 3372 | |
| 3373 | static bool |
| 3374 | sub_filter_need_pre_filter(grn_ctx *ctx, |
| 3375 | grn_obj *res, |
| 3376 | int32_t pre_filter_threshold) |
| 3377 | { |
| 3378 | if (pre_filter_threshold < 0) { |
| 3379 | return true; |
| 3380 | } |
| 3381 | |
| 3382 | if ((int32_t)(grn_table_size(ctx, res)) <= pre_filter_threshold) { |
| 3383 | return true; |
| 3384 | } |
| 3385 | |
| 3386 | return false; |
| 3387 | } |
| 3388 | |
| 3389 | static bool |
| 3390 | sub_filter_pre_filter(grn_ctx *ctx, |
| 3391 | grn_obj *res, |
| 3392 | grn_obj *scope, |
| 3393 | grn_obj *base_res, |
| 3394 | int32_t pre_filter_threshold) |
| 3395 | { |
| 3396 | const char *action = "[sub-filter][pre-filter]"; |
| 3397 | |
| 3398 | if (!sub_filter_need_pre_filter(ctx, res, pre_filter_threshold)) { |
| 3399 | return false; |
| 3400 | } |
| 3401 | |
| 3402 | if (grn_obj_is_scalar_column(ctx, scope)) { |
| 3403 | grn_posting_internal posting = {0}; |
| 3404 | grn_obj value; |
| 3405 | |
| 3406 | posting.weight_float = 1; |
| 3407 | GRN_RECORD_INIT(&value, 0, grn_obj_get_range(ctx, scope)); |
| 3408 | GRN_TABLE_EACH_BEGIN(ctx, res, cursor, id) |
| 3409 | { |
| 3410 | grn_id *matched_id; |
| 3411 | |
| 3412 | grn_table_cursor_get_key(ctx, cursor, (void **)&matched_id); |
| 3413 | GRN_BULK_REWIND(&value); |
| 3414 | grn_obj_get_value(ctx, scope, *matched_id, &value); |
| 3415 | if (GRN_BULK_VSIZE(&value) > 0) { |
| 3416 | posting.rid = GRN_RECORD_VALUE(&value); |
| 3417 | grn_ii_posting_add_float(ctx, |
| 3418 | (grn_posting *)(&posting), |
| 3419 | (grn_hash *)base_res, |
| 3420 | GRN_OP_OR); |
| 3421 | } |
| 3422 | } |
| 3423 | GRN_TABLE_EACH_END(ctx, cursor); |
| 3424 | GRN_OBJ_FIN(ctx, &value); |
| 3425 | |
| 3426 | grn_report_column(ctx, action, "[scalar]", scope); |
| 3427 | return true; |
| 3428 | } else if (grn_obj_is_vector_column(ctx, scope)) { |
| 3429 | grn_posting_internal posting = {0}; |
| 3430 | grn_obj values; |
| 3431 | |
| 3432 | posting.weight_float = 1; |
| 3433 | GRN_RECORD_INIT(&values, GRN_OBJ_VECTOR, grn_obj_get_range(ctx, scope)); |
| 3434 | GRN_TABLE_EACH_BEGIN(ctx, res, cursor, id) |
| 3435 | { |
| 3436 | grn_id *matched_id; |
| 3437 | unsigned int i, n; |
| 3438 | |
| 3439 | grn_table_cursor_get_key(ctx, cursor, (void **)&matched_id); |
| 3440 | GRN_BULK_REWIND(&values); |
| 3441 | grn_obj_get_value(ctx, scope, *matched_id, &values); |
| 3442 | n = grn_vector_size(ctx, &values); |
| 3443 | for (i = 0; i < n; i++) { |
| 3444 | posting.rid = |
| 3445 | grn_uvector_get_element(ctx, &values, i, &(posting.weight)); |
| 3446 | grn_ii_posting_add_float(ctx, |
| 3447 | (grn_posting *)(&posting), |
| 3448 | (grn_hash *)base_res, |
| 3449 | GRN_OP_OR); |
| 3450 | } |
| 3451 | } |
| 3452 | GRN_TABLE_EACH_END(ctx, cursor); |
| 3453 | GRN_OBJ_FIN(ctx, &values); |
| 3454 | |
| 3455 | grn_report_column(ctx, action, "[vector]", scope); |
| 3456 | return true; |
| 3457 | } else if (grn_obj_is_index_column(ctx, scope)) { |
| 3458 | GRN_TABLE_EACH_BEGIN(ctx, res, cursor, id) |
| 3459 | { |
| 3460 | grn_id *matched_id; |
| 3461 | grn_table_cursor_get_key(ctx, cursor, (void **)&matched_id); |
| 3462 | grn_ii_at(ctx, |
| 3463 | (grn_ii *)scope, |
| 3464 | *matched_id, |
| 3465 | (grn_hash *)base_res, |
| 3466 | GRN_OP_OR); |
| 3467 | } |
| 3468 | GRN_TABLE_EACH_END(ctx, cursor); |
| 3469 | |
| 3470 | grn_report_column(ctx, action, "[index]", scope); |
| 3471 | return true; |
| 3472 | } else if (grn_obj_is_accessor(ctx, scope)) { |
| 3473 | grn_accessor *accessor = (grn_accessor *)scope; |
| 3474 | if (!sub_filter_pre_filter_accessor_is_available(ctx, accessor)) { |
| 3475 | return false; |
| 3476 | } |
| 3477 | GRN_TABLE_EACH_BEGIN(ctx, res, cursor, id) |
| 3478 | { |
| 3479 | grn_id *matched_id; |
| 3480 | grn_table_cursor_get_key(ctx, cursor, (void **)&matched_id); |
| 3481 | sub_filter_pre_filter_accessor(ctx, accessor, *matched_id, base_res); |
| 3482 | } |
| 3483 | GRN_TABLE_EACH_END(ctx, cursor); |
| 3484 | grn_report_accessor(ctx, action, "", scope); |
| 3485 | return true; |
| 3486 | } else { |
| 3487 | return false; |
| 3488 | } |
| 3489 | } |
| 3490 | |
| 3491 | static grn_rc |
| 3492 | run_sub_filter(grn_ctx *ctx, |
| 3493 | grn_obj *table, |
| 3494 | int nargs, |
| 3495 | grn_obj **args, |
| 3496 | grn_obj *res, |
| 3497 | grn_operator op) |
| 3498 | { |
| 3499 | const char *tag = "[sub-filter]"; |
| 3500 | grn_rc rc = GRN_SUCCESS; |
| 3501 | grn_obj *scope; |
| 3502 | grn_obj *sub_filter_string; |
| 3503 | int32_t pre_filter_threshold = grn_sub_filter_pre_filter_threshold; |
| 3504 | grn_obj *scope_domain = NULL; |
| 3505 | grn_obj *sub_filter = NULL; |
| 3506 | grn_obj *dummy_variable = NULL; |
| 3507 | |
| 3508 | if (!(nargs >= 2 && nargs <= 3)) { |
| 3509 | ERR(GRN_INVALID_ARGUMENT, |
| 3510 | "%s wrong number of arguments (%d for 2..3)", |
| 3511 | tag, |
| 3512 | nargs); |
| 3513 | rc = ctx->rc; |
| 3514 | goto exit; |
| 3515 | } |
| 3516 | |
| 3517 | scope = args[0]; |
| 3518 | sub_filter_string = args[1]; |
| 3519 | if (nargs == 3) { |
| 3520 | grn_proc_options_parse(ctx, |
| 3521 | args[2], |
| 3522 | tag, |
| 3523 | "pre_filter_threshold", |
| 3524 | GRN_PROC_OPTION_VALUE_INT32, |
| 3525 | &pre_filter_threshold, |
| 3526 | NULL); |
| 3527 | } |
| 3528 | |
| 3529 | switch (scope->header.type) { |
| 3530 | case GRN_ACCESSOR: |
| 3531 | case GRN_COLUMN_FIX_SIZE: |
| 3532 | case GRN_COLUMN_VAR_SIZE: |
| 3533 | case GRN_COLUMN_INDEX: |
| 3534 | break; |
| 3535 | default: |
| 3536 | /* TODO: put inspected the 1st argument to message */ |
| 3537 | ERR(GRN_INVALID_ARGUMENT, |
| 3538 | "%s the 1st argument must be column or accessor", |
| 3539 | tag); |
| 3540 | rc = ctx->rc; |
| 3541 | goto exit; |
| 3542 | break; |
| 3543 | } |
| 3544 | |
| 3545 | scope_domain = grn_ctx_at(ctx, grn_obj_get_range(ctx, scope)); |
| 3546 | |
| 3547 | if (sub_filter_string->header.domain != GRN_DB_TEXT) { |
| 3548 | /* TODO: put inspected the 2nd argument to message */ |
| 3549 | ERR(GRN_INVALID_ARGUMENT, "%s the 2nd argument must be String", tag); |
| 3550 | rc = ctx->rc; |
| 3551 | goto exit; |
| 3552 | } |
| 3553 | if (GRN_TEXT_LEN(sub_filter_string) == 0) { |
| 3554 | ERR(GRN_INVALID_ARGUMENT, |
| 3555 | "%s the 2nd argument must not be empty String", |
| 3556 | tag); |
| 3557 | rc = ctx->rc; |
| 3558 | goto exit; |
| 3559 | } |
| 3560 | |
| 3561 | GRN_EXPR_CREATE_FOR_QUERY(ctx, scope_domain, sub_filter, dummy_variable); |
| 3562 | if (!sub_filter) { |
| 3563 | rc = ctx->rc; |
| 3564 | goto exit; |
| 3565 | } |
| 3566 | |
| 3567 | grn_expr_parse(ctx, |
| 3568 | sub_filter, |
| 3569 | GRN_TEXT_VALUE(sub_filter_string), |
| 3570 | GRN_TEXT_LEN(sub_filter_string), |
| 3571 | NULL, |
| 3572 | GRN_OP_MATCH, |
| 3573 | GRN_OP_AND, |
| 3574 | GRN_EXPR_SYNTAX_SCRIPT); |
| 3575 | if (ctx->rc != GRN_SUCCESS) { |
| 3576 | rc = ctx->rc; |
| 3577 | goto exit; |
| 3578 | } |
| 3579 | |
| 3580 | { |
| 3581 | grn_obj *base_res = NULL; |
| 3582 | grn_operator select_op = GRN_OP_OR; |
| 3583 | |
| 3584 | base_res = grn_table_create(ctx, |
| 3585 | NULL, |
| 3586 | 0, |
| 3587 | NULL, |
| 3588 | GRN_OBJ_TABLE_HASH_KEY | GRN_OBJ_WITH_SUBREC, |
| 3589 | scope_domain, |
| 3590 | NULL); |
| 3591 | if (op == GRN_OP_AND && sub_filter_pre_filter(ctx, |
| 3592 | res, |
| 3593 | scope, |
| 3594 | base_res, |
| 3595 | pre_filter_threshold)) { |
| 3596 | if (base_res) { |
| 3597 | void *key = NULL, *value = NULL; |
| 3598 | uint32_t key_size = 0; |
| 3599 | |
| 3600 | GRN_TABLE_EACH(ctx, base_res, 0, 0, id, &key, &key_size, &value, { |
| 3601 | grn_rset_recinfo *ri = value; |
| 3602 | ri->score = 0; |
| 3603 | }); |
| 3604 | } |
| 3605 | select_op = GRN_OP_AND; |
| 3606 | } |
| 3607 | grn_table_select(ctx, scope_domain, sub_filter, base_res, select_op); |
| 3608 | if (scope->header.type == GRN_ACCESSOR) { |
| 3609 | rc = grn_accessor_resolve(ctx, scope, -1, base_res, res, op); |
| 3610 | } else { |
| 3611 | grn_accessor accessor; |
| 3612 | accessor.header.type = GRN_ACCESSOR; |
| 3613 | accessor.obj = scope; |
| 3614 | accessor.action = GRN_ACCESSOR_GET_COLUMN_VALUE; |
| 3615 | accessor.next = NULL; |
| 3616 | rc = |
| 3617 | grn_accessor_resolve(ctx, (grn_obj *)&accessor, -1, base_res, res, op); |
| 3618 | } |
| 3619 | grn_obj_unlink(ctx, base_res); |
| 3620 | } |
| 3621 | |
| 3622 | exit: |
| 3623 | if (scope_domain) { |
| 3624 | grn_obj_unlink(ctx, scope_domain); |
| 3625 | } |
| 3626 | if (sub_filter) { |
| 3627 | grn_obj_unlink(ctx, sub_filter); |
| 3628 | } |
| 3629 | |
| 3630 | return rc; |
| 3631 | } |
| 3632 | |
| 3633 | static grn_rc |
| 3634 | selector_sub_filter(grn_ctx *ctx, |
| 3635 | grn_obj *table, |
| 3636 | grn_obj *index, |
| 3637 | int nargs, |
| 3638 | grn_obj **args, |
| 3639 | grn_obj *res, |
| 3640 | grn_operator op) |
| 3641 | { |
| 3642 | return run_sub_filter(ctx, table, nargs - 1, args + 1, res, op); |
| 3643 | } |
| 3644 | |
| 3645 | static grn_obj * |
| 3646 | func_html_untag(grn_ctx *ctx, |
| 3647 | int nargs, |
| 3648 | grn_obj **args, |
| 3649 | grn_user_data *user_data) |
| 3650 | { |
| 3651 | grn_obj *html_arg; |
| 3652 | int html_arg_domain; |
| 3653 | grn_obj html; |
| 3654 | grn_obj *text; |
| 3655 | const char *html_raw; |
| 3656 | int i, length; |
| 3657 | bool in_tag = false; |
| 3658 | |
| 3659 | if (nargs != 1) { |
| 3660 | ERR(GRN_INVALID_ARGUMENT, "HTML is missing"); |
| 3661 | return NULL; |
| 3662 | } |
| 3663 | |
| 3664 | html_arg = args[0]; |
| 3665 | html_arg_domain = html_arg->header.domain; |
| 3666 | switch (html_arg_domain) { |
| 3667 | case GRN_DB_SHORT_TEXT: |
| 3668 | case GRN_DB_TEXT: |
| 3669 | case GRN_DB_LONG_TEXT: |
| 3670 | GRN_VALUE_VAR_SIZE_INIT(&html, GRN_OBJ_DO_SHALLOW_COPY, html_arg_domain); |
| 3671 | GRN_TEXT_SET(ctx, &html, GRN_TEXT_VALUE(html_arg), GRN_TEXT_LEN(html_arg)); |
| 3672 | break; |
| 3673 | default: |
| 3674 | GRN_TEXT_INIT(&html, 0); |
| 3675 | if (grn_obj_cast(ctx, html_arg, &html, false)) { |
| 3676 | grn_obj inspected; |
| 3677 | GRN_TEXT_INIT(&inspected, 0); |
| 3678 | grn_inspect(ctx, &inspected, html_arg); |
| 3679 | ERR(GRN_INVALID_ARGUMENT, |
| 3680 | "failed to cast to text: <%.*s>", |
| 3681 | (int)GRN_TEXT_LEN(&inspected), |
| 3682 | GRN_TEXT_VALUE(&inspected)); |
| 3683 | GRN_OBJ_FIN(ctx, &inspected); |
| 3684 | GRN_OBJ_FIN(ctx, &html); |
| 3685 | return NULL; |
| 3686 | } |
| 3687 | break; |
| 3688 | } |
| 3689 | |
| 3690 | text = GRN_PROC_ALLOC(html.header.domain, 0); |
| 3691 | if (!text) { |
| 3692 | GRN_OBJ_FIN(ctx, &html); |
| 3693 | return NULL; |
| 3694 | } |
| 3695 | |
| 3696 | html_raw = GRN_TEXT_VALUE(&html); |
| 3697 | length = GRN_TEXT_LEN(&html); |
| 3698 | for (i = 0; i < length; i++) { |
| 3699 | switch (html_raw[i]) { |
| 3700 | case '<': |
| 3701 | in_tag = true; |
| 3702 | break; |
| 3703 | case '>': |
| 3704 | if (in_tag) { |
| 3705 | in_tag = false; |
| 3706 | } else { |
| 3707 | GRN_TEXT_PUTC(ctx, text, html_raw[i]); |
| 3708 | } |
| 3709 | break; |
| 3710 | default: |
| 3711 | if (!in_tag) { |
| 3712 | GRN_TEXT_PUTC(ctx, text, html_raw[i]); |
| 3713 | } |
| 3714 | break; |
| 3715 | } |
| 3716 | } |
| 3717 | |
| 3718 | GRN_OBJ_FIN(ctx, &html); |
| 3719 | |
| 3720 | return text; |
| 3721 | } |
| 3722 | |
| 3723 | static bool |
| 3724 | grn_text_equal_cstr(grn_ctx *ctx, grn_obj *text, const char *cstr) |
| 3725 | { |
| 3726 | size_t cstr_len = strlen(cstr); |
| 3727 | return (GRN_TEXT_LEN(text) == cstr_len && |
| 3728 | strncmp(GRN_TEXT_VALUE(text), cstr, cstr_len) == 0); |
| 3729 | } |
| 3730 | |
| 3731 | typedef enum { |
| 3732 | BETWEEN_BORDER_INVALID, |
| 3733 | BETWEEN_BORDER_INCLUDE, |
| 3734 | BETWEEN_BORDER_EXCLUDE |
| 3735 | } between_border_type; |
| 3736 | |
| 3737 | typedef struct { |
| 3738 | grn_obj *value; |
| 3739 | grn_obj *min; |
| 3740 | grn_obj casted_min; |
| 3741 | between_border_type min_border_type; |
| 3742 | grn_obj *max; |
| 3743 | grn_obj casted_max; |
| 3744 | between_border_type max_border_type; |
| 3745 | int cursor_flags; |
| 3746 | double too_many_index_match_ratio; |
| 3747 | const char *tag; |
| 3748 | } between_data; |
| 3749 | |
| 3750 | static void |
| 3751 | between_data_init(grn_ctx *ctx, between_data *data) |
| 3752 | { |
| 3753 | GRN_VOID_INIT(&(data->casted_min)); |
| 3754 | GRN_VOID_INIT(&(data->casted_max)); |
| 3755 | data->cursor_flags = 0; |
| 3756 | data->too_many_index_match_ratio = grn_between_too_many_index_match_ratio; |
| 3757 | data->tag = "[between]"; |
| 3758 | } |
| 3759 | |
| 3760 | static void |
| 3761 | between_data_fin(grn_ctx *ctx, between_data *data) |
| 3762 | { |
| 3763 | GRN_OBJ_FIN(ctx, &(data->casted_min)); |
| 3764 | GRN_OBJ_FIN(ctx, &(data->casted_max)); |
| 3765 | } |
| 3766 | |
| 3767 | static between_border_type |
| 3768 | between_parse_border(grn_ctx *ctx, |
| 3769 | grn_obj *border, |
| 3770 | const char *argument_description) |
| 3771 | { |
| 3772 | grn_obj inspected; |
| 3773 | |
| 3774 | /* TODO: support other text types */ |
| 3775 | if (border->header.domain == GRN_DB_TEXT) { |
| 3776 | if (grn_text_equal_cstr(ctx, border, "include")) { |
| 3777 | return BETWEEN_BORDER_INCLUDE; |
| 3778 | } else if (grn_text_equal_cstr(ctx, border, "exclude")) { |
| 3779 | return BETWEEN_BORDER_EXCLUDE; |
| 3780 | } |
| 3781 | } |
| 3782 | |
| 3783 | GRN_TEXT_INIT(&inspected, 0); |
| 3784 | grn_inspect(ctx, &inspected, border); |
| 3785 | ERR(GRN_INVALID_ARGUMENT, |
| 3786 | "between(): %s must be \"include\" or \"exclude\": <%.*s>", |
| 3787 | argument_description, |
| 3788 | (int)GRN_TEXT_LEN(&inspected), |
| 3789 | GRN_TEXT_VALUE(&inspected)); |
| 3790 | grn_obj_unlink(ctx, &inspected); |
| 3791 | |
| 3792 | return BETWEEN_BORDER_INVALID; |
| 3793 | } |
| 3794 | |
| 3795 | static grn_rc |
| 3796 | between_cast(grn_ctx *ctx, |
| 3797 | grn_obj *source, |
| 3798 | grn_obj *destination, |
| 3799 | grn_id domain, |
| 3800 | const char *target_argument_name) |
| 3801 | { |
| 3802 | grn_rc rc; |
| 3803 | |
| 3804 | GRN_OBJ_INIT(destination, GRN_BULK, 0, domain); |
| 3805 | rc = grn_obj_cast(ctx, source, destination, false); |
| 3806 | if (rc != GRN_SUCCESS) { |
| 3807 | grn_obj inspected_source; |
| 3808 | grn_obj *domain_object = NULL; |
| 3809 | char domain_name[GRN_TABLE_MAX_KEY_SIZE]; |
| 3810 | int domain_name_length; |
| 3811 | |
| 3812 | GRN_TEXT_INIT(&inspected_source, 0); |
| 3813 | grn_inspect(ctx, &inspected_source, source); |
| 3814 | |
| 3815 | if (domain == GRN_ID_NIL) { |
| 3816 | grn_strcpy(domain_name, sizeof(domain_name), "Void"); |
| 3817 | domain_name_length = strlen(domain_name); |
| 3818 | } else if (grn_type_id_is_builtin(ctx, domain)) { |
| 3819 | grn_strcpy(domain_name, |
| 3820 | sizeof(domain_name), |
| 3821 | grn_type_id_to_string_builtin(ctx, domain)); |
| 3822 | domain_name_length = strlen(domain_name); |
| 3823 | } else { |
| 3824 | domain_object = grn_ctx_at(ctx, domain); |
| 3825 | domain_name_length = |
| 3826 | grn_obj_name(ctx, domain_object, domain_name, GRN_TABLE_MAX_KEY_SIZE); |
| 3827 | } |
| 3828 | |
| 3829 | ERR(rc, |
| 3830 | "between(): failed to cast %s: <%.*s> -> <%.*s>", |
| 3831 | target_argument_name, |
| 3832 | (int)GRN_TEXT_LEN(&inspected_source), |
| 3833 | GRN_TEXT_VALUE(&inspected_source), |
| 3834 | domain_name_length, |
| 3835 | domain_name); |
| 3836 | |
| 3837 | grn_obj_unlink(ctx, &inspected_source); |
| 3838 | if (domain_object) { |
| 3839 | grn_obj_unlink(ctx, domain_object); |
| 3840 | } |
| 3841 | } |
| 3842 | |
| 3843 | return rc; |
| 3844 | } |
| 3845 | |
| 3846 | static void |
| 3847 | between_parse_options(grn_ctx *ctx, grn_obj *options, between_data *data) |
| 3848 | { |
| 3849 | grn_proc_options_parse(ctx, |
| 3850 | options, |
| 3851 | data->tag, |
| 3852 | "too_many_index_match_ratio", |
| 3853 | GRN_PROC_OPTION_VALUE_DOUBLE, |
| 3854 | &(data->too_many_index_match_ratio), |
| 3855 | NULL); |
| 3856 | } |
| 3857 | |
| 3858 | static grn_rc |
| 3859 | between_parse_args(grn_ctx *ctx, int nargs, grn_obj **args, between_data *data) |
| 3860 | { |
| 3861 | grn_rc rc = GRN_SUCCESS; |
| 3862 | |
| 3863 | data->value = args[0]; |
| 3864 | data->min = args[1]; |
| 3865 | switch (nargs) { |
| 3866 | case 3: |
| 3867 | case 4: |
| 3868 | data->min_border_type = BETWEEN_BORDER_INCLUDE; |
| 3869 | data->max = args[2]; |
| 3870 | data->max_border_type = BETWEEN_BORDER_INCLUDE; |
| 3871 | if (nargs == 4) { |
| 3872 | between_parse_options(ctx, args[3], data); |
| 3873 | if (ctx->rc != GRN_SUCCESS) { |
| 3874 | rc = ctx->rc; |
| 3875 | goto exit; |
| 3876 | } |
| 3877 | } |
| 3878 | break; |
| 3879 | case 5: |
| 3880 | case 6: |
| 3881 | data->min_border_type = |
| 3882 | between_parse_border(ctx, args[2], "the 3rd argument (min_border)"); |
| 3883 | if (data->min_border_type == BETWEEN_BORDER_INVALID) { |
| 3884 | rc = ctx->rc; |
| 3885 | goto exit; |
| 3886 | } |
| 3887 | data->max = args[3]; |
| 3888 | data->max_border_type = |
| 3889 | between_parse_border(ctx, args[4], "the 5th argument (max_border)"); |
| 3890 | if (data->max_border_type == BETWEEN_BORDER_INVALID) { |
| 3891 | rc = ctx->rc; |
| 3892 | goto exit; |
| 3893 | } |
| 3894 | if (nargs == 6) { |
| 3895 | between_parse_options(ctx, args[5], data); |
| 3896 | if (ctx->rc != GRN_SUCCESS) { |
| 3897 | rc = ctx->rc; |
| 3898 | goto exit; |
| 3899 | } |
| 3900 | } |
| 3901 | break; |
| 3902 | default: |
| 3903 | ERR(GRN_INVALID_ARGUMENT, |
| 3904 | "between(): wrong number of arguments (%d for 3 or 5)", |
| 3905 | nargs); |
| 3906 | rc = ctx->rc; |
| 3907 | goto exit; |
| 3908 | } |
| 3909 | |
| 3910 | { |
| 3911 | grn_id value_type; |
| 3912 | switch (data->value->header.type) { |
| 3913 | case GRN_BULK: |
| 3914 | case GRN_UVECTOR: |
| 3915 | value_type = data->value->header.domain; |
| 3916 | break; |
| 3917 | case GRN_VECTOR: |
| 3918 | if (grn_vector_size(ctx, data->value) == 0) { |
| 3919 | value_type = GRN_ID_NIL; |
| 3920 | } else { |
| 3921 | const char *element; |
| 3922 | grn_vector_get_element(ctx, |
| 3923 | data->value, |
| 3924 | 0, |
| 3925 | &element, |
| 3926 | NULL, |
| 3927 | &value_type); |
| 3928 | } |
| 3929 | break; |
| 3930 | case GRN_COLUMN_INDEX: |
| 3931 | { |
| 3932 | grn_obj *domain_object; |
| 3933 | domain_object = grn_ctx_at(ctx, data->value->header.domain); |
| 3934 | value_type = domain_object->header.domain; |
| 3935 | } |
| 3936 | break; |
| 3937 | default: |
| 3938 | value_type = grn_obj_get_range(ctx, data->value); |
| 3939 | break; |
| 3940 | } |
| 3941 | if (value_type != data->min->header.domain) { |
| 3942 | rc = between_cast(ctx, data->min, &data->casted_min, value_type, "min"); |
| 3943 | if (rc != GRN_SUCCESS) { |
| 3944 | goto exit; |
| 3945 | } |
| 3946 | data->min = &(data->casted_min); |
| 3947 | } |
| 3948 | |
| 3949 | if (value_type != data->max->header.domain) { |
| 3950 | rc = between_cast(ctx, data->max, &data->casted_max, value_type, "max"); |
| 3951 | if (rc != GRN_SUCCESS) { |
| 3952 | goto exit; |
| 3953 | } |
| 3954 | data->max = &(data->casted_max); |
| 3955 | } |
| 3956 | } |
| 3957 | |
| 3958 | exit: |
| 3959 | return rc; |
| 3960 | } |
| 3961 | |
| 3962 | static bool |
| 3963 | between_create_expr_value(grn_ctx *ctx, |
| 3964 | grn_obj *table, |
| 3965 | between_data *data, |
| 3966 | grn_obj *value, |
| 3967 | grn_obj *expr) |
| 3968 | { |
| 3969 | if (value->header.type == GRN_BULK) { |
| 3970 | grn_expr_append_const(ctx, expr, value, GRN_OP_PUSH, 1); |
| 3971 | } else { |
| 3972 | grn_expr_append_obj(ctx, expr, value, GRN_OP_GET_VALUE, 1); |
| 3973 | } |
| 3974 | if (ctx->rc != GRN_SUCCESS) { |
| 3975 | return false; |
| 3976 | } |
| 3977 | grn_expr_append_const(ctx, expr, data->min, GRN_OP_PUSH, 1); |
| 3978 | if (ctx->rc != GRN_SUCCESS) { |
| 3979 | return false; |
| 3980 | } |
| 3981 | if (data->min_border_type == BETWEEN_BORDER_INCLUDE) { |
| 3982 | grn_expr_append_op(ctx, expr, GRN_OP_GREATER_EQUAL, 2); |
| 3983 | } else { |
| 3984 | grn_expr_append_op(ctx, expr, GRN_OP_GREATER, 2); |
| 3985 | } |
| 3986 | if (ctx->rc != GRN_SUCCESS) { |
| 3987 | return false; |
| 3988 | } |
| 3989 | |
| 3990 | if (value->header.type == GRN_BULK) { |
| 3991 | grn_expr_append_const(ctx, expr, value, GRN_OP_PUSH, 1); |
| 3992 | } else { |
| 3993 | grn_expr_append_obj(ctx, expr, value, GRN_OP_GET_VALUE, 1); |
| 3994 | } |
| 3995 | if (ctx->rc != GRN_SUCCESS) { |
| 3996 | return false; |
| 3997 | } |
| 3998 | grn_expr_append_const(ctx, expr, data->max, GRN_OP_PUSH, 1); |
| 3999 | if (ctx->rc != GRN_SUCCESS) { |
| 4000 | return false; |
| 4001 | } |
| 4002 | if (data->max_border_type == BETWEEN_BORDER_INCLUDE) { |
| 4003 | grn_expr_append_op(ctx, expr, GRN_OP_LESS_EQUAL, 2); |
| 4004 | } else { |
| 4005 | grn_expr_append_op(ctx, expr, GRN_OP_LESS, 2); |
| 4006 | } |
| 4007 | if (ctx->rc != GRN_SUCCESS) { |
| 4008 | return false; |
| 4009 | } |
| 4010 | |
| 4011 | grn_expr_append_op(ctx, expr, GRN_OP_AND, 2); |
| 4012 | if (ctx->rc != GRN_SUCCESS) { |
| 4013 | return false; |
| 4014 | } |
| 4015 | |
| 4016 | return true; |
| 4017 | } |
| 4018 | |
| 4019 | static bool |
| 4020 | between_create_expr(grn_ctx *ctx, |
| 4021 | grn_obj *table, |
| 4022 | between_data *data, |
| 4023 | grn_obj **expr, |
| 4024 | grn_obj **variable) |
| 4025 | { |
| 4026 | GRN_EXPR_CREATE_FOR_QUERY(ctx, table, *expr, *variable); |
| 4027 | if (!*expr) { |
| 4028 | return false; |
| 4029 | } |
| 4030 | if (!*variable) { |
| 4031 | return false; |
| 4032 | } |
| 4033 | |
| 4034 | if (data->value->header.type == GRN_UVECTOR) { |
| 4035 | uint32_t n = grn_uvector_size(ctx, data->value); |
| 4036 | uint32_t element_size = grn_uvector_element_size(ctx, data->value); |
| 4037 | grn_obj element; |
| 4038 | GRN_OBJ_INIT(&element, |
| 4039 | GRN_BULK, |
| 4040 | GRN_OBJ_DO_SHALLOW_COPY, |
| 4041 | data->value->header.domain); |
| 4042 | for (uint32_t i = 0; i < n; i++) { |
| 4043 | GRN_TEXT_SET(ctx, |
| 4044 | &element, |
| 4045 | GRN_BULK_HEAD(data->value) + (element_size * i), |
| 4046 | element_size); |
| 4047 | if (!between_create_expr_value(ctx, table, data, &element, *expr)) { |
| 4048 | break; |
| 4049 | } |
| 4050 | if (i > 0) { |
| 4051 | if (grn_expr_append_op(ctx, *expr, GRN_OP_OR, 2) != GRN_SUCCESS) { |
| 4052 | break; |
| 4053 | } |
| 4054 | } |
| 4055 | } |
| 4056 | GRN_OBJ_FIN(ctx, &element); |
| 4057 | return ctx->rc == GRN_SUCCESS; |
| 4058 | } else if (data->value->header.type == GRN_VECTOR) { |
| 4059 | grn_id target_domain = data->min->header.domain; |
| 4060 | uint32_t n = grn_vector_size(ctx, data->value); |
| 4061 | uint32_t n_used_elements = 0; |
| 4062 | grn_obj element; |
| 4063 | GRN_OBJ_INIT(&element, GRN_BULK, GRN_OBJ_DO_SHALLOW_COPY, target_domain); |
| 4064 | for (uint32_t i = 0; i < n; i++) { |
| 4065 | const char *raw_element; |
| 4066 | grn_id domain; |
| 4067 | uint32_t size = grn_vector_get_element(ctx, |
| 4068 | data->value, |
| 4069 | i, |
| 4070 | &raw_element, |
| 4071 | NULL, |
| 4072 | &domain); |
| 4073 | if (domain != target_domain) { |
| 4074 | continue; |
| 4075 | } |
| 4076 | GRN_TEXT_SET(ctx, &element, raw_element, size); |
| 4077 | if (!between_create_expr_value(ctx, table, data, &element, *expr)) { |
| 4078 | break; |
| 4079 | } |
| 4080 | if (n_used_elements > 0) { |
| 4081 | if (grn_expr_append_op(ctx, *expr, GRN_OP_OR, 2) != GRN_SUCCESS) { |
| 4082 | break; |
| 4083 | } |
| 4084 | } |
| 4085 | n_used_elements++; |
| 4086 | } |
| 4087 | GRN_OBJ_FIN(ctx, &element); |
| 4088 | return ctx->rc == GRN_SUCCESS; |
| 4089 | } else { |
| 4090 | return between_create_expr_value(ctx, table, data, data->value, *expr); |
| 4091 | } |
| 4092 | } |
| 4093 | |
| 4094 | static grn_obj * |
| 4095 | func_between(grn_ctx *ctx, int nargs, grn_obj **args, grn_user_data *user_data) |
| 4096 | { |
| 4097 | grn_rc rc = GRN_SUCCESS; |
| 4098 | grn_obj *found; |
| 4099 | between_data data; |
| 4100 | grn_obj *condition = NULL; |
| 4101 | grn_obj *variable; |
| 4102 | grn_obj *table = NULL; |
| 4103 | grn_obj *between_expr; |
| 4104 | grn_obj *between_variable; |
| 4105 | grn_obj *result; |
| 4106 | |
| 4107 | found = GRN_PROC_ALLOC(GRN_DB_BOOL, 0); |
| 4108 | if (!found) { |
| 4109 | return NULL; |
| 4110 | } |
| 4111 | GRN_BOOL_SET(ctx, found, false); |
| 4112 | |
| 4113 | grn_proc_get_info(ctx, user_data, NULL, NULL, &condition); |
| 4114 | if (!condition) { |
| 4115 | return found; |
| 4116 | } |
| 4117 | |
| 4118 | variable = grn_expr_get_var_by_offset(ctx, condition, 0); |
| 4119 | if (!variable) { |
| 4120 | return found; |
| 4121 | } |
| 4122 | |
| 4123 | between_data_init(ctx, &data); |
| 4124 | rc = between_parse_args(ctx, nargs, args, &data); |
| 4125 | if (rc != GRN_SUCCESS) { |
| 4126 | goto exit; |
| 4127 | } |
| 4128 | |
| 4129 | table = grn_ctx_at(ctx, variable->header.domain); |
| 4130 | if (!table) { |
| 4131 | goto exit; |
| 4132 | } |
| 4133 | if (!between_create_expr(ctx, |
| 4134 | table, |
| 4135 | &data, |
| 4136 | &between_expr, |
| 4137 | &between_variable)) { |
| 4138 | goto exit; |
| 4139 | } |
| 4140 | |
| 4141 | GRN_RECORD_SET(ctx, between_variable, GRN_RECORD_VALUE(variable)); |
| 4142 | result = grn_expr_exec(ctx, between_expr, 0); |
| 4143 | if (grn_obj_is_true(ctx, result)) { |
| 4144 | GRN_BOOL_SET(ctx, found, true); |
| 4145 | } |
| 4146 | |
| 4147 | grn_obj_unlink(ctx, between_expr); |
| 4148 | |
| 4149 | exit: |
| 4150 | between_data_fin(ctx, &data); |
| 4151 | if (table) { |
| 4152 | grn_obj_unref(ctx, table); |
| 4153 | } |
| 4154 | |
| 4155 | return found; |
| 4156 | } |
| 4157 | |
| 4158 | static bool |
| 4159 | selector_between_sequential_search_should_use(grn_ctx *ctx, |
| 4160 | grn_obj *table, |
| 4161 | grn_obj *index, |
| 4162 | grn_obj *index_table, |
| 4163 | between_data *data, |
| 4164 | grn_obj *res, |
| 4165 | grn_operator op) |
| 4166 | { |
| 4167 | if (data->too_many_index_match_ratio < 0.0) { |
| 4168 | return false; |
| 4169 | } |
| 4170 | |
| 4171 | if (op != GRN_OP_AND) { |
| 4172 | return false; |
| 4173 | } |
| 4174 | |
| 4175 | if (!index) { |
| 4176 | return false; |
| 4177 | } |
| 4178 | |
| 4179 | if (index->header.flags & GRN_OBJ_WITH_WEIGHT) { |
| 4180 | return false; |
| 4181 | } |
| 4182 | |
| 4183 | if (data->value->header.type == GRN_COLUMN_INDEX) { |
| 4184 | return false; |
| 4185 | } |
| 4186 | |
| 4187 | grn_table_cursor *cursor = grn_table_cursor_open(ctx, |
| 4188 | index_table, |
| 4189 | GRN_BULK_HEAD(data->min), |
| 4190 | GRN_BULK_VSIZE(data->min), |
| 4191 | GRN_BULK_HEAD(data->max), |
| 4192 | GRN_BULK_VSIZE(data->max), |
| 4193 | 0, |
| 4194 | -1, |
| 4195 | data->cursor_flags); |
| 4196 | if (!cursor) { |
| 4197 | return false; |
| 4198 | } |
| 4199 | uint32_t estimated_size = |
| 4200 | grn_ii_estimate_size_for_lexicon_cursor(ctx, (grn_ii *)index, cursor); |
| 4201 | grn_table_cursor_close(ctx, cursor); |
| 4202 | |
| 4203 | if (estimated_size == 0) { |
| 4204 | return false; |
| 4205 | } |
| 4206 | |
| 4207 | uint32_t n_existing_records = grn_table_size(ctx, res); |
| 4208 | double too_many_index_match_threshold = |
| 4209 | (estimated_size * data->too_many_index_match_ratio); |
| 4210 | bool use_sequential_search = |
| 4211 | (n_existing_records < too_many_index_match_threshold); |
| 4212 | if (use_sequential_search) { |
| 4213 | grn_obj reason; |
| 4214 | GRN_TEXT_INIT(&reason, 0); |
| 4215 | grn_text_printf(ctx, |
| 4216 | &reason, |
| 4217 | "too many index match: " |
| 4218 | "%d < %f (%u * %f)", |
| 4219 | n_existing_records, |
| 4220 | too_many_index_match_threshold, |
| 4221 | estimated_size, |
| 4222 | data->too_many_index_match_ratio); |
| 4223 | GRN_TEXT_PUTC(ctx, &reason, '\0'); |
| 4224 | grn_report_index_not_used(ctx, |
| 4225 | data->tag, |
| 4226 | "", |
| 4227 | index, |
| 4228 | GRN_TEXT_VALUE(&reason)); |
| 4229 | GRN_OBJ_FIN(ctx, &reason); |
| 4230 | } |
| 4231 | return use_sequential_search; |
| 4232 | } |
| 4233 | |
| 4234 | static grn_rc |
| 4235 | selector_between_sequential_search(grn_ctx *ctx, |
| 4236 | grn_obj *table, |
| 4237 | between_data *data, |
| 4238 | grn_obj *res, |
| 4239 | grn_operator op) |
| 4240 | { |
| 4241 | { |
| 4242 | int offset = 0; |
| 4243 | int limit = -1; |
| 4244 | int flags = 0; |
| 4245 | grn_obj *target_table; |
| 4246 | grn_obj *target_column; |
| 4247 | grn_operator_exec_func *greater; |
| 4248 | grn_operator_exec_func *less; |
| 4249 | grn_table_cursor *cursor; |
| 4250 | grn_id id; |
| 4251 | grn_obj value; |
| 4252 | |
| 4253 | if (op == GRN_OP_AND) { |
| 4254 | target_table = res; |
| 4255 | } else { |
| 4256 | target_table = table; |
| 4257 | } |
| 4258 | cursor = grn_table_cursor_open(ctx, |
| 4259 | target_table, |
| 4260 | NULL, |
| 4261 | 0, |
| 4262 | NULL, |
| 4263 | 0, |
| 4264 | offset, |
| 4265 | limit, |
| 4266 | flags); |
| 4267 | if (!cursor) { |
| 4268 | return ctx->rc; |
| 4269 | } |
| 4270 | |
| 4271 | if (data->value->header.type == GRN_BULK) { |
| 4272 | target_column = grn_obj_column(ctx, |
| 4273 | table, |
| 4274 | GRN_TEXT_VALUE(data->value), |
| 4275 | GRN_TEXT_LEN(data->value)); |
| 4276 | } else { |
| 4277 | target_column = data->value; |
| 4278 | } |
| 4279 | if (data->min_border_type == BETWEEN_BORDER_INCLUDE) { |
| 4280 | greater = grn_operator_exec_greater_equal; |
| 4281 | } else { |
| 4282 | greater = grn_operator_exec_greater; |
| 4283 | } |
| 4284 | if (data->max_border_type == BETWEEN_BORDER_INCLUDE) { |
| 4285 | less = grn_operator_exec_less_equal; |
| 4286 | } else { |
| 4287 | less = grn_operator_exec_less; |
| 4288 | } |
| 4289 | |
| 4290 | GRN_VOID_INIT(&value); |
| 4291 | while ((id = grn_table_cursor_next(ctx, cursor)) != GRN_ID_NIL) { |
| 4292 | grn_id record_id; |
| 4293 | |
| 4294 | if (target_table == res) { |
| 4295 | grn_id *key; |
| 4296 | grn_table_cursor_get_key(ctx, cursor, (void **)&key); |
| 4297 | record_id = *key; |
| 4298 | } else { |
| 4299 | record_id = id; |
| 4300 | } |
| 4301 | |
| 4302 | GRN_BULK_REWIND(&value); |
| 4303 | grn_obj_get_value(ctx, target_column, record_id, &value); |
| 4304 | if (greater(ctx, &value, data->min) && less(ctx, &value, data->max)) { |
| 4305 | grn_posting_internal posting = {0}; |
| 4306 | posting.rid = record_id; |
| 4307 | posting.sid = 1; |
| 4308 | posting.pos = 0; |
| 4309 | posting.weight_float = 1; |
| 4310 | grn_ii_posting_add_float(ctx, |
| 4311 | (grn_posting *)(&posting), |
| 4312 | (grn_hash *)res, |
| 4313 | op); |
| 4314 | } |
| 4315 | } |
| 4316 | |
| 4317 | GRN_OBJ_FIN(ctx, &value); |
| 4318 | |
| 4319 | if (target_column != data->value && |
| 4320 | target_column->header.type == GRN_ACCESSOR) { |
| 4321 | grn_obj_unlink(ctx, target_column); |
| 4322 | } |
| 4323 | |
| 4324 | grn_table_cursor_close(ctx, cursor); |
| 4325 | |
| 4326 | grn_ii_resolve_sel_and(ctx, (grn_hash *)res, op); |
| 4327 | } |
| 4328 | |
| 4329 | return GRN_SUCCESS; |
| 4330 | } |
| 4331 | |
| 4332 | static grn_rc |
| 4333 | selector_between(grn_ctx *ctx, |
| 4334 | grn_obj *table, |
| 4335 | grn_obj *index, |
| 4336 | int nargs, |
| 4337 | grn_obj **args, |
| 4338 | grn_obj *res, |
| 4339 | grn_operator op) |
| 4340 | { |
| 4341 | grn_rc rc = GRN_SUCCESS; |
| 4342 | int offset = 0; |
| 4343 | int limit = -1; |
| 4344 | between_data data; |
| 4345 | bool use_sequential_search; |
| 4346 | bool index_table_need_unref = false; |
| 4347 | grn_obj *index_table = NULL; |
| 4348 | grn_table_cursor *cursor; |
| 4349 | |
| 4350 | between_data_init(ctx, &data); |
| 4351 | data.cursor_flags = GRN_CURSOR_ASCENDING | GRN_CURSOR_BY_KEY; |
| 4352 | rc = between_parse_args(ctx, nargs - 1, args + 1, &data); |
| 4353 | if (rc != GRN_SUCCESS) { |
| 4354 | goto exit; |
| 4355 | } |
| 4356 | |
| 4357 | if (data.min_border_type == BETWEEN_BORDER_EXCLUDE) { |
| 4358 | data.cursor_flags |= GRN_CURSOR_GT; |
| 4359 | } |
| 4360 | if (data.max_border_type == BETWEEN_BORDER_EXCLUDE) { |
| 4361 | data.cursor_flags |= GRN_CURSOR_LT; |
| 4362 | } |
| 4363 | |
| 4364 | if (data.value->header.type == GRN_COLUMN_INDEX) { |
| 4365 | index = data.value; |
| 4366 | } |
| 4367 | |
| 4368 | if (index) { |
| 4369 | switch (index->header.type) { |
| 4370 | case GRN_TABLE_NO_KEY: |
| 4371 | case GRN_TABLE_HASH_KEY: |
| 4372 | break; |
| 4373 | case GRN_TABLE_PAT_KEY: |
| 4374 | case GRN_TABLE_DAT_KEY: |
| 4375 | index_table = index; |
| 4376 | index = NULL; |
| 4377 | break; |
| 4378 | default: |
| 4379 | index_table = grn_ctx_at(ctx, index->header.domain); |
| 4380 | if (index_table) { |
| 4381 | index_table_need_unref = true; |
| 4382 | } |
| 4383 | break; |
| 4384 | } |
| 4385 | } |
| 4386 | |
| 4387 | if (index_table) { |
| 4388 | use_sequential_search = |
| 4389 | selector_between_sequential_search_should_use(ctx, |
| 4390 | table, |
| 4391 | index, |
| 4392 | index_table, |
| 4393 | &data, |
| 4394 | res, |
| 4395 | op); |
| 4396 | } else { |
| 4397 | use_sequential_search = true; |
| 4398 | } |
| 4399 | if (use_sequential_search) { |
| 4400 | rc = selector_between_sequential_search(ctx, table, &data, res, op); |
| 4401 | goto exit; |
| 4402 | } |
| 4403 | |
| 4404 | cursor = grn_table_cursor_open(ctx, |
| 4405 | index_table, |
| 4406 | GRN_BULK_HEAD(data.min), |
| 4407 | GRN_BULK_VSIZE(data.min), |
| 4408 | GRN_BULK_HEAD(data.max), |
| 4409 | GRN_BULK_VSIZE(data.max), |
| 4410 | offset, |
| 4411 | limit, |
| 4412 | data.cursor_flags); |
| 4413 | if (!cursor) { |
| 4414 | rc = ctx->rc; |
| 4415 | goto exit; |
| 4416 | } |
| 4417 | |
| 4418 | if (index) { |
| 4419 | grn_obj *index_cursor = |
| 4420 | grn_index_cursor_open(ctx, cursor, index, GRN_ID_NIL, GRN_ID_MAX, 0); |
| 4421 | if (index_cursor) { |
| 4422 | grn_result_set_add_index_cursor(ctx, |
| 4423 | (grn_hash *)res, |
| 4424 | index_cursor, |
| 4425 | 1, |
| 4426 | 1, |
| 4427 | op); |
| 4428 | grn_obj_close(ctx, index_cursor); |
| 4429 | } |
| 4430 | } else { |
| 4431 | grn_result_set_add_table_cursor(ctx, (grn_hash *)res, cursor, 1, op); |
| 4432 | } |
| 4433 | grn_ii_resolve_sel_and(ctx, (grn_hash *)res, op); |
| 4434 | grn_table_cursor_close(ctx, cursor); |
| 4435 | |
| 4436 | exit: |
| 4437 | between_data_fin(ctx, &data); |
| 4438 | |
| 4439 | if (index_table_need_unref) { |
| 4440 | grn_obj_unref(ctx, index_table); |
| 4441 | } |
| 4442 | |
| 4443 | return rc; |
| 4444 | } |
| 4445 | |
| 4446 | static grn_obj * |
| 4447 | func_in_values(grn_ctx *ctx, |
| 4448 | int nargs, |
| 4449 | grn_obj **args, |
| 4450 | grn_user_data *user_data) |
| 4451 | { |
| 4452 | grn_obj *found; |
| 4453 | grn_obj *target_value; |
| 4454 | int i; |
| 4455 | |
| 4456 | found = GRN_PROC_ALLOC(GRN_DB_BOOL, 0); |
| 4457 | if (!found) { |
| 4458 | return NULL; |
| 4459 | } |
| 4460 | GRN_BOOL_SET(ctx, found, false); |
| 4461 | |
| 4462 | if (nargs < 1) { |
| 4463 | ERR(GRN_INVALID_ARGUMENT, |
| 4464 | "in_values(): wrong number of arguments (%d for 1..)", |
| 4465 | nargs); |
| 4466 | return found; |
| 4467 | } |
| 4468 | |
| 4469 | target_value = args[0]; |
| 4470 | for (i = 1; i < nargs; i++) { |
| 4471 | grn_obj *value = args[i]; |
| 4472 | |
| 4473 | bool result = grn_operator_exec_equal(ctx, target_value, value); |
| 4474 | if (ctx->rc) { |
| 4475 | break; |
| 4476 | } |
| 4477 | |
| 4478 | if (result) { |
| 4479 | GRN_BOOL_SET(ctx, found, true); |
| 4480 | break; |
| 4481 | } |
| 4482 | } |
| 4483 | |
| 4484 | return found; |
| 4485 | } |
| 4486 | |
| 4487 | static bool |
| 4488 | is_reference_type_column(grn_ctx *ctx, grn_obj *column) |
| 4489 | { |
| 4490 | bool is_reference_type; |
| 4491 | grn_obj *range; |
| 4492 | |
| 4493 | range = grn_ctx_at(ctx, grn_obj_get_range(ctx, column)); |
| 4494 | switch (range->header.type) { |
| 4495 | case GRN_TABLE_HASH_KEY: |
| 4496 | case GRN_TABLE_PAT_KEY: |
| 4497 | case GRN_TABLE_DAT_KEY: |
| 4498 | is_reference_type = true; |
| 4499 | break; |
| 4500 | default: |
| 4501 | is_reference_type = false; |
| 4502 | break; |
| 4503 | } |
| 4504 | grn_obj_unlink(ctx, range); |
| 4505 | |
| 4506 | return is_reference_type; |
| 4507 | } |
| 4508 | |
| 4509 | static grn_obj * |
| 4510 | selector_in_values_find_source(grn_ctx *ctx, grn_obj *index, grn_obj *res) |
| 4511 | { |
| 4512 | grn_id source_id = GRN_ID_NIL; |
| 4513 | grn_obj source_ids; |
| 4514 | unsigned int n_source_ids; |
| 4515 | |
| 4516 | GRN_UINT32_INIT(&source_ids, GRN_OBJ_VECTOR); |
| 4517 | grn_obj_get_info(ctx, index, GRN_INFO_SOURCE, &source_ids); |
| 4518 | n_source_ids = GRN_BULK_VSIZE(&source_ids) / sizeof(grn_id); |
| 4519 | if (n_source_ids == 1) { |
| 4520 | source_id = GRN_UINT32_VALUE_AT(&source_ids, 0); |
| 4521 | } |
| 4522 | GRN_OBJ_FIN(ctx, &source_ids); |
| 4523 | |
| 4524 | if (source_id == GRN_ID_NIL) { |
| 4525 | return NULL; |
| 4526 | } else { |
| 4527 | return grn_ctx_at(ctx, source_id); |
| 4528 | } |
| 4529 | } |
| 4530 | |
| 4531 | static bool |
| 4532 | selector_in_values_sequential_search(grn_ctx *ctx, |
| 4533 | grn_obj *table, |
| 4534 | grn_obj *index, |
| 4535 | int n_values, |
| 4536 | grn_obj **values, |
| 4537 | grn_obj *res, |
| 4538 | grn_operator op, |
| 4539 | double too_many_index_match_ratio, |
| 4540 | const char *tag) |
| 4541 | { |
| 4542 | grn_obj *source; |
| 4543 | int n_existing_records; |
| 4544 | |
| 4545 | if (too_many_index_match_ratio < 0.0) { |
| 4546 | return false; |
| 4547 | } |
| 4548 | |
| 4549 | if (op != GRN_OP_AND) { |
| 4550 | return false; |
| 4551 | } |
| 4552 | |
| 4553 | if (index->header.flags & GRN_OBJ_WITH_WEIGHT) { |
| 4554 | return false; |
| 4555 | } |
| 4556 | |
| 4557 | n_existing_records = grn_table_size(ctx, res); |
| 4558 | if (n_existing_records == 0) { |
| 4559 | return true; |
| 4560 | } |
| 4561 | |
| 4562 | source = selector_in_values_find_source(ctx, index, res); |
| 4563 | if (!source) { |
| 4564 | return false; |
| 4565 | } |
| 4566 | |
| 4567 | if (!is_reference_type_column(ctx, source)) { |
| 4568 | grn_obj_unlink(ctx, source); |
| 4569 | return false; |
| 4570 | } |
| 4571 | |
| 4572 | uint32_t n_indexed_records = 0; |
| 4573 | { |
| 4574 | grn_obj value_ids; |
| 4575 | int i, n_value_ids; |
| 4576 | |
| 4577 | { |
| 4578 | grn_id range_id; |
| 4579 | grn_obj *range; |
| 4580 | |
| 4581 | range_id = grn_obj_get_range(ctx, source); |
| 4582 | range = grn_ctx_at(ctx, range_id); |
| 4583 | if (!range) { |
| 4584 | grn_obj_unlink(ctx, source); |
| 4585 | return false; |
| 4586 | } |
| 4587 | |
| 4588 | GRN_RECORD_INIT(&value_ids, GRN_OBJ_VECTOR, range_id); |
| 4589 | for (i = 0; i < n_values; i++) { |
| 4590 | grn_obj *value = values[i]; |
| 4591 | grn_id value_id; |
| 4592 | |
| 4593 | value_id = |
| 4594 | grn_table_get(ctx, range, GRN_TEXT_VALUE(value), GRN_TEXT_LEN(value)); |
| 4595 | if (value_id == GRN_ID_NIL) { |
| 4596 | continue; |
| 4597 | } |
| 4598 | GRN_RECORD_PUT(ctx, &value_ids, value_id); |
| 4599 | } |
| 4600 | grn_obj_unlink(ctx, range); |
| 4601 | } |
| 4602 | |
| 4603 | n_value_ids = GRN_BULK_VSIZE(&value_ids) / sizeof(grn_id); |
| 4604 | for (i = 0; i < n_value_ids; i++) { |
| 4605 | grn_id value_id = GRN_RECORD_VALUE_AT(&value_ids, i); |
| 4606 | n_indexed_records += grn_ii_estimate_size(ctx, (grn_ii *)index, value_id); |
| 4607 | } |
| 4608 | |
| 4609 | /* |
| 4610 | * Same as: |
| 4611 | * ((n_existing_record / n_indexed_records) > |
| 4612 | * grn_in_values_too_many_index_match_ratio) |
| 4613 | */ |
| 4614 | if (n_existing_records > (n_indexed_records * too_many_index_match_ratio)) { |
| 4615 | grn_obj_unlink(ctx, &value_ids); |
| 4616 | grn_obj_unlink(ctx, source); |
| 4617 | return false; |
| 4618 | } |
| 4619 | |
| 4620 | { |
| 4621 | grn_obj *accessor; |
| 4622 | char local_source_name[GRN_TABLE_MAX_KEY_SIZE]; |
| 4623 | int local_source_name_length; |
| 4624 | |
| 4625 | local_source_name_length = |
| 4626 | grn_column_name(ctx, source, local_source_name, GRN_TABLE_MAX_KEY_SIZE); |
| 4627 | accessor = |
| 4628 | grn_obj_column(ctx, res, local_source_name, local_source_name_length); |
| 4629 | { |
| 4630 | grn_table_cursor *cursor; |
| 4631 | grn_id id; |
| 4632 | grn_obj record_value; |
| 4633 | |
| 4634 | GRN_VOID_INIT(&record_value); |
| 4635 | cursor = grn_table_cursor_open(ctx, |
| 4636 | res, |
| 4637 | NULL, |
| 4638 | 0, |
| 4639 | NULL, |
| 4640 | 0, |
| 4641 | 0, |
| 4642 | -1, |
| 4643 | GRN_CURSOR_ASCENDING); |
| 4644 | while ((id = grn_table_cursor_next(ctx, cursor)) != GRN_ID_NIL) { |
| 4645 | grn_id *record_id; |
| 4646 | grn_table_cursor_get_key(ctx, cursor, (void **)&record_id); |
| 4647 | GRN_BULK_REWIND(&record_value); |
| 4648 | grn_obj_get_value(ctx, accessor, id, &record_value); |
| 4649 | for (i = 0; i < n_value_ids; i++) { |
| 4650 | grn_id value_id = GRN_RECORD_VALUE_AT(&value_ids, i); |
| 4651 | switch (record_value.header.type) { |
| 4652 | case GRN_BULK: |
| 4653 | if (value_id == GRN_RECORD_VALUE(&record_value)) { |
| 4654 | grn_posting_internal posting = {0}; |
| 4655 | posting.rid = *record_id; |
| 4656 | posting.sid = 1; |
| 4657 | posting.pos = 0; |
| 4658 | posting.weight_float = 1; |
| 4659 | grn_ii_posting_add_float(ctx, |
| 4660 | (grn_posting *)(&posting), |
| 4661 | (grn_hash *)res, |
| 4662 | op); |
| 4663 | } |
| 4664 | break; |
| 4665 | case GRN_UVECTOR: |
| 4666 | { |
| 4667 | int j, n_elements; |
| 4668 | n_elements = GRN_BULK_VSIZE(&record_value) / sizeof(grn_id); |
| 4669 | for (j = 0; j < n_elements; j++) { |
| 4670 | if (value_id == GRN_RECORD_VALUE_AT(&record_value, j)) { |
| 4671 | grn_posting_internal posting = {0}; |
| 4672 | posting.rid = *record_id; |
| 4673 | posting.sid = 1; |
| 4674 | posting.pos = 0; |
| 4675 | posting.weight_float = 1; |
| 4676 | grn_ii_posting_add_float(ctx, |
| 4677 | (grn_posting *)(&posting), |
| 4678 | (grn_hash *)res, |
| 4679 | op); |
| 4680 | } |
| 4681 | } |
| 4682 | } |
| 4683 | break; |
| 4684 | default: |
| 4685 | break; |
| 4686 | } |
| 4687 | } |
| 4688 | } |
| 4689 | grn_table_cursor_close(ctx, cursor); |
| 4690 | grn_ii_resolve_sel_and(ctx, (grn_hash *)res, op); |
| 4691 | GRN_OBJ_FIN(ctx, &record_value); |
| 4692 | } |
| 4693 | grn_obj_unlink(ctx, accessor); |
| 4694 | } |
| 4695 | grn_obj_unlink(ctx, &value_ids); |
| 4696 | } |
| 4697 | grn_obj_unlink(ctx, source); |
| 4698 | |
| 4699 | { |
| 4700 | grn_obj reason; |
| 4701 | GRN_TEXT_INIT(&reason, 0); |
| 4702 | grn_text_printf(ctx, |
| 4703 | &reason, |
| 4704 | "too many index match: " |
| 4705 | "%d < %f (%u * %f)", |
| 4706 | n_existing_records, |
| 4707 | (n_indexed_records * too_many_index_match_ratio), |
| 4708 | n_indexed_records, |
| 4709 | too_many_index_match_ratio); |
| 4710 | GRN_TEXT_PUTC(ctx, &reason, '\0'); |
| 4711 | grn_report_index_not_used(ctx, tag, "", index, GRN_TEXT_VALUE(&reason)); |
| 4712 | GRN_OBJ_FIN(ctx, &reason); |
| 4713 | } |
| 4714 | |
| 4715 | return true; |
| 4716 | } |
| 4717 | |
| 4718 | static void |
| 4719 | in_values_parse_options(grn_ctx *ctx, |
| 4720 | grn_obj *options, |
| 4721 | double *too_many_index_match_ratio, |
| 4722 | const char *tag) |
| 4723 | { |
| 4724 | grn_proc_options_parse(ctx, |
| 4725 | options, |
| 4726 | tag, |
| 4727 | "too_many_index_match_ratio", |
| 4728 | GRN_PROC_OPTION_VALUE_DOUBLE, |
| 4729 | too_many_index_match_ratio, |
| 4730 | NULL); |
| 4731 | } |
| 4732 | |
| 4733 | static grn_rc |
| 4734 | selector_in_values(grn_ctx *ctx, |
| 4735 | grn_obj *table, |
| 4736 | grn_obj *index, |
| 4737 | int nargs, |
| 4738 | grn_obj **args, |
| 4739 | grn_obj *res, |
| 4740 | grn_operator op) |
| 4741 | { |
| 4742 | grn_rc rc = GRN_SUCCESS; |
| 4743 | int i, n_values; |
| 4744 | grn_obj **values; |
| 4745 | double too_many_index_match_ratio = grn_in_values_too_many_index_match_ratio; |
| 4746 | const char *tag = "[in_values]"; |
| 4747 | |
| 4748 | if (!index) { |
| 4749 | return GRN_INVALID_ARGUMENT; |
| 4750 | } |
| 4751 | |
| 4752 | if (nargs < 2) { |
| 4753 | ERR(GRN_INVALID_ARGUMENT, |
| 4754 | "%s wrong number of arguments (%d for 1..)", |
| 4755 | tag, |
| 4756 | nargs); |
| 4757 | return ctx->rc; |
| 4758 | } |
| 4759 | |
| 4760 | n_values = nargs - 2; |
| 4761 | if (args[nargs - 1]->header.type == GRN_TABLE_HASH_KEY) { |
| 4762 | in_values_parse_options(ctx, |
| 4763 | args[nargs - 1], |
| 4764 | &too_many_index_match_ratio, |
| 4765 | tag); |
| 4766 | if (ctx->rc != GRN_SUCCESS) { |
| 4767 | return ctx->rc; |
| 4768 | } |
| 4769 | n_values--; |
| 4770 | } |
| 4771 | values = args + 2; |
| 4772 | |
| 4773 | if (n_values == 0) { |
| 4774 | return rc; |
| 4775 | } |
| 4776 | |
| 4777 | if (selector_in_values_sequential_search(ctx, |
| 4778 | table, |
| 4779 | index, |
| 4780 | n_values, |
| 4781 | values, |
| 4782 | res, |
| 4783 | op, |
| 4784 | too_many_index_match_ratio, |
| 4785 | tag)) { |
| 4786 | return ctx->rc; |
| 4787 | } |
| 4788 | |
| 4789 | int original_flags = ctx->flags; |
| 4790 | ctx->flags |= GRN_CTX_TEMPORARY_DISABLE_II_RESOLVE_SEL_AND; |
| 4791 | for (i = 0; i < n_values; i++) { |
| 4792 | grn_obj *value = values[i]; |
| 4793 | grn_search_optarg search_options; |
| 4794 | memset(&search_options, 0, sizeof(grn_search_optarg)); |
| 4795 | search_options.mode = GRN_OP_EXACT; |
| 4796 | search_options.similarity_threshold = 0; |
| 4797 | search_options.max_interval = 0; |
| 4798 | search_options.additional_last_interval = 0; |
| 4799 | search_options.weight_vector = NULL; |
| 4800 | search_options.vector_size = 0; |
| 4801 | search_options.proc = NULL; |
| 4802 | search_options.max_size = 0; |
| 4803 | search_options.scorer = NULL; |
| 4804 | search_options.query_options = NULL; |
| 4805 | search_options.min_interval = NULL; |
| 4806 | search_options.start_position = NULL; |
| 4807 | if (i == n_values - 1) { |
| 4808 | ctx->flags = original_flags; |
| 4809 | } |
| 4810 | rc = grn_obj_search(ctx, index, value, res, op, &search_options); |
| 4811 | if (rc != GRN_SUCCESS) { |
| 4812 | break; |
| 4813 | } |
| 4814 | } |
| 4815 | |
| 4816 | return rc; |
| 4817 | } |
| 4818 | |
| 4819 | static grn_obj * |
| 4820 | proc_range_filter(grn_ctx *ctx, |
| 4821 | int nargs, |
| 4822 | grn_obj **args, |
| 4823 | grn_user_data *user_data) |
| 4824 | { |
| 4825 | grn_obj *table_name = VAR(0); |
| 4826 | grn_obj *column_name = VAR(1); |
| 4827 | grn_obj *min = VAR(2); |
| 4828 | grn_obj *min_border = VAR(3); |
| 4829 | grn_obj *max = VAR(4); |
| 4830 | grn_obj *max_border = VAR(5); |
| 4831 | grn_obj *offset = VAR(6); |
| 4832 | grn_obj *limit = VAR(7); |
| 4833 | grn_obj *filter = VAR(8); |
| 4834 | grn_obj *output_columns = VAR(9); |
| 4835 | grn_obj *table; |
| 4836 | grn_obj *res = NULL; |
| 4837 | grn_obj *filter_expr = NULL; |
| 4838 | grn_obj *filter_variable = NULL; |
| 4839 | int real_offset; |
| 4840 | int real_limit; |
| 4841 | |
| 4842 | table = |
| 4843 | grn_ctx_get(ctx, GRN_TEXT_VALUE(table_name), GRN_TEXT_LEN(table_name)); |
| 4844 | if (!table) { |
| 4845 | ERR(GRN_INVALID_ARGUMENT, |
| 4846 | "[range_filter] nonexistent table <%.*s>", |
| 4847 | (int)GRN_TEXT_LEN(table_name), |
| 4848 | GRN_TEXT_VALUE(table_name)); |
| 4849 | return NULL; |
| 4850 | } |
| 4851 | |
| 4852 | if (GRN_TEXT_LEN(filter) > 0) { |
| 4853 | GRN_EXPR_CREATE_FOR_QUERY(ctx, table, filter_expr, filter_variable); |
| 4854 | if (!filter_expr) { |
| 4855 | ERR(GRN_INVALID_ARGUMENT, "[range_filter] failed to create expression"); |
| 4856 | goto exit; |
| 4857 | } |
| 4858 | |
| 4859 | grn_expr_parse(ctx, |
| 4860 | filter_expr, |
| 4861 | GRN_TEXT_VALUE(filter), |
| 4862 | GRN_TEXT_LEN(filter), |
| 4863 | NULL, |
| 4864 | GRN_OP_MATCH, |
| 4865 | GRN_OP_AND, |
| 4866 | GRN_EXPR_SYNTAX_SCRIPT); |
| 4867 | if (ctx->rc != GRN_SUCCESS) { |
| 4868 | goto exit; |
| 4869 | } |
| 4870 | } |
| 4871 | |
| 4872 | res = grn_table_create(ctx, |
| 4873 | NULL, |
| 4874 | 0, |
| 4875 | NULL, |
| 4876 | GRN_OBJ_TABLE_HASH_KEY | GRN_OBJ_WITH_SUBREC, |
| 4877 | table, |
| 4878 | NULL); |
| 4879 | if (!res) { |
| 4880 | ERR(GRN_INVALID_ARGUMENT, "[range_filter] failed to result table"); |
| 4881 | goto exit; |
| 4882 | } |
| 4883 | |
| 4884 | { |
| 4885 | grn_obj int32_value; |
| 4886 | |
| 4887 | GRN_INT32_INIT(&int32_value, 0); |
| 4888 | |
| 4889 | if (GRN_TEXT_LEN(offset) > 0) { |
| 4890 | if (grn_obj_cast(ctx, offset, &int32_value, false) != GRN_SUCCESS) { |
| 4891 | ERR(GRN_INVALID_ARGUMENT, |
| 4892 | "[range_filter] invalid offset format: <%.*s>", |
| 4893 | (int)GRN_TEXT_LEN(offset), |
| 4894 | GRN_TEXT_VALUE(offset)); |
| 4895 | GRN_OBJ_FIN(ctx, &int32_value); |
| 4896 | goto exit; |
| 4897 | } |
| 4898 | real_offset = GRN_INT32_VALUE(&int32_value); |
| 4899 | } else { |
| 4900 | real_offset = 0; |
| 4901 | } |
| 4902 | |
| 4903 | GRN_BULK_REWIND(&int32_value); |
| 4904 | |
| 4905 | if (GRN_TEXT_LEN(limit) > 0) { |
| 4906 | if (grn_obj_cast(ctx, limit, &int32_value, false) != GRN_SUCCESS) { |
| 4907 | ERR(GRN_INVALID_ARGUMENT, |
| 4908 | "[range_filter] invalid limit format: <%.*s>", |
| 4909 | (int)GRN_TEXT_LEN(limit), |
| 4910 | GRN_TEXT_VALUE(limit)); |
| 4911 | GRN_OBJ_FIN(ctx, &int32_value); |
| 4912 | goto exit; |
| 4913 | } |
| 4914 | real_limit = GRN_INT32_VALUE(&int32_value); |
| 4915 | } else { |
| 4916 | real_limit = GRN_SELECT_DEFAULT_LIMIT; |
| 4917 | } |
| 4918 | |
| 4919 | GRN_OBJ_FIN(ctx, &int32_value); |
| 4920 | } |
| 4921 | { |
| 4922 | grn_rc rc; |
| 4923 | int original_offset = real_offset; |
| 4924 | int original_limit = real_limit; |
| 4925 | rc = grn_output_range_normalize(ctx, |
| 4926 | grn_table_size(ctx, table), |
| 4927 | &real_offset, |
| 4928 | &real_limit); |
| 4929 | switch (rc) { |
| 4930 | case GRN_TOO_SMALL_OFFSET: |
| 4931 | ERR(GRN_INVALID_ARGUMENT, |
| 4932 | "[range_filter] too small offset: <%d>", |
| 4933 | original_offset); |
| 4934 | goto exit; |
| 4935 | case GRN_TOO_LARGE_OFFSET: |
| 4936 | ERR(GRN_INVALID_ARGUMENT, |
| 4937 | "[range_filter] too large offset: <%d>", |
| 4938 | original_offset); |
| 4939 | goto exit; |
| 4940 | case GRN_TOO_SMALL_LIMIT: |
| 4941 | ERR(GRN_INVALID_ARGUMENT, |
| 4942 | "[range_filter] too small limit: <%d>", |
| 4943 | original_limit); |
| 4944 | goto exit; |
| 4945 | default: |
| 4946 | break; |
| 4947 | } |
| 4948 | } |
| 4949 | |
| 4950 | if (real_limit != 0) { |
| 4951 | grn_table_sort_key *sort_keys; |
| 4952 | unsigned int n_sort_keys; |
| 4953 | sort_keys = grn_table_sort_key_from_str(ctx, |
| 4954 | GRN_TEXT_VALUE(column_name), |
| 4955 | GRN_TEXT_LEN(column_name), |
| 4956 | table, |
| 4957 | &n_sort_keys); |
| 4958 | if (n_sort_keys == 1) { |
| 4959 | grn_table_sort_key *sort_key; |
| 4960 | grn_obj *index; |
| 4961 | int n_indexes; |
| 4962 | grn_operator op = GRN_OP_OR; |
| 4963 | |
| 4964 | sort_key = &(sort_keys[0]); |
| 4965 | n_indexes = |
| 4966 | grn_column_index(ctx, sort_key->key, GRN_OP_LESS, &index, 1, NULL); |
| 4967 | if (n_indexes > 0) { |
| 4968 | grn_obj *lexicon; |
| 4969 | grn_table_cursor *table_cursor; |
| 4970 | int table_cursor_flags = 0; |
| 4971 | between_border_type min_border_type; |
| 4972 | between_border_type max_border_type; |
| 4973 | grn_obj real_min; |
| 4974 | grn_obj real_max; |
| 4975 | int n_records = 0; |
| 4976 | grn_obj *index_cursor; |
| 4977 | int index_cursor_flags = 0; |
| 4978 | grn_posting *posting; |
| 4979 | |
| 4980 | lexicon = grn_ctx_at(ctx, index->header.domain); |
| 4981 | if (sort_key->flags & GRN_TABLE_SORT_DESC) { |
| 4982 | table_cursor_flags |= GRN_CURSOR_DESCENDING; |
| 4983 | } else { |
| 4984 | table_cursor_flags |= GRN_CURSOR_ASCENDING; |
| 4985 | } |
| 4986 | if (GRN_TEXT_LEN(min_border) > 0) { |
| 4987 | min_border_type = between_parse_border(ctx, min_border, "min_border"); |
| 4988 | } else { |
| 4989 | min_border_type = BETWEEN_BORDER_INCLUDE; |
| 4990 | } |
| 4991 | if (GRN_TEXT_LEN(max_border) > 0) { |
| 4992 | max_border_type = between_parse_border(ctx, max_border, "max_border"); |
| 4993 | } else { |
| 4994 | max_border_type = BETWEEN_BORDER_INCLUDE; |
| 4995 | } |
| 4996 | if (min_border_type == BETWEEN_BORDER_EXCLUDE) { |
| 4997 | table_cursor_flags |= GRN_CURSOR_GT; |
| 4998 | } |
| 4999 | if (max_border_type == BETWEEN_BORDER_EXCLUDE) { |
| 5000 | table_cursor_flags |= GRN_CURSOR_LT; |
| 5001 | } |
| 5002 | GRN_OBJ_INIT(&real_min, GRN_BULK, 0, lexicon->header.domain); |
| 5003 | GRN_OBJ_INIT(&real_max, GRN_BULK, 0, lexicon->header.domain); |
| 5004 | if (GRN_TEXT_LEN(min) > 0) { |
| 5005 | grn_obj_cast(ctx, min, &real_min, false); |
| 5006 | } |
| 5007 | if (GRN_TEXT_LEN(max) > 0) { |
| 5008 | grn_obj_cast(ctx, max, &real_max, false); |
| 5009 | } |
| 5010 | table_cursor = grn_table_cursor_open(ctx, |
| 5011 | lexicon, |
| 5012 | GRN_BULK_HEAD(&real_min), |
| 5013 | GRN_BULK_VSIZE(&real_min), |
| 5014 | GRN_BULK_HEAD(&real_max), |
| 5015 | GRN_BULK_VSIZE(&real_max), |
| 5016 | 0, |
| 5017 | -1, |
| 5018 | table_cursor_flags); |
| 5019 | index_cursor = grn_index_cursor_open(ctx, |
| 5020 | table_cursor, |
| 5021 | index, |
| 5022 | GRN_ID_NIL, |
| 5023 | GRN_ID_NIL, |
| 5024 | index_cursor_flags); |
| 5025 | while ((posting = grn_index_cursor_next(ctx, index_cursor, NULL))) { |
| 5026 | bool result_boolean = false; |
| 5027 | |
| 5028 | if (filter_expr) { |
| 5029 | grn_obj *result; |
| 5030 | GRN_RECORD_SET(ctx, filter_variable, posting->rid); |
| 5031 | result = grn_expr_exec(ctx, filter_expr, 0); |
| 5032 | if (ctx->rc) { |
| 5033 | break; |
| 5034 | } |
| 5035 | result_boolean = grn_obj_is_true(ctx, result); |
| 5036 | } else { |
| 5037 | result_boolean = true; |
| 5038 | } |
| 5039 | |
| 5040 | if (result_boolean) { |
| 5041 | if (n_records >= real_offset) { |
| 5042 | grn_posting_internal add_posting = |
| 5043 | *((grn_posting_internal *)posting); |
| 5044 | add_posting.weight_float += 1; |
| 5045 | grn_ii_posting_add_float(ctx, |
| 5046 | (grn_posting *)(&add_posting), |
| 5047 | (grn_hash *)res, |
| 5048 | op); |
| 5049 | } |
| 5050 | n_records++; |
| 5051 | if (n_records == real_limit) { |
| 5052 | break; |
| 5053 | } |
| 5054 | } |
| 5055 | } |
| 5056 | grn_obj_unlink(ctx, index_cursor); |
| 5057 | grn_table_cursor_close(ctx, table_cursor); |
| 5058 | |
| 5059 | GRN_OBJ_FIN(ctx, &real_min); |
| 5060 | GRN_OBJ_FIN(ctx, &real_max); |
| 5061 | } |
| 5062 | grn_ii_resolve_sel_and(ctx, (grn_hash *)res, op); |
| 5063 | } |
| 5064 | grn_table_sort_key_close(ctx, sort_keys, n_sort_keys); |
| 5065 | } |
| 5066 | |
| 5067 | if (ctx->rc == GRN_SUCCESS) { |
| 5068 | const char *raw_output_columns; |
| 5069 | int raw_output_columns_len; |
| 5070 | |
| 5071 | raw_output_columns = GRN_TEXT_VALUE(output_columns); |
| 5072 | raw_output_columns_len = GRN_TEXT_LEN(output_columns); |
| 5073 | if (raw_output_columns_len == 0) { |
| 5074 | if (grn_obj_is_table_with_key(ctx, table)) { |
| 5075 | raw_output_columns = GRN_SELECT_DEFAULT_OUTPUT_COLUMNS_FOR_WITH_KEY; |
| 5076 | } else { |
| 5077 | raw_output_columns = GRN_SELECT_DEFAULT_OUTPUT_COLUMNS_FOR_NO_KEY; |
| 5078 | } |
| 5079 | raw_output_columns_len = strlen(raw_output_columns); |
| 5080 | } |
| 5081 | grn_proc_select_output_columns(ctx, |
| 5082 | res, |
| 5083 | -1, |
| 5084 | real_offset, |
| 5085 | real_limit, |
| 5086 | raw_output_columns, |
| 5087 | raw_output_columns_len, |
| 5088 | filter_expr); |
| 5089 | } |
| 5090 | |
| 5091 | exit: |
| 5092 | if (filter_expr) { |
| 5093 | grn_obj_unlink(ctx, filter_expr); |
| 5094 | } |
| 5095 | if (res) { |
| 5096 | grn_obj_unlink(ctx, res); |
| 5097 | } |
| 5098 | |
| 5099 | return NULL; |
| 5100 | } |
| 5101 | |
| 5102 | static grn_obj * |
| 5103 | proc_request_cancel(grn_ctx *ctx, |
| 5104 | int nargs, |
| 5105 | grn_obj **args, |
| 5106 | grn_user_data *user_data) |
| 5107 | { |
| 5108 | grn_obj *id = VAR(0); |
| 5109 | bool canceled; |
| 5110 | |
| 5111 | if (GRN_TEXT_LEN(id) == 0) { |
| 5112 | ERR(GRN_INVALID_ARGUMENT, "[request_cancel] ID is missing"); |
| 5113 | return NULL; |
| 5114 | } |
| 5115 | |
| 5116 | canceled = grn_request_canceler_cancel(GRN_TEXT_VALUE(id), GRN_TEXT_LEN(id)); |
| 5117 | |
| 5118 | GRN_OUTPUT_MAP_OPEN("result", 2); |
| 5119 | GRN_OUTPUT_CSTR("id"); |
| 5120 | GRN_OUTPUT_STR(GRN_TEXT_VALUE(id), GRN_TEXT_LEN(id)); |
| 5121 | GRN_OUTPUT_CSTR("canceled"); |
| 5122 | GRN_OUTPUT_BOOL(canceled); |
| 5123 | GRN_OUTPUT_MAP_CLOSE(); |
| 5124 | |
| 5125 | return NULL; |
| 5126 | } |
| 5127 | |
| 5128 | static grn_obj * |
| 5129 | proc_plugin_register(grn_ctx *ctx, |
| 5130 | int nargs, |
| 5131 | grn_obj **args, |
| 5132 | grn_user_data *user_data) |
| 5133 | { |
| 5134 | if (GRN_TEXT_LEN(VAR(0))) { |
| 5135 | const char *name; |
| 5136 | GRN_TEXT_PUTC(ctx, VAR(0), '\0'); |
| 5137 | name = GRN_TEXT_VALUE(VAR(0)); |
| 5138 | grn_plugin_register(ctx, name); |
| 5139 | } else { |
| 5140 | ERR(GRN_INVALID_ARGUMENT, "[plugin_register] name is missing"); |
| 5141 | } |
| 5142 | GRN_OUTPUT_BOOL(!ctx->rc); |
| 5143 | return NULL; |
| 5144 | } |
| 5145 | |
| 5146 | static grn_obj * |
| 5147 | proc_plugin_unregister(grn_ctx *ctx, |
| 5148 | int nargs, |
| 5149 | grn_obj **args, |
| 5150 | grn_user_data *user_data) |
| 5151 | { |
| 5152 | if (GRN_TEXT_LEN(VAR(0))) { |
| 5153 | const char *name; |
| 5154 | GRN_TEXT_PUTC(ctx, VAR(0), '\0'); |
| 5155 | name = GRN_TEXT_VALUE(VAR(0)); |
| 5156 | grn_plugin_unregister(ctx, name); |
| 5157 | } else { |
| 5158 | ERR(GRN_INVALID_ARGUMENT, "[plugin_unregister] name is missing"); |
| 5159 | } |
| 5160 | GRN_OUTPUT_BOOL(!ctx->rc); |
| 5161 | return NULL; |
| 5162 | } |
| 5163 | |
| 5164 | static grn_obj * |
| 5165 | proc_io_flush(grn_ctx *ctx, int nargs, grn_obj **args, grn_user_data *user_data) |
| 5166 | { |
| 5167 | grn_obj *db = grn_ctx_db(ctx); |
| 5168 | |
| 5169 | grn_raw_string target_name; |
| 5170 | target_name.value = grn_plugin_proc_get_var_string(ctx, |
| 5171 | user_data, |
| 5172 | "target_name", |
| 5173 | -1, |
| 5174 | &(target_name.length)); |
| 5175 | grn_obj *target; |
| 5176 | if (target_name.length > 0) { |
| 5177 | target = grn_ctx_get(ctx, target_name.value, target_name.length); |
| 5178 | if (!target) { |
| 5179 | ERR(GRN_INVALID_ARGUMENT, |
| 5180 | "[io_flush] unknown target: <%.*s>", |
| 5181 | (int)target_name.length, |
| 5182 | target_name.value); |
| 5183 | GRN_OUTPUT_BOOL(false); |
| 5184 | return NULL; |
| 5185 | } |
| 5186 | } else { |
| 5187 | target = db; |
| 5188 | } |
| 5189 | |
| 5190 | grn_raw_string recursive; |
| 5191 | recursive.value = grn_plugin_proc_get_var_string(ctx, |
| 5192 | user_data, |
| 5193 | "recursive", |
| 5194 | -1, |
| 5195 | &(recursive.length)); |
| 5196 | bool is_only_opened = |
| 5197 | grn_plugin_proc_get_var_bool(ctx, user_data, "only_opened", -1, false); |
| 5198 | grn_rc rc = GRN_SUCCESS; |
| 5199 | if (is_only_opened) { |
| 5200 | rc = grn_obj_flush_only_opened(ctx, target); |
| 5201 | } else { |
| 5202 | if (GRN_RAW_STRING_EQUAL_CSTRING(recursive, "dependent")) { |
| 5203 | rc = grn_obj_flush_recursive_dependent(ctx, target); |
| 5204 | } else if (GRN_RAW_STRING_EQUAL_CSTRING(recursive, "no")) { |
| 5205 | rc = grn_obj_flush(ctx, target); |
| 5206 | } else { |
| 5207 | rc = grn_obj_flush_recursive(ctx, target); |
| 5208 | } |
| 5209 | } |
| 5210 | |
| 5211 | if (target->header.type != GRN_DB) { |
| 5212 | grn_obj_unlink(ctx, target); |
| 5213 | } |
| 5214 | |
| 5215 | GRN_OUTPUT_BOOL(rc == GRN_SUCCESS); |
| 5216 | |
| 5217 | return NULL; |
| 5218 | } |
| 5219 | |
| 5220 | static grn_obj * |
| 5221 | proc_database_unmap(grn_ctx *ctx, |
| 5222 | int nargs, |
| 5223 | grn_obj **args, |
| 5224 | grn_user_data *user_data) |
| 5225 | { |
| 5226 | grn_rc rc; |
| 5227 | uint32_t current_limit; |
| 5228 | |
| 5229 | current_limit = grn_thread_get_limit_with_ctx(ctx); |
| 5230 | if (current_limit != 1) { |
| 5231 | ERR(GRN_OPERATION_NOT_PERMITTED, |
| 5232 | "[database_unmap] the max number of threads must be 1: <%u>", |
| 5233 | current_limit); |
| 5234 | GRN_OUTPUT_BOOL(false); |
| 5235 | return NULL; |
| 5236 | } |
| 5237 | |
| 5238 | rc = grn_db_unmap(ctx, grn_ctx_db(ctx)); |
| 5239 | GRN_OUTPUT_BOOL(rc == GRN_SUCCESS); |
| 5240 | |
| 5241 | return NULL; |
| 5242 | } |
| 5243 | |
| 5244 | static grn_obj * |
| 5245 | proc_reindex(grn_ctx *ctx, int nargs, grn_obj **args, grn_user_data *user_data) |
| 5246 | { |
| 5247 | grn_obj *target_name; |
| 5248 | grn_obj *target; |
| 5249 | |
| 5250 | target_name = VAR(0); |
| 5251 | if (GRN_TEXT_LEN(target_name) == 0) { |
| 5252 | target = grn_ctx_db(ctx); |
| 5253 | } else { |
| 5254 | target = |
| 5255 | grn_ctx_get(ctx, GRN_TEXT_VALUE(target_name), GRN_TEXT_LEN(target_name)); |
| 5256 | if (!target) { |
| 5257 | ERR(GRN_INVALID_ARGUMENT, |
| 5258 | "[reindex] nonexistent target: <%.*s>", |
| 5259 | (int)GRN_TEXT_LEN(target_name), |
| 5260 | GRN_TEXT_VALUE(target_name)); |
| 5261 | GRN_OUTPUT_BOOL(false); |
| 5262 | return NULL; |
| 5263 | } |
| 5264 | } |
| 5265 | |
| 5266 | grn_obj_reindex(ctx, target); |
| 5267 | |
| 5268 | GRN_OUTPUT_BOOL(ctx->rc == GRN_SUCCESS); |
| 5269 | |
| 5270 | return NULL; |
| 5271 | } |
| 5272 | |
| 5273 | static grn_rc |
| 5274 | selector_prefix_rk_search_key( |
| 5275 | grn_ctx *ctx, grn_obj *table, grn_obj *query, grn_obj *res, grn_operator op) |
| 5276 | { |
| 5277 | grn_rc rc = GRN_SUCCESS; |
| 5278 | |
| 5279 | if (table->header.type != GRN_TABLE_PAT_KEY) { |
| 5280 | grn_obj inspected_table; |
| 5281 | GRN_TEXT_INIT(&inspected_table, 0); |
| 5282 | grn_inspect(ctx, &inspected_table, table); |
| 5283 | ERR(GRN_INVALID_ARGUMENT, |
| 5284 | "prefix_rk_serach(): table of _key must TABLE_PAT_KEY: %.*s", |
| 5285 | (int)GRN_TEXT_LEN(&inspected_table), |
| 5286 | GRN_TEXT_VALUE(&inspected_table)); |
| 5287 | rc = ctx->rc; |
| 5288 | GRN_OBJ_FIN(ctx, &inspected_table); |
| 5289 | goto exit; |
| 5290 | } |
| 5291 | |
| 5292 | GRN_TABLE_EACH_BEGIN_MIN(ctx, |
| 5293 | table, |
| 5294 | cursor, |
| 5295 | id, |
| 5296 | GRN_TEXT_VALUE(query), |
| 5297 | GRN_TEXT_LEN(query), |
| 5298 | GRN_CURSOR_PREFIX | GRN_CURSOR_RK) |
| 5299 | { |
| 5300 | grn_posting_internal posting = {0}; |
| 5301 | posting.rid = id; |
| 5302 | posting.sid = 1; |
| 5303 | posting.pos = 0; |
| 5304 | posting.weight_float = 1; |
| 5305 | grn_ii_posting_add_float(ctx, |
| 5306 | (grn_posting *)(&posting), |
| 5307 | (grn_hash *)res, |
| 5308 | op); |
| 5309 | } |
| 5310 | GRN_TABLE_EACH_END(ctx, cursor); |
| 5311 | grn_ii_resolve_sel_and(ctx, (grn_hash *)res, op); |
| 5312 | |
| 5313 | exit: |
| 5314 | return rc; |
| 5315 | } |
| 5316 | |
| 5317 | static grn_rc |
| 5318 | selector_prefix_rk_search_index( |
| 5319 | grn_ctx *ctx, grn_obj *index, grn_obj *query, grn_obj *res, grn_operator op) |
| 5320 | { |
| 5321 | grn_rc rc = GRN_SUCCESS; |
| 5322 | grn_obj *table; |
| 5323 | |
| 5324 | table = grn_column_table(ctx, index); |
| 5325 | |
| 5326 | GRN_TABLE_EACH_BEGIN_MIN(ctx, |
| 5327 | table, |
| 5328 | cursor, |
| 5329 | id, |
| 5330 | GRN_TEXT_VALUE(query), |
| 5331 | GRN_TEXT_LEN(query), |
| 5332 | GRN_CURSOR_PREFIX | GRN_CURSOR_RK) |
| 5333 | { |
| 5334 | grn_ii_at(ctx, (grn_ii *)index, id, (grn_hash *)res, op); |
| 5335 | } |
| 5336 | GRN_TABLE_EACH_END(ctx, cursor); |
| 5337 | grn_ii_resolve_sel_and(ctx, (grn_hash *)res, op); |
| 5338 | |
| 5339 | return rc; |
| 5340 | } |
| 5341 | |
| 5342 | static grn_rc |
| 5343 | selector_prefix_rk_search(grn_ctx *ctx, |
| 5344 | grn_obj *table, |
| 5345 | grn_obj *index, |
| 5346 | int nargs, |
| 5347 | grn_obj **args, |
| 5348 | grn_obj *res, |
| 5349 | grn_operator op) |
| 5350 | { |
| 5351 | grn_rc rc = GRN_SUCCESS; |
| 5352 | grn_obj *column; |
| 5353 | grn_obj *query; |
| 5354 | |
| 5355 | if ((nargs - 1) != 2) { |
| 5356 | ERR(GRN_INVALID_ARGUMENT, |
| 5357 | "prefix_rk_serach(): wrong number of arguments (%d for 2)", |
| 5358 | nargs - 1); |
| 5359 | return ctx->rc; |
| 5360 | } |
| 5361 | |
| 5362 | column = args[1]; |
| 5363 | query = args[2]; |
| 5364 | |
| 5365 | if (grn_obj_is_table(ctx, index)) { |
| 5366 | rc = selector_prefix_rk_search_key(ctx, index, query, res, op); |
| 5367 | } else { |
| 5368 | rc = selector_prefix_rk_search_index(ctx, index, query, res, op); |
| 5369 | } |
| 5370 | return rc; |
| 5371 | } |
| 5372 | |
| 5373 | #define DEF_VAR(v, name_str) \ |
| 5374 | do { \ |
| 5375 | (v).name = (name_str); \ |
| 5376 | (v).name_size = GRN_STRLEN(name_str); \ |
| 5377 | GRN_TEXT_INIT(&(v).value, 0); \ |
| 5378 | } while (0) |
| 5379 | |
| 5380 | #define DEF_COMMAND(name, func, nvars, vars) \ |
| 5381 | (grn_proc_create(ctx, \ |
| 5382 | (name), \ |
| 5383 | (sizeof(name) - 1), \ |
| 5384 | GRN_PROC_COMMAND, \ |
| 5385 | (func), \ |
| 5386 | NULL, \ |
| 5387 | NULL, \ |
| 5388 | (nvars), \ |
| 5389 | (vars))) |
| 5390 | |
| 5391 | void |
| 5392 | grn_db_init_builtin_commands(grn_ctx *ctx) |
| 5393 | { |
| 5394 | grn_expr_var vars[10]; |
| 5395 | |
| 5396 | grn_proc_init_define_selector(ctx); |
| 5397 | grn_proc_init_select(ctx); |
| 5398 | |
| 5399 | DEF_VAR(vars[0], "values"); |
| 5400 | DEF_VAR(vars[1], "table"); |
| 5401 | DEF_VAR(vars[2], "columns"); |
| 5402 | DEF_VAR(vars[3], "ifexists"); |
| 5403 | DEF_VAR(vars[4], "input_type"); |
| 5404 | DEF_VAR(vars[5], "each"); |
| 5405 | DEF_VAR(vars[6], "output_ids"); |
| 5406 | DEF_VAR(vars[7], "output_errors"); |
| 5407 | DEF_VAR(vars[8], "lock_table"); |
| 5408 | DEF_COMMAND("load", proc_load, 9, vars); |
| 5409 | |
| 5410 | DEF_COMMAND("status", proc_status, 0, vars); |
| 5411 | |
| 5412 | grn_proc_init_table_list(ctx); |
| 5413 | |
| 5414 | grn_proc_init_column_list(ctx); |
| 5415 | |
| 5416 | grn_proc_init_table_create(ctx); |
| 5417 | |
| 5418 | grn_proc_init_table_remove(ctx); |
| 5419 | |
| 5420 | grn_proc_init_table_rename(ctx); |
| 5421 | |
| 5422 | grn_proc_init_column_create(ctx); |
| 5423 | |
| 5424 | grn_proc_init_column_remove(ctx); |
| 5425 | |
| 5426 | grn_proc_init_column_rename(ctx); |
| 5427 | |
| 5428 | DEF_VAR(vars[0], "path"); |
| 5429 | DEF_COMMAND(GRN_EXPR_MISSING_NAME, proc_missing, 1, vars); |
| 5430 | |
| 5431 | DEF_COMMAND("quit", proc_quit, 0, vars); |
| 5432 | |
| 5433 | DEF_VAR(vars[0], "mode"); |
| 5434 | DEF_COMMAND("shutdown", proc_shutdown, 1, vars); |
| 5435 | |
| 5436 | grn_proc_init_clearlock(ctx); |
| 5437 | grn_proc_init_lock_clear(ctx); |
| 5438 | |
| 5439 | DEF_VAR(vars[0], "target_name"); |
| 5440 | DEF_VAR(vars[1], "threshold"); |
| 5441 | DEF_COMMAND("defrag", proc_defrag, 2, vars); |
| 5442 | |
| 5443 | grn_proc_init_log_level(ctx); |
| 5444 | grn_proc_init_log_put(ctx); |
| 5445 | grn_proc_init_log_reopen(ctx); |
| 5446 | |
| 5447 | DEF_VAR(vars[0], "table"); |
| 5448 | DEF_VAR(vars[1], "key"); |
| 5449 | DEF_VAR(vars[2], "id"); |
| 5450 | DEF_VAR(vars[3], "filter"); |
| 5451 | DEF_COMMAND("delete", proc_delete, 4, vars); |
| 5452 | |
| 5453 | DEF_VAR(vars[0], "max"); |
| 5454 | DEF_COMMAND("cache_limit", proc_cache_limit, 1, vars); |
| 5455 | |
| 5456 | grn_proc_init_dump(ctx); |
| 5457 | |
| 5458 | /* Deprecated. Use "plugin_register" instead. */ |
| 5459 | DEF_VAR(vars[0], "path"); |
| 5460 | DEF_COMMAND("register", proc_register, 1, vars); |
| 5461 | |
| 5462 | DEF_VAR(vars[0], "obj"); |
| 5463 | DEF_COMMAND("check", proc_check, 1, vars); |
| 5464 | |
| 5465 | DEF_VAR(vars[0], "target_name"); |
| 5466 | DEF_VAR(vars[1], "table"); |
| 5467 | DEF_COMMAND("truncate", proc_truncate, 2, vars); |
| 5468 | |
| 5469 | grn_proc_init_normalize(ctx); |
| 5470 | |
| 5471 | grn_proc_init_tokenize(ctx); |
| 5472 | grn_proc_init_table_tokenize(ctx); |
| 5473 | |
| 5474 | DEF_COMMAND("tokenizer_list", proc_tokenizer_list, 0, vars); |
| 5475 | |
| 5476 | DEF_COMMAND("normalizer_list", proc_normalizer_list, 0, vars); |
| 5477 | |
| 5478 | { |
| 5479 | grn_obj *proc; |
| 5480 | proc = grn_proc_create(ctx, |
| 5481 | "rand", |
| 5482 | -1, |
| 5483 | GRN_PROC_FUNCTION, |
| 5484 | func_rand, |
| 5485 | NULL, |
| 5486 | NULL, |
| 5487 | 0, |
| 5488 | NULL); |
| 5489 | grn_proc_set_is_stable(ctx, proc, false); |
| 5490 | } |
| 5491 | |
| 5492 | { |
| 5493 | grn_obj *proc; |
| 5494 | proc = grn_proc_create(ctx, |
| 5495 | "now", |
| 5496 | -1, |
| 5497 | GRN_PROC_FUNCTION, |
| 5498 | func_now, |
| 5499 | NULL, |
| 5500 | NULL, |
| 5501 | 0, |
| 5502 | NULL); |
| 5503 | grn_proc_set_is_stable(ctx, proc, false); |
| 5504 | } |
| 5505 | |
| 5506 | grn_proc_create(ctx, |
| 5507 | "max", |
| 5508 | -1, |
| 5509 | GRN_PROC_FUNCTION, |
| 5510 | func_max, |
| 5511 | NULL, |
| 5512 | NULL, |
| 5513 | 0, |
| 5514 | NULL); |
| 5515 | grn_proc_create(ctx, |
| 5516 | "min", |
| 5517 | -1, |
| 5518 | GRN_PROC_FUNCTION, |
| 5519 | func_min, |
| 5520 | NULL, |
| 5521 | NULL, |
| 5522 | 0, |
| 5523 | NULL); |
| 5524 | |
| 5525 | { |
| 5526 | grn_obj *selector_proc; |
| 5527 | |
| 5528 | selector_proc = grn_proc_create(ctx, |
| 5529 | "geo_in_circle", |
| 5530 | -1, |
| 5531 | GRN_PROC_FUNCTION, |
| 5532 | func_geo_in_circle, |
| 5533 | NULL, |
| 5534 | NULL, |
| 5535 | 0, |
| 5536 | NULL); |
| 5537 | grn_proc_set_selector(ctx, selector_proc, grn_selector_geo_in_circle); |
| 5538 | /* We may need GRN_OP_GEO_IN_CIRCLE. */ |
| 5539 | grn_proc_set_selector_operator(ctx, selector_proc, GRN_OP_MATCH); |
| 5540 | |
| 5541 | selector_proc = grn_proc_create(ctx, |
| 5542 | "geo_in_rectangle", |
| 5543 | -1, |
| 5544 | GRN_PROC_FUNCTION, |
| 5545 | func_geo_in_rectangle, |
| 5546 | NULL, |
| 5547 | NULL, |
| 5548 | 0, |
| 5549 | NULL); |
| 5550 | grn_proc_set_selector(ctx, selector_proc, grn_selector_geo_in_rectangle); |
| 5551 | /* We may need GRN_OP_GEO_IN_RECTANGLE. */ |
| 5552 | grn_proc_set_selector_operator(ctx, selector_proc, GRN_OP_MATCH); |
| 5553 | } |
| 5554 | |
| 5555 | { |
| 5556 | grn_obj *geo_distance = grn_proc_create(ctx, |
| 5557 | "geo_distance", |
| 5558 | -1, |
| 5559 | GRN_PROC_FUNCTION, |
| 5560 | func_geo_distance, |
| 5561 | NULL, |
| 5562 | NULL, |
| 5563 | 0, |
| 5564 | NULL); |
| 5565 | grn_proc_set_sorter(ctx, geo_distance, grn_geo_distance_sorter); |
| 5566 | } |
| 5567 | |
| 5568 | /* deprecated. */ |
| 5569 | grn_proc_create(ctx, |
| 5570 | "geo_distance2", |
| 5571 | -1, |
| 5572 | GRN_PROC_FUNCTION, |
| 5573 | func_geo_distance2, |
| 5574 | NULL, |
| 5575 | NULL, |
| 5576 | 0, |
| 5577 | NULL); |
| 5578 | |
| 5579 | /* deprecated. */ |
| 5580 | grn_proc_create(ctx, |
| 5581 | "geo_distance3", |
| 5582 | -1, |
| 5583 | GRN_PROC_FUNCTION, |
| 5584 | func_geo_distance3, |
| 5585 | NULL, |
| 5586 | NULL, |
| 5587 | 0, |
| 5588 | NULL); |
| 5589 | |
| 5590 | grn_proc_init_edit_distance(ctx); |
| 5591 | |
| 5592 | { |
| 5593 | grn_obj *selector_proc; |
| 5594 | |
| 5595 | selector_proc = grn_proc_create(ctx, |
| 5596 | "all_records", |
| 5597 | -1, |
| 5598 | GRN_PROC_FUNCTION, |
| 5599 | func_all_records, |
| 5600 | NULL, |
| 5601 | NULL, |
| 5602 | 0, |
| 5603 | NULL); |
| 5604 | grn_proc_set_selector(ctx, selector_proc, selector_all_records); |
| 5605 | grn_proc_set_selector_operator(ctx, selector_proc, GRN_OP_NOP); |
| 5606 | } |
| 5607 | |
| 5608 | /* experimental */ |
| 5609 | grn_proc_init_snippet_html(ctx); |
| 5610 | |
| 5611 | grn_proc_init_query(ctx); |
| 5612 | |
| 5613 | { |
| 5614 | grn_obj *selector_proc; |
| 5615 | |
| 5616 | selector_proc = grn_proc_create(ctx, |
| 5617 | "sub_filter", |
| 5618 | -1, |
| 5619 | GRN_PROC_FUNCTION, |
| 5620 | NULL, |
| 5621 | NULL, |
| 5622 | NULL, |
| 5623 | 0, |
| 5624 | NULL); |
| 5625 | grn_proc_set_selector(ctx, selector_proc, selector_sub_filter); |
| 5626 | grn_proc_set_selector_operator(ctx, selector_proc, GRN_OP_NOP); |
| 5627 | } |
| 5628 | |
| 5629 | grn_proc_create(ctx, |
| 5630 | "html_untag", |
| 5631 | -1, |
| 5632 | GRN_PROC_FUNCTION, |
| 5633 | func_html_untag, |
| 5634 | NULL, |
| 5635 | NULL, |
| 5636 | 0, |
| 5637 | NULL); |
| 5638 | |
| 5639 | { |
| 5640 | grn_obj *selector_proc; |
| 5641 | |
| 5642 | selector_proc = grn_proc_create(ctx, |
| 5643 | "between", |
| 5644 | -1, |
| 5645 | GRN_PROC_FUNCTION, |
| 5646 | func_between, |
| 5647 | NULL, |
| 5648 | NULL, |
| 5649 | 0, |
| 5650 | NULL); |
| 5651 | grn_proc_set_selector(ctx, selector_proc, selector_between); |
| 5652 | grn_proc_set_selector_operator(ctx, selector_proc, GRN_OP_LESS); |
| 5653 | } |
| 5654 | |
| 5655 | grn_proc_init_highlight_html(ctx); |
| 5656 | grn_proc_init_highlight_full(ctx); |
| 5657 | |
| 5658 | { |
| 5659 | grn_obj *selector_proc; |
| 5660 | |
| 5661 | selector_proc = grn_proc_create(ctx, |
| 5662 | "in_values", |
| 5663 | -1, |
| 5664 | GRN_PROC_FUNCTION, |
| 5665 | func_in_values, |
| 5666 | NULL, |
| 5667 | NULL, |
| 5668 | 0, |
| 5669 | NULL); |
| 5670 | grn_proc_set_selector(ctx, selector_proc, selector_in_values); |
| 5671 | grn_proc_set_selector_operator(ctx, selector_proc, GRN_OP_EQUAL); |
| 5672 | } |
| 5673 | |
| 5674 | DEF_VAR(vars[0], "table"); |
| 5675 | DEF_VAR(vars[1], "column"); |
| 5676 | DEF_VAR(vars[2], "min"); |
| 5677 | DEF_VAR(vars[3], "min_border"); |
| 5678 | DEF_VAR(vars[4], "max"); |
| 5679 | DEF_VAR(vars[5], "max_border"); |
| 5680 | DEF_VAR(vars[6], "offset"); |
| 5681 | DEF_VAR(vars[7], "limit"); |
| 5682 | DEF_VAR(vars[8], "filter"); |
| 5683 | DEF_VAR(vars[9], "output_columns"); |
| 5684 | DEF_COMMAND("range_filter", proc_range_filter, 10, vars); |
| 5685 | |
| 5686 | DEF_VAR(vars[0], "id"); |
| 5687 | DEF_COMMAND("request_cancel", proc_request_cancel, 1, vars); |
| 5688 | |
| 5689 | DEF_VAR(vars[0], "name"); |
| 5690 | DEF_COMMAND("plugin_register", proc_plugin_register, 1, vars); |
| 5691 | |
| 5692 | DEF_VAR(vars[0], "name"); |
| 5693 | DEF_COMMAND("plugin_unregister", proc_plugin_unregister, 1, vars); |
| 5694 | |
| 5695 | DEF_VAR(vars[0], "target_name"); |
| 5696 | DEF_VAR(vars[1], "recursive"); |
| 5697 | DEF_VAR(vars[2], "only_opened"); |
| 5698 | DEF_COMMAND("io_flush", proc_io_flush, 3, vars); |
| 5699 | |
| 5700 | grn_proc_init_object_exist(ctx); |
| 5701 | |
| 5702 | grn_proc_init_thread_limit(ctx); |
| 5703 | |
| 5704 | DEF_COMMAND("database_unmap", proc_database_unmap, 0, vars); |
| 5705 | |
| 5706 | grn_proc_init_column_copy(ctx); |
| 5707 | |
| 5708 | grn_proc_init_schema(ctx); |
| 5709 | |
| 5710 | DEF_VAR(vars[0], "target_name"); |
| 5711 | DEF_COMMAND("reindex", proc_reindex, 1, vars); |
| 5712 | |
| 5713 | { |
| 5714 | grn_obj *selector_proc; |
| 5715 | |
| 5716 | selector_proc = grn_proc_create(ctx, |
| 5717 | "prefix_rk_search", |
| 5718 | -1, |
| 5719 | GRN_PROC_FUNCTION, |
| 5720 | NULL, |
| 5721 | NULL, |
| 5722 | NULL, |
| 5723 | 0, |
| 5724 | NULL); |
| 5725 | grn_proc_set_selector(ctx, selector_proc, selector_prefix_rk_search); |
| 5726 | grn_proc_set_selector_operator(ctx, selector_proc, GRN_OP_PREFIX); |
| 5727 | } |
| 5728 | |
| 5729 | grn_proc_init_config_get(ctx); |
| 5730 | grn_proc_init_config_set(ctx); |
| 5731 | grn_proc_init_config_delete(ctx); |
| 5732 | |
| 5733 | grn_proc_init_lock_acquire(ctx); |
| 5734 | grn_proc_init_lock_release(ctx); |
| 5735 | |
| 5736 | grn_proc_init_object_inspect(ctx); |
| 5737 | |
| 5738 | grn_proc_init_fuzzy_search(ctx); |
| 5739 | |
| 5740 | grn_proc_init_object_remove(ctx); |
| 5741 | |
| 5742 | grn_proc_init_snippet(ctx); |
| 5743 | grn_proc_init_highlight(ctx); |
| 5744 | |
| 5745 | grn_proc_init_query_expand(ctx); |
| 5746 | |
| 5747 | grn_proc_init_object_list(ctx); |
| 5748 | |
| 5749 | grn_proc_init_table_copy(ctx); |
| 5750 | |
| 5751 | grn_proc_init_in_records(ctx); |
| 5752 | |
| 5753 | grn_proc_init_query_log_flags_get(ctx); |
| 5754 | grn_proc_init_query_log_flags_set(ctx); |
| 5755 | grn_proc_init_query_log_flags_add(ctx); |
| 5756 | grn_proc_init_query_log_flags_remove(ctx); |
| 5757 | |
| 5758 | grn_proc_init_cast_loose(ctx); |
| 5759 | |
| 5760 | grn_proc_init_index_column_diff(ctx); |
| 5761 | |
| 5762 | grn_proc_init_object_set_visibility(ctx); |
| 5763 | |
| 5764 | grn_proc_init_reference_acquire(ctx); |
| 5765 | grn_proc_init_reference_release(ctx); |
| 5766 | |
| 5767 | grn_proc_init_query_parallel_or(ctx); |
| 5768 | |
| 5769 | grn_proc_init_object_warm(ctx); |
| 5770 | |
| 5771 | grn_proc_init_table_create_similar(ctx); |
| 5772 | |
| 5773 | grn_proc_init_column_create_similar(ctx); |
| 5774 | |
| 5775 | grn_proc_init_thread_dump(ctx); |
| 5776 | |
| 5777 | grn_proc_init_escalate(ctx); |
| 5778 | |
| 5779 | grn_proc_init_sleep(ctx); |
| 5780 | |
| 5781 | grn_proc_init_distance_cosine(ctx); |
| 5782 | grn_proc_init_distance_inner_product(ctx); |
| 5783 | grn_proc_init_distance_l1_norm(ctx); |
| 5784 | grn_proc_init_distance_l2_norm_squared(ctx); |
| 5785 | |
| 5786 | grn_proc_init_command_list(ctx); |
| 5787 | |
| 5788 | grn_proc_init_extract(ctx); |
| 5789 | |
| 5790 | grn_proc_init_json_extract(ctx); |
| 5791 | } |