| 1 | /* |
| 2 | Copyright (C) 2009-2018 Brazil |
| 3 | Copyright (C) 2018-2025 Sutou Kouhei <[email protected]> |
| 4 | |
| 5 | This library is free software; you can redistribute it and/or |
| 6 | modify it under the terms of the GNU Lesser General Public |
| 7 | License as published by the Free Software Foundation; either |
| 8 | version 2.1 of the License, or (at your option) any later version. |
| 9 | |
| 10 | This library is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | Lesser General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU Lesser General Public |
| 16 | License along with this library; if not, write to the Free Software |
| 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | */ |
| 19 | |
| 20 | #include "grn.h" |
| 21 | |
| 22 | #include "grn_ctx_impl.h" |
| 23 | #include "grn_db.h" |
| 24 | #include "grn_expr.h" |
| 25 | #include "grn_float.h" |
| 26 | #include "grn_output.h" |
| 27 | #include "grn_output_columns.h" |
| 28 | #include "grn_str.h" |
| 29 | #include "grn_table.h" |
| 30 | #include "grn_util.h" |
| 31 | |
| 32 | #include <string.h> |
| 33 | #include <math.h> |
| 34 | |
| 35 | uint32_t grn_output_auto_flush_interval = 1024; |
| 36 | |
| 37 | void |
| 38 | grn_output_init_from_env(void) |
| 39 | { |
| 40 | /* clang-format is broken with this additional block... */ |
| 41 | /* { */ |
| 42 | char grn_output_auto_flush_interval_env[GRN_ENV_BUFFER_SIZE]; |
| 43 | grn_getenv("GRN_OUTPUT_AUTO_FLUSH_INTERVAL", |
| 44 | grn_output_auto_flush_interval_env, |
| 45 | GRN_ENV_BUFFER_SIZE); |
| 46 | if (grn_output_auto_flush_interval_env[0]) { |
| 47 | size_t env_len = strlen(grn_output_auto_flush_interval_env); |
| 48 | uint32_t interval = grn_atoui(grn_output_auto_flush_interval_env, |
| 49 | grn_output_auto_flush_interval_env + env_len, |
| 50 | NULL); |
| 51 | if (interval > 0) { |
| 52 | grn_output_auto_flush_interval = interval; |
| 53 | } |
| 54 | } |
| 55 | /* } */ |
| 56 | } |
| 57 | |
| 58 | #define LEVELS (&ctx->impl->output.levels) |
| 59 | #define DEPTH (GRN_BULK_VSIZE(LEVELS) >> 2) |
| 60 | #define CURR_LEVEL (DEPTH ? (GRN_UINT32_VALUE_AT(LEVELS, (DEPTH - 1))) : 0) |
| 61 | #define INCR_DEPTH(i) GRN_UINT32_PUT(ctx, LEVELS, i) |
| 62 | #define DECR_DEPTH \ |
| 63 | (DEPTH ? grn_bulk_truncate(ctx, \ |
| 64 | LEVELS, \ |
| 65 | GRN_BULK_VSIZE(LEVELS) - sizeof(uint32_t)) \ |
| 66 | : 0) |
| 67 | #define INCR_LENGTH \ |
| 68 | (DEPTH ? (GRN_UINT32_VALUE_AT(LEVELS, (DEPTH - 1)) += 2) : 0) |
| 69 | |
| 70 | grn_rc |
| 71 | grn_output_range_normalize(grn_ctx *ctx, int size, int *offset, int *limit) |
| 72 | { |
| 73 | int end; |
| 74 | int normalized_offset = *offset; |
| 75 | int normalized_limit = *limit; |
| 76 | |
| 77 | if (normalized_limit < 0) { |
| 78 | normalized_limit += size + 1; |
| 79 | if (normalized_limit < 0) { |
| 80 | *offset = 0; |
| 81 | *limit = 0; |
| 82 | return GRN_TOO_SMALL_LIMIT; |
| 83 | } |
| 84 | } else if (normalized_limit > size) { |
| 85 | normalized_limit = size; |
| 86 | } |
| 87 | |
| 88 | if (normalized_offset < 0) { |
| 89 | normalized_offset += size; |
| 90 | if (normalized_offset < 0) { |
| 91 | if (normalized_limit + normalized_offset >= 0) { |
| 92 | normalized_limit += normalized_offset; |
| 93 | normalized_offset = 0; |
| 94 | } else { |
| 95 | *offset = 0; |
| 96 | *limit = 0; |
| 97 | return GRN_TOO_SMALL_OFFSET; |
| 98 | } |
| 99 | } |
| 100 | } else if (normalized_offset != 0 && normalized_offset >= size) { |
| 101 | *offset = 0; |
| 102 | *limit = 0; |
| 103 | return GRN_TOO_LARGE_OFFSET; |
| 104 | } |
| 105 | |
| 106 | /* At this point, offset and limit must be zero or positive. */ |
| 107 | end = normalized_offset + normalized_limit; |
| 108 | if (end > size) { |
| 109 | normalized_limit -= end - size; |
| 110 | } |
| 111 | *offset = normalized_offset; |
| 112 | *limit = normalized_limit; |
| 113 | return GRN_SUCCESS; |
| 114 | } |
| 115 | |
| 116 | static void |
| 117 | indent(grn_ctx *ctx, grn_obj *outbuf, size_t level) |
| 118 | { |
| 119 | size_t i; |
| 120 | for (i = 0; i < level; i++) { |
| 121 | GRN_TEXT_PUTS(ctx, outbuf, " "); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | static void |
| 126 | json_array_open(grn_ctx *ctx, grn_obj *outbuf, size_t *indent_level) |
| 127 | { |
| 128 | GRN_TEXT_PUTC(ctx, outbuf, '['); |
| 129 | if (ctx->impl->output.is_pretty) { |
| 130 | GRN_TEXT_PUTC(ctx, outbuf, '\n'); |
| 131 | (*indent_level)++; |
| 132 | indent(ctx, outbuf, *indent_level); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | static void |
| 137 | json_array_close(grn_ctx *ctx, grn_obj *outbuf, size_t *indent_level) |
| 138 | { |
| 139 | if (ctx->impl->output.is_pretty) { |
| 140 | GRN_TEXT_PUTC(ctx, outbuf, '\n'); |
| 141 | (*indent_level)--; |
| 142 | indent(ctx, outbuf, *indent_level); |
| 143 | } |
| 144 | GRN_TEXT_PUTC(ctx, outbuf, ']'); |
| 145 | } |
| 146 | |
| 147 | static void |
| 148 | json_element_end(grn_ctx *ctx, grn_obj *outbuf, size_t indent_level) |
| 149 | { |
| 150 | GRN_TEXT_PUTC(ctx, outbuf, ','); |
| 151 | if (ctx->impl->output.is_pretty) { |
| 152 | GRN_TEXT_PUTC(ctx, outbuf, '\n'); |
| 153 | indent(ctx, outbuf, indent_level); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | static void |
| 158 | json_map_open(grn_ctx *ctx, grn_obj *outbuf, size_t *indent_level) |
| 159 | { |
| 160 | GRN_TEXT_PUTC(ctx, outbuf, '{'); |
| 161 | if (ctx->impl->output.is_pretty) { |
| 162 | GRN_TEXT_PUTC(ctx, outbuf, '\n'); |
| 163 | (*indent_level)++; |
| 164 | indent(ctx, outbuf, *indent_level); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | static void |
| 169 | json_map_close(grn_ctx *ctx, grn_obj *outbuf, size_t *indent_level) |
| 170 | { |
| 171 | if (ctx->impl->output.is_pretty) { |
| 172 | GRN_TEXT_PUTC(ctx, outbuf, '\n'); |
| 173 | (*indent_level)--; |
| 174 | indent(ctx, outbuf, *indent_level); |
| 175 | } |
| 176 | GRN_TEXT_PUTC(ctx, outbuf, '}'); |
| 177 | } |
| 178 | |
| 179 | static void |
| 180 | json_key_end(grn_ctx *ctx, grn_obj *outbuf) |
| 181 | { |
| 182 | GRN_TEXT_PUTC(ctx, outbuf, ':'); |
| 183 | if (ctx->impl->output.is_pretty) { |
| 184 | GRN_TEXT_PUTC(ctx, outbuf, ' '); |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | static void |
| 189 | json_key(grn_ctx *ctx, grn_obj *outbuf, const char *key) |
| 190 | { |
| 191 | grn_text_esc(ctx, outbuf, key, strlen(key)); |
| 192 | json_key_end(ctx, outbuf); |
| 193 | } |
| 194 | |
| 195 | static void |
| 196 | json_value_end(grn_ctx *ctx, grn_obj *outbuf, size_t indent_level) |
| 197 | { |
| 198 | GRN_TEXT_PUTC(ctx, outbuf, ','); |
| 199 | if (ctx->impl->output.is_pretty) { |
| 200 | GRN_TEXT_PUTC(ctx, outbuf, '\n'); |
| 201 | indent(ctx, outbuf, indent_level); |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | static void |
| 206 | put_delimiter(grn_ctx *ctx, grn_obj *outbuf, grn_content_type output_type) |
| 207 | { |
| 208 | uint32_t level = CURR_LEVEL; |
| 209 | switch (output_type) { |
| 210 | case GRN_CONTENT_JSON: |
| 211 | if (level < 2) { |
| 212 | if (DEPTH > 0 && ctx->impl->output.is_pretty) { |
| 213 | GRN_TEXT_PUTC(ctx, outbuf, '\n'); |
| 214 | indent(ctx, outbuf, DEPTH + 1); |
| 215 | } |
| 216 | return; |
| 217 | } |
| 218 | if ((level & 3) == 3) { |
| 219 | GRN_TEXT_PUTC(ctx, outbuf, ':'); |
| 220 | if (ctx->impl->output.is_pretty) { |
| 221 | GRN_TEXT_PUTC(ctx, outbuf, ' '); |
| 222 | } |
| 223 | } else { |
| 224 | json_element_end(ctx, outbuf, DEPTH + 1); |
| 225 | } |
| 226 | // if (DEPTH == 1 && ((level & 3) != 3)) { GRN_TEXT_PUTC(ctx, outbuf, '\n'); |
| 227 | // } |
| 228 | break; |
| 229 | case GRN_CONTENT_XML: |
| 230 | if (!DEPTH) { |
| 231 | return; |
| 232 | } |
| 233 | GRN_TEXT_PUTC(ctx, outbuf, '\n'); |
| 234 | break; |
| 235 | case GRN_CONTENT_TSV: |
| 236 | if (level < 2) { |
| 237 | return; |
| 238 | } |
| 239 | if (DEPTH <= 2) { |
| 240 | GRN_TEXT_PUTC(ctx, outbuf, ((level & 3) == 3) ? '\t' : '\n'); |
| 241 | } else { |
| 242 | GRN_TEXT_PUTC(ctx, outbuf, '\t'); |
| 243 | } |
| 244 | case GRN_CONTENT_MSGPACK: |
| 245 | // do nothing |
| 246 | break; |
| 247 | case GRN_CONTENT_GROONGA_COMMAND_LIST: |
| 248 | break; |
| 249 | case GRN_CONTENT_APACHE_ARROW: |
| 250 | break; |
| 251 | case GRN_CONTENT_NONE: |
| 252 | break; |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | void |
| 257 | grn_output_array_open(grn_ctx *ctx, |
| 258 | grn_obj *outbuf, |
| 259 | grn_content_type output_type, |
| 260 | const char *name, |
| 261 | int nelements) |
| 262 | { |
| 263 | put_delimiter(ctx, outbuf, output_type); |
| 264 | switch (output_type) { |
| 265 | case GRN_CONTENT_JSON: |
| 266 | GRN_TEXT_PUTC(ctx, outbuf, '['); |
| 267 | break; |
| 268 | case GRN_CONTENT_XML: |
| 269 | GRN_TEXT_PUTC(ctx, outbuf, '<'); |
| 270 | GRN_TEXT_PUTS(ctx, outbuf, name); |
| 271 | GRN_TEXT_PUTC(ctx, outbuf, '>'); |
| 272 | grn_vector_add_element(ctx, |
| 273 | &ctx->impl->output.names, |
| 274 | name, |
| 275 | (uint32_t)strlen(name), |
| 276 | 0, |
| 277 | GRN_DB_SHORT_TEXT); |
| 278 | break; |
| 279 | case GRN_CONTENT_TSV: |
| 280 | if (DEPTH > 2) { |
| 281 | GRN_TEXT_PUTS(ctx, outbuf, "[\t"); |
| 282 | } |
| 283 | break; |
| 284 | case GRN_CONTENT_MSGPACK: |
| 285 | #ifdef GRN_WITH_MESSAGE_PACK |
| 286 | if (nelements < 0) { |
| 287 | GRN_LOG(ctx, |
| 288 | GRN_LOG_DEBUG, |
| 289 | "grn_output_array_open nelements (%d) for <%s>", |
| 290 | nelements, |
| 291 | name); |
| 292 | } |
| 293 | msgpack_pack_array(&ctx->impl->output.msgpacker, (size_t)nelements); |
| 294 | #endif |
| 295 | break; |
| 296 | case GRN_CONTENT_GROONGA_COMMAND_LIST: |
| 297 | break; |
| 298 | case GRN_CONTENT_APACHE_ARROW: |
| 299 | break; |
| 300 | case GRN_CONTENT_NONE: |
| 301 | break; |
| 302 | } |
| 303 | INCR_DEPTH(0); |
| 304 | } |
| 305 | |
| 306 | void |
| 307 | grn_output_array_close(grn_ctx *ctx, |
| 308 | grn_obj *outbuf, |
| 309 | grn_content_type output_type) |
| 310 | { |
| 311 | switch (output_type) { |
| 312 | case GRN_CONTENT_JSON: |
| 313 | if (ctx->impl->output.is_pretty) { |
| 314 | GRN_TEXT_PUTC(ctx, outbuf, '\n'); |
| 315 | indent(ctx, outbuf, DEPTH); |
| 316 | } |
| 317 | GRN_TEXT_PUTC(ctx, outbuf, ']'); |
| 318 | break; |
| 319 | case GRN_CONTENT_TSV: |
| 320 | if (DEPTH > 3) { |
| 321 | if (CURR_LEVEL >= 2) { |
| 322 | GRN_TEXT_PUTC(ctx, outbuf, '\t'); |
| 323 | } |
| 324 | GRN_TEXT_PUTC(ctx, outbuf, ']'); |
| 325 | } |
| 326 | break; |
| 327 | case GRN_CONTENT_XML: |
| 328 | { |
| 329 | const char *name; |
| 330 | unsigned int name_len; |
| 331 | name_len = grn_vector_pop_element(ctx, |
| 332 | &ctx->impl->output.names, |
| 333 | &name, |
| 334 | NULL, |
| 335 | NULL); |
| 336 | GRN_TEXT_PUTS(ctx, outbuf, "</"); |
| 337 | GRN_TEXT_PUT(ctx, outbuf, name, name_len); |
| 338 | GRN_TEXT_PUTC(ctx, outbuf, '>'); |
| 339 | } |
| 340 | break; |
| 341 | case GRN_CONTENT_MSGPACK: |
| 342 | // do nothing |
| 343 | break; |
| 344 | case GRN_CONTENT_GROONGA_COMMAND_LIST: |
| 345 | break; |
| 346 | case GRN_CONTENT_APACHE_ARROW: |
| 347 | break; |
| 348 | case GRN_CONTENT_NONE: |
| 349 | break; |
| 350 | } |
| 351 | DECR_DEPTH; |
| 352 | INCR_LENGTH; |
| 353 | } |
| 354 | |
| 355 | void |
| 356 | grn_output_map_open(grn_ctx *ctx, |
| 357 | grn_obj *outbuf, |
| 358 | grn_content_type output_type, |
| 359 | const char *name, |
| 360 | int nelements) |
| 361 | { |
| 362 | put_delimiter(ctx, outbuf, output_type); |
| 363 | switch (output_type) { |
| 364 | case GRN_CONTENT_JSON: |
| 365 | GRN_TEXT_PUTS(ctx, outbuf, "{"); |
| 366 | break; |
| 367 | case GRN_CONTENT_XML: |
| 368 | GRN_TEXT_PUTC(ctx, outbuf, '<'); |
| 369 | GRN_TEXT_PUTS(ctx, outbuf, name); |
| 370 | GRN_TEXT_PUTC(ctx, outbuf, '>'); |
| 371 | grn_vector_add_element(ctx, |
| 372 | &ctx->impl->output.names, |
| 373 | name, |
| 374 | (uint32_t)strlen(name), |
| 375 | 0, |
| 376 | GRN_DB_SHORT_TEXT); |
| 377 | break; |
| 378 | case GRN_CONTENT_TSV: |
| 379 | if (DEPTH > 2) { |
| 380 | GRN_TEXT_PUTS(ctx, outbuf, "{\t"); |
| 381 | } |
| 382 | break; |
| 383 | case GRN_CONTENT_MSGPACK: |
| 384 | #ifdef GRN_WITH_MESSAGE_PACK |
| 385 | if (nelements < 0) { |
| 386 | GRN_LOG(ctx, |
| 387 | GRN_LOG_DEBUG, |
| 388 | "grn_output_map_open nelements (%d) for <%s>", |
| 389 | nelements, |
| 390 | name); |
| 391 | } |
| 392 | msgpack_pack_map(&ctx->impl->output.msgpacker, (size_t)nelements); |
| 393 | #endif |
| 394 | break; |
| 395 | case GRN_CONTENT_GROONGA_COMMAND_LIST: |
| 396 | break; |
| 397 | case GRN_CONTENT_APACHE_ARROW: |
| 398 | break; |
| 399 | case GRN_CONTENT_NONE: |
| 400 | break; |
| 401 | } |
| 402 | INCR_DEPTH(1); |
| 403 | } |
| 404 | |
| 405 | void |
| 406 | grn_output_map_close(grn_ctx *ctx, |
| 407 | grn_obj *outbuf, |
| 408 | grn_content_type output_type) |
| 409 | { |
| 410 | switch (output_type) { |
| 411 | case GRN_CONTENT_JSON: |
| 412 | if (ctx->impl->output.is_pretty) { |
| 413 | GRN_TEXT_PUTC(ctx, outbuf, '\n'); |
| 414 | indent(ctx, outbuf, DEPTH); |
| 415 | } |
| 416 | GRN_TEXT_PUTS(ctx, outbuf, "}"); |
| 417 | break; |
| 418 | case GRN_CONTENT_TSV: |
| 419 | if (DEPTH > 3) { |
| 420 | if (CURR_LEVEL >= 2) { |
| 421 | GRN_TEXT_PUTC(ctx, outbuf, '\t'); |
| 422 | } |
| 423 | GRN_TEXT_PUTC(ctx, outbuf, '}'); |
| 424 | } |
| 425 | break; |
| 426 | case GRN_CONTENT_XML: |
| 427 | { |
| 428 | const char *name; |
| 429 | unsigned int name_len; |
| 430 | name_len = grn_vector_pop_element(ctx, |
| 431 | &ctx->impl->output.names, |
| 432 | &name, |
| 433 | NULL, |
| 434 | NULL); |
| 435 | GRN_TEXT_PUTS(ctx, outbuf, "</"); |
| 436 | GRN_TEXT_PUT(ctx, outbuf, name, name_len); |
| 437 | GRN_TEXT_PUTC(ctx, outbuf, '>'); |
| 438 | } |
| 439 | break; |
| 440 | case GRN_CONTENT_MSGPACK: |
| 441 | // do nothing |
| 442 | break; |
| 443 | case GRN_CONTENT_GROONGA_COMMAND_LIST: |
| 444 | break; |
| 445 | case GRN_CONTENT_APACHE_ARROW: |
| 446 | break; |
| 447 | case GRN_CONTENT_NONE: |
| 448 | break; |
| 449 | } |
| 450 | DECR_DEPTH; |
| 451 | INCR_LENGTH; |
| 452 | } |
| 453 | |
| 454 | static void |
| 455 | grn_output_int8(grn_ctx *ctx, |
| 456 | grn_obj *outbuf, |
| 457 | grn_content_type output_type, |
| 458 | int8_t value) |
| 459 | { |
| 460 | put_delimiter(ctx, outbuf, output_type); |
| 461 | switch (output_type) { |
| 462 | case GRN_CONTENT_JSON: |
| 463 | grn_text_itoa(ctx, outbuf, value); |
| 464 | break; |
| 465 | case GRN_CONTENT_TSV: |
| 466 | grn_text_itoa(ctx, outbuf, value); |
| 467 | break; |
| 468 | case GRN_CONTENT_XML: |
| 469 | GRN_TEXT_PUTS(ctx, outbuf, "<INT>"); |
| 470 | grn_text_itoa(ctx, outbuf, value); |
| 471 | GRN_TEXT_PUTS(ctx, outbuf, "</INT>"); |
| 472 | break; |
| 473 | case GRN_CONTENT_MSGPACK: |
| 474 | #ifdef GRN_WITH_MESSAGE_PACK |
| 475 | msgpack_pack_int8(&ctx->impl->output.msgpacker, value); |
| 476 | #endif |
| 477 | break; |
| 478 | case GRN_CONTENT_GROONGA_COMMAND_LIST: |
| 479 | grn_text_itoa(ctx, outbuf, value); |
| 480 | break; |
| 481 | case GRN_CONTENT_APACHE_ARROW: |
| 482 | if (ctx->impl->output.arrow_stream_writer) { |
| 483 | grn_arrow_stream_writer_add_column_int8( |
| 484 | ctx, |
| 485 | ctx->impl->output.arrow_stream_writer, |
| 486 | value); |
| 487 | } |
| 488 | break; |
| 489 | case GRN_CONTENT_NONE: |
| 490 | break; |
| 491 | } |
| 492 | INCR_LENGTH; |
| 493 | } |
| 494 | |
| 495 | static void |
| 496 | grn_output_uint8(grn_ctx *ctx, |
| 497 | grn_obj *outbuf, |
| 498 | grn_content_type output_type, |
| 499 | uint8_t value) |
| 500 | { |
| 501 | /* TODO */ |
| 502 | grn_output_uint32(ctx, outbuf, output_type, value); |
| 503 | } |
| 504 | |
| 505 | static void |
| 506 | grn_output_int16(grn_ctx *ctx, |
| 507 | grn_obj *outbuf, |
| 508 | grn_content_type output_type, |
| 509 | int16_t value) |
| 510 | { |
| 511 | /* TODO */ |
| 512 | grn_output_int32(ctx, outbuf, output_type, value); |
| 513 | } |
| 514 | |
| 515 | static void |
| 516 | grn_output_uint16(grn_ctx *ctx, |
| 517 | grn_obj *outbuf, |
| 518 | grn_content_type output_type, |
| 519 | uint16_t value) |
| 520 | { |
| 521 | /* TODO */ |
| 522 | grn_output_uint32(ctx, outbuf, output_type, value); |
| 523 | } |
| 524 | |
| 525 | void |
| 526 | grn_output_int32(grn_ctx *ctx, |
| 527 | grn_obj *outbuf, |
| 528 | grn_content_type output_type, |
| 529 | int32_t value) |
| 530 | { |
| 531 | put_delimiter(ctx, outbuf, output_type); |
| 532 | switch (output_type) { |
| 533 | case GRN_CONTENT_JSON: |
| 534 | grn_text_itoa(ctx, outbuf, value); |
| 535 | break; |
| 536 | case GRN_CONTENT_TSV: |
| 537 | grn_text_itoa(ctx, outbuf, value); |
| 538 | break; |
| 539 | case GRN_CONTENT_XML: |
| 540 | GRN_TEXT_PUTS(ctx, outbuf, "<INT>"); |
| 541 | grn_text_itoa(ctx, outbuf, value); |
| 542 | GRN_TEXT_PUTS(ctx, outbuf, "</INT>"); |
| 543 | break; |
| 544 | case GRN_CONTENT_MSGPACK: |
| 545 | #ifdef GRN_WITH_MESSAGE_PACK |
| 546 | msgpack_pack_int32(&ctx->impl->output.msgpacker, value); |
| 547 | #endif |
| 548 | break; |
| 549 | case GRN_CONTENT_GROONGA_COMMAND_LIST: |
| 550 | grn_text_itoa(ctx, outbuf, value); |
| 551 | break; |
| 552 | case GRN_CONTENT_APACHE_ARROW: |
| 553 | if (ctx->impl->output.arrow_stream_writer) { |
| 554 | grn_arrow_stream_writer_add_column_int32( |
| 555 | ctx, |
| 556 | ctx->impl->output.arrow_stream_writer, |
| 557 | value); |
| 558 | } |
| 559 | break; |
| 560 | case GRN_CONTENT_NONE: |
| 561 | break; |
| 562 | } |
| 563 | INCR_LENGTH; |
| 564 | } |
| 565 | |
| 566 | void |
| 567 | grn_output_uint32(grn_ctx *ctx, |
| 568 | grn_obj *outbuf, |
| 569 | grn_content_type output_type, |
| 570 | uint32_t value) |
| 571 | { |
| 572 | put_delimiter(ctx, outbuf, output_type); |
| 573 | switch (output_type) { |
| 574 | case GRN_CONTENT_JSON: |
| 575 | grn_text_ulltoa(ctx, outbuf, value); |
| 576 | break; |
| 577 | case GRN_CONTENT_TSV: |
| 578 | grn_text_ulltoa(ctx, outbuf, value); |
| 579 | break; |
| 580 | case GRN_CONTENT_XML: |
| 581 | GRN_TEXT_PUTS(ctx, outbuf, "<INT>"); |
| 582 | grn_text_ulltoa(ctx, outbuf, value); |
| 583 | GRN_TEXT_PUTS(ctx, outbuf, "</INT>"); |
| 584 | break; |
| 585 | case GRN_CONTENT_MSGPACK: |
| 586 | #ifdef GRN_WITH_MESSAGE_PACK |
| 587 | msgpack_pack_uint32(&ctx->impl->output.msgpacker, value); |
| 588 | #endif |
| 589 | break; |
| 590 | case GRN_CONTENT_GROONGA_COMMAND_LIST: |
| 591 | grn_text_ulltoa(ctx, outbuf, value); |
| 592 | break; |
| 593 | case GRN_CONTENT_APACHE_ARROW: |
| 594 | if (ctx->impl->output.arrow_stream_writer) { |
| 595 | grn_arrow_stream_writer_add_column_uint32( |
| 596 | ctx, |
| 597 | ctx->impl->output.arrow_stream_writer, |
| 598 | value); |
| 599 | } |
| 600 | break; |
| 601 | case GRN_CONTENT_NONE: |
| 602 | break; |
| 603 | } |
| 604 | INCR_LENGTH; |
| 605 | } |
| 606 | |
| 607 | void |
| 608 | grn_output_int64(grn_ctx *ctx, |
| 609 | grn_obj *outbuf, |
| 610 | grn_content_type output_type, |
| 611 | int64_t value) |
| 612 | { |
| 613 | put_delimiter(ctx, outbuf, output_type); |
| 614 | switch (output_type) { |
| 615 | case GRN_CONTENT_JSON: |
| 616 | grn_text_lltoa(ctx, outbuf, value); |
| 617 | break; |
| 618 | case GRN_CONTENT_TSV: |
| 619 | grn_text_lltoa(ctx, outbuf, value); |
| 620 | break; |
| 621 | case GRN_CONTENT_XML: |
| 622 | GRN_TEXT_PUTS(ctx, outbuf, "<INT>"); |
| 623 | grn_text_lltoa(ctx, outbuf, value); |
| 624 | GRN_TEXT_PUTS(ctx, outbuf, "</INT>"); |
| 625 | break; |
| 626 | case GRN_CONTENT_MSGPACK: |
| 627 | #ifdef GRN_WITH_MESSAGE_PACK |
| 628 | msgpack_pack_int64(&ctx->impl->output.msgpacker, value); |
| 629 | #endif |
| 630 | break; |
| 631 | case GRN_CONTENT_GROONGA_COMMAND_LIST: |
| 632 | grn_text_lltoa(ctx, outbuf, value); |
| 633 | break; |
| 634 | case GRN_CONTENT_APACHE_ARROW: |
| 635 | if (ctx->impl->output.arrow_stream_writer) { |
| 636 | grn_arrow_stream_writer_add_column_int64( |
| 637 | ctx, |
| 638 | ctx->impl->output.arrow_stream_writer, |
| 639 | value); |
| 640 | } |
| 641 | break; |
| 642 | case GRN_CONTENT_NONE: |
| 643 | break; |
| 644 | } |
| 645 | INCR_LENGTH; |
| 646 | } |
| 647 | |
| 648 | void |
| 649 | grn_output_uint64(grn_ctx *ctx, |
| 650 | grn_obj *outbuf, |
| 651 | grn_content_type output_type, |
| 652 | uint64_t value) |
| 653 | { |
| 654 | put_delimiter(ctx, outbuf, output_type); |
| 655 | switch (output_type) { |
| 656 | case GRN_CONTENT_JSON: |
| 657 | grn_text_ulltoa(ctx, outbuf, value); |
| 658 | break; |
| 659 | case GRN_CONTENT_TSV: |
| 660 | grn_text_ulltoa(ctx, outbuf, value); |
| 661 | break; |
| 662 | case GRN_CONTENT_XML: |
| 663 | GRN_TEXT_PUTS(ctx, outbuf, "<INT>"); |
| 664 | grn_text_ulltoa(ctx, outbuf, value); |
| 665 | GRN_TEXT_PUTS(ctx, outbuf, "</INT>"); |
| 666 | break; |
| 667 | case GRN_CONTENT_MSGPACK: |
| 668 | #ifdef GRN_WITH_MESSAGE_PACK |
| 669 | msgpack_pack_uint64(&ctx->impl->output.msgpacker, value); |
| 670 | #endif |
| 671 | break; |
| 672 | case GRN_CONTENT_GROONGA_COMMAND_LIST: |
| 673 | grn_text_ulltoa(ctx, outbuf, value); |
| 674 | break; |
| 675 | case GRN_CONTENT_APACHE_ARROW: |
| 676 | if (ctx->impl->output.arrow_stream_writer) { |
| 677 | grn_arrow_stream_writer_add_column_uint64( |
| 678 | ctx, |
| 679 | ctx->impl->output.arrow_stream_writer, |
| 680 | value); |
| 681 | } |
| 682 | break; |
| 683 | case GRN_CONTENT_NONE: |
| 684 | break; |
| 685 | } |
| 686 | INCR_LENGTH; |
| 687 | } |
| 688 | |
| 689 | #ifdef GRN_HAVE_BFLOAT16 |
| 690 | void |
| 691 | grn_output_bfloat16(grn_ctx *ctx, |
| 692 | grn_obj *outbuf, |
| 693 | grn_content_type output_type, |
| 694 | grn_bfloat16 value) |
| 695 | { |
| 696 | put_delimiter(ctx, outbuf, output_type); |
| 697 | switch (output_type) { |
| 698 | case GRN_CONTENT_JSON: |
| 699 | switch (fpclassify(grn_bfloat16_to_float32(value))) { |
| 700 | case FP_NAN: |
| 701 | case FP_INFINITE: |
| 702 | GRN_TEXT_PUTS(ctx, outbuf, "null"); |
| 703 | break; |
| 704 | default: |
| 705 | grn_text_bf16toa(ctx, outbuf, value); |
| 706 | break; |
| 707 | } |
| 708 | break; |
| 709 | case GRN_CONTENT_TSV: |
| 710 | grn_text_bf16toa(ctx, outbuf, value); |
| 711 | break; |
| 712 | case GRN_CONTENT_XML: |
| 713 | GRN_TEXT_PUTS(ctx, outbuf, "<BFLOAT16>"); |
| 714 | grn_text_bf16toa(ctx, outbuf, value); |
| 715 | GRN_TEXT_PUTS(ctx, outbuf, "</BFLOAT16>"); |
| 716 | break; |
| 717 | case GRN_CONTENT_MSGPACK: |
| 718 | # ifdef GRN_WITH_MESSAGE_PACK |
| 719 | msgpack_pack_float(&ctx->impl->output.msgpacker, |
| 720 | grn_bfloat16_to_float32(value)); |
| 721 | # endif |
| 722 | break; |
| 723 | case GRN_CONTENT_GROONGA_COMMAND_LIST: |
| 724 | grn_text_bf16toa(ctx, outbuf, value); |
| 725 | break; |
| 726 | case GRN_CONTENT_APACHE_ARROW: |
| 727 | if (ctx->impl->output.arrow_stream_writer) { |
| 728 | grn_arrow_stream_writer_add_column_bfloat16( |
| 729 | ctx, |
| 730 | ctx->impl->output.arrow_stream_writer, |
| 731 | value); |
| 732 | } |
| 733 | break; |
| 734 | case GRN_CONTENT_NONE: |
| 735 | break; |
| 736 | } |
| 737 | INCR_LENGTH; |
| 738 | } |
| 739 | #endif |
| 740 | |
| 741 | void |
| 742 | grn_output_float32(grn_ctx *ctx, |
| 743 | grn_obj *outbuf, |
| 744 | grn_content_type output_type, |
| 745 | float value) |
| 746 | { |
| 747 | put_delimiter(ctx, outbuf, output_type); |
| 748 | switch (output_type) { |
| 749 | case GRN_CONTENT_JSON: |
| 750 | switch (fpclassify(value)) { |
| 751 | case FP_NAN: |
| 752 | case FP_INFINITE: |
| 753 | GRN_TEXT_PUTS(ctx, outbuf, "null"); |
| 754 | break; |
| 755 | default: |
| 756 | grn_text_f32toa(ctx, outbuf, value); |
| 757 | break; |
| 758 | } |
| 759 | break; |
| 760 | case GRN_CONTENT_TSV: |
| 761 | grn_text_f32toa(ctx, outbuf, value); |
| 762 | break; |
| 763 | case GRN_CONTENT_XML: |
| 764 | GRN_TEXT_PUTS(ctx, outbuf, "<FLOAT32>"); |
| 765 | grn_text_f32toa(ctx, outbuf, value); |
| 766 | GRN_TEXT_PUTS(ctx, outbuf, "</FLOAT32>"); |
| 767 | break; |
| 768 | case GRN_CONTENT_MSGPACK: |
| 769 | #ifdef GRN_WITH_MESSAGE_PACK |
| 770 | msgpack_pack_float(&ctx->impl->output.msgpacker, value); |
| 771 | #endif |
| 772 | break; |
| 773 | case GRN_CONTENT_GROONGA_COMMAND_LIST: |
| 774 | grn_text_f32toa(ctx, outbuf, value); |
| 775 | break; |
| 776 | case GRN_CONTENT_APACHE_ARROW: |
| 777 | if (ctx->impl->output.arrow_stream_writer) { |
| 778 | grn_arrow_stream_writer_add_column_float32( |
| 779 | ctx, |
| 780 | ctx->impl->output.arrow_stream_writer, |
| 781 | value); |
| 782 | } |
| 783 | break; |
| 784 | case GRN_CONTENT_NONE: |
| 785 | break; |
| 786 | } |
| 787 | INCR_LENGTH; |
| 788 | } |
| 789 | |
| 790 | void |
| 791 | grn_output_float(grn_ctx *ctx, |
| 792 | grn_obj *outbuf, |
| 793 | grn_content_type output_type, |
| 794 | double value) |
| 795 | { |
| 796 | put_delimiter(ctx, outbuf, output_type); |
| 797 | switch (output_type) { |
| 798 | case GRN_CONTENT_JSON: |
| 799 | switch (fpclassify(value)) { |
| 800 | case FP_NAN: |
| 801 | case FP_INFINITE: |
| 802 | GRN_TEXT_PUTS(ctx, outbuf, "null"); |
| 803 | break; |
| 804 | default: |
| 805 | grn_text_ftoa(ctx, outbuf, value); |
| 806 | break; |
| 807 | } |
| 808 | break; |
| 809 | case GRN_CONTENT_TSV: |
| 810 | grn_text_ftoa(ctx, outbuf, value); |
| 811 | break; |
| 812 | case GRN_CONTENT_XML: |
| 813 | GRN_TEXT_PUTS(ctx, outbuf, "<FLOAT>"); |
| 814 | grn_text_ftoa(ctx, outbuf, value); |
| 815 | GRN_TEXT_PUTS(ctx, outbuf, "</FLOAT>"); |
| 816 | break; |
| 817 | case GRN_CONTENT_MSGPACK: |
| 818 | #ifdef GRN_WITH_MESSAGE_PACK |
| 819 | msgpack_pack_double(&ctx->impl->output.msgpacker, value); |
| 820 | #endif |
| 821 | break; |
| 822 | case GRN_CONTENT_GROONGA_COMMAND_LIST: |
| 823 | grn_text_ftoa(ctx, outbuf, value); |
| 824 | break; |
| 825 | case GRN_CONTENT_APACHE_ARROW: |
| 826 | if (ctx->impl->output.arrow_stream_writer) { |
| 827 | grn_arrow_stream_writer_add_column_float( |
| 828 | ctx, |
| 829 | ctx->impl->output.arrow_stream_writer, |
| 830 | value); |
| 831 | } |
| 832 | break; |
| 833 | case GRN_CONTENT_NONE: |
| 834 | break; |
| 835 | } |
| 836 | INCR_LENGTH; |
| 837 | } |
| 838 | |
| 839 | void |
| 840 | grn_output_str(grn_ctx *ctx, |
| 841 | grn_obj *outbuf, |
| 842 | grn_content_type output_type, |
| 843 | const char *value, |
| 844 | size_t value_len) |
| 845 | { |
| 846 | put_delimiter(ctx, outbuf, output_type); |
| 847 | switch (output_type) { |
| 848 | case GRN_CONTENT_JSON: |
| 849 | grn_text_esc(ctx, outbuf, value, value_len); |
| 850 | break; |
| 851 | case GRN_CONTENT_TSV: |
| 852 | grn_text_esc(ctx, outbuf, value, value_len); |
| 853 | break; |
| 854 | case GRN_CONTENT_XML: |
| 855 | GRN_TEXT_PUTS(ctx, outbuf, "<TEXT>"); |
| 856 | grn_text_escape_xml(ctx, outbuf, value, value_len); |
| 857 | GRN_TEXT_PUTS(ctx, outbuf, "</TEXT>"); |
| 858 | break; |
| 859 | case GRN_CONTENT_MSGPACK: |
| 860 | #ifdef GRN_WITH_MESSAGE_PACK |
| 861 | msgpack_pack_str(&ctx->impl->output.msgpacker, value_len); |
| 862 | msgpack_pack_str_body(&ctx->impl->output.msgpacker, value, value_len); |
| 863 | #endif |
| 864 | break; |
| 865 | case GRN_CONTENT_GROONGA_COMMAND_LIST: |
| 866 | GRN_TEXT_PUT(ctx, outbuf, value, value_len); |
| 867 | break; |
| 868 | case GRN_CONTENT_APACHE_ARROW: |
| 869 | if (ctx->impl->output.arrow_stream_writer) { |
| 870 | grn_arrow_stream_writer_add_column_text( |
| 871 | ctx, |
| 872 | ctx->impl->output.arrow_stream_writer, |
| 873 | value, |
| 874 | value_len); |
| 875 | } |
| 876 | break; |
| 877 | case GRN_CONTENT_NONE: |
| 878 | break; |
| 879 | } |
| 880 | INCR_LENGTH; |
| 881 | } |
| 882 | |
| 883 | void |
| 884 | grn_output_cstr(grn_ctx *ctx, |
| 885 | grn_obj *outbuf, |
| 886 | grn_content_type output_type, |
| 887 | const char *value) |
| 888 | { |
| 889 | grn_output_str(ctx, outbuf, output_type, value, strlen(value)); |
| 890 | } |
| 891 | |
| 892 | void |
| 893 | grn_output_binary(grn_ctx *ctx, |
| 894 | grn_obj *outbuf, |
| 895 | grn_content_type output_type, |
| 896 | const uint8_t *value, |
| 897 | size_t value_len) |
| 898 | { |
| 899 | put_delimiter(ctx, outbuf, output_type); |
| 900 | grn_obj bulk; |
| 901 | GRN_BINARY_INIT(&bulk, GRN_OBJ_DO_SHALLOW_COPY); |
| 902 | GRN_BINARY_SET(ctx, &bulk, value, value_len); |
| 903 | switch (output_type) { |
| 904 | case GRN_CONTENT_JSON: |
| 905 | GRN_TEXT_PUTC(ctx, outbuf, '"'); |
| 906 | grn_obj_cast(ctx, &bulk, outbuf, false); |
| 907 | GRN_TEXT_PUTC(ctx, outbuf, '"'); |
| 908 | break; |
| 909 | case GRN_CONTENT_TSV: |
| 910 | grn_obj_cast(ctx, outbuf, &bulk, false); |
| 911 | break; |
| 912 | case GRN_CONTENT_XML: |
| 913 | GRN_TEXT_PUTS(ctx, outbuf, "<BINARY>"); |
| 914 | grn_obj_cast(ctx, outbuf, &bulk, false); |
| 915 | GRN_TEXT_PUTS(ctx, outbuf, "</BINARY>"); |
| 916 | break; |
| 917 | case GRN_CONTENT_MSGPACK: |
| 918 | #ifdef GRN_WITH_MESSAGE_PACK |
| 919 | msgpack_pack_bin(&ctx->impl->output.msgpacker, value_len); |
| 920 | msgpack_pack_bin_body(&ctx->impl->output.msgpacker, value, value_len); |
| 921 | #endif |
| 922 | break; |
| 923 | case GRN_CONTENT_GROONGA_COMMAND_LIST: |
| 924 | grn_obj_cast(ctx, outbuf, &bulk, false); |
| 925 | break; |
| 926 | case GRN_CONTENT_APACHE_ARROW: |
| 927 | if (ctx->impl->output.arrow_stream_writer) { |
| 928 | grn_arrow_stream_writer_add_column_binary( |
| 929 | ctx, |
| 930 | ctx->impl->output.arrow_stream_writer, |
| 931 | value, |
| 932 | value_len); |
| 933 | } |
| 934 | break; |
| 935 | case GRN_CONTENT_NONE: |
| 936 | break; |
| 937 | } |
| 938 | GRN_OBJ_FIN(ctx, &bulk); |
| 939 | INCR_LENGTH; |
| 940 | } |
| 941 | |
| 942 | void |
| 943 | grn_output_json(grn_ctx *ctx, |
| 944 | grn_obj *outbuf, |
| 945 | grn_content_type output_type, |
| 946 | const uint8_t *value, |
| 947 | size_t value_len) |
| 948 | { |
| 949 | put_delimiter(ctx, outbuf, output_type); |
| 950 | grn_obj bulk; |
| 951 | GRN_JSON_INIT(&bulk, GRN_OBJ_DO_SHALLOW_COPY); |
| 952 | GRN_JSON_SET(ctx, &bulk, value, value_len); |
| 953 | switch (output_type) { |
| 954 | case GRN_CONTENT_JSON: |
| 955 | /* TODO: pretty output */ |
| 956 | grn_json_to_string(ctx, &bulk, outbuf); |
| 957 | break; |
| 958 | case GRN_CONTENT_TSV: |
| 959 | ERR(GRN_FUNCTION_NOT_IMPLEMENTED, |
| 960 | "[output][json] TSV output isn't supported"); |
| 961 | break; |
| 962 | case GRN_CONTENT_XML: |
| 963 | ERR(GRN_FUNCTION_NOT_IMPLEMENTED, |
| 964 | "[output][json] XML output isn't supported"); |
| 965 | break; |
| 966 | case GRN_CONTENT_MSGPACK: |
| 967 | ERR(GRN_FUNCTION_NOT_IMPLEMENTED, |
| 968 | "[output][json] MessagePack output isn't supported"); |
| 969 | break; |
| 970 | case GRN_CONTENT_GROONGA_COMMAND_LIST: |
| 971 | ERR(GRN_FUNCTION_NOT_IMPLEMENTED, |
| 972 | "[output][json] Groonga command list output isn't supported"); |
| 973 | break; |
| 974 | case GRN_CONTENT_APACHE_ARROW: |
| 975 | ERR(GRN_FUNCTION_NOT_IMPLEMENTED, |
| 976 | "[output][json] Apache Arrow output isn't supported"); |
| 977 | break; |
| 978 | case GRN_CONTENT_NONE: |
| 979 | break; |
| 980 | } |
| 981 | GRN_OBJ_FIN(ctx, &bulk); |
| 982 | INCR_LENGTH; |
| 983 | } |
| 984 | |
| 985 | void |
| 986 | grn_output_bool(grn_ctx *ctx, |
| 987 | grn_obj *outbuf, |
| 988 | grn_content_type output_type, |
| 989 | bool value) |
| 990 | { |
| 991 | put_delimiter(ctx, outbuf, output_type); |
| 992 | switch (output_type) { |
| 993 | case GRN_CONTENT_JSON: |
| 994 | GRN_TEXT_PUTS(ctx, outbuf, value ? "true" : "false"); |
| 995 | break; |
| 996 | case GRN_CONTENT_TSV: |
| 997 | GRN_TEXT_PUTS(ctx, outbuf, value ? "true" : "false"); |
| 998 | break; |
| 999 | case GRN_CONTENT_XML: |
| 1000 | GRN_TEXT_PUTS(ctx, outbuf, "<BOOL>"); |
| 1001 | GRN_TEXT_PUTS(ctx, outbuf, value ? "true" : "false"); |
| 1002 | GRN_TEXT_PUTS(ctx, outbuf, "</BOOL>"); |
| 1003 | break; |
| 1004 | case GRN_CONTENT_MSGPACK: |
| 1005 | #ifdef GRN_WITH_MESSAGE_PACK |
| 1006 | if (value) { |
| 1007 | msgpack_pack_true(&ctx->impl->output.msgpacker); |
| 1008 | } else { |
| 1009 | msgpack_pack_false(&ctx->impl->output.msgpacker); |
| 1010 | } |
| 1011 | #endif |
| 1012 | break; |
| 1013 | case GRN_CONTENT_GROONGA_COMMAND_LIST: |
| 1014 | GRN_TEXT_PUTS(ctx, outbuf, value ? "true" : "false"); |
| 1015 | break; |
| 1016 | case GRN_CONTENT_APACHE_ARROW: |
| 1017 | break; |
| 1018 | case GRN_CONTENT_NONE: |
| 1019 | break; |
| 1020 | } |
| 1021 | INCR_LENGTH; |
| 1022 | } |
| 1023 | |
| 1024 | void |
| 1025 | grn_output_null(grn_ctx *ctx, grn_obj *outbuf, grn_content_type output_type) |
| 1026 | { |
| 1027 | put_delimiter(ctx, outbuf, output_type); |
| 1028 | switch (output_type) { |
| 1029 | case GRN_CONTENT_JSON: |
| 1030 | GRN_TEXT_PUTS(ctx, outbuf, "null"); |
| 1031 | break; |
| 1032 | case GRN_CONTENT_TSV: |
| 1033 | break; |
| 1034 | case GRN_CONTENT_XML: |
| 1035 | GRN_TEXT_PUTS(ctx, outbuf, "<NULL/>"); |
| 1036 | break; |
| 1037 | case GRN_CONTENT_MSGPACK: |
| 1038 | #ifdef GRN_WITH_MESSAGE_PACK |
| 1039 | msgpack_pack_nil(&ctx->impl->output.msgpacker); |
| 1040 | #endif |
| 1041 | break; |
| 1042 | case GRN_CONTENT_GROONGA_COMMAND_LIST: |
| 1043 | break; |
| 1044 | case GRN_CONTENT_APACHE_ARROW: |
| 1045 | break; |
| 1046 | case GRN_CONTENT_NONE: |
| 1047 | break; |
| 1048 | } |
| 1049 | INCR_LENGTH; |
| 1050 | } |
| 1051 | |
| 1052 | static inline void |
| 1053 | grn_output_bulk_void(grn_ctx *ctx, |
| 1054 | grn_obj *outbuf, |
| 1055 | grn_content_type output_type, |
| 1056 | const char *value, |
| 1057 | size_t value_len) |
| 1058 | { |
| 1059 | if (value_len == sizeof(grn_id) && *(grn_id *)value == GRN_ID_NIL) { |
| 1060 | grn_output_null(ctx, outbuf, output_type); |
| 1061 | } else { |
| 1062 | grn_output_str(ctx, outbuf, output_type, value, value_len); |
| 1063 | } |
| 1064 | } |
| 1065 | |
| 1066 | void |
| 1067 | grn_output_time(grn_ctx *ctx, |
| 1068 | grn_obj *outbuf, |
| 1069 | grn_content_type output_type, |
| 1070 | int64_t value) |
| 1071 | { |
| 1072 | double dv = (double)value; |
| 1073 | dv /= GRN_TIME_USEC_PER_SEC_F; |
| 1074 | put_delimiter(ctx, outbuf, output_type); |
| 1075 | switch (output_type) { |
| 1076 | case GRN_CONTENT_JSON: |
| 1077 | grn_text_ftoa(ctx, outbuf, dv); |
| 1078 | break; |
| 1079 | case GRN_CONTENT_TSV: |
| 1080 | grn_text_ftoa(ctx, outbuf, dv); |
| 1081 | break; |
| 1082 | case GRN_CONTENT_XML: |
| 1083 | GRN_TEXT_PUTS(ctx, outbuf, "<DATE>"); |
| 1084 | grn_text_ftoa(ctx, outbuf, dv); |
| 1085 | GRN_TEXT_PUTS(ctx, outbuf, "</DATE>"); |
| 1086 | break; |
| 1087 | case GRN_CONTENT_MSGPACK: |
| 1088 | #ifdef GRN_WITH_MESSAGE_PACK |
| 1089 | msgpack_pack_double(&ctx->impl->output.msgpacker, dv); |
| 1090 | #endif |
| 1091 | break; |
| 1092 | case GRN_CONTENT_GROONGA_COMMAND_LIST: |
| 1093 | grn_text_ftoa(ctx, outbuf, dv); |
| 1094 | break; |
| 1095 | case GRN_CONTENT_APACHE_ARROW: |
| 1096 | if (ctx->impl->output.arrow_stream_writer) { |
| 1097 | grn_timeval timeval; |
| 1098 | timeval.tv_sec = GRN_TIME_USEC_TO_SEC(value); |
| 1099 | timeval.tv_nsec = |
| 1100 | GRN_TIME_USEC_TO_NSEC((int32_t)(value % GRN_TIME_USEC_PER_SEC)); |
| 1101 | grn_arrow_stream_writer_add_column_timestamp( |
| 1102 | ctx, |
| 1103 | ctx->impl->output.arrow_stream_writer, |
| 1104 | timeval); |
| 1105 | } |
| 1106 | break; |
| 1107 | case GRN_CONTENT_NONE: |
| 1108 | break; |
| 1109 | } |
| 1110 | INCR_LENGTH; |
| 1111 | } |
| 1112 | |
| 1113 | void |
| 1114 | grn_output_geo_point(grn_ctx *ctx, |
| 1115 | grn_obj *outbuf, |
| 1116 | grn_content_type output_type, |
| 1117 | grn_geo_point *value) |
| 1118 | { |
| 1119 | put_delimiter(ctx, outbuf, output_type); |
| 1120 | switch (output_type) { |
| 1121 | case GRN_CONTENT_JSON: |
| 1122 | if (value) { |
| 1123 | GRN_TEXT_PUTC(ctx, outbuf, '"'); |
| 1124 | grn_text_itoa(ctx, outbuf, value->latitude); |
| 1125 | GRN_TEXT_PUTC(ctx, outbuf, 'x'); |
| 1126 | grn_text_itoa(ctx, outbuf, value->longitude); |
| 1127 | GRN_TEXT_PUTC(ctx, outbuf, '"'); |
| 1128 | } else { |
| 1129 | GRN_TEXT_PUTS(ctx, outbuf, "null"); |
| 1130 | } |
| 1131 | break; |
| 1132 | case GRN_CONTENT_TSV: |
| 1133 | if (value) { |
| 1134 | GRN_TEXT_PUTC(ctx, outbuf, '"'); |
| 1135 | grn_text_itoa(ctx, outbuf, value->latitude); |
| 1136 | GRN_TEXT_PUTC(ctx, outbuf, 'x'); |
| 1137 | grn_text_itoa(ctx, outbuf, value->longitude); |
| 1138 | GRN_TEXT_PUTC(ctx, outbuf, '"'); |
| 1139 | } else { |
| 1140 | GRN_TEXT_PUTS(ctx, outbuf, "\"\""); |
| 1141 | } |
| 1142 | break; |
| 1143 | case GRN_CONTENT_XML: |
| 1144 | GRN_TEXT_PUTS(ctx, outbuf, "<GEO_POINT>"); |
| 1145 | if (value) { |
| 1146 | grn_text_itoa(ctx, outbuf, value->latitude); |
| 1147 | GRN_TEXT_PUTC(ctx, outbuf, 'x'); |
| 1148 | grn_text_itoa(ctx, outbuf, value->longitude); |
| 1149 | } |
| 1150 | GRN_TEXT_PUTS(ctx, outbuf, "</GEO_POINT>"); |
| 1151 | break; |
| 1152 | case GRN_CONTENT_MSGPACK: |
| 1153 | #ifdef GRN_WITH_MESSAGE_PACK |
| 1154 | if (value) { |
| 1155 | grn_obj buf; |
| 1156 | GRN_TEXT_INIT(&buf, 0); |
| 1157 | grn_text_itoa(ctx, &buf, value->latitude); |
| 1158 | GRN_TEXT_PUTC(ctx, &buf, 'x'); |
| 1159 | grn_text_itoa(ctx, &buf, value->longitude); |
| 1160 | msgpack_pack_str(&ctx->impl->output.msgpacker, GRN_TEXT_LEN(&buf)); |
| 1161 | msgpack_pack_str_body(&ctx->impl->output.msgpacker, |
| 1162 | GRN_TEXT_VALUE(&buf), |
| 1163 | GRN_TEXT_LEN(&buf)); |
| 1164 | grn_obj_close(ctx, &buf); |
| 1165 | } else { |
| 1166 | msgpack_pack_nil(&ctx->impl->output.msgpacker); |
| 1167 | } |
| 1168 | #endif |
| 1169 | break; |
| 1170 | case GRN_CONTENT_GROONGA_COMMAND_LIST: |
| 1171 | if (value) { |
| 1172 | GRN_TEXT_PUTC(ctx, outbuf, '"'); |
| 1173 | grn_text_itoa(ctx, outbuf, value->latitude); |
| 1174 | GRN_TEXT_PUTC(ctx, outbuf, 'x'); |
| 1175 | grn_text_itoa(ctx, outbuf, value->longitude); |
| 1176 | GRN_TEXT_PUTC(ctx, outbuf, '"'); |
| 1177 | } else { |
| 1178 | GRN_TEXT_PUTS(ctx, outbuf, "\"\""); |
| 1179 | } |
| 1180 | break; |
| 1181 | case GRN_CONTENT_APACHE_ARROW: |
| 1182 | break; |
| 1183 | case GRN_CONTENT_NONE: |
| 1184 | break; |
| 1185 | } |
| 1186 | INCR_LENGTH; |
| 1187 | } |
| 1188 | |
| 1189 | static void |
| 1190 | grn_output_record(grn_ctx *ctx, |
| 1191 | grn_obj *outbuf, |
| 1192 | grn_content_type output_type, |
| 1193 | grn_obj *record) |
| 1194 | { |
| 1195 | if (output_type == GRN_CONTENT_APACHE_ARROW) { |
| 1196 | if (ctx->impl->output.arrow_stream_writer) { |
| 1197 | grn_arrow_stream_writer_add_column_record( |
| 1198 | ctx, |
| 1199 | ctx->impl->output.arrow_stream_writer, |
| 1200 | record); |
| 1201 | } |
| 1202 | } else { |
| 1203 | grn_obj *table = grn_ctx_at(ctx, record->header.domain); |
| 1204 | grn_id id = GRN_RECORD_VALUE(record); |
| 1205 | if (table && table->header.type != GRN_TABLE_NO_KEY) { |
| 1206 | grn_obj buf; |
| 1207 | grn_obj *accessor = grn_obj_column(ctx, |
| 1208 | table, |
| 1209 | GRN_COLUMN_NAME_KEY, |
| 1210 | GRN_COLUMN_NAME_KEY_LEN); |
| 1211 | GRN_VOID_INIT(&buf); |
| 1212 | if (accessor) { |
| 1213 | if (id == GRN_ID_NIL) { |
| 1214 | grn_obj_reinit_for(ctx, &buf, accessor); |
| 1215 | } else { |
| 1216 | grn_obj_get_value(ctx, accessor, id, &buf); |
| 1217 | } |
| 1218 | grn_obj_unlink(ctx, accessor); |
| 1219 | } |
| 1220 | grn_output_obj(ctx, outbuf, output_type, &buf, NULL); |
| 1221 | GRN_OBJ_FIN(ctx, &buf); |
| 1222 | } else { |
| 1223 | grn_output_int64(ctx, outbuf, output_type, id); |
| 1224 | } |
| 1225 | if (table) { |
| 1226 | grn_obj_unref(ctx, table); |
| 1227 | } |
| 1228 | } |
| 1229 | } |
| 1230 | |
| 1231 | typedef struct { |
| 1232 | grn_obj bulk; |
| 1233 | grn_obj vector; |
| 1234 | grn_obj sub_ids_stack; |
| 1235 | size_t next_sub_ids; |
| 1236 | size_t n_processed_records; |
| 1237 | bool auto_flush; |
| 1238 | } grn_output_table_data; |
| 1239 | |
| 1240 | static void |
| 1241 | grn_output_table_data_init(grn_ctx *ctx, |
| 1242 | grn_output_table_data *data, |
| 1243 | grn_obj_format *format) |
| 1244 | { |
| 1245 | GRN_TEXT_INIT(&(data->bulk), 0); |
| 1246 | GRN_TEXT_INIT(&(data->vector), GRN_OBJ_VECTOR); |
| 1247 | GRN_TEXT_INIT(&(data->sub_ids_stack), 0); |
| 1248 | data->next_sub_ids = 0; |
| 1249 | data->n_processed_records = 0; |
| 1250 | data->auto_flush = (format && (format->flags & GRN_OBJ_FORMAT_AUTO_FLUSH)); |
| 1251 | } |
| 1252 | |
| 1253 | static grn_obj * |
| 1254 | grn_output_table_data_push_sub_ids(grn_ctx *ctx, grn_output_table_data *data) |
| 1255 | { |
| 1256 | grn_obj *sub_ids = NULL; |
| 1257 | size_t n_sub_ids = GRN_BULK_VSIZE(&(data->sub_ids_stack)) / sizeof(grn_obj); |
| 1258 | bool reuse = (data->next_sub_ids < n_sub_ids); |
| 1259 | if (!reuse) { |
| 1260 | grn_obj dummy = {0}; |
| 1261 | grn_bulk_write(ctx, |
| 1262 | &(data->sub_ids_stack), |
| 1263 | (void *)&dummy, |
| 1264 | sizeof(grn_obj)); |
| 1265 | } |
| 1266 | grn_obj *sub_ids_stack_raw = (grn_obj *)GRN_BULK_HEAD(&(data->sub_ids_stack)); |
| 1267 | sub_ids = &(sub_ids_stack_raw[data->next_sub_ids]); |
| 1268 | if (reuse) { |
| 1269 | GRN_BULK_REWIND(sub_ids); |
| 1270 | } else { |
| 1271 | GRN_RECORD_INIT(sub_ids, GRN_OBJ_VECTOR, GRN_ID_NIL); |
| 1272 | } |
| 1273 | data->next_sub_ids++; |
| 1274 | return sub_ids; |
| 1275 | } |
| 1276 | |
| 1277 | static void |
| 1278 | grn_output_table_data_pop_sub_ids(grn_ctx *ctx, grn_output_table_data *data) |
| 1279 | { |
| 1280 | data->next_sub_ids--; |
| 1281 | } |
| 1282 | |
| 1283 | static void |
| 1284 | grn_output_table_data_open_record(grn_ctx *ctx, grn_output_table_data *data) |
| 1285 | { |
| 1286 | if (!data->auto_flush) { |
| 1287 | return; |
| 1288 | } |
| 1289 | if (data->n_processed_records == 0) { |
| 1290 | return; |
| 1291 | } |
| 1292 | |
| 1293 | if ((data->n_processed_records % grn_output_auto_flush_interval) == 0) { |
| 1294 | const int flags = 0; |
| 1295 | grn_ctx_output_flush(ctx, flags); |
| 1296 | } |
| 1297 | } |
| 1298 | |
| 1299 | static void |
| 1300 | grn_output_table_data_close_record(grn_ctx *ctx, grn_output_table_data *data) |
| 1301 | { |
| 1302 | data->n_processed_records++; |
| 1303 | } |
| 1304 | |
| 1305 | static void |
| 1306 | grn_output_table_data_fin(grn_ctx *ctx, grn_output_table_data *data) |
| 1307 | { |
| 1308 | GRN_OBJ_FIN(ctx, &(data->bulk)); |
| 1309 | GRN_OBJ_FIN(ctx, &(data->vector)); |
| 1310 | size_t i; |
| 1311 | size_t n_sub_ids = GRN_BULK_VSIZE(&(data->sub_ids_stack)) / sizeof(grn_obj); |
| 1312 | grn_obj *sub_ids_stack_raw = (grn_obj *)GRN_BULK_HEAD(&(data->sub_ids_stack)); |
| 1313 | for (i = 0; i < n_sub_ids; i++) { |
| 1314 | GRN_OBJ_FIN(ctx, &(sub_ids_stack_raw[i])); |
| 1315 | } |
| 1316 | GRN_OBJ_FIN(ctx, &(data->sub_ids_stack)); |
| 1317 | } |
| 1318 | |
| 1319 | static void |
| 1320 | grn_output_table_column_value(grn_ctx *ctx, |
| 1321 | grn_obj *outbuf, |
| 1322 | grn_content_type output_type, |
| 1323 | grn_obj *obj, |
| 1324 | grn_id id, |
| 1325 | grn_output_table_data *data) |
| 1326 | { |
| 1327 | uint32_t vs; |
| 1328 | grn_obj *bulk = &(data->bulk); |
| 1329 | grn_obj *vector = &(data->vector); |
| 1330 | if (obj->header.type == GRN_ACCESSOR) { |
| 1331 | grn_accessor *a = (grn_accessor *)obj; |
| 1332 | grn_obj_reinit(ctx, bulk, GRN_DB_TEXT, 0); |
| 1333 | for (;;) { |
| 1334 | bulk->header.domain = grn_obj_get_range(ctx, obj); |
| 1335 | GRN_BULK_REWIND(bulk); |
| 1336 | switch (a->action) { |
| 1337 | case GRN_ACCESSOR_GET_ID: |
| 1338 | GRN_UINT32_PUT(ctx, bulk, id); |
| 1339 | bulk->header.domain = GRN_DB_UINT32; |
| 1340 | break; |
| 1341 | case GRN_ACCESSOR_GET_KEY: |
| 1342 | grn_table_get_key2(ctx, a->obj, id, bulk); |
| 1343 | bulk->header.domain = DB_OBJ(a->obj)->header.domain; |
| 1344 | break; |
| 1345 | case GRN_ACCESSOR_GET_VALUE: |
| 1346 | grn_obj_get_value(ctx, a->obj, id, bulk); |
| 1347 | bulk->header.domain = DB_OBJ(a->obj)->range; |
| 1348 | break; |
| 1349 | case GRN_ACCESSOR_GET_SCORE: |
| 1350 | { |
| 1351 | double score = grn_table_get_score(ctx, a->obj, id); |
| 1352 | if (grn_ctx_get_command_version(ctx) == GRN_COMMAND_VERSION_1) { |
| 1353 | int32_t int32_score = (int32_t)score; |
| 1354 | GRN_INT32_PUT(ctx, bulk, int32_score); |
| 1355 | bulk->header.domain = GRN_DB_INT32; |
| 1356 | } else { |
| 1357 | GRN_FLOAT_PUT(ctx, bulk, score); |
| 1358 | bulk->header.domain = GRN_DB_FLOAT; |
| 1359 | } |
| 1360 | } |
| 1361 | break; |
| 1362 | case GRN_ACCESSOR_GET_NSUBRECS: |
| 1363 | { |
| 1364 | grn_rset_recinfo *ri = |
| 1365 | (grn_rset_recinfo *)grn_obj_get_value_(ctx, a->obj, id, &vs); |
| 1366 | GRN_INT32_PUT(ctx, bulk, ri->n_subrecs); |
| 1367 | } |
| 1368 | bulk->header.domain = GRN_DB_INT32; |
| 1369 | break; |
| 1370 | case GRN_ACCESSOR_GET_MAX: |
| 1371 | bulk->header.domain = DB_OBJ(a->obj)->group.aggregated_value_type_id; |
| 1372 | { |
| 1373 | grn_rset_recinfo *ri = |
| 1374 | (grn_rset_recinfo *)grn_obj_get_value_(ctx, a->obj, id, &vs); |
| 1375 | grn_rset_aggregated_value max = |
| 1376 | grn_rset_recinfo_get_max(ctx, ri, a->obj); |
| 1377 | if (bulk->header.domain == GRN_DB_INT64) { |
| 1378 | GRN_INT64_PUT(ctx, bulk, max.value_int64); |
| 1379 | } else { |
| 1380 | GRN_FLOAT_PUT(ctx, bulk, max.value_double); |
| 1381 | } |
| 1382 | } |
| 1383 | break; |
| 1384 | case GRN_ACCESSOR_GET_MIN: |
| 1385 | bulk->header.domain = DB_OBJ(a->obj)->group.aggregated_value_type_id; |
| 1386 | { |
| 1387 | grn_rset_recinfo *ri = |
| 1388 | (grn_rset_recinfo *)grn_obj_get_value_(ctx, a->obj, id, &vs); |
| 1389 | grn_rset_aggregated_value min = |
| 1390 | grn_rset_recinfo_get_min(ctx, ri, a->obj); |
| 1391 | if (bulk->header.domain == GRN_DB_INT64) { |
| 1392 | GRN_INT64_PUT(ctx, bulk, min.value_int64); |
| 1393 | } else { |
| 1394 | GRN_FLOAT_PUT(ctx, bulk, min.value_double); |
| 1395 | } |
| 1396 | } |
| 1397 | break; |
| 1398 | case GRN_ACCESSOR_GET_SUM: |
| 1399 | bulk->header.domain = DB_OBJ(a->obj)->group.aggregated_value_type_id; |
| 1400 | { |
| 1401 | grn_rset_recinfo *ri = |
| 1402 | (grn_rset_recinfo *)grn_obj_get_value_(ctx, a->obj, id, &vs); |
| 1403 | grn_rset_aggregated_value sum = |
| 1404 | grn_rset_recinfo_get_sum(ctx, ri, a->obj); |
| 1405 | if (bulk->header.domain == GRN_DB_INT64) { |
| 1406 | GRN_INT64_PUT(ctx, bulk, sum.value_int64); |
| 1407 | } else { |
| 1408 | GRN_FLOAT_PUT(ctx, bulk, sum.value_double); |
| 1409 | } |
| 1410 | } |
| 1411 | break; |
| 1412 | case GRN_ACCESSOR_GET_AVG: |
| 1413 | case GRN_ACCESSOR_GET_MEAN: |
| 1414 | { |
| 1415 | grn_rset_recinfo *ri = |
| 1416 | (grn_rset_recinfo *)grn_obj_get_value_(ctx, a->obj, id, &vs); |
| 1417 | double mean; |
| 1418 | mean = grn_rset_recinfo_get_mean(ctx, ri, a->obj); |
| 1419 | GRN_FLOAT_PUT(ctx, bulk, mean); |
| 1420 | } |
| 1421 | bulk->header.domain = GRN_DB_FLOAT; |
| 1422 | break; |
| 1423 | case GRN_ACCESSOR_GET_COLUMN_VALUE: |
| 1424 | switch (a->obj->header.flags & GRN_OBJ_COLUMN_TYPE_MASK) { |
| 1425 | case GRN_OBJ_COLUMN_VECTOR: |
| 1426 | if (a->next) { |
| 1427 | grn_obj *sub_ids = grn_output_table_data_push_sub_ids(ctx, data); |
| 1428 | sub_ids->header.domain = DB_OBJ(a->obj)->range; |
| 1429 | grn_obj_get_value(ctx, a->obj, id, sub_ids); |
| 1430 | size_t n = GRN_RECORD_VECTOR_SIZE(sub_ids); |
| 1431 | grn_output_array_open(ctx, outbuf, output_type, "VECTOR", (int)n); |
| 1432 | size_t i; |
| 1433 | for (i = 0; i < n; i++) { |
| 1434 | grn_id sub_id = GRN_RECORD_VALUE_AT(sub_ids, i); |
| 1435 | grn_output_table_column_value(ctx, |
| 1436 | outbuf, |
| 1437 | output_type, |
| 1438 | (grn_obj *)a->next, |
| 1439 | sub_id, |
| 1440 | data); |
| 1441 | } |
| 1442 | grn_output_table_data_pop_sub_ids(ctx, data); |
| 1443 | grn_output_array_close(ctx, outbuf, output_type); |
| 1444 | } else { |
| 1445 | grn_output_table_column_value(ctx, |
| 1446 | outbuf, |
| 1447 | output_type, |
| 1448 | a->obj, |
| 1449 | id, |
| 1450 | data); |
| 1451 | } |
| 1452 | return; |
| 1453 | case GRN_OBJ_COLUMN_INDEX: |
| 1454 | if (a->next) { |
| 1455 | grn_ii *ii = (grn_ii *)(a->obj); |
| 1456 | grn_obj *sub_ids = grn_output_table_data_push_sub_ids(ctx, data); |
| 1457 | sub_ids->header.domain = DB_OBJ(ii)->range; |
| 1458 | grn_ii_cursor *cursor = |
| 1459 | grn_ii_cursor_open(ctx, |
| 1460 | ii, |
| 1461 | id, |
| 1462 | GRN_ID_NIL, |
| 1463 | GRN_ID_MAX, |
| 1464 | (int)grn_ii_get_n_elements(ctx, ii), |
| 1465 | 0); |
| 1466 | if (cursor) { |
| 1467 | grn_posting *posting; |
| 1468 | while ((posting = grn_ii_cursor_next(ctx, cursor))) { |
| 1469 | GRN_RECORD_PUT(ctx, sub_ids, posting->rid); |
| 1470 | } |
| 1471 | grn_ii_cursor_close(ctx, cursor); |
| 1472 | } |
| 1473 | size_t n = GRN_RECORD_VECTOR_SIZE(sub_ids); |
| 1474 | grn_output_array_open(ctx, outbuf, output_type, "VECTOR", (int)n); |
| 1475 | size_t i; |
| 1476 | for (i = 0; i < n; i++) { |
| 1477 | grn_id sub_id = GRN_RECORD_VALUE_AT(sub_ids, i); |
| 1478 | grn_output_table_column_value(ctx, |
| 1479 | outbuf, |
| 1480 | output_type, |
| 1481 | (grn_obj *)(a->next), |
| 1482 | sub_id, |
| 1483 | data); |
| 1484 | } |
| 1485 | grn_output_table_data_pop_sub_ids(ctx, data); |
| 1486 | grn_output_array_close(ctx, outbuf, output_type); |
| 1487 | return; |
| 1488 | } else { |
| 1489 | grn_obj_get_value(ctx, a->obj, id, bulk); |
| 1490 | break; |
| 1491 | } |
| 1492 | default: |
| 1493 | grn_obj_get_value(ctx, a->obj, id, bulk); |
| 1494 | break; |
| 1495 | } |
| 1496 | break; |
| 1497 | case GRN_ACCESSOR_GET_DB_OBJ: |
| 1498 | /* todo */ |
| 1499 | break; |
| 1500 | case GRN_ACCESSOR_LOOKUP: |
| 1501 | /* todo */ |
| 1502 | break; |
| 1503 | case GRN_ACCESSOR_FUNCALL: |
| 1504 | /* todo */ |
| 1505 | break; |
| 1506 | } |
| 1507 | if (a->next) { |
| 1508 | a = a->next; |
| 1509 | if (GRN_BULK_VSIZE(bulk) >= sizeof(grn_id)) { |
| 1510 | id = *((grn_id *)GRN_BULK_HEAD(bulk)); |
| 1511 | } else { |
| 1512 | id = GRN_ID_NIL; |
| 1513 | } |
| 1514 | } else { |
| 1515 | break; |
| 1516 | } |
| 1517 | } |
| 1518 | grn_output_obj(ctx, outbuf, output_type, bulk, NULL); |
| 1519 | } else { |
| 1520 | grn_obj *buf = NULL; |
| 1521 | grn_obj_format *format_argument = NULL; |
| 1522 | grn_obj_format format; |
| 1523 | GRN_OBJ_FORMAT_INIT(&format, 0, 0, 0, 0); |
| 1524 | switch (obj->header.type) { |
| 1525 | case GRN_COLUMN_FIX_SIZE: |
| 1526 | grn_obj_reinit(ctx, bulk, DB_OBJ(obj)->range, 0); |
| 1527 | buf = bulk; |
| 1528 | break; |
| 1529 | case GRN_COLUMN_VAR_SIZE: |
| 1530 | { |
| 1531 | grn_column_flags flags = grn_column_get_flags(ctx, obj); |
| 1532 | if ((flags & GRN_OBJ_COLUMN_TYPE_MASK) == GRN_OBJ_COLUMN_VECTOR) { |
| 1533 | bool is_var_size_element = |
| 1534 | grn_type_id_is_text_family(ctx, DB_OBJ(obj)->range); |
| 1535 | if (is_var_size_element) { |
| 1536 | buf = vector; |
| 1537 | } else { |
| 1538 | buf = bulk; |
| 1539 | } |
| 1540 | grn_obj_reinit(ctx, buf, DB_OBJ(obj)->range, GRN_OBJ_VECTOR); |
| 1541 | if (flags & GRN_OBJ_WITH_WEIGHT) { |
| 1542 | format.flags |= GRN_OBJ_FORMAT_WITH_WEIGHT; |
| 1543 | if (flags & (GRN_OBJ_WEIGHT_FLOAT32 | GRN_OBJ_WEIGHT_BFLOAT16)) { |
| 1544 | format.flags |= GRN_OBJ_FORMAT_WEIGHT_FLOAT32; |
| 1545 | } |
| 1546 | format_argument = &format; |
| 1547 | } |
| 1548 | } else { |
| 1549 | grn_obj_reinit(ctx, bulk, DB_OBJ(obj)->range, 0); |
| 1550 | buf = bulk; |
| 1551 | } |
| 1552 | } |
| 1553 | break; |
| 1554 | case GRN_COLUMN_INDEX: |
| 1555 | grn_obj_reinit(ctx, bulk, GRN_DB_UINT32, 0); |
| 1556 | buf = bulk; |
| 1557 | break; |
| 1558 | default: |
| 1559 | grn_obj_reinit(ctx, bulk, GRN_DB_TEXT, 0); |
| 1560 | buf = bulk; |
| 1561 | break; |
| 1562 | } |
| 1563 | grn_obj_get_value(ctx, obj, id, buf); |
| 1564 | grn_output_obj(ctx, outbuf, output_type, buf, format_argument); |
| 1565 | } |
| 1566 | } |
| 1567 | |
| 1568 | static inline void |
| 1569 | grn_output_void(grn_ctx *ctx, |
| 1570 | grn_obj *outbuf, |
| 1571 | grn_content_type output_type, |
| 1572 | grn_obj *bulk, |
| 1573 | grn_obj_format *format) |
| 1574 | { |
| 1575 | grn_output_null(ctx, outbuf, output_type); |
| 1576 | } |
| 1577 | |
| 1578 | static inline void |
| 1579 | grn_output_bulk(grn_ctx *ctx, |
| 1580 | grn_obj *outbuf, |
| 1581 | grn_content_type output_type, |
| 1582 | grn_obj *bulk, |
| 1583 | grn_obj_format *format) |
| 1584 | { |
| 1585 | grn_obj buf; |
| 1586 | GRN_TEXT_INIT(&buf, 0); |
| 1587 | switch (bulk->header.domain) { |
| 1588 | case GRN_DB_VOID: |
| 1589 | grn_output_bulk_void(ctx, |
| 1590 | outbuf, |
| 1591 | output_type, |
| 1592 | GRN_BULK_HEAD(bulk), |
| 1593 | GRN_BULK_VSIZE(bulk)); |
| 1594 | break; |
| 1595 | case GRN_DB_SHORT_TEXT: |
| 1596 | case GRN_DB_TEXT: |
| 1597 | case GRN_DB_LONG_TEXT: |
| 1598 | grn_output_str(ctx, |
| 1599 | outbuf, |
| 1600 | output_type, |
| 1601 | GRN_BULK_HEAD(bulk), |
| 1602 | GRN_BULK_VSIZE(bulk)); |
| 1603 | break; |
| 1604 | case GRN_DB_BOOL: |
| 1605 | grn_output_bool(ctx, |
| 1606 | outbuf, |
| 1607 | output_type, |
| 1608 | GRN_BULK_VSIZE(bulk) ? GRN_UINT8_VALUE(bulk) : 0); |
| 1609 | break; |
| 1610 | case GRN_DB_INT8: |
| 1611 | grn_output_int8(ctx, |
| 1612 | outbuf, |
| 1613 | output_type, |
| 1614 | GRN_BULK_VSIZE(bulk) ? GRN_INT8_VALUE(bulk) : 0); |
| 1615 | break; |
| 1616 | case GRN_DB_UINT8: |
| 1617 | grn_output_uint8(ctx, |
| 1618 | outbuf, |
| 1619 | output_type, |
| 1620 | GRN_BULK_VSIZE(bulk) ? GRN_UINT8_VALUE(bulk) : 0); |
| 1621 | break; |
| 1622 | case GRN_DB_INT16: |
| 1623 | grn_output_int16(ctx, |
| 1624 | outbuf, |
| 1625 | output_type, |
| 1626 | GRN_BULK_VSIZE(bulk) ? GRN_INT16_VALUE(bulk) : 0); |
| 1627 | break; |
| 1628 | case GRN_DB_UINT16: |
| 1629 | grn_output_uint16(ctx, |
| 1630 | outbuf, |
| 1631 | output_type, |
| 1632 | GRN_BULK_VSIZE(bulk) ? GRN_UINT16_VALUE(bulk) : 0); |
| 1633 | break; |
| 1634 | case GRN_DB_INT32: |
| 1635 | grn_output_int32(ctx, |
| 1636 | outbuf, |
| 1637 | output_type, |
| 1638 | GRN_BULK_VSIZE(bulk) ? GRN_INT32_VALUE(bulk) : 0); |
| 1639 | break; |
| 1640 | case GRN_DB_UINT32: |
| 1641 | grn_output_uint32(ctx, |
| 1642 | outbuf, |
| 1643 | output_type, |
| 1644 | GRN_BULK_VSIZE(bulk) ? GRN_UINT32_VALUE(bulk) : 0); |
| 1645 | break; |
| 1646 | case GRN_DB_INT64: |
| 1647 | grn_output_int64(ctx, |
| 1648 | outbuf, |
| 1649 | output_type, |
| 1650 | GRN_BULK_VSIZE(bulk) ? GRN_INT64_VALUE(bulk) : 0); |
| 1651 | break; |
| 1652 | case GRN_DB_UINT64: |
| 1653 | grn_output_uint64(ctx, |
| 1654 | outbuf, |
| 1655 | output_type, |
| 1656 | GRN_BULK_VSIZE(bulk) ? GRN_UINT64_VALUE(bulk) : 0); |
| 1657 | break; |
| 1658 | #ifdef GRN_HAVE_BFLOAT16 |
| 1659 | case GRN_DB_BFLOAT16: |
| 1660 | grn_output_bfloat16(ctx, |
| 1661 | outbuf, |
| 1662 | output_type, |
| 1663 | GRN_BULK_VSIZE(bulk) > 0 ? GRN_BFLOAT16_VALUE(bulk) |
| 1664 | : 0); |
| 1665 | break; |
| 1666 | #endif |
| 1667 | case GRN_DB_FLOAT32: |
| 1668 | grn_output_float32(ctx, |
| 1669 | outbuf, |
| 1670 | output_type, |
| 1671 | GRN_BULK_VSIZE(bulk) ? GRN_FLOAT32_VALUE(bulk) : 0); |
| 1672 | break; |
| 1673 | case GRN_DB_FLOAT: |
| 1674 | grn_output_float(ctx, |
| 1675 | outbuf, |
| 1676 | output_type, |
| 1677 | GRN_BULK_VSIZE(bulk) ? GRN_FLOAT_VALUE(bulk) : 0); |
| 1678 | break; |
| 1679 | case GRN_DB_TIME: |
| 1680 | grn_output_time(ctx, |
| 1681 | outbuf, |
| 1682 | output_type, |
| 1683 | GRN_BULK_VSIZE(bulk) ? GRN_INT64_VALUE(bulk) : 0); |
| 1684 | break; |
| 1685 | case GRN_DB_TOKYO_GEO_POINT: |
| 1686 | case GRN_DB_WGS84_GEO_POINT: |
| 1687 | grn_output_geo_point( |
| 1688 | ctx, |
| 1689 | outbuf, |
| 1690 | output_type, |
| 1691 | GRN_BULK_VSIZE(bulk) ? (grn_geo_point *)GRN_BULK_HEAD(bulk) : NULL); |
| 1692 | break; |
| 1693 | case GRN_DB_SHORT_BINARY: |
| 1694 | case GRN_DB_BINARY: |
| 1695 | case GRN_DB_LONG_BINARY: |
| 1696 | grn_output_binary(ctx, |
| 1697 | outbuf, |
| 1698 | output_type, |
| 1699 | GRN_BINARY_VALUE(bulk), |
| 1700 | GRN_BINARY_LEN(bulk)); |
| 1701 | break; |
| 1702 | case GRN_DB_JSON: |
| 1703 | grn_output_json(ctx, |
| 1704 | outbuf, |
| 1705 | output_type, |
| 1706 | GRN_JSON_VALUE(bulk), |
| 1707 | GRN_JSON_LEN(bulk)); |
| 1708 | break; |
| 1709 | default: |
| 1710 | if (format) { |
| 1711 | size_t j; |
| 1712 | size_t ncolumns = GRN_BULK_VSIZE(&format->columns) / sizeof(grn_obj *); |
| 1713 | grn_id id = GRN_RECORD_VALUE(bulk); |
| 1714 | grn_obj **columns = (grn_obj **)GRN_BULK_HEAD(&format->columns); |
| 1715 | if (format->flags & GRN_OBJ_FORMAT_WITH_COLUMN_NAMES) { |
| 1716 | grn_output_array_open(ctx, |
| 1717 | outbuf, |
| 1718 | output_type, |
| 1719 | "COLUMNS", |
| 1720 | (int)ncolumns); |
| 1721 | for (j = 0; j < ncolumns; j++) { |
| 1722 | grn_id range_id; |
| 1723 | grn_output_array_open(ctx, outbuf, output_type, "COLUMN", 2); |
| 1724 | GRN_BULK_REWIND(&buf); |
| 1725 | grn_column_name_(ctx, columns[j], &buf); |
| 1726 | grn_output_obj(ctx, outbuf, output_type, &buf, NULL); |
| 1727 | /* column range */ |
| 1728 | range_id = grn_obj_get_range(ctx, columns[j]); |
| 1729 | if (range_id == GRN_ID_NIL) { |
| 1730 | GRN_TEXT_PUTS(ctx, outbuf, "null"); |
| 1731 | } else { |
| 1732 | int name_len; |
| 1733 | grn_obj *range_obj; |
| 1734 | char name_buf[GRN_TABLE_MAX_KEY_SIZE]; |
| 1735 | |
| 1736 | range_obj = grn_ctx_at(ctx, range_id); |
| 1737 | name_len = |
| 1738 | grn_obj_name(ctx, range_obj, name_buf, GRN_TABLE_MAX_KEY_SIZE); |
| 1739 | GRN_BULK_REWIND(&buf); |
| 1740 | GRN_TEXT_PUT(ctx, &buf, name_buf, name_len); |
| 1741 | grn_output_obj(ctx, outbuf, output_type, &buf, NULL); |
| 1742 | } |
| 1743 | grn_output_array_close(ctx, outbuf, output_type); |
| 1744 | } |
| 1745 | grn_output_array_close(ctx, outbuf, output_type); |
| 1746 | } |
| 1747 | grn_output_array_open(ctx, outbuf, output_type, "HIT", (int)ncolumns); |
| 1748 | { |
| 1749 | grn_output_table_data data; |
| 1750 | grn_output_table_data_init(ctx, &data, format); |
| 1751 | for (j = 0; j < ncolumns; j++) { |
| 1752 | grn_output_table_column_value(ctx, |
| 1753 | outbuf, |
| 1754 | output_type, |
| 1755 | columns[j], |
| 1756 | id, |
| 1757 | &data); |
| 1758 | } |
| 1759 | grn_output_table_data_fin(ctx, &data); |
| 1760 | } |
| 1761 | grn_output_array_close(ctx, outbuf, output_type); |
| 1762 | } else { |
| 1763 | grn_output_record(ctx, outbuf, output_type, bulk); |
| 1764 | } |
| 1765 | break; |
| 1766 | } |
| 1767 | GRN_OBJ_FIN(ctx, &buf); |
| 1768 | } |
| 1769 | |
| 1770 | static void |
| 1771 | grn_output_uvector_result_set(grn_ctx *ctx, |
| 1772 | grn_obj *outbuf, |
| 1773 | grn_content_type output_type, |
| 1774 | grn_obj *uvector, |
| 1775 | grn_obj_format *format) |
| 1776 | { |
| 1777 | uint32_t i_hit, n_hits; |
| 1778 | size_t i_column, n_columns; |
| 1779 | uint32_t n_elements; |
| 1780 | grn_obj **columns; |
| 1781 | grn_obj buf; |
| 1782 | bool with_column_names = false; |
| 1783 | |
| 1784 | n_hits = grn_vector_size(ctx, uvector); |
| 1785 | |
| 1786 | n_columns = GRN_BULK_VSIZE(&format->columns) / sizeof(grn_obj *); |
| 1787 | columns = (grn_obj **)GRN_BULK_HEAD(&format->columns); |
| 1788 | |
| 1789 | GRN_TEXT_INIT(&buf, 0); |
| 1790 | |
| 1791 | if (n_hits > 0 && format->flags & GRN_OBJ_FORMAT_WITH_COLUMN_NAMES) { |
| 1792 | with_column_names = true; |
| 1793 | } |
| 1794 | |
| 1795 | n_elements = 1; /* for NHITS */ |
| 1796 | if (with_column_names) { |
| 1797 | n_elements += 1; /* for COLUMNS */ |
| 1798 | } |
| 1799 | n_elements += n_hits; /* for HITS */ |
| 1800 | grn_output_array_open(ctx, outbuf, output_type, "RESULTSET", (int)n_elements); |
| 1801 | |
| 1802 | grn_output_array_open(ctx, outbuf, output_type, "NHITS", 1); |
| 1803 | grn_text_itoa(ctx, outbuf, (int)n_hits); |
| 1804 | grn_output_array_close(ctx, outbuf, output_type); |
| 1805 | |
| 1806 | if (with_column_names) { |
| 1807 | grn_output_array_open(ctx, outbuf, output_type, "COLUMNS", (int)n_columns); |
| 1808 | for (i_column = 0; i_column < n_columns; i_column++) { |
| 1809 | grn_id range_id; |
| 1810 | grn_output_array_open(ctx, outbuf, output_type, "COLUMN", 2); |
| 1811 | |
| 1812 | /* name */ |
| 1813 | GRN_BULK_REWIND(&buf); |
| 1814 | grn_column_name_(ctx, columns[i_column], &buf); |
| 1815 | grn_output_obj(ctx, outbuf, output_type, &buf, NULL); |
| 1816 | |
| 1817 | /* type */ |
| 1818 | range_id = grn_obj_get_range(ctx, columns[i_column]); |
| 1819 | if (range_id == GRN_ID_NIL) { |
| 1820 | GRN_TEXT_PUTS(ctx, outbuf, "null"); |
| 1821 | } else { |
| 1822 | int name_len; |
| 1823 | grn_obj *range_obj; |
| 1824 | char name_buf[GRN_TABLE_MAX_KEY_SIZE]; |
| 1825 | |
| 1826 | range_obj = grn_ctx_at(ctx, range_id); |
| 1827 | name_len = |
| 1828 | grn_obj_name(ctx, range_obj, name_buf, GRN_TABLE_MAX_KEY_SIZE); |
| 1829 | GRN_BULK_REWIND(&buf); |
| 1830 | GRN_TEXT_PUT(ctx, &buf, name_buf, name_len); |
| 1831 | grn_output_obj(ctx, outbuf, output_type, &buf, NULL); |
| 1832 | } |
| 1833 | |
| 1834 | grn_output_array_close(ctx, outbuf, output_type); |
| 1835 | } |
| 1836 | grn_output_array_close(ctx, outbuf, output_type); |
| 1837 | } |
| 1838 | |
| 1839 | for (i_hit = 0; i_hit < n_hits++; i_hit++) { |
| 1840 | grn_id id; |
| 1841 | |
| 1842 | id = grn_uvector_get_element(ctx, uvector, i_hit, NULL); |
| 1843 | grn_output_array_open(ctx, outbuf, output_type, "HITS", (int)n_columns); |
| 1844 | for (i_column = 0; i_column < n_columns; i_column++) { |
| 1845 | GRN_BULK_REWIND(&buf); |
| 1846 | grn_obj_get_value(ctx, columns[i_column], id, &buf); |
| 1847 | grn_output_obj(ctx, outbuf, output_type, &buf, NULL); |
| 1848 | } |
| 1849 | grn_output_array_close(ctx, outbuf, output_type); |
| 1850 | } |
| 1851 | |
| 1852 | grn_output_array_close(ctx, outbuf, output_type); |
| 1853 | |
| 1854 | GRN_OBJ_FIN(ctx, &buf); |
| 1855 | } |
| 1856 | |
| 1857 | static inline void |
| 1858 | grn_output_uvector_apache_arrow(grn_ctx *ctx, grn_obj *outbuf, grn_obj *uvector) |
| 1859 | { |
| 1860 | if (!ctx->impl->output.arrow_stream_writer) { |
| 1861 | return; |
| 1862 | } |
| 1863 | |
| 1864 | grn_arrow_stream_writer_add_column_uvector( |
| 1865 | ctx, |
| 1866 | ctx->impl->output.arrow_stream_writer, |
| 1867 | uvector); |
| 1868 | } |
| 1869 | |
| 1870 | static inline void |
| 1871 | grn_output_uvector(grn_ctx *ctx, |
| 1872 | grn_obj *outbuf, |
| 1873 | grn_content_type output_type, |
| 1874 | grn_obj *uvector, |
| 1875 | grn_obj_format *format) |
| 1876 | { |
| 1877 | bool output_result_set = false; |
| 1878 | bool with_weight = false; |
| 1879 | bool is_weight_float32 = false; |
| 1880 | |
| 1881 | if (format) { |
| 1882 | if (GRN_BULK_VSIZE(&(format->columns)) > 0) { |
| 1883 | output_result_set = true; |
| 1884 | } |
| 1885 | if (format->flags & GRN_OBJ_FORMAT_WITH_WEIGHT) { |
| 1886 | with_weight = true; |
| 1887 | } |
| 1888 | if (format->flags & GRN_OBJ_FORMAT_WEIGHT_FLOAT32) { |
| 1889 | is_weight_float32 = true; |
| 1890 | } |
| 1891 | } |
| 1892 | |
| 1893 | if (output_result_set) { |
| 1894 | grn_output_uvector_result_set(ctx, outbuf, output_type, uvector, format); |
| 1895 | return; |
| 1896 | } |
| 1897 | |
| 1898 | if (output_type == GRN_CONTENT_APACHE_ARROW) { |
| 1899 | grn_output_uvector_apache_arrow(ctx, outbuf, uvector); |
| 1900 | return; |
| 1901 | } |
| 1902 | |
| 1903 | if (grn_type_id_is_builtin(ctx, uvector->header.domain)) { |
| 1904 | size_t i, n; |
| 1905 | char *raw_elements; |
| 1906 | size_t element_size; |
| 1907 | grn_obj element; |
| 1908 | |
| 1909 | raw_elements = GRN_BULK_HEAD(uvector); |
| 1910 | element_size = grn_type_id_size(ctx, uvector->header.domain); |
| 1911 | n = GRN_BULK_VSIZE(uvector) / element_size; |
| 1912 | |
| 1913 | grn_output_array_open(ctx, outbuf, output_type, "VECTOR", (int)n); |
| 1914 | GRN_OBJ_INIT(&element, GRN_BULK, 0, uvector->header.domain); |
| 1915 | for (i = 0; i < n; i++) { |
| 1916 | GRN_BULK_REWIND(&element); |
| 1917 | grn_bulk_write_from(ctx, |
| 1918 | &element, |
| 1919 | raw_elements + (element_size * i), |
| 1920 | 0, |
| 1921 | element_size); |
| 1922 | grn_output_obj(ctx, outbuf, output_type, &element, NULL); |
| 1923 | } |
| 1924 | GRN_OBJ_FIN(ctx, &element); |
| 1925 | grn_output_array_close(ctx, outbuf, output_type); |
| 1926 | } else { |
| 1927 | grn_obj *range = grn_ctx_at(ctx, uvector->header.domain); |
| 1928 | |
| 1929 | uint32_t i, n; |
| 1930 | grn_obj id_value; |
| 1931 | grn_obj key_value; |
| 1932 | |
| 1933 | GRN_UINT32_INIT(&id_value, 0); |
| 1934 | GRN_OBJ_INIT(&key_value, GRN_BULK, 0, range->header.domain); |
| 1935 | |
| 1936 | n = grn_vector_size(ctx, uvector); |
| 1937 | if (with_weight) { |
| 1938 | grn_output_map_open(ctx, outbuf, output_type, "WEIGHT_VECTOR", (int)n); |
| 1939 | } else { |
| 1940 | grn_output_array_open(ctx, outbuf, output_type, "VECTOR", (int)n); |
| 1941 | } |
| 1942 | |
| 1943 | for (i = 0; i < n; i++) { |
| 1944 | grn_id id; |
| 1945 | float weight; |
| 1946 | |
| 1947 | id = grn_uvector_get_element_record(ctx, uvector, i, &weight); |
| 1948 | if (range->header.type == GRN_TABLE_NO_KEY) { |
| 1949 | GRN_UINT32_SET(ctx, &id_value, id); |
| 1950 | grn_output_obj(ctx, outbuf, output_type, &id_value, NULL); |
| 1951 | } else { |
| 1952 | GRN_BULK_REWIND(&key_value); |
| 1953 | grn_table_get_key2(ctx, range, id, &key_value); |
| 1954 | grn_output_obj(ctx, outbuf, output_type, &key_value, NULL); |
| 1955 | } |
| 1956 | |
| 1957 | if (with_weight) { |
| 1958 | if (is_weight_float32) { |
| 1959 | grn_output_float32(ctx, outbuf, output_type, weight); |
| 1960 | } else { |
| 1961 | grn_output_uint64(ctx, outbuf, output_type, (uint64_t)weight); |
| 1962 | } |
| 1963 | } |
| 1964 | } |
| 1965 | |
| 1966 | if (with_weight) { |
| 1967 | grn_output_map_close(ctx, outbuf, output_type); |
| 1968 | } else { |
| 1969 | grn_output_array_close(ctx, outbuf, output_type); |
| 1970 | } |
| 1971 | |
| 1972 | GRN_OBJ_FIN(ctx, &id_value); |
| 1973 | GRN_OBJ_FIN(ctx, &key_value); |
| 1974 | |
| 1975 | grn_obj_unref(ctx, range); |
| 1976 | } |
| 1977 | } |
| 1978 | |
| 1979 | static inline void |
| 1980 | grn_output_vector(grn_ctx *ctx, |
| 1981 | grn_obj *outbuf, |
| 1982 | grn_content_type output_type, |
| 1983 | grn_obj *vector, |
| 1984 | grn_obj_format *format) |
| 1985 | { |
| 1986 | bool with_weight = false; |
| 1987 | bool is_weight_float32 = false; |
| 1988 | |
| 1989 | if (format) { |
| 1990 | if (format->flags & GRN_OBJ_FORMAT_WITH_WEIGHT) { |
| 1991 | with_weight = true; |
| 1992 | } |
| 1993 | if (format->flags & GRN_OBJ_FORMAT_WEIGHT_FLOAT32) { |
| 1994 | is_weight_float32 = true; |
| 1995 | } |
| 1996 | } |
| 1997 | |
| 1998 | if (with_weight) { |
| 1999 | uint32_t i, n; |
| 2000 | grn_obj value; |
| 2001 | |
| 2002 | GRN_VOID_INIT(&value); |
| 2003 | n = grn_vector_size(ctx, vector); |
| 2004 | grn_output_map_open(ctx, outbuf, output_type, "WEIGHT_VECTOR", (int)n); |
| 2005 | for (i = 0; i < n; i++) { |
| 2006 | const char *_value; |
| 2007 | uint32_t weight; |
| 2008 | float weight_float; |
| 2009 | unsigned int length; |
| 2010 | grn_id domain; |
| 2011 | |
| 2012 | if (is_weight_float32) { |
| 2013 | length = grn_vector_get_element_float(ctx, |
| 2014 | vector, |
| 2015 | i, |
| 2016 | &_value, |
| 2017 | &weight_float, |
| 2018 | &domain); |
| 2019 | } else { |
| 2020 | length = |
| 2021 | grn_vector_get_element(ctx, vector, i, &_value, &weight, &domain); |
| 2022 | } |
| 2023 | |
| 2024 | if (domain != GRN_DB_VOID) { |
| 2025 | grn_obj_reinit(ctx, &value, domain, 0); |
| 2026 | } else { |
| 2027 | grn_obj_reinit(ctx, &value, vector->header.domain, 0); |
| 2028 | } |
| 2029 | grn_bulk_write(ctx, &value, _value, length); |
| 2030 | grn_output_obj(ctx, outbuf, output_type, &value, NULL); |
| 2031 | if (is_weight_float32) { |
| 2032 | grn_output_float32(ctx, outbuf, output_type, weight_float); |
| 2033 | } else { |
| 2034 | grn_output_uint64(ctx, outbuf, output_type, weight); |
| 2035 | } |
| 2036 | } |
| 2037 | grn_output_map_close(ctx, outbuf, output_type); |
| 2038 | GRN_OBJ_FIN(ctx, &value); |
| 2039 | } else { |
| 2040 | uint32_t i, n; |
| 2041 | grn_obj value; |
| 2042 | GRN_VOID_INIT(&value); |
| 2043 | n = grn_vector_size(ctx, vector); |
| 2044 | grn_output_array_open(ctx, outbuf, output_type, "VECTOR", (int)n); |
| 2045 | for (i = 0; i < n; i++) { |
| 2046 | const char *_value; |
| 2047 | uint32_t weight; |
| 2048 | unsigned int length; |
| 2049 | grn_id domain; |
| 2050 | |
| 2051 | length = |
| 2052 | grn_vector_get_element(ctx, vector, i, &_value, &weight, &domain); |
| 2053 | if (domain != GRN_DB_VOID) { |
| 2054 | grn_obj_reinit(ctx, &value, domain, 0); |
| 2055 | } else { |
| 2056 | grn_obj_reinit(ctx, &value, vector->header.domain, 0); |
| 2057 | } |
| 2058 | grn_bulk_write(ctx, &value, _value, length); |
| 2059 | grn_output_obj(ctx, outbuf, output_type, &value, NULL); |
| 2060 | } |
| 2061 | grn_output_array_close(ctx, outbuf, output_type); |
| 2062 | GRN_OBJ_FIN(ctx, &value); |
| 2063 | } |
| 2064 | } |
| 2065 | |
| 2066 | static inline void |
| 2067 | grn_output_pvector(grn_ctx *ctx, |
| 2068 | grn_obj *outbuf, |
| 2069 | grn_content_type output_type, |
| 2070 | grn_obj *pvector, |
| 2071 | grn_obj_format *format) |
| 2072 | { |
| 2073 | if (format) { |
| 2074 | ERR(GRN_FUNCTION_NOT_IMPLEMENTED, |
| 2075 | "cannot print GRN_PVECTOR using grn_obj_format"); |
| 2076 | } else { |
| 2077 | size_t i, n; |
| 2078 | grn_output_array_open(ctx, outbuf, output_type, "VECTOR", -1); |
| 2079 | n = GRN_PTR_VECTOR_SIZE(pvector); |
| 2080 | for (i = 0; i < n; i++) { |
| 2081 | grn_obj *value; |
| 2082 | |
| 2083 | value = GRN_PTR_VALUE_AT(pvector, i); |
| 2084 | grn_output_obj(ctx, outbuf, output_type, value, NULL); |
| 2085 | } |
| 2086 | grn_output_array_close(ctx, outbuf, output_type); |
| 2087 | } |
| 2088 | } |
| 2089 | |
| 2090 | static inline void |
| 2091 | grn_output_result_set_n_hits_v1(grn_ctx *ctx, |
| 2092 | grn_obj *outbuf, |
| 2093 | grn_content_type output_type, |
| 2094 | grn_obj_format *format) |
| 2095 | { |
| 2096 | grn_output_array_open(ctx, outbuf, output_type, "NHITS", 1); |
| 2097 | if (output_type == GRN_CONTENT_XML) { |
| 2098 | grn_text_itoa(ctx, outbuf, format->nhits); |
| 2099 | } else { |
| 2100 | grn_output_int32(ctx, outbuf, output_type, format->nhits); |
| 2101 | } |
| 2102 | grn_output_array_close(ctx, outbuf, output_type); |
| 2103 | } |
| 2104 | |
| 2105 | static inline void |
| 2106 | grn_output_result_set_n_hits_v3(grn_ctx *ctx, |
| 2107 | grn_obj *outbuf, |
| 2108 | grn_content_type output_type, |
| 2109 | grn_obj_format *format) |
| 2110 | { |
| 2111 | grn_output_cstr(ctx, outbuf, output_type, "n_hits"); |
| 2112 | grn_output_int32(ctx, outbuf, output_type, format->nhits); |
| 2113 | } |
| 2114 | |
| 2115 | static inline void |
| 2116 | grn_output_result_set_n_hits(grn_ctx *ctx, |
| 2117 | grn_obj *outbuf, |
| 2118 | grn_content_type output_type, |
| 2119 | grn_obj_format *format) |
| 2120 | { |
| 2121 | if (format->nhits == -1) { |
| 2122 | return; |
| 2123 | } |
| 2124 | |
| 2125 | if (output_type == GRN_CONTENT_APACHE_ARROW) { |
| 2126 | grn_obj buffer; |
| 2127 | GRN_TEXT_INIT(&buffer, 0); |
| 2128 | grn_text_itoa(ctx, &buffer, format->nhits); |
| 2129 | GRN_TEXT_PUTC(ctx, &buffer, '\0'); |
| 2130 | grn_arrow_stream_writer_add_metadata(ctx, |
| 2131 | ctx->impl->output.arrow_stream_writer, |
| 2132 | "GROONGA:n_hits", |
| 2133 | GRN_TEXT_VALUE(&buffer)); |
| 2134 | GRN_OBJ_FIN(ctx, &buffer); |
| 2135 | return; |
| 2136 | } |
| 2137 | |
| 2138 | if (grn_ctx_get_command_version(ctx) < GRN_COMMAND_VERSION_3) { |
| 2139 | grn_output_result_set_n_hits_v1(ctx, outbuf, output_type, format); |
| 2140 | } else { |
| 2141 | grn_output_result_set_n_hits_v3(ctx, outbuf, output_type, format); |
| 2142 | } |
| 2143 | } |
| 2144 | |
| 2145 | static inline void |
| 2146 | grn_output_table_column_info(grn_ctx *ctx, |
| 2147 | grn_obj *outbuf, |
| 2148 | grn_content_type output_type, |
| 2149 | const char *name, |
| 2150 | const char *type, |
| 2151 | grn_obj *column) |
| 2152 | { |
| 2153 | if (output_type == GRN_CONTENT_APACHE_ARROW) { |
| 2154 | grn_arrow_stream_writer_add_field(ctx, |
| 2155 | ctx->impl->output.arrow_stream_writer, |
| 2156 | name, |
| 2157 | column); |
| 2158 | return; |
| 2159 | } |
| 2160 | |
| 2161 | if (grn_ctx_get_command_version(ctx) < GRN_COMMAND_VERSION_3) { |
| 2162 | grn_output_array_open(ctx, outbuf, output_type, "COLUMN", 2); |
| 2163 | if (name) { |
| 2164 | grn_output_cstr(ctx, outbuf, output_type, name); |
| 2165 | } else { |
| 2166 | grn_output_null(ctx, outbuf, output_type); |
| 2167 | } |
| 2168 | if (type) { |
| 2169 | grn_output_cstr(ctx, outbuf, output_type, type); |
| 2170 | } else { |
| 2171 | grn_output_null(ctx, outbuf, output_type); |
| 2172 | } |
| 2173 | grn_output_array_close(ctx, outbuf, output_type); |
| 2174 | } else { |
| 2175 | grn_output_map_open(ctx, outbuf, output_type, "column", 2); |
| 2176 | grn_output_cstr(ctx, outbuf, output_type, "name"); |
| 2177 | if (name) { |
| 2178 | grn_output_cstr(ctx, outbuf, output_type, name); |
| 2179 | } else { |
| 2180 | grn_output_null(ctx, outbuf, output_type); |
| 2181 | } |
| 2182 | grn_output_cstr(ctx, outbuf, output_type, "type"); |
| 2183 | if (type) { |
| 2184 | grn_output_cstr(ctx, outbuf, output_type, type); |
| 2185 | } else { |
| 2186 | grn_output_null(ctx, outbuf, output_type); |
| 2187 | } |
| 2188 | grn_output_map_close(ctx, outbuf, output_type); |
| 2189 | } |
| 2190 | } |
| 2191 | |
| 2192 | static bool |
| 2193 | is_score_accessor(grn_ctx *ctx, grn_obj *obj) |
| 2194 | { |
| 2195 | grn_accessor *a; |
| 2196 | |
| 2197 | if (obj->header.type != GRN_ACCESSOR) { |
| 2198 | return false; |
| 2199 | } |
| 2200 | |
| 2201 | for (a = (grn_accessor *)obj; a->next; a = a->next) { |
| 2202 | } |
| 2203 | return a->action == GRN_ACCESSOR_GET_SCORE; |
| 2204 | } |
| 2205 | |
| 2206 | static inline void |
| 2207 | grn_output_table_column(grn_ctx *ctx, |
| 2208 | grn_obj *outbuf, |
| 2209 | grn_content_type output_type, |
| 2210 | grn_obj *column, |
| 2211 | grn_obj *buf) |
| 2212 | { |
| 2213 | grn_id range_id = GRN_ID_NIL; |
| 2214 | |
| 2215 | if (!column) { |
| 2216 | grn_output_table_column_info(ctx, outbuf, output_type, NULL, NULL, NULL); |
| 2217 | return; |
| 2218 | } |
| 2219 | |
| 2220 | GRN_BULK_REWIND(buf); |
| 2221 | grn_column_name_(ctx, column, buf); |
| 2222 | GRN_TEXT_PUTC(ctx, buf, '\0'); |
| 2223 | |
| 2224 | if (column->header.type == GRN_COLUMN_INDEX) { |
| 2225 | range_id = GRN_DB_UINT32; |
| 2226 | } else if (is_score_accessor(ctx, column)) { |
| 2227 | if (grn_ctx_get_command_version(ctx) == GRN_COMMAND_VERSION_1) { |
| 2228 | range_id = GRN_DB_INT32; |
| 2229 | } else { |
| 2230 | range_id = GRN_DB_FLOAT; |
| 2231 | } |
| 2232 | } |
| 2233 | if (range_id == GRN_ID_NIL) { |
| 2234 | range_id = grn_obj_get_range(ctx, column); |
| 2235 | } |
| 2236 | if (range_id == GRN_ID_NIL) { |
| 2237 | grn_output_table_column_info(ctx, |
| 2238 | outbuf, |
| 2239 | output_type, |
| 2240 | GRN_TEXT_VALUE(buf), |
| 2241 | NULL, |
| 2242 | column); |
| 2243 | } else { |
| 2244 | grn_obj *range_obj; |
| 2245 | char type_name[GRN_TABLE_MAX_KEY_SIZE]; |
| 2246 | int type_name_len; |
| 2247 | |
| 2248 | range_obj = grn_ctx_at(ctx, range_id); |
| 2249 | type_name_len = |
| 2250 | grn_obj_name(ctx, range_obj, type_name, GRN_TABLE_MAX_KEY_SIZE); |
| 2251 | type_name[type_name_len] = '\0'; |
| 2252 | grn_output_table_column_info(ctx, |
| 2253 | outbuf, |
| 2254 | output_type, |
| 2255 | GRN_TEXT_VALUE(buf), |
| 2256 | type_name, |
| 2257 | column); |
| 2258 | grn_obj_unref(ctx, range_obj); |
| 2259 | } |
| 2260 | } |
| 2261 | |
| 2262 | static inline void |
| 2263 | grn_output_table_column_by_expression(grn_ctx *ctx, |
| 2264 | grn_obj *outbuf, |
| 2265 | grn_content_type output_type, |
| 2266 | grn_obj *expr, |
| 2267 | grn_obj *buf) |
| 2268 | { |
| 2269 | grn_expr *e = (grn_expr *)expr; |
| 2270 | if (e->codes_curr == 1 && e->codes[0].op == GRN_OP_GET_VALUE) { |
| 2271 | grn_output_table_column(ctx, outbuf, output_type, e->codes[0].value, buf); |
| 2272 | return; |
| 2273 | } else if (e->codes_curr == 3 && e->codes[2].op == GRN_OP_GET_MEMBER) { |
| 2274 | GRN_BULK_REWIND(buf); |
| 2275 | grn_column_name_(ctx, e->codes[0].value, buf); |
| 2276 | GRN_TEXT_PUTC(ctx, buf, '['); |
| 2277 | grn_inspect(ctx, buf, e->codes[1].value); |
| 2278 | GRN_TEXT_PUTC(ctx, buf, ']'); |
| 2279 | GRN_TEXT_PUTC(ctx, buf, '\0'); |
| 2280 | |
| 2281 | grn_output_table_column_info(ctx, |
| 2282 | outbuf, |
| 2283 | output_type, |
| 2284 | GRN_TEXT_VALUE(buf), |
| 2285 | NULL, |
| 2286 | e->codes[0].value); |
| 2287 | return; |
| 2288 | } |
| 2289 | |
| 2290 | if (grn_ctx_get_command_version(ctx) < GRN_COMMAND_VERSION_3) { |
| 2291 | grn_obj *column = NULL; |
| 2292 | if (e->codes_curr > 0) { |
| 2293 | column = e->codes[0].value; |
| 2294 | } |
| 2295 | grn_output_table_column(ctx, outbuf, output_type, column, buf); |
| 2296 | return; |
| 2297 | } |
| 2298 | |
| 2299 | grn_id range_id = grn_obj_get_range(ctx, expr); |
| 2300 | grn_obj *range_obj = NULL; |
| 2301 | char type_name[GRN_TABLE_MAX_KEY_SIZE]; |
| 2302 | int type_name_len = 0; |
| 2303 | grn_obj name_buffer; |
| 2304 | GRN_TEXT_INIT(&name_buffer, 0); |
| 2305 | const char *name = NULL; |
| 2306 | |
| 2307 | if (range_id != GRN_ID_NIL) { |
| 2308 | range_obj = grn_ctx_at(ctx, range_id); |
| 2309 | type_name_len = |
| 2310 | grn_obj_name(ctx, range_obj, type_name, GRN_TABLE_MAX_KEY_SIZE); |
| 2311 | type_name[type_name_len] = '\0'; |
| 2312 | } |
| 2313 | grn_expr_to_script_syntax(ctx, expr, &name_buffer); |
| 2314 | if (GRN_TEXT_LEN(&name_buffer) > 0) { |
| 2315 | GRN_TEXT_PUTC(ctx, &name_buffer, '\0'); |
| 2316 | name = GRN_TEXT_VALUE(&name_buffer); |
| 2317 | } |
| 2318 | grn_output_table_column_info(ctx, |
| 2319 | outbuf, |
| 2320 | output_type, |
| 2321 | name, |
| 2322 | type_name_len > 0 ? type_name : NULL, |
| 2323 | expr); |
| 2324 | if (range_obj) { |
| 2325 | grn_obj_unref(ctx, range_obj); |
| 2326 | } |
| 2327 | GRN_OBJ_FIN(ctx, &name_buffer); |
| 2328 | } |
| 2329 | |
| 2330 | static inline void |
| 2331 | grn_output_table_columns_open(grn_ctx *ctx, |
| 2332 | grn_obj *outbuf, |
| 2333 | grn_content_type output_type, |
| 2334 | int n_columns) |
| 2335 | { |
| 2336 | if (output_type == GRN_CONTENT_APACHE_ARROW) { |
| 2337 | return; |
| 2338 | } |
| 2339 | |
| 2340 | if (grn_ctx_get_command_version(ctx) < GRN_COMMAND_VERSION_3) { |
| 2341 | grn_output_array_open(ctx, outbuf, output_type, "COLUMNS", n_columns); |
| 2342 | } else { |
| 2343 | grn_output_cstr(ctx, outbuf, output_type, "columns"); |
| 2344 | grn_output_array_open(ctx, outbuf, output_type, "columns", n_columns); |
| 2345 | } |
| 2346 | } |
| 2347 | |
| 2348 | static inline void |
| 2349 | grn_output_table_columns_close(grn_ctx *ctx, |
| 2350 | grn_obj *outbuf, |
| 2351 | grn_content_type output_type) |
| 2352 | { |
| 2353 | if (output_type == GRN_CONTENT_APACHE_ARROW) { |
| 2354 | grn_arrow_stream_writer_write_schema(ctx, |
| 2355 | ctx->impl->output.arrow_stream_writer); |
| 2356 | return; |
| 2357 | } |
| 2358 | |
| 2359 | if (grn_ctx_get_command_version(ctx) < GRN_COMMAND_VERSION_3) { |
| 2360 | grn_output_array_close(ctx, outbuf, output_type); |
| 2361 | } else { |
| 2362 | grn_output_array_close(ctx, outbuf, output_type); |
| 2363 | } |
| 2364 | } |
| 2365 | |
| 2366 | static inline void |
| 2367 | grn_output_table_columns_by_expression(grn_ctx *ctx, |
| 2368 | grn_obj *outbuf, |
| 2369 | grn_content_type output_type, |
| 2370 | grn_obj *table, |
| 2371 | grn_obj_format *format, |
| 2372 | grn_obj *buf) |
| 2373 | { |
| 2374 | grn_expr *expr = (grn_expr *)format->expression; |
| 2375 | grn_obj offsets; |
| 2376 | size_t i, n; |
| 2377 | |
| 2378 | GRN_UINT32_INIT(&offsets, GRN_OBJ_VECTOR); |
| 2379 | grn_output_columns_get_offsets(ctx, format->expression, &offsets); |
| 2380 | |
| 2381 | n = GRN_UINT32_VECTOR_SIZE(&offsets) / 2; |
| 2382 | |
| 2383 | grn_output_table_columns_open(ctx, outbuf, output_type, (int)n); |
| 2384 | |
| 2385 | grn_expr_code *codes = expr->codes; |
| 2386 | uint32_t codes_curr = expr->codes_curr; |
| 2387 | for (i = 0; i < n; i++) { |
| 2388 | uint32_t code_start_offset = GRN_UINT32_VALUE_AT(&offsets, i * 2); |
| 2389 | uint32_t code_end_offset = GRN_UINT32_VALUE_AT(&offsets, i * 2 + 1); |
| 2390 | expr->codes = codes + code_start_offset; |
| 2391 | expr->codes_curr = code_end_offset - code_start_offset; |
| 2392 | grn_output_table_column_by_expression(ctx, |
| 2393 | outbuf, |
| 2394 | output_type, |
| 2395 | (grn_obj *)expr, |
| 2396 | buf); |
| 2397 | } |
| 2398 | expr->codes = codes; |
| 2399 | expr->codes_curr = codes_curr; |
| 2400 | |
| 2401 | grn_output_table_columns_close(ctx, outbuf, output_type); |
| 2402 | |
| 2403 | GRN_OBJ_FIN(ctx, &offsets); |
| 2404 | } |
| 2405 | |
| 2406 | static inline void |
| 2407 | grn_output_table_columns_by_columns(grn_ctx *ctx, |
| 2408 | grn_obj *outbuf, |
| 2409 | grn_content_type output_type, |
| 2410 | grn_obj *table, |
| 2411 | grn_obj_format *format, |
| 2412 | grn_obj *buf) |
| 2413 | { |
| 2414 | size_t i; |
| 2415 | size_t ncolumns = GRN_PTR_VECTOR_SIZE(&format->columns); |
| 2416 | grn_obj **columns = (grn_obj **)GRN_BULK_HEAD(&format->columns); |
| 2417 | |
| 2418 | grn_output_table_columns_open(ctx, outbuf, output_type, (int)ncolumns); |
| 2419 | for (i = 0; i < ncolumns; i++) { |
| 2420 | grn_output_table_column(ctx, outbuf, output_type, columns[i], buf); |
| 2421 | } |
| 2422 | grn_output_table_columns_close(ctx, outbuf, output_type); |
| 2423 | } |
| 2424 | |
| 2425 | void |
| 2426 | grn_output_table_columns(grn_ctx *ctx, |
| 2427 | grn_obj *outbuf, |
| 2428 | grn_content_type output_type, |
| 2429 | grn_obj *table, |
| 2430 | grn_obj_format *format) |
| 2431 | { |
| 2432 | grn_obj buf; |
| 2433 | |
| 2434 | GRN_TEXT_INIT(&buf, 0); |
| 2435 | if (format->expression) { |
| 2436 | grn_output_table_columns_by_expression(ctx, |
| 2437 | outbuf, |
| 2438 | output_type, |
| 2439 | table, |
| 2440 | format, |
| 2441 | &buf); |
| 2442 | } else { |
| 2443 | grn_output_table_columns_by_columns(ctx, |
| 2444 | outbuf, |
| 2445 | output_type, |
| 2446 | table, |
| 2447 | format, |
| 2448 | &buf); |
| 2449 | } |
| 2450 | GRN_OBJ_FIN(ctx, &buf); |
| 2451 | } |
| 2452 | |
| 2453 | static inline void |
| 2454 | grn_output_table_record_open(grn_ctx *ctx, |
| 2455 | grn_obj *outbuf, |
| 2456 | grn_content_type output_type, |
| 2457 | int n_columns) |
| 2458 | { |
| 2459 | if (output_type == GRN_CONTENT_APACHE_ARROW) { |
| 2460 | grn_arrow_stream_writer_open_record(ctx, |
| 2461 | ctx->impl->output.arrow_stream_writer); |
| 2462 | return; |
| 2463 | } |
| 2464 | |
| 2465 | if (grn_ctx_get_command_version(ctx) < GRN_COMMAND_VERSION_3) { |
| 2466 | grn_output_array_open(ctx, outbuf, output_type, "HIT", n_columns); |
| 2467 | } else { |
| 2468 | grn_output_array_open(ctx, outbuf, output_type, "record", n_columns); |
| 2469 | } |
| 2470 | } |
| 2471 | |
| 2472 | static inline void |
| 2473 | grn_output_table_record_close(grn_ctx *ctx, |
| 2474 | grn_obj *outbuf, |
| 2475 | grn_content_type output_type) |
| 2476 | { |
| 2477 | if (output_type == GRN_CONTENT_APACHE_ARROW) { |
| 2478 | grn_arrow_stream_writer_close_record(ctx, |
| 2479 | ctx->impl->output.arrow_stream_writer); |
| 2480 | return; |
| 2481 | } |
| 2482 | |
| 2483 | if (grn_ctx_get_command_version(ctx) < GRN_COMMAND_VERSION_3) { |
| 2484 | grn_output_array_close(ctx, outbuf, output_type); |
| 2485 | } else { |
| 2486 | grn_output_array_close(ctx, outbuf, output_type); |
| 2487 | } |
| 2488 | } |
| 2489 | |
| 2490 | static inline void |
| 2491 | grn_output_table_record_by_column(grn_ctx *ctx, |
| 2492 | grn_obj *outbuf, |
| 2493 | grn_content_type output_type, |
| 2494 | grn_obj *column, |
| 2495 | grn_id id, |
| 2496 | grn_output_table_data *data) |
| 2497 | { |
| 2498 | grn_output_table_column_value(ctx, outbuf, output_type, column, id, data); |
| 2499 | } |
| 2500 | |
| 2501 | static inline void |
| 2502 | grn_output_table_record_by_expression(grn_ctx *ctx, |
| 2503 | grn_obj *outbuf, |
| 2504 | grn_content_type output_type, |
| 2505 | grn_obj *expression, |
| 2506 | uint32_t code_start_offset, |
| 2507 | uint32_t code_end_offset, |
| 2508 | grn_obj *record, |
| 2509 | grn_output_table_data *data) |
| 2510 | { |
| 2511 | grn_expr *expr = (grn_expr *)expression; |
| 2512 | grn_expr_code *codes = expr->codes; |
| 2513 | uint32_t codes_curr = expr->codes_curr; |
| 2514 | |
| 2515 | expr->codes += code_start_offset; |
| 2516 | expr->codes_curr = code_end_offset - code_start_offset; |
| 2517 | |
| 2518 | if (expr->codes_curr == 1 && expr->codes[0].op == GRN_OP_GET_VALUE) { |
| 2519 | grn_obj *column = expr->codes[0].value; |
| 2520 | grn_output_table_record_by_column(ctx, |
| 2521 | outbuf, |
| 2522 | output_type, |
| 2523 | column, |
| 2524 | GRN_RECORD_VALUE(record), |
| 2525 | data); |
| 2526 | } else { |
| 2527 | grn_obj *result; |
| 2528 | result = grn_expr_exec(ctx, expression, 0); |
| 2529 | if (result) { |
| 2530 | grn_output_obj(ctx, outbuf, output_type, result, NULL); |
| 2531 | } else { |
| 2532 | grn_output_cstr(ctx, outbuf, output_type, ctx->errbuf); |
| 2533 | } |
| 2534 | } |
| 2535 | |
| 2536 | expr->codes = codes; |
| 2537 | expr->codes_curr = codes_curr; |
| 2538 | } |
| 2539 | |
| 2540 | static inline void |
| 2541 | grn_output_table_records_by_expression(grn_ctx *ctx, |
| 2542 | grn_obj *outbuf, |
| 2543 | grn_content_type output_type, |
| 2544 | grn_table_cursor *tc, |
| 2545 | grn_obj_format *format, |
| 2546 | grn_output_table_data *data) |
| 2547 | { |
| 2548 | grn_obj offsets; |
| 2549 | size_t n; |
| 2550 | grn_id id; |
| 2551 | grn_obj *record; |
| 2552 | |
| 2553 | GRN_UINT32_INIT(&offsets, GRN_OBJ_VECTOR); |
| 2554 | grn_output_columns_get_offsets(ctx, format->expression, &offsets); |
| 2555 | n = GRN_UINT32_VECTOR_SIZE(&offsets) / 2; |
| 2556 | record = grn_expr_get_var_by_offset(ctx, format->expression, 0); |
| 2557 | while ((id = grn_table_cursor_next(ctx, tc)) != GRN_ID_NIL) { |
| 2558 | grn_output_table_data_open_record(ctx, data); |
| 2559 | GRN_RECORD_SET(ctx, record, id); |
| 2560 | grn_output_table_record_open(ctx, outbuf, output_type, (int)n); |
| 2561 | size_t i; |
| 2562 | for (i = 0; i < n; i++) { |
| 2563 | uint32_t code_start_offset; |
| 2564 | uint32_t code_end_offset; |
| 2565 | |
| 2566 | code_start_offset = GRN_UINT32_VALUE_AT(&offsets, i * 2); |
| 2567 | code_end_offset = GRN_UINT32_VALUE_AT(&offsets, i * 2 + 1); |
| 2568 | grn_output_table_record_by_expression(ctx, |
| 2569 | outbuf, |
| 2570 | output_type, |
| 2571 | format->expression, |
| 2572 | code_start_offset, |
| 2573 | code_end_offset, |
| 2574 | record, |
| 2575 | data); |
| 2576 | } |
| 2577 | grn_output_table_record_close(ctx, outbuf, output_type); |
| 2578 | grn_output_table_data_close_record(ctx, data); |
| 2579 | } |
| 2580 | |
| 2581 | GRN_OBJ_FIN(ctx, &offsets); |
| 2582 | } |
| 2583 | |
| 2584 | static inline void |
| 2585 | grn_output_table_records_by_columns(grn_ctx *ctx, |
| 2586 | grn_obj *outbuf, |
| 2587 | grn_content_type output_type, |
| 2588 | grn_table_cursor *tc, |
| 2589 | grn_obj_format *format, |
| 2590 | grn_output_table_data *data) |
| 2591 | { |
| 2592 | size_t i; |
| 2593 | grn_id id; |
| 2594 | size_t ncolumns = GRN_PTR_VECTOR_SIZE(&format->columns); |
| 2595 | grn_obj **columns = (grn_obj **)GRN_BULK_HEAD(&format->columns); |
| 2596 | while (ctx->rc == GRN_SUCCESS && |
| 2597 | (id = grn_table_cursor_next(ctx, tc)) != GRN_ID_NIL) { |
| 2598 | grn_output_table_data_open_record(ctx, data); |
| 2599 | grn_output_table_record_open(ctx, outbuf, output_type, (int)ncolumns); |
| 2600 | for (i = 0; i < ncolumns; i++) { |
| 2601 | grn_output_table_record_by_column(ctx, |
| 2602 | outbuf, |
| 2603 | output_type, |
| 2604 | columns[i], |
| 2605 | id, |
| 2606 | data); |
| 2607 | } |
| 2608 | grn_output_table_record_close(ctx, outbuf, output_type); |
| 2609 | grn_output_table_data_close_record(ctx, data); |
| 2610 | } |
| 2611 | } |
| 2612 | |
| 2613 | void |
| 2614 | grn_output_table_records_open(grn_ctx *ctx, |
| 2615 | grn_obj *outbuf, |
| 2616 | grn_content_type output_type, |
| 2617 | int n_records) |
| 2618 | { |
| 2619 | if (output_type == GRN_CONTENT_APACHE_ARROW) { |
| 2620 | return; |
| 2621 | } |
| 2622 | |
| 2623 | if (grn_ctx_get_command_version(ctx) >= GRN_COMMAND_VERSION_3) { |
| 2624 | grn_output_cstr(ctx, outbuf, output_type, "records"); |
| 2625 | grn_output_array_open(ctx, outbuf, output_type, "records", n_records); |
| 2626 | } |
| 2627 | } |
| 2628 | |
| 2629 | static void |
| 2630 | grn_output_table_keys_open(grn_ctx *ctx, |
| 2631 | grn_obj *outbuf, |
| 2632 | grn_content_type output_type, |
| 2633 | int n_records) |
| 2634 | { |
| 2635 | if (output_type == GRN_CONTENT_APACHE_ARROW) { |
| 2636 | return; |
| 2637 | } |
| 2638 | |
| 2639 | if (grn_ctx_get_command_version(ctx) >= GRN_COMMAND_VERSION_3) { |
| 2640 | grn_output_cstr(ctx, outbuf, output_type, "keys"); |
| 2641 | grn_output_array_open(ctx, outbuf, output_type, "keys", n_records); |
| 2642 | } |
| 2643 | } |
| 2644 | |
| 2645 | void |
| 2646 | grn_output_table_records_close(grn_ctx *ctx, |
| 2647 | grn_obj *outbuf, |
| 2648 | grn_content_type output_type) |
| 2649 | { |
| 2650 | if (output_type == GRN_CONTENT_APACHE_ARROW) { |
| 2651 | return; |
| 2652 | } |
| 2653 | |
| 2654 | if (grn_ctx_get_command_version(ctx) >= GRN_COMMAND_VERSION_3) { |
| 2655 | grn_output_array_close(ctx, outbuf, output_type); |
| 2656 | } |
| 2657 | } |
| 2658 | |
| 2659 | void |
| 2660 | grn_output_table_records_content(grn_ctx *ctx, |
| 2661 | grn_obj *outbuf, |
| 2662 | grn_content_type output_type, |
| 2663 | grn_obj *table, |
| 2664 | grn_obj_format *format) |
| 2665 | { |
| 2666 | if (format) { |
| 2667 | grn_table_cursor *tc = grn_table_cursor_open(ctx, |
| 2668 | table, |
| 2669 | NULL, |
| 2670 | 0, |
| 2671 | NULL, |
| 2672 | 0, |
| 2673 | format->offset, |
| 2674 | format->limit, |
| 2675 | GRN_CURSOR_ASCENDING); |
| 2676 | if (!tc) { |
| 2677 | ERRCLR(ctx); |
| 2678 | return; |
| 2679 | } |
| 2680 | grn_output_table_data data; |
| 2681 | grn_output_table_data_init(ctx, &data, format); |
| 2682 | if (format->expression) { |
| 2683 | grn_output_table_records_by_expression(ctx, |
| 2684 | outbuf, |
| 2685 | output_type, |
| 2686 | tc, |
| 2687 | format, |
| 2688 | &data); |
| 2689 | } else { |
| 2690 | grn_output_table_records_by_columns(ctx, |
| 2691 | outbuf, |
| 2692 | output_type, |
| 2693 | tc, |
| 2694 | format, |
| 2695 | &data); |
| 2696 | } |
| 2697 | grn_output_table_data_fin(ctx, &data); |
| 2698 | grn_table_cursor_close(ctx, tc); |
| 2699 | } else { |
| 2700 | grn_obj *column = |
| 2701 | grn_obj_column(ctx, table, GRN_COLUMN_NAME_KEY, GRN_COLUMN_NAME_KEY_LEN); |
| 2702 | grn_obj buf; |
| 2703 | GRN_TEXT_INIT(&buf, 0); |
| 2704 | GRN_TABLE_EACH_BEGIN(ctx, table, cursor, id) |
| 2705 | { |
| 2706 | GRN_BULK_REWIND(&buf); |
| 2707 | grn_obj_get_value(ctx, column, id, &buf); |
| 2708 | grn_text_esc(ctx, outbuf, GRN_BULK_HEAD(&buf), GRN_BULK_VSIZE(&buf)); |
| 2709 | } |
| 2710 | GRN_TABLE_EACH_END(ctx, cursor); |
| 2711 | GRN_OBJ_FIN(ctx, &buf); |
| 2712 | grn_obj_unlink(ctx, column); |
| 2713 | } |
| 2714 | } |
| 2715 | |
| 2716 | void |
| 2717 | grn_output_table_records(grn_ctx *ctx, |
| 2718 | grn_obj *outbuf, |
| 2719 | grn_content_type output_type, |
| 2720 | grn_obj *table, |
| 2721 | grn_obj_format *format) |
| 2722 | { |
| 2723 | if (format) { |
| 2724 | grn_output_table_records_open(ctx, outbuf, output_type, format->limit); |
| 2725 | } else { |
| 2726 | unsigned int n_records = grn_table_size(ctx, table); |
| 2727 | grn_output_table_keys_open(ctx, outbuf, output_type, (int)n_records); |
| 2728 | } |
| 2729 | grn_output_table_records_content(ctx, outbuf, output_type, table, format); |
| 2730 | grn_output_table_records_close(ctx, outbuf, output_type); |
| 2731 | } |
| 2732 | |
| 2733 | static void |
| 2734 | grn_output_result_set_open_metadata_v1(grn_ctx *ctx, |
| 2735 | grn_obj *outbuf, |
| 2736 | grn_content_type output_type, |
| 2737 | grn_obj *table, |
| 2738 | grn_obj_format *format, |
| 2739 | uint32_t n_additional_elements) |
| 2740 | { |
| 2741 | if (format) { |
| 2742 | int resultset_size = 1; |
| 2743 | /* resultset: [NHITS, (COLUMNS), (HITS)] */ |
| 2744 | if (format->flags & GRN_OBJ_FORMAT_WITH_COLUMN_NAMES) { |
| 2745 | resultset_size++; |
| 2746 | } |
| 2747 | resultset_size += format->limit; |
| 2748 | resultset_size += (int)n_additional_elements; |
| 2749 | grn_output_array_open(ctx, |
| 2750 | outbuf, |
| 2751 | output_type, |
| 2752 | "RESULTSET", |
| 2753 | resultset_size); |
| 2754 | grn_output_result_set_n_hits(ctx, outbuf, output_type, format); |
| 2755 | if (format->flags & GRN_OBJ_FORMAT_WITH_COLUMN_NAMES) { |
| 2756 | grn_output_table_columns(ctx, outbuf, output_type, table, format); |
| 2757 | } |
| 2758 | } else { |
| 2759 | grn_output_array_open(ctx, outbuf, output_type, "HIT", -1); |
| 2760 | } |
| 2761 | } |
| 2762 | |
| 2763 | static void |
| 2764 | grn_output_result_set_close_v1(grn_ctx *ctx, |
| 2765 | grn_obj *outbuf, |
| 2766 | grn_content_type output_type, |
| 2767 | grn_obj *table, |
| 2768 | grn_obj_format *format) |
| 2769 | { |
| 2770 | grn_output_array_close(ctx, outbuf, output_type); |
| 2771 | } |
| 2772 | |
| 2773 | static void |
| 2774 | grn_output_result_set_open_metadata_v3(grn_ctx *ctx, |
| 2775 | grn_obj *outbuf, |
| 2776 | grn_content_type output_type, |
| 2777 | grn_obj *result_set, |
| 2778 | grn_obj_format *format, |
| 2779 | uint32_t n_additional_elements) |
| 2780 | { |
| 2781 | int n_elements = (int)n_additional_elements; |
| 2782 | if (format) { |
| 2783 | /* result_set: {"n_hits": N, ("columns": COLUMNS,) */ |
| 2784 | n_elements++; |
| 2785 | if (format->flags & GRN_OBJ_FORMAT_WITH_COLUMN_NAMES) { |
| 2786 | n_elements++; |
| 2787 | } |
| 2788 | grn_output_map_open(ctx, outbuf, output_type, "result_set", n_elements); |
| 2789 | grn_output_result_set_n_hits(ctx, outbuf, output_type, format); |
| 2790 | if (format->flags & GRN_OBJ_FORMAT_WITH_COLUMN_NAMES) { |
| 2791 | grn_output_table_columns(ctx, outbuf, output_type, result_set, format); |
| 2792 | } |
| 2793 | } else { |
| 2794 | grn_output_map_open(ctx, outbuf, output_type, "result_set", n_elements); |
| 2795 | } |
| 2796 | } |
| 2797 | |
| 2798 | static void |
| 2799 | grn_output_result_set_close_v3(grn_ctx *ctx, |
| 2800 | grn_obj *outbuf, |
| 2801 | grn_content_type output_type, |
| 2802 | grn_obj *result_set, |
| 2803 | grn_obj_format *format) |
| 2804 | { |
| 2805 | grn_output_map_close(ctx, outbuf, output_type); |
| 2806 | } |
| 2807 | |
| 2808 | void |
| 2809 | grn_output_result_set_open_metadata(grn_ctx *ctx, |
| 2810 | grn_obj *outbuf, |
| 2811 | grn_content_type output_type, |
| 2812 | grn_obj *result_set, |
| 2813 | grn_obj_format *format, |
| 2814 | uint32_t n_additional_elements) |
| 2815 | { |
| 2816 | if (output_type == GRN_CONTENT_APACHE_ARROW) { |
| 2817 | if (ctx->impl->output.arrow_stream_writer) { |
| 2818 | grn_arrow_stream_writer_close(ctx, ctx->impl->output.arrow_stream_writer); |
| 2819 | } |
| 2820 | ctx->impl->output.arrow_stream_writer = |
| 2821 | grn_arrow_stream_writer_open(ctx, outbuf); |
| 2822 | } |
| 2823 | |
| 2824 | if (grn_ctx_get_command_version(ctx) < GRN_COMMAND_VERSION_3) { |
| 2825 | grn_output_result_set_open_metadata_v1(ctx, |
| 2826 | outbuf, |
| 2827 | output_type, |
| 2828 | result_set, |
| 2829 | format, |
| 2830 | n_additional_elements); |
| 2831 | } else { |
| 2832 | grn_output_result_set_open_metadata_v3(ctx, |
| 2833 | outbuf, |
| 2834 | output_type, |
| 2835 | result_set, |
| 2836 | format, |
| 2837 | n_additional_elements); |
| 2838 | } |
| 2839 | } |
| 2840 | |
| 2841 | void |
| 2842 | grn_output_result_set_open(grn_ctx *ctx, |
| 2843 | grn_obj *outbuf, |
| 2844 | grn_content_type output_type, |
| 2845 | grn_obj *result_set, |
| 2846 | grn_obj_format *format, |
| 2847 | uint32_t n_additional_elements) |
| 2848 | { |
| 2849 | grn_output_result_set_open_metadata(ctx, |
| 2850 | outbuf, |
| 2851 | output_type, |
| 2852 | result_set, |
| 2853 | format, |
| 2854 | n_additional_elements + 1); |
| 2855 | grn_output_table_records(ctx, outbuf, output_type, result_set, format); |
| 2856 | } |
| 2857 | |
| 2858 | void |
| 2859 | grn_output_result_set_close(grn_ctx *ctx, |
| 2860 | grn_obj *outbuf, |
| 2861 | grn_content_type output_type, |
| 2862 | grn_obj *result_set, |
| 2863 | grn_obj_format *format) |
| 2864 | { |
| 2865 | if (output_type == GRN_CONTENT_APACHE_ARROW) { |
| 2866 | if (ctx->impl->output.arrow_stream_writer) { |
| 2867 | grn_arrow_stream_writer_close(ctx, ctx->impl->output.arrow_stream_writer); |
| 2868 | ctx->impl->output.arrow_stream_writer = NULL; |
| 2869 | } |
| 2870 | } |
| 2871 | |
| 2872 | if (grn_ctx_get_command_version(ctx) < GRN_COMMAND_VERSION_3) { |
| 2873 | grn_output_result_set_close_v1(ctx, |
| 2874 | outbuf, |
| 2875 | output_type, |
| 2876 | result_set, |
| 2877 | format); |
| 2878 | } else { |
| 2879 | grn_output_result_set_close_v3(ctx, |
| 2880 | outbuf, |
| 2881 | output_type, |
| 2882 | result_set, |
| 2883 | format); |
| 2884 | } |
| 2885 | } |
| 2886 | |
| 2887 | void |
| 2888 | grn_output_result_set(grn_ctx *ctx, |
| 2889 | grn_obj *outbuf, |
| 2890 | grn_content_type output_type, |
| 2891 | grn_obj *result_set, |
| 2892 | grn_obj_format *format) |
| 2893 | { |
| 2894 | uint32_t n_additional_elements = 0; |
| 2895 | |
| 2896 | grn_output_result_set_open(ctx, |
| 2897 | outbuf, |
| 2898 | output_type, |
| 2899 | result_set, |
| 2900 | format, |
| 2901 | n_additional_elements); |
| 2902 | grn_output_result_set_close(ctx, outbuf, output_type, result_set, format); |
| 2903 | } |
| 2904 | |
| 2905 | static void |
| 2906 | grn_output_table_hash_key(grn_ctx *ctx, |
| 2907 | grn_obj *outbuf, |
| 2908 | grn_content_type output_type, |
| 2909 | grn_obj *obj, |
| 2910 | grn_obj_format *format) |
| 2911 | { |
| 2912 | const unsigned int n_elements = grn_table_size(ctx, obj); |
| 2913 | grn_obj key; |
| 2914 | GRN_OBJ_INIT(&key, GRN_BULK, GRN_OBJ_DO_SHALLOW_COPY, obj->header.domain); |
| 2915 | grn_output_map_open(ctx, outbuf, output_type, "object", (int)n_elements); |
| 2916 | GRN_TABLE_EACH_BEGIN(ctx, obj, cursor, id) |
| 2917 | { |
| 2918 | void *raw_key; |
| 2919 | const int key_size = grn_table_cursor_get_key(ctx, cursor, &raw_key); |
| 2920 | GRN_TEXT_SET(ctx, &key, raw_key, key_size); |
| 2921 | grn_output_obj(ctx, outbuf, output_type, &key, format); |
| 2922 | void *raw_value; |
| 2923 | const int value_size = grn_table_cursor_get_value(ctx, cursor, &raw_value); |
| 2924 | if (value_size == 0) { |
| 2925 | grn_output_null(ctx, outbuf, output_type); |
| 2926 | } else { |
| 2927 | grn_obj *value = raw_value; |
| 2928 | grn_output_obj(ctx, outbuf, output_type, value, format); |
| 2929 | } |
| 2930 | } |
| 2931 | GRN_TABLE_EACH_END(ctx, cursor); |
| 2932 | grn_output_map_close(ctx, outbuf, output_type); |
| 2933 | } |
| 2934 | |
| 2935 | void |
| 2936 | grn_output_obj(grn_ctx *ctx, |
| 2937 | grn_obj *outbuf, |
| 2938 | grn_content_type output_type, |
| 2939 | grn_obj *obj, |
| 2940 | grn_obj_format *format) |
| 2941 | { |
| 2942 | grn_obj buf; |
| 2943 | GRN_TEXT_INIT(&buf, 0); |
| 2944 | switch (obj->header.type) { |
| 2945 | case GRN_VOID: |
| 2946 | grn_output_void(ctx, outbuf, output_type, obj, format); |
| 2947 | break; |
| 2948 | case GRN_BULK: |
| 2949 | grn_output_bulk(ctx, outbuf, output_type, obj, format); |
| 2950 | break; |
| 2951 | case GRN_UVECTOR: |
| 2952 | grn_output_uvector(ctx, outbuf, output_type, obj, format); |
| 2953 | break; |
| 2954 | case GRN_VECTOR: |
| 2955 | grn_output_vector(ctx, outbuf, output_type, obj, format); |
| 2956 | break; |
| 2957 | case GRN_PVECTOR: |
| 2958 | grn_output_pvector(ctx, outbuf, output_type, obj, format); |
| 2959 | break; |
| 2960 | case GRN_TABLE_HASH_KEY: |
| 2961 | if (obj->header.flags & GRN_HASH_TINY) { |
| 2962 | grn_output_table_hash_key(ctx, outbuf, output_type, obj, format); |
| 2963 | } else { |
| 2964 | /* Deprecated. Use grn_output_result_set() directly. */ |
| 2965 | grn_output_result_set(ctx, outbuf, output_type, obj, format); |
| 2966 | } |
| 2967 | break; |
| 2968 | case GRN_TABLE_PAT_KEY: |
| 2969 | case GRN_TABLE_DAT_KEY: |
| 2970 | case GRN_TABLE_NO_KEY: |
| 2971 | /* Deprecated. Use grn_output_result_set() directly. */ |
| 2972 | grn_output_result_set(ctx, outbuf, output_type, obj, format); |
| 2973 | break; |
| 2974 | } |
| 2975 | GRN_OBJ_FIN(ctx, &buf); |
| 2976 | } |
| 2977 | |
| 2978 | typedef enum { |
| 2979 | XML_START, |
| 2980 | XML_START_ELEMENT, |
| 2981 | XML_END_ELEMENT, |
| 2982 | XML_TEXT |
| 2983 | } xml_status; |
| 2984 | |
| 2985 | typedef enum { |
| 2986 | XML_PLACE_NONE, |
| 2987 | XML_PLACE_COLUMN, |
| 2988 | XML_PLACE_HIT |
| 2989 | } xml_place; |
| 2990 | |
| 2991 | static char * |
| 2992 | transform_xml_next_column(grn_obj *columns, int n) |
| 2993 | { |
| 2994 | char *column = GRN_TEXT_VALUE(columns); |
| 2995 | while (n--) { |
| 2996 | while (*column) { |
| 2997 | column++; |
| 2998 | } |
| 2999 | column++; |
| 3000 | } |
| 3001 | return column; |
| 3002 | } |
| 3003 | |
| 3004 | static void |
| 3005 | transform_xml(grn_ctx *ctx, grn_obj *output, grn_obj *transformed) |
| 3006 | { |
| 3007 | char *s, *e; |
| 3008 | xml_status status = XML_START; |
| 3009 | xml_place place = XML_PLACE_NONE; |
| 3010 | grn_obj buf, name, columns, *expr; |
| 3011 | int len; |
| 3012 | int offset = 0, limit = 0, record_n = 0; |
| 3013 | int column_n = 0, column_text_n = 0, result_set_n = -1; |
| 3014 | bool in_vector = false; |
| 3015 | unsigned int vector_element_n = 0; |
| 3016 | bool in_weight_vector = false; |
| 3017 | unsigned int weight_vector_item_n = 0; |
| 3018 | |
| 3019 | s = GRN_TEXT_VALUE(output); |
| 3020 | e = GRN_BULK_CURR(output); |
| 3021 | GRN_TEXT_INIT(&buf, 0); |
| 3022 | GRN_TEXT_INIT(&name, 0); |
| 3023 | GRN_TEXT_INIT(&columns, 0); |
| 3024 | |
| 3025 | expr = ctx->impl->curr_expr; |
| 3026 | |
| 3027 | #define EQUAL_NAME_P(_name) \ |
| 3028 | (GRN_TEXT_LEN(&name) == strlen(_name) && \ |
| 3029 | !memcmp(GRN_TEXT_VALUE(&name), _name, strlen(_name))) |
| 3030 | |
| 3031 | while (s < e) { |
| 3032 | switch (*s) { |
| 3033 | case '<': |
| 3034 | s++; |
| 3035 | switch (*s) { |
| 3036 | case '/': |
| 3037 | status = XML_END_ELEMENT; |
| 3038 | s++; |
| 3039 | break; |
| 3040 | default: |
| 3041 | status = XML_START_ELEMENT; |
| 3042 | break; |
| 3043 | } |
| 3044 | GRN_BULK_REWIND(&name); |
| 3045 | break; |
| 3046 | case '>': |
| 3047 | switch (status) { |
| 3048 | case XML_START_ELEMENT: |
| 3049 | if (EQUAL_NAME_P("COLUMN")) { |
| 3050 | place = XML_PLACE_COLUMN; |
| 3051 | column_text_n = 0; |
| 3052 | } else if (EQUAL_NAME_P("HIT")) { |
| 3053 | place = XML_PLACE_HIT; |
| 3054 | column_n = 0; |
| 3055 | if (result_set_n == 0) { |
| 3056 | GRN_TEXT_PUTS(ctx, transformed, "<HIT NO=\""); |
| 3057 | grn_text_itoa(ctx, transformed, record_n++); |
| 3058 | GRN_TEXT_PUTS(ctx, transformed, "\">\n"); |
| 3059 | } else { |
| 3060 | GRN_TEXT_PUTS(ctx, transformed, "<NAVIGATIONELEMENT "); |
| 3061 | } |
| 3062 | } else if (EQUAL_NAME_P("RESULTSET")) { |
| 3063 | GRN_BULK_REWIND(&columns); |
| 3064 | result_set_n++; |
| 3065 | if (result_set_n == 0) { |
| 3066 | } else { |
| 3067 | GRN_TEXT_PUTS(ctx, transformed, "<NAVIGATIONENTRY>\n"); |
| 3068 | } |
| 3069 | } else if (EQUAL_NAME_P("VECTOR")) { |
| 3070 | char *c = transform_xml_next_column(&columns, column_n++); |
| 3071 | in_vector = true; |
| 3072 | vector_element_n = 0; |
| 3073 | GRN_TEXT_PUTS(ctx, transformed, "<FIELD NAME=\""); |
| 3074 | GRN_TEXT_PUTS(ctx, transformed, c); |
| 3075 | GRN_TEXT_PUTS(ctx, transformed, "\">"); |
| 3076 | } else if (EQUAL_NAME_P("WEIGHT_VECTOR")) { |
| 3077 | char *c = transform_xml_next_column(&columns, column_n++); |
| 3078 | in_weight_vector = true; |
| 3079 | weight_vector_item_n = 0; |
| 3080 | GRN_TEXT_PUTS(ctx, transformed, "<FIELD NAME=\""); |
| 3081 | GRN_TEXT_PUTS(ctx, transformed, c); |
| 3082 | GRN_TEXT_PUTS(ctx, transformed, "\">"); |
| 3083 | } |
| 3084 | break; |
| 3085 | case XML_END_ELEMENT: |
| 3086 | if (EQUAL_NAME_P("HIT")) { |
| 3087 | place = XML_PLACE_NONE; |
| 3088 | if (result_set_n == 0) { |
| 3089 | GRN_TEXT_PUTS(ctx, transformed, "</HIT>\n"); |
| 3090 | } else { |
| 3091 | GRN_TEXT_PUTS(ctx, transformed, "/>\n"); |
| 3092 | } |
| 3093 | } else if (EQUAL_NAME_P("RESULTSET")) { |
| 3094 | place = XML_PLACE_NONE; |
| 3095 | if (result_set_n == 0) { |
| 3096 | GRN_TEXT_PUTS(ctx, transformed, "</RESULTSET>\n"); |
| 3097 | } else { |
| 3098 | GRN_TEXT_PUTS(ctx, |
| 3099 | transformed, |
| 3100 | "</NAVIGATIONELEMENTS>\n" |
| 3101 | "</NAVIGATIONENTRY>\n"); |
| 3102 | } |
| 3103 | } else if (EQUAL_NAME_P("RESULT")) { |
| 3104 | GRN_TEXT_PUTS(ctx, |
| 3105 | transformed, |
| 3106 | "</RESULTPAGE>\n" |
| 3107 | "</SEGMENT>\n" |
| 3108 | "</SEGMENTS>\n"); |
| 3109 | } else if (EQUAL_NAME_P("VECTOR")) { |
| 3110 | in_vector = false; |
| 3111 | GRN_TEXT_PUTS(ctx, transformed, "</FIELD>\n"); |
| 3112 | } else if (EQUAL_NAME_P("WEIGHT_VECTOR")) { |
| 3113 | in_weight_vector = false; |
| 3114 | GRN_TEXT_PUTS(ctx, transformed, "</FIELD>\n"); |
| 3115 | } else { |
| 3116 | switch (place) { |
| 3117 | case XML_PLACE_HIT: |
| 3118 | if (result_set_n == 0) { |
| 3119 | if (in_vector) { |
| 3120 | if (vector_element_n > 0) { |
| 3121 | GRN_TEXT_PUTS(ctx, transformed, ", "); |
| 3122 | } |
| 3123 | GRN_TEXT_PUT(ctx, |
| 3124 | transformed, |
| 3125 | GRN_TEXT_VALUE(&buf), |
| 3126 | GRN_TEXT_LEN(&buf)); |
| 3127 | vector_element_n++; |
| 3128 | } else if (in_weight_vector) { |
| 3129 | bool is_key = ((weight_vector_item_n % 2) == 0); |
| 3130 | if (is_key) { |
| 3131 | unsigned int weight_vector_key_n; |
| 3132 | weight_vector_key_n = weight_vector_item_n / 2; |
| 3133 | if (weight_vector_key_n > 0) { |
| 3134 | GRN_TEXT_PUTS(ctx, transformed, ", "); |
| 3135 | } |
| 3136 | } else { |
| 3137 | GRN_TEXT_PUTS(ctx, transformed, ":"); |
| 3138 | } |
| 3139 | GRN_TEXT_PUT(ctx, |
| 3140 | transformed, |
| 3141 | GRN_TEXT_VALUE(&buf), |
| 3142 | GRN_TEXT_LEN(&buf)); |
| 3143 | weight_vector_item_n++; |
| 3144 | } else { |
| 3145 | char *c = transform_xml_next_column(&columns, column_n++); |
| 3146 | GRN_TEXT_PUTS(ctx, transformed, "<FIELD NAME=\""); |
| 3147 | GRN_TEXT_PUTS(ctx, transformed, c); |
| 3148 | GRN_TEXT_PUTS(ctx, transformed, "\">"); |
| 3149 | GRN_TEXT_PUT(ctx, |
| 3150 | transformed, |
| 3151 | GRN_TEXT_VALUE(&buf), |
| 3152 | GRN_TEXT_LEN(&buf)); |
| 3153 | GRN_TEXT_PUTS(ctx, transformed, "</FIELD>\n"); |
| 3154 | } |
| 3155 | } else { |
| 3156 | char *c = transform_xml_next_column(&columns, column_n++); |
| 3157 | GRN_TEXT_PUTS(ctx, transformed, c); |
| 3158 | GRN_TEXT_PUTS(ctx, transformed, "=\""); |
| 3159 | GRN_TEXT_PUT(ctx, |
| 3160 | transformed, |
| 3161 | GRN_TEXT_VALUE(&buf), |
| 3162 | GRN_TEXT_LEN(&buf)); |
| 3163 | GRN_TEXT_PUTS(ctx, transformed, "\" "); |
| 3164 | } |
| 3165 | break; |
| 3166 | default: |
| 3167 | if (EQUAL_NAME_P("NHITS")) { |
| 3168 | if (result_set_n == 0) { |
| 3169 | uint32_t nhits; |
| 3170 | grn_obj *offset_value, *limit_value; |
| 3171 | |
| 3172 | nhits = |
| 3173 | grn_atoui(GRN_TEXT_VALUE(&buf), GRN_BULK_CURR(&buf), NULL); |
| 3174 | offset_value = |
| 3175 | grn_expr_get_var(ctx, expr, "offset", strlen("offset")); |
| 3176 | limit_value = |
| 3177 | grn_expr_get_var(ctx, expr, "limit", strlen("limit")); |
| 3178 | if (GRN_TEXT_LEN(offset_value)) { |
| 3179 | offset = grn_atoi(GRN_TEXT_VALUE(offset_value), |
| 3180 | GRN_BULK_CURR(offset_value), |
| 3181 | NULL); |
| 3182 | } else { |
| 3183 | offset = 0; |
| 3184 | } |
| 3185 | if (GRN_TEXT_LEN(limit_value)) { |
| 3186 | limit = grn_atoi(GRN_TEXT_VALUE(limit_value), |
| 3187 | GRN_BULK_CURR(limit_value), |
| 3188 | NULL); |
| 3189 | } else { |
| 3190 | #define DEFAULT_LIMIT 10 |
| 3191 | limit = DEFAULT_LIMIT; |
| 3192 | #undef DEFAULT_LIMIT |
| 3193 | } |
| 3194 | grn_output_range_normalize(ctx, (int)nhits, &offset, &limit); |
| 3195 | record_n = offset + 1; |
| 3196 | GRN_TEXT_PUTS(ctx, |
| 3197 | transformed, |
| 3198 | "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" |
| 3199 | "<SEGMENTS>\n" |
| 3200 | "<SEGMENT>\n" |
| 3201 | "<RESULTPAGE>\n" |
| 3202 | "<RESULTSET OFFSET=\""); |
| 3203 | grn_text_lltoa(ctx, transformed, offset); |
| 3204 | GRN_TEXT_PUTS(ctx, transformed, "\" LIMIT=\""); |
| 3205 | grn_text_lltoa(ctx, transformed, limit); |
| 3206 | GRN_TEXT_PUTS(ctx, transformed, "\" NHITS=\""); |
| 3207 | grn_text_lltoa(ctx, transformed, nhits); |
| 3208 | GRN_TEXT_PUTS(ctx, transformed, "\">\n"); |
| 3209 | } else { |
| 3210 | GRN_TEXT_PUTS(ctx, transformed, "<NAVIGATIONELEMENTS COUNT=\""); |
| 3211 | GRN_TEXT_PUT(ctx, |
| 3212 | transformed, |
| 3213 | GRN_TEXT_VALUE(&buf), |
| 3214 | GRN_TEXT_LEN(&buf)); |
| 3215 | GRN_TEXT_PUTS(ctx, transformed, "\">\n"); |
| 3216 | } |
| 3217 | } else if (EQUAL_NAME_P("TEXT")) { |
| 3218 | switch (place) { |
| 3219 | case XML_PLACE_COLUMN: |
| 3220 | if (column_text_n == 0) { |
| 3221 | GRN_TEXT_PUT(ctx, |
| 3222 | &columns, |
| 3223 | GRN_TEXT_VALUE(&buf), |
| 3224 | GRN_TEXT_LEN(&buf)); |
| 3225 | GRN_TEXT_PUTC(ctx, &columns, '\0'); |
| 3226 | } |
| 3227 | column_text_n++; |
| 3228 | break; |
| 3229 | default: |
| 3230 | break; |
| 3231 | } |
| 3232 | } |
| 3233 | } |
| 3234 | } |
| 3235 | default: |
| 3236 | break; |
| 3237 | } |
| 3238 | s++; |
| 3239 | GRN_BULK_REWIND(&buf); |
| 3240 | status = XML_TEXT; |
| 3241 | break; |
| 3242 | default: |
| 3243 | len = grn_charlen(ctx, s, e); |
| 3244 | switch (status) { |
| 3245 | case XML_START_ELEMENT: |
| 3246 | case XML_END_ELEMENT: |
| 3247 | GRN_TEXT_PUT(ctx, &name, s, len); |
| 3248 | break; |
| 3249 | default: |
| 3250 | GRN_TEXT_PUT(ctx, &buf, s, len); |
| 3251 | break; |
| 3252 | } |
| 3253 | s += len; |
| 3254 | break; |
| 3255 | } |
| 3256 | } |
| 3257 | #undef EQUAL_NAME_P |
| 3258 | |
| 3259 | GRN_OBJ_FIN(ctx, &buf); |
| 3260 | GRN_OBJ_FIN(ctx, &name); |
| 3261 | GRN_OBJ_FIN(ctx, &columns); |
| 3262 | } |
| 3263 | |
| 3264 | #ifdef GRN_WITH_MESSAGE_PACK |
| 3265 | typedef struct { |
| 3266 | grn_ctx *ctx; |
| 3267 | grn_obj *buffer; |
| 3268 | } msgpack_writer_ctx; |
| 3269 | |
| 3270 | static inline int |
| 3271 | msgpack_buffer_writer(void *data, const char *buf, msgpack_size_t len) |
| 3272 | { |
| 3273 | msgpack_writer_ctx *writer_ctx = (msgpack_writer_ctx *)data; |
| 3274 | return grn_bulk_write(writer_ctx->ctx, writer_ctx->buffer, buf, len); |
| 3275 | } |
| 3276 | #endif |
| 3277 | |
| 3278 | #define JSON_CALLBACK_PARAM "callback" |
| 3279 | |
| 3280 | static void |
| 3281 | grn_output_envelope_open_json_v1(grn_ctx *ctx, |
| 3282 | grn_obj *head, |
| 3283 | bool is_stream_mode, |
| 3284 | bool have_body) |
| 3285 | { |
| 3286 | size_t indent_level = 0; |
| 3287 | |
| 3288 | json_array_open(ctx, head, &indent_level); |
| 3289 | } |
| 3290 | |
| 3291 | static void |
| 3292 | grn_output_envelope_close_json_v1(grn_ctx *ctx, |
| 3293 | grn_obj *head, |
| 3294 | grn_obj *foot, |
| 3295 | bool is_stream_mode, |
| 3296 | bool have_body, |
| 3297 | grn_rc rc, |
| 3298 | double started, |
| 3299 | double elapsed, |
| 3300 | const char *file, |
| 3301 | int line) |
| 3302 | { |
| 3303 | size_t indent_level = 0; |
| 3304 | if (ctx->impl->output.is_pretty) { |
| 3305 | indent_level++; |
| 3306 | } |
| 3307 | |
| 3308 | { |
| 3309 | if (is_stream_mode && have_body) { |
| 3310 | json_element_end(ctx, head, indent_level); |
| 3311 | } |
| 3312 | |
| 3313 | json_array_open(ctx, head, &indent_level); |
| 3314 | { |
| 3315 | grn_text_itoa(ctx, head, rc); |
| 3316 | |
| 3317 | json_element_end(ctx, head, indent_level); |
| 3318 | grn_text_ftoa(ctx, head, started); |
| 3319 | |
| 3320 | json_element_end(ctx, head, indent_level); |
| 3321 | grn_text_ftoa(ctx, head, elapsed); |
| 3322 | |
| 3323 | if (rc != GRN_SUCCESS) { |
| 3324 | json_element_end(ctx, head, indent_level); |
| 3325 | grn_text_esc(ctx, head, ctx->errbuf, strlen(ctx->errbuf)); |
| 3326 | |
| 3327 | if (ctx->errfunc && ctx->errfile) { |
| 3328 | grn_obj *command; |
| 3329 | |
| 3330 | json_element_end(ctx, head, indent_level); |
| 3331 | json_array_open(ctx, head, &indent_level); |
| 3332 | { |
| 3333 | json_array_open(ctx, head, &indent_level); |
| 3334 | { |
| 3335 | grn_text_esc(ctx, head, ctx->errfunc, strlen(ctx->errfunc)); |
| 3336 | |
| 3337 | json_element_end(ctx, head, indent_level); |
| 3338 | grn_text_esc(ctx, head, ctx->errfile, strlen(ctx->errfile)); |
| 3339 | |
| 3340 | json_element_end(ctx, head, indent_level); |
| 3341 | grn_text_ulltoa(ctx, head, ctx->errline); |
| 3342 | } |
| 3343 | json_array_close(ctx, head, &indent_level); |
| 3344 | |
| 3345 | if (file && (command = GRN_CTX_USER_DATA(ctx)->ptr)) { |
| 3346 | json_element_end(ctx, head, indent_level); |
| 3347 | json_array_open(ctx, head, &indent_level); |
| 3348 | { |
| 3349 | grn_text_esc(ctx, head, file, strlen(file)); |
| 3350 | |
| 3351 | json_element_end(ctx, head, indent_level); |
| 3352 | grn_text_itoa(ctx, head, line); |
| 3353 | |
| 3354 | json_element_end(ctx, head, indent_level); |
| 3355 | grn_text_esc(ctx, |
| 3356 | head, |
| 3357 | GRN_TEXT_VALUE(command), |
| 3358 | GRN_TEXT_LEN(command)); |
| 3359 | } |
| 3360 | json_array_close(ctx, head, &indent_level); |
| 3361 | } |
| 3362 | } |
| 3363 | json_array_close(ctx, head, &indent_level); |
| 3364 | } |
| 3365 | } |
| 3366 | } |
| 3367 | json_array_close(ctx, head, &indent_level); |
| 3368 | } |
| 3369 | |
| 3370 | if (!is_stream_mode && have_body) { |
| 3371 | json_element_end(ctx, head, indent_level); |
| 3372 | } |
| 3373 | |
| 3374 | json_array_close(ctx, foot, &indent_level); |
| 3375 | } |
| 3376 | |
| 3377 | static void |
| 3378 | grn_output_envelope_json_v1(grn_ctx *ctx, |
| 3379 | grn_obj *head, |
| 3380 | grn_obj *body, |
| 3381 | grn_obj *foot, |
| 3382 | grn_rc rc, |
| 3383 | double started, |
| 3384 | double elapsed, |
| 3385 | const char *file, |
| 3386 | int line) |
| 3387 | { |
| 3388 | bool is_stream_mode = false; |
| 3389 | bool have_body = GRN_TEXT_LEN(body) > 0; |
| 3390 | grn_output_envelope_open_json_v1(ctx, head, is_stream_mode, have_body); |
| 3391 | grn_output_envelope_close_json_v1(ctx, |
| 3392 | head, |
| 3393 | foot, |
| 3394 | is_stream_mode, |
| 3395 | have_body, |
| 3396 | rc, |
| 3397 | started, |
| 3398 | elapsed, |
| 3399 | file, |
| 3400 | line); |
| 3401 | } |
| 3402 | |
| 3403 | static void |
| 3404 | grn_output_envelope_open_json(grn_ctx *ctx, |
| 3405 | grn_obj *head, |
| 3406 | bool is_stream_mode, |
| 3407 | bool have_body) |
| 3408 | { |
| 3409 | size_t indent_level = 0; |
| 3410 | |
| 3411 | json_map_open(ctx, head, &indent_level); |
| 3412 | { |
| 3413 | if (is_stream_mode && have_body) { |
| 3414 | json_key(ctx, head, "body"); |
| 3415 | } |
| 3416 | } |
| 3417 | } |
| 3418 | |
| 3419 | static void |
| 3420 | grn_output_envelope_close_json_header(grn_ctx *ctx, |
| 3421 | grn_obj *head, |
| 3422 | grn_rc rc, |
| 3423 | double started, |
| 3424 | double elapsed, |
| 3425 | const char *file, |
| 3426 | int line, |
| 3427 | size_t *indent_level) |
| 3428 | { |
| 3429 | json_key(ctx, head, "header"); |
| 3430 | json_map_open(ctx, head, indent_level); |
| 3431 | { |
| 3432 | json_key(ctx, head, "return_code"); |
| 3433 | grn_text_itoa(ctx, head, rc); |
| 3434 | |
| 3435 | json_value_end(ctx, head, *indent_level); |
| 3436 | json_key(ctx, head, "start_time"); |
| 3437 | grn_text_ftoa(ctx, head, started); |
| 3438 | |
| 3439 | json_value_end(ctx, head, *indent_level); |
| 3440 | json_key(ctx, head, "elapsed_time"); |
| 3441 | grn_text_ftoa(ctx, head, elapsed); |
| 3442 | |
| 3443 | if (rc != GRN_SUCCESS) { |
| 3444 | json_value_end(ctx, head, *indent_level); |
| 3445 | json_key(ctx, head, "error"); |
| 3446 | json_map_open(ctx, head, indent_level); |
| 3447 | { |
| 3448 | json_key(ctx, head, "message"); |
| 3449 | grn_text_esc(ctx, head, ctx->errbuf, strlen(ctx->errbuf)); |
| 3450 | |
| 3451 | if (ctx->errfunc && ctx->errfile) { |
| 3452 | json_value_end(ctx, head, *indent_level); |
| 3453 | json_key(ctx, head, "function"); |
| 3454 | grn_text_esc(ctx, head, ctx->errfunc, strlen(ctx->errfunc)); |
| 3455 | |
| 3456 | json_value_end(ctx, head, *indent_level); |
| 3457 | json_key(ctx, head, "file"); |
| 3458 | grn_text_esc(ctx, head, ctx->errfile, strlen(ctx->errfile)); |
| 3459 | |
| 3460 | json_value_end(ctx, head, *indent_level); |
| 3461 | json_key(ctx, head, "line"); |
| 3462 | grn_text_ulltoa(ctx, head, ctx->errline); |
| 3463 | } |
| 3464 | |
| 3465 | if (file) { |
| 3466 | grn_obj *command; |
| 3467 | |
| 3468 | command = GRN_CTX_USER_DATA(ctx)->ptr; |
| 3469 | if (command) { |
| 3470 | json_value_end(ctx, head, *indent_level); |
| 3471 | json_key(ctx, head, "input"); |
| 3472 | json_map_open(ctx, head, indent_level); |
| 3473 | { |
| 3474 | json_key(ctx, head, "file"); |
| 3475 | grn_text_esc(ctx, head, file, strlen(file)); |
| 3476 | |
| 3477 | json_value_end(ctx, head, *indent_level); |
| 3478 | json_key(ctx, head, "line"); |
| 3479 | grn_text_itoa(ctx, head, line); |
| 3480 | |
| 3481 | json_value_end(ctx, head, *indent_level); |
| 3482 | json_key(ctx, head, "command"); |
| 3483 | grn_text_esc(ctx, |
| 3484 | head, |
| 3485 | GRN_TEXT_VALUE(command), |
| 3486 | GRN_TEXT_LEN(command)); |
| 3487 | } |
| 3488 | json_map_close(ctx, head, indent_level); |
| 3489 | } |
| 3490 | } |
| 3491 | } |
| 3492 | json_map_close(ctx, head, indent_level); |
| 3493 | } |
| 3494 | } |
| 3495 | json_map_close(ctx, head, indent_level); |
| 3496 | } |
| 3497 | |
| 3498 | static void |
| 3499 | grn_output_envelope_close_json_trace_log(grn_ctx *ctx, |
| 3500 | grn_obj *head, |
| 3501 | size_t *indent_level) |
| 3502 | { |
| 3503 | json_value_end(ctx, head, *indent_level); |
| 3504 | json_key(ctx, head, "trace_log"); |
| 3505 | json_map_open(ctx, head, indent_level); |
| 3506 | { |
| 3507 | json_key(ctx, head, "columns"); |
| 3508 | json_array_open(ctx, head, indent_level); |
| 3509 | { |
| 3510 | json_map_open(ctx, head, indent_level); |
| 3511 | { |
| 3512 | json_key(ctx, head, "name"); |
| 3513 | grn_text_esc(ctx, head, "depth", strlen("depth")); |
| 3514 | } |
| 3515 | json_map_close(ctx, head, indent_level); |
| 3516 | |
| 3517 | json_element_end(ctx, head, *indent_level); |
| 3518 | json_map_open(ctx, head, indent_level); |
| 3519 | { |
| 3520 | json_key(ctx, head, "name"); |
| 3521 | grn_text_esc(ctx, head, "sequence", strlen("sequence")); |
| 3522 | } |
| 3523 | json_map_close(ctx, head, indent_level); |
| 3524 | |
| 3525 | json_element_end(ctx, head, *indent_level); |
| 3526 | json_map_open(ctx, head, indent_level); |
| 3527 | { |
| 3528 | json_key(ctx, head, "name"); |
| 3529 | grn_text_esc(ctx, head, "name", strlen("name")); |
| 3530 | } |
| 3531 | json_map_close(ctx, head, indent_level); |
| 3532 | |
| 3533 | json_element_end(ctx, head, *indent_level); |
| 3534 | json_map_open(ctx, head, indent_level); |
| 3535 | { |
| 3536 | json_key(ctx, head, "name"); |
| 3537 | grn_text_esc(ctx, head, "value", strlen("value")); |
| 3538 | } |
| 3539 | json_map_close(ctx, head, indent_level); |
| 3540 | |
| 3541 | json_element_end(ctx, head, *indent_level); |
| 3542 | json_map_open(ctx, head, indent_level); |
| 3543 | { |
| 3544 | json_key(ctx, head, "name"); |
| 3545 | grn_text_esc(ctx, head, "elapsed_time", strlen("elapsed_time")); |
| 3546 | } |
| 3547 | json_map_close(ctx, head, indent_level); |
| 3548 | } |
| 3549 | json_array_close(ctx, head, indent_level); |
| 3550 | |
| 3551 | json_value_end(ctx, head, *indent_level); |
| 3552 | json_key(ctx, head, "logs"); |
| 3553 | json_array_open(ctx, head, indent_level); |
| 3554 | { |
| 3555 | grn_obj *depths = &(ctx->impl->trace_log.depths); |
| 3556 | grn_obj *sequences = &(ctx->impl->trace_log.sequences); |
| 3557 | grn_obj *names = &(ctx->impl->trace_log.names); |
| 3558 | grn_obj *values = &(ctx->impl->trace_log.values); |
| 3559 | grn_obj *elapsed_times = &(ctx->impl->trace_log.elapsed_times); |
| 3560 | uint32_t n_logs = grn_vector_size(ctx, names); |
| 3561 | uint32_t i; |
| 3562 | for (i = 0; i < n_logs; i++) { |
| 3563 | if (i > 0) { |
| 3564 | json_element_end(ctx, head, *indent_level); |
| 3565 | } |
| 3566 | json_array_open(ctx, head, indent_level); |
| 3567 | { |
| 3568 | uint16_t depth = GRN_UINT16_VALUE_AT(depths, i); |
| 3569 | grn_text_ulltoa(ctx, head, depth); |
| 3570 | |
| 3571 | json_element_end(ctx, head, *indent_level); |
| 3572 | uint16_t sequence = GRN_UINT16_VALUE_AT(sequences, i); |
| 3573 | grn_text_ulltoa(ctx, head, sequence); |
| 3574 | |
| 3575 | json_element_end(ctx, head, *indent_level); |
| 3576 | const char *name; |
| 3577 | uint32_t name_size = |
| 3578 | grn_vector_get_element_float(ctx, names, i, &name, NULL, NULL); |
| 3579 | grn_text_esc(ctx, head, name, name_size); |
| 3580 | |
| 3581 | json_element_end(ctx, head, *indent_level); |
| 3582 | const char *value; |
| 3583 | grn_id value_domain; |
| 3584 | uint32_t value_size = grn_vector_get_element_float(ctx, |
| 3585 | values, |
| 3586 | i, |
| 3587 | &value, |
| 3588 | NULL, |
| 3589 | &value_domain); |
| 3590 | switch (value_domain) { |
| 3591 | case GRN_DB_UINT32: |
| 3592 | { |
| 3593 | uint32_t value_uint32 = |
| 3594 | (value_size == 0) ? 0 : *((const uint32_t *)value); |
| 3595 | grn_text_ulltoa(ctx, head, value_uint32); |
| 3596 | } |
| 3597 | break; |
| 3598 | default: |
| 3599 | grn_text_esc(ctx, head, value, value_size); |
| 3600 | break; |
| 3601 | } |
| 3602 | |
| 3603 | json_element_end(ctx, head, *indent_level); |
| 3604 | uint64_t elapsed_time = GRN_UINT64_VALUE_AT(elapsed_times, i); |
| 3605 | grn_text_ulltoa(ctx, head, elapsed_time); |
| 3606 | } |
| 3607 | json_array_close(ctx, head, indent_level); |
| 3608 | } |
| 3609 | } |
| 3610 | json_array_close(ctx, head, indent_level); |
| 3611 | } |
| 3612 | json_map_close(ctx, head, indent_level); |
| 3613 | } |
| 3614 | |
| 3615 | static void |
| 3616 | grn_output_envelope_close_json(grn_ctx *ctx, |
| 3617 | grn_obj *head, |
| 3618 | grn_obj *foot, |
| 3619 | bool is_stream_mode, |
| 3620 | bool have_body, |
| 3621 | grn_rc rc, |
| 3622 | double started, |
| 3623 | double elapsed, |
| 3624 | const char *file, |
| 3625 | int line) |
| 3626 | { |
| 3627 | size_t indent_level = 0; |
| 3628 | if (ctx->impl->output.is_pretty) { |
| 3629 | indent_level++; |
| 3630 | } |
| 3631 | |
| 3632 | { |
| 3633 | if (is_stream_mode && have_body) { |
| 3634 | json_value_end(ctx, head, indent_level); |
| 3635 | } |
| 3636 | |
| 3637 | grn_output_envelope_close_json_header(ctx, |
| 3638 | head, |
| 3639 | rc, |
| 3640 | started, |
| 3641 | elapsed, |
| 3642 | file, |
| 3643 | line, |
| 3644 | &indent_level); |
| 3645 | |
| 3646 | if (grn_ctx_trace_log_is_enabled(ctx)) { |
| 3647 | grn_output_envelope_close_json_trace_log(ctx, head, &indent_level); |
| 3648 | } |
| 3649 | |
| 3650 | if (!is_stream_mode && have_body) { |
| 3651 | json_value_end(ctx, head, indent_level); |
| 3652 | json_key(ctx, head, "body"); |
| 3653 | } |
| 3654 | } |
| 3655 | json_map_close(ctx, foot, &indent_level); |
| 3656 | } |
| 3657 | |
| 3658 | static void |
| 3659 | grn_output_envelope_json(grn_ctx *ctx, |
| 3660 | grn_obj *head, |
| 3661 | grn_obj *body, |
| 3662 | grn_obj *foot, |
| 3663 | grn_rc rc, |
| 3664 | double started, |
| 3665 | double elapsed, |
| 3666 | const char *file, |
| 3667 | int line) |
| 3668 | { |
| 3669 | bool is_stream_mode = false; |
| 3670 | bool have_body = GRN_TEXT_LEN(body) > 0; |
| 3671 | grn_output_envelope_open_json(ctx, head, is_stream_mode, have_body); |
| 3672 | grn_output_envelope_close_json(ctx, |
| 3673 | head, |
| 3674 | foot, |
| 3675 | is_stream_mode, |
| 3676 | have_body, |
| 3677 | rc, |
| 3678 | started, |
| 3679 | elapsed, |
| 3680 | file, |
| 3681 | line); |
| 3682 | } |
| 3683 | |
| 3684 | #ifdef GRN_WITH_MESSAGE_PACK |
| 3685 | static void |
| 3686 | msgpack_pack_cstr(msgpack_packer *packer, const char *string) |
| 3687 | { |
| 3688 | size_t size; |
| 3689 | |
| 3690 | size = strlen(string); |
| 3691 | msgpack_pack_str(packer, size); |
| 3692 | msgpack_pack_str_body(packer, string, size); |
| 3693 | } |
| 3694 | |
| 3695 | static void |
| 3696 | grn_output_envelope_msgpack_v1(grn_ctx *ctx, |
| 3697 | grn_rc rc, |
| 3698 | grn_obj *head, |
| 3699 | grn_obj *body, |
| 3700 | grn_obj *foot, |
| 3701 | double started, |
| 3702 | double elapsed, |
| 3703 | const char *file, |
| 3704 | int line) |
| 3705 | { |
| 3706 | msgpack_writer_ctx head_writer_ctx; |
| 3707 | msgpack_packer header_packer; |
| 3708 | size_t header_size; |
| 3709 | |
| 3710 | head_writer_ctx.ctx = ctx; |
| 3711 | head_writer_ctx.buffer = head; |
| 3712 | msgpack_packer_init(&header_packer, &head_writer_ctx, msgpack_buffer_writer); |
| 3713 | |
| 3714 | /* [HEADER, (BODY)] */ |
| 3715 | if (GRN_TEXT_LEN(body) > 0) { |
| 3716 | msgpack_pack_array(&header_packer, 2); |
| 3717 | } else { |
| 3718 | msgpack_pack_array(&header_packer, 1); |
| 3719 | } |
| 3720 | |
| 3721 | /* HEADER := [rc, started, elapsed, (error, (ERROR DETAIL))] */ |
| 3722 | header_size = 3; |
| 3723 | if (rc != GRN_SUCCESS) { |
| 3724 | header_size++; |
| 3725 | if (ctx->errfunc && ctx->errfile) { |
| 3726 | header_size++; |
| 3727 | } |
| 3728 | } |
| 3729 | msgpack_pack_array(&header_packer, header_size); |
| 3730 | msgpack_pack_int(&header_packer, rc); |
| 3731 | |
| 3732 | msgpack_pack_double(&header_packer, started); |
| 3733 | msgpack_pack_double(&header_packer, elapsed); |
| 3734 | |
| 3735 | if (rc != GRN_SUCCESS) { |
| 3736 | msgpack_pack_str(&header_packer, strlen(ctx->errbuf)); |
| 3737 | msgpack_pack_str_body(&header_packer, ctx->errbuf, strlen(ctx->errbuf)); |
| 3738 | if (ctx->errfunc && ctx->errfile) { |
| 3739 | grn_obj *command = GRN_CTX_USER_DATA(ctx)->ptr; |
| 3740 | size_t error_detail_size; |
| 3741 | |
| 3742 | /* ERROR DETAIL := [[errfunc, errfile, errline, (file, line, command)]] */ |
| 3743 | /* TODO: output backtrace */ |
| 3744 | msgpack_pack_array(&header_packer, 1); |
| 3745 | error_detail_size = 3; |
| 3746 | if (command) { |
| 3747 | error_detail_size += 3; |
| 3748 | } |
| 3749 | msgpack_pack_array(&header_packer, error_detail_size); |
| 3750 | |
| 3751 | msgpack_pack_str(&header_packer, strlen(ctx->errfunc)); |
| 3752 | msgpack_pack_str_body(&header_packer, ctx->errfunc, strlen(ctx->errfunc)); |
| 3753 | |
| 3754 | msgpack_pack_str(&header_packer, strlen(ctx->errfile)); |
| 3755 | msgpack_pack_str_body(&header_packer, ctx->errfile, strlen(ctx->errfile)); |
| 3756 | |
| 3757 | msgpack_pack_unsigned_int(&header_packer, ctx->errline); |
| 3758 | |
| 3759 | if (command) { |
| 3760 | if (file) { |
| 3761 | msgpack_pack_str(&header_packer, strlen(file)); |
| 3762 | msgpack_pack_str_body(&header_packer, file, strlen(file)); |
| 3763 | } else { |
| 3764 | msgpack_pack_str(&header_packer, 7); |
| 3765 | msgpack_pack_str_body(&header_packer, "(stdin)", 7); |
| 3766 | } |
| 3767 | |
| 3768 | msgpack_pack_int(&header_packer, line); |
| 3769 | |
| 3770 | msgpack_pack_str(&header_packer, GRN_TEXT_LEN(command)); |
| 3771 | msgpack_pack_str_body(&header_packer, |
| 3772 | GRN_TEXT_VALUE(command), |
| 3773 | GRN_TEXT_LEN(command)); |
| 3774 | } |
| 3775 | } |
| 3776 | } |
| 3777 | } |
| 3778 | |
| 3779 | static void |
| 3780 | grn_output_envelope_msgpack(grn_ctx *ctx, |
| 3781 | grn_rc rc, |
| 3782 | grn_obj *head, |
| 3783 | grn_obj *body, |
| 3784 | grn_obj *foot, |
| 3785 | double started, |
| 3786 | double elapsed, |
| 3787 | const char *file, |
| 3788 | int line) |
| 3789 | { |
| 3790 | msgpack_writer_ctx writer_ctx; |
| 3791 | msgpack_packer packer; |
| 3792 | size_t n_elements; |
| 3793 | |
| 3794 | writer_ctx.ctx = ctx; |
| 3795 | writer_ctx.buffer = head; |
| 3796 | msgpack_packer_init(&packer, &writer_ctx, msgpack_buffer_writer); |
| 3797 | |
| 3798 | /* |
| 3799 | * ENVELOPE := { |
| 3800 | * "header": HEADER, |
| 3801 | * "body": BODY (optional) |
| 3802 | * } |
| 3803 | */ |
| 3804 | if (GRN_TEXT_LEN(body) > 0) { |
| 3805 | n_elements = 2; |
| 3806 | } else { |
| 3807 | n_elements = 1; |
| 3808 | } |
| 3809 | |
| 3810 | msgpack_pack_map(&packer, n_elements); |
| 3811 | { |
| 3812 | size_t n_header_elements = 3; |
| 3813 | |
| 3814 | /* |
| 3815 | * HEADER := { |
| 3816 | * "return_code": rc, |
| 3817 | * "start_time": started, |
| 3818 | * "elapsed_time": elapsed, |
| 3819 | " "error": { (optional) |
| 3820 | * "message": errbuf, |
| 3821 | * "function": errfunc, |
| 3822 | * "file": errfile, |
| 3823 | * "line": errline, |
| 3824 | * "input": { (optional) |
| 3825 | * "file": input_file, |
| 3826 | * "line": line, |
| 3827 | * "command": command |
| 3828 | * } |
| 3829 | * } |
| 3830 | * } |
| 3831 | */ |
| 3832 | |
| 3833 | if (rc != GRN_SUCCESS) { |
| 3834 | n_header_elements++; |
| 3835 | } |
| 3836 | |
| 3837 | msgpack_pack_cstr(&packer, "header"); |
| 3838 | msgpack_pack_map(&packer, n_header_elements); |
| 3839 | { |
| 3840 | msgpack_pack_cstr(&packer, "return_code"); |
| 3841 | msgpack_pack_int(&packer, rc); |
| 3842 | |
| 3843 | msgpack_pack_cstr(&packer, "start_time"); |
| 3844 | msgpack_pack_double(&packer, started); |
| 3845 | |
| 3846 | msgpack_pack_cstr(&packer, "elapsed_time"); |
| 3847 | msgpack_pack_double(&packer, elapsed); |
| 3848 | |
| 3849 | if (rc != GRN_SUCCESS) { |
| 3850 | size_t n_error_elements = 1; |
| 3851 | grn_obj *command; |
| 3852 | |
| 3853 | if (ctx->errfunc) { |
| 3854 | n_error_elements++; |
| 3855 | } |
| 3856 | if (ctx->errfile) { |
| 3857 | n_error_elements += 2; |
| 3858 | } |
| 3859 | |
| 3860 | command = GRN_CTX_USER_DATA(ctx)->ptr; |
| 3861 | if (file || command) { |
| 3862 | n_error_elements++; |
| 3863 | } |
| 3864 | |
| 3865 | msgpack_pack_cstr(&packer, "error"); |
| 3866 | msgpack_pack_map(&packer, n_error_elements); |
| 3867 | { |
| 3868 | msgpack_pack_cstr(&packer, "message"); |
| 3869 | msgpack_pack_cstr(&packer, ctx->errbuf); |
| 3870 | |
| 3871 | if (ctx->errfunc) { |
| 3872 | msgpack_pack_cstr(&packer, "function"); |
| 3873 | msgpack_pack_cstr(&packer, ctx->errfunc); |
| 3874 | } |
| 3875 | |
| 3876 | if (ctx->errfile) { |
| 3877 | msgpack_pack_cstr(&packer, "file"); |
| 3878 | msgpack_pack_cstr(&packer, ctx->errfile); |
| 3879 | |
| 3880 | msgpack_pack_cstr(&packer, "line"); |
| 3881 | msgpack_pack_unsigned_int(&packer, ctx->errline); |
| 3882 | } |
| 3883 | |
| 3884 | if (file || command) { |
| 3885 | size_t n_input_elements = 0; |
| 3886 | |
| 3887 | if (file) { |
| 3888 | n_input_elements += 2; |
| 3889 | } |
| 3890 | if (command) { |
| 3891 | n_input_elements++; |
| 3892 | } |
| 3893 | |
| 3894 | msgpack_pack_cstr(&packer, "input"); |
| 3895 | msgpack_pack_map(&packer, n_input_elements); |
| 3896 | |
| 3897 | if (file) { |
| 3898 | msgpack_pack_cstr(&packer, "file"); |
| 3899 | msgpack_pack_cstr(&packer, file); |
| 3900 | |
| 3901 | msgpack_pack_cstr(&packer, "line"); |
| 3902 | msgpack_pack_int(&packer, line); |
| 3903 | } |
| 3904 | |
| 3905 | if (command) { |
| 3906 | msgpack_pack_cstr(&packer, "command"); |
| 3907 | msgpack_pack_str(&packer, GRN_TEXT_LEN(command)); |
| 3908 | msgpack_pack_str_body(&packer, |
| 3909 | GRN_TEXT_VALUE(command), |
| 3910 | GRN_TEXT_LEN(command)); |
| 3911 | } |
| 3912 | } |
| 3913 | } |
| 3914 | } |
| 3915 | } |
| 3916 | |
| 3917 | if (GRN_TEXT_LEN(body) > 0) { |
| 3918 | msgpack_pack_cstr(&packer, "body"); |
| 3919 | } |
| 3920 | } |
| 3921 | } |
| 3922 | #endif /* GRN_WITH_MESSAGE_PACK */ |
| 3923 | |
| 3924 | static void |
| 3925 | grn_output_envelope_close_apache_arrow_metadata(grn_ctx *ctx, |
| 3926 | grn_obj *output, |
| 3927 | grn_rc rc, |
| 3928 | double started, |
| 3929 | double elapsed, |
| 3930 | const char *file, |
| 3931 | int line) |
| 3932 | { |
| 3933 | grn_arrow_stream_writer *writer = grn_arrow_stream_writer_open(ctx, output); |
| 3934 | if (!writer) { |
| 3935 | return; |
| 3936 | } |
| 3937 | |
| 3938 | grn_arrow_stream_writer_add_metadata(ctx, |
| 3939 | writer, |
| 3940 | "GROONGA:data_type", |
| 3941 | "metadata"); |
| 3942 | grn_arrow_stream_writer_add_field(ctx, |
| 3943 | writer, |
| 3944 | "return_code", |
| 3945 | grn_ctx_at(ctx, GRN_DB_INT32)); |
| 3946 | grn_arrow_stream_writer_add_field(ctx, |
| 3947 | writer, |
| 3948 | "start_time", |
| 3949 | grn_ctx_at(ctx, GRN_DB_TIME)); |
| 3950 | grn_arrow_stream_writer_add_field(ctx, |
| 3951 | writer, |
| 3952 | "elapsed_time", |
| 3953 | grn_ctx_at(ctx, GRN_DB_FLOAT)); |
| 3954 | grn_arrow_stream_writer_add_field(ctx, |
| 3955 | writer, |
| 3956 | "error_message", |
| 3957 | grn_ctx_at(ctx, GRN_DB_SHORT_TEXT)); |
| 3958 | grn_arrow_stream_writer_add_field(ctx, |
| 3959 | writer, |
| 3960 | "error_file", |
| 3961 | grn_ctx_at(ctx, GRN_DB_SHORT_TEXT)); |
| 3962 | grn_arrow_stream_writer_add_field(ctx, |
| 3963 | writer, |
| 3964 | "error_line", |
| 3965 | grn_ctx_at(ctx, GRN_DB_UINT32)); |
| 3966 | grn_arrow_stream_writer_add_field(ctx, |
| 3967 | writer, |
| 3968 | "error_function", |
| 3969 | grn_ctx_at(ctx, GRN_DB_SHORT_TEXT)); |
| 3970 | grn_arrow_stream_writer_add_field(ctx, |
| 3971 | writer, |
| 3972 | "error_input_file", |
| 3973 | grn_ctx_at(ctx, GRN_DB_SHORT_TEXT)); |
| 3974 | grn_arrow_stream_writer_add_field(ctx, |
| 3975 | writer, |
| 3976 | "error_input_line", |
| 3977 | grn_ctx_at(ctx, GRN_DB_INT32)); |
| 3978 | grn_arrow_stream_writer_add_field(ctx, |
| 3979 | writer, |
| 3980 | "error_input_command", |
| 3981 | grn_ctx_at(ctx, GRN_DB_SHORT_TEXT)); |
| 3982 | grn_arrow_stream_writer_write_schema(ctx, writer); |
| 3983 | |
| 3984 | grn_arrow_stream_writer_open_record(ctx, writer); |
| 3985 | grn_arrow_stream_writer_add_column_int32(ctx, writer, rc); |
| 3986 | grn_arrow_stream_writer_add_column_timestamp( |
| 3987 | ctx, |
| 3988 | writer, |
| 3989 | grn_timeval_from_double(ctx, started)); |
| 3990 | grn_arrow_stream_writer_add_column_float(ctx, writer, elapsed); |
| 3991 | if (rc == GRN_SUCCESS) { |
| 3992 | /* error_message */ |
| 3993 | grn_arrow_stream_writer_add_column_null(ctx, writer); |
| 3994 | /* error_file */ |
| 3995 | grn_arrow_stream_writer_add_column_null(ctx, writer); |
| 3996 | /* error_line */ |
| 3997 | grn_arrow_stream_writer_add_column_null(ctx, writer); |
| 3998 | /* error_function */ |
| 3999 | grn_arrow_stream_writer_add_column_null(ctx, writer); |
| 4000 | /* error_input_file */ |
| 4001 | grn_arrow_stream_writer_add_column_null(ctx, writer); |
| 4002 | /* error_input_line */ |
| 4003 | grn_arrow_stream_writer_add_column_null(ctx, writer); |
| 4004 | /* error_input_command */ |
| 4005 | grn_arrow_stream_writer_add_column_null(ctx, writer); |
| 4006 | } else { |
| 4007 | grn_arrow_stream_writer_add_column_text(ctx, |
| 4008 | writer, |
| 4009 | ctx->errbuf, |
| 4010 | strlen(ctx->errbuf)); |
| 4011 | if (ctx->errfile && ctx->errfunc) { |
| 4012 | grn_arrow_stream_writer_add_column_text(ctx, |
| 4013 | writer, |
| 4014 | ctx->errfile, |
| 4015 | strlen(ctx->errfile)); |
| 4016 | grn_arrow_stream_writer_add_column_uint32(ctx, writer, ctx->errline); |
| 4017 | grn_arrow_stream_writer_add_column_text(ctx, |
| 4018 | writer, |
| 4019 | ctx->errfunc, |
| 4020 | strlen(ctx->errfunc)); |
| 4021 | } else { |
| 4022 | grn_arrow_stream_writer_add_column_null(ctx, writer); |
| 4023 | grn_arrow_stream_writer_add_column_null(ctx, writer); |
| 4024 | grn_arrow_stream_writer_add_column_null(ctx, writer); |
| 4025 | } |
| 4026 | grn_obj *command = GRN_CTX_USER_DATA(ctx)->ptr; |
| 4027 | if (file && command) { |
| 4028 | grn_arrow_stream_writer_add_column_text(ctx, writer, file, strlen(file)); |
| 4029 | grn_arrow_stream_writer_add_column_int32(ctx, writer, line); |
| 4030 | grn_arrow_stream_writer_add_column_text(ctx, |
| 4031 | writer, |
| 4032 | GRN_TEXT_VALUE(command), |
| 4033 | GRN_TEXT_LEN(command)); |
| 4034 | } else { |
| 4035 | grn_arrow_stream_writer_add_column_null(ctx, writer); |
| 4036 | grn_arrow_stream_writer_add_column_null(ctx, writer); |
| 4037 | grn_arrow_stream_writer_add_column_null(ctx, writer); |
| 4038 | } |
| 4039 | } |
| 4040 | grn_arrow_stream_writer_close_record(ctx, writer); |
| 4041 | grn_arrow_stream_writer_close(ctx, writer); |
| 4042 | } |
| 4043 | |
| 4044 | static void |
| 4045 | grn_output_envelope_close_apache_arrow_trace_log(grn_ctx *ctx, grn_obj *output) |
| 4046 | { |
| 4047 | grn_arrow_stream_writer *writer = grn_arrow_stream_writer_open(ctx, output); |
| 4048 | if (!writer) { |
| 4049 | return; |
| 4050 | } |
| 4051 | |
| 4052 | grn_arrow_stream_writer_add_metadata(ctx, |
| 4053 | writer, |
| 4054 | "GROONGA:data_type", |
| 4055 | "trace_log"); |
| 4056 | grn_arrow_stream_writer_add_field(ctx, |
| 4057 | writer, |
| 4058 | "depth", |
| 4059 | grn_ctx_at(ctx, GRN_DB_UINT16)); |
| 4060 | grn_arrow_stream_writer_add_field(ctx, |
| 4061 | writer, |
| 4062 | "sequence", |
| 4063 | grn_ctx_at(ctx, GRN_DB_UINT16)); |
| 4064 | grn_arrow_stream_writer_add_field_text_dictionary( |
| 4065 | ctx, |
| 4066 | writer, |
| 4067 | "name", |
| 4068 | grn_ctx_at(ctx, GRN_DB_INT16)); |
| 4069 | enum value_type { |
| 4070 | VALUE_TYPE_UINT32, |
| 4071 | VALUE_TYPE_SHORT_TEXT, |
| 4072 | N_VALUE_TYPES, |
| 4073 | }; |
| 4074 | grn_obj *value_types[N_VALUE_TYPES]; |
| 4075 | value_types[VALUE_TYPE_UINT32] = grn_ctx_at(ctx, GRN_DB_UINT32); |
| 4076 | value_types[VALUE_TYPE_SHORT_TEXT] = grn_ctx_at(ctx, GRN_DB_SHORT_TEXT); |
| 4077 | grn_arrow_stream_writer_add_field_union(ctx, |
| 4078 | writer, |
| 4079 | "value", |
| 4080 | value_types, |
| 4081 | N_VALUE_TYPES); |
| 4082 | grn_arrow_stream_writer_add_field(ctx, |
| 4083 | writer, |
| 4084 | "elapsed_time", |
| 4085 | grn_ctx_at(ctx, GRN_DB_UINT64)); |
| 4086 | grn_arrow_stream_writer_write_schema(ctx, writer); |
| 4087 | |
| 4088 | grn_obj *depths = &(ctx->impl->trace_log.depths); |
| 4089 | grn_obj *sequences = &(ctx->impl->trace_log.sequences); |
| 4090 | grn_obj *names = &(ctx->impl->trace_log.names); |
| 4091 | grn_obj *values = &(ctx->impl->trace_log.values); |
| 4092 | grn_obj *elapsed_times = &(ctx->impl->trace_log.elapsed_times); |
| 4093 | uint32_t n_logs = grn_vector_size(ctx, names); |
| 4094 | uint32_t i; |
| 4095 | for (i = 0; i < n_logs; i++) { |
| 4096 | grn_arrow_stream_writer_open_record(ctx, writer); |
| 4097 | |
| 4098 | uint16_t depth = GRN_UINT16_VALUE_AT(depths, i); |
| 4099 | grn_arrow_stream_writer_add_column_uint16(ctx, writer, depth); |
| 4100 | |
| 4101 | uint16_t sequence = GRN_UINT16_VALUE_AT(sequences, i); |
| 4102 | grn_arrow_stream_writer_add_column_uint16(ctx, writer, sequence); |
| 4103 | |
| 4104 | const char *name; |
| 4105 | uint32_t name_size = |
| 4106 | grn_vector_get_element_float(ctx, names, i, &name, NULL, NULL); |
| 4107 | grn_arrow_stream_writer_add_column_text_dictionary(ctx, |
| 4108 | writer, |
| 4109 | name, |
| 4110 | name_size); |
| 4111 | |
| 4112 | const char *value; |
| 4113 | grn_id value_domain; |
| 4114 | uint32_t value_size = |
| 4115 | grn_vector_get_element_float(ctx, values, i, &value, NULL, &value_domain); |
| 4116 | switch (value_domain) { |
| 4117 | case GRN_DB_UINT32: |
| 4118 | { |
| 4119 | grn_arrow_stream_writer_add_column_union(ctx, |
| 4120 | writer, |
| 4121 | VALUE_TYPE_UINT32); |
| 4122 | uint32_t value_uint32 = |
| 4123 | (value_size == 0) ? 0 : *((const uint32_t *)value); |
| 4124 | grn_arrow_stream_writer_add_column_uint32(ctx, writer, value_uint32); |
| 4125 | } |
| 4126 | break; |
| 4127 | default: |
| 4128 | grn_arrow_stream_writer_add_column_union(ctx, |
| 4129 | writer, |
| 4130 | VALUE_TYPE_SHORT_TEXT); |
| 4131 | grn_arrow_stream_writer_add_column_text(ctx, writer, value, value_size); |
| 4132 | break; |
| 4133 | } |
| 4134 | |
| 4135 | uint64_t elapsed_time = GRN_UINT64_VALUE_AT(elapsed_times, i); |
| 4136 | grn_arrow_stream_writer_add_column_uint64(ctx, writer, elapsed_time); |
| 4137 | |
| 4138 | grn_arrow_stream_writer_close_record(ctx, writer); |
| 4139 | } |
| 4140 | grn_arrow_stream_writer_close(ctx, writer); |
| 4141 | } |
| 4142 | |
| 4143 | static void |
| 4144 | grn_output_envelope_close_apache_arrow(grn_ctx *ctx, |
| 4145 | grn_obj *output, |
| 4146 | grn_rc rc, |
| 4147 | double started, |
| 4148 | double elapsed, |
| 4149 | const char *file, |
| 4150 | int line) |
| 4151 | { |
| 4152 | if (ctx->impl->output.arrow_stream_writer) { |
| 4153 | grn_arrow_stream_writer_close(ctx, ctx->impl->output.arrow_stream_writer); |
| 4154 | ctx->impl->output.arrow_stream_writer = NULL; |
| 4155 | if (ctx->rc != GRN_SUCCESS) { |
| 4156 | return; |
| 4157 | } |
| 4158 | } |
| 4159 | |
| 4160 | grn_output_envelope_close_apache_arrow_metadata(ctx, |
| 4161 | output, |
| 4162 | rc, |
| 4163 | started, |
| 4164 | elapsed, |
| 4165 | file, |
| 4166 | line); |
| 4167 | if (grn_ctx_trace_log_is_enabled(ctx)) { |
| 4168 | grn_output_envelope_close_apache_arrow_trace_log(ctx, output); |
| 4169 | } |
| 4170 | } |
| 4171 | |
| 4172 | static void |
| 4173 | grn_output_envelope_apache_arrow(grn_ctx *ctx, |
| 4174 | grn_obj *head, |
| 4175 | grn_obj *body, |
| 4176 | grn_obj *foot, |
| 4177 | grn_rc rc, |
| 4178 | double started, |
| 4179 | double elapsed, |
| 4180 | const char *file, |
| 4181 | int line) |
| 4182 | { |
| 4183 | grn_output_envelope_close_apache_arrow(ctx, |
| 4184 | head, |
| 4185 | rc, |
| 4186 | started, |
| 4187 | elapsed, |
| 4188 | file, |
| 4189 | line); |
| 4190 | } |
| 4191 | |
| 4192 | void |
| 4193 | grn_output_envelope(grn_ctx *ctx, |
| 4194 | grn_rc rc, |
| 4195 | grn_obj *head, |
| 4196 | grn_obj *body, |
| 4197 | grn_obj *foot, |
| 4198 | const char *file, |
| 4199 | int line) |
| 4200 | { |
| 4201 | double started, finished, elapsed; |
| 4202 | |
| 4203 | grn_timeval tv_now; |
| 4204 | grn_timeval_now(ctx, &tv_now); |
| 4205 | started = (double)(ctx->impl->tv.tv_sec); |
| 4206 | started += ctx->impl->tv.tv_nsec / GRN_TIME_NSEC_PER_SEC_F; |
| 4207 | finished = (double)(tv_now.tv_sec); |
| 4208 | finished += tv_now.tv_nsec / GRN_TIME_NSEC_PER_SEC_F; |
| 4209 | elapsed = finished - started; |
| 4210 | |
| 4211 | switch (ctx->impl->output.type) { |
| 4212 | case GRN_CONTENT_JSON: |
| 4213 | { |
| 4214 | grn_obj *expr; |
| 4215 | grn_obj *jsonp_func = NULL; |
| 4216 | |
| 4217 | expr = ctx->impl->curr_expr; |
| 4218 | if (expr) { |
| 4219 | jsonp_func = grn_expr_get_var(ctx, |
| 4220 | expr, |
| 4221 | JSON_CALLBACK_PARAM, |
| 4222 | strlen(JSON_CALLBACK_PARAM)); |
| 4223 | } |
| 4224 | if (jsonp_func && GRN_TEXT_LEN(jsonp_func)) { |
| 4225 | GRN_TEXT_PUT(ctx, |
| 4226 | head, |
| 4227 | GRN_TEXT_VALUE(jsonp_func), |
| 4228 | GRN_TEXT_LEN(jsonp_func)); |
| 4229 | GRN_TEXT_PUTC(ctx, head, '('); |
| 4230 | } |
| 4231 | |
| 4232 | if (grn_ctx_get_command_version(ctx) <= GRN_COMMAND_VERSION_2) { |
| 4233 | grn_output_envelope_json_v1(ctx, |
| 4234 | head, |
| 4235 | body, |
| 4236 | foot, |
| 4237 | rc, |
| 4238 | started, |
| 4239 | elapsed, |
| 4240 | file, |
| 4241 | line); |
| 4242 | } else { |
| 4243 | grn_output_envelope_json(ctx, |
| 4244 | head, |
| 4245 | body, |
| 4246 | foot, |
| 4247 | rc, |
| 4248 | started, |
| 4249 | elapsed, |
| 4250 | file, |
| 4251 | line); |
| 4252 | } |
| 4253 | |
| 4254 | if (jsonp_func && GRN_TEXT_LEN(jsonp_func)) { |
| 4255 | GRN_TEXT_PUTS(ctx, foot, ");"); |
| 4256 | } |
| 4257 | } |
| 4258 | break; |
| 4259 | case GRN_CONTENT_TSV: |
| 4260 | grn_text_itoa(ctx, head, rc); |
| 4261 | GRN_TEXT_PUTC(ctx, head, '\t'); |
| 4262 | grn_text_ftoa(ctx, head, started); |
| 4263 | GRN_TEXT_PUTC(ctx, head, '\t'); |
| 4264 | grn_text_ftoa(ctx, head, elapsed); |
| 4265 | if (rc != GRN_SUCCESS) { |
| 4266 | GRN_TEXT_PUTC(ctx, head, '\t'); |
| 4267 | grn_text_esc(ctx, head, ctx->errbuf, strlen(ctx->errbuf)); |
| 4268 | if (ctx->errfunc && ctx->errfile) { |
| 4269 | /* TODO: output backtrace */ |
| 4270 | GRN_TEXT_PUTC(ctx, head, '\t'); |
| 4271 | grn_text_esc(ctx, head, ctx->errfunc, strlen(ctx->errfunc)); |
| 4272 | GRN_TEXT_PUTC(ctx, head, '\t'); |
| 4273 | grn_text_esc(ctx, head, ctx->errfile, strlen(ctx->errfile)); |
| 4274 | GRN_TEXT_PUTC(ctx, head, '\t'); |
| 4275 | grn_text_ulltoa(ctx, head, ctx->errline); |
| 4276 | } |
| 4277 | } |
| 4278 | GRN_TEXT_PUTS(ctx, head, "\n"); |
| 4279 | GRN_TEXT_PUTS(ctx, foot, "\nEND"); |
| 4280 | break; |
| 4281 | case GRN_CONTENT_XML: |
| 4282 | { |
| 4283 | char buf[GRN_TABLE_MAX_KEY_SIZE]; |
| 4284 | int is_select = 0; |
| 4285 | if (!rc && ctx->impl->curr_expr) { |
| 4286 | int len = |
| 4287 | grn_obj_name(ctx, ctx->impl->curr_expr, buf, GRN_TABLE_MAX_KEY_SIZE); |
| 4288 | buf[len] = '\0'; |
| 4289 | is_select = strcmp(buf, "select") == 0; |
| 4290 | } |
| 4291 | if (is_select) { |
| 4292 | grn_obj transformed; |
| 4293 | GRN_TEXT_INIT(&transformed, 0); |
| 4294 | transform_xml(ctx, body, &transformed); |
| 4295 | if (body->header.impl_flags & GRN_OBJ_REFER) { |
| 4296 | body->header.impl_flags = |
| 4297 | (uint8_t)(body->header.impl_flags & |
| 4298 | ~((uint8_t)GRN_OBJ_DO_SHALLOW_COPY)); |
| 4299 | } |
| 4300 | GRN_TEXT_SET(ctx, |
| 4301 | body, |
| 4302 | GRN_TEXT_VALUE(&transformed), |
| 4303 | GRN_TEXT_LEN(&transformed)); |
| 4304 | GRN_OBJ_FIN(ctx, &transformed); |
| 4305 | } else { |
| 4306 | GRN_TEXT_PUTS( |
| 4307 | ctx, |
| 4308 | head, |
| 4309 | "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<RESULT CODE=\""); |
| 4310 | grn_text_itoa(ctx, head, rc); |
| 4311 | GRN_TEXT_PUTS(ctx, head, "\" UP=\""); |
| 4312 | grn_text_ftoa(ctx, head, started); |
| 4313 | GRN_TEXT_PUTS(ctx, head, "\" ELAPSED=\""); |
| 4314 | grn_text_ftoa(ctx, head, elapsed); |
| 4315 | GRN_TEXT_PUTS(ctx, head, "\">\n"); |
| 4316 | if (rc != GRN_SUCCESS) { |
| 4317 | GRN_TEXT_PUTS(ctx, head, "<ERROR>"); |
| 4318 | grn_text_escape_xml(ctx, head, ctx->errbuf, strlen(ctx->errbuf)); |
| 4319 | if (ctx->errfunc && ctx->errfile) { |
| 4320 | /* TODO: output backtrace */ |
| 4321 | GRN_TEXT_PUTS(ctx, head, "<INFO FUNC=\""); |
| 4322 | grn_text_escape_xml(ctx, head, ctx->errfunc, strlen(ctx->errfunc)); |
| 4323 | GRN_TEXT_PUTS(ctx, head, "\" FILE=\""); |
| 4324 | grn_text_escape_xml(ctx, head, ctx->errfile, strlen(ctx->errfile)); |
| 4325 | GRN_TEXT_PUTS(ctx, head, "\" LINE=\""); |
| 4326 | grn_text_ulltoa(ctx, head, ctx->errline); |
| 4327 | GRN_TEXT_PUTS(ctx, head, "\"/>"); |
| 4328 | } |
| 4329 | GRN_TEXT_PUTS(ctx, head, "</ERROR>"); |
| 4330 | } |
| 4331 | GRN_TEXT_PUTS(ctx, foot, "\n</RESULT>"); |
| 4332 | } |
| 4333 | } |
| 4334 | break; |
| 4335 | case GRN_CONTENT_MSGPACK: |
| 4336 | #ifdef GRN_WITH_MESSAGE_PACK |
| 4337 | if (grn_ctx_get_command_version(ctx) <= GRN_COMMAND_VERSION_2) { |
| 4338 | grn_output_envelope_msgpack_v1(ctx, |
| 4339 | rc, |
| 4340 | head, |
| 4341 | body, |
| 4342 | foot, |
| 4343 | started, |
| 4344 | elapsed, |
| 4345 | file, |
| 4346 | line); |
| 4347 | } else { |
| 4348 | grn_output_envelope_msgpack(ctx, |
| 4349 | rc, |
| 4350 | head, |
| 4351 | body, |
| 4352 | foot, |
| 4353 | started, |
| 4354 | elapsed, |
| 4355 | file, |
| 4356 | line); |
| 4357 | } |
| 4358 | #endif /* GRN_WITH_MESSAGE_PACK */ |
| 4359 | break; |
| 4360 | case GRN_CONTENT_GROONGA_COMMAND_LIST: |
| 4361 | break; |
| 4362 | case GRN_CONTENT_APACHE_ARROW: |
| 4363 | grn_output_envelope_apache_arrow(ctx, |
| 4364 | head, |
| 4365 | body, |
| 4366 | foot, |
| 4367 | rc, |
| 4368 | started, |
| 4369 | elapsed, |
| 4370 | file, |
| 4371 | line); |
| 4372 | break; |
| 4373 | case GRN_CONTENT_NONE: |
| 4374 | break; |
| 4375 | } |
| 4376 | } |
| 4377 | |
| 4378 | void |
| 4379 | grn_output_envelope_open(grn_ctx *ctx, grn_obj *output) |
| 4380 | { |
| 4381 | switch (ctx->impl->output.type) { |
| 4382 | case GRN_CONTENT_JSON: |
| 4383 | { |
| 4384 | grn_obj *expr; |
| 4385 | grn_obj *jsonp_func = NULL; |
| 4386 | |
| 4387 | expr = ctx->impl->curr_expr; |
| 4388 | if (expr) { |
| 4389 | jsonp_func = grn_expr_get_var(ctx, |
| 4390 | expr, |
| 4391 | JSON_CALLBACK_PARAM, |
| 4392 | strlen(JSON_CALLBACK_PARAM)); |
| 4393 | } |
| 4394 | if (jsonp_func && GRN_TEXT_LEN(jsonp_func) > 0) { |
| 4395 | GRN_TEXT_PUT(ctx, |
| 4396 | output, |
| 4397 | GRN_TEXT_VALUE(jsonp_func), |
| 4398 | GRN_TEXT_LEN(jsonp_func)); |
| 4399 | GRN_TEXT_PUTC(ctx, output, '('); |
| 4400 | } |
| 4401 | |
| 4402 | bool is_stream_mode = true; |
| 4403 | bool have_body = true; |
| 4404 | if (grn_ctx_get_command_version(ctx) <= GRN_COMMAND_VERSION_2) { |
| 4405 | grn_output_envelope_open_json_v1(ctx, |
| 4406 | output, |
| 4407 | is_stream_mode, |
| 4408 | have_body); |
| 4409 | } else { |
| 4410 | grn_output_envelope_open_json(ctx, output, is_stream_mode, have_body); |
| 4411 | } |
| 4412 | } |
| 4413 | break; |
| 4414 | case GRN_CONTENT_TSV: |
| 4415 | case GRN_CONTENT_XML: |
| 4416 | case GRN_CONTENT_MSGPACK: |
| 4417 | case GRN_CONTENT_GROONGA_COMMAND_LIST: |
| 4418 | case GRN_CONTENT_APACHE_ARROW: |
| 4419 | case GRN_CONTENT_NONE: |
| 4420 | break; |
| 4421 | } |
| 4422 | } |
| 4423 | |
| 4424 | void |
| 4425 | grn_output_envelope_close( |
| 4426 | grn_ctx *ctx, grn_obj *output, grn_rc rc, const char *file, int line) |
| 4427 | { |
| 4428 | double started, finished, elapsed; |
| 4429 | |
| 4430 | grn_timeval tv_now; |
| 4431 | grn_timeval_now(ctx, &tv_now); |
| 4432 | started = (double)(ctx->impl->tv.tv_sec); |
| 4433 | started += ctx->impl->tv.tv_nsec / GRN_TIME_NSEC_PER_SEC_F; |
| 4434 | finished = (double)(tv_now.tv_sec); |
| 4435 | finished += tv_now.tv_nsec / GRN_TIME_NSEC_PER_SEC_F; |
| 4436 | elapsed = finished - started; |
| 4437 | |
| 4438 | switch (ctx->impl->output.type) { |
| 4439 | case GRN_CONTENT_JSON: |
| 4440 | { |
| 4441 | grn_obj *expr; |
| 4442 | grn_obj *jsonp_func = NULL; |
| 4443 | |
| 4444 | expr = ctx->impl->curr_expr; |
| 4445 | if (expr) { |
| 4446 | jsonp_func = grn_expr_get_var(ctx, |
| 4447 | expr, |
| 4448 | JSON_CALLBACK_PARAM, |
| 4449 | strlen(JSON_CALLBACK_PARAM)); |
| 4450 | } |
| 4451 | |
| 4452 | bool is_stream_mode = true; |
| 4453 | bool have_body = true; |
| 4454 | if (grn_ctx_get_command_version(ctx) <= GRN_COMMAND_VERSION_2) { |
| 4455 | grn_output_envelope_close_json_v1(ctx, |
| 4456 | output, |
| 4457 | output, |
| 4458 | is_stream_mode, |
| 4459 | have_body, |
| 4460 | rc, |
| 4461 | started, |
| 4462 | elapsed, |
| 4463 | file, |
| 4464 | line); |
| 4465 | } else { |
| 4466 | grn_output_envelope_close_json(ctx, |
| 4467 | output, |
| 4468 | output, |
| 4469 | is_stream_mode, |
| 4470 | have_body, |
| 4471 | rc, |
| 4472 | started, |
| 4473 | elapsed, |
| 4474 | file, |
| 4475 | line); |
| 4476 | } |
| 4477 | |
| 4478 | if (jsonp_func && GRN_TEXT_LEN(jsonp_func) > 0) { |
| 4479 | GRN_TEXT_PUTS(ctx, output, ");"); |
| 4480 | } |
| 4481 | } |
| 4482 | break; |
| 4483 | case GRN_CONTENT_TSV: |
| 4484 | case GRN_CONTENT_XML: |
| 4485 | case GRN_CONTENT_MSGPACK: |
| 4486 | case GRN_CONTENT_GROONGA_COMMAND_LIST: |
| 4487 | break; |
| 4488 | case GRN_CONTENT_APACHE_ARROW: |
| 4489 | grn_output_envelope_close_apache_arrow(ctx, |
| 4490 | output, |
| 4491 | rc, |
| 4492 | started, |
| 4493 | elapsed, |
| 4494 | file, |
| 4495 | line); |
| 4496 | break; |
| 4497 | case GRN_CONTENT_NONE: |
| 4498 | break; |
| 4499 | } |
| 4500 | } |
| 4501 | |
| 4502 | grn_rc |
| 4503 | grn_obj_format_fin(grn_ctx *ctx, grn_obj_format *format) |
| 4504 | { |
| 4505 | GRN_API_ENTER; |
| 4506 | |
| 4507 | size_t n_columns = GRN_PTR_VECTOR_SIZE(&(format->columns)); |
| 4508 | size_t i; |
| 4509 | for (i = 0; i < n_columns; i++) { |
| 4510 | grn_obj *column = GRN_PTR_VALUE_AT(&(format->columns), i); |
| 4511 | if (grn_is_reference_count_enable() || |
| 4512 | column->header.type == GRN_ACCESSOR) { |
| 4513 | grn_obj_unlink(ctx, column); |
| 4514 | } |
| 4515 | } |
| 4516 | GRN_OBJ_FIN(ctx, &(format->columns)); |
| 4517 | if (format->expression) { |
| 4518 | GRN_OBJ_FIN(ctx, format->expression); |
| 4519 | } |
| 4520 | |
| 4521 | GRN_API_RETURN(ctx->rc); |
| 4522 | } |
| 4523 | |
| 4524 | grn_rc |
| 4525 | grn_obj_format_set_columns(grn_ctx *ctx, |
| 4526 | grn_obj_format *format, |
| 4527 | grn_obj *table, |
| 4528 | const char *columns, |
| 4529 | unsigned int columns_len) |
| 4530 | { |
| 4531 | grn_rc rc; |
| 4532 | |
| 4533 | if (grn_expr_is_v1_format(ctx, |
| 4534 | columns, |
| 4535 | columns_len, |
| 4536 | GRN_EXPR_V1_FORMAT_TYPE_OUTPUT_COLUMNS)) { |
| 4537 | rc = grn_obj_columns(ctx, table, columns, columns_len, &(format->columns)); |
| 4538 | } else { |
| 4539 | format->expression = |
| 4540 | grn_output_columns_parse(ctx, table, columns, columns_len); |
| 4541 | rc = ctx->rc; |
| 4542 | } |
| 4543 | |
| 4544 | return rc; |
| 4545 | } |